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

Fix(VIM-3827): Fix an incorrect implementation of the >= operator

As a note: This is the first bug in IdeaVim that was fixed by AI (with manual verification, of course).
This commit is contained in:
Alex Plate 2025-03-10 10:50:26 +02:00
parent 55257e1017
commit f76b07521e
No known key found for this signature in database
GPG Key ID: 0B97153C8FFEC09F
4 changed files with 54 additions and 5 deletions
src/test/java/org/jetbrains/plugins/ideavim/ex/implementation/expressions/operators
vim-engine/src/main/kotlin/com/maddyhome/idea/vim/vimscript/model/expressions/operators/handlers/binary

View File

@ -0,0 +1,49 @@
/*
* 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.expressions.operators
import com.maddyhome.idea.vim.vimscript.model.datatypes.VimInt
import com.maddyhome.idea.vim.vimscript.parser.VimscriptParser
import org.jetbrains.plugins.ideavim.VimTestCase
import org.jetbrains.plugins.ideavim.ex.evaluate
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
class GreaterOrEqualsOperatorTest : VimTestCase() {
@Test
fun `test equal numbers`() {
assertEquals(VimInt(1), VimscriptParser.parseExpression("1 >= 1")!!.evaluate())
}
@Test
fun `test greater number`() {
assertEquals(VimInt(1), VimscriptParser.parseExpression("2 >= 1")!!.evaluate())
}
@Test
fun `test smaller number`() {
assertEquals(VimInt(0), VimscriptParser.parseExpression("1 >= 2")!!.evaluate())
}
@Test
fun `test equal strings`() {
assertEquals(VimInt(1), VimscriptParser.parseExpression("'abc' >= 'abc'")!!.evaluate())
}
@Test
fun `test greater string`() {
assertEquals(VimInt(1), VimscriptParser.parseExpression("'def' >= 'abc'")!!.evaluate())
}
@Test
fun `test smaller string`() {
assertEquals(VimInt(0), VimscriptParser.parseExpression("'abc' >= 'def'")!!.evaluate())
}
}

View File

@ -16,9 +16,9 @@ object GreaterOrEqualsCaseSensitiveHandler : BinaryOperatorHandler() {
override fun performOperation(left: VimDataType, right: VimDataType): VimDataType {
return if (left is VimString || right is VimString) {
VimInt(if (left.asString() < right.asString()) 0 else 1)
VimInt(if (left.asString() >= right.asString()) 1 else 0)
} else {
VimInt(if (left.asDouble() < right.asDouble()) 0 else 1)
VimInt(if (left.asDouble() >= right.asDouble()) 1 else 0)
}
}
}

View File

@ -9,4 +9,4 @@
package com.maddyhome.idea.vim.vimscript.model.expressions.operators.handlers.binary
object GreaterOrEqualsHandler :
BinaryOperatorWithIgnoreCaseOption(GreaterIgnoreCaseHandler, GreaterCaseSensitiveHandler)
BinaryOperatorWithIgnoreCaseOption(GreaterOrEqualsIgnoreCaseHandler, GreaterOrEqualsCaseSensitiveHandler)

View File

@ -16,9 +16,9 @@ object GreaterOrEqualsIgnoreCaseHandler : BinaryOperatorHandler() {
override fun performOperation(left: VimDataType, right: VimDataType): VimDataType {
return if (left is VimString || right is VimString) {
VimInt(if (left.asString().compareTo(right.asString(), ignoreCase = true) < 0) 0 else 1)
VimInt(if (left.asString().compareTo(right.asString(), ignoreCase = true) >= 0) 1 else 0)
} else {
VimInt(if (left.asDouble() < right.asDouble()) 0 else 1)
VimInt(if (left.asDouble() >= right.asDouble()) 1 else 0)
}
}
}