1
0
mirror of https://github.com/chylex/IntelliJ-IdeaVim.git synced 2025-05-07 21:34:09 +02:00

Add power functions

This commit is contained in:
Matt Ellis 2025-02-19 01:52:46 +00:00 committed by Alex Pláte
parent 613a8b5792
commit d1127a5238
4 changed files with 188 additions and 0 deletions
src/test/java/org/jetbrains/plugins/ideavim/ex/implementation/functions/floatFunctions
vim-engine/src/main
kotlin/com/maddyhome/idea/vim/vimscript/model/functions/handlers/floatFunctions
resources/ksp-generated

View File

@ -0,0 +1,98 @@
/*
* Copyright 2003-2025 The IdeaVim authors
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE.txt file or at
* https://opensource.org/licenses/MIT.
*/
package org.jetbrains.plugins.ideavim.ex.implementation.functions.floatFunctions
import org.jetbrains.plugins.ideavim.VimTestCase
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInfo
class PowFunctionTest : VimTestCase() {
@BeforeEach
override fun setUp(testInfo: TestInfo) {
super.setUp(testInfo)
configureByText("\n")
}
@Test
fun `test pow with Number`() {
assertCommandOutput("echo pow(3, 3) pow(2, 16)", "27.0 65536.0")
}
@Test
fun `test pow with Float`() {
assertCommandOutput("echo pow(32, 0.2)", "2.0")
}
@Test
fun `test pow with negative number`() {
assertCommandOutput("echo pow(-2, 3)", "-8.0")
}
@Test
fun `test pow with negative number for exponent`() {
assertCommandOutput("echo pow(2, -3)", "0.125")
}
@Test
fun `test pow with string causes errors`() {
enterCommand("echo pow('1.0', 2)")
assertPluginError(true)
assertPluginErrorMessageContains("E808: Number or Float required")
}
@Test
fun `test pow with string causes errors 2`() {
enterCommand("echo pow(1.0, '2')")
assertPluginError(true)
assertPluginErrorMessageContains("E808: Number or Float required")
}
@Test
fun `test pow with invalid string causes errors`() {
enterCommand("echo pow('cheese', 2)")
assertPluginError(true)
assertPluginErrorMessageContains("E808: Number or Float required")
}
@Test
fun `test pow with invalid string causes errors 2`() {
enterCommand("echo pow(2, 'cheese')")
assertPluginError(true)
assertPluginErrorMessageContains("E808: Number or Float required")
}
@Test
fun `test pow with list causes errors`() {
enterCommand("echo pow([1.0], 2)")
assertPluginError(true)
assertPluginErrorMessageContains("E808: Number or Float required")
}
@Test
fun `test pow with list causes errors 2`() {
enterCommand("echo pow(2, [1.0])")
assertPluginError(true)
assertPluginErrorMessageContains("E808: Number or Float required")
}
@Test
fun `test pow with dictionary causes errors`() {
enterCommand("echo pow({1: 1.0}, 2)")
assertPluginError(true)
assertPluginErrorMessageContains("E808: Number or Float required")
}
@Test
fun `test pow with dictionary causes errors 2`() {
enterCommand("echo pow(2, {1: 1.0})")
assertPluginError(true)
assertPluginErrorMessageContains("E808: Number or Float required")
}
}

View File

@ -0,0 +1,65 @@
/*
* Copyright 2003-2025 The IdeaVim authors
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE.txt file or at
* https://opensource.org/licenses/MIT.
*/
package org.jetbrains.plugins.ideavim.ex.implementation.functions.floatFunctions
import org.jetbrains.plugins.ideavim.VimTestCase
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInfo
class SqrtFunctionTest : VimTestCase() {
@BeforeEach
override fun setUp(testInfo: TestInfo) {
super.setUp(testInfo)
configureByText("\n")
}
@Test
fun `test sqrt with integer value`() {
assertCommandOutput("echo sqrt(100)", "10.0")
}
@Test
fun `test sqrt with float value`() {
assertCommandOutput("echo sqrt(4.0)", "2.0")
}
@Test
fun `test sqrt with negative float value`() {
assertCommandOutput("echo sqrt(-4.01)", "nan")
}
@Test
fun `test sqrt with string causes errors`() {
enterCommand("echo sqrt('1.0')")
assertPluginError(true)
assertPluginErrorMessageContains("E808: Number or Float required")
}
@Test
fun `test sqrt with invalid string causes errors`() {
enterCommand("echo sqrt('cheese')")
assertPluginError(true)
assertPluginErrorMessageContains("E808: Number or Float required")
}
@Test
fun `test sqrt with list causes errors`() {
enterCommand("echo sqrt([1.0])")
assertPluginError(true)
assertPluginErrorMessageContains("E808: Number or Float required")
}
@Test
fun `test sqrt with dictionary causes errors`() {
enterCommand("echo sqrt({1: 1.0})")
assertPluginError(true)
assertPluginErrorMessageContains("E808: Number or Float required")
}
}

View File

@ -0,0 +1,23 @@
/*
* Copyright 2003-2025 The IdeaVim authors
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE.txt file or at
* https://opensource.org/licenses/MIT.
*/
package com.maddyhome.idea.vim.vimscript.model.functions.handlers.floatFunctions
import com.intellij.vim.annotations.VimscriptFunction
import kotlin.math.pow
import kotlin.math.sqrt
@VimscriptFunction("pow")
internal class PowFunctionHandler : BinaryFloatFunctionHandlerBase() {
override fun invoke(arg1: Double, arg2: Double) = arg1.pow(arg2)
}
@VimscriptFunction("sqrt")
internal class SqrtFunctionHandler : UnaryFloatFunctionHandlerBase() {
override fun invoke(argument: Double) = sqrt(argument)
}

View File

@ -25,10 +25,12 @@
"log": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.floatFunctions.LogFunctionHandler",
"log10": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.floatFunctions.Log10FunctionHandler",
"or": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.bitwiseFunctions.OrFunctionHandler",
"pow": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.floatFunctions.PowFunctionHandler",
"round": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.floatFunctions.RoundFunctionHandler",
"sin": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.floatFunctions.SinFunctionHandler",
"sinh": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.floatFunctions.SinhFunctionHandler",
"split": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.listFunctions.SplitFunctionHandler",
"sqrt": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.floatFunctions.SqrtFunctionHandler",
"submatch": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.stringFunctions.SubmatchFunctionHandler",
"tan": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.floatFunctions.TanFunctionHandler",
"tanh": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.floatFunctions.TanhFunctionHandler",