Skip to content

Commit

Permalink
Fix merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Schwarz committed Jan 31, 2022
1 parent 8286190 commit a90418b
Showing 1 changed file with 47 additions and 23 deletions.
Expand Up @@ -4,13 +4,15 @@ import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.compileAndLint
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatExceptionOfType
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import java.util.regex.PatternSyntaxException

class LateinitUsageSpec : Spek({
class LateinitUsageSpec {

describe("LateinitUsage rule") {
@Nested
inner class `LateinitUsage rule` {
val code = """
import kotlin.SinceKotlin
Expand All @@ -20,65 +22,87 @@ class LateinitUsageSpec : Spek({
}
"""

it("should report lateinit usages") {
@Test
fun `should report lateinit usages`() {
val findings = LateinitUsage().compileAndLint(code)
assertThat(findings).hasSize(2)
}

it("should only report lateinit property with no @SinceKotlin annotation") {
val findings = LateinitUsage(TestConfig(mapOf(EXCLUDE_ANNOTATED_PROPERTIES to listOf("SinceKotlin")))).compileAndLint(code)
@Test
@DisplayName("should only report lateinit property with no @SinceKotlin annotation")
fun `should only report lateinit property with no SinceKotlin annotation`() {
val findings =
LateinitUsage(TestConfig(mapOf(EXCLUDE_ANNOTATED_PROPERTIES to listOf("SinceKotlin")))).compileAndLint(
code
)
assertThat(findings).hasSize(1)
}

it("should only report lateinit properties not matching kotlin.*") {
val findings = LateinitUsage(TestConfig(mapOf(EXCLUDE_ANNOTATED_PROPERTIES to listOf("kotlin.*")))).compileAndLint(code)
@Test
@DisplayName("should only report lateinit properties not matching kotlin.*")
fun `should only report lateinit properties not matching any kotlin annotation`() {
val findings =
LateinitUsage(TestConfig(mapOf(EXCLUDE_ANNOTATED_PROPERTIES to listOf("kotlin.*")))).compileAndLint(code)
assertThat(findings).hasSize(1)
}

it("should only report lateinit properties matching kotlin.SinceKotlin") {
@Test
fun `should only report lateinit properties matching kotlin_SinceKotlin`() {
val config = TestConfig(mapOf(EXCLUDE_ANNOTATED_PROPERTIES to listOf("kotlin.SinceKotlin")))
val findings = LateinitUsage(config).compileAndLint(code)
assertThat(findings).hasSize(1)
}

it("should only report lateinit property with no @SinceKotlin annotation with config string") {
val findings = LateinitUsage(TestConfig(mapOf(EXCLUDE_ANNOTATED_PROPERTIES to "SinceKotlin"))).compileAndLint(code)
@Test
fun `should only report lateinit property with no @SinceKotlin annotation with config string`() {
val findings =
LateinitUsage(TestConfig(mapOf(EXCLUDE_ANNOTATED_PROPERTIES to "SinceKotlin"))).compileAndLint(code)
assertThat(findings).hasSize(1)
}

it("should only report lateinit property with no @SinceKotlin annotation containing whitespaces with config string") {
val findings = LateinitUsage(TestConfig(mapOf(EXCLUDE_ANNOTATED_PROPERTIES to " SinceKotlin "))).compileAndLint(code)
@Test
fun `should only report lateinit property with no @SinceKotlin annotation containing whitespaces with config string`() {
val findings =
LateinitUsage(TestConfig(mapOf(EXCLUDE_ANNOTATED_PROPERTIES to " SinceKotlin "))).compileAndLint(code)
assertThat(findings).hasSize(1)
}

it("should report lateinit properties not matching the exclude pattern") {
val findings = LateinitUsage(TestConfig(mapOf(EXCLUDE_ANNOTATED_PROPERTIES to "IgnoreThis"))).compileAndLint(code)
@Test
fun `should report lateinit properties not matching the exclude pattern`() {
val findings =
LateinitUsage(TestConfig(mapOf(EXCLUDE_ANNOTATED_PROPERTIES to "IgnoreThis"))).compileAndLint(code)
assertThat(findings).hasSize(2)
}

it("should report lateinit properties when ignoreOnClassesPattern does not match") {
val findings = LateinitUsage(TestConfig(mapOf(IGNORE_ON_CLASSES_PATTERN to "[\\w]+Test1234"))).compileAndLint(code)
@Test
fun `should report lateinit properties when ignoreOnClassesPattern does not match`() {
val findings =
LateinitUsage(TestConfig(mapOf(IGNORE_ON_CLASSES_PATTERN to "[\\w]+Test1234"))).compileAndLint(code)
assertThat(findings).hasSize(2)
}

it("should not report lateinit properties when ignoreOnClassesPattern does match") {
val findings = LateinitUsage(TestConfig(mapOf(IGNORE_ON_CLASSES_PATTERN to "[\\w]+Test"))).compileAndLint(code)
@Test
fun `should not report lateinit properties when ignoreOnClassesPattern does match`() {
val findings =
LateinitUsage(TestConfig(mapOf(IGNORE_ON_CLASSES_PATTERN to "[\\w]+Test"))).compileAndLint(code)
assertThat(findings).isEmpty()
}

it("should fail when enabled with faulty regex pattern") {
@Test
fun `should fail when enabled with faulty regex pattern`() {
assertThatExceptionOfType(PatternSyntaxException::class.java).isThrownBy {
LateinitUsage(TestConfig(mapOf(IGNORE_ON_CLASSES_PATTERN to "*Test"))).compileAndLint(code)
}
}

it("should not fail when disabled with faulty regex pattern") {
@Test
fun `should not fail when disabled with faulty regex pattern`() {
val configValues = mapOf("active" to "false", IGNORE_ON_CLASSES_PATTERN to "*Test")
val findings = LateinitUsage(TestConfig(configValues)).compileAndLint(code)
assertThat(findings).isEmpty()
}
}
})
}

private const val EXCLUDE_ANNOTATED_PROPERTIES = "excludeAnnotatedProperties"
private const val IGNORE_ON_CLASSES_PATTERN = "ignoreOnClassesPattern"

0 comments on commit a90418b

Please sign in to comment.