Skip to content

Commit

Permalink
Use inline code snippets instead of case files (#1976)
Browse files Browse the repository at this point in the history
This commit inlines the code snippets for data class related rules in
the style ruleset.
  • Loading branch information
schalkms authored and arturbosch committed Oct 6, 2019
1 parent 46c49f8 commit 8daf49e
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 128 deletions.
Expand Up @@ -11,10 +11,6 @@ enum class Case(val file: String) {
CollapsibleIfsNegative("/cases/CollapsibleIfsNegative.kt"),
ComplexMethods("/cases/ComplexMethods.kt"),
ConstInObjects("/cases/ConstInObjects.kt"),
DataClassContainsFunctionsPositive("/cases/DataClassContainsFunctionsPositive.kt"),
DataClassContainsFunctionsNegative("/cases/DataClassContainsFunctionsNegative.kt"),
DataClassShouldBeImmutablePositive("/cases/DataClassShouldBeImmutablePositive.kt"),
DataClassShouldBeImmutableNegative("/cases/DataClassShouldBeImmutableNegative.kt"),
Default("/cases/Default.kt"),
Empty("/cases/Empty.kt"),
EmptyKtFile("/cases/EmptyKtFile.kt"),
Expand Down
@@ -1,8 +1,7 @@
package io.gitlab.arturbosch.detekt.rules.style

import io.gitlab.arturbosch.detekt.rules.Case
import io.gitlab.arturbosch.detekt.test.compileAndLint
import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.lint
import org.assertj.core.api.Assertions.assertThat
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
Expand All @@ -12,20 +11,60 @@ class DataClassContainsFunctionsSpec : Spek({

describe("DataClassContainsFunctions rule") {

val path = Case.DataClassContainsFunctionsPositive.path()
context("flagged functions in data class") {
val code = """
data class C(val s: String) {
fun f() {}
it("reports valid data class w/o conversion function") {
assertThat(subject.lint(path)).hasSize(3)
data class Nested(val i: Int) {
fun toConversion() = C(i.toString())
}
}
"""

it("reports valid data class w/o conversion function") {
assertThat(subject.compileAndLint(code)).hasSize(2)
}

it("reports valid data class w/ conversion function") {
val config = TestConfig(mapOf(DataClassContainsFunctions.CONVERSION_FUNCTION_PREFIX to "to"))
val rule = DataClassContainsFunctions(config)
assertThat(rule.compileAndLint(code)).hasSize(1)
}
}

it("does not report a data class without a function") {
val code = "data class C(val i: Int)"
assertThat(subject.compileAndLint(code)).isEmpty()
}

it("reports valid data class w/ conversion function") {
val config = TestConfig(mapOf(DataClassContainsFunctions.CONVERSION_FUNCTION_PREFIX to "to"))
val rule = DataClassContainsFunctions(config)
assertThat(rule.lint(path)).hasSize(2)
it("does not report a non-data class without a function") {
val code = """
class C {
fun f() {}
}
"""
assertThat(subject.compileAndLint(code)).isEmpty()
}

it("does not report data class w/o conversion function") {
assertThat(subject.lint(Case.DataClassContainsFunctionsNegative.path())).hasSize(0)
it("does not report a data class with overridden functions") {
val code = """
data class C(val i: Int) {
override fun hashCode(): Int {
return super.hashCode()
}
override fun equals(other: Any?): Boolean {
return super.equals(other)
}
override fun toString(): String {
return super.toString()
}
}
"""
assertThat(subject.compileAndLint(code)).isEmpty()
}
}
})
@@ -1,7 +1,6 @@
package io.gitlab.arturbosch.detekt.rules.style

import io.gitlab.arturbosch.detekt.rules.Case
import io.gitlab.arturbosch.detekt.test.lint
import io.gitlab.arturbosch.detekt.test.compileAndLint
import org.assertj.core.api.Assertions.assertThat
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
Expand All @@ -11,14 +10,69 @@ class DataClassShouldBeImmutableSpec : Spek({

describe("DataClassShouldBeImmutable rule") {

it("reports positive cases") {
val path = Case.DataClassShouldBeImmutablePositive.path()
assertThat(subject.lint(path)).hasSize(4)
it("reports mutable variable in primary constructor") {
val code = "data class C(var i: Int)"
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("does not report negative cases") {
val path = Case.DataClassShouldBeImmutableNegative.path()
assertThat(subject.lint(path)).hasSize(0)
it("reports mutable property in class body") {
val code = """
data class C(val i: Int) {
var s: String? = null
}
"""
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("reports mutable private property in class body") {
val code = """
data class C(val i: Int) {
var s: String = ""
private set
}
"""
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("reports lateinit property in class body") {
val code = """
data class C(val i: Int) {
lateinit var s: String
}
"""
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("does not report readonly variable in primary constructor") {
val code = "data class C(val i: Int)"
assertThat(subject.compileAndLint(code)).isEmpty()
}

it("does not report readonly property in class body") {
val code = """
data class C(val i: Int) {
val s: String? = null
}
"""
assertThat(subject.compileAndLint(code)).isEmpty()
}

it("does not report lazy property in class body") {
val code = """
data class C(val i: Int) {
val s: String by lazy { "" }
}
"""
assertThat(subject.compileAndLint(code)).isEmpty()
}

it("does not report mutable variables in non-data classes") {
val code = """
class C(var i: Int) {
val s: String by lazy { "" }
}
"""
assertThat(subject.compileAndLint(code)).isEmpty()
}
}
})

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 8daf49e

Please sign in to comment.