Skip to content

Commit

Permalink
Migrate detekt-rules-style tests to JUnit (#4575)
Browse files Browse the repository at this point in the history
* Migrate detekt-rules-style tests to JUnit

* Replace Spek in test cases with MockK

* Revert inadvertent changes

* Update detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/MagicNumberSpec.kt

Co-authored-by: marschwar <marschwar@users.noreply.github.com>

* Update detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/MagicNumberSpec.kt

Co-authored-by: marschwar <marschwar@users.noreply.github.com>

* Update detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/MagicNumberSpec.kt

Co-authored-by: marschwar <marschwar@users.noreply.github.com>

Co-authored-by: Chao Zhang <zhangchao6865@gmail.com>
Co-authored-by: marschwar <marschwar@users.noreply.github.com>
  • Loading branch information
3 people committed Apr 5, 2022
1 parent e9d26d0 commit 0be7775
Show file tree
Hide file tree
Showing 76 changed files with 4,045 additions and 2,276 deletions.
3 changes: 1 addition & 2 deletions detekt-rules-style/build.gradle.kts
Expand Up @@ -9,6 +9,5 @@ dependencies {
testImplementation(projects.detektMetrics)
testImplementation(projects.detektTest)
testImplementation(libs.mockk)
testImplementation(libs.bundles.testImplementation)
testRuntimeOnly(libs.spek.runner)
testImplementation(libs.assertj)
}

Large diffs are not rendered by default.

Expand Up @@ -3,15 +3,17 @@ package io.gitlab.arturbosch.detekt.rules.style
import io.gitlab.arturbosch.detekt.api.Config
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
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test

class ClassOrderingSpec : Spek({
val subject by memoized { ClassOrdering(Config.empty) }
class ClassOrderingSpec {
val subject = ClassOrdering(Config.empty)

describe("ClassOrdering rule") {
@Nested
inner class `ClassOrdering rule` {

it("does not report when class contents are in expected order with property first") {
@Test
fun `does not report when class contents are in expected order with property first`() {
val code = """
class InOrder(private val x: String) {
val y = x
Expand All @@ -33,7 +35,8 @@ class ClassOrderingSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}

it("does not report when class contents are in expected order with class initializer first") {
@Test
fun `does not report when class contents are in expected order with class initializer first`() {
val code = """
class InOrder(private val x: String) {
init {
Expand All @@ -55,7 +58,8 @@ class ClassOrderingSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}

it("reports when class initializer block is out of order") {
@Test
fun `reports when class initializer block is out of order`() {
val code = """
class OutOfOrder(private val x: String) {
val y = x
Expand All @@ -81,7 +85,8 @@ class ClassOrderingSpec : Spek({
)
}

it("reports when secondary constructor is out of order") {
@Test
fun `reports when secondary constructor is out of order`() {
val code = """
class OutOfOrder(private val x: String) {
constructor(z: Int): this(z.toString())
Expand Down Expand Up @@ -110,7 +115,8 @@ class ClassOrderingSpec : Spek({
)
}

it("reports when method is out of order") {
@Test
fun `reports when method is out of order`() {
val code = """
class OutOfOrder(private val x: String) {
fun returnX() = x
Expand Down Expand Up @@ -139,7 +145,8 @@ class ClassOrderingSpec : Spek({
.isEqualTo("secondary constructor should be declared before method declarations.")
}

it("reports when companion object is out of order") {
@Test
fun `reports when companion object is out of order`() {
val code = """
class OutOfOrder(private val x: String) {
val y = x
Expand All @@ -163,7 +170,8 @@ class ClassOrderingSpec : Spek({
assertThat(findings[0].message).isEqualTo("method `returnX()` should be declared before companion object.")
}

it("does not report nested class order") {
@Test
fun `does not report nested class order`() {
val code = """
class OutOfOrder(private val x: String) {
val y = x
Expand All @@ -185,7 +193,8 @@ class ClassOrderingSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(0)
}

it("does not report anonymous object order") {
@Test
fun `does not report anonymous object order`() {
val code = """
class OutOfOrder(private val x: String) {
val y = x
Expand All @@ -207,7 +216,8 @@ class ClassOrderingSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(0)
}

it("report all issues with interleaving nested class") {
@Test
fun `report all issues with interleaving nested class`() {
val code = """
class MultipleMisorders(private val x: String) {
companion object {
Expand Down Expand Up @@ -238,7 +248,8 @@ class ClassOrderingSpec : Spek({
.isEqualTo("property `y` should be declared before companion object.")
}

it("does report all issues in a class with multiple misorderings") {
@Test
fun `does report all issues in a class with multiple misorderings`() {
val code = """
class MultipleMisorders(private val x: String) {
companion object {
Expand All @@ -263,4 +274,4 @@ class ClassOrderingSpec : Spek({
.isEqualTo("property `y` should be declared before companion object.")
}
}
})
}
Expand Up @@ -3,15 +3,17 @@ package io.gitlab.arturbosch.detekt.rules.style
import io.gitlab.arturbosch.detekt.api.Config
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
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test

class CollapsibleIfStatementsSpec : Spek({
val subject by memoized { CollapsibleIfStatements(Config.empty) }
class CollapsibleIfStatementsSpec {
val subject = CollapsibleIfStatements(Config.empty)

describe("CollapsibleIfStatements rule") {
@Nested
inner class `CollapsibleIfStatements rule` {

it("reports if statements which can be merged") {
@Test
fun `reports if statements which can be merged`() {
val code = """
fun f() {
if (true) {
Expand All @@ -23,7 +25,8 @@ class CollapsibleIfStatementsSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("reports nested if statements which can be merged") {
@Test
fun `reports nested if statements which can be merged`() {
val code = """
fun f() {
if (true) {
Expand All @@ -37,7 +40,8 @@ class CollapsibleIfStatementsSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("does not report else-if") {
@Test
fun `does not report else-if`() {
val code = """
fun f() {
if (true) {}
Expand All @@ -49,7 +53,8 @@ class CollapsibleIfStatementsSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}

it("does not report if-else") {
@Test
fun `does not report if-else`() {
val code = """
fun f() {
if (true) {
Expand All @@ -60,7 +65,8 @@ class CollapsibleIfStatementsSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}

it("does not report if-elseif-else") {
@Test
fun `does not report if-elseif-else`() {
val code = """
fun f() {
if (true) {
Expand All @@ -72,7 +78,8 @@ class CollapsibleIfStatementsSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}

it("does not report if with statements in the if body") {
@Test
fun `does not report if with statements in the if body`() {
val code = """
fun f() {
if (true) {
Expand All @@ -84,7 +91,8 @@ class CollapsibleIfStatementsSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}

it("does not report nested if-else") {
@Test
fun `does not report nested if-else`() {
val code = """
fun f() {
if (true) {
Expand All @@ -96,7 +104,8 @@ class CollapsibleIfStatementsSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}

it("does not report nested if-elseif") {
@Test
fun `does not report nested if-elseif`() {
val code = """
fun f() {
if (true) {
Expand All @@ -108,4 +117,4 @@ class CollapsibleIfStatementsSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}
}
})
}
Expand Up @@ -3,17 +3,19 @@ package io.gitlab.arturbosch.detekt.rules.style
import io.gitlab.arturbosch.detekt.test.TestConfig
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
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test

private const val CONVERSION_FUNCTION_PREFIX = "conversionFunctionPrefix"

class DataClassContainsFunctionsSpec : Spek({
val subject by memoized { DataClassContainsFunctions() }
class DataClassContainsFunctionsSpec {
val subject = DataClassContainsFunctions()

describe("DataClassContainsFunctions rule") {
@Nested
inner class `DataClassContainsFunctions rule` {

context("flagged functions in data class") {
@Nested
inner class `flagged functions in data class` {
val code = """
data class C(val s: String) {
fun f() {}
Expand All @@ -24,23 +26,27 @@ class DataClassContainsFunctionsSpec : Spek({
}
"""

it("reports valid data class w/ conversion function") {
@Test
fun `reports valid data class with conversion function`() {
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("reports valid data class w/o conversion function") {
@Test
fun `reports valid data class without conversion function`() {
val config = TestConfig(mapOf(CONVERSION_FUNCTION_PREFIX to ""))
val rule = DataClassContainsFunctions(config)
assertThat(rule.compileAndLint(code)).hasSize(2)
}
}

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

it("does not report a non-data class without a function") {
@Test
fun `does not report a non-data class without a function`() {
val code = """
class C {
fun f() {}
Expand All @@ -49,7 +55,8 @@ class DataClassContainsFunctionsSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}

it("does not report a data class with overridden functions") {
@Test
fun `does not report a data class with overridden functions`() {
val code = """
data class C(val i: Int) {
Expand All @@ -69,4 +76,4 @@ class DataClassContainsFunctionsSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}
}
})
}
Expand Up @@ -2,20 +2,23 @@ package io.gitlab.arturbosch.detekt.rules.style

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
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test

class DataClassShouldBeImmutableSpec : Spek({
val subject by memoized { DataClassShouldBeImmutable() }
class DataClassShouldBeImmutableSpec {
val subject = DataClassShouldBeImmutable()

describe("DataClassShouldBeImmutable rule") {
@Nested
inner class `DataClassShouldBeImmutable rule` {

it("reports mutable variable in primary constructor") {
@Test
fun `reports mutable variable in primary constructor`() {
val code = "data class C(var i: Int)"
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("reports mutable property in class body") {
@Test
fun `reports mutable property in class body`() {
val code = """
data class C(val i: Int) {
var s: String? = null
Expand All @@ -24,7 +27,8 @@ class DataClassShouldBeImmutableSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("reports mutable private property in class body") {
@Test
fun `reports mutable private property in class body`() {
val code = """
data class C(val i: Int) {
var s: String = ""
Expand All @@ -34,7 +38,8 @@ class DataClassShouldBeImmutableSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("reports lateinit property in class body") {
@Test
fun `reports lateinit property in class body`() {
val code = """
data class C(val i: Int) {
lateinit var s: String
Expand All @@ -43,12 +48,14 @@ class DataClassShouldBeImmutableSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("does not report readonly variable in primary constructor") {
@Test
fun `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") {
@Test
fun `does not report readonly property in class body`() {
val code = """
data class C(val i: Int) {
val s: String? = null
Expand All @@ -57,7 +64,8 @@ class DataClassShouldBeImmutableSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}

it("does not report lazy property in class body") {
@Test
fun `does not report lazy property in class body`() {
val code = """
data class C(val i: Int) {
val s: String by lazy { "" }
Expand All @@ -66,7 +74,8 @@ class DataClassShouldBeImmutableSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}

it("does not report mutable variables in non-data classes") {
@Test
fun `does not report mutable variables in non-data classes`() {
val code = """
class C(var i: Int) {
val s: String by lazy { "" }
Expand All @@ -75,4 +84,4 @@ class DataClassShouldBeImmutableSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}
}
})
}

0 comments on commit 0be7775

Please sign in to comment.