/
EntitySpec.kt
123 lines (99 loc) · 4.67 KB
/
EntitySpec.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
119
120
121
122
123
package io.gitlab.arturbosch.detekt.api
import io.github.detekt.test.utils.compileForTest
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
import org.jetbrains.kotlin.psi.psiUtil.findDescendantOfType
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import kotlin.io.path.Path
import kotlin.io.path.absolute
import kotlin.io.path.relativeToOrSelf
import kotlin.io.path.toPath
class EntitySpec {
private val path = Path("src/test/resources/EntitySpecFixture.kt").absolute()
private val basePath = EntitySpec::class.java.getResource("/")!!.toURI().toPath()
private val relativePath = path.relativeToOrSelf(basePath)
private val code = compileForTest(path)
@Nested
inner class `Named functions` {
private val functions = code.collectDescendantsOfType<KtNamedFunction>()
@Test
fun `includes full function header, class name and filename`() {
val memberFunction = functions.first { it.name == "memberFun" }
assertThat(Entity.atName(memberFunction).signature)
.isEqualTo("EntitySpecFixture.kt\$C\$private fun memberFun(): Int")
}
@Test
fun `includes full function header and filename for a top level function`() {
val topLevelFunction = functions.first { it.name == "topLevelFun" }
assertThat(Entity.atName(topLevelFunction).signature)
.isEqualTo("EntitySpecFixture.kt\$fun topLevelFun(number: Int)")
}
@Test
fun `includes function name in entity compact`() {
val memberFunction = functions.first { it.name == "memberFun" }
assertThat(Entity.atName(memberFunction).compact())
.isEqualTo("[memberFun] at $path:5:17")
}
@Test
fun `toString gives all details`() {
val memberFunction = functions.first { it.name == "memberFun" }
assertThat(Entity.atName(memberFunction).toString())
.isEqualTo(
"Entity(name=memberFun, signature=EntitySpecFixture.kt\$C\$private fun memberFun(): Int, " +
"location=Location(source=5:17, endSource=5:26, text=49:58, " +
"filePath=FilePath(absolutePath=$path, basePath=$basePath, relativePath=$relativePath)), " +
"ktElement=FUN)"
)
}
}
@Nested
inner class Classes {
private val clazz = requireNotNull(code.findDescendantOfType<KtClass>())
@Test
fun `includes full class signature`() {
assertThat(Entity.atName(clazz).signature).isEqualTo("EntitySpecFixture.kt\$C : Any")
}
@Test
fun `includes class name in entity compact`() {
assertThat(Entity.atName(clazz).compact()).isEqualTo("[C] at $path:3:7")
}
@Test
fun `toString gives all details`() {
assertThat(Entity.atName(clazz).toString())
.isEqualTo(
"Entity(name=C, signature=EntitySpecFixture.kt\$C : Any, " +
"location=Location(source=3:7, endSource=3:8, text=20:21, " +
"filePath=FilePath(absolutePath=$path, basePath=$basePath, relativePath=$relativePath)), " +
"ktElement=CLASS)"
)
}
}
@Nested
inner class Files {
@Test
fun `includes package and file name in entity signature`() {
val expectedResult = "EntitySpecFixture.kt\$test.EntitySpecFixture.kt"
assertThat(Entity.from(code).signature).isEqualTo(expectedResult)
assertThat(Entity.atPackageOrFirstDecl(code).signature).isEqualTo(expectedResult)
}
@Test
fun `includes file name in entity compact`() {
val expectedResult = "[EntitySpecFixture.kt] at $path:1:1"
assertThat(Entity.from(code).compact()).isEqualTo(expectedResult)
assertThat(Entity.atPackageOrFirstDecl(code).compact()).isEqualTo(expectedResult)
}
@Test
fun `toString gives all details`() {
assertThat(Entity.from(code).toString())
.isEqualTo(
"Entity(name=EntitySpecFixture.kt, signature=EntitySpecFixture.kt\$test.EntitySpecFixture.kt, " +
"location=Location(source=1:1, endSource=9:1, text=0:109, " +
"filePath=FilePath(absolutePath=$path, basePath=$basePath, relativePath=$relativePath)), " +
"ktElement=KtFile: EntitySpecFixture.kt)"
)
}
}
}