[test] Fix testOnRunPossibility
This commit is contained in:
committed by
Pavel V. Talanov
parent
3152792860
commit
740f311af6
@@ -44,11 +44,11 @@ public class KotlinConsoleKeeper(val project: Project) {
|
||||
fun putVirtualFileToConsole(virtualFile: VirtualFile, console: KotlinConsoleRunner) = consoleMap.put(virtualFile, console)
|
||||
fun removeConsole(virtualFile: VirtualFile) = consoleMap.remove(virtualFile)
|
||||
|
||||
fun run(module: Module): KotlinConsoleRunner? {
|
||||
fun run(module: Module, testMode: Boolean = false): KotlinConsoleRunner? {
|
||||
val path = module.moduleFilePath
|
||||
val cmdLine = createCommandLine(module) ?: return errorNotification(project, "Module SDK not found") let { null }
|
||||
|
||||
val consoleRunner = KotlinConsoleRunner(cmdLine, module, project, REPL_TITLE, path)
|
||||
val consoleRunner = KotlinConsoleRunner(cmdLine, module, testMode, project, REPL_TITLE, path)
|
||||
consoleRunner.initAndRun()
|
||||
consoleRunner.setupGutters()
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ import com.intellij.openapi.editor.ex.EditorEx
|
||||
import com.intellij.openapi.editor.markup.*
|
||||
import com.intellij.openapi.module.Module
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.util.Disposer
|
||||
import com.intellij.psi.PsiManager
|
||||
import com.intellij.util.Consumer
|
||||
import org.jetbrains.kotlin.console.actions.BuildAndRestartConsoleAction
|
||||
@@ -46,10 +47,12 @@ import java.awt.Color
|
||||
import java.awt.Font
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import javax.swing.Icon
|
||||
import kotlin.properties.Delegates
|
||||
|
||||
public class KotlinConsoleRunner(
|
||||
private val cmdLine: GeneralCommandLine,
|
||||
private val module: Module,
|
||||
private val testMode: Boolean,
|
||||
myProject: Project,
|
||||
title: String,
|
||||
path: String?
|
||||
@@ -58,6 +61,8 @@ public class KotlinConsoleRunner(
|
||||
private val historyManager = KotlinConsoleHistoryManager(this)
|
||||
val executor = KotlinConsoleExecutor(this, historyManager)
|
||||
|
||||
private var disposableDescriptor: RunContentDescriptor by Delegates.notNull()
|
||||
|
||||
override fun createProcess() = cmdLine.createProcess()
|
||||
|
||||
override fun createConsoleView(): LanguageConsoleView? {
|
||||
@@ -102,8 +107,10 @@ public class KotlinConsoleRunner(
|
||||
defaultExecutor: Executor,
|
||||
contentDescriptor: RunContentDescriptor
|
||||
): List<AnAction> {
|
||||
disposableDescriptor = contentDescriptor
|
||||
|
||||
val actionList = arrayListOf<AnAction>(
|
||||
BuildAndRestartConsoleAction(project, module, defaultExecutor, contentDescriptor, restarter),
|
||||
BuildAndRestartConsoleAction(project, module, defaultExecutor, contentDescriptor, restarter, testMode),
|
||||
createConsoleExecAction(consoleExecuteActionHandler),
|
||||
createCloseAction(defaultExecutor, contentDescriptor)
|
||||
)
|
||||
@@ -177,4 +184,10 @@ public class KotlinConsoleRunner(
|
||||
val oldHighlighter = editorToIndicator[editor] ?: return
|
||||
WriteCommandAction.runWriteCommandAction(project) { oldHighlighter.gutterIconRenderer = KotlinConsoleIndicatorRenderer(newIcon) }
|
||||
}
|
||||
|
||||
// this method shouldn't be called in normal usage; it is for test purpose only
|
||||
fun dispose() {
|
||||
processHandler.destroyProcess()
|
||||
Disposer.dispose(disposableDescriptor)
|
||||
}
|
||||
}
|
||||
+3
-2
@@ -41,11 +41,12 @@ public class BuildAndRestartConsoleAction(
|
||||
private val module: Module,
|
||||
private val executor: Executor,
|
||||
private val contentDescriptor: RunContentDescriptor,
|
||||
private val restarter: Consumer<Module>
|
||||
private val restarter: Consumer<Module>,
|
||||
private val testMode: Boolean
|
||||
) : AnAction("Build and restart", buildAndRestartMessage(module), AllIcons.Actions.Restart) {
|
||||
|
||||
init {
|
||||
showOutdatedClassedNotificationIfNeeded()
|
||||
if (!testMode) showOutdatedClassedNotificationIfNeeded()
|
||||
}
|
||||
|
||||
override fun actionPerformed(_: AnActionEvent) = compileModule()
|
||||
|
||||
@@ -17,53 +17,38 @@
|
||||
package org.jetbrains.kotlin.idea.console
|
||||
|
||||
import com.intellij.execution.impl.ConsoleViewImpl
|
||||
import com.intellij.openapi.Disposable
|
||||
import com.intellij.openapi.util.Disposer
|
||||
import com.intellij.testFramework.PlatformTestCase
|
||||
import org.jetbrains.kotlin.console.KotlinConsoleKeeper
|
||||
import org.jetbrains.kotlin.console.KotlinConsoleRunner
|
||||
import org.junit.FixMethodOrder
|
||||
import org.junit.Test
|
||||
import org.junit.runners.MethodSorters
|
||||
import kotlin.properties.Delegates
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
@FixMethodOrder(MethodSorters.JVM)
|
||||
public class KotlinReplTest : PlatformTestCase() {
|
||||
private var consoleRunner: KotlinConsoleRunner by Delegates.notNull()
|
||||
|
||||
override fun setUp() {
|
||||
super.setUp()
|
||||
consoleRunner = KotlinConsoleKeeper.getInstance(project).run(module)!!
|
||||
consoleRunner = KotlinConsoleKeeper.getInstance(project).run(module, testMode = true)!!
|
||||
}
|
||||
|
||||
override fun tearDown() {
|
||||
val consoleView = consoleRunner.consoleView
|
||||
|
||||
consoleRunner.processHandler.destroyProcess()
|
||||
|
||||
// dispose RunContentDescriptor by reflection
|
||||
val getNodeMethod = Disposer.getTree().javaClass.getDeclaredMethod("getNode", Object().javaClass)
|
||||
getNodeMethod.isAccessible = true
|
||||
val consoleNode = getNodeMethod(Disposer.getTree(), consoleView)
|
||||
val getParentMethod = consoleNode.javaClass.getDeclaredMethod("getParent")
|
||||
getParentMethod.isAccessible = true
|
||||
val consoleParent = getParentMethod(consoleNode)
|
||||
val getObjectMethod = consoleParent.javaClass.getDeclaredMethod("getObject")
|
||||
getObjectMethod.isAccessible = true
|
||||
val descriptor = getObjectMethod(consoleParent) as Disposable
|
||||
|
||||
Disposer.dispose(descriptor)
|
||||
|
||||
consoleRunner.dispose()
|
||||
super.tearDown()
|
||||
}
|
||||
|
||||
fun checkHistoryUpdate(maxIter: Int = 20, sleepTime: Long = 500, p: (String) -> Boolean): String {
|
||||
private fun checkHistoryUpdate(maxIterations: Int = 20, sleepTime: Long = 1000, predicate: (String) -> Boolean): String {
|
||||
val consoleView = consoleRunner.consoleView as ConsoleViewImpl
|
||||
var docHistory: String = ""
|
||||
|
||||
for (i in 1..maxIter) {
|
||||
for (i in 1..maxIterations) {
|
||||
docHistory = consoleRunner.consoleView.historyViewer.document.text.trim()
|
||||
|
||||
if (p(docHistory)) break
|
||||
if (predicate(docHistory)) break
|
||||
|
||||
Thread.sleep(sleepTime)
|
||||
consoleView.flushDeferredText()
|
||||
@@ -81,11 +66,4 @@ public class KotlinReplTest : PlatformTestCase() {
|
||||
assertTrue(allOk(historyText), "Successful run should contain text: ':help for help'")
|
||||
assertFalse(consoleRunner.processHandler.isProcessTerminated, "Process accidentally terminated")
|
||||
}
|
||||
|
||||
@Test fun testSimpleCommand() {
|
||||
consoleRunner.pipeline.submitCommand("1 + 1")
|
||||
val docHistory = checkHistoryUpdate { x: String -> x.endsWith("2</output>") }
|
||||
|
||||
assertTrue(docHistory.endsWith("2</output>"), "1 + 1 should be equal 2, but document history is: '$docHistory'")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user