1
- package com.github.ivy.explicit
1
+ package com.github.ivy.explicit.rule
2
2
3
3
import io.gitlab.arturbosch.detekt.api.Config
4
4
import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest
5
5
import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext
6
6
import io.kotest.matchers.collections.shouldHaveSize
7
- import io.kotest.matchers.string.shouldNotBeBlank
7
+ import io.kotest.matchers.shouldBe
8
8
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
9
9
import org.junit.jupiter.api.Test
10
10
11
11
@KotlinCoreEnvironmentTest
12
12
internal class DataClassFunctionsRuleTest (private val env : KotlinCoreEnvironment ) {
13
13
14
14
@Test
15
- fun `reports data class having functions ` () {
15
+ fun `reports data class having one function ` () {
16
16
val code = """
17
17
data class A(
18
18
val x: Int
@@ -23,7 +23,9 @@ internal class DataClassFunctionsRuleTest(private val env: KotlinCoreEnvironment
23
23
val findings = DataClassFunctionsRule (Config .empty).compileAndLintWithContext(env, code)
24
24
findings shouldHaveSize 1
25
25
val message = findings.first().message
26
- message.shouldNotBeBlank()
26
+ message shouldBe """
27
+ Data class 'A' should not contain functions. Data classes should only model data and not define behavior. Found: function 'a()'.
28
+ """ .trimIndent()
27
29
}
28
30
29
31
@Test
@@ -38,7 +40,34 @@ internal class DataClassFunctionsRuleTest(private val env: KotlinCoreEnvironment
38
40
}
39
41
40
42
@Test
41
- fun `doesn't report data class with companion object` () {
43
+ fun `doesn't report data class with override functions` () {
44
+ val code = """
45
+ data class DisplayLoan(
46
+ val loan: Loan,
47
+ val amountPaid: Double,
48
+ val currencyCode: String? = getDefaultFIATCurrency().currencyCode,
49
+ val formattedDisplayText: String = "",
50
+ val percentPaid: Double = 0.0
51
+ ) : Reorderable {
52
+ override fun getItemOrderNum(): Double {
53
+ return loan.orderNum
54
+ }
55
+
56
+ override fun withNewOrderNum(newOrderNum: Double): Reorderable {
57
+ return this.copy(
58
+ loan = loan.copy(
59
+ orderNum = newOrderNum
60
+ )
61
+ )
62
+ }
63
+ }
64
+ """
65
+ val findings = DataClassFunctionsRule (Config .empty).compileAndLintWithContext(env, code)
66
+ findings shouldHaveSize 0
67
+ }
68
+
69
+ @Test
70
+ fun `doesn't report data class with functions in companion object` () {
42
71
val code = """
43
72
data class A(
44
73
val x: Int
0 commit comments