/
AbstractKotlinEvaluateExpressionTest.kt
99 lines (87 loc) · 4.15 KB
/
AbstractKotlinEvaluateExpressionTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.plugin.debugger.evaluate
import com.intellij.debugger.DebuggerInvocationUtil
import com.intellij.debugger.engine.evaluation.expression.EvaluatorBuilderImpl
import com.intellij.debugger.engine.evaluation.TextWithImportsImpl
import com.intellij.debugger.engine.evaluation.CodeFragmentKind
import com.intellij.debugger.engine.ContextUtil
import com.intellij.debugger.engine.SuspendContextImpl
import org.jetbrains.jet.plugin.debugger.*
import org.junit.Assert
import org.jetbrains.jet.plugin.JetFileType
import com.intellij.openapi.util.io.FileUtil
import java.io.File
import org.jetbrains.jet.InTextDirectivesUtils
import org.jetbrains.eval4j.jdi.asValue
public abstract class AbstractKotlinEvaluateExpressionTest : KotlinDebuggerTestCase() {
fun doTest(path: String) {
val fileContent = FileUtil.loadFile(File(path))
val expressions = InTextDirectivesUtils.findLinesWithPrefixesRemoved(fileContent, "// EXPRESSION: ")
val expectedResults = InTextDirectivesUtils.findLinesWithPrefixesRemoved(fileContent, "// RESULT: ")
assert(expressions.size == expectedResults.size, "Sizes of test directives are different")
createDebugProcess(path)
onBreakpoint {
val exceptions = linkedMapOf<String, Throwable>()
for ((i, expression) in expressions.withIndices()) {
try {
evaluate(expression, expectedResults[i])
}
catch (e: Throwable) {
exceptions.put(expression, e)
}
}
if (!exceptions.empty) {
for (exc in exceptions.values()) {
exc.printStackTrace()
}
throw AssertionError("Test failed:\n" + exceptions.map { "expression: ${it.key}, exception: ${it.value.getMessage()}" }.makeString("\n"))
}
}
finish()
}
private fun onBreakpoint(doOnBreakpoint: SuspendContextImpl.() -> Unit) {
super.onBreakpoint {
super.printContext(it)
it.doOnBreakpoint()
}
}
private fun finish() {
onBreakpoint {
resume(this)
}
}
private fun SuspendContextImpl.evaluate(expression: String, expectedResult: String) {
try {
val sourcePosition = ContextUtil.getSourcePosition(this)
val contextElement = ContextUtil.getContextElement(sourcePosition)!!
Assert.assertTrue("KotlinCodeFragmentFactory should be accepted for context element otherwise default evaluator will be called. ContextElement = ${contextElement.getText()}",
KotlinCodeFragmentFactory().isContextAccepted(contextElement))
val evaluator = DebuggerInvocationUtil.commitAndRunReadAction(getProject()) {
EvaluatorBuilderImpl.build(TextWithImportsImpl(CodeFragmentKind.EXPRESSION, expression, KotlinEditorTextProvider.getImports(contextElement), JetFileType.INSTANCE),
contextElement,
sourcePosition)
}
if (evaluator == null) throw AssertionError("Cannot create an Evaluator for Evaluate Expression")
val value = evaluator.evaluate(createEvaluationContext(this))
val actualResult = value.asValue().toString()
Assert.assertTrue("Evaluate expression returns wrong result for $expression:\nexpected = $expectedResult\nactual = $actualResult\n", expectedResult == actualResult)
}
finally {
resume(this)
}
}
}