forked from detekt/detekt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KtCompilerSpec.kt
118 lines (98 loc) · 4.25 KB
/
KtCompilerSpec.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package io.github.detekt.parser
import io.github.davidburstrom.contester.ConTesterDriver
import io.github.detekt.psi.BASE_PATH
import io.github.detekt.psi.LINE_SEPARATOR
import io.github.detekt.psi.RELATIVE_PATH
import io.github.detekt.test.utils.resourceAsPath
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatIllegalArgumentException
import org.jetbrains.kotlin.com.intellij.psi.PsiErrorElement
import org.jetbrains.kotlin.psi.KtTreeVisitorVoid
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
class KtCompilerSpec {
@AfterEach
internal fun tearDown() {
ConTesterDriver.cleanUp()
}
@Test
fun `parallel construction of KtCompilers should be thread safe`() {
val thread1 = ConTesterDriver.thread { KtCompiler() }
val thread2 = ConTesterDriver.thread { KtCompiler() }
ConTesterDriver.runToBreakpoint(thread1, "DetektPomModel.registerExtensionPoint")
ConTesterDriver.runUntilBlockedOrTerminated(thread2)
ConTesterDriver.join(thread1)
}
@Nested
inner class `Kotlin Compiler` {
val path = resourceAsPath("/cases")
private val ktCompiler = KtCompiler()
@Test
fun `Kotlin file with LF line separators has extra user data`() {
val ktFile = ktCompiler.compile(path, path.resolve("DefaultLf.kt"))
assertThat(ktFile.getUserData(LINE_SEPARATOR)).isEqualTo("\n")
assertThat(ktFile.getUserData(RELATIVE_PATH))
.isEqualTo("DefaultLf.kt")
assertThat(ktFile.getUserData(BASE_PATH))
.endsWith("cases")
}
@Test
fun `Kotlin file with CRLF line separators has extra user data`() {
val ktFile = ktCompiler.compile(path, path.resolve("DefaultCrLf.kt"))
assertThat(ktFile.getUserData(LINE_SEPARATOR)).isEqualTo("\r\n")
assertThat(ktFile.getUserData(RELATIVE_PATH))
.isEqualTo("DefaultCrLf.kt")
assertThat(ktFile.getUserData(BASE_PATH))
.endsWith("cases")
}
@Test
fun `Kotlin file with LF line separators does not store extra data for relative path if not provided`() {
val ktFile = ktCompiler.compile(null, path.resolve("DefaultLf.kt"))
assertThat(ktFile.getUserData(LINE_SEPARATOR)).isEqualTo("\n")
assertThat(ktFile.getUserData(RELATIVE_PATH)).isNull()
assertThat(ktFile.getUserData(BASE_PATH)).isNull()
}
@Test
fun `Kotlin file with CRLF line separators does not store extra data for relative path if not provided`() {
val ktFile = ktCompiler.compile(null, path.resolve("DefaultCrLf.kt"))
assertThat(ktFile.getUserData(LINE_SEPARATOR)).isEqualTo("\r\n")
assertThat(ktFile.getUserData(RELATIVE_PATH)).isNull()
assertThat(ktFile.getUserData(BASE_PATH)).isNull()
}
@Test
fun `throws an exception for an invalid sub path`() {
assertThatIllegalArgumentException()
.isThrownBy { ktCompiler.compile(path, path) }
.withMessageStartingWith("Given sub path (")
.withMessageEndingWith(") should be a regular file!")
}
@Test
fun `parses with errors for non kotlin files`() {
val cssPath = resourceAsPath("css")
val ktFile = ktCompiler.compile(cssPath, cssPath.resolve("test.css"))
val errors = mutableListOf<PsiErrorElement>()
ktFile.accept(object : KtTreeVisitorVoid() {
override fun visitErrorElement(element: PsiErrorElement) {
errors.add(element)
}
})
assertThat(errors).isNotEmpty()
}
}
@Nested
inner class `line ending detection` {
@Test
fun `detects CRLF line endings`() {
assertThat("1\r\n2".determineLineSeparator()).isEqualTo("\r\n")
}
@Test
fun `detects LF line endings`() {
assertThat("1\n2".determineLineSeparator()).isEqualTo("\n")
}
@Test
fun `detects CR line endings`() {
assertThat("1\r2".determineLineSeparator()).isEqualTo("\r")
}
}
}