Skip to content

Commit

Permalink
Refactor neovim tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexPl292 committed Jul 27, 2020
1 parent 442e739 commit 0e8a1bf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
19 changes: 13 additions & 6 deletions test/org/jetbrains/plugins/ideavim/NeovimTesting.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ internal object NeovimTesting {
lateinit var neovimApi: NeovimApi
lateinit var neovim: Process

fun setUp() {
fun setUp(test: VimTestCase) {
if (!neovimEnabled(test)) return
val pb = ProcessBuilder("nvim", "-u", "NONE", "--embed", "--headless")
neovim = pb.start()
val neovimConnection = ProcessRPCConnection(neovim, true)
neovimApi = NeovimApis.getApiForConnection(neovimConnection)
}

fun tearDown() {
fun tearDown(test: VimTestCase) {
if (!neovimEnabled(test)) return
neovim.destroy()
}

Expand All @@ -52,23 +54,27 @@ internal object NeovimTesting {
&& System.getProperty("ideavim.neovim.test", "false")!!.toBoolean()
}

fun setupEditor(editor: Editor) {
fun setupEditor(editor: Editor, test: VimTestCase) {
if (!neovimEnabled(test)) 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) {
fun typeCommand(keys: String, test: VimTestCase) {
if (!neovimEnabled(test)) return
neovimApi.input(neovimApi.replaceTermcodes(keys, true, false, true).get()).get()
}

fun assertState(editor: Editor) {
fun assertState(editor: Editor, test: VimTestCase) {
if (!neovimEnabled(test)) return
assertText(editor)
assertCaret(editor)
assertMode(editor)
}

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

Expand Down Expand Up @@ -104,4 +110,5 @@ enum class SkipNeovimReason {
MAPPING,
SELECT_MODE,
VISUAL_BLOCK_MODE,
DIFFERENT,
}
15 changes: 6 additions & 9 deletions test/org/jetbrains/plugins/ideavim/VimTestCase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ import javax.swing.KeyStroke
*/
abstract class VimTestCase : UsefulTestCase() {
protected lateinit var myFixture: CodeInsightTestFixture
private var neovimTestingEnabled = true

@Throws(Exception::class)
override fun setUp() {
Expand All @@ -88,9 +87,7 @@ abstract class VimTestCase : UsefulTestCase() {
// Make sure the entry text field gets a bounds, or we won't be able to work out caret location
ExEntryPanel.getInstance().entry.setBounds(0, 0, 100, 25)

neovimTestingEnabled = NeovimTesting.neovimEnabled(this)

if (neovimTestingEnabled) NeovimTesting.setUp()
NeovimTesting.setUp(this)
}

protected val testDataPath: String
Expand All @@ -110,7 +107,7 @@ abstract class VimTestCase : UsefulTestCase() {
VimPlugin.getMark().resetAllMarks()

// Tear down neovim
if (neovimTestingEnabled) NeovimTesting.tearDown()
NeovimTesting.tearDown(this)

super.tearDown()
}
Expand Down Expand Up @@ -250,12 +247,12 @@ abstract class VimTestCase : UsefulTestCase() {
subModeAfter: SubMode) {
configureByText(before)

if (neovimTestingEnabled) NeovimTesting.setupEditor(myFixture.editor)
if (neovimTestingEnabled) NeovimTesting.typeCommand(keys)
NeovimTesting.setupEditor(myFixture.editor, this)
NeovimTesting.typeCommand(keys, this)

performTest(keys, after, modeAfter, subModeAfter)

if (neovimTestingEnabled) NeovimTesting.assertState(myFixture.editor)
NeovimTesting.assertState(myFixture.editor, this)
}

private fun performTest(keys: String, after: String, modeAfter: CommandState.Mode, subModeAfter: SubMode) {
Expand All @@ -278,7 +275,7 @@ abstract class VimTestCase : UsefulTestCase() {

protected fun setRegister(register: Char, keys: String) {
VimPlugin.getRegister().setKeys(register, stringToKeys(keys))
if (neovimTestingEnabled) NeovimTesting.setRegister(register, keys)
NeovimTesting.setRegister(register, keys, this)
}

protected fun assertState(modeAfter: CommandState.Mode, subModeAfter: SubMode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,19 @@ import org.jetbrains.jetCheck.Generator
import org.jetbrains.jetCheck.ImperativeCommand
import org.jetbrains.jetCheck.PropertyChecker
import org.jetbrains.plugins.ideavim.NeovimTesting
import org.jetbrains.plugins.ideavim.SkipNeovimReason
import org.jetbrains.plugins.ideavim.TestWithoutNeovim
import org.jetbrains.plugins.ideavim.VimTestCase

class IncrementDecrementTest : VimPropertyTest() {
@TestWithoutNeovim(SkipNeovimReason.DIFFERENT)
fun testPlayingWithNumbers() {
PropertyChecker.checkScenarios {
ImperativeCommand { env ->
val editor = configureByText(numbers)
try {
moveCaretToRandomPlace(env, editor)
env.executeCommands(Generator.sampledFrom(IncrementDecrementActions(editor)))
env.executeCommands(Generator.sampledFrom(IncrementDecrementActions(editor, this)))
} finally {
reset(editor)
}
Expand All @@ -52,12 +55,12 @@ class IncrementDecrementTest : VimPropertyTest() {
try {
moveCaretToRandomPlace(env, editor)

if (NeovimTesting.neovimEnabled(this)) NeovimTesting.setupEditor(editor)
if (NeovimTesting.neovimEnabled(this)) NeovimTesting.typeCommand(":set nrformats+=octal<CR>")
NeovimTesting.setupEditor(editor, this)
NeovimTesting.typeCommand(":set nrformats+=octal<CR>", this)

env.executeCommands(Generator.sampledFrom(IncrementDecrementActions(editor, NeovimTesting.neovimEnabled(this))))
env.executeCommands(Generator.sampledFrom(IncrementDecrementActions(editor, this)))

if (NeovimTesting.neovimEnabled(this)) NeovimTesting.assertState(editor)
NeovimTesting.assertState(editor, this)
} finally {
reset(editor)
}
Expand All @@ -66,14 +69,14 @@ class IncrementDecrementTest : VimPropertyTest() {
}
}

private class IncrementDecrementActions(private val editor: Editor, private val withNeovim: Boolean = false) : ImperativeCommand {
private class IncrementDecrementActions(private val editor: Editor, val test: VimTestCase) : ImperativeCommand {
override fun performCommand(env: ImperativeCommand.Environment) {
val generator = Generator.sampledFrom("<C-A>", "<C-X>")
val key = env.generateValue(generator, null)
val action = parseKeys(key).single()
env.logMessage("Use command: ${StringHelper.toKeyNotation(action)}.")
VimTestCase.typeText(listOf(action), editor, editor.project)
if (withNeovim) NeovimTesting.typeCommand(key)
NeovimTesting.typeCommand(key, test)

IdeEventQueue.getInstance().flushQueue()
PlatformTestUtil.dispatchAllInvocationEventsInIdeEventQueue()
Expand Down

0 comments on commit 0e8a1bf

Please sign in to comment.