forked from detekt/detekt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KotlinCoreEnvironmentTestSpec.kt
86 lines (72 loc) · 3.27 KB
/
KotlinCoreEnvironmentTestSpec.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
package io.gitlab.arturbosch.detekt.rules
import io.github.detekt.test.utils.compileContentForTest
import org.assertj.core.api.AbstractAssert
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.cli.jvm.compiler.NoScopeRecordCliBindingTrace
import org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull
import org.jetbrains.kotlin.resolve.descriptorUtil.getAllSuperclassesWithoutAny
import org.jetbrains.kotlin.resolve.lazy.declarations.FileBasedDeclarationProviderFactory
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
internal class KotlinCoreEnvironmentTestSpec {
private val code = """
import org.assertj.core.api.AbstractAssert
class CustomAssert(actual: String) : AbstractAssert<CustomAssert, String>(actual, CustomAssert::class.java)
""".trimIndent()
@Nested
@KotlinCoreEnvironmentTest
inner class `Without additional types`(private val env: KotlinCoreEnvironment) {
@Test
fun `no assertj api are available on classpath`() {
val actual = code.compileAndGetSuperTypes(env)
assertThat(actual).isEmpty()
}
}
@Nested
inner class `With additional types` {
val expected = listOf("org.assertj.core.api.AbstractAssert")
@Nested
@KotlinCoreEnvironmentTest(additionalTypes = [AbstractAssert::class])
inner class `Only addiontial types`(private val env: KotlinCoreEnvironment) {
@Test
fun `types from detekt api are available on classpath`() {
val actual = code.compileAndGetSuperTypes(env)
assertThat(actual).isEqualTo(expected)
}
}
@Nested
@KotlinCoreEnvironmentTest(additionalTypes = [AbstractAssert::class, CharRange::class])
inner class `Also types that are already available`(private val env: KotlinCoreEnvironment) {
@Test
fun `no conflict if types are added multiple times`() {
val actual = code.compileAndGetSuperTypes(env)
assertThat(actual).isEqualTo(expected)
}
}
}
private fun String.compileAndGetSuperTypes(env: KotlinCoreEnvironment): List<String> {
val ktFile = compileContentForTest(this)
val ktClass = ktFile.findChildByClass(KtClass::class.java)!!
val bindingContext = env.getContextForPaths(listOf(ktFile))
return bindingContext[BindingContext.CLASS, ktClass]
?.getAllSuperclassesWithoutAny()
?.map { checkNotNull(it.fqNameOrNull()).toString() }
.orEmpty()
}
}
// copied from KotlinCoreEnvironmentExtensions.kt
// this is not ideal
private fun KotlinCoreEnvironment.getContextForPaths(paths: List<KtFile>): BindingContext =
TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(
this.project,
paths,
NoScopeRecordCliBindingTrace(),
this.configuration,
this::createPackagePartProvider,
::FileBasedDeclarationProviderFactory
).bindingContext