Skip to content

Commit a513a2a

Browse files
author
yevhenii-nadtochii
committed
Extract common code of positive tests
1 parent 05bfe8f commit a513a2a

File tree

1 file changed

+50
-21
lines changed

1 file changed

+50
-21
lines changed

java-ksp/src/test/kotlin/io/spine/validation/java/ksp/ValidatorProcessorKotlinSpec.kt

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,49 +42,67 @@ internal class ValidatorProcessorKotlinSpec : ValidatorCompilationTest() {
4242
@Nested inner class
4343
Discover {
4444

45-
private val topLevelValidator = kotlinFile("TimestampValidator", """
46-
package io.spine.validation.java.ksp.test
47-
48-
import io.spine.validation.api.DetectedViolation
49-
import io.spine.validation.api.MessageValidator
50-
import io.spine.validation.api.Validator
51-
import com.google.protobuf.Timestamp
52-
45+
@Test
46+
fun `a top level validator`() = assertDiscovered(
47+
"""
5348
@Validator(Timestamp::class)
5449
public class TimestampValidator : MessageValidator<Timestamp> {
5550
public override fun validate(message: Timestamp): List<DetectedViolation> {
5651
return emptyList() // Always valid.
5752
}
58-
}
59-
""".trimIndent())
53+
}
54+
""".trimIndent(),
55+
"TimestampValidator"
56+
)
6057

6158
@Test
62-
fun `a top level validator`() {
63-
compilation.apply {
64-
sources = listOf(topLevelValidator)
65-
}
66-
59+
fun `a nested validator`() = assertDiscovered(
60+
"""
61+
public class Outer {
62+
@Validator(Timestamp::class)
63+
public class TimestampValidator : MessageValidator<Timestamp> {
64+
public override fun validate(message: Timestamp): List<DetectedViolation> {
65+
return emptyList() // Always valid.
66+
}
67+
}
68+
}
69+
""".trimIndent(),
70+
"Outer.TimestampValidator"
71+
)
72+
73+
private fun assertDiscovered(declaration: String, expected: String) {
74+
val sourceFile = kotlinFile("TimestampValidator", """
75+
package $VALIDATOR_PACKAGE
76+
77+
$IMPORTS
78+
79+
$declaration
80+
""".trimIndent()
81+
)
82+
83+
compilation.sources = listOf(sourceFile)
6784
val result = compilation.compileSilently()
6885
result.exitCode shouldBe OK
6986

7087
val discovered = compilation.kspSourcesDir
7188
.resolve("resources")
7289
.resolve(DiscoveredValidators.RESOURCES_LOCATION)
90+
7391
with(discovered) {
7492
exists() shouldBe true
75-
readText().trim() shouldBe "com.google.protobuf.Timestamp:io.spine.validation.java.ksp.test.TimestampValidator"
93+
readText() shouldBe "$MESSAGE_CLASS:$VALIDATOR_PACKAGE.$expected\n"
7694
}
7795
}
78-
79-
@Test
80-
fun `a nested validator`() {
81-
82-
}
8396
}
8497

8598
@Nested inner class
8699
RejectValidator {
87100

101+
@Test
102+
fun `declared as inner class`() {
103+
104+
}
105+
88106
@Test
89107
fun `not implementing 'MessageValidator' interface`() {
90108

@@ -105,4 +123,15 @@ internal class ValidatorProcessorKotlinSpec : ValidatorCompilationTest() {
105123

106124
}
107125
}
126+
127+
companion object {
128+
private const val MESSAGE_CLASS = "com.google.protobuf.Timestamp"
129+
private const val VALIDATOR_PACKAGE = "io.spine.validation.java.ksp.test"
130+
private val IMPORTS = """
131+
import io.spine.validation.api.DetectedViolation
132+
import io.spine.validation.api.MessageValidator
133+
import io.spine.validation.api.Validator
134+
import com.google.protobuf.Timestamp
135+
""".trimIndent()
136+
}
108137
}

0 commit comments

Comments
 (0)