mirror of
https://github.com/chylex/IntelliJ-IdeaVim.git
synced 2025-05-12 00:34:06 +02:00
Add rounding functions
This commit is contained in:
parent
3b96e2eacd
commit
6f63371e9b
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
@ -0,0 +1,66 @@
|
||||
/*
|
||||
* 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 CeilFunctionTest : VimTestCase() {
|
||||
@BeforeEach
|
||||
override fun setUp(testInfo: TestInfo) {
|
||||
super.setUp(testInfo)
|
||||
configureByText("\n")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test ceil rounds up float value`() {
|
||||
assertCommandOutput("echo ceil(1.456)", "2.0")
|
||||
assertCommandOutput("echo ceil(4.0)", "4.0")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test ceil returns integer value as float value`() {
|
||||
assertCommandOutput("echo ceil(1)", "1.0")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test ceil with negative float value`() {
|
||||
assertCommandOutput("echo ceil(-5.456)", "-5.0")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test ceil with string causes errors`() {
|
||||
enterCommand("echo ceil('1.0')")
|
||||
assertPluginError(true)
|
||||
assertPluginErrorMessageContains("E808: Number or Float required")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test ceil with invalid string causes errors`() {
|
||||
enterCommand("echo ceil('cheese')")
|
||||
assertPluginError(true)
|
||||
assertPluginErrorMessageContains("E808: Number or Float required")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test ceil with list causes errors`() {
|
||||
enterCommand("echo ceil([1.0])")
|
||||
assertPluginError(true)
|
||||
assertPluginErrorMessageContains("E808: Number or Float required")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test ceil with dictionary causes errors`() {
|
||||
enterCommand("echo ceil({1: 1.0})")
|
||||
assertPluginError(true)
|
||||
assertPluginErrorMessageContains("E808: Number or Float required")
|
||||
}
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
/*
|
||||
* 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 FloorFunctionTest : VimTestCase() {
|
||||
@BeforeEach
|
||||
override fun setUp(testInfo: TestInfo) {
|
||||
super.setUp(testInfo)
|
||||
configureByText("\n")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test floor rounds down float value`() {
|
||||
assertCommandOutput("echo floor(1.856)", "1.0")
|
||||
assertCommandOutput("echo floor(4.0)", "4.0")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test floor returns integer value as float value`() {
|
||||
assertCommandOutput("echo floor(1)", "1.0")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test floor with negative float value`() {
|
||||
assertCommandOutput("echo floor(-5.456)", "-6.0")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test floor with string causes errors`() {
|
||||
enterCommand("echo floor('1.0')")
|
||||
assertPluginError(true)
|
||||
assertPluginErrorMessageContains("E808: Number or Float required")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test floor with invalid string causes errors`() {
|
||||
enterCommand("echo floor('cheese')")
|
||||
assertPluginError(true)
|
||||
assertPluginErrorMessageContains("E808: Number or Float required")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test floor with list causes errors`() {
|
||||
enterCommand("echo floor([1.0])")
|
||||
assertPluginError(true)
|
||||
assertPluginErrorMessageContains("E808: Number or Float required")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test floor with dictionary causes errors`() {
|
||||
enterCommand("echo floor({1: 1.0})")
|
||||
assertPluginError(true)
|
||||
assertPluginErrorMessageContains("E808: Number or Float required")
|
||||
}
|
||||
}
|
@ -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 RoundFunctionTest : VimTestCase() {
|
||||
@BeforeEach
|
||||
override fun setUp(testInfo: TestInfo) {
|
||||
super.setUp(testInfo)
|
||||
configureByText("\n")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test round rounds float value to nearest integer`() {
|
||||
assertCommandOutput("echo round(0.456) round(4.5)", "0.0 5.0")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test round returns integer value as float value`() {
|
||||
assertCommandOutput("echo round(1)", "1.0")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test round with negative float value`() {
|
||||
assertCommandOutput("echo round(-4.5)", "-5.0")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test round with string causes errors`() {
|
||||
enterCommand("echo round('1.0')")
|
||||
assertPluginError(true)
|
||||
assertPluginErrorMessageContains("E808: Number or Float required")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test round with invalid string causes errors`() {
|
||||
enterCommand("echo round('cheese')")
|
||||
assertPluginError(true)
|
||||
assertPluginErrorMessageContains("E808: Number or Float required")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test round with list causes errors`() {
|
||||
enterCommand("echo round([1.0])")
|
||||
assertPluginError(true)
|
||||
assertPluginErrorMessageContains("E808: Number or Float required")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test round with dictionary causes errors`() {
|
||||
enterCommand("echo round({1: 1.0})")
|
||||
assertPluginError(true)
|
||||
assertPluginErrorMessageContains("E808: Number or Float required")
|
||||
}
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
/*
|
||||
* 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 TruncFunctionTest : VimTestCase() {
|
||||
@BeforeEach
|
||||
override fun setUp(testInfo: TestInfo) {
|
||||
super.setUp(testInfo)
|
||||
configureByText("\n")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test trunc truncates float value`() {
|
||||
assertCommandOutput("echo trunc(1.456)", "1.0")
|
||||
assertCommandOutput("echo trunc(4.0)", "4.0")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test trunc returns integer value as float value`() {
|
||||
assertCommandOutput("echo trunc(1)", "1.0")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test trunc with negative float value`() {
|
||||
assertCommandOutput("echo trunc(-5.456)", "-5.0")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test trunc with string causes errors`() {
|
||||
enterCommand("echo trunc('1.0')")
|
||||
assertPluginError(true)
|
||||
assertPluginErrorMessageContains("E808: Number or Float required")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test trunc with invalid string causes errors`() {
|
||||
enterCommand("echo trunc('cheese')")
|
||||
assertPluginError(true)
|
||||
assertPluginErrorMessageContains("E808: Number or Float required")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test trunc with list causes errors`() {
|
||||
enterCommand("echo trunc([1.0])")
|
||||
assertPluginError(true)
|
||||
assertPluginErrorMessageContains("E808: Number or Float required")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test trunc with dictionary causes errors`() {
|
||||
enterCommand("echo trunc({1: 1.0})")
|
||||
assertPluginError(true)
|
||||
assertPluginErrorMessageContains("E808: Number or Float required")
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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.ceil
|
||||
import kotlin.math.floor
|
||||
import kotlin.math.truncate
|
||||
|
||||
@VimscriptFunction("ceil")
|
||||
internal class CeilFunctionHandler : UnaryFloatFunctionHandlerBase() {
|
||||
override fun invoke(argument: Double) = ceil(argument)
|
||||
}
|
||||
|
||||
@VimscriptFunction("floor")
|
||||
internal class FloorFunctionHandler : UnaryFloatFunctionHandlerBase() {
|
||||
override fun invoke(argument: Double) = floor(argument)
|
||||
}
|
||||
|
||||
@VimscriptFunction("round")
|
||||
internal class RoundFunctionHandler : UnaryFloatFunctionHandlerBase() {
|
||||
// kotlin.math.round does bankers' rounding
|
||||
override fun invoke(argument: Double) = if (argument >= 0) {
|
||||
floor(argument + 0.5)
|
||||
} else {
|
||||
-floor(-argument + 0.5)
|
||||
}
|
||||
}
|
||||
|
||||
@VimscriptFunction("trunc")
|
||||
internal class TruncFunctionHandler : UnaryFloatFunctionHandlerBase() {
|
||||
override fun invoke(argument: Double) = truncate(argument)
|
||||
}
|
@ -5,12 +5,14 @@
|
||||
"asin": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.floatFunctions.AsinFunctionHandler",
|
||||
"atan": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.floatFunctions.AtanFunctionHandler",
|
||||
"atan2": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.floatFunctions.Atan2FunctionHandler",
|
||||
"ceil": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.floatFunctions.CeilFunctionHandler",
|
||||
"col": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.cursorFunctions.ColFunctionHandler",
|
||||
"cos": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.floatFunctions.CosFunctionHandler",
|
||||
"cosh": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.floatFunctions.CoshFunctionHandler",
|
||||
"empty": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.EmptyFunctionHandler",
|
||||
"escape": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.stringFunctions.EscapeFunctionHandler",
|
||||
"exists": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.variousFunctions.ExistsFunctionHandler",
|
||||
"floor": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.floatFunctions.FloorFunctionHandler",
|
||||
"funcref": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.varFunctions.FuncrefFunctionHandler",
|
||||
"function": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.varFunctions.FunctionFunctionHandler",
|
||||
"get": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.GetFunctionHandler",
|
||||
@ -20,6 +22,7 @@
|
||||
"len": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.LenFunctionHandler",
|
||||
"line": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.cursorFunctions.LineFunctionHandler",
|
||||
"or": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.bitwiseFunctions.OrFunctionHandler",
|
||||
"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",
|
||||
@ -28,5 +31,6 @@
|
||||
"tanh": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.floatFunctions.TanhFunctionHandler",
|
||||
"tolower": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.stringFunctions.TolowerFunctionHandler",
|
||||
"toupper": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.stringFunctions.ToupperFunctionHandler",
|
||||
"trunc": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.floatFunctions.TruncFunctionHandler",
|
||||
"xor": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.bitwiseFunctions.XorFunctionHandler"
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user