Skip to content

Commit

Permalink
Migration of tests to JUnit 5
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexPl292 committed Mar 14, 2023
1 parent 0389ea8 commit 9271ca0
Show file tree
Hide file tree
Showing 318 changed files with 5,839 additions and 1,688 deletions.
5 changes: 5 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ dependencies {
api(project(":vim-engine"))

testApi("com.squareup.okhttp3:okhttp:4.10.0")

testImplementation("org.junit.jupiter:junit-jupiter-api:5.9.2")
testImplementation("org.junit.jupiter:junit-jupiter-engine:5.9.2")
testImplementation("org.junit.jupiter:junit-jupiter-params:5.9.2")
}

configurations {
Expand Down Expand Up @@ -341,6 +345,7 @@ ktlint {

tasks {
test {
useJUnitPlatform()
exclude("**/propertybased/**")
exclude("**/longrunning/**")
exclude("/ui/**")
Expand Down
40 changes: 30 additions & 10 deletions src/test/java/org/jetbrains/plugins/ideavim/NeovimTesting.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.ensarsarajcic.neovim.java.api.types.api.VimCoords
import com.ensarsarajcic.neovim.java.corerpc.client.ProcessRpcConnection
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.LogicalPosition
import com.intellij.util.containers.toArray
import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.command.SelectionType
import com.maddyhome.idea.vim.common.CharacterPosition
Expand All @@ -29,6 +30,9 @@ import com.maddyhome.idea.vim.register.RegisterConstants.LAST_INSERTED_TEXT_REGI
import com.maddyhome.idea.vim.register.RegisterConstants.LAST_SEARCH_REGISTER
import com.maddyhome.idea.vim.register.RegisterConstants.VALID_REGISTERS
import org.junit.Assert.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInfo
import org.junit.jupiter.params.provider.Arguments

internal object NeovimTesting {
private lateinit var neovimApi: NeovimApi
Expand All @@ -45,7 +49,7 @@ internal object NeovimTesting {

private var singleCaret = true

fun setUp(test: VimTestCase) {
fun setUp(test: TestInfo) {
if (!neovimEnabled(test)) return
val nvimPath = System.getenv("ideavim.nvim.path") ?: "nvim"

Expand All @@ -65,10 +69,10 @@ internal object NeovimTesting {
exitCommand = neovimApi.replaceTermcodes("<esc><esc>:qa!", true, false, true).get()
escapeCommand = neovimApi.replaceTermcodes("<esc>", true, false, true).get()
ctrlcCommand = neovimApi.replaceTermcodes("<C-C>", true, false, true).get()
currentTestName = test.name
currentTestName = test.displayName
}

fun tearDown(test: VimTestCase) {
fun tearDown(test: TestInfo) {
if (!neovimEnabled(test)) return
println("Tested with neovim: $neovimTestsCounter")
if (VimTestCase.Checks.neoVim.exitOnTearDown) {
Expand All @@ -82,8 +86,8 @@ internal object NeovimTesting {
}
}

private fun neovimEnabled(test: VimTestCase, editor: Editor? = null): Boolean {
val method = test.javaClass.getMethod(test.name)
private fun neovimEnabled(test: TestInfo, editor: Editor? = null): Boolean {
val method = test.testMethod.get()
val noBehaviourDiffers = !method.isAnnotationPresent(VimBehaviorDiffers::class.java)
val noTestingWithoutNeovim = !method.isAnnotationPresent(TestWithoutNeovim::class.java) &&
!test.javaClass.isAnnotationPresent(TestWithoutNeovim::class.java)
Expand All @@ -103,14 +107,14 @@ internal object NeovimTesting {
singleCaret
}

fun setupEditor(editor: Editor, test: VimTestCase) {
fun setupEditor(editor: Editor, test: TestInfo) {
if (!neovimEnabled(test, editor)) return
neovimApi.currentBuffer.get().setLines(0, -1, false, editor.document.text.split("\n")).get()
val charPosition = CharacterPosition.fromOffset(editor, editor.caretModel.offset)
neovimApi.currentWindow.get().setCursor(VimCoords(charPosition.line + 1, charPosition.column)).get()
}

fun typeCommand(keys: String, test: VimTestCase, editor: Editor) {
fun typeCommand(keys: String, test: TestInfo, editor: Editor) {
if (!neovimEnabled(test, editor)) return
when {
keys.equals("<esc>", ignoreCase = true) -> neovimApi.input(escapeCommand).get()
Expand All @@ -122,7 +126,7 @@ internal object NeovimTesting {
}
}

fun assertState(editor: Editor, test: VimTestCase) {
fun assertState(editor: Editor, test: TestInfo) {
if (!neovimEnabled(test, editor)) return
if (currentTestName != "") {
currentTestName = ""
Expand All @@ -134,15 +138,15 @@ internal object NeovimTesting {
assertRegisters()
}

fun setRegister(register: Char, keys: String, test: VimTestCase) {
fun setRegister(register: Char, keys: String, test: TestInfo) {
if (!neovimEnabled(test)) return
neovimApi.callFunction("setreg", listOf(register, keys, 'c'))
}

private fun getCaret(): VimCoords = neovimApi.currentWindow.get().cursor.get()
private fun getText(): String = neovimApi.currentBuffer.get().getLines(0, -1, false).get().joinToString("\n")

fun assertCaret(editor: Editor, test: VimTestCase) {
fun assertCaret(editor: Editor, test: TestInfo) {
if (!neovimEnabled(test, editor)) return
if (currentTestName != "") {
currentTestName = ""
Expand Down Expand Up @@ -199,6 +203,7 @@ internal object NeovimTesting {
}
}

@Test
annotation class TestWithoutNeovim(val reason: SkipNeovimReason, val description: String = "")

enum class SkipNeovimReason {
Expand Down Expand Up @@ -238,3 +243,18 @@ enum class SkipNeovimReason {
fun LogicalPosition.toVimCoords(): VimCoords {
return VimCoords(this.line + 1, this.column)
}

fun <T, S, V> Collection<T>.cartesianProduct(other: Iterable<S>, transformer: (first: T, second: S) -> V): List<V> {
return this.flatMap { first -> other.map { second -> transformer.invoke(first, second) } }
}

// Cartesian product of multiple lists. Useful for making parameterized tests with all available combinations.
// Can be used instead of @Theory from JUnit 4
fun combinate(vararg elements: List<String>): List<Arguments> {
val res = elements.fold(listOf<List<String>>(emptyList())) { acc, items ->
acc.cartesianProduct(items) { accItems, item ->
accItems + item
}
}
return res.map { Arguments.of(*it.toArray(emptyArray())) }
}
14 changes: 10 additions & 4 deletions src/test/java/org/jetbrains/plugins/ideavim/RegisterActionsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ import com.maddyhome.idea.vim.command.VimStateMachine
import com.maddyhome.idea.vim.handler.ActionBeanClass
import com.maddyhome.idea.vim.key.CommandNode
import com.maddyhome.idea.vim.key.CommandPartNode
import junit.framework.TestCase
import org.junit.jupiter.api.Test
import javax.swing.KeyStroke
import kotlin.test.assertNotNull

class RegisterActionsTest : VimTestCase() {
@Test
fun `test simple action`() {
val before = "I ${c}found it in a legendary land"
val after = "I f${c}ound it in a legendary land"
doTest("l", before, after, VimStateMachine.Mode.COMMAND, VimStateMachine.SubMode.NONE)
}

@TestWithoutNeovim(reason = SkipNeovimReason.EDITOR_MODIFICATION)
@Test
fun `test action in disabled plugin`() {
try {
setupChecks {
Expand All @@ -42,6 +45,7 @@ class RegisterActionsTest : VimTestCase() {
}

@TestWithoutNeovim(reason = SkipNeovimReason.EDITOR_MODIFICATION)
@Test
fun `test turn plugin off and on`() {
val before = "I ${c}found it in a legendary land"
val after = "I f${c}ound it in a legendary land"
Expand All @@ -52,6 +56,7 @@ class RegisterActionsTest : VimTestCase() {
}

@TestWithoutNeovim(reason = SkipNeovimReason.EDITOR_MODIFICATION)
@Test
fun `test enable twice`() {
val before = "I ${c}found it in a legendary land"
val after = "I f${c}ound it in a legendary land"
Expand All @@ -63,6 +68,7 @@ class RegisterActionsTest : VimTestCase() {
}

@TestWithoutNeovim(reason = SkipNeovimReason.EDITOR_MODIFICATION)
@Test
fun `test unregister extension`() {
val before = "I ${c}found it in a legendary land"
val after = "I f${c}ound it in a legendary land"
Expand All @@ -71,15 +77,15 @@ class RegisterActionsTest : VimTestCase() {
motionRightAction =
VIM_ACTIONS_EP.getExtensionList(null).first { it.actionId == "VimPreviousTabAction" }

assertNotNull(getCommandNode())
assertNotNull<Any>(getCommandNode())

@Suppress("DEPRECATION")
VIM_ACTIONS_EP.getPoint(null).unregisterExtension(motionRightAction!!)
assertNull(getCommandNode())
kotlin.test.assertNull(getCommandNode())
}
@Suppress("DEPRECATION")
VIM_ACTIONS_EP.getPoint(null).registerExtension(motionRightAction!!)
TestCase.assertNotNull(getCommandNode())
assertNotNull<Any>(getCommandNode())
}

private fun getCommandNode(): CommandNode<*>? {
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/org/jetbrains/plugins/ideavim/TestHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.LogicalPosition
import com.intellij.testFramework.EditorTestUtil
import com.intellij.testFramework.fixtures.CodeInsightTestFixture
import com.maddyhome.idea.vim.api.getKnownToggleOption
import com.maddyhome.idea.vim.api.globalOptions
import com.maddyhome.idea.vim.api.injector
import com.maddyhome.idea.vim.api.setToggleOption
import com.maddyhome.idea.vim.command.VimStateMachine
import com.maddyhome.idea.vim.common.TextRange
import com.maddyhome.idea.vim.group.IjOptionConstants
import com.maddyhome.idea.vim.helper.editorMode
import com.maddyhome.idea.vim.options.OptionScope
import javax.swing.KeyStroke
import kotlin.test.fail

/**
Expand Down Expand Up @@ -107,3 +111,39 @@ fun waitCondition(
}
return false
}

internal const val c = EditorTestUtil.CARET_TAG
internal const val s = EditorTestUtil.SELECTION_START_TAG
internal const val se = EditorTestUtil.SELECTION_END_TAG

internal fun enableExtensions(vararg extensionNames: String) {
for (name in extensionNames) {
injector.optionGroup.setToggleOption(injector.optionGroup.getKnownToggleOption(name), OptionScope.GLOBAL)
}
}

internal fun String.dotToTab(): String = replace('.', '\t')

internal fun String.dotToSpace(): String = replace('.', ' ')

internal fun commandToKeys(command: String): List<KeyStroke> {
val keys: MutableList<KeyStroke> = ArrayList()
if (!command.startsWith(":")) {
keys.addAll(injector.parser.parseKeys(":"))
}
keys.addAll(injector.parser.stringToKeys(command)) // Avoids trying to parse 'command ... <args>' as a special char
keys.addAll(injector.parser.parseKeys("<Enter>"))
return keys
}

internal fun exCommand(command: String) = ":$command<CR>"

internal fun searchToKeys(pattern: String, forwards: Boolean): List<KeyStroke> {
val keys: MutableList<KeyStroke> = ArrayList()
keys.addAll(injector.parser.parseKeys(if (forwards) "/" else "?"))
keys.addAll(injector.parser.stringToKeys(pattern)) // Avoids trying to parse 'command ... <args>' as a special char
keys.addAll(injector.parser.parseKeys("<CR>"))
return keys
}

internal fun searchCommand(pattern: String) = "$pattern<CR>"
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import com.maddyhome.idea.vim.ex.exExceptionMessage
import com.maddyhome.idea.vim.options.OptionScope
import com.maddyhome.idea.vim.vimscript.model.datatypes.VimInt
import com.maddyhome.idea.vim.vimscript.model.datatypes.VimString
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.TestInfo

/**
* @author Alex Plate
Expand All @@ -33,9 +35,10 @@ import com.maddyhome.idea.vim.vimscript.model.datatypes.VimString
abstract class VimOptionTestCase(option: String, vararg otherOptions: String) : VimTestCase() {
private val options: Set<String> = setOf(option, *otherOptions)

override fun setUp() {
super.setUp()
val testMethod = this.javaClass.getMethod(this.name)
@BeforeEach
override fun setUp(testInfo: TestInfo) {
super.setUp(testInfo)
val testMethod = this.testInfo.testMethod.get()
if (!testMethod.isAnnotationPresent(VimOptionDefaultAll::class.java)) {
if (!testMethod.isAnnotationPresent(VimOptionTestConfiguration::class.java)) kotlin.test.fail("You should add VimOptionTestConfiguration with options for this method")

Expand Down
Loading

0 comments on commit 9271ca0

Please sign in to comment.