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

Migrate java tests to VimNoWriteActionTestCase

This commit is contained in:
Alex Plate 2025-02-20 18:46:50 +02:00
parent 96a1456dcd
commit e18035b729
No known key found for this signature in database
GPG Key ID: 0B97153C8FFEC09F
10 changed files with 162 additions and 75 deletions
src/testFixtures/kotlin/org/jetbrains/plugins/ideavim
tests/java-tests/src/test/kotlin/org/jetbrains/plugins/ideavim

View File

@ -8,7 +8,6 @@
package org.jetbrains.plugins.ideavim
import com.intellij.testFramework.LoggedErrorProcessor
import com.intellij.testFramework.TestLoggerFactory.TestLoggerAssertionError
import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.api.fail
@ -32,7 +31,7 @@ object OnlyThrowLoggedErrorProcessor : LoggedErrorProcessor() {
* Asserts that [T] was thrown via `LOG.error("message", e)` call where `e` has a type of [T].
*/
inline fun <reified T : Throwable> assertThrowsLogError(crossinline action: () -> Unit): T {
val exception = assertThrows<TestLoggerAssertionError> {
val exception = assertThrows<Throwable> {
LoggedErrorProcessor.executeWith<Throwable>(OnlyThrowLoggedErrorProcessor) {
action()
}

View File

@ -20,6 +20,7 @@ import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.actionSystem.PlatformCoreDataKeys
import com.intellij.openapi.actionSystem.ex.ActionUtil
import com.intellij.openapi.actionSystem.impl.SimpleDataContext
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.PathManager
import com.intellij.openapi.application.WriteAction
import com.intellij.openapi.diagnostic.thisLogger
@ -350,9 +351,15 @@ abstract class VimNoWriteActionTestCase {
protected fun configureByText(fileType: FileType, content: String): Editor {
fixture.configureByText(fileType, content)
setDefaultIntelliJSettings(fixture.editor)
// Note: Change to coroutines
ApplicationManager.getApplication().invokeAndWait {
setDefaultIntelliJSettings(fixture.editor)
}
NeovimTesting.setupEditor(fixture.editor, testInfo)
setEditorVisibleSize(screenWidth, screenHeight)
// Note: Change to coroutines
ApplicationManager.getApplication().invokeAndWait {
setEditorVisibleSize(screenWidth, screenHeight)
}
return fixture.editor
}
@ -444,7 +451,11 @@ abstract class VimNoWriteActionTestCase {
protected fun typeText(vararg keys: String) = typeText(keys.flatMap { injector.parser.parseKeys(it) })
protected fun typeText(keys: List<KeyStroke?>): Editor {
return typeText(fixture.editor, keys)
var editor: Editor? = null
ApplicationManager.getApplication().invokeAndWait {
editor = typeText(fixture.editor, keys)
}
return editor!!
}
protected fun typeText(editor: Editor, keys: List<KeyStroke?>): Editor {
@ -601,8 +612,10 @@ abstract class VimNoWriteActionTestCase {
)
}
assertEquals(expectedOffsets.size, carets.size, "Wrong amount of carets")
for (i in expectedOffsets.indices) {
assertEquals(expectedOffsets[i], carets[i].offset)
ApplicationManager.getApplication().runReadAction {
for (i in expectedOffsets.indices) {
assertEquals(expectedOffsets[i], carets[i].offset)
}
}
NeovimTesting.assertState(fixture.editor, testInfo)
@ -700,7 +713,12 @@ abstract class VimNoWriteActionTestCase {
}
fun assertSelection(expected: String?) {
val selected = fixture.editor.selectionModel.selectedText
var selected: String? = null
ApplicationManager.getApplication().invokeAndWait {
ApplicationManager.getApplication().runReadAction {
selected = fixture.editor.selectionModel.selectedText
}
}
assertEquals(expected, selected)
}
@ -890,7 +908,9 @@ abstract class VimNoWriteActionTestCase {
protected fun performTest(keys: String, after: String, modeAfter: Mode) {
typeText(keys)
PlatformTestUtil.dispatchAllInvocationEventsInIdeEventQueue()
ApplicationManager.getApplication().invokeAndWait {
PlatformTestUtil.dispatchAllInvocationEventsInIdeEventQueue()
}
assertState(after)
assertState(modeAfter)
}

View File

@ -9,6 +9,7 @@
package org.jetbrains.plugins.ideavim
import com.intellij.ide.IdeEventQueue
import com.intellij.openapi.application.ApplicationManager
import com.intellij.testFramework.fixtures.CodeInsightTestFixture
import com.intellij.util.containers.toArray
import com.maddyhome.idea.vim.api.injector
@ -55,14 +56,16 @@ annotation class VimBehaviorDiffers(
val shouldBeFixed: Boolean = true,
)
inline fun waitAndAssert(timeInMillis: Int = 1000, condition: () -> Boolean) {
val end = System.currentTimeMillis() + timeInMillis
while (end > System.currentTimeMillis()) {
Thread.sleep(10)
IdeEventQueue.getInstance().flushQueue()
if (condition()) return
inline fun waitAndAssert(timeInMillis: Int = 1000, crossinline condition: () -> Boolean) {
ApplicationManager.getApplication().invokeAndWait {
val end = System.currentTimeMillis() + timeInMillis
while (end > System.currentTimeMillis()) {
Thread.sleep(10)
IdeEventQueue.getInstance().flushQueue()
if (condition()) return@invokeAndWait
}
fail()
}
fail()
}
fun assertHappened(timeInMillis: Int = 1000, precision: Int, condition: () -> Boolean) {
@ -72,14 +75,16 @@ fun assertHappened(timeInMillis: Int = 1000, precision: Int, condition: () -> Bo
}
fun assertDoesntChange(timeInMillis: Int = 1000, condition: () -> Boolean) {
val end = System.currentTimeMillis() + timeInMillis
while (end > System.currentTimeMillis()) {
if (!condition()) {
fail()
}
ApplicationManager.getApplication().invokeAndWait {
val end = System.currentTimeMillis() + timeInMillis
while (end > System.currentTimeMillis()) {
if (!condition()) {
fail()
}
Thread.sleep(10)
IdeEventQueue.getInstance().flushQueue()
Thread.sleep(10)
IdeEventQueue.getInstance().flushQueue()
}
}
}

View File

@ -10,6 +10,6 @@ package org.jetbrains.plugins.ideavim
import com.intellij.ide.highlighter.JavaFileType
abstract class VimJavaTestCase : VimTestCase() {
abstract class VimJavaTestCase : VimNoWriteActionTestCase() {
protected fun configureByJavaText(content: String) = configureByText(JavaFileType.INSTANCE, content)
}

View File

@ -10,6 +10,7 @@ package org.jetbrains.plugins.ideavim.action
import com.intellij.codeInsight.folding.CodeFoldingManager
import com.intellij.codeInsight.folding.impl.FoldingUtil
import com.intellij.openapi.application.ApplicationManager
import com.maddyhome.idea.vim.api.injector
import org.jetbrains.plugins.ideavim.SkipNeovimReason
import org.jetbrains.plugins.ideavim.TestWithoutNeovim
@ -135,12 +136,24 @@ and some text after""",
and some text after
""".trimIndent(),
)
CodeFoldingManager.getInstance(fixture.project).updateFoldRegions(fixture.editor)
assertEquals(FoldingUtil.findFoldRegionStartingAtLine(fixture.editor, 0)!!.isExpanded, true)
ApplicationManager.getApplication().invokeAndWait {
ApplicationManager.getApplication().runWriteAction {
CodeFoldingManager.getInstance(fixture.project).updateFoldRegions(fixture.editor)
assertEquals(FoldingUtil.findFoldRegionStartingAtLine(fixture.editor, 0)!!.isExpanded, true)
}
}
typeText(injector.parser.parseKeys("za"))
assertEquals(FoldingUtil.findFoldRegionStartingAtLine(fixture.editor, 0)!!.isExpanded, false)
ApplicationManager.getApplication().invokeAndWait {
ApplicationManager.getApplication().runWriteAction {
assertEquals(FoldingUtil.findFoldRegionStartingAtLine(fixture.editor, 0)!!.isExpanded, false)
}
}
typeText(injector.parser.parseKeys("za"))
assertEquals(FoldingUtil.findFoldRegionStartingAtLine(fixture.editor, 0)!!.isExpanded, true)
ApplicationManager.getApplication().invokeAndWait {
ApplicationManager.getApplication().runWriteAction {
assertEquals(FoldingUtil.findFoldRegionStartingAtLine(fixture.editor, 0)!!.isExpanded, true)
}
}
}
// VIM-287 |zc| |o|
@ -158,9 +171,11 @@ and some text after""",
""".trimIndent(),
)
fixture.editor.foldingModel.runBatchFoldingOperation {
CodeFoldingManager.getInstance(fixture.project).updateFoldRegions(fixture.editor)
FoldingUtil.findFoldRegionStartingAtLine(fixture.editor, 0)!!.isExpanded = false
ApplicationManager.getApplication().invokeAndWait {
fixture.editor.foldingModel.runBatchFoldingOperation {
CodeFoldingManager.getInstance(fixture.project).updateFoldRegions(fixture.editor)
FoldingUtil.findFoldRegionStartingAtLine(fixture.editor, 0)!!.isExpanded = false
}
}
typeText(injector.parser.parseKeys("o"))

View File

@ -10,6 +10,7 @@ package org.jetbrains.plugins.ideavim.action.change.insert
import com.intellij.codeInsight.folding.CodeFoldingManager
import com.intellij.codeInsight.folding.impl.FoldingUtil
import com.intellij.openapi.application.ApplicationManager
import com.maddyhome.idea.vim.api.injector
import org.jetbrains.plugins.ideavim.SkipNeovimReason
import org.jetbrains.plugins.ideavim.TestWithoutNeovim
@ -30,9 +31,11 @@ bar
""",
)
fixture.editor.foldingModel.runBatchFoldingOperation {
CodeFoldingManager.getInstance(fixture.project).updateFoldRegions(fixture.editor)
FoldingUtil.findFoldRegionStartingAtLine(fixture.editor, 0)!!.isExpanded = false
ApplicationManager.getApplication().invokeAndWait {
fixture.editor.foldingModel.runBatchFoldingOperation {
CodeFoldingManager.getInstance(fixture.project).updateFoldRegions(fixture.editor)
FoldingUtil.findFoldRegionStartingAtLine(fixture.editor, 0)!!.isExpanded = false
}
}
typeText(injector.parser.parseKeys("j" + "<C-V>" + "j" + "I" + "X" + "<Esc>"))

View File

@ -8,6 +8,7 @@
package org.jetbrains.plugins.ideavim.action.motion.search
import com.intellij.openapi.application.ApplicationManager
import com.maddyhome.idea.vim.api.getVisualLineCount
import com.maddyhome.idea.vim.newapi.vim
import org.jetbrains.plugins.ideavim.VimJavaTestCase
@ -26,14 +27,18 @@ class SearchAgainNextActionJavaTest : VimJavaTestCase() {
""".trimIndent()
)
val foldingModel = fixture.editor.foldingModel
foldingModel.runBatchFoldingOperation {
val foldRegion = foldingModel.addFoldRegion(61, 71, "pupa")
foldRegion!!.isExpanded = false
ApplicationManager.getApplication().invokeAndWait {
val foldingModel = fixture.editor.foldingModel
foldingModel.runBatchFoldingOperation {
val foldRegion = foldingModel.addFoldRegion(61, 71, "pupa")
foldRegion!!.isExpanded = false
}
assertEquals(2, fixture.editor.vim.getVisualLineCount())
}
assertEquals(2, fixture.editor.vim.getVisualLineCount())
typeText("/pupa<CR>")
assertEquals(4, fixture.editor.vim.getVisualLineCount())
ApplicationManager.getApplication().invokeAndWait {
assertEquals(4, fixture.editor.vim.getVisualLineCount())
}
}
}

View File

@ -8,6 +8,7 @@
package org.jetbrains.plugins.ideavim.action.motion.updown
import com.intellij.openapi.application.ApplicationManager
import com.maddyhome.idea.vim.api.getVisualLineCount
import com.maddyhome.idea.vim.newapi.vim
import org.jetbrains.plugins.ideavim.VimJavaTestCase
@ -24,14 +25,18 @@ class MotionDownActionJavaTest : VimJavaTestCase() {
*/
""".trimIndent())
val foldingModel = fixture.editor.foldingModel
foldingModel.runBatchFoldingOperation {
val foldRegion = foldingModel.addFoldRegion(61, 71, "pupa")
foldRegion!!.isExpanded = false
ApplicationManager.getApplication().invokeAndWait {
val foldingModel = fixture.editor.foldingModel
foldingModel.runBatchFoldingOperation {
val foldRegion = foldingModel.addFoldRegion(61, 71, "pupa")
foldRegion!!.isExpanded = false
}
assertEquals(2, fixture.editor.vim.getVisualLineCount())
}
assertEquals(2, fixture.editor.vim.getVisualLineCount())
typeText("gg" + "$" + "j")
assertEquals(2, fixture.editor.vim.getVisualLineCount())
ApplicationManager.getApplication().invokeAndWait {
assertEquals(2, fixture.editor.vim.getVisualLineCount())
}
}
}

View File

@ -9,9 +9,9 @@
package org.jetbrains.plugins.ideavim.ex.implementation.commands
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.editor.textarea.TextComponentEditorImpl
import com.intellij.openapi.util.Disposer
import com.intellij.testFramework.TestLoggerFactory
import com.maddyhome.idea.vim.api.injector
import com.maddyhome.idea.vim.ex.ExException
import com.maddyhome.idea.vim.newapi.vim
@ -178,15 +178,17 @@ class MapCommandJavaTest : VimJavaTestCase() {
typeText(commandToKeys("map kk l"))
typeText(injector.parser.parseKeys("k"))
checkDelayedMapping(
text,
"""
ApplicationManager.getApplication().invokeAndWait {
checkDelayedMapping(
text,
"""
-$c----
12345
abcde
-----
""".trimIndent(),
)
)
}
}
@TestWithoutNeovim(reason = SkipNeovimReason.DIFFERENT)
@ -204,15 +206,17 @@ class MapCommandJavaTest : VimJavaTestCase() {
typeText(commandToKeys("map kk l"))
typeText(injector.parser.parseKeys("k"))
checkDelayedMapping(
text,
"""
ApplicationManager.getApplication().invokeAndWait {
checkDelayedMapping(
text,
"""
-----
12345
a${c}bcde
-----
""".trimIndent(),
)
)
}
}
@TestWithoutNeovim(SkipNeovimReason.DIFFERENT)
@ -233,15 +237,17 @@ class MapCommandJavaTest : VimJavaTestCase() {
typeText(commandToKeys("map jz w"))
typeText(injector.parser.parseKeys("k"))
checkDelayedMapping(
text,
"""
ApplicationManager.getApplication().invokeAndWait {
checkDelayedMapping(
text,
"""
-----
${c}12345
abcde
-----
""".trimIndent(),
)
)
}
}
@Test
@ -349,10 +355,10 @@ class MapCommandJavaTest : VimJavaTestCase() {
indicateErrors = true,
null,
)
val exception = assertThrowsLogError<TestLoggerFactory.TestLoggerAssertionError> {
val exception = assertThrowsLogError<Throwable> {
typeText(injector.parser.parseKeys("t"))
}
assertIs<ExException>(exception.cause) // Exception is wrapped into LOG.error twice
assertIs<ExException>(exception.cause!!.cause) // Exception is wrapped into LOG.error twice
assertPluginError(true)
assertPluginErrorMessageContains("E121: Undefined variable: s:mapping")
@ -374,15 +380,17 @@ class MapCommandJavaTest : VimJavaTestCase() {
typeText(commandToKeys("map kk h"))
typeText(injector.parser.parseKeys("kk"))
checkDelayedMapping(
text,
"""
ApplicationManager.getApplication().invokeAndWait {
checkDelayedMapping(
text,
"""
-----
${c}12345
abcde
-----
""".trimIndent(),
)
)
}
assertMode(Mode.NORMAL())
}
@ -397,10 +405,11 @@ class MapCommandJavaTest : VimJavaTestCase() {
""".trimIndent()
configureByJavaText(text)
assertThrowsLogError<ExException> {
val exception = assertThrowsLogError<Throwable> {
typeText(commandToKeys("inoremap <expr> <cr> unknownFunction() ? '\\<C-y>' : '\\<C-g>u\\<CR>'"))
typeText(injector.parser.parseKeys("i<CR>"))
}
assertIs<ExException>(exception.cause)
assertPluginError(true)
assertPluginErrorMessageContains("E117: Unknown function: unknownFunction")

View File

@ -16,7 +16,9 @@ import com.intellij.codeInsight.template.TemplateManager
import com.intellij.codeInsight.template.impl.TemplateManagerImpl
import com.intellij.ide.DataManager
import com.intellij.injected.editor.EditorWindow
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.editor.Editor
import com.intellij.psi.PsiElement
import com.intellij.refactoring.rename.inplace.VariableInplaceRenameHandler
import com.intellij.testFramework.PlatformTestUtil
import com.intellij.testFramework.fixtures.CodeInsightTestUtil.doInlineRename
@ -65,7 +67,9 @@ class TemplateTest : VimJavaTestCase() {
}
""".trimIndent(),
)
doInlineRename(VariableInplaceRenameHandler(), "myNewVar", fixture)
ApplicationManager.getApplication().invokeAndWait {
doInlineRename(VariableInplaceRenameHandler(), "myNewVar", fixture)
}
assertState(
"""
class Hello {
@ -143,7 +147,9 @@ class TemplateTest : VimJavaTestCase() {
waitAndAssertMode(fixture, Mode.SELECT(SelectionType.CHARACTER_WISE))
assertState(Mode.SELECT(SelectionType.CHARACTER_WISE))
LookupManager.hideActiveLookup(fixture.project)
ApplicationManager.getApplication().invokeAndWait {
LookupManager.hideActiveLookup(fixture.project)
}
typeText(injector.parser.parseKeys("<Left>"))
assertState(Mode.INSERT)
typeText(injector.parser.parseKeys("pre" + "<CR>"))
@ -176,7 +182,9 @@ class TemplateTest : VimJavaTestCase() {
waitAndAssertMode(fixture, Mode.SELECT(SelectionType.CHARACTER_WISE))
assertState(Mode.SELECT(SelectionType.CHARACTER_WISE))
LookupManager.hideActiveLookup(fixture.project)
ApplicationManager.getApplication().invokeAndWait {
LookupManager.hideActiveLookup(fixture.project)
}
typeText(injector.parser.parseKeys("<Right>"))
assertState(Mode.INSERT)
assertState(
@ -206,7 +214,9 @@ class TemplateTest : VimJavaTestCase() {
waitAndAssertMode(fixture, Mode.SELECT(SelectionType.CHARACTER_WISE))
assertState(Mode.SELECT(SelectionType.CHARACTER_WISE))
LookupManager.hideActiveLookup(fixture.project)
ApplicationManager.getApplication().invokeAndWait {
LookupManager.hideActiveLookup(fixture.project)
}
typeText(injector.parser.parseKeys("<Left>"))
assertState(Mode.INSERT)
assertState(
@ -236,7 +246,9 @@ class TemplateTest : VimJavaTestCase() {
waitAndAssertMode(fixture, Mode.SELECT(SelectionType.CHARACTER_WISE))
assertState(Mode.SELECT(SelectionType.CHARACTER_WISE))
LookupManager.hideActiveLookup(fixture.project)
ApplicationManager.getApplication().invokeAndWait {
LookupManager.hideActiveLookup(fixture.project)
}
typeText(injector.parser.parseKeys("<Right>"))
assertState(Mode.INSERT)
assertState(
@ -527,8 +539,12 @@ class TemplateTest : VimJavaTestCase() {
template.addVariable("V1", "", "\"123\"", true)
template.addVariable("V2", "", "\"239\"", true)
manager.startTemplate(fixture.editor, template)
PlatformTestUtil.dispatchAllInvocationEventsInIdeEventQueue()
ApplicationManager.getApplication().invokeAndWait {
ApplicationManager.getApplication().runWriteAction {
manager.startTemplate(fixture.editor, template)
}
PlatformTestUtil.dispatchAllInvocationEventsInIdeEventQueue()
}
assertMode(Mode.NORMAL())
assertOffset(2)
@ -555,7 +571,9 @@ class TemplateTest : VimJavaTestCase() {
)
startRenaming(VariableInplaceRenameHandler())
val lookupValue = fixture.lookupElementStrings?.get(0) ?: kotlin.test.fail()
fixture.finishLookup(Lookup.NORMAL_SELECT_CHAR)
ApplicationManager.getApplication().invokeAndWait {
fixture.finishLookup(Lookup.NORMAL_SELECT_CHAR)
}
assertState(
"""
class Hello {
@ -570,7 +588,15 @@ class TemplateTest : VimJavaTestCase() {
private fun startRenaming(handler: VariableInplaceRenameHandler): Editor {
val editor = if (fixture.editor is EditorWindow) (fixture.editor as EditorWindow).delegate else fixture.editor
handler.doRename(fixture.elementAtCaret, editor, dataContext)
var elementToRename: PsiElement? = null
ApplicationManager.getApplication().invokeAndWait {
ApplicationManager.getApplication().runReadAction {
elementToRename = fixture.elementAtCaret
}
ApplicationManager.getApplication().runWriteAction {
handler.doRename(elementToRename!!, editor, dataContext)
}
}
return editor
}