forked from fwcd/kotlin-language-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompilerTest.kt
72 lines (60 loc) · 3.09 KB
/
CompilerTest.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
package org.javacs.kt
import org.hamcrest.Matchers.hasToString
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.KtReferenceExpression
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
import org.jetbrains.kotlin.resolve.BindingContext
import org.junit.Assert.assertThat
import org.junit.Test
import java.nio.file.Files
class CompilerTest {
val compiler = Compiler(setOf())
val myTestResources = testResourcesRoot().resolve("compiler")
val file = myTestResources.resolve("FileToEdit.kt")
val editedText = """
private class FileToEdit {
val someVal = 1
}"""
@Test fun compileFile() {
val content = Files.readAllLines(file).joinToString("\n")
val original = compiler.createFile(content, file)
val (context, _) = compiler.compileFile(original, listOf(original))
val psi = original.findElementAt(45)!!
val kt = psi.parentsWithSelf.filterIsInstance<KtExpression>().first()
assertThat(context.getType(kt), hasToString("String"))
}
@Test fun newFile() {
val original = compiler.createFile(editedText, file)
val (context, _) = compiler.compileFile(original, listOf(original))
val psi = original.findElementAt(46)!!
val kt = psi.parentsWithSelf.filterIsInstance<KtExpression>().first()
assertThat(context.getType(kt), hasToString("Int"))
}
@Test fun editFile() {
val content = Files.readAllLines(file).joinToString("\n")
val original = compiler.createFile(content, file)
var (context, _) = compiler.compileFile(original, listOf(original))
var psi = original.findElementAt(46)!!
var kt = psi.parentsWithSelf.filterIsInstance<KtExpression>().first()
assertThat(context.getType(kt), hasToString("String"))
val edited = compiler.createFile(editedText, file)
context = compiler.compileFile(edited, listOf(edited)).first
psi = edited.findElementAt(46)!!
kt = psi.parentsWithSelf.filterIsInstance<KtExpression>().first()
assertThat(context.getType(kt), hasToString("Int"))
}
@Test fun editRef() {
val file1 = testResourcesRoot().resolve("hover/Recover.kt")
val content = Files.readAllLines(file1).joinToString("\n")
val original = compiler.createFile(content, file1)
val (context, _) = compiler.compileFile(original, listOf(original))
val function = original.findElementAt(49)!!.parentsWithSelf.filterIsInstance<KtNamedFunction>().first()
val scope = context.get(BindingContext.LEXICAL_SCOPE, function.bodyExpression)!!
val recompile = compiler.createDeclaration("""private fun singleExpressionFunction() = intFunction()""")
val (recompileContext, _) = compiler.compileExpression(recompile, scope, setOf(original))
val intFunctionRef = recompile.findElementAt(41)!!.parentsWithSelf.filterIsInstance<KtReferenceExpression>().first()
val target = recompileContext.get(BindingContext.REFERENCE_TARGET, intFunctionRef)!!
assertThat(target.name, hasToString("intFunction"))
}
}