forked from detekt/detekt
/
SignaturesSpec.kt
128 lines (116 loc) · 3.94 KB
/
SignaturesSpec.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
124
125
126
127
128
package io.gitlab.arturbosch.detekt.api.internal
import io.gitlab.arturbosch.detekt.api.Debt
import io.gitlab.arturbosch.detekt.api.Issue
import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.api.Severity
import io.gitlab.arturbosch.detekt.test.compileAndLint
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.junit.jupiter.api.Test
class SignaturesSpec {
private var result = ""
private val subject = TestRule { result = it }
@Test
fun `function with type reference`() {
subject.compileAndLint(functionWithTypeReference())
assertThat(result).isEqualTo(signatureWithTypeReference())
}
@Test
fun `function without type reference`() {
subject.compileAndLint(functionWithoutTypeReference())
assertThat(result).isEqualTo(signatureWithoutTypeReference())
}
@Test
fun `function throws exception`() {
assertThatThrownBy {
subject.compileAndLint(functionWontCompile())
}
.isInstanceOf(IllegalArgumentException::class.java)
.hasMessageContaining("Error building function signature")
}
}
private class TestRule(val block: (String) -> Unit) : Rule() {
override val issue = Issue(javaClass.simpleName, Severity.CodeSmell, "", Debt.TWENTY_MINS)
override fun visitNamedFunction(function: KtNamedFunction) {
block(function.buildFullSignature())
}
}
private fun signatureWithTypeReference() = """
Test.kt${'$'}TestClass.Companion${'$'}@JvmStatic @Parameterized.Parameters(name = "parameterName") /** * Function KDoc */ fun data(): List<Array<out Any?>>
""".trimIndent()
private fun signatureWithoutTypeReference() = """
Test.kt${'$'}TestClass.Companion${'$'}@JvmStatic @Parameterized.Parameters(name = "parameterName") /** * Function KDoc */ fun data()
""".trimIndent()
private fun functionWithTypeReference() = """
@RunWith(Parameterized::class)
class TestClass {
companion object {
@JvmStatic
@Parameterized.Parameters(name = "parameterName")
/**
* Function KDoc
*/
fun data(): List<Array<out Any?>> =
/**
* more description
* more description
* more description
* more description
* more description
*/
listOf(
arrayOf(1, 2, 3, 4),
arrayOf(11, 22, 33, 44)
)
}
}
""".trimIndent()
private fun functionWithoutTypeReference() = """
@RunWith(Parameterized::class)
class TestClass {
companion object {
@JvmStatic
@Parameterized.Parameters(name = "parameterName")
/**
* Function KDoc
*/
fun data() =
/**
* more description
* more description
* more description
* more description
* more description
*/
listOf(
arrayOf(1, 2, 3, 4),
arrayOf(11, 22, 33, 44)
)
}
}
""".trimIndent()
private fun functionWontCompile() = """
@RunWith(Parameterized.class)
class TestClass {
companion object {
@JvmStatic
@Parameterized.Parameters(name = "parameterName")
/**
* Function KDoc
*/
fun data() =
/**
* more description
* more description
* more description
* more description
* more description
*/
listOf(
arrayOf(1, 2, 3, 4),
arrayOf(11, 22, 33, 44)
)
}
}
""".trimIndent()