diff --git a/detekt-rules-style/build.gradle.kts b/detekt-rules-style/build.gradle.kts index d5bc1feba96c..eeaa48ff06af 100644 --- a/detekt-rules-style/build.gradle.kts +++ b/detekt-rules-style/build.gradle.kts @@ -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) } diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/CanBeNonNullableSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/CanBeNonNullableSpec.kt index 2081d25a5397..68b0faebd702 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/CanBeNonNullableSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/CanBeNonNullableSpec.kt @@ -1,22 +1,22 @@ package io.gitlab.arturbosch.detekt.rules.style import io.gitlab.arturbosch.detekt.api.Config -import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.compileAndLint import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext import org.assertj.core.api.Assertions.assertThat import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -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 CanBeNonNullableSpec : Spek({ - setupKotlinEnvironment() +@KotlinCoreEnvironmentTest +class CanBeNonNullableSpec(val env: KotlinCoreEnvironment) { + val subject = CanBeNonNullable(Config.empty) - val env: KotlinCoreEnvironment by memoized() - val subject by memoized { CanBeNonNullable(Config.empty) } - - describe("CanBeNonNullable Rule") { - it("does not report when there is no context") { + @Nested + inner class `CanBeNonNullable Rule` { + @Test + fun `does not report when there is no context`() { val code = """ class A { private var a: Int? = 5 @@ -28,8 +28,10 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - context("evaluating private vars") { - it("reports when class-level vars are never assigned nullable values") { + @Nested + inner class `evaluating private properties` { + @Test + fun `reports when class-level vars are never assigned nullable values`() { val code = """ class A(bVal: Int) { private var a: Int? = 5 @@ -50,7 +52,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(2) } - it("reports when vars utilize non-nullable delegate values") { + @Test + fun `reports when vars utilize non-nullable delegate values`() { val code = """ import kotlin.reflect.KProperty @@ -80,7 +83,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(2) } - it("reports when file-level vars are never assigned nullable values") { + @Test + fun `reports when file-level vars are never assigned nullable values`() { val code = """ private var fileA: Int? = 5 private var fileB: Int? = 5 @@ -92,7 +96,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(2) } - it("does not report when class-level vars are assigned nullable values") { + @Test + fun `does not report when class-level vars are assigned nullable values`() { val code = """ import kotlin.random.Random @@ -124,7 +129,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report vars that utilize nullable delegate values") { + @Test + fun `does not report vars that utilize nullable delegate values`() { val code = """ class A(private var aDelegate: Int?) { private var a: Int? by this::aDelegate @@ -133,7 +139,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report when file-level vars are assigned nullable values") { + @Test + fun `does not report when file-level vars are assigned nullable values`() { val code = """ import kotlin.random.Random @@ -154,7 +161,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("reports when vars with private setters are never assigned nullable values") { + @Test + fun `reports when vars with private setters are never assigned nullable values`() { val code = """ class A { var a: Int? = 5 @@ -167,7 +175,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("does not report when vars with private setters are assigned nullable values") { + @Test + fun `does not report when vars with private setters are assigned nullable values`() { val code = """ class A { var a: Int? = 5 @@ -180,7 +189,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report when vars use public setters") { + @Test + fun `does not report when vars use public setters`() { val code = """ class A { var a: Int? = 5 @@ -192,7 +202,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report when vars use non-private setters") { + @Test + fun `does not report when vars use non-private setters`() { val code = """ class A { var a: Int? = 5 @@ -205,7 +216,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report when private vars are declared in the constructor") { + @Test + fun `does not report when private vars are declared in the constructor`() { val code = """ class A(private var a: Int?) { fun foo() { @@ -217,8 +229,10 @@ class CanBeNonNullableSpec : Spek({ } } - context("evaluating private vars") { - it("reports when class-level vals are set to non-nullable values") { + @Nested + inner class `evaluating public properties` { + @Test + fun `reports when class-level vals are set to non-nullable values`() { val code = """ class A(cVal: Int) { val a: Int? = 5 @@ -234,7 +248,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(3) } - it("reports when vals utilize non-nullable delegate values") { + @Test + fun `reports when vals utilize non-nullable delegate values`() { val code = """ class A { val a: Int? by lazy { @@ -245,14 +260,16 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("reports when file-level vals are set to non-nullable values") { + @Test + fun `reports when file-level vals are set to non-nullable values`() { val code = """ val fileA: Int? = 5 """ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("does not report when class-level vals are assigned a nullable value") { + @Test + fun `does not report when class-level vals are assigned a nullable value`() { val code = """ import kotlin.random.Random @@ -270,7 +287,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report when vals utilize nullable delegate values") { + @Test + fun `does not report when vals utilize nullable delegate values`() { val code = """ import kotlin.random.Random @@ -284,14 +302,16 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report when file-level vals are assigned a nullable value") { + @Test + fun `does not report when file-level vals are assigned a nullable value`() { val code = """ val fileA: Int? = null """ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report when vals are declared non-nullable") { + @Test + fun `does not report when vals are declared non-nullable`() { val code = """ class A { val a: Int = 5 @@ -300,14 +320,16 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report when vals are declared in the constructor") { + @Test + fun `does not report when vals are declared in the constructor`() { val code = """ class A(private val a: Int?) """ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("reports when vals with getters never return nullable values") { + @Test + fun `reports when vals with getters never return nullable values`() { val code = """ class A { val a: Int? @@ -327,7 +349,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(3) } - it("does not report when vals with getters return potentially-nullable values") { + @Test + fun `does not report when vals with getters return potentially-nullable values`() { val code = """ import kotlin.random.Random @@ -351,7 +374,8 @@ class CanBeNonNullableSpec : Spek({ } } - it("does not report open properties") { + @Test + fun `does not report open properties`() { val code = """ abstract class A { open val a: Int? = 5 @@ -361,7 +385,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report properties whose initial assignment derives from unsafe non-Java code") { + @Test + fun `does not report properties whose initial assignment derives from unsafe non-Java code`() { val code = """ class A(msg: String?) { private val e = Exception(msg) @@ -374,7 +399,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report interface properties") { + @Test + fun `does not report interface properties`() { val code = """ interface A { val a: Int? @@ -384,9 +410,12 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - context("nullable function parameters") { - context("using a de-nullifier") { - it("does report when a param is de-nullified with a postfix expression") { + @Nested + inner class `nullable function parameters` { + @Nested + inner class `using a de-nullifier` { + @Test + fun `does report when a param is de-nullified with a postfix expression`() { val code = """ fun foo(a: Int?) { val b = a!! + 2 @@ -395,7 +424,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("does report when a param is de-nullified with a dot-qualified expression") { + @Test + fun `does report when a param is de-nullified with a dot-qualified expression`() { val code = """ fun foo(a: Int?) { val b = a!!.plus(2) @@ -406,7 +436,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(2) } - it("does report when a de-nullifier precondition is called on the param") { + @Test + fun `does report when a de-nullifier precondition is called on the param`() { val code = """ fun foo(a: Int?, b: Int?) { val aNonNull = requireNotNull(a) @@ -416,7 +447,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(2) } - it("does not report a double-bang call the field of a non-null param") { + @Test + fun `does not report a double-bang call the field of a non-null param`() { val code = """ class A(val a: Int?) @@ -427,7 +459,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report on overridden function parameter") { + @Test + fun `does not report on overridden function parameter`() { val code = """ interface A { fun foo(a: Int?) @@ -443,9 +476,12 @@ class CanBeNonNullableSpec : Spek({ } } - context("using a null-safe expression") { - context("in initializer") { - it("does not report when the safe-qualified expression is the only expression of the function") { + @Nested + inner class `using a null-safe expression` { + @Nested + inner class `in initializer` { + @Test + fun `does not report when the safe-qualified expression is the only expression of the function`() { val code = """ class A { val foo = "BAR" @@ -457,8 +493,10 @@ class CanBeNonNullableSpec : Spek({ } } - context("in a non-return statement") { - it("does report when the safe-qualified expression is the only expression of the function") { + @Nested + inner class `in a non-return statement` { + @Test + fun `does report when the safe-qualified expression is the only expression of the function`() { val code = """ class A(val foo: String) @@ -469,7 +507,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("does not report when the safe-qualified expression is within a lambda") { + @Test + fun `does not report when the safe-qualified expression is within a lambda`() { val code = """ class A { fun doFoo(callback: () -> Unit) { @@ -486,7 +525,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report when the safe-qualified expression is not the only expression of the function") { + @Test + fun `does not report when the safe-qualified expression is not the only expression of the function`() { val code = """ class A { fun doFoo() { println("FOO") } @@ -500,8 +540,11 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } } - context("in a return statement") { - it("does not report when the safe-qualified expression is the only expression of the function") { + + @Nested + inner class `in a return statement` { + @Test + fun `does not report when the safe-qualified expression is the only expression of the function`() { val code = """ class A { val foo = "BAR" @@ -516,9 +559,12 @@ class CanBeNonNullableSpec : Spek({ } } - context("when statements") { - context("without a subject") { - it("does not report when the parameter is checked on nullity") { + @Nested + inner class `when statements` { + @Nested + inner class `without a subject` { + @Test + fun `does not report when the parameter is checked on nullity`() { val code = """ fun foo(a: Int?) { when { @@ -529,7 +575,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report when the parameter is checked on nullity in a reversed manner") { + @Test + fun `does not report when the parameter is checked on nullity in a reversed manner`() { val code = """ fun foo(a: Int?) { when { @@ -540,7 +587,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report when the parameter is checked on nullity with multiple clauses") { + @Test + fun `does not report when the parameter is checked on nullity with multiple clauses`() { val code = """ fun foo(a: Int?, other: Int) { when { @@ -551,7 +599,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does report when the parameter is only checked on non-nullity") { + @Test + fun `does report when the parameter is only checked on non-nullity`() { val code = """ fun foo(a: Int?) { when { @@ -562,7 +611,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("does report when the parameter is only checked on non-nullity with multiple clauses") { + @Test + fun `does report when the parameter is only checked on non-nullity with multiple clauses`() { val code = """ fun foo(a: Int?) { when { @@ -573,7 +623,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("does not report when the parameter is checked on non-nullity with an else statement") { + @Test + fun `does not report when the parameter is checked on non-nullity with an else statement`() { val code = """ fun foo(a: Int?) { when { @@ -585,7 +636,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report on nullable type matching") { + @Test + fun `does not report on nullable type matching`() { val code = """ fun foo(a: Int?) { when { @@ -602,7 +654,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does report on non-null type matching") { + @Test + fun `does report on non-null type matching`() { val code = """ fun foo(a: Int?) { when { @@ -613,7 +666,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("does report on non-null type matching with multiple clauses") { + @Test + fun `does report on non-null type matching with multiple clauses`() { val code = """ fun foo(a: Int?) { when { @@ -624,7 +678,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("does not report on non-null type matching with an else statement") { + @Test + fun `does not report on non-null type matching with an else statement`() { val code = """ fun foo(a: Int?) { when { @@ -637,8 +692,10 @@ class CanBeNonNullableSpec : Spek({ } } - context("with a subject") { - it("does not report when the parameter is checked on nullity") { + @Nested + inner class `with a subject` { + @Test + fun `does not report when the parameter is checked on nullity`() { val code = """ fun foo(a: Int?) { when (a) { @@ -649,7 +706,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report on nullable type matching") { + @Test + fun `does not report on nullable type matching`() { val code = """ fun foo(a: Int?) { when (a) { @@ -666,7 +724,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does report on non-null type matching") { + @Test + fun `does report on non-null type matching`() { val code = """ fun foo(a: Int?) { when(a) { @@ -677,7 +736,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("does not report on non-null type matching with an else statement") { + @Test + fun `does not report on non-null type matching with an else statement`() { val code = """ fun foo(a: Int?) { when(a) { @@ -691,8 +751,10 @@ class CanBeNonNullableSpec : Spek({ } } - context("if-statements") { - it("does not report when the parameter is checked on nullity") { + @Nested + inner class `if-statements` { + @Test + fun `does not report when the parameter is checked on nullity`() { val code = """ fun foo(a: Int?) { if (a == null) { @@ -709,7 +771,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report when the if-check is in the else statement") { + @Test + fun `does not report when the if-check is in the else statement`() { val code = """ fun foo(num: Int, a: Int?) { if (num % 2 == 0) { @@ -722,7 +785,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does report when the parameter is only checked on non-nullity in a function") { + @Test + fun `does report when the parameter is only checked on non-nullity in a function`() { val code = """ fun foo(a: Int?) { if (a != null) { @@ -739,7 +803,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(2) } - it("does report when the parameter is only checked on non-nullity with multiple clauses") { + @Test + fun `does report when the parameter is only checked on non-nullity with multiple clauses`() { val code = """ fun foo(a: Int?, other: Int) { if (a != null && other % 2 == 0) { @@ -750,7 +815,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("does not report when the parameter is checked on non-nullity with an else statement") { + @Test + fun `does not report when the parameter is checked on non-nullity with an else statement`() { val code = """ fun foo(a: Int?) { if (a != null) { @@ -763,7 +829,8 @@ class CanBeNonNullableSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report when there are other expressions after the non-null check") { + @Test + fun `does not report when there are other expressions after the non-null check`() { val code = """ fun foo(a: Int?) { if (a != null) { @@ -777,4 +844,4 @@ class CanBeNonNullableSpec : Spek({ } } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ClassOrderingSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ClassOrderingSpec.kt index b9c6723d02d1..1112b4a1c0f1 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ClassOrderingSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ClassOrderingSpec.kt @@ -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 @@ -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 { @@ -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 @@ -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()) @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 { @@ -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 { @@ -263,4 +274,4 @@ class ClassOrderingSpec : Spek({ .isEqualTo("property `y` should be declared before companion object.") } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/CollapsibleIfStatementsSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/CollapsibleIfStatementsSpec.kt index 07486f7df414..cb35de8edfdf 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/CollapsibleIfStatementsSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/CollapsibleIfStatementsSpec.kt @@ -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) { @@ -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) { @@ -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) {} @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -108,4 +117,4 @@ class CollapsibleIfStatementsSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/DataClassContainsFunctionsSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/DataClassContainsFunctionsSpec.kt index ed22b74e6491..74d0761d2799 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/DataClassContainsFunctionsSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/DataClassContainsFunctionsSpec.kt @@ -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() {} @@ -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() {} @@ -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) { @@ -69,4 +76,4 @@ class DataClassContainsFunctionsSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/DataClassShouldBeImmutableSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/DataClassShouldBeImmutableSpec.kt index 2d365f69d438..2696641127a8 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/DataClassShouldBeImmutableSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/DataClassShouldBeImmutableSpec.kt @@ -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 @@ -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 = "" @@ -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 @@ -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 @@ -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 { "" } @@ -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 { "" } @@ -75,4 +84,4 @@ class DataClassShouldBeImmutableSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/DestructuringDeclarationWithTooManyEntriesSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/DestructuringDeclarationWithTooManyEntriesSpec.kt index 5859c8aabdc6..c1f487fd2dd9 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/DestructuringDeclarationWithTooManyEntriesSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/DestructuringDeclarationWithTooManyEntriesSpec.kt @@ -3,19 +3,22 @@ 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 MAX_DESTRUCTURING_ENTRIES = "maxDestructuringEntries" -class DestructuringDeclarationWithTooManyEntriesSpec : Spek({ - val subject by memoized { DestructuringDeclarationWithTooManyEntries() } +class DestructuringDeclarationWithTooManyEntriesSpec { + val subject = DestructuringDeclarationWithTooManyEntries() - describe("DestructuringDeclarationWithTooManyEntries rule") { + @Nested + inner class `DestructuringDeclarationWithTooManyEntries rule` { - context("default configuration") { + @Nested + inner class `default configuration` { - it("does not report destructuring declarations with 2 or 3 entries") { + @Test + fun `does not report destructuring declarations with 2 or 3 entries`() { val code = """ fun testFun() { val (x, y) = Pair(3, 4) @@ -31,7 +34,8 @@ class DestructuringDeclarationWithTooManyEntriesSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("reports destructuring declarations with more than 3 entries") { + @Test + fun `reports destructuring declarations with more than 3 entries`() { val code = """ fun testFun() { data class ManyElements(val a: Int, val b: Int, val c: Int, val d: Int) @@ -46,7 +50,8 @@ class DestructuringDeclarationWithTooManyEntriesSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("does not report destructuring declarations in lambdas with 2 or 3 entries") { + @Test + fun `does not report destructuring declarations in lambdas with 2 or 3 entries`() { val code = """ fun testFun() { val items = listOf(Pair(3, 4)) @@ -60,7 +65,8 @@ class DestructuringDeclarationWithTooManyEntriesSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("reports destructuring declarations in lambdas with more than 3 entries") { + @Test + fun `reports destructuring declarations in lambdas with more than 3 entries`() { val code = """ fun testFun() { data class ManyElements(val a: Int, val b: Int, val c: Int, val d: Int) @@ -78,15 +84,16 @@ class DestructuringDeclarationWithTooManyEntriesSpec : Spek({ } } - context("maxDestructuringEntries = 2") { + @Nested + inner class `maxDestructuringEntries = 2` { - val configuredRule by memoized { + val configuredRule = DestructuringDeclarationWithTooManyEntries( TestConfig(mapOf(MAX_DESTRUCTURING_ENTRIES to "2")) ) - } - it("does not report destructuring declarations with 2 entries") { + @Test + fun `does not report destructuring declarations with 2 entries`() { val code = """ fun testFun() { val (x, y) = Pair(3, 4) @@ -97,7 +104,8 @@ class DestructuringDeclarationWithTooManyEntriesSpec : Spek({ assertThat(configuredRule.compileAndLint(code)).isEmpty() } - it("reports destructuring declarations with more than 2 entries") { + @Test + fun `reports destructuring declarations with more than 2 entries`() { val code = """ fun testFun() { val (a, b, c) = Triple(1, 2, 3) @@ -110,4 +118,4 @@ class DestructuringDeclarationWithTooManyEntriesSpec : Spek({ } } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/EqualsNullCallSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/EqualsNullCallSpec.kt index 6cf9d2794122..74bad95baa7b 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/EqualsNullCallSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/EqualsNullCallSpec.kt @@ -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 EqualsNullCallSpec : Spek({ - val subject by memoized { EqualsNullCall(Config.empty) } +class EqualsNullCallSpec { + val subject = EqualsNullCall(Config.empty) - describe("EqualsNullCall rule") { + @Nested + inner class `EqualsNullCall rule` { - it("reports equals call with null as parameter") { + @Test + fun `reports equals call with null as parameter`() { val code = """ fun x(a: String) { a.equals(null) @@ -20,7 +22,8 @@ class EqualsNullCallSpec : Spek({ assertThat(subject.compileAndLint(code).size).isEqualTo(1) } - it("reports nested equals call with null as parameter") { + @Test + fun `reports nested equals call with null as parameter`() { val code = """ fun x(a: String, b: String) { a.equals(b.equals(null)) @@ -29,7 +32,8 @@ class EqualsNullCallSpec : Spek({ assertThat(subject.compileAndLint(code).size).isEqualTo(1) } - it("does not report equals call with parameter of type string") { + @Test + fun `does not report equals call with parameter of type string`() { val code = """ fun x(a: String, b: String) { a.equals(b) @@ -38,4 +42,4 @@ class EqualsNullCallSpec : Spek({ assertThat(subject.compileAndLint(code).size).isEqualTo(0) } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/EqualsOnSignatureLineSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/EqualsOnSignatureLineSpec.kt index 126f46007054..0bd708bffe0e 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/EqualsOnSignatureLineSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/EqualsOnSignatureLineSpec.kt @@ -3,16 +3,19 @@ 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 EqualsOnSignatureLineSpec : Spek({ - val subject by memoized { EqualsOnSignatureLine(Config.empty) } +class EqualsOnSignatureLineSpec { + val subject = EqualsOnSignatureLine(Config.empty) - describe("EqualsOnSignatureLine rule") { + @Nested + inner class `EqualsOnSignatureLine rule` { - context("with expression syntax and without a return type") { - it("reports when the equals is on a new line") { + @Nested + inner class `with expression syntax and without a return type` { + @Test + fun `reports when the equals is on a new line`() { val findings = subject.compileAndLint( """ fun foo() @@ -22,7 +25,8 @@ class EqualsOnSignatureLineSpec : Spek({ assertThat(findings).hasSize(1) } - it("does not report when the equals is on the same line") { + @Test + fun `does not report when the equals is on the same line`() { val findings = subject.compileAndLint( """ fun foo() = 1 @@ -35,8 +39,10 @@ class EqualsOnSignatureLineSpec : Spek({ } } - context("with expression syntax and with a return type") { - it("reports when the equals is on a new line") { + @Nested + inner class `with expression syntax and with a return type` { + @Test + fun `reports when the equals is on a new line`() { val findings = subject.compileAndLint( """ fun one(): Int @@ -56,7 +62,8 @@ class EqualsOnSignatureLineSpec : Spek({ assertThat(findings).hasSize(3) } - it("does not report when the equals is on the same line") { + @Test + fun `does not report when the equals is on the same line`() { val findings = subject.compileAndLint( """ fun one(): Int = @@ -93,8 +100,10 @@ class EqualsOnSignatureLineSpec : Spek({ } } - context("with expression syntax and with a where clause") { - it("reports when the equals is on a new line") { + @Nested + inner class `with expression syntax and with a where clause` { + @Test + fun `reports when the equals is on a new line`() { val findings = subject.compileAndLint( """ fun one(): Int where V : Number @@ -115,7 +124,8 @@ class EqualsOnSignatureLineSpec : Spek({ assertThat(findings).hasSize(3) } - it("does not report when the equals is on the same line") { + @Test + fun `does not report when the equals is on the same line`() { val findings = subject.compileAndLint( """ fun one(): Int where V : Number = @@ -131,7 +141,8 @@ class EqualsOnSignatureLineSpec : Spek({ } } - it("does not report non-expression functions") { + @Test + fun `does not report non-expression functions`() { val findings = subject.compileAndLint( """ fun foo() { @@ -151,4 +162,4 @@ class EqualsOnSignatureLineSpec : Spek({ assertThat(findings).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExplicitCollectionElementAccessMethodSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExplicitCollectionElementAccessMethodSpec.kt index 1f8fba73d817..6d8ba99b97ad 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExplicitCollectionElementAccessMethodSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExplicitCollectionElementAccessMethodSpec.kt @@ -1,24 +1,27 @@ package io.gitlab.arturbosch.detekt.rules.style +import io.github.detekt.test.utils.createEnvironment import io.github.detekt.test.utils.resourceAsPath import io.gitlab.arturbosch.detekt.api.Config -import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext import io.gitlab.arturbosch.detekt.test.lintWithContext import org.assertj.core.api.Assertions.assertThat import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -import org.spekframework.spek2.Spek -import org.spekframework.spek2.style.specification.describe +import org.junit.jupiter.api.AfterAll +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test -class ExplicitCollectionElementAccessMethodSpec : Spek({ - setupKotlinEnvironment(additionalJavaSourceRootPath = resourceAsPath("java")) +@KotlinCoreEnvironmentTest +class ExplicitCollectionElementAccessMethodSpec(val env: KotlinCoreEnvironment) { + val subject = ExplicitCollectionElementAccessMethod(Config.empty) - val env: KotlinCoreEnvironment by memoized() - val subject by memoized { ExplicitCollectionElementAccessMethod(Config.empty) } + @Nested + inner class `Kotlin map` { - describe("Kotlin map") { - - it("reports map element access with get method") { + @Test + fun `reports map element access with get method`() { val code = """ fun f() { val map = mapOf() @@ -28,7 +31,8 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("does not report safe map element access") { + @Test + fun `does not report safe map element access`() { val code = """ fun f() { val map = mapOf() @@ -38,7 +42,8 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("reports map set method usage with unused return value") { + @Test + fun `reports map set method usage with unused return value`() { val code = """ fun f() { val map = mutableMapOf() @@ -48,7 +53,8 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("reports map put method usage with unused return value") { + @Test + fun `reports map put method usage with unused return value`() { val code = """ fun f() { val map = mutableMapOf() @@ -58,7 +64,8 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("does not report map put method usage with variable assignment") { + @Test + fun `does not report map put method usage with variable assignment`() { val code = """ fun f() { val map = mutableMapOf() @@ -68,7 +75,8 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report map put method with used return value") { + @Test + fun `does not report map put method with used return value`() { val code = """ fun f(): Boolean { val map = mutableMapOf() @@ -78,7 +86,8 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("reports map element access with get method of non-abstract map") { + @Test + fun `reports map element access with get method of non-abstract map`() { val code = """ fun f() { val map = hashMapOf() @@ -88,7 +97,8 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("reports map element insert with put method of non-abstract map") { + @Test + fun `reports map element insert with put method of non-abstract map`() { val code = """ fun f() { val map = hashMapOf() @@ -98,7 +108,9 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("does not report map access with []") { + @Test + @DisplayName("does not report map access with []") + fun noReportMapAccessWithBrackets() { val code = """ fun f() { val map = mapOf() @@ -108,7 +120,9 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report map insert with []") { + @Test + @DisplayName("does not report map insert with []") + fun noReportMapInsertWithBrackets() { val code = """ fun f() { val map = mutableMapOf() @@ -118,7 +132,8 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("reports map element access with get method from map in a chain") { + @Test + fun `reports map element access with get method from map in a chain`() { val code = """ fun f() { val map = mapOf() @@ -128,7 +143,8 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("reports map element access with get method from non-abstract map") { + @Test + fun `reports map element access with get method from non-abstract map`() { val code = """ fun f() { val map = linkedMapOf() @@ -138,7 +154,8 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("does not report calls on implicit receiver") { + @Test + fun `does not report calls on implicit receiver`() { val code = """ fun f() { val map = mapOf() @@ -149,8 +166,10 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ } } - describe("Kotlin list") { - it("reports list element access with get method") { + @Nested + inner class `Kotlin list` { + @Test + fun `reports list element access with get method`() { val code = """ fun f() { val list = listOf() @@ -160,7 +179,8 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("reports mutable list element access with get method") { + @Test + fun `reports mutable list element access with get method`() { val code = """ fun f() { val list = mutableListOf() @@ -170,7 +190,9 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("does not report element access with []") { + @Test + @DisplayName("does not report element access with []") + fun noReportElementAccessWithBrackets() { val code = """ fun f() { val list = listOf() @@ -180,7 +202,8 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("reports element access with get method of non-abstract list") { + @Test + fun `reports element access with get method of non-abstract list`() { val code = """ fun f() { val list = arrayListOf() @@ -190,7 +213,8 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("does not report calls on implicit receiver") { + @Test + fun `does not report calls on implicit receiver`() { val code = """ fun f() { val list = listOf() @@ -201,9 +225,11 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ } } - describe("Java map") { + @Nested + inner class `Java map` { - it("reports map element access with get method") { + @Test + fun `reports map element access with get method`() { val code = """ fun f() { val map = java.util.HashMap() @@ -213,7 +239,8 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("reports map set method usage with unused return value") { + @Test + fun `reports map set method usage with unused return value`() { val code = """ fun f() { val map = java.util.HashMap() @@ -223,7 +250,8 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("reports map put method usage with unused return value") { + @Test + fun `reports map put method usage with unused return value`() { val code = """ fun f() { val map = java.util.HashMap() @@ -233,7 +261,9 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("does not report map access with []") { + @Test + @DisplayName("does not report map access with []") + fun noReportMapAccessWithBrackets() { val code = """ fun f() { val map = java.util.HashMap() @@ -243,7 +273,9 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report map insert with []") { + @Test + @DisplayName("does not report map insert with []") + fun noReportMapInsertWithBrackets() { val code = """ fun f() { val map = java.util.HashMap() @@ -253,7 +285,8 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("reports map element access with get method from map in a chain") { + @Test + fun `reports map element access with get method from map in a chain`() { val code = """ fun f() { val map = java.util.HashMap() @@ -264,9 +297,11 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ } } - describe("custom operators") { + @Nested + inner class `custom operators` { - it("reports custom get operator") { + @Test + fun `reports custom get operator`() { val code = """ class Custom { operator fun get(i: Int) = 42 } fun f() { @@ -277,7 +312,8 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("does not report non-operator get method") { + @Test + fun `does not report non-operator get method`() { val code = """ class Custom { fun get(i: Int) = 42 } fun f() { @@ -288,7 +324,8 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("reports custom set operator with unused return value") { + @Test + fun `reports custom set operator with unused return value`() { val code = """ class Custom { operator fun set(key: String, value: String) {} } fun f() { @@ -299,7 +336,8 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("does not report non-operator set method") { + @Test + fun `does not report non-operator set method`() { val code = """ class Custom { fun set(key: String, value: String) {} } fun f() { @@ -311,9 +349,11 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ } } - describe("Java list") { + @Nested + inner class `Java list` { - it("reports list element access with get method") { + @Test + fun `reports list element access with get method`() { val code = """ fun f() { val list = java.util.ArrayList() @@ -323,7 +363,9 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("does not report element access with []") { + @Test + @DisplayName("does not report element access with []") + fun noReportElementAccessWithBrackets() { val code = """ fun f() { val list = java.util.ArrayList() @@ -334,9 +376,11 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ } } - describe("edge cases") { + @Nested + inner class `edge cases` { - it("does not crash for getter") { + @Test + fun `does not crash for getter`() { val code = """ class A { val i: Int get() = 1 + 2 @@ -346,7 +390,8 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not crash for fluent api") { + @Test + fun `does not crash for fluent api`() { val code = """ val string = "" .toString() @@ -354,7 +399,8 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report for unresolvable code") { + @Test + fun `does not report for unresolvable code`() { val code = """ fun f() { val unknownType = UnknownType() @@ -364,7 +410,8 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ assertThat(subject.lintWithContext(env, code)).isEmpty() } - it("does not report for put functions without caller") { + @Test + fun `does not report for put functions without caller`() { val code = """ fun put() { } fun f() { @@ -374,16 +421,30 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report if the function has 3 or more arguments and it's defined in java - #4288") { - val code = """ - import com.example.fromjava.Rect + @Nested + inner class JavaSourceTests { - fun foo() { - val rect = Rect() - rect.set(0, 1, 2) - } - """ - assertThat(subject.lintWithContext(env, code)).isEmpty() + private val environmentWrapper = + createEnvironment(additionalJavaSourceRootPaths = listOf(resourceAsPath("java").toFile())) + private val customEnv = environmentWrapper.env + + @AfterAll + fun disposeEnvironment() { + environmentWrapper.dispose() + } + + @Test + fun `does not report if the function has 3 or more arguments and it's defined in java - #4288`() { + val code = """ + import com.example.fromjava.Rect + + fun foo() { + val rect = Rect() + rect.set(0, 1, 2) + } + """ + assertThat(subject.lintWithContext(customEnv, code)).isEmpty() + } } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExplicitItLambdaParameterSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExplicitItLambdaParameterSpec.kt index 71eb07a786ea..40ab8dedb37b 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExplicitItLambdaParameterSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExplicitItLambdaParameterSpec.kt @@ -3,15 +3,18 @@ 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 ExplicitItLambdaParameterSpec : Spek({ - val subject by memoized { ExplicitItLambdaParameter(Config.empty) } +class ExplicitItLambdaParameterSpec { + val subject = ExplicitItLambdaParameter(Config.empty) - describe("ExplicitItLambdaParameter rule") { - context("single parameter lambda with name `it` declared explicitly") { - it("reports when parameter type is not declared") { + @Nested + inner class `ExplicitItLambdaParameter rule` { + @Nested + inner class `single parameter lambda with name 'it' declared explicitly` { + @Test + fun `reports when parameter type is not declared`() { val findings = subject.compileAndLint( """ fun f() { @@ -21,7 +24,9 @@ class ExplicitItLambdaParameterSpec : Spek({ ) assertThat(findings).hasSize(1) } - it("reports when parameter type is declared explicitly") { + + @Test + fun `reports when parameter type is declared explicitly`() { val findings = subject.compileAndLint( """ fun f() { @@ -32,8 +37,11 @@ class ExplicitItLambdaParameterSpec : Spek({ assertThat(findings).hasSize(1) } } - context("no parameter declared explicitly") { - it("does not report implicit `it` parameter usage") { + + @Nested + inner class `no parameter declared explicitly` { + @Test + fun `does not report implicit 'it' parameter usage`() { val findings = subject.compileAndLint( """ fun f() { @@ -47,8 +55,10 @@ class ExplicitItLambdaParameterSpec : Spek({ } } - context("multiple parameters one of which with name `it` declared explicitly") { - it("reports when parameter types are not declared") { + @Nested + inner class `multiple parameters one of which with name 'it' declared explicitly` { + @Test + fun `reports when parameter types are not declared`() { val findings = subject.compileAndLint( """ fun f() { @@ -58,7 +68,9 @@ class ExplicitItLambdaParameterSpec : Spek({ ) assertThat(findings).hasSize(1) } - it("reports when parameter types are declared explicitly") { + + @Test + fun `reports when parameter types are declared explicitly`() { val findings = subject.compileAndLint( """ fun f() { @@ -70,4 +82,4 @@ class ExplicitItLambdaParameterSpec : Spek({ } } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExpressionBodySyntaxSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExpressionBodySyntaxSpec.kt index f660d3ad1d4b..cfc8fa20b7d8 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExpressionBodySyntaxSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExpressionBodySyntaxSpec.kt @@ -4,19 +4,22 @@ import io.gitlab.arturbosch.detekt.api.Config 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 INCLUDE_LINE_WRAPPING = "includeLineWrapping" -class ExpressionBodySyntaxSpec : Spek({ - val subject by memoized { ExpressionBodySyntax(Config.empty) } +class ExpressionBodySyntaxSpec { + val subject = ExpressionBodySyntax(Config.empty) - describe("ExpressionBodySyntax rule") { + @Nested + inner class `ExpressionBodySyntax rule` { - context("several return statements") { + @Nested + inner class `several return statements` { - it("reports constant return") { + @Test + fun `reports constant return`() { assertThat( subject.compileAndLint( """ @@ -28,7 +31,8 @@ class ExpressionBodySyntaxSpec : Spek({ ).hasSize(1) } - it("reports return statement with method chain") { + @Test + fun `reports return statement with method chain`() { assertThat( subject.compileAndLint( """ @@ -40,7 +44,8 @@ class ExpressionBodySyntaxSpec : Spek({ ).hasSize(1) } - it("reports return statements with conditionals") { + @Test + fun `reports return statements with conditionals`() { assertThat( subject.compileAndLint( """ @@ -55,7 +60,8 @@ class ExpressionBodySyntaxSpec : Spek({ ).hasSize(2) } - it("does not report multiple if statements") { + @Test + fun `does not report multiple if statements`() { assertThat( subject.compileAndLint( """ @@ -68,7 +74,8 @@ class ExpressionBodySyntaxSpec : Spek({ ).isEmpty() } - it("does not report when using shortcut return") { + @Test + fun `does not report when using shortcut return`() { assertThat( subject.compileAndLint( """ @@ -83,7 +90,8 @@ class ExpressionBodySyntaxSpec : Spek({ } } - context("several return statements with multiline method chain") { + @Nested + inner class `several return statements with multiline method chain` { val code = """ fun stuff(): String { @@ -93,17 +101,20 @@ class ExpressionBodySyntaxSpec : Spek({ } """ - it("does not report with the default configuration") { + @Test + fun `does not report with the default configuration`() { assertThat(subject.compileAndLint(code)).isEmpty() } - it("reports with includeLineWrapping = true configuration") { + @Test + fun `reports with includeLineWrapping = true configuration`() { val config = TestConfig(mapOf(INCLUDE_LINE_WRAPPING to "true")) assertThat(ExpressionBodySyntax(config).compileAndLint(code)).hasSize(1) } } - context("several return statements with multiline when expression") { + @Nested + inner class `several return statements with multiline when expression` { val code = """ fun stuff(arg: Int): Int { @@ -114,17 +125,20 @@ class ExpressionBodySyntaxSpec : Spek({ } """ - it("does not report with the default configuration") { + @Test + fun `does not report with the default configuration`() { assertThat(subject.compileAndLint(code)).isEmpty() } - it("reports with includeLineWrapping = true configuration") { + @Test + fun `reports with includeLineWrapping = true configuration`() { val config = TestConfig(mapOf(INCLUDE_LINE_WRAPPING to "true")) assertThat(ExpressionBodySyntax(config).compileAndLint(code)).hasSize(1) } } - context("several return statements with multiline if expression") { + @Nested + inner class `several return statements with multiline if expression` { val code = """ fun stuff(arg: Int): Int { @@ -133,14 +147,16 @@ class ExpressionBodySyntaxSpec : Spek({ } """ - it("does not report with the default configuration") { + @Test + fun `does not report with the default configuration`() { assertThat(subject.compileAndLint(code)).isEmpty() } - it("reports with includeLineWrapping = true configuration") { + @Test + fun `reports with includeLineWrapping = true configuration`() { val config = TestConfig(mapOf(INCLUDE_LINE_WRAPPING to "true")) assertThat(ExpressionBodySyntax(config).compileAndLint(code)).hasSize(1) } } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ForbiddenCommentSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ForbiddenCommentSpec.kt index 91f73370d40b..ef9d2c18cf0b 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ForbiddenCommentSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ForbiddenCommentSpec.kt @@ -3,14 +3,15 @@ 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.DisplayName +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test private const val VALUES = "values" private const val ALLOWED_PATTERNS = "allowedPatterns" private const val MESSAGE = "customMessage" -class ForbiddenCommentSpec : Spek({ +class ForbiddenCommentSpec { val todoColon = "// TODO: I need to fix this." val todo = "// TODO I need to fix this." @@ -21,41 +22,53 @@ class ForbiddenCommentSpec : Spek({ val stopShipColon = "// STOPSHIP: I need to fix this." val stopShip = "// STOPSHIP I need to fix this." - describe("ForbiddenComment rule") { + @Nested + inner class `ForbiddenComment rule` { - context("the default values are configured") { + @Nested + inner class `the default values are configured` { - it("should report TODO: usages") { + @Test + @DisplayName("should report TODO: usages") + fun reportTodoColon() { val findings = ForbiddenComment().compileAndLint(todoColon) assertThat(findings).hasSize(1) } - it("should not report TODO usages") { + @Test + fun `should not report TODO usages`() { val findings = ForbiddenComment().compileAndLint(todo) assertThat(findings).isEmpty() } - it("should report FIXME: usages") { + @Test + @DisplayName("should report FIXME: usages") + fun reportFixMe() { val findings = ForbiddenComment().compileAndLint(fixmeColon) assertThat(findings).hasSize(1) } - it("should not report FIXME usages") { + @Test + fun `should not report FIXME usages`() { val findings = ForbiddenComment().compileAndLint(fixme) assertThat(findings).isEmpty() } - it("should report STOPSHIP: usages") { + @Test + @DisplayName("should report STOPSHIP: usages") + fun reportStopShipColon() { val findings = ForbiddenComment().compileAndLint(stopShipColon) assertThat(findings).hasSize(1) } - it("should not report STOPSHIP usages") { + @Test + fun `should not report STOPSHIP usages`() { val findings = ForbiddenComment().compileAndLint(stopShip) assertThat(findings).isEmpty() } - it("should report violation in multiline comment") { + @Test + fun `should report violation in multiline comment`() { val code = """ /* TODO: I need to fix this. @@ -65,7 +78,8 @@ class ForbiddenCommentSpec : Spek({ assertThat(findings).hasSize(1) } - it("should report violation in KDoc") { + @Test + fun `should report violation in KDoc`() { val code = """ /** * TODO: I need to fix this. @@ -81,84 +95,135 @@ class ForbiddenCommentSpec : Spek({ } } - context("custom default values are configured") { + @Nested + inner class `custom default values are configured` { + val banana = "// Banana." - listOf( - TestConfig(mapOf(VALUES to "Banana")), - TestConfig(mapOf(VALUES to listOf("Banana"))) - ) - .forEach { config -> - val banana = "// Banana." + @Nested + inner class `when given Banana` { + val config = TestConfig(mapOf(VALUES to "Banana")) - it("should not report TODO: usages") { - val findings = ForbiddenComment(config).compileAndLint(todoColon) - assertThat(findings).isEmpty() - } + @Test + @DisplayName("should not report TODO: usages") + fun todoColon() { + val findings = ForbiddenComment(config).compileAndLint(todoColon) + assertThat(findings).isEmpty() + } - it("should not report FIXME: usages") { - val findings = ForbiddenComment(config).compileAndLint(fixmeColon) - assertThat(findings).isEmpty() - } + @Test + @DisplayName("should not report FIXME: usages") + fun fixmeColon() { + val findings = ForbiddenComment(config).compileAndLint(fixmeColon) + assertThat(findings).isEmpty() + } - it("should not report STOPME: usages") { - val findings = ForbiddenComment(config).compileAndLint(stopShipColon) - assertThat(findings).isEmpty() - } + @Test + @DisplayName("should not report STOPME: usages") + fun stopShipColon() { + val findings = ForbiddenComment(config).compileAndLint(stopShipColon) + assertThat(findings).isEmpty() + } - it("should report Banana usages") { - val findings = ForbiddenComment(config).compileAndLint(banana) - assertThat(findings).hasSize(1) - } + @Test + fun `should report Banana usages`() { + val findings = ForbiddenComment(config).compileAndLint(banana) + assertThat(findings).hasSize(1) + } - it("should report Banana usages regardless of case sensitive") { - val forbiddenComment = ForbiddenComment(TestConfig(mapOf(VALUES to "bAnAnA"))) - val findings = forbiddenComment.compileAndLint(banana) - assertThat(findings).hasSize(1) - } + @Test + fun `should report Banana usages regardless of case sensitive`() { + val forbiddenComment = ForbiddenComment(TestConfig(mapOf(VALUES to "bAnAnA"))) + val findings = forbiddenComment.compileAndLint(banana) + assertThat(findings).hasSize(1) + } + } + + @Nested + @DisplayName("when given listOf(\"banana\")") + inner class ListOfBanana { + val config = TestConfig(mapOf(VALUES to listOf("Banana"))) + + @Test + @DisplayName("should not report TODO: usages") + fun todoColon() { + val findings = ForbiddenComment(config).compileAndLint(todoColon) + assertThat(findings).isEmpty() } + + @Test + @DisplayName("should not report FIXME: usages") + fun fixmeColon() { + val findings = ForbiddenComment(config).compileAndLint(fixmeColon) + assertThat(findings).isEmpty() + } + + @Test + @DisplayName("should not report STOPME: usages") + fun stopShipColon() { + val findings = ForbiddenComment(config).compileAndLint(stopShipColon) + assertThat(findings).isEmpty() + } + + @Test + fun `should report Banana usages`() { + val findings = ForbiddenComment(config).compileAndLint(banana) + assertThat(findings).hasSize(1) + } + + @Test + fun `should report Banana usages regardless of case sensitive`() { + val forbiddenComment = ForbiddenComment(TestConfig(mapOf(VALUES to "bAnAnA"))) + val findings = forbiddenComment.compileAndLint(banana) + assertThat(findings).hasSize(1) + } + } } - context("custom default values with allowed patterns are configured") { + @Nested + inner class `custom default values with allowed patterns are configured` { - val patternsConfig by memoized { + val patternsConfig = TestConfig( mapOf( VALUES to "Comment", ALLOWED_PATTERNS to "Ticket|Task" ) ) - } - it("should report Comment usages when regex does not match") { + @Test + fun `should report Comment usages when regex does not match`() { val comment = "// Comment is added here." val findings = ForbiddenComment(patternsConfig).compileAndLint(comment) assertThat(findings).hasSize(1) } - it("should not report Comment usages when any one pattern is present") { + @Test + fun `should not report Comment usages when any one pattern is present`() { val comment = "// Comment Ticket:234." val findings = ForbiddenComment(patternsConfig).compileAndLint(comment) assertThat(findings).isEmpty() } - it("should not report Comment usages when all patterns are present") { + @Test + fun `should not report Comment usages when all patterns are present`() { val comment = "// Comment Ticket:123 Task:456 comment." val findings = ForbiddenComment(patternsConfig).compileAndLint(comment) assertThat(findings).isEmpty() } } - context("custom message is configured") { - val messageConfig by memoized { + @Nested + inner class `custom message is configured` { + val messageConfig = TestConfig( mapOf( VALUES to "Comment", MESSAGE to "Custom Message" ) ) - } - it("should report a Finding with message 'Custom Message'") { + @Test + fun `should report a Finding with message 'Custom Message'`() { val comment = "// Comment" val findings = ForbiddenComment(messageConfig).compileAndLint(comment) assertThat(findings).hasSize(1) @@ -166,16 +231,17 @@ class ForbiddenCommentSpec : Spek({ } } - context("custom message is not configured") { - val messageConfig by memoized { + @Nested + inner class `custom message is not configured` { + val messageConfig = TestConfig( mapOf( VALUES to "Comment" ) ) - } - it("should report a Finding with default Message") { + @Test + fun `should report a Finding with default Message`() { val comment = "// Comment" val findings = ForbiddenComment(messageConfig).compileAndLint(comment) val expectedMessage = String.format(ForbiddenComment.DEFAULT_ERROR_MESSAGE, "Comment") @@ -184,4 +250,4 @@ class ForbiddenCommentSpec : Spek({ } } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ForbiddenImportSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ForbiddenImportSpec.kt index d550e21a8940..59e48431c76a 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ForbiddenImportSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ForbiddenImportSpec.kt @@ -3,14 +3,16 @@ package io.gitlab.arturbosch.detekt.rules.style 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 +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test private const val IMPORTS = "imports" private const val FORBIDDEN_PATTERNS = "forbiddenPatterns" -class ForbiddenImportSpec : Spek({ - describe("ForbiddenImport rule") { +class ForbiddenImportSpec { + @Nested + inner class `ForbiddenImport rule` { val code = """ package foo @@ -22,33 +24,42 @@ class ForbiddenImportSpec : Spek({ import net.example.R.dimension """ - it("should report nothing by default") { + @Test + fun `should report nothing by default`() { val findings = ForbiddenImport().lint(code) assertThat(findings).isEmpty() } - it("should report nothing when imports are blank") { + @Test + fun `should report nothing when imports are blank`() { val findings = ForbiddenImport(TestConfig(mapOf(IMPORTS to " "))).lint(code) assertThat(findings).isEmpty() } - it("should report nothing when imports do not match") { + @Test + fun `should report nothing when imports do not match`() { val findings = ForbiddenImport(TestConfig(mapOf(IMPORTS to "org.*"))).lint(code) assertThat(findings).isEmpty() } - it("should report kotlin.* when imports are kotlin.*") { + @Test + @DisplayName("should report kotlin.* when imports are kotlin.*") + fun reportKotlinWildcardImports() { val findings = ForbiddenImport(TestConfig(mapOf(IMPORTS to "kotlin.*"))).lint(code) assertThat(findings).hasSize(2) } - it("should report kotlin.SinceKotlin when specified via fully qualified name") { + @Test + @DisplayName("should report kotlin.SinceKotlin when specified via fully qualified name") + fun reportKotlinSinceKotlinWhenFqdnSpecified() { val findings = ForbiddenImport(TestConfig(mapOf(IMPORTS to "kotlin.SinceKotlin"))).lint(code) assertThat(findings).hasSize(1) } - it("should report kotlin.SinceKotlin and kotlin.jvm.JvmField when specified via fully qualified names") { + @Test + @DisplayName("should report kotlin.SinceKotlin and kotlin.jvm.JvmField when specified via fully qualified names") + fun reportMultipleConfiguredImportsCommaSeparated() { val findings = ForbiddenImport(TestConfig(mapOf(IMPORTS to "kotlin.SinceKotlin,kotlin.jvm.JvmField"))).lint( code @@ -56,7 +67,9 @@ class ForbiddenImportSpec : Spek({ assertThat(findings).hasSize(2) } - it("should report kotlin.SinceKotlin and kotlin.jvm.JvmField when specified via fully qualified names list") { + @Test + @DisplayName("should report kotlin.SinceKotlin and kotlin.jvm.JvmField when specified via fully qualified names list") + fun reportMultipleConfiguredImportsInList() { val findings = ForbiddenImport( TestConfig( @@ -68,32 +81,40 @@ class ForbiddenImportSpec : Spek({ assertThat(findings).hasSize(2) } - it("should report kotlin.SinceKotlin when specified via kotlin.Since*") { + @Test + @DisplayName("should report kotlin.SinceKotlin when specified via kotlin.Since*") + fun reportsKotlinSinceKotlinWhenSpecifiedWithWildcard() { val findings = ForbiddenImport(TestConfig(mapOf(IMPORTS to "kotlin.Since*"))).lint(code) assertThat(findings).hasSize(1) } - it("should report all of com.example.R.string, net.example.R.dimen, and net.example.R.dimension") { + @Test + @DisplayName("should report all of com.example.R.string, net.example.R.dimen, and net.example.R.dimension") + fun preAndPostWildcard() { val findings = ForbiddenImport(TestConfig(mapOf(IMPORTS to "*.R.*"))).lint(code) assertThat(findings).hasSize(3) } - it("should report net.example.R.dimen but not net.example.R.dimension") { + @Test + @DisplayName("should report net.example.R.dimen but not net.example.R.dimension") + fun doNotReportSubstringOfFqdn() { val findings = ForbiddenImport(TestConfig(mapOf(IMPORTS to "net.example.R.dimen"))).lint(code) assertThat(findings).hasSize(1) } - it("should not report import when it does not match any pattern") { + @Test + fun `should not report import when it does not match any pattern`() { val findings = ForbiddenImport(TestConfig(mapOf(FORBIDDEN_PATTERNS to "nets.*R"))).lint(code) assertThat(findings).isEmpty() } - it("should report import when it matches the forbidden pattern") { + @Test + fun `should report import when it matches the forbidden pattern`() { val findings = ForbiddenImport(TestConfig(mapOf(FORBIDDEN_PATTERNS to "net.*R|com.*expiremental"))).lint(code) assertThat(findings).hasSize(2) } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ForbiddenMethodCallSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ForbiddenMethodCallSpec.kt index 3d5844cdf344..3820c5902aa7 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ForbiddenMethodCallSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ForbiddenMethodCallSpec.kt @@ -1,24 +1,24 @@ package io.gitlab.arturbosch.detekt.rules.style import io.gitlab.arturbosch.detekt.api.SourceLocation -import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.TestConfig import io.gitlab.arturbosch.detekt.test.assertThat import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -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 METHODS = "methods" -class ForbiddenMethodCallSpec : Spek({ - setupKotlinEnvironment() +@KotlinCoreEnvironmentTest +class ForbiddenMethodCallSpec(val env: KotlinCoreEnvironment) { - val env: KotlinCoreEnvironment by memoized() + @Nested + inner class `ForbiddenMethodCall rule` { - describe("ForbiddenMethodCall rule") { - - it("should report kotlin print usages by default") { + @Test + fun `should report kotlin print usages by default`() { val code = """ fun main() { print("3") @@ -33,7 +33,8 @@ class ForbiddenMethodCallSpec : Spek({ ) } - it("should report nothing when methods are blank") { + @Test + fun `should report nothing when methods are blank`() { val code = """ import java.lang.System fun main() { @@ -48,7 +49,8 @@ class ForbiddenMethodCallSpec : Spek({ assertThat(findings).isEmpty() } - it("should report nothing when methods do not match") { + @Test + fun `should report nothing when methods do not match`() { val code = """ import java.lang.System fun main() { @@ -61,7 +63,8 @@ class ForbiddenMethodCallSpec : Spek({ assertThat(findings).isEmpty() } - it("should report method call when using the fully qualified name") { + @Test + fun `should report method call when using the fully qualified name`() { val code = """ fun main() { java.lang.System.out.println("hello") @@ -74,7 +77,8 @@ class ForbiddenMethodCallSpec : Spek({ assertThat(findings).hasTextLocations(38 to 54) } - it("should report method call when not using the fully qualified name") { + @Test + fun `should report method call when not using the fully qualified name`() { val code = """ import java.lang.System.out fun main() { @@ -88,7 +92,8 @@ class ForbiddenMethodCallSpec : Spek({ assertThat(findings).hasTextLocations(49 to 65) } - it("should report multiple different methods") { + @Test + fun `should report multiple different methods`() { val code = """ import java.lang.System fun main() { @@ -110,7 +115,8 @@ class ForbiddenMethodCallSpec : Spek({ assertThat(findings).hasTextLocations(48 to 64, 76 to 80) } - it("should report multiple different methods config with sting") { + @Test + fun `should report multiple different methods config with sting`() { val code = """ import java.lang.System fun main() { @@ -125,7 +131,8 @@ class ForbiddenMethodCallSpec : Spek({ assertThat(findings).hasTextLocations(48 to 64, 76 to 80) } - it("should report equals operator") { + @Test + fun `should report equals operator`() { val code = """ fun main() { java.math.BigDecimal(5.5) == java.math.BigDecimal(5.5) @@ -137,7 +144,8 @@ class ForbiddenMethodCallSpec : Spek({ assertThat(findings).hasSize(1) } - it("should report prefix operator") { + @Test + fun `should report prefix operator`() { val code = """ fun test() { var i = 1 @@ -150,7 +158,8 @@ class ForbiddenMethodCallSpec : Spek({ assertThat(findings).hasSize(1) } - it("should report postfix operator") { + @Test + fun `should report postfix operator`() { val code = """ fun test() { var i = 1 @@ -163,7 +172,8 @@ class ForbiddenMethodCallSpec : Spek({ assertThat(findings).hasSize(1) } - it("should report both methods when using just method name without full signature") { + @Test + fun `should report both methods when using just method name without full signature`() { val code = """ import java.time.Clock import java.time.LocalDate @@ -179,7 +189,8 @@ class ForbiddenMethodCallSpec : Spek({ assertThat(findings).hasSize(2) } - it("should report parameterless method when full signature matches") { + @Test + fun `should report parameterless method when full signature matches`() { val code = """ import java.time.Clock import java.time.LocalDate @@ -196,7 +207,8 @@ class ForbiddenMethodCallSpec : Spek({ assertThat(findings).hasSourceLocation(5, 26) } - it("should report method with param when full signature matches") { + @Test + fun `should report method with param when full signature matches`() { val code = """ import java.time.Clock import java.time.LocalDate @@ -213,7 +225,8 @@ class ForbiddenMethodCallSpec : Spek({ assertThat(findings).hasSourceLocation(6, 27) } - it("should report method with multiple params when full signature matches") { + @Test + fun `should report method with multiple params when full signature matches`() { val code = """ import java.time.LocalDate fun test() { @@ -227,7 +240,8 @@ class ForbiddenMethodCallSpec : Spek({ assertThat(findings).hasSourceLocation(3, 26) } - it("should report method with multiple params when full signature matches with additional spacing") { + @Test + fun `should report method with multiple params when full signature matches with additional spacing`() { val code = """ import java.time.LocalDate fun test() { @@ -241,7 +255,8 @@ class ForbiddenMethodCallSpec : Spek({ assertThat(findings).hasSourceLocation(3, 26) } - it("should report method with multiple params when method has spaces and commas") { + @Test + fun `should report method with multiple params when method has spaces and commas`() { val code = """ package io.gitlab.arturbosch.detekt.rules.style @@ -258,7 +273,8 @@ class ForbiddenMethodCallSpec : Spek({ assertThat(findings).hasSourceLocation(6, 13) } - it("should report method with default params") { + @Test + fun `should report method with default params`() { val code = """ package io.gitlab.arturbosch.detekt.rules.style @@ -280,7 +296,8 @@ class ForbiddenMethodCallSpec : Spek({ assertThat(findings).hasSourceLocation(6, 13) } - it("should report overriding method calls") { + @Test + fun `should report overriding method calls`() { val code = """ package org.example.com @@ -303,7 +320,8 @@ class ForbiddenMethodCallSpec : Spek({ assertThat(findings).hasSize(2) } - it("should report functions with lambda params") { + @Test + fun `should report functions with lambda params`() { val code = """ package org.example @@ -319,7 +337,8 @@ class ForbiddenMethodCallSpec : Spek({ assertThat(findings).hasSize(1) } - it("should report extension functions") { + @Test + fun `should report extension functions`() { val code = """ package org.example @@ -335,7 +354,8 @@ class ForbiddenMethodCallSpec : Spek({ assertThat(findings).hasSize(1) } - context("work with generics") { + @Nested + inner class `work with generics` { val code = """ package org.example @@ -346,14 +366,16 @@ class ForbiddenMethodCallSpec : Spek({ } """ - it("raise the issue") { + @Test + fun `raise the issue`() { val findings = ForbiddenMethodCall( TestConfig(mapOf(METHODS to listOf("org.example.bar(T, U, kotlin.String)"))) ).compileAndLintWithContext(env, code) assertThat(findings).hasSize(1) } - it("It doesn't raise any issue because the generics don't match") { + @Test + fun `It doesn't raise any issue because the generics don't match`() { val findings = ForbiddenMethodCall( TestConfig(mapOf(METHODS to listOf("org.example.bar(U, T, kotlin.String)"))) ).compileAndLintWithContext(env, code) @@ -361,7 +383,8 @@ class ForbiddenMethodCallSpec : Spek({ } } - context("work with generic extensions") { + @Nested + inner class `work with generic extensions` { val code = """ package org.example @@ -372,14 +395,16 @@ class ForbiddenMethodCallSpec : Spek({ } """ - it("raise the issue") { + @Test + fun `raise the issue`() { val findings = ForbiddenMethodCall( TestConfig(mapOf(METHODS to listOf("org.example.bar(R, kotlin.String)"))) ).compileAndLintWithContext(env, code) assertThat(findings).hasSize(1) } - it("It doesn't raise any issue because the type doesn't match") { + @Test + fun `It doesn't raise any issue because the type doesn't match`() { val findings = ForbiddenMethodCall( TestConfig(mapOf(METHODS to listOf("org.example.bar(kotlin.Int, kotlin.String)"))) ).compileAndLintWithContext(env, code) @@ -387,7 +412,8 @@ class ForbiddenMethodCallSpec : Spek({ } } - context("Should distinguish between runCatching - #4448") { + @Nested + inner class `Should distinguish between runCatching - #4448` { val code = """ package org.example @@ -399,7 +425,8 @@ class ForbiddenMethodCallSpec : Spek({ } """ - it("forbid the one without receiver") { + @Test + fun `forbid the one without receiver`() { val findings = ForbiddenMethodCall( TestConfig(mapOf(METHODS to listOf("kotlin.runCatching(() -> R)"))) ).compileAndLintWithContext(env, code) @@ -408,7 +435,8 @@ class ForbiddenMethodCallSpec : Spek({ .hasSourceLocation(5, 16) } - it("forbid the one with receiver") { + @Test + fun `forbid the one with receiver`() { val findings = ForbiddenMethodCall( TestConfig(mapOf(METHODS to listOf("kotlin.runCatching(T, (T) -> R)"))) ).compileAndLintWithContext(env, code) @@ -418,4 +446,4 @@ class ForbiddenMethodCallSpec : Spek({ } } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ForbiddenPublicDataClassSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ForbiddenPublicDataClassSpec.kt index 4af69b41b28f..f036fd0079c0 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ForbiddenPublicDataClassSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ForbiddenPublicDataClassSpec.kt @@ -4,12 +4,16 @@ import io.gitlab.arturbosch.detekt.api.Config 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 -class ForbiddenPublicDataClassSpec : Spek({ - describe("ForbiddenPublicDataClass rule") { - it("public data class should pass without explicit filters set") { +class ForbiddenPublicDataClassSpec { + @Nested + inner class `ForbiddenPublicDataClass rule` { + val subject = ForbiddenPublicDataClass() + + @Test + fun `public data class should pass without explicit filters set`() { val code = """ data class C(val a: String) """ @@ -17,11 +21,8 @@ class ForbiddenPublicDataClassSpec : Spek({ assertThat(ForbiddenPublicDataClass(TestConfig(Config.EXCLUDES_KEY to "**")).compileAndLint(code)).isEmpty() } - val subject by memoized { - ForbiddenPublicDataClass() - } - - it("public data class should fail") { + @Test + fun `public data class should fail`() { val code = """ data class C(val a: String) """ @@ -29,7 +30,8 @@ class ForbiddenPublicDataClassSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("private data class should pass") { + @Test + fun `private data class should pass`() { val code = """ private data class C(val a: String) """ @@ -37,7 +39,8 @@ class ForbiddenPublicDataClassSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("internal data class should pass") { + @Test + fun `internal data class should pass`() { val code = """ internal data class C(val a: String) """ @@ -45,7 +48,8 @@ class ForbiddenPublicDataClassSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("public class should pass") { + @Test + fun `public class should pass`() { val code = """ class C(val a: String) """ @@ -53,7 +57,8 @@ class ForbiddenPublicDataClassSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("private data class inside a public class should pass") { + @Test + fun `private data class inside a public class should pass`() { val code = """ class C { private data class D(val a: String) @@ -63,7 +68,8 @@ class ForbiddenPublicDataClassSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("public data class inside a public class should fail") { + @Test + fun `public data class inside a public class should fail`() { val code = """ class C { data class D(val a: String) @@ -73,7 +79,8 @@ class ForbiddenPublicDataClassSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("protected data class inside a public class should fail") { + @Test + fun `protected data class inside a public class should fail`() { val code = """ open class C { protected data class D(val a: String) @@ -83,7 +90,8 @@ class ForbiddenPublicDataClassSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("public data class inside an internal class should pass") { + @Test + fun `public data class inside an internal class should pass`() { val code = """ internal class C { data class D(val a: String) @@ -93,7 +101,8 @@ class ForbiddenPublicDataClassSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("public data class inside an internal package should pass") { + @Test + fun `public data class inside an internal package should pass`() { val code = """ package com.example.internal @@ -103,7 +112,8 @@ class ForbiddenPublicDataClassSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("public data class inside an internal subpackage should pass") { + @Test + fun `public data class inside an internal subpackage should pass`() { val code = """ package com.example.internal.other @@ -113,7 +123,8 @@ class ForbiddenPublicDataClassSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("public data class inside an internalise package should fail") { + @Test + fun `public data class inside an internalise package should fail`() { val code = """ package com.example.internalise @@ -123,7 +134,8 @@ class ForbiddenPublicDataClassSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("public data class inside a random package should fail") { + @Test + fun `public data class inside a random package should fail`() { val code = """ package com.random @@ -133,7 +145,8 @@ class ForbiddenPublicDataClassSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("public data class inside an ignored package should pass") { + @Test + fun `public data class inside an ignored package should pass`() { val code = """ package com.example @@ -144,7 +157,8 @@ class ForbiddenPublicDataClassSpec : Spek({ assertThat(ForbiddenPublicDataClass(config).compileAndLint(code)).isEmpty() } - it("public data class inside an ignored package should pass config as string") { + @Test + fun `public data class inside an ignored package should pass config as string`() { val code = """ package org.example @@ -155,4 +169,4 @@ class ForbiddenPublicDataClassSpec : Spek({ assertThat(ForbiddenPublicDataClass(config).compileAndLint(code)).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ForbiddenVoidSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ForbiddenVoidSpec.kt index 87600de8f035..5e5e11bdd08a 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ForbiddenVoidSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ForbiddenVoidSpec.kt @@ -1,26 +1,26 @@ package io.gitlab.arturbosch.detekt.rules.style import io.gitlab.arturbosch.detekt.api.Config -import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.TestConfig import io.gitlab.arturbosch.detekt.test.compileAndLint import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext import org.assertj.core.api.Assertions.assertThat import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -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 IGNORE_OVERRIDDEN = "ignoreOverridden" private const val IGNORE_USAGE_IN_GENERICS = "ignoreUsageInGenerics" -class ForbiddenVoidSpec : Spek({ - setupKotlinEnvironment() +@KotlinCoreEnvironmentTest +class ForbiddenVoidSpec(val env: KotlinCoreEnvironment) { + val subject = ForbiddenVoid(Config.empty) - val env: KotlinCoreEnvironment by memoized() - val subject by memoized { ForbiddenVoid(Config.empty) } - - describe("ForbiddenVoid rule") { - it("should report all Void type usage") { + @Nested + inner class `ForbiddenVoid rule` { + @Test + fun `should report all Void type usage`() { val code = """ lateinit var c: () -> Void @@ -33,7 +33,8 @@ class ForbiddenVoidSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(4) } - it("should not report Void class literal") { + @Test + fun `should not report Void class literal`() { val code = """ val clazz = java.lang.Void::class val klass = Void::class @@ -42,7 +43,8 @@ class ForbiddenVoidSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report when functions or classes are called 'Void'") { + @Test + fun `does not report when functions or classes are called 'Void'`() { val code = """ class Void { fun void() {} @@ -60,11 +62,13 @@ class ForbiddenVoidSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - describe("ignoreOverridden is enabled") { + @Nested + inner class `ignoreOverridden is enabled` { - val config by memoized { TestConfig(mapOf(IGNORE_OVERRIDDEN to "true")) } + val config = TestConfig(mapOf(IGNORE_OVERRIDDEN to "true")) - it("should not report Void in overriding function declarations") { + @Test + fun `should not report Void in overriding function declarations`() { val code = """ abstract class A { @Suppress("ForbiddenVoid") @@ -82,7 +86,8 @@ class ForbiddenVoidSpec : Spek({ assertThat(findings).isEmpty() } - it("should not report Void in overriding function declarations with parameterized types") { + @Test + fun `should not report Void in overriding function declarations with parameterized types`() { val code = """ class Foo {} @@ -102,7 +107,8 @@ class ForbiddenVoidSpec : Spek({ assertThat(findings).isEmpty() } - it("should report Void in body of overriding function even") { + @Test + fun `should report Void in body of overriding function even`() { val code = """ abstract class A { abstract fun method(param: String) @@ -119,7 +125,8 @@ class ForbiddenVoidSpec : Spek({ assertThat(findings).hasSize(1) } - it("should report Void in not overridden function declarations") { + @Test + fun `should report Void in not overridden function declarations`() { val code = """ fun method(param: Void) : Void { return param @@ -131,11 +138,13 @@ class ForbiddenVoidSpec : Spek({ } } - describe("ignoreUsageInGenerics is enabled") { + @Nested + inner class `ignoreUsageInGenerics is enabled` { - val config by memoized { TestConfig(mapOf(IGNORE_USAGE_IN_GENERICS to "true")) } + val config = TestConfig(mapOf(IGNORE_USAGE_IN_GENERICS to "true")) - it("should not report Void in generic type declaration") { + @Test + fun `should not report Void in generic type declaration`() { val code = """ interface A @@ -156,7 +165,8 @@ class ForbiddenVoidSpec : Spek({ assertThat(findings).isEmpty() } - it("should not report Void in nested generic type definition") { + @Test + fun `should not report Void in nested generic type definition`() { val code = """ interface A interface B @@ -167,7 +177,8 @@ class ForbiddenVoidSpec : Spek({ assertThat(findings).isEmpty() } - it("should not report Void in definition with multiple generic parameters") { + @Test + fun `should not report Void in definition with multiple generic parameters`() { val code = """ val foo = mutableMapOf() """ @@ -176,7 +187,8 @@ class ForbiddenVoidSpec : Spek({ assertThat(findings).isEmpty() } - it("should report non-generic Void type usage") { + @Test + fun `should report non-generic Void type usage`() { val code = """ lateinit var c: () -> Void @@ -190,4 +202,4 @@ class ForbiddenVoidSpec : Spek({ } } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/FunctionOnlyReturningConstantSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/FunctionOnlyReturningConstantSpec.kt index 0167c73631c2..9d132d003501 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/FunctionOnlyReturningConstantSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/FunctionOnlyReturningConstantSpec.kt @@ -5,81 +5,95 @@ import io.gitlab.arturbosch.detekt.test.TestConfig import io.gitlab.arturbosch.detekt.test.compileAndLint 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 +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test private const val IGNORE_OVERRIDABLE_FUNCTION = "ignoreOverridableFunction" private const val IGNORE_ACTUAL_FUNCTION = "ignoreActualFunction" private const val EXCLUDED_FUNCTIONS = "excludedFunctions" private const val EXCLUDE_ANNOTATED_FUNCTION = "excludeAnnotatedFunction" -class FunctionOnlyReturningConstantSpec : Spek({ - val subject by memoized { FunctionOnlyReturningConstant() } +class FunctionOnlyReturningConstantSpec { + val subject = FunctionOnlyReturningConstant() - describe("FunctionOnlyReturningConstant rule - positive cases") { + @Nested + inner class `FunctionOnlyReturningConstant rule - positive cases` { val path = Case.FunctionReturningConstantPositive.path() - it("reports functions which return constants") { + val actualFunctionCode = """ + actual class ActualFunctionReturningConstant { + actual fun f() = 1 + } + """ + + val code = """ + import kotlin.SinceKotlin + class Test { + @SinceKotlin("1.0.0") + fun someIgnoredFun(): String { + return "I am a constant" + } + } + """ + + @Test + fun `reports functions which return constants`() { assertThat(subject.lint(path)).hasSize(6) } - it("reports overridden functions which return constants") { + @Test + fun `reports overridden functions which return constants`() { val config = TestConfig(mapOf(IGNORE_OVERRIDABLE_FUNCTION to "false")) val rule = FunctionOnlyReturningConstant(config) assertThat(rule.lint(path)).hasSize(9) } - val actualFunctionCode = """ - actual class ActualFunctionReturningConstant { - actual fun f() = 1 - } - """ - - it("does not report actual functions which return constants") { + @Test + fun `does not report actual functions which return constants`() { assertThat(subject.lint(actualFunctionCode)).isEmpty() } - it("reports actual functions which return constants") { + @Test + fun `reports actual functions which return constants`() { val config = TestConfig(mapOf(IGNORE_ACTUAL_FUNCTION to "false")) val rule = FunctionOnlyReturningConstant(config) assertThat(rule.lint(actualFunctionCode)).hasSize(1) } - it("does not report excluded function which returns a constant") { + @Test + fun `does not report excluded function which returns a constant`() { val code = "fun f() = 1" val config = TestConfig(mapOf(EXCLUDED_FUNCTIONS to "f")) val rule = FunctionOnlyReturningConstant(config) assertThat(rule.compileAndLint(code)).isEmpty() } - val code = """ - import kotlin.SinceKotlin - class Test { - @SinceKotlin("1.0.0") - fun someIgnoredFun(): String { - return "I am a constant" - } - } - """ - - listOf( - TestConfig(mapOf(EXCLUDE_ANNOTATED_FUNCTION to "kotlin.SinceKotlin")), - TestConfig(mapOf(EXCLUDE_ANNOTATED_FUNCTION to listOf("kotlin.SinceKotlin"))) - ).forEach { config -> - it("does not report excluded annotated function which returns a constant") { + @Test + @DisplayName("does not report excluded annotated function which returns a constant when given \"kotlin.SinceKotlin\"") + fun ignoreAnnotatedFunctionWhichReturnsConstantWhenGivenKotlinSinceKotlin() { + val config = TestConfig(mapOf(EXCLUDE_ANNOTATED_FUNCTION to "kotlin.SinceKotlin")) + val rule = FunctionOnlyReturningConstant(config) + assertThat(rule.compileAndLint(code)).isEmpty() + } - val rule = FunctionOnlyReturningConstant(config) - assertThat(rule.compileAndLint(code)).isEmpty() - } + @Test + @DisplayName("does not report excluded annotated function which returns a constant when given listOf(\"kotlin.SinceKotlin\")") + fun ignoreAnnotatedFunctionWhichReturnsConstantWhenGivenListOfKotlinSinceKotlin() { + val config = TestConfig(mapOf(EXCLUDE_ANNOTATED_FUNCTION to listOf("kotlin.SinceKotlin"))) + val rule = FunctionOnlyReturningConstant(config) + assertThat(rule.compileAndLint(code)).isEmpty() } } - describe("FunctionOnlyReturningConstant rule - negative cases") { + @Nested + inner class `FunctionOnlyReturningConstant rule - negative cases` { - it("does not report functions which do not return constants") { + @Test + fun `does not report functions which do not return constants`() { val path = Case.FunctionReturningConstantNegative.path() assertThat(subject.lint(path)).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/LibraryCodeMustSpecifyReturnTypeSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/LibraryCodeMustSpecifyReturnTypeSpec.kt index 9690d214438d..cc8166624318 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/LibraryCodeMustSpecifyReturnTypeSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/LibraryCodeMustSpecifyReturnTypeSpec.kt @@ -1,23 +1,23 @@ package io.gitlab.arturbosch.detekt.rules.style import io.gitlab.arturbosch.detekt.api.Config -import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.TestConfig import io.gitlab.arturbosch.detekt.test.assertThat import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext import io.gitlab.arturbosch.detekt.test.lintWithContext import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -import org.spekframework.spek2.Spek -import org.spekframework.spek2.style.specification.describe +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test -internal class LibraryCodeMustSpecifyReturnTypeSpec : Spek({ +@KotlinCoreEnvironmentTest +class LibraryCodeMustSpecifyReturnTypeSpec(val env: KotlinCoreEnvironment) { - setupKotlinEnvironment() - val env: KotlinCoreEnvironment by memoized() + @Nested + inner class `library code must have explicit return types` { - describe("library code must have explicit return types") { - - it("should not report without explicit filters set") { + @Test + fun `should not report without explicit filters set`() { val subject = LibraryCodeMustSpecifyReturnType(TestConfig(Config.EXCLUDES_KEY to "**")) assertThat( subject.compileAndLintWithContext( @@ -34,13 +34,12 @@ internal class LibraryCodeMustSpecifyReturnTypeSpec : Spek({ ).isEmpty() } - val subject by memoized { - LibraryCodeMustSpecifyReturnType() - } - - describe("positive cases") { + @Nested + inner class `positive cases` { + val subject = LibraryCodeMustSpecifyReturnType() - it("should report a top level function") { + @Test + fun `should report a top level function`() { assertThat( subject.compileAndLintWithContext( env, @@ -51,7 +50,8 @@ internal class LibraryCodeMustSpecifyReturnTypeSpec : Spek({ ).hasSize(1) } - it("should report a top level property") { + @Test + fun `should report a top level property`() { assertThat( subject.compileAndLintWithContext( env, @@ -62,7 +62,8 @@ internal class LibraryCodeMustSpecifyReturnTypeSpec : Spek({ ).hasSize(1) } - it("should report a public class with public members") { + @Test + fun `should report a public class with public members`() { assertThat( subject.compileAndLintWithContext( env, @@ -76,7 +77,8 @@ internal class LibraryCodeMustSpecifyReturnTypeSpec : Spek({ ).hasSize(2) } - it("should report a public class with protected members") { + @Test + fun `should report a public class with protected members`() { assertThat( subject.compileAndLintWithContext( env, @@ -91,9 +93,12 @@ internal class LibraryCodeMustSpecifyReturnTypeSpec : Spek({ } } - describe("negative cases with public scope") { + @Nested + inner class `negative cases with public scope` { + val subject = LibraryCodeMustSpecifyReturnType() - it("should not report a top level function") { + @Test + fun `should not report a top level function`() { assertThat( subject.compileAndLintWithContext( env, @@ -104,7 +109,8 @@ internal class LibraryCodeMustSpecifyReturnTypeSpec : Spek({ ).isEmpty() } - it("should not report a non expression function") { + @Test + fun `should not report a non expression function`() { assertThat( subject.compileAndLintWithContext( env, @@ -115,7 +121,8 @@ internal class LibraryCodeMustSpecifyReturnTypeSpec : Spek({ ).isEmpty() } - it("should not report a top level property") { + @Test + fun `should not report a top level property`() { assertThat( subject.compileAndLintWithContext( env, @@ -126,7 +133,8 @@ internal class LibraryCodeMustSpecifyReturnTypeSpec : Spek({ ).isEmpty() } - it("should not report a public class with public members") { + @Test + fun `should not report a public class with public members`() { assertThat( subject.compileAndLintWithContext( env, @@ -140,9 +148,13 @@ internal class LibraryCodeMustSpecifyReturnTypeSpec : Spek({ ).isEmpty() } } - describe("negative cases with no public scope") { - it("should not report a private top level function") { + @Nested + inner class `negative cases with no public scope` { + val subject = LibraryCodeMustSpecifyReturnType() + + @Test + fun `should not report a private top level function`() { assertThat( subject.lintWithContext( env, @@ -154,7 +166,8 @@ internal class LibraryCodeMustSpecifyReturnTypeSpec : Spek({ ).isEmpty() } - it("should not report a internal top level property") { + @Test + fun `should not report a internal top level property`() { assertThat( subject.compileAndLintWithContext( env, @@ -165,7 +178,8 @@ internal class LibraryCodeMustSpecifyReturnTypeSpec : Spek({ ).isEmpty() } - it("should not report members and local variables") { + @Test + fun `should not report members and local variables`() { assertThat( subject.compileAndLintWithContext( env, @@ -182,7 +196,8 @@ internal class LibraryCodeMustSpecifyReturnTypeSpec : Spek({ ).isEmpty() } - it("should not report effectively private properties and functions") { + @Test + fun `should not report effectively private properties and functions`() { assertThat( subject.compileAndLintWithContext( env, @@ -197,4 +212,4 @@ internal class LibraryCodeMustSpecifyReturnTypeSpec : Spek({ } } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/LibraryEntitiesShouldNotBePublicSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/LibraryEntitiesShouldNotBePublicSpec.kt index 7c6e923ea6ea..0e1662b7cc4c 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/LibraryEntitiesShouldNotBePublicSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/LibraryEntitiesShouldNotBePublicSpec.kt @@ -4,13 +4,15 @@ import io.gitlab.arturbosch.detekt.api.Config import io.gitlab.arturbosch.detekt.test.TestConfig import io.gitlab.arturbosch.detekt.test.assertThat import io.gitlab.arturbosch.detekt.test.compileAndLint -import org.spekframework.spek2.Spek -import org.spekframework.spek2.style.specification.describe +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test -internal class LibraryEntitiesShouldNotBePublicSpec : Spek({ +class LibraryEntitiesShouldNotBePublicSpec { - describe("Library class cannot be public") { - it("should not report without explicit filters set") { + @Nested + inner class `Library class cannot be public` { + @Test + fun `should not report without explicit filters set`() { val subject = LibraryEntitiesShouldNotBePublic(TestConfig(Config.EXCLUDES_KEY to "**")) assertThat( subject.compileAndLint( @@ -21,12 +23,12 @@ internal class LibraryEntitiesShouldNotBePublicSpec : Spek({ ).isEmpty() } - val subject by memoized { - LibraryEntitiesShouldNotBePublic() - } + @Nested + inner class `positive cases` { + val subject = LibraryEntitiesShouldNotBePublic() - describe("positive cases") { - it("should report a class") { + @Test + fun `should report a class`() { assertThat( subject.compileAndLint( """ @@ -36,7 +38,8 @@ internal class LibraryEntitiesShouldNotBePublicSpec : Spek({ ).hasSize(1) } - it("should report a class with function") { + @Test + fun `should report a class with function`() { assertThat( subject.compileAndLint( """ @@ -50,7 +53,8 @@ internal class LibraryEntitiesShouldNotBePublicSpec : Spek({ ).hasSize(1) } - it("should report a typealias") { + @Test + fun `should report a typealias`() { assertThat( subject.compileAndLint( """ @@ -60,7 +64,8 @@ internal class LibraryEntitiesShouldNotBePublicSpec : Spek({ ).hasSize(1) } - it("should report a typealias and a function") { + @Test + fun `should report a typealias and a function`() { assertThat( subject.compileAndLint( """ @@ -71,7 +76,8 @@ internal class LibraryEntitiesShouldNotBePublicSpec : Spek({ ).hasSize(2) } - it("should report a function") { + @Test + fun `should report a function`() { assertThat( subject.compileAndLint( """ @@ -82,8 +88,12 @@ internal class LibraryEntitiesShouldNotBePublicSpec : Spek({ } } - describe("negative cases") { - it("should not report a class") { + @Nested + inner class `negative cases` { + val subject = LibraryEntitiesShouldNotBePublic() + + @Test + fun `should not report a class`() { assertThat( subject.compileAndLint( """ @@ -97,7 +107,8 @@ internal class LibraryEntitiesShouldNotBePublicSpec : Spek({ ).isEmpty() } - it("should not report a class with function") { + @Test + fun `should not report a class with function`() { assertThat( subject.compileAndLint( """ @@ -107,7 +118,8 @@ internal class LibraryEntitiesShouldNotBePublicSpec : Spek({ ).isEmpty() } - it("should not report a typealias") { + @Test + fun `should not report a typealias`() { assertThat( subject.compileAndLint( """ @@ -118,4 +130,4 @@ internal class LibraryEntitiesShouldNotBePublicSpec : Spek({ } } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/LoopWithTooManyJumpStatementsSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/LoopWithTooManyJumpStatementsSpec.kt index a463ed338618..ecfb1f69899f 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/LoopWithTooManyJumpStatementsSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/LoopWithTooManyJumpStatementsSpec.kt @@ -4,31 +4,35 @@ import io.gitlab.arturbosch.detekt.rules.Case 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 +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test private const val MAX_JUMP_COUNT = "maxJumpCount" -class LoopWithTooManyJumpStatementsSpec : Spek({ - val subject by memoized { LoopWithTooManyJumpStatements() } +class LoopWithTooManyJumpStatementsSpec { + val subject = LoopWithTooManyJumpStatements() - describe("LoopWithTooManyJumpStatements rule") { + @Nested + inner class `LoopWithTooManyJumpStatements rule` { val path = Case.LoopWithTooManyJumpStatementsPositive.path() - it("reports loops with more than 1 break or continue statement") { + @Test + fun `reports loops with more than 1 break or continue statement`() { assertThat(subject.lint(path)).hasSize(3) } - it("does not report when max count configuration is set to 2") { + @Test + fun `does not report when max count configuration is set to 2`() { val config = TestConfig(mapOf(MAX_JUMP_COUNT to "2")) val findings = LoopWithTooManyJumpStatements(config).lint(path) assertThat(findings).isEmpty() } - it("does not report loop with less than 1 break or continue statement") { + @Test + fun `does not report loop with less than 1 break or continue statement`() { val findings = subject.lint(Case.LoopWithTooManyJumpStatementsNegative.path()) assertThat(findings).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/MagicNumberSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/MagicNumberSpec.kt index 62ade6fa642b..de6f338a6652 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/MagicNumberSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/MagicNumberSpec.kt @@ -6,8 +6,11 @@ import io.gitlab.arturbosch.detekt.test.assertThat import io.gitlab.arturbosch.detekt.test.compileAndLint import io.gitlab.arturbosch.detekt.test.lint 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 org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.MethodSource private const val IGNORE_NUMBERS = "ignoreNumbers" private const val IGNORE_HASH_CODE = "ignoreHashCodeFunction" @@ -21,251 +24,304 @@ private const val IGNORE_ENUMS = "ignoreEnums" private const val IGNORE_RANGES = "ignoreRanges" private const val IGNORE_EXTENSION_FUNCTIONS = "ignoreExtensionFunctions" -class MagicNumberSpec : Spek({ +class MagicNumberSpec { - describe("Magic Number rule") { + @Nested + inner class `Magic Number rule` { - context("a float of 1") { + @Nested + inner class `a float of 1` { val code = "val myFloat = 1.0f" - it("should not be reported by default") { + @Test + fun `should not be reported by default`() { val findings = MagicNumber().lint(code) assertThat(findings).isEmpty() } - it("should be reported when ignoredNumbers is empty") { + @Test + fun `should be reported when ignoredNumbers is empty`() { val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to emptyList()))).lint(code) assertThat(findings).hasSourceLocation(1, 15) } } - context("a const float of 1") { + @Nested + inner class `a const float of 1` { val code = "const val MY_FLOAT = 1.0f" - it("should not be reported by default") { + @Test + fun `should not be reported by default`() { val findings = MagicNumber().lint(code) assertThat(findings).isEmpty() } - it("should not be reported when ignoredNumbers is empty") { + @Test + fun `should not be reported when ignoredNumbers is empty`() { val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to emptyList()))).lint(code) assertThat(findings).isEmpty() } } - context("an integer of 1") { + @Nested + inner class `an integer of 1` { val code = "val myInt = 1" - it("should not be reported by default") { + @Test + fun `should not be reported by default`() { val findings = MagicNumber().lint(code) assertThat(findings).isEmpty() } - it("should be reported when ignoredNumbers is empty") { + @Test + fun `should be reported when ignoredNumbers is empty`() { val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to emptyList()))).lint(code) assertThat(findings).hasSourceLocation(1, 13) } } - context("a const integer of 1") { + @Nested + inner class `a const integer of 1` { val code = "const val MY_INT = 1" - it("should not be reported by default") { + @Test + fun `should not be reported by default`() { val findings = MagicNumber().lint(code) assertThat(findings).isEmpty() } - it("should not be reported when ignoredNumbers is empty") { + @Test + fun `should not be reported when ignoredNumbers is empty`() { val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to emptyList()))).lint(code) assertThat(findings).isEmpty() } } - context("a long of 1") { + @Nested + inner class `a long of 1` { val code = "val myLong = 1L" - it("should not be reported by default") { + @Test + fun `should not be reported by default`() { val findings = MagicNumber().lint(code) assertThat(findings).isEmpty() } - it("should be reported when ignoredNumbers is empty") { + @Test + fun `should be reported when ignoredNumbers is empty`() { val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to emptyList()))).lint(code) assertThat(findings).hasSourceLocation(1, 14) } } - context("a long of -1") { + @Nested + inner class `a long of -1` { val code = "val myLong = -1L" - it("should not be reported by default") { + @Test + fun `should not be reported by default`() { val findings = MagicNumber().lint(code) assertThat(findings).isEmpty() } - it("should be reported when ignoredNumbers is empty") { + @Test + fun `should be reported when ignoredNumbers is empty`() { val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to emptyList()))).lint(code) assertThat(findings).hasSourceLocation(1, 15) } } - context("a long of -2") { + @Nested + inner class `a long of -2` { val code = "val myLong = -2L" - it("should be reported by default") { + @Test + fun `should be reported by default`() { val findings = MagicNumber().lint(code) assertThat(findings).hasSourceLocation(1, 15) } - it("should be ignored when ignoredNumbers contains it verbatim") { + @Test + fun `should be ignored when ignoredNumbers contains it verbatim`() { val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to listOf("-2L")))).lint(code) assertThat(findings).isEmpty() } - it("should be ignored when ignoredNumbers contains it as floating point") { + @Test + fun `should be ignored when ignoredNumbers contains it as floating point`() { val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to listOf("-2f")))).lint(code) assertThat(findings).isEmpty() } - it("should not be ignored when ignoredNumbers contains 2 but not -2") { + @Test + fun `should not be ignored when ignoredNumbers contains 2 but not -2`() { val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to listOf("1", "2", "3", "-1", "0")))) .lint(code) assertThat(findings).hasSourceLocation(1, 15) } - it("should not be ignored when ignoredNumbers contains 2 but not -2 config with string") { + @Test + fun `should not be ignored when ignoredNumbers contains 2 but not -2 config with string`() { val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to "1,2,3,-1,0"))) .lint(code) assertThat(findings).hasSourceLocation(1, 15) } } - context("a const long of 1") { + @Nested + inner class `a const long of 1` { val code = "const val MY_LONG = 1L" - it("should not be reported by default") { + @Test + fun `should not be reported by default`() { val findings = MagicNumber().lint(code) assertThat(findings).isEmpty() } - it("should not be reported when ignoredNumbers is empty") { + @Test + fun `should not be reported when ignoredNumbers is empty`() { val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to emptyList()))).lint(code) assertThat(findings).isEmpty() } } - context("a double of 1") { + @Nested + inner class `a double of 1` { val code = "val myDouble = 1.0" - it("should not be reported by default") { + @Test + fun `should not be reported by default`() { val findings = MagicNumber().lint(code) assertThat(findings).isEmpty() } - it("should be reported when ignoredNumbers is empty") { + @Test + fun `should be reported when ignoredNumbers is empty`() { val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to emptyList()))).lint(code) assertThat(findings).hasSourceLocation(1, 16) } } - context("a const double of 1") { + @Nested + inner class `a const double of 1` { val code = "const val MY_DOUBLE = 1.0" - it("should not be reported by default") { + @Test + fun `should not be reported by default`() { val findings = MagicNumber().lint(code) assertThat(findings).isEmpty() } - it("should not be reported when ignoredNumbers is empty") { + @Test + fun `should not be reported when ignoredNumbers is empty`() { val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to emptyList()))).lint(code) assertThat(findings).isEmpty() } } - context("a hex of 1") { + @Nested + inner class `a hex of 1` { val code = "val myHex = 0x1" - it("should not be reported by default") { + @Test + fun `should not be reported by default`() { val findings = MagicNumber().lint(code) assertThat(findings).isEmpty() } - it("should be reported when ignoredNumbers is empty") { + @Test + fun `should be reported when ignoredNumbers is empty`() { val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to emptyList()))).lint(code) assertThat(findings).hasSourceLocation(1, 13) } } - context("a const hex of 1") { + @Nested + inner class `a const hex of 1` { val code = "const val MY_HEX = 0x1" - it("should not be reported by default") { + @Test + fun `should not be reported by default`() { val findings = MagicNumber().lint(code) assertThat(findings).isEmpty() } - it("should not be reported when ignoredNumbers is empty") { + @Test + fun `should not be reported when ignoredNumbers is empty`() { val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to emptyList()))).lint(code) assertThat(findings).isEmpty() } } - context("an integer of 300") { + @Nested + inner class `an integer of 300` { val code = "val myInt = 300" - it("should not be reported when ignoredNumbers contains 300") { + @Test + fun `should not be reported when ignoredNumbers contains 300`() { val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to listOf("300")))).lint(code) assertThat(findings).isEmpty() } - it("should not be reported when ignoredNumbers contains a floating point 300") { + @Test + fun `should not be reported when ignoredNumbers contains a floating point 300`() { val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to listOf("300.0")))).lint(code) assertThat(findings).isEmpty() } } - context("a binary literal") { + @Nested + inner class `a binary literal` { val code = "val myBinary = 0b01001" - it("should not be reported") { + @Test + fun `should not be reported`() { val findings = MagicNumber().lint(code) assertThat(findings).hasSize(1) } - it("should not be reported when ignoredNumbers contains a binary literal 0b01001") { + @Test + fun `should not be reported when ignoredNumbers contains a binary literal 0b01001`() { val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to listOf("0b01001")))).lint(code) assertThat(findings).isEmpty() } } - context("an integer literal with underscores") { + @Nested + inner class `an integer literal with underscores` { val code = "val myInt = 100_000" - it("should be reported by default") { + @Test + fun `should be reported by default`() { val findings = MagicNumber().lint(code) assertThat(findings).hasSourceLocation(1, 13) } - it("should not be reported when ignored verbatim") { + @Test + fun `should not be reported when ignored verbatim`() { val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to listOf("100_000")))).lint(code) assertThat(findings).isEmpty() } - it("should not be reported when ignored with different underscores") { + @Test + fun `should not be reported when ignored with different underscores`() { val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to listOf("10_00_00")))).lint(code) assertThat(findings).isEmpty() } - it("should not be reported when ignored without underscores") { + @Test + fun `should not be reported when ignored without underscores`() { val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to listOf("100000")))).lint(code) assertThat(findings).isEmpty() } } - context("an if statement with magic numbers") { + @Nested + inner class `an if statement with magic numbers` { val code = "val myInt = if (5 < 6) 7 else 8" - it("should be reported") { + @Test + fun `should be reported`() { val findings = MagicNumber().lint(code) assertThat(findings) .hasSourceLocations( @@ -277,7 +333,8 @@ class MagicNumberSpec : Spek({ } } - context("a when statement with magic numbers") { + @Nested + inner class `a when statement with magic numbers` { val code = """ fun test(x: Int) { when (x) { @@ -288,7 +345,8 @@ class MagicNumberSpec : Spek({ } """ - it("should be reported") { + @Test + fun `should be reported`() { val findings = MagicNumber().lint(code) assertThat(findings).hasSourceLocations( SourceLocation(3, 9), @@ -301,81 +359,98 @@ class MagicNumberSpec : Spek({ } } - context("a method containing variables with magic numbers") { + @Nested + inner class `a method containing variables with magic numbers` { val code = """ fun test(x: Int) { val i = 5 } """ - it("should be reported") { + @Test + fun `should be reported`() { val findings = MagicNumber().lint(code) assertThat(findings).hasSize(1) } } - context("a boolean value") { + @Nested + inner class `a boolean value` { val code = """ fun test() : Boolean { return true; } """ - it("should not be reported") { + @Test + fun `should not be reported`() { val findings = MagicNumber().lint(code) assertThat(findings).isEmpty() } } - context("a non-numeric constant expression") { + @Nested + inner class `a non-numeric constant expression` { val code = "val surprise = true" - it("should not be reported") { + @Test + fun `should not be reported`() { val findings = MagicNumber().lint(code) assertThat(findings).isEmpty() } } - context("a float of 0.5") { + @Nested + @DisplayName("a float of 0.5") + inner class Float { val code = "val test = 0.5f" - it("should be reported by default") { + @Test + fun `should be reported by default`() { val findings = MagicNumber().lint(code) assertThat(findings).hasSourceLocation(1, 12) } - it("should not be reported when ignoredNumbers contains it") { + @Test + fun `should not be reported when ignoredNumbers contains it`() { val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to listOf(".5")))).lint(code) assertThat(findings).isEmpty() } } - context("a magic number number in a constructor call") { + @Nested + inner class `a magic number number in a constructor call` { - it("should report") { + @Test + fun `should report`() { val code = "val file = Array(42) { null }" val findings = MagicNumber().compileAndLint(code) assertThat(findings).hasSize(1) } } - context("an invalid ignoredNumber") { + @Nested + inner class `an invalid ignoredNumber` { - it("throws a NumberFormatException") { + @Test + fun `throws a NumberFormatException`() { assertThatExceptionOfType(NumberFormatException::class.java).isThrownBy { MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to listOf("banana")))).compileAndLint("val i = 0") } } } - context("an empty ignoredNumber") { + @Nested + inner class `an empty ignoredNumber` { - it("doesn't throw an exception") { + @Test + fun `doesn't throw an exception`() { MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to emptyList()))) } } - context("ignoring properties") { + @Nested + inner class `ignoring properties` { val code = """ @Magic(number = 69) class A { @@ -399,7 +474,8 @@ class MagicNumberSpec : Spek({ data class Color(val color: Int) """ - it("should report all without ignore flags") { + @Test + fun `should report all without ignore flags`() { val config = TestConfig( mapOf( IGNORE_PROPERTY_DECLARATION to "false", @@ -426,7 +502,8 @@ class MagicNumberSpec : Spek({ ) } - it("should not report any issues with all ignore flags") { + @Test + fun `should not report any issues with all ignore flags`() { val config = TestConfig( mapOf( IGNORE_PROPERTY_DECLARATION to "true", @@ -442,7 +519,8 @@ class MagicNumberSpec : Spek({ } } - context("magic numbers in companion object property assignments") { + @Nested + inner class `magic numbers in companion object property assignments` { val code = """ class A { @@ -453,12 +531,14 @@ class MagicNumberSpec : Spek({ } """ - it("should not report any issues by default") { + @Test + fun `should not report any issues by default`() { val findings = MagicNumber().lint(code) assertThat(findings).isEmpty() } - it("should not report any issues when ignoring properties but not constants nor companion objects") { + @Test + fun `should not report any issues when ignoring properties but not constants nor companion objects`() { val config = TestConfig( mapOf( IGNORE_PROPERTY_DECLARATION to "true", @@ -471,7 +551,8 @@ class MagicNumberSpec : Spek({ assertThat(findings).isEmpty() } - it("should not report any issues when ignoring properties and constants but not companion objects") { + @Test + fun `should not report any issues when ignoring properties and constants but not companion objects`() { val config = TestConfig( mapOf( IGNORE_PROPERTY_DECLARATION to "true", @@ -484,7 +565,8 @@ class MagicNumberSpec : Spek({ assertThat(findings).isEmpty() } - it("should not report any issues when ignoring properties, constants and companion objects") { + @Test + fun `should not report any issues when ignoring properties, constants and companion objects`() { val config = TestConfig( mapOf( IGNORE_PROPERTY_DECLARATION to "true", @@ -497,7 +579,8 @@ class MagicNumberSpec : Spek({ assertThat(findings).isEmpty() } - it("should not report any issues when ignoring companion objects but not properties and constants") { + @Test + fun `should not report any issues when ignoring companion objects but not properties and constants`() { val config = TestConfig( mapOf( IGNORE_PROPERTY_DECLARATION to "false", @@ -510,7 +593,8 @@ class MagicNumberSpec : Spek({ assertThat(findings).isEmpty() } - it("should report property when ignoring constants but not properties and companion objects") { + @Test + fun `should report property when ignoring constants but not properties and companion objects`() { val config = TestConfig( mapOf( IGNORE_PROPERTY_DECLARATION to "false", @@ -523,7 +607,8 @@ class MagicNumberSpec : Spek({ assertThat(findings).hasSourceLocation(4, 35) } - it("should report property and constant when not ignoring properties, constants nor companion objects") { + @Test + fun `should report property and constant when not ignoring properties, constants nor companion objects`() { val config = TestConfig( mapOf( IGNORE_PROPERTY_DECLARATION to "false", @@ -541,17 +626,21 @@ class MagicNumberSpec : Spek({ } } - context("a property without number") { + @Nested + inner class `a property without number` { val code = "private var pair: Pair? = null" - it("should not lead to a crash #276") { + @Test + fun `should not lead to a crash #276`() { val findings = MagicNumber().lint(code) assertThat(findings).isEmpty() } } - context("ignoring named arguments") { - context("in constructor invocation") { + @Nested + inner class `ignoring named arguments` { + @Nested + inner class `in constructor invocation` { fun code(numberString: String) = """ data class Model( val someVal: Int, @@ -561,35 +650,42 @@ class MagicNumberSpec : Spek({ var model = Model(someVal = $numberString) """ - it("should not ignore int") { + @Test + fun `should not ignore int`() { val rule = MagicNumber(TestConfig(mapOf(IGNORE_NAMED_ARGUMENT to "false"))) assertThat(rule.lint(code("53"))).hasSize(1) } - it("should not ignore float") { + @Test + fun `should not ignore float`() { val rule = MagicNumber(TestConfig(mapOf(IGNORE_NAMED_ARGUMENT to "false"))) assertThat(rule.lint(code("53f"))).hasSize(1) } - it("should not ignore binary") { + @Test + fun `should not ignore binary`() { val rule = MagicNumber(TestConfig(mapOf(IGNORE_NAMED_ARGUMENT to "false"))) assertThat(rule.lint(code("0b01001"))).hasSize(1) } - it("should ignore integer with underscores") { + @Test + fun `should ignore integer with underscores`() { val rule = MagicNumber(TestConfig(mapOf(IGNORE_NAMED_ARGUMENT to "false"))) assertThat(rule.lint(code("101_000"))).hasSize(1) } - it("should ignore numbers by default") { + @Test + fun `should ignore numbers by default`() { assertThat(MagicNumber().lint(code("53"))).isEmpty() } - it("should ignore negative numbers by default") { + @Test + fun `should ignore negative numbers by default`() { assertThat(MagicNumber().lint(code("-53"))).isEmpty() } - it("should ignore named arguments in inheritance - #992") { + @Test + fun `should ignore named arguments in inheritance - #992`() { val code = """ abstract class A(n: Int) @@ -598,14 +694,16 @@ class MagicNumberSpec : Spek({ assertThat(MagicNumber().compileAndLint(code)).isEmpty() } - it("should ignore named arguments in parameter annotations - #1115") { + @Test + fun `should ignore named arguments in parameter annotations - #1115`() { val code = "@JvmStatic fun setCustomDimension(@IntRange(from = 0, to = 19) index: Int, value: String?) {}" assertThat(MagicNumber().lint(code)).isEmpty() } } - context("Issue#659 - false-negative reporting on unnamed argument when ignore is true") { + @Nested + inner class `Issue#659 - false-negative reporting on unnamed argument when ignore is true` { fun code(numberString: String) = """ data class Model( @@ -616,60 +714,79 @@ class MagicNumberSpec : Spek({ var model = Model($numberString) """ - it("should detect the argument by default") { + @Test + fun `should detect the argument by default`() { assertThat(MagicNumber().lint(code("53"))).hasSize(1) } } - context("in function invocation") { + @Nested + inner class `in function invocation` { fun code(number: Number) = """ fun tested(someVal: Int, other: String = "default") val t = tested(someVal = $number) """ - it("should ignore int by default") { + + @Test + fun `should ignore int by default`() { assertThat(MagicNumber().lint(code(53))).isEmpty() } - it("should ignore float by default") { + @Test + fun `should ignore float by default`() { assertThat(MagicNumber().lint(code(53f))).isEmpty() } - it("should ignore binary by default") { + @Test + fun `should ignore binary by default`() { assertThat(MagicNumber().lint(code(0b01001))).isEmpty() } - it("should ignore integer with underscores") { + @Test + fun `should ignore integer with underscores`() { assertThat(MagicNumber().lint(code(101_000))).isEmpty() } } - context("in enum constructor argument") { + + @Nested + inner class `in enum constructor argument` { val code = """ enum class Bag(id: Int) { SMALL(1), EXTRA_LARGE(5) } """ - it("should be reported by default") { + + @Test + fun `should be reported by default`() { assertThat(MagicNumber().lint(code)).hasSize(1) } - it("numbers when 'ignoreEnums' is set to true") { + + @Test + fun `numbers when 'ignoreEnums' is set to true`() { val rule = MagicNumber(TestConfig(mapOf(IGNORE_ENUMS to "true"))) assertThat(rule.lint(code)).isEmpty() } } - context("in enum constructor as named argument") { + + @Nested + inner class `in enum constructor as named argument` { val code = """ enum class Bag(id: Int) { SMALL(id = 1), EXTRA_LARGE(id = 5) } """ - it("should be reported") { + + @Test + fun `should be reported`() { val rule = MagicNumber(TestConfig(mapOf(IGNORE_NAMED_ARGUMENT to "false"))) assertThat(rule.lint(code)).hasSize(1) } - it("numbers when 'ignoreEnums' is set to true") { + + @Test + fun `numbers when 'ignoreEnums' is set to true`() { val rule = MagicNumber( TestConfig( mapOf( @@ -683,9 +800,11 @@ class MagicNumberSpec : Spek({ } } - context("functions with and without braces which return values") { + @Nested + inner class `functions with and without braces which return values` { - it("does not report functions that always returns a constant value") { + @Test + fun `does not report functions that always returns a constant value`() { val code = """ fun x() = 9 fun y(): Int { return 9 } @@ -693,7 +812,8 @@ class MagicNumberSpec : Spek({ assertThat(MagicNumber().compileAndLint(code)).isEmpty() } - it("reports functions that does not return a constant value") { + @Test + fun `reports functions that does not return a constant value`() { val code = """ fun x() = 9 + 1 fun y(): Int { return 9 + 1 } @@ -702,27 +822,34 @@ class MagicNumberSpec : Spek({ } } - context("in-class declaration with default parameters") { + @Nested + inner class `in-class declaration with default parameters` { - it("reports no finding") { + @Test + fun `reports no finding`() { val code = "class SomeClassWithDefault(val defaultValue: Int = 10)" assertThat(MagicNumber().lint(code)).isEmpty() } - it("reports no finding for an explicit declaration") { + @Test + fun `reports no finding for an explicit declaration`() { val code = "class SomeClassWithDefault constructor(val defaultValue: Int = 10)" assertThat(MagicNumber().lint(code)).isEmpty() } - it("reports no finding for a function expression") { - val code = "class SomeClassWithDefault constructor(val defaultValue: Duration = 10.toDuration(DurationUnit.MILLISECONDS))" + @Test + fun `reports no finding for a function expression`() { + val code = + "class SomeClassWithDefault constructor(val defaultValue: Duration = 10.toDuration(DurationUnit.MILLISECONDS))" assertThat(MagicNumber().lint(code)).isEmpty() } } - context("default parameters in secondary constructor") { + @Nested + inner class `default parameters in secondary constructor` { - it("reports no finding") { + @Test + fun `reports no finding`() { val code = """ class SomeClassWithDefault { constructor(val defaultValue: Int = 10) { } @@ -731,7 +858,8 @@ class MagicNumberSpec : Spek({ assertThat(MagicNumber().lint(code)).isEmpty() } - it("reports no finding for a function expression") { + @Test + fun `reports no finding for a function expression`() { val code = """ class SomeClassWithDefault { constructor(val defaultValue: Duration = 10.toDuration(DurationUnit.MILLISECONDS)) { } @@ -741,22 +869,26 @@ class MagicNumberSpec : Spek({ } } - context("default parameters in function") { + @Nested + inner class `default parameters in function` { - it("reports no finding") { + @Test + fun `reports no finding`() { val code = "fun f(p: Int = 100)" assertThat(MagicNumber().lint(code)).isEmpty() } - it("reports no finding for a function expression") { + @Test + fun `reports no finding for a function expression`() { val code = "fun f(p: Duration = 10.toDuration(DurationUnit.MILLISECONDS))" assertThat(MagicNumber().lint(code)).isEmpty() } } - context("a number as part of a range") { + @Nested + inner class `a number as part of a range` { - listOf( + fun cases() = listOf( "val range = 1..27", "val range = (1..27)", "val range = 27 downTo 1", @@ -764,51 +896,63 @@ class MagicNumberSpec : Spek({ "val inRange = 1 in 1..27", "val inRange = (1 in 27 downTo 0 step 1)", "val inRange = (1..27 step 1).last" - ).forEach { codeWithMagicNumberInRange -> - it("'$codeWithMagicNumberInRange' reports a code smell by default") { - val code = codeWithMagicNumberInRange - assertThat(MagicNumber().lint(code)).hasSize(1) - } - it("'$codeWithMagicNumberInRange' reports a code smell if ranges are not ignored") { - val code = codeWithMagicNumberInRange - assertThat(MagicNumber(TestConfig(mapOf(IGNORE_RANGES to "false"))).lint(code)) - .hasSize(1) - } - it("'$codeWithMagicNumberInRange' reports no finding if ranges are ignored") { - val code = codeWithMagicNumberInRange - assertThat(MagicNumber(TestConfig(mapOf(IGNORE_RANGES to "true"))).lint(code)) - .isEmpty() - } + ) + + @ParameterizedTest + @MethodSource("cases") + fun `reports a code smell by default`(code: String) { + assertThat(MagicNumber().lint(code)).hasSize(1) } - it("reports a finding for a parenthesized number if ranges are ignored") { + @ParameterizedTest + @MethodSource("cases") + fun `reports a code smell if ranges are not ignored`(code: String) { + assertThat(MagicNumber(TestConfig(mapOf(IGNORE_RANGES to "false"))).lint(code)) + .hasSize(1) + } + + @ParameterizedTest + @MethodSource("cases") + fun `reports no finding if ranges are ignored`(code: String) { + assertThat(MagicNumber(TestConfig(mapOf(IGNORE_RANGES to "true"))).lint(code)) + .isEmpty() + } + + @Test + fun `reports a finding for a parenthesized number if ranges are ignored`() { val code = "val foo : Int = (127)" assertThat(MagicNumber(TestConfig(mapOf(IGNORE_RANGES to "true"))).lint(code)).hasSize(1) } - it("reports a finding for an addition if ranges are ignored") { + + @Test + fun `reports a finding for an addition if ranges are ignored`() { val code = "val foo : Int = 1 + 27" assertThat(MagicNumber(TestConfig(mapOf(IGNORE_RANGES to "true"))).lint(code)).hasSize(1) } } - context("a number assigned to a local variable") { + @Nested + inner class `a number assigned to a local variable` { val code = """fun f() { val a = 3; }""" - it("reports 3 due to the assignment to a local variable") { + @Test + fun `reports 3 due to the assignment to a local variable`() { val rule = MagicNumber(TestConfig(mapOf(IGNORE_LOCAL_VARIABLES to "false"))) assertThat(rule.compileAndLint(code)).hasSize(1) } - it("should not report 3 due to the ignored local variable config") { + @Test + fun `should not report 3 due to the ignored local variable config`() { val rule = MagicNumber(TestConfig(mapOf(IGNORE_LOCAL_VARIABLES to "true"))) assertThat(rule.compileAndLint(code)).isEmpty() } } - context("meaningful variables - #1536") { + @Nested + inner class `meaningful variables - #1536` { - val rule by memoized { + val rule = MagicNumber( TestConfig( mapOf( @@ -817,24 +961,27 @@ class MagicNumberSpec : Spek({ ) ) ) - } - it("should report 3") { + @Test + fun `should report 3`() { assertThat(rule.compileAndLint("""fun bar() { foo(3) }; fun foo(n: Int) {}""")).hasSize(1) } - it("should not report named 3") { + @Test + fun `should not report named 3`() { assertThat(rule.compileAndLint("""fun bar() { foo(param=3) }; fun foo(param: Int) {}""")).isEmpty() } - it("should not report 3 due to scoped describing variable") { + @Test + fun `should not report 3 due to scoped describing variable`() { assertThat(rule.compileAndLint("""fun bar() { val a = 3; foo(a) }; fun foo(n: Int) {}""")).isEmpty() } } - context("with extension function") { + @Nested + inner class `with extension function` { - val rule by memoized { + val rule = MagicNumber( TestConfig( mapOf( @@ -842,9 +989,9 @@ class MagicNumberSpec : Spek({ ) ) ) - } - it("should not report when function") { + @Test + fun `should not report when function`() { val code = """ fun Int.dp() = this + 1 @@ -854,7 +1001,8 @@ class MagicNumberSpec : Spek({ assertThat(rule.compileAndLint(code)).isEmpty() } - it("should not report when property") { + @Test + fun `should not report when property`() { val code = """ val Int.dp: Int get() = this + 1 @@ -865,7 +1013,8 @@ class MagicNumberSpec : Spek({ assertThat(rule.compileAndLint(code)).isEmpty() } - it("should report the argument") { + @Test + fun `should report the argument`() { val code = """ fun Int.dp(a: Int) = this + a @@ -876,4 +1025,4 @@ class MagicNumberSpec : Spek({ } } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/MaxLineLengthSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/MaxLineLengthSpec.kt index a0f8cb1332ee..aa310f8959f9 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/MaxLineLengthSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/MaxLineLengthSpec.kt @@ -6,8 +6,8 @@ import io.gitlab.arturbosch.detekt.api.SourceLocation import io.gitlab.arturbosch.detekt.rules.Case import io.gitlab.arturbosch.detekt.test.TestConfig import io.gitlab.arturbosch.detekt.test.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 import org.assertj.core.api.Assertions.assertThat as doAssert private const val MAX_LINE_LENGTH = "maxLineLength" @@ -15,31 +15,36 @@ private const val EXCLUDE_PACKAGE_STATEMENTS = "excludePackageStatements" private const val EXCLUDE_IMPORT_STATEMENTS = "excludeImportStatements" private const val EXCLUDE_COMMENT_STATEMENTS = "excludeCommentStatements" -class MaxLineLengthSpec : Spek({ +class MaxLineLengthSpec { - describe("MaxLineLength rule") { + @Nested + inner class `MaxLineLength rule` { - context("a kt file with some long lines") { + @Nested + inner class `a kt file with some long lines` { - val file by memoized { compileForTest(Case.MaxLineLength.path()) } - val lines by memoized { file.text.splitToSequence("\n") } - val fileContent by memoized { KtFileContent(file, lines) } + val file = compileForTest(Case.MaxLineLength.path()) + val lines = file.text.splitToSequence("\n") + val fileContent = KtFileContent(file, lines) - it("should report no errors when maxLineLength is set to 200") { + @Test + fun `should report no errors when maxLineLength is set to 200`() { val rule = MaxLineLength(TestConfig(mapOf(MAX_LINE_LENGTH to "200"))) rule.visit(fileContent) assertThat(rule.findings).isEmpty() } - it("should report all errors with default maxLineLength") { + @Test + fun `should report all errors with default maxLineLength`() { val rule = MaxLineLength() rule.visit(fileContent) assertThat(rule.findings).hasSize(7) } - it("should report meaningful signature for all violations") { + @Test + fun `should report meaningful signature for all violations`() { val rule = MaxLineLength() rule.visit(fileContent) @@ -48,13 +53,15 @@ class MaxLineLengthSpec : Spek({ } } - context("a kt file with long but suppressed lines") { + @Nested + inner class `a kt file with long but suppressed lines` { - val file by memoized { compileForTest(Case.MaxLineLengthSuppressed.path()) } - val lines by memoized { file.text.splitToSequence("\n") } - val fileContent by memoized { KtFileContent(file, lines) } + val file = compileForTest(Case.MaxLineLengthSuppressed.path()) + val lines = file.text.splitToSequence("\n") + val fileContent = KtFileContent(file, lines) - it("should not report as lines are suppressed") { + @Test + fun `should not report as lines are suppressed`() { val rule = MaxLineLength() rule.visit(fileContent) @@ -62,7 +69,8 @@ class MaxLineLengthSpec : Spek({ } } - context("a kt file with a long package name and long import statements") { + @Nested + inner class `a kt file with a long package name and long import statements` { val code = """ package anIncrediblyLongAndComplexPackageNameThatProbablyShouldBeMuchShorterButForTheSakeOfTheTestItsNot @@ -72,11 +80,12 @@ class MaxLineLengthSpec : Spek({ } """ - val file by memoized { compileContentForTest(code) } - val lines by memoized { file.text.splitToSequence("\n") } - val fileContent by memoized { KtFileContent(file, lines) } + val file = compileContentForTest(code) + val lines = file.text.splitToSequence("\n") + val fileContent = KtFileContent(file, lines) - it("should not report the package statement and import statements by default") { + @Test + fun `should not report the package statement and import statements by default`() { val rule = MaxLineLength( TestConfig( mapOf( @@ -89,7 +98,8 @@ class MaxLineLengthSpec : Spek({ assertThat(rule.findings).isEmpty() } - it("should report the package statement and import statements if they're enabled") { + @Test + fun `should report the package statement and import statements if they're enabled`() { val rule = MaxLineLength( TestConfig( mapOf( @@ -104,7 +114,8 @@ class MaxLineLengthSpec : Spek({ assertThat(rule.findings).hasSize(2) } - it("should not report anything if both package and import statements are disabled") { + @Test + fun `should not report anything if both package and import statements are disabled`() { val rule = MaxLineLength( TestConfig( mapOf( @@ -120,13 +131,15 @@ class MaxLineLengthSpec : Spek({ } } - context("a kt file with a long package name, long import statements, a long line and long comments") { + @Nested + inner class `a kt file with a long package name, long import statements, a long line and long comments` { - val file by memoized { compileForTest(Case.MaxLineLengthWithLongComments.path()) } - val lines by memoized { file.text.splitToSequence("\n") } - val fileContent by memoized { KtFileContent(file, lines) } + val file = compileForTest(Case.MaxLineLengthWithLongComments.path()) + val lines = file.text.splitToSequence("\n") + val fileContent = KtFileContent(file, lines) - it("should report the package statement, import statements, line and comments by default") { + @Test + fun `should report the package statement, import statements, line and comments by default`() { val rule = MaxLineLength( TestConfig( mapOf( @@ -139,7 +152,8 @@ class MaxLineLengthSpec : Spek({ assertThat(rule.findings).hasSize(8) } - it("should report the package statement, import statements, line and comments if they're enabled") { + @Test + fun `should report the package statement, import statements, line and comments if they're enabled`() { val rule = MaxLineLength( TestConfig( mapOf( @@ -155,7 +169,8 @@ class MaxLineLengthSpec : Spek({ assertThat(rule.findings).hasSize(8) } - it("should not report comments if they're disabled") { + @Test + fun `should not report comments if they're disabled`() { val rule = MaxLineLength( TestConfig( mapOf( @@ -170,7 +185,8 @@ class MaxLineLengthSpec : Spek({ } } - context("a kt file with a long package name, long import statements and a long line") { + @Nested + inner class `a kt file with a long package name, long import statements and a long line` { val code = """ package anIncrediblyLongAndComplexPackageNameThatProbablyShouldBeMuchShorterButForTheSakeOfTheTestItsNot @@ -181,11 +197,12 @@ class MaxLineLengthSpec : Spek({ } """.trim() - val file by memoized { compileContentForTest(code) } - val lines by memoized { file.text.splitToSequence("\n") } - val fileContent by memoized { KtFileContent(file, lines) } + val file = compileContentForTest(code) + val lines = file.text.splitToSequence("\n") + val fileContent = KtFileContent(file, lines) - it("should only the function line by default") { + @Test + fun `should only the function line by default`() { val rule = MaxLineLength( TestConfig( mapOf( @@ -198,7 +215,8 @@ class MaxLineLengthSpec : Spek({ assertThat(rule.findings).hasSize(1) } - it("should report the package statement, import statements and line if they're not excluded") { + @Test + fun `should report the package statement, import statements and line if they're not excluded`() { val rule = MaxLineLength( TestConfig( mapOf( @@ -213,7 +231,8 @@ class MaxLineLengthSpec : Spek({ assertThat(rule.findings).hasSize(3) } - it("should report only method if both package and import statements are disabled") { + @Test + fun `should report only method if both package and import statements are disabled`() { val rule = MaxLineLength( TestConfig( mapOf( @@ -228,7 +247,8 @@ class MaxLineLengthSpec : Spek({ assertThat(rule.findings).hasSize(1) } - it("should report correct line and column for function with excessive length") { + @Test + fun `should report correct line and column for function with excessive length`() { val rule = MaxLineLength( TestConfig( mapOf( @@ -245,4 +265,4 @@ class MaxLineLengthSpec : Spek({ } } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/MayBeConstSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/MayBeConstSpec.kt index 1ef439a4e64a..2ce511fb6b01 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/MayBeConstSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/MayBeConstSpec.kt @@ -4,17 +4,20 @@ import io.gitlab.arturbosch.detekt.api.SourceLocation import io.gitlab.arturbosch.detekt.test.assertThat import io.gitlab.arturbosch.detekt.test.compileAndLint import io.gitlab.arturbosch.detekt.test.lint -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 MayBeConstSpec : Spek({ +class MayBeConstSpec { - val subject by memoized { MayBeConst() } + val subject = MayBeConst() - describe("MayBeConst rule") { + @Nested + inner class `MayBeConst rule` { - context("some valid constants") { - it("is a valid constant") { + @Nested + inner class `some valid constants` { + @Test + fun `is a valid constant`() { val code = """ object Something { const val X = 42 @@ -24,7 +27,8 @@ class MayBeConstSpec : Spek({ assertThat(subject.findings).isEmpty() } - it("is const vals in object") { + @Test + fun `is const vals in object`() { val code = """ object Test { const val TEST = "Test" @@ -34,7 +38,8 @@ class MayBeConstSpec : Spek({ assertThat(subject.findings).isEmpty() } - it("isconst vals in companion objects") { + @Test + fun `isconst vals in companion objects`() { val code = """ class Test { companion object { @@ -46,7 +51,8 @@ class MayBeConstSpec : Spek({ assertThat(subject.findings).isEmpty() } - it("does not report const vals that use other const vals") { + @Test + fun `does not report const vals that use other const vals`() { val code = """ object Something { const val A = 0 @@ -62,7 +68,8 @@ class MayBeConstSpec : Spek({ assertThat(subject.findings).isEmpty() } - it("does not report none const val candidates") { + @Test + fun `does not report none const val candidates`() { val code = """ object Something { const val a = 0 @@ -75,8 +82,10 @@ class MayBeConstSpec : Spek({ } } - context("some vals that could be constants") { - it("is a simple val") { + @Nested + inner class `some vals that could be constants` { + @Test + fun `is a simple val`() { val code = """ val x = 1 """ @@ -86,7 +95,8 @@ class MayBeConstSpec : Spek({ ) } - it("is a simple JvmField val") { + @Test + fun `is a simple JvmField val`() { val code = """ @JvmField val x = 1 """ @@ -96,7 +106,8 @@ class MayBeConstSpec : Spek({ ) } - it("is a field in an object") { + @Test + fun `is a field in an object`() { val code = """ object Test { @JvmField val test = "Test" @@ -108,7 +119,8 @@ class MayBeConstSpec : Spek({ ) } - it("reports vals in companion objects") { + @Test + fun `reports vals in companion objects`() { val code = """ class Test { companion object { @@ -123,8 +135,10 @@ class MayBeConstSpec : Spek({ } } - context("vals that can be constants but detekt doesn't handle yet") { - it("is a constant binary expression") { + @Nested + inner class `vals that can be constants but detekt doesn't handle yet` { + @Test + fun `is a constant binary expression`() { val code = """ object Something { const val one = 1 @@ -137,7 +151,8 @@ class MayBeConstSpec : Spek({ ) } - it("is a constant binary expression in a companion object") { + @Test + fun `is a constant binary expression in a companion object`() { val code = """ class Test { companion object { @@ -152,7 +167,8 @@ class MayBeConstSpec : Spek({ ) } - it("is a nested constant binary expression") { + @Test + fun `is a nested constant binary expression`() { val code = """ object Something { const val one = 1 @@ -165,7 +181,8 @@ class MayBeConstSpec : Spek({ ) } - it("is a nested constant parenthesised expression") { + @Test + fun `is a nested constant parenthesised expression`() { val code = """ object Something { const val one = 1 @@ -178,7 +195,8 @@ class MayBeConstSpec : Spek({ ) } - it("reports vals that use other const vals") { + @Test + fun `reports vals that use other const vals`() { val code = """ object Something { const val a = 0 @@ -193,7 +211,8 @@ class MayBeConstSpec : Spek({ ) } - it("reports concatenated string vals") { + @Test + fun `reports concatenated string vals`() { val code = """ object Something { private const val A = "a" @@ -207,32 +226,38 @@ class MayBeConstSpec : Spek({ } } - context("vals that cannot be constants") { - it("does not report arrays") { + @Nested + inner class `vals that cannot be constants` { + @Test + fun `does not report arrays`() { val code = "val arr = arrayOf(\"a\", \"b\")" subject.compileAndLint(code) assertThat(subject.findings).isEmpty() } - it("is a var") { + @Test + fun `is a var`() { val code = "var test = 1" subject.compileAndLint(code) assertThat(subject.findings).isEmpty() } - it("has a getter") { + @Test + fun `has a getter`() { val code = "val withGetter get() = 42" subject.compileAndLint(code) assertThat(subject.findings).isEmpty() } - it("is initialized to null") { + @Test + fun `is initialized to null`() { val code = "val test = null" subject.compileAndLint(code) assertThat(subject.findings).isEmpty() } - it("is a JvmField in a class") { + @Test + fun `is a JvmField in a class`() { val code = """ class Test { @JvmField val a = 3 @@ -242,7 +267,8 @@ class MayBeConstSpec : Spek({ assertThat(subject.findings).isEmpty() } - it("has some annotation") { + @Test + fun `has some annotation`() { val code = """ annotation class A @@ -252,7 +278,8 @@ class MayBeConstSpec : Spek({ assertThat(subject.findings).isEmpty() } - it("overrides something") { + @Test + fun `overrides something`() { val code = """ interface Base { val property: Int @@ -266,13 +293,15 @@ class MayBeConstSpec : Spek({ assertThat(subject.findings).isEmpty() } - it("does not detect just a dollar as interpolation") { + @Test + fun `does not detect just a dollar as interpolation`() { val code = """ val hasDollar = "$" """ subject.compileAndLint(code) assertThat(subject.findings).hasSize(1) } - it("does not report interpolated strings") { + @Test + fun `does not report interpolated strings`() { val innerCode = "\"\"\"object Test { val TEST = \"Test \$test_var\"}\"\"\"" val classReference = "\${AnotherClass::class.java.name}" val staticReference = "\${AnotherClass.staticVariable}" @@ -293,7 +322,8 @@ class MayBeConstSpec : Spek({ assertThat(subject.findings).isEmpty() } - it("does not report vals inside anonymous object declaration") { + @Test + fun `does not report vals inside anonymous object declaration`() { subject.compileAndLint( """ fun main() { @@ -307,16 +337,19 @@ class MayBeConstSpec : Spek({ assertThat(subject.findings).isEmpty() } - it("does not report actual vals") { + @Test + fun `does not report actual vals`() { // Until the [KotlinScriptEngine] can compile KMP, we will only lint. subject.lint("""actual val abc123 = "abc123" """) assertThat(subject.findings).isEmpty() } } - context("some const val candidates in nested objects") { + @Nested + inner class `some const val candidates in nested objects` { - it("reports the const val candidates") { + @Test + fun `reports the const val candidates`() { val code = """ object Root { const val ROOT_CONST = 1 @@ -341,4 +374,4 @@ class MayBeConstSpec : Spek({ } } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ModifierOrderSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ModifierOrderSpec.kt index 28578c7a44c6..b0c219a07d4d 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ModifierOrderSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ModifierOrderSpec.kt @@ -5,32 +5,37 @@ import io.gitlab.arturbosch.detekt.test.TestConfig import io.gitlab.arturbosch.detekt.test.compileAndLint 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 +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test -class ModifierOrderSpec : Spek({ - val subject by memoized { ModifierOrder(Config.empty) } +class ModifierOrderSpec { + val subject = ModifierOrder(Config.empty) - describe("ModifierOrder rule") { + @Nested + inner class `ModifierOrder rule` { - context("kt classes with modifiers") { + @Nested + inner class `kt classes with modifiers` { val bad1 = "data internal class Test(val test: String)" val bad2 = "actual private class Test(val test: String)" val bad3 = "annotation expect class Test" - it("should report incorrectly ordered modifiers") { + @Test + fun `should report incorrectly ordered modifiers`() { assertThat(subject.compileAndLint(bad1)).hasSize(1) assertThat(subject.lint(bad2)).hasSize(1) assertThat(subject.lint(bad3)).hasSize(1) } - it("does not report correctly ordered modifiers") { + @Test + fun `does not report correctly ordered modifiers`() { assertThat(subject.compileAndLint("internal data class Test(val test: String)")).isEmpty() assertThat(subject.lint("private actual class Test(val test: String)")).isEmpty() assertThat(subject.lint("expect annotation class Test")).isEmpty() } - it("should not report issues if inactive") { + @Test + fun `should not report issues if inactive`() { val rule = ModifierOrder(TestConfig(mapOf(Config.ACTIVE_KEY to "false"))) assertThat(rule.compileAndLint(bad1)).isEmpty() assertThat(rule.lint(bad2)).isEmpty() @@ -38,22 +43,27 @@ class ModifierOrderSpec : Spek({ } } - context("a kt parameter with modifiers") { + @Nested + inner class `a kt parameter with modifiers` { - it("should report wrongly ordered modifiers") { + @Test + fun `should report wrongly ordered modifiers`() { val code = "lateinit internal var test: String" assertThat(subject.compileAndLint(code)).hasSize(1) } - it("should not report correctly ordered modifiers") { + @Test + fun `should not report correctly ordered modifiers`() { val code = "internal lateinit var test: String" assertThat(subject.compileAndLint(code)).isEmpty() } } - context("an overridden function") { + @Nested + inner class `an overridden function` { - it("should report incorrectly ordered modifiers") { + @Test + fun `should report incorrectly ordered modifiers`() { val code = """ abstract class A { abstract fun test() @@ -65,7 +75,8 @@ class ModifierOrderSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("should not report correctly ordered modifiers") { + @Test + fun `should not report correctly ordered modifiers`() { val code = """ abstract class A { abstract fun test() @@ -78,9 +89,11 @@ class ModifierOrderSpec : Spek({ } } - context("a tailrec function") { + @Nested + inner class `a tailrec function` { - it("should report incorrectly ordered modifiers") { + @Test + fun `should report incorrectly ordered modifiers`() { val code = """ public class A { tailrec private fun foo(x: Double = 1.0): Double = 1.0 @@ -89,7 +102,8 @@ class ModifierOrderSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("should not report correctly ordered modifiers") { + @Test + fun `should not report correctly ordered modifiers`() { val code = """ public class A { private tailrec fun foo(x: Double = 1.0): Double = 1.0 @@ -99,22 +113,27 @@ class ModifierOrderSpec : Spek({ } } - context("a vararg argument") { + @Nested + inner class `a vararg argument` { - it("should report incorrectly ordered modifiers") { + @Test + fun `should report incorrectly ordered modifiers`() { val code = "class Foo(vararg private val strings: String) {}" assertThat(subject.compileAndLint(code)).hasSize(1) } - it("should not report correctly ordered modifiers") { + @Test + fun `should not report correctly ordered modifiers`() { val code = "class Foo(private vararg val strings: String) {}" assertThat(subject.compileAndLint(code)).isEmpty() } } - context("fun interface") { + @Nested + inner class `fun interface` { - it("should not report correctly ordered modifiers") { + @Test + fun `should not report correctly ordered modifiers`() { val code = """ private fun interface LoadMoreCallback { fun loadMore(): Boolean @@ -124,9 +143,11 @@ class ModifierOrderSpec : Spek({ } } - context("value class") { + @Nested + inner class `value class` { - it("should not report correctly ordered modifiers") { + @Test + fun `should not report correctly ordered modifiers`() { val code = """ @JvmInline private value class Foo(val bar: Int) @@ -135,4 +156,4 @@ class ModifierOrderSpec : Spek({ } } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/MultilineLambdaItParameterSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/MultilineLambdaItParameterSpec.kt index 3f7c984ec019..010e6e14bac4 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/MultilineLambdaItParameterSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/MultilineLambdaItParameterSpec.kt @@ -1,21 +1,23 @@ package io.gitlab.arturbosch.detekt.rules.style import io.gitlab.arturbosch.detekt.api.Config -import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext import org.assertj.core.api.Assertions.assertThat import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -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 MultilineLambdaItParameterSpec : Spek({ - setupKotlinEnvironment() - val subject by memoized { MultilineLambdaItParameter(Config.empty) } - val env: KotlinCoreEnvironment by memoized() +@KotlinCoreEnvironmentTest +class MultilineLambdaItParameterSpec(val env: KotlinCoreEnvironment) { + val subject = MultilineLambdaItParameter(Config.empty) - describe("MultilineLambdaItParameter rule") { - context("single parameter, multiline lambda with multiple statements") { - it("reports when parameter name is implicit `it`") { + @Nested + inner class `MultilineLambdaItParameter rule` { + @Nested + inner class `single parameter, multiline lambda with multiple statements` { + @Test + fun `reports when parameter name is implicit 'it'`() { val code = """ fun f() { val digits = 1234.let { @@ -28,7 +30,8 @@ class MultilineLambdaItParameterSpec : Spek({ assertThat(findings).hasSize(1) } - it("reports when parameter name is explicit `it`") { + @Test + fun `reports when parameter name is explicit 'it'`() { val code = """ fun f() { val digits = 1234.let { it -> @@ -41,7 +44,8 @@ class MultilineLambdaItParameterSpec : Spek({ assertThat(findings).hasSize(1) } - it("does not report when parameter name is explicit and not `it`") { + @Test + fun `does not report when parameter name is explicit and not 'it'`() { val code = """ fun f() { val digits = 1234.let { param -> @@ -54,7 +58,8 @@ class MultilineLambdaItParameterSpec : Spek({ assertThat(findings).isEmpty() } - it("does not report when lambda has no implicit parameter references") { + @Test + fun `does not report when lambda has no implicit parameter references`() { val code = """ fun foo(f: (Int) -> Unit) {} fun main() { @@ -71,8 +76,10 @@ class MultilineLambdaItParameterSpec : Spek({ } } - context("single parameter, multiline lambda with a single statement") { - it("does not report when parameter name is an implicit `it`") { + @Nested + inner class `single parameter, multiline lambda with a single statement` { + @Test + fun `does not report when parameter name is an implicit 'it'`() { val code = """ fun f() { val digits = 1234.let { @@ -84,7 +91,8 @@ class MultilineLambdaItParameterSpec : Spek({ assertThat(findings).isEmpty() } - it("does not report when parameter name is an explicit `it`") { + @Test + fun `does not report when parameter name is an explicit 'it'`() { val code = """ fun f() { val digits = 1234.let { it -> @@ -95,7 +103,9 @@ class MultilineLambdaItParameterSpec : Spek({ val findings = subject.compileAndLintWithContext(env, code) assertThat(findings).isEmpty() } - it("does not report when parameter name is explicit and not `it`") { + + @Test + fun `does not report when parameter name is explicit and not 'it'`() { val code = """ fun f() { val digits = 1234.let { param -> @@ -108,8 +118,10 @@ class MultilineLambdaItParameterSpec : Spek({ } } - context("single parameter, single-line lambda") { - it("does not report when parameter name is an implicit `it` with type resolution") { + @Nested + inner class `single parameter, single-line lambda` { + @Test + fun `does not report when parameter name is an implicit 'it' with type resolution`() { val code = """ fun f() { val digits = 1234.let { listOf(it) } @@ -119,7 +131,8 @@ class MultilineLambdaItParameterSpec : Spek({ assertThat(findings).isEmpty() } - it("does not report when parameter name is an implicit `it`") { + @Test + fun `does not report when parameter name is an implicit 'it'`() { val code = """ fun f() { val digits = 1234.let { listOf(it) } @@ -129,7 +142,8 @@ class MultilineLambdaItParameterSpec : Spek({ assertThat(findings).isEmpty() } - it("does not report when parameter name is an explicit `it`") { + @Test + fun `does not report when parameter name is an explicit 'it'`() { val code = """ fun f() { val digits = 1234.let { it -> listOf(it) } @@ -139,7 +153,8 @@ class MultilineLambdaItParameterSpec : Spek({ assertThat(findings).isEmpty() } - it("does not report when parameter name is explicit and not `it`") { + @Test + fun `does not report when parameter name is explicit and not 'it'`() { val code = """ fun f() { val digits = 1234.let { param -> listOf(param) } @@ -150,8 +165,10 @@ class MultilineLambdaItParameterSpec : Spek({ } } - context("multiple parameters, multiline lambda") { - it("reports when one of the explicit parameters is an `it`") { + @Nested + inner class `multiple parameters, multiline lambda` { + @Test + fun `reports when one of the explicit parameters is an 'it'`() { val code = """ fun f() { val flat = listOf(listOf(1), listOf(2)).mapIndexed { index, it -> @@ -164,7 +181,8 @@ class MultilineLambdaItParameterSpec : Spek({ assertThat(findings).hasSize(1) } - it("does not report when none of the explicit parameters is an `it`") { + @Test + fun `does not report when none of the explicit parameters is an 'it'`() { val code = """ fun f() { val lambda = { item: Int, that: String -> @@ -178,8 +196,10 @@ class MultilineLambdaItParameterSpec : Spek({ } } - context("no parameter, multiline lambda with multiple statements") { - it("does not report when there is no parameter") { + @Nested + inner class `no parameter, multiline lambda with multiple statements` { + @Test + fun `does not report when there is no parameter`() { val code = """ fun f() { val string = StringBuilder().apply { @@ -193,4 +213,4 @@ class MultilineLambdaItParameterSpec : Spek({ } } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/NestedClassesVisibilitySpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/NestedClassesVisibilitySpec.kt index 8427d633d5e6..a8371c171ffc 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/NestedClassesVisibilitySpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/NestedClassesVisibilitySpec.kt @@ -2,15 +2,17 @@ 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 NestedClassesVisibilitySpec : Spek({ - val subject by memoized { NestedClassesVisibility() } +class NestedClassesVisibilitySpec { + val subject = NestedClassesVisibility() - describe("NestedClassesVisibility rule") { + @Nested + inner class `NestedClassesVisibility rule` { - it("reports explicit public visibility in nested objects/classes/interfaces") { + @Test + fun `reports explicit public visibility in nested objects, classes and interfaces`() { val code = """ internal class Outer { public interface A @@ -21,7 +23,8 @@ class NestedClassesVisibilitySpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(3) } - it("reports explicit public visibility in nested classes inside an enum") { + @Test + fun `reports explicit public visibility in nested classes inside an enum`() { val code = """ internal enum class Outer { A; @@ -31,7 +34,8 @@ class NestedClassesVisibilitySpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("does not report nested internal classes and interfaces") { + @Test + fun `does not report nested internal classes and interfaces`() { val code = """ internal class Outer { class A @@ -43,7 +47,8 @@ class NestedClassesVisibilitySpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report nested private classes") { + @Test + fun `does not report nested private classes`() { val code = """ internal class Outer { private class A @@ -52,7 +57,8 @@ class NestedClassesVisibilitySpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report nested public enums") { + @Test + fun `does not report nested public enums`() { val code = """ internal class Outer { public enum class E { E1; } @@ -61,7 +67,8 @@ class NestedClassesVisibilitySpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report companion object that is explicitly public") { + @Test + fun `does not report companion object that is explicitly public`() { val code = """ internal class Outer { public companion object C @@ -70,7 +77,8 @@ class NestedClassesVisibilitySpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report companion object") { + @Test + fun `does not report companion object`() { val code = """ internal class Outer { companion object C @@ -79,7 +87,8 @@ class NestedClassesVisibilitySpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report nested classes inside a private class") { + @Test + fun `does not report nested classes inside a private class`() { val code = """ private class Outer { class A @@ -88,7 +97,8 @@ class NestedClassesVisibilitySpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report nested internal classes inside an interface") { + @Test + fun `does not report nested internal classes inside an interface`() { val code = """ internal interface Outer { class A @@ -97,7 +107,8 @@ class NestedClassesVisibilitySpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report nested classes with a nesting depth higher than 1") { + @Test + fun `does not report nested classes with a nesting depth higher than 1`() { val code = """ internal class Outer { class C1 { @@ -108,4 +119,4 @@ class NestedClassesVisibilitySpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/NewLineAtEndOfFileSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/NewLineAtEndOfFileSpec.kt index 4f9a87dd12ce..67c6f53abf10 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/NewLineAtEndOfFileSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/NewLineAtEndOfFileSpec.kt @@ -2,29 +2,33 @@ package io.gitlab.arturbosch.detekt.rules.style import io.gitlab.arturbosch.detekt.test.assertThat import io.gitlab.arturbosch.detekt.test.compileAndLint -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 NewLineAtEndOfFileSpec : Spek({ +class NewLineAtEndOfFileSpec { - val subject by memoized { NewLineAtEndOfFile() } + val subject = NewLineAtEndOfFile() - describe("NewLineAtEndOfFile rule") { + @Nested + inner class `NewLineAtEndOfFile rule` { - it("should not flag a kt file containing new line at the end") { + @Test + fun `should not flag a kt file containing new line at the end`() { val code = "class Test\n\n" // we need double '\n' because .lint() applies .trimIndent() which removes one assertThat(subject.compileAndLint(code)).isEmpty() } - it("should flag a kt file not containing new line at the end") { + @Test + fun `should flag a kt file not containing new line at the end`() { val code = "class Test" assertThat(subject.compileAndLint(code)).hasSize(1) .hasSourceLocation(1, 11) } - it("should not flag an empty kt file") { + @Test + fun `should not flag an empty kt file`() { val code = "" assertThat(subject.compileAndLint(code)).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/NoTabsSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/NoTabsSpec.kt index 60f99a83331f..73c74bed6754 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/NoTabsSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/NoTabsSpec.kt @@ -3,25 +3,28 @@ package io.gitlab.arturbosch.detekt.rules.style import io.github.detekt.test.utils.compileForTest import io.gitlab.arturbosch.detekt.rules.Case import io.gitlab.arturbosch.detekt.test.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 NoTabsSpec : Spek({ +class NoTabsSpec { - val subject by memoized { NoTabs() } + @Nested + inner class `NoTabs rule` { - describe("NoTabs rule") { - - it("should flag a line that contains a tab") { + @Test + fun `should flag a line that contains a tab`() { + val subject = NoTabs() val file = compileForTest(Case.NoTabsPositive.path()) subject.findTabs(file) assertThat(subject.findings).hasSize(5) } - it("should not flag a line that does not contain a tab") { + @Test + fun `should not flag a line that does not contain a tab`() { + val subject = NoTabs() val file = compileForTest(Case.NoTabsNegative.path()) subject.findTabs(file) assertThat(subject.findings).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ObjectLiteralToLambdaSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ObjectLiteralToLambdaSpec.kt index 8823e8448f20..1dd4bbed6c9b 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ObjectLiteralToLambdaSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ObjectLiteralToLambdaSpec.kt @@ -1,26 +1,29 @@ package io.gitlab.arturbosch.detekt.rules.style +import io.github.detekt.test.utils.createEnvironment import io.github.detekt.test.utils.resourceAsPath import io.gitlab.arturbosch.detekt.api.SourceLocation -import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.assert import io.gitlab.arturbosch.detekt.test.compileAndLint import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext import io.gitlab.arturbosch.detekt.test.lintWithContext import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -import org.spekframework.spek2.Spek -import org.spekframework.spek2.style.specification.describe +import org.junit.jupiter.api.AfterAll +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test -class ObjectLiteralToLambdaSpec : Spek({ - setupKotlinEnvironment(additionalJavaSourceRootPath = resourceAsPath("java")) +@KotlinCoreEnvironmentTest +class ObjectLiteralToLambdaSpec(val env: KotlinCoreEnvironment) { + val subject = ObjectLiteralToLambda() - val env: KotlinCoreEnvironment by memoized() - val subject by memoized { ObjectLiteralToLambda() } + @Nested + inner class `ObjectLiteralToLambda rule` { - describe("ObjectLiteralToLambda rule") { - - context("report convertible expression") { - it("is property") { + @Nested + inner class `report convertible expression` { + @Test + fun `is property`() { val code = """ fun interface Sam { fun foo() @@ -36,7 +39,8 @@ class ObjectLiteralToLambdaSpec : Spek({ .hasSourceLocations(SourceLocation(4, 9)) } - it("is in function") { + @Test + fun `is in function`() { val code = """ fun interface Sam { fun foo() @@ -54,7 +58,8 @@ class ObjectLiteralToLambdaSpec : Spek({ .hasSourceLocations(SourceLocation(5, 5)) } - it("is in init") { + @Test + fun `is in init`() { val code = """ fun interface Sam { fun foo() @@ -74,7 +79,8 @@ class ObjectLiteralToLambdaSpec : Spek({ .hasSourceLocations(SourceLocation(6, 9)) } - it("is generic") { + @Test + fun `is generic`() { val code = """ fun interface Sam { fun foo(): T @@ -91,7 +97,8 @@ class ObjectLiteralToLambdaSpec : Spek({ .hasSourceLocations(SourceLocation(4, 9)) } - it("has other default method") { + @Test + fun `has other default method`() { val code = """ fun interface Sam { fun foo() @@ -108,7 +115,8 @@ class ObjectLiteralToLambdaSpec : Spek({ .hasSourceLocations(SourceLocation(5, 9)) } - it("nested declaration") { + @Test + fun `nested declaration`() { val code = """ interface First { fun foo() @@ -128,7 +136,8 @@ class ObjectLiteralToLambdaSpec : Spek({ .hasSourceLocations(SourceLocation(7, 5)) } - it("expression body syntax") { + @Test + fun `expression body syntax`() { val code = """ fun interface Sam { fun foo(): Int @@ -144,8 +153,10 @@ class ObjectLiteralToLambdaSpec : Spek({ } } - context("is not correct implement") { - it("without type resolution") { + @Nested + inner class `is not correct implement` { + @Test + fun `without type resolution`() { val code = """ fun interface Sam { fun foo() @@ -158,7 +169,8 @@ class ObjectLiteralToLambdaSpec : Spek({ subject.compileAndLint(code).assert().isEmpty() } - it("is empty interface") { + @Test + fun `is empty interface`() { val code = """ interface Sam val a = object : Sam {} @@ -166,7 +178,8 @@ class ObjectLiteralToLambdaSpec : Spek({ subject.compileAndLintWithContext(env, code).assert().isEmpty() } - it("is empty interface and has own function") { + @Test + fun `is empty interface and has own function`() { val code = """ interface Sam val a = object : Sam { @@ -177,7 +190,8 @@ class ObjectLiteralToLambdaSpec : Spek({ subject.compileAndLintWithContext(env, code).assert().isEmpty() } - it("is single property interface") { + @Test + fun `is single property interface`() { val code = """ interface Sam { val foo: Int @@ -189,7 +203,8 @@ class ObjectLiteralToLambdaSpec : Spek({ subject.compileAndLintWithContext(env, code).assert().isEmpty() } - it("is empty interface and has own property") { + @Test + fun `is empty interface and has own property`() { val code = """ interface Sam val a = object : Sam { @@ -199,7 +214,8 @@ class ObjectLiteralToLambdaSpec : Spek({ subject.compileAndLintWithContext(env, code).assert().isEmpty() } - it("is not fun interface") { + @Test + fun `is not fun interface`() { val code = """ interface Sam { fun foo() @@ -212,7 +228,8 @@ class ObjectLiteralToLambdaSpec : Spek({ subject.compileAndLintWithContext(env, code).assert().isEmpty() } - it("is not interface") { + @Test + fun `is not interface`() { val code = """ abstract class Something { abstract fun foo() @@ -225,7 +242,8 @@ class ObjectLiteralToLambdaSpec : Spek({ subject.compileAndLintWithContext(env, code).assert().isEmpty() } - it("has multi implement") { + @Test + fun `has multi implement`() { val code = """ fun interface First { fun foo() @@ -240,7 +258,8 @@ class ObjectLiteralToLambdaSpec : Spek({ subject.compileAndLintWithContext(env, code).assert().isEmpty() } - it("has complex implement") { + @Test + fun `has complex implement`() { val code = """ abstract class First { abstract fun foo() @@ -258,8 +277,10 @@ class ObjectLiteralToLambdaSpec : Spek({ } } - context("has impurities") { - it("has more than one method") { + @Nested + inner class `has impurities` { + @Test + fun `has more than one method`() { val code = """ fun interface Sam { fun foo() @@ -274,7 +295,8 @@ class ObjectLiteralToLambdaSpec : Spek({ subject.compileAndLintWithContext(env, code).assert().isEmpty() } - it("has property") { + @Test + fun `has property`() { val code = """ fun interface Sam { fun foo() @@ -288,7 +310,8 @@ class ObjectLiteralToLambdaSpec : Spek({ subject.compileAndLintWithContext(env, code).assert().isEmpty() } - it("has init") { + @Test + fun `has init`() { val code = """ fun interface Sam { fun foo() @@ -304,8 +327,10 @@ class ObjectLiteralToLambdaSpec : Spek({ } } - context("java interface") { - it("is convertible") { + @Nested + inner class `java interface` { + @Test + fun `is convertible`() { val code = """ val a = object : Runnable { override fun run(){ @@ -318,7 +343,8 @@ class ObjectLiteralToLambdaSpec : Spek({ .hasSourceLocations(SourceLocation(1, 9)) } - it("is convertible generic") { + @Test + fun `is convertible Callable generic`() { val code = """ import java.util.concurrent.Callable val a = object : Callable { @@ -333,7 +359,8 @@ class ObjectLiteralToLambdaSpec : Spek({ .hasSourceLocations(SourceLocation(2, 9)) } - it("empty interface") { + @Test + fun `empty interface`() { val code = """ import java.util.EventListener val a = object : EventListener { @@ -344,7 +371,8 @@ class ObjectLiteralToLambdaSpec : Spek({ subject.compileAndLintWithContext(env, code).assert().isEmpty() } - it("is convertible generic") { + @Test + fun `is convertible Enumeration generic`() { val code = """ import java.util.Enumeration val a = object : Enumeration { @@ -360,51 +388,70 @@ class ObjectLiteralToLambdaSpec : Spek({ subject.compileAndLintWithContext(env, code).assert().isEmpty() } - it("has other default methods") { - val code = """ - import com.example.fromjava.SamWithDefaultMethods - - fun main() { - val x = object : SamWithDefaultMethods { - override fun foo() { - println() + @Nested + inner class JavaSourceTests { + + private val environmentWrapper = + createEnvironment(additionalJavaSourceRootPaths = listOf(resourceAsPath("java").toFile())) + private val customEnv = environmentWrapper.env + + @AfterAll + fun disposeEnvironment() { + environmentWrapper.dispose() + } + + @Test + fun `has other default methods`() { + val code = """ + import com.example.fromjava.SamWithDefaultMethods + + fun main() { + val x = object : SamWithDefaultMethods { + override fun foo() { + println() + } } - } - } - """ - subject.lintWithContext(env, code).assert().hasSize(1) - } + } + """ - it("has only default methods") { - val code = """ - import com.example.fromjava.OnlyDefaultMethods - - fun main() { - val x = object : OnlyDefaultMethods { - } - } - """ - subject.lintWithContext(env, code).assert().isEmpty() - } + subject.lintWithContext(customEnv, code).assert().hasSize(1) + } - it("implements a default method") { - val code = """ - import com.example.fromjava.OnlyDefaultMethods - - fun main() { - val x = object : OnlyDefaultMethods { - override fun foo() { - println() + @Test + fun `has only default methods`() { + val code = """ + import com.example.fromjava.OnlyDefaultMethods + + fun main() { + val x = object : OnlyDefaultMethods { } - } - } - """ - subject.lintWithContext(env, code).assert().isEmpty() + } + """ + subject.lintWithContext(customEnv, code).assert().isEmpty() + } + + @Test + fun `implements a default method`() { + val code = """ + import com.example.fromjava.OnlyDefaultMethods + + fun main() { + val x = object : OnlyDefaultMethods { + override fun foo() { + println() + } + } + } + """ + subject.lintWithContext(customEnv, code).assert().isEmpty() + } } } - context("object use itself") { - it("call `this`") { + @Nested + inner class `object use itself` { + @Test + fun `call 'this'`() { val code = """ fun interface Sam { fun foo() @@ -421,7 +468,8 @@ class ObjectLiteralToLambdaSpec : Spek({ subject.compileAndLintWithContext(env, code).assert().isEmpty() } - it("use `this`") { + @Test + fun `use 'this'`() { val code = """ fun interface Sam { fun foo() @@ -440,7 +488,8 @@ class ObjectLiteralToLambdaSpec : Spek({ subject.compileAndLintWithContext(env, code).assert().isEmpty() } - it("use class method") { + @Test + fun `use class method`() { val code = """ fun interface Sam { fun foo() @@ -457,7 +506,8 @@ class ObjectLiteralToLambdaSpec : Spek({ subject.compileAndLintWithContext(env, code).assert().isEmpty() } - it("call `this` inside nested object") { + @Test + fun `call 'this' inside nested object`() { val code = """ fun interface Sam { fun foo() @@ -481,7 +531,8 @@ class ObjectLiteralToLambdaSpec : Spek({ .hasSourceLocations(SourceLocation(6, 5)) } - it("call labeled `this`") { + @Test + fun `call labeled 'this'`() { val code = """ fun interface Sam { fun foo() @@ -503,7 +554,8 @@ class ObjectLiteralToLambdaSpec : Spek({ .hasSourceLocations(SourceLocation(7, 9)) } - it("recursive call") { + @Test + fun `recursive call`() { val code = """ fun interface Sam { fun foo() @@ -521,14 +573,11 @@ class ObjectLiteralToLambdaSpec : Spek({ } } - context("Edge case") { + @Nested + inner class `Edge case` { // https://github.com/detekt/detekt/pull/3599#issuecomment-806389701 - it( - """Anonymous objects are always newly created, - |but lambdas are singletons, - |so they have the same reference. - """.trimMargin() - ) { + @Test + fun `Anonymous objects are always newly created, but lambdas are singletons, so they have the same reference`() { val code = """ fun interface Sam { fun foo() @@ -551,4 +600,4 @@ class ObjectLiteralToLambdaSpec : Spek({ } } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/OptionalAbstractKeywordSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/OptionalAbstractKeywordSpec.kt index 7c245c88ac95..56b6b91bf7c2 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/OptionalAbstractKeywordSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/OptionalAbstractKeywordSpec.kt @@ -2,25 +2,29 @@ package io.gitlab.arturbosch.detekt.rules.style import io.gitlab.arturbosch.detekt.test.assertThat import io.gitlab.arturbosch.detekt.test.compileAndLint -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 OptionalAbstractKeywordSpec : Spek({ - val subject by memoized { OptionalAbstractKeyword() } +class OptionalAbstractKeywordSpec { + val subject = OptionalAbstractKeyword() - describe("some abstract keyword definitions are checked for optionality") { + @Nested + inner class `some abstract keyword definitions are checked for optionality` { - it("does not report abstract keywords on an interface") { + @Test + fun `does not report abstract keywords on an interface`() { val code = "interface A {}" assertThat(subject.compileAndLint(code)).isEmpty() } - it("reports abstract interface with abstract property") { + @Test + fun `reports abstract interface with abstract property`() { val code = "abstract interface A { abstract var x: Int }" assertThat(subject.compileAndLint(code)).hasSize(2) } - it("reports abstract interface with abstract function") { + @Test + fun `reports abstract interface with abstract function`() { val code = "abstract interface A { abstract fun x() }" val findings = subject.compileAndLint(code) @@ -28,7 +32,8 @@ class OptionalAbstractKeywordSpec : Spek({ assertThat(findings).hasTextLocations(0 to 8, 23 to 31) } - it("reports nested abstract interface") { + @Test + fun `reports nested abstract interface`() { val code = """ class A { abstract interface B { @@ -39,12 +44,14 @@ class OptionalAbstractKeywordSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(2) } - it("does not report an abstract class") { + @Test + fun `does not report an abstract class`() { val code = "abstract class A { abstract fun x() }" assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report a nested abstract class function") { + @Test + fun `does not report a nested abstract class function`() { val code = """ interface I { abstract class A { @@ -55,4 +62,4 @@ class OptionalAbstractKeywordSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/OptionalWhenBracesSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/OptionalWhenBracesSpec.kt index 1b6f28893007..40faac0c15de 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/OptionalWhenBracesSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/OptionalWhenBracesSpec.kt @@ -3,15 +3,17 @@ package io.gitlab.arturbosch.detekt.rules.style import io.gitlab.arturbosch.detekt.api.SourceLocation import io.gitlab.arturbosch.detekt.test.assertThat import io.gitlab.arturbosch.detekt.test.compileAndLint -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 OptionalWhenBracesSpec : Spek({ - val subject by memoized { OptionalWhenBraces() } +class OptionalWhenBracesSpec { + val subject = OptionalWhenBraces() - describe("check optional braces in when expression") { + @Nested + inner class `check optional braces in when expression` { - it("does not report necessary braces") { + @Test + fun `does not report necessary braces`() { val code = """ fun x() { when (1) { @@ -30,7 +32,8 @@ class OptionalWhenBracesSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("reports unnecessary braces") { + @Test + fun `reports unnecessary braces`() { val code = """ fun x() { when (1) { @@ -42,7 +45,8 @@ class OptionalWhenBracesSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("reports unnecessary braces for nested when") { + @Test + fun `reports unnecessary braces for nested when`() { val code = """ import kotlin.random.Random @@ -68,8 +72,10 @@ class OptionalWhenBracesSpec : Spek({ .hasSourceLocations(SourceLocation(7, 17), SourceLocation(10, 17)) } - context("the statement is a lambda expression") { - it("does not report if the lambda has no arrow") { + @Nested + inner class `the statement is a lambda expression` { + @Test + fun `does not report if the lambda has no arrow`() { val code = """ fun test(b: Boolean): (Int) -> Int { return when (b) { @@ -81,7 +87,8 @@ class OptionalWhenBracesSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("reports if the lambda has an arrow") { + @Test + fun `reports if the lambda has an arrow`() { val code = """ fun test(b: Boolean): (Int) -> Int { return when (b) { @@ -94,4 +101,4 @@ class OptionalWhenBracesSpec : Spek({ } } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ProtectedMemberInFinalClassSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ProtectedMemberInFinalClassSpec.kt index b3b0b120ac74..1bc59f6c5829 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ProtectedMemberInFinalClassSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ProtectedMemberInFinalClassSpec.kt @@ -4,15 +4,17 @@ import io.gitlab.arturbosch.detekt.api.Config import io.gitlab.arturbosch.detekt.api.SourceLocation import io.gitlab.arturbosch.detekt.test.assertThat import io.gitlab.arturbosch.detekt.test.compileAndLint -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 ProtectedMemberInFinalClassSpec : Spek({ - val subject by memoized { ProtectedMemberInFinalClass(Config.empty) } +class ProtectedMemberInFinalClassSpec { + val subject = ProtectedMemberInFinalClass(Config.empty) - describe("check all variants of protected visibility modifier in final class") { + @Nested + inner class `check all variants of protected visibility modifier in final class` { - it("reports a protected field in a final class") { + @Test + fun `reports a protected field in a final class`() { val code = """ class Foo { protected var i1 = 0 @@ -23,7 +25,8 @@ class ProtectedMemberInFinalClassSpec : Spek({ assertThat(findings).hasSourceLocation(2, 5) } - it("reports a protected constructor in a final class") { + @Test + fun `reports a protected constructor in a final class`() { val code = """ class Foo { var i1: Int = 0 @@ -37,7 +40,8 @@ class ProtectedMemberInFinalClassSpec : Spek({ assertThat(findings).hasSourceLocation(3, 5) } - it("reports a protected function in a final class") { + @Test + fun `reports a protected function in a final class`() { val code = """ class Foo { protected fun function() {} @@ -48,7 +52,8 @@ class ProtectedMemberInFinalClassSpec : Spek({ assertThat(findings).hasSourceLocation(2, 5) } - it("reports an inner class with a protected field in a final class") { + @Test + fun `reports an inner class with a protected field in a final class`() { val code = """ class Foo { inner class InnerClass2 { @@ -61,7 +66,8 @@ class ProtectedMemberInFinalClassSpec : Spek({ assertThat(findings).hasSourceLocation(3, 9) } - it("reports a protected inner class with a protected field in a final class") { + @Test + fun `reports a protected inner class with a protected field in a final class`() { val code = """ class Fee { protected inner class YetAnotherInnerClass { @@ -77,7 +83,8 @@ class ProtectedMemberInFinalClassSpec : Spek({ ) } - it("reports a protected companion object in a final class") { + @Test + fun `reports a protected companion object in a final class`() { val code = """ class Foo { protected companion object { @@ -96,7 +103,8 @@ class ProtectedMemberInFinalClassSpec : Spek({ ) } - it("reports a protected companion object in an nested class") { + @Test + fun `reports a protected companion object in an nested class`() { val code = """ abstract class Foo { protected companion object { @@ -111,7 +119,8 @@ class ProtectedMemberInFinalClassSpec : Spek({ assertThat(findings).hasSourceLocation(4, 13) } - it("reports a protected field object in a final inner class") { + @Test + fun `reports a protected field object in a final inner class`() { val code = """ open class OpenClass { inner class InnerClass { @@ -124,7 +133,8 @@ class ProtectedMemberInFinalClassSpec : Spek({ assertThat(findings).hasSourceLocation(3, 9) } - it("reports a protected primary constructor in a final class") { + @Test + fun `reports a protected primary constructor in a final class`() { val code = """ class FinalClassWithProtectedConstructor protected constructor() """ @@ -134,9 +144,11 @@ class ProtectedMemberInFinalClassSpec : Spek({ } } - describe("check valid occurrences of protected that should not be reported") { + @Nested + inner class `check valid occurrences of protected that should not be reported` { - it("does not report non-protected members in final class") { + @Test + fun `does not report non-protected members in final class`() { val code = """ abstract class BaseClass class Foo : BaseClass() { @@ -146,7 +158,8 @@ class ProtectedMemberInFinalClassSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report overridden fields") { + @Test + fun `does not report overridden fields`() { val code = """ abstract class BaseClass { protected abstract val abstractProp : Int @@ -159,7 +172,8 @@ class ProtectedMemberInFinalClassSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report overridden functions") { + @Test + fun `does not report overridden functions`() { val code = """ abstract class BaseClass { protected abstract fun abstractFunction() @@ -173,7 +187,8 @@ class ProtectedMemberInFinalClassSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report protected definitions in abstract class") { + @Test + fun `does not report protected definitions in abstract class`() { val code = """ abstract class BaseClass { protected abstract val abstractProp: Int @@ -185,7 +200,8 @@ class ProtectedMemberInFinalClassSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report protected definitions in sealed class") { + @Test + fun `does not report protected definitions in sealed class`() { val code = """ sealed class SealedClass { protected fun a() {} @@ -194,7 +210,8 @@ class ProtectedMemberInFinalClassSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report protected definitions in enum class") { + @Test + fun `does not report protected definitions in enum class`() { val code = """ enum class EnumClass { ; @@ -204,4 +221,4 @@ class ProtectedMemberInFinalClassSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/RedundantExplicitTypeSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/RedundantExplicitTypeSpec.kt index 8cd5c140d09e..ce05311193a3 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/RedundantExplicitTypeSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/RedundantExplicitTypeSpec.kt @@ -1,22 +1,22 @@ package io.gitlab.arturbosch.detekt.rules.style import io.gitlab.arturbosch.detekt.api.Config -import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext import org.assertj.core.api.Assertions.assertThat import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -import org.spekframework.spek2.Spek -import org.spekframework.spek2.style.specification.describe +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test -object RedundantExplicitTypeSpec : Spek({ - setupKotlinEnvironment() +@KotlinCoreEnvironmentTest +class RedundantExplicitTypeSpec(val env: KotlinCoreEnvironment) { + val subject = RedundantExplicitType(Config.empty) - val env: KotlinCoreEnvironment by memoized() - val subject by memoized { RedundantExplicitType(Config.empty) } + @Nested + inner class `RedundantExplicitType` { - describe("RedundantExplicitType") { - - it("reports explicit type for boolean") { + @Test + fun `reports explicit type for boolean`() { val code = """ fun function() { val x: Boolean = true @@ -25,7 +25,8 @@ object RedundantExplicitTypeSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("reports explicit type for integer") { + @Test + fun `reports explicit type for integer`() { val code = """ fun function() { val x: Int = 3 @@ -34,7 +35,8 @@ object RedundantExplicitTypeSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("reports explicit type for long") { + @Test + fun `reports explicit type for long`() { val code = """ fun function() { val x: Long = 3L @@ -43,7 +45,8 @@ object RedundantExplicitTypeSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("reports explicit type for float") { + @Test + fun `reports explicit type for float`() { val code = """ fun function() { val x: Float = 3.0f @@ -52,7 +55,8 @@ object RedundantExplicitTypeSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("reports explicit type for double") { + @Test + fun `reports explicit type for double`() { val code = """ fun function() { val x: Double = 3.0 @@ -61,7 +65,8 @@ object RedundantExplicitTypeSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("reports explicit type for char") { + @Test + fun `reports explicit type for char`() { val code = """ fun function() { val x: Char = 'f' @@ -70,7 +75,8 @@ object RedundantExplicitTypeSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("reports explicit type for string template") { + @Test + fun `reports explicit type for string template`() { val substitute = "\$x" val code = """ fun function() { @@ -81,7 +87,8 @@ object RedundantExplicitTypeSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("reports explicit type for name reference expression") { + @Test + fun `reports explicit type for name reference expression`() { val code = """ object Test @@ -92,7 +99,8 @@ object RedundantExplicitTypeSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("reports explicit type for call expression") { + @Test + fun `reports explicit type for call expression`() { val code = """ interface Person { val firstName: String @@ -107,7 +115,8 @@ object RedundantExplicitTypeSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("does not report explicit type for call expression when type is an interface") { + @Test + fun `does not report explicit type for call expression when type is an interface`() { val code = """ interface Person { val firstName: String @@ -122,4 +131,4 @@ object RedundantExplicitTypeSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/RedundantHigherOrderMapUsageSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/RedundantHigherOrderMapUsageSpec.kt index 812a32247b88..d73d3da62697 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/RedundantHigherOrderMapUsageSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/RedundantHigherOrderMapUsageSpec.kt @@ -1,19 +1,20 @@ package io.gitlab.arturbosch.detekt.rules.style -import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.assertThat import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -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 RedundantHigherOrderMapUsageSpec : Spek({ - setupKotlinEnvironment() - val env: KotlinCoreEnvironment by memoized() - val subject by memoized { RedundantHigherOrderMapUsage() } +@KotlinCoreEnvironmentTest +class RedundantHigherOrderMapUsageSpec(val env: KotlinCoreEnvironment) { + val subject = RedundantHigherOrderMapUsage() - describe("report RedundantHigherOrderMapUsage rule") { - it("simple") { + @Nested + inner class `report RedundantHigherOrderMapUsage rule` { + @Test + fun `simple`() { val code = """ fun test() { listOf(1, 2, 3) @@ -27,7 +28,8 @@ class RedundantHigherOrderMapUsageSpec : Spek({ assertThat(findings[0]).hasMessage("This 'map' call can be removed.") } - it("lambda body is not single statement") { + @Test + fun `lambda body is not single statement`() { val code = """ fun doSomething() {} @@ -46,7 +48,8 @@ class RedundantHigherOrderMapUsageSpec : Spek({ assertThat(findings[0]).hasMessage("This 'map' call can be replaced with 'onEach' or 'forEach'.") } - it("explicit lambda parameter") { + @Test + fun `explicit lambda parameter`() { val code = """ fun test() { listOf(1, 2, 3).map { foo -> foo } @@ -56,7 +59,8 @@ class RedundantHigherOrderMapUsageSpec : Spek({ assertThat(findings).hasSize(1) } - it("lambda in argument list") { + @Test + fun `lambda in argument list`() { val code = """ fun test() { listOf(1).map({ it }) @@ -66,7 +70,8 @@ class RedundantHigherOrderMapUsageSpec : Spek({ assertThat(findings).hasSize(1) } - it("labeled return") { + @Test + fun `labeled return`() { val code = """ fun test(list: List) { list.map { @@ -80,7 +85,8 @@ class RedundantHigherOrderMapUsageSpec : Spek({ assertThat(findings).hasSize(1) } - it("return for outer function") { + @Test + fun `return for outer function`() { val code = """ fun doSomething() {} @@ -96,7 +102,8 @@ class RedundantHigherOrderMapUsageSpec : Spek({ assertThat(findings).hasSize(1) } - it("return for outer lambda") { + @Test + fun `return for outer lambda`() { val code = """ fun test(list: List): List { return listOf("a", "b", "c").map outer@{ s -> @@ -111,7 +118,8 @@ class RedundantHigherOrderMapUsageSpec : Spek({ assertThat(findings).hasSize(1) } - it("implicit receiver") { + @Test + fun `implicit receiver`() { val code = """ fun List.test() { map { it } @@ -121,7 +129,8 @@ class RedundantHigherOrderMapUsageSpec : Spek({ assertThat(findings).hasSize(1) } - it("this receiver") { + @Test + fun `this receiver`() { val code = """ fun List.test() { this.map { it } @@ -131,7 +140,8 @@ class RedundantHigherOrderMapUsageSpec : Spek({ assertThat(findings).hasSize(1) } - it("mutable list receiver") { + @Test + fun `mutable list receiver`() { val code = """ fun test() { mutableListOf(1).map { it } @@ -141,7 +151,8 @@ class RedundantHigherOrderMapUsageSpec : Spek({ assertThat(findings).hasSize(1) } - it("sequence receiver") { + @Test + fun `sequence receiver`() { val code = """ fun test() { val x:Sequence = sequenceOf(1).map { it } @@ -151,7 +162,8 @@ class RedundantHigherOrderMapUsageSpec : Spek({ assertThat(findings).hasSize(1) } - it("set receiver") { + @Test + fun `set receiver`() { val code = """ fun test() { setOf(1).map { it } @@ -163,8 +175,10 @@ class RedundantHigherOrderMapUsageSpec : Spek({ } } - describe("does not report RedundantHigherOrderMapUsage rule") { - it("last statement is not lambda parameter") { + @Nested + inner class `does not report RedundantHigherOrderMapUsage rule` { + @Test + fun `last statement is not lambda parameter`() { val code = """ fun test() { listOf(1, 2, 3) @@ -176,7 +190,8 @@ class RedundantHigherOrderMapUsageSpec : Spek({ assertThat(findings).isEmpty() } - it("labeled return is not lambda parameter") { + @Test + fun `labeled return is not lambda parameter`() { val code = """ fun test(list: List) { list.map { @@ -189,7 +204,8 @@ class RedundantHigherOrderMapUsageSpec : Spek({ assertThat(findings).isEmpty() } - it("destructuring lambda parameter") { + @Test + fun `destructuring lambda parameter`() { val code = """ fun test() { listOf(1 to 2).map { (a, b) -> a } @@ -199,7 +215,8 @@ class RedundantHigherOrderMapUsageSpec : Spek({ assertThat(findings).isEmpty() } - it("map receiver") { + @Test + fun `map receiver`() { val code = """ fun test() { val x: List> = mapOf(1 to "a").map { it } @@ -209,4 +226,4 @@ class RedundantHigherOrderMapUsageSpec : Spek({ assertThat(findings).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/RedundantVisibilityModifierRuleSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/RedundantVisibilityModifierRuleSpec.kt index e9f97ddd97d9..a766e930bfa2 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/RedundantVisibilityModifierRuleSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/RedundantVisibilityModifierRuleSpec.kt @@ -10,14 +10,16 @@ import org.jetbrains.kotlin.config.AnalysisFlags import org.jetbrains.kotlin.config.ExplicitApiMode import org.jetbrains.kotlin.config.LanguageVersionSettings import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactoryImpl -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 RedundantVisibilityModifierRuleSpec : Spek({ - val subject by memoized { RedundantVisibilityModifierRule() } +class RedundantVisibilityModifierRuleSpec { + val subject = RedundantVisibilityModifierRule() - describe("RedundantVisibilityModifier rule") { - it("does not report overridden function of abstract class w/ public modifier") { + @Nested + inner class `RedundantVisibilityModifier rule` { + @Test + fun `does not report overridden function of abstract class with public modifier`() { val code = """ abstract class A { abstract protected fun f() @@ -30,7 +32,8 @@ class RedundantVisibilityModifierRuleSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report overridden function of abstract class w/o public modifier") { + @Test + fun `does not report overridden function of abstract class without public modifier`() { val code = """ abstract class A { abstract protected fun f() @@ -43,7 +46,8 @@ class RedundantVisibilityModifierRuleSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report overridden function of interface") { + @Test + fun `does not report overridden function of interface`() { val code = """ interface A { fun f() @@ -56,7 +60,8 @@ class RedundantVisibilityModifierRuleSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("should ignore the issue by alias suppression") { + @Test + fun `should ignore the issue by alias suppression`() { val code = """ class Test { @Suppress("RedundantVisibilityModifier") @@ -66,7 +71,8 @@ class RedundantVisibilityModifierRuleSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("reports public function in class") { + @Test + fun `reports public function in class`() { val code = """ class Test { public fun f() {} @@ -75,7 +81,8 @@ class RedundantVisibilityModifierRuleSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("does not report function in class w/o modifier") { + @Test + fun `does not report function in class without modifier`() { val code = """ class Test { fun f() {} @@ -84,7 +91,8 @@ class RedundantVisibilityModifierRuleSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("reports public class") { + @Test + fun `reports public class`() { val code = """ public class Test { fun f() {} @@ -93,7 +101,8 @@ class RedundantVisibilityModifierRuleSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("reports interface w/ public modifier") { + @Test + fun `reports interface with public modifier`() { val code = """ public interface Test { public fun f() @@ -102,7 +111,8 @@ class RedundantVisibilityModifierRuleSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(2) } - it("reports field w/ public modifier") { + @Test + fun `reports field with public modifier`() { val code = """ class Test { public val str : String = "test" @@ -111,7 +121,8 @@ class RedundantVisibilityModifierRuleSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("does not report field w/o public modifier") { + @Test + fun `does not report field without public modifier`() { val code = """ class Test { val str : String = "test" @@ -120,7 +131,8 @@ class RedundantVisibilityModifierRuleSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report overridden field w/o public modifier") { + @Test + fun `does not report overridden field without public modifier`() { val code = """ abstract class A { abstract val test: String @@ -133,7 +145,8 @@ class RedundantVisibilityModifierRuleSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report overridden field w/ public modifier") { + @Test + fun `does not report overridden field with public modifier`() { val code = """ abstract class A { abstract val test: String @@ -146,7 +159,8 @@ class RedundantVisibilityModifierRuleSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("reports internal modifier on nested class in private object") { + @Test + fun `reports internal modifier on nested class in private object`() { val code = """ private object A { internal class InternalClass @@ -155,7 +169,8 @@ class RedundantVisibilityModifierRuleSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("reports internal modifier on function declaration in private object") { + @Test + fun `reports internal modifier on function declaration in private object`() { val code = """ private object A { internal fun internalFunction() {} @@ -164,9 +179,10 @@ class RedundantVisibilityModifierRuleSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - describe("Explicit API mode") { + @Nested + inner class `Explicit API mode` { - val code by memoized { + val code = compileContentForTest( """ public class A() { @@ -174,8 +190,8 @@ class RedundantVisibilityModifierRuleSpec : Spek({ } """ ) - } - val rule by memoized { RedundantVisibilityModifierRule() } + + val rule = RedundantVisibilityModifierRule() fun mockCompilerResources(mode: ExplicitApiMode): CompilerResources { val languageVersionSettings = mockk() @@ -187,20 +203,23 @@ class RedundantVisibilityModifierRuleSpec : Spek({ return CompilerResources(languageVersionSettings, DataFlowValueFactoryImpl(languageVersionSettings)) } - it("does not report public function in class if explicit API mode is set to strict") { + @Test + fun `does not report public function in class if explicit API mode is set to strict`() { rule.visitFile(code, compilerResources = mockCompilerResources(ExplicitApiMode.STRICT)) assertThat(rule.findings).isEmpty() } - it("reports public function in class if explicit API mode is disabled") { + @Test + fun `reports public function in class if explicit API mode is disabled`() { rule.visitFile(code, compilerResources = mockCompilerResources(ExplicitApiMode.DISABLED)) assertThat(rule.findings).hasSize(1) } - it("reports public function in class if compiler resources are not available") { + @Test + fun `reports public function in class if compiler resources are not available`() { rule.visitFile(code, compilerResources = null) assertThat(rule.findings).hasSize(1) } } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ReturnCountSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ReturnCountSpec.kt index b12a4a5b9214..5eea28c0428a 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ReturnCountSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ReturnCountSpec.kt @@ -5,8 +5,8 @@ import io.gitlab.arturbosch.detekt.test.TestConfig import io.gitlab.arturbosch.detekt.test.compileAndLint 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 +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test private const val MAX = "max" private const val EXCLUDED_FUNCTIONS = "excludedFunctions" @@ -14,31 +14,37 @@ private const val EXCLUDE_LABELED = "excludeLabeled" private const val EXCLUDE_RETURN_FROM_LAMBDA = "excludeReturnFromLambda" private const val EXCLUDE_GUARD_CLAUSES = "excludeGuardClauses" -class ReturnCountSpec : Spek({ +class ReturnCountSpec { - describe("ReturnCount rule") { + @Nested + inner class `ReturnCount rule` { - context("a function without a body") { + @Nested + inner class `a function without a body` { val code = """ fun func() = Unit """ - it("does not report violation by default") { + @Test + fun `does not report violation by default`() { assertThat(ReturnCount(Config.empty).compileAndLint(code)).isEmpty() } } - context("a function with an empty body") { + @Nested + inner class `a function with an empty body` { val code = """ fun func() {} """ - it("does not report violation by default") { + @Test + fun `does not report violation by default`() { assertThat(ReturnCount(Config.empty).compileAndLint(code)).isEmpty() } } - context("a file with an if condition guard clause and 2 returns") { + @Nested + inner class `a file with an if condition guard clause and 2 returns` { val code = """ fun test(x: Int): Int { if (x < 4) return 0 @@ -50,14 +56,16 @@ class ReturnCountSpec : Spek({ } """ - it("should not get flagged for if condition guard clauses") { + @Test + fun `should not get flagged for if condition guard clauses`() { val findings = ReturnCount(TestConfig(mapOf(EXCLUDE_GUARD_CLAUSES to "true"))) .compileAndLint(code) assertThat(findings).isEmpty() } } - context("a file with an if condition guard clause with body and 2 returns") { + @Nested + inner class `a file with an if condition guard clause with body and 2 returns` { val code = """ fun test(x: Int): Int { if (x < 4) { @@ -72,20 +80,23 @@ class ReturnCountSpec : Spek({ } """ - it("should not get flagged for if condition guard clauses") { + @Test + fun `should not get flagged for if condition guard clauses`() { val findings = ReturnCount(TestConfig(mapOf(EXCLUDE_GUARD_CLAUSES to "true"))) .compileAndLint(code) assertThat(findings).isEmpty() } - it("should get flagged without guard clauses") { + @Test + fun `should get flagged without guard clauses`() { val findings = ReturnCount(TestConfig(mapOf(EXCLUDE_GUARD_CLAUSES to "false"))) .compileAndLint(code) assertThat(findings).hasSize(1) } } - context("reports a too-complicated if statement for being a guard clause") { + @Nested + inner class `reports a too-complicated if statement for being a guard clause` { val code = """ fun test(x: Int): Int { if (x < 4) { @@ -104,14 +115,16 @@ class ReturnCountSpec : Spek({ } """ - it("should report a too-complicated if statement for being a guard clause, with EXCLUDE_GUARD_CLAUSES on") { + @Test + fun `should report a too-complicated if statement for being a guard clause, with EXCLUDE_GUARD_CLAUSES on`() { val findings = ReturnCount(TestConfig(mapOf(EXCLUDE_GUARD_CLAUSES to "true"))) .compileAndLint(code) assertThat(findings).hasSize(1) } } - context("a file with an ELVIS operator guard clause and 2 returns") { + @Nested + inner class `a file with an ELVIS operator guard clause and 2 returns` { val code = """ fun test(x: Int): Int { val y = x ?: return 0 @@ -123,14 +136,16 @@ class ReturnCountSpec : Spek({ } """ - it("should not get flagged for ELVIS operator guard clauses") { + @Test + fun `should not get flagged for ELVIS operator guard clauses`() { val findings = ReturnCount(TestConfig(mapOf(EXCLUDE_GUARD_CLAUSES to "true"))) .compileAndLint(code) assertThat(findings).isEmpty() } } - context("a file with 2 returns and an if condition guard clause which is not the first statement") { + @Nested + inner class `a file with 2 returns and an if condition guard clause which is not the first statement` { val code = """ fun test(x: Int): Int { when (x) { @@ -142,14 +157,16 @@ class ReturnCountSpec : Spek({ } """ - it("should get flagged for an if condition guard clause which is not the first statement") { + @Test + fun `should get flagged for an if condition guard clause which is not the first statement`() { val findings = ReturnCount(TestConfig(mapOf(EXCLUDE_GUARD_CLAUSES to "true"))) .compileAndLint(code) assertThat(findings).hasSize(1) } } - context("a file with 2 returns and an ELVIS guard clause which is not the first statement") { + @Nested + inner class `a file with 2 returns and an ELVIS guard clause which is not the first statement` { val code = """ fun test(x: Int): Int { when (x) { @@ -161,14 +178,16 @@ class ReturnCountSpec : Spek({ } """ - it("should get flagged for an ELVIS guard clause which is not the first statement") { + @Test + fun `should get flagged for an ELVIS guard clause which is not the first statement`() { val findings = ReturnCount(TestConfig(mapOf(EXCLUDE_GUARD_CLAUSES to "true"))) .compileAndLint(code) assertThat(findings).hasSize(1) } } - context("a file with multiple guard clauses") { + @Nested + inner class `a file with multiple guard clauses` { val code = """ fun multipleGuards(a: Int?, b: Any?, c: Int?) { if(a == null) return @@ -183,7 +202,8 @@ class ReturnCountSpec : Spek({ } """ - it("should not count all four guard clauses") { + @Test + fun `should not count all four guard clauses`() { val findings = ReturnCount( TestConfig( EXCLUDE_GUARD_CLAUSES to "true" @@ -192,7 +212,8 @@ class ReturnCountSpec : Spek({ assertThat(findings).isEmpty() } - it("should count all four guard clauses") { + @Test + fun `should count all four guard clauses`() { val findings = ReturnCount( TestConfig( EXCLUDE_GUARD_CLAUSES to "false" @@ -202,7 +223,8 @@ class ReturnCountSpec : Spek({ } } - context("a file with 3 returns") { + @Nested + inner class `a file with 3 returns` { val code = """ fun test(x: Int): Int { when (x) { @@ -214,23 +236,27 @@ class ReturnCountSpec : Spek({ } """ - it("should get flagged by default") { + @Test + fun `should get flagged by default`() { val findings = ReturnCount().compileAndLint(code) assertThat(findings).hasSize(1) } - it("should not get flagged when max value is 3") { + @Test + fun `should not get flagged when max value is 3`() { val findings = ReturnCount(TestConfig(mapOf(MAX to "3"))).compileAndLint(code) assertThat(findings).isEmpty() } - it("should get flagged when max value is 1") { + @Test + fun `should get flagged when max value is 1`() { val findings = ReturnCount(TestConfig(mapOf(MAX to "1"))).compileAndLint(code) assertThat(findings).hasSize(1) } } - context("a file with 2 returns") { + @Nested + inner class `a file with 2 returns` { val code = """ fun test(x: Int): Int { when (x) { @@ -241,23 +267,27 @@ class ReturnCountSpec : Spek({ } """ - it("should not get flagged by default") { + @Test + fun `should not get flagged by default`() { val findings = ReturnCount().compileAndLint(code) assertThat(findings).isEmpty() } - it("should not get flagged when max value is 2") { + @Test + fun `should not get flagged when max value is 2`() { val findings = ReturnCount(TestConfig(mapOf(MAX to "2"))).compileAndLint(code) assertThat(findings).isEmpty() } - it("should get flagged when max value is 1") { + @Test + fun `should get flagged when max value is 1`() { val findings = ReturnCount(TestConfig(mapOf(MAX to "1"))).compileAndLint(code) assertThat(findings).hasSize(1) } } - context("a function is ignored") { + @Nested + inner class `a function is ignored` { val code = """ fun test(x: Int): Int { when (x) { @@ -269,7 +299,8 @@ class ReturnCountSpec : Spek({ } """ - it("should not get flagged") { + @Test + fun `should not get flagged`() { val findings = ReturnCount( TestConfig( mapOf( @@ -282,7 +313,8 @@ class ReturnCountSpec : Spek({ } } - context("a subset of functions are ignored") { + @Nested + inner class `a subset of functions are ignored` { val code = """ fun test1(x: Int): Int { when (x) { @@ -312,7 +344,8 @@ class ReturnCountSpec : Spek({ } """ - it("should flag none of the ignored functions") { + @Test + fun `should flag none of the ignored functions`() { val findings = ReturnCount( TestConfig( mapOf( @@ -325,7 +358,8 @@ class ReturnCountSpec : Spek({ } } - context("a function with inner object") { + @Nested + inner class `a function with inner object` { val code = """ fun test(x: Int): Int { val a = object { @@ -345,13 +379,15 @@ class ReturnCountSpec : Spek({ } """ - it("should not get flag when returns is in inner object") { + @Test + fun `should not get flag when returns is in inner object`() { val findings = ReturnCount(TestConfig(mapOf(MAX to "2"))).compileAndLint(code) assertThat(findings).isEmpty() } } - context("a function with 2 inner object") { + @Nested + inner class `a function with 2 inner object` { val code = """ fun test(x: Int): Int { val a = object { @@ -380,13 +416,15 @@ class ReturnCountSpec : Spek({ } """ - it("should not get flag when returns is in inner object") { + @Test + fun `should not get flag when returns is in inner object`() { val findings = ReturnCount(TestConfig(mapOf(MAX to "2"))).compileAndLint(code) assertThat(findings).isEmpty() } } - context("a function with 2 inner object and exceeded max") { + @Nested + inner class `a function with 2 inner object and exceeded max` { val code = """ fun test(x: Int): Int { val a = object { @@ -417,13 +455,15 @@ class ReturnCountSpec : Spek({ } """ - it("should get flagged when returns is in inner object") { + @Test + fun `should get flagged when returns is in inner object`() { val findings = ReturnCount(TestConfig(mapOf(MAX to "2"))).compileAndLint(code) assertThat(findings).hasSize(1) } } - context("function with multiple labeled return statements") { + @Nested + inner class `function with multiple labeled return statements` { val code = """ fun readUsers(name: String): Flowable { @@ -435,19 +475,22 @@ class ReturnCountSpec : Spek({ } """ - it("should not count labeled returns from lambda by default") { + @Test + fun `should not count labeled returns from lambda by default`() { val findings = ReturnCount().lint(code) assertThat(findings).isEmpty() } - it("should count labeled returns from lambda when activated") { + @Test + fun `should count labeled returns from lambda when activated`() { val findings = ReturnCount( TestConfig(mapOf(EXCLUDE_RETURN_FROM_LAMBDA to "false")) ).lint(code) assertThat(findings).hasSize(1) } - it("should be empty when labeled returns are de-activated") { + @Test + fun `should be empty when labeled returns are de-activated`() { val findings = ReturnCount( TestConfig( mapOf( @@ -460,4 +503,4 @@ class ReturnCountSpec : Spek({ } } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/SafeCastSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/SafeCastSpec.kt index ab7347b430a8..03b0baa2b9c5 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/SafeCastSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/SafeCastSpec.kt @@ -2,15 +2,17 @@ package io.gitlab.arturbosch.detekt.rules.style import io.gitlab.arturbosch.detekt.test.assertThat import io.gitlab.arturbosch.detekt.test.compileAndLint -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 SafeCastSpec : Spek({ - val subject by memoized { SafeCast() } +class SafeCastSpec { + val subject = SafeCast() - describe("SafeCast rule") { + @Nested + inner class `SafeCast rule` { - it("reports negated expression") { + @Test + fun `reports negated expression`() { val code = """ fun test(element: Int) { val cast = if (element !is Number) { @@ -23,7 +25,8 @@ class SafeCastSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("reports expression") { + @Test + fun `reports expression`() { val code = """ fun test(element: Int) { val cast = if (element is Number) { @@ -36,7 +39,8 @@ class SafeCastSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("does not report wrong condition") { + @Test + fun `does not report wrong condition`() { val code = """ fun test(element: Int) { val other = 3 @@ -50,7 +54,8 @@ class SafeCastSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report wrong else clause") { + @Test + fun `does not report wrong else clause`() { val code = """ fun test(element: Int) { val cast = if (element is Number) { @@ -63,4 +68,4 @@ class SafeCastSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/SerialVersionUIDInSerializableClassSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/SerialVersionUIDInSerializableClassSpec.kt index 07e7a532f88e..7950e5be07eb 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/SerialVersionUIDInSerializableClassSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/SerialVersionUIDInSerializableClassSpec.kt @@ -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 SerialVersionUIDInSerializableClassSpec : Spek({ - val subject by memoized { SerialVersionUIDInSerializableClass(Config.empty) } +class SerialVersionUIDInSerializableClassSpec { + val subject = SerialVersionUIDInSerializableClass(Config.empty) - describe("SerialVersionUIDInSerializableClass rule") { + @Nested + inner class `SerialVersionUIDInSerializableClass rule` { - it("reports class with no serialVersionUID") { + @Test + fun `reports class with no serialVersionUID`() { val code = """ import java.io.Serializable @@ -20,7 +22,8 @@ class SerialVersionUIDInSerializableClassSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("reports class with wrong datatype") { + @Test + fun `reports class with wrong datatype`() { val code = """ import java.io.Serializable @@ -33,7 +36,8 @@ class SerialVersionUIDInSerializableClassSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("reports class with wrong explicitly defined datatype") { + @Test + fun `reports class with wrong explicitly defined datatype`() { val code = """ import java.io.Serializable @@ -46,7 +50,8 @@ class SerialVersionUIDInSerializableClassSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("reports class with wrong naming and without const modifier") { + @Test + fun `reports class with wrong naming and without const modifier`() { val code = """ import java.io.Serializable @@ -63,12 +68,14 @@ class SerialVersionUIDInSerializableClassSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(2) } - it("does not report a unserializable class") { + @Test + fun `does not report a unserializable class`() { val code = "class NoSerializableClass" assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report an interface that implements Serializable") { + @Test + fun `does not report an interface that implements Serializable`() { val code = """ import java.io.Serializable @@ -77,7 +84,8 @@ class SerialVersionUIDInSerializableClassSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report UID constant with positive value") { + @Test + fun `does not report UID constant with positive value`() { val code = """ import java.io.Serializable @@ -90,7 +98,8 @@ class SerialVersionUIDInSerializableClassSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report UID constant with negative value") { + @Test + fun `does not report UID constant with negative value`() { val code = """ import java.io.Serializable @@ -103,7 +112,8 @@ class SerialVersionUIDInSerializableClassSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report UID constant with explicit Long type") { + @Test + fun `does not report UID constant with explicit Long type`() { val code = """ import java.io.Serializable @@ -116,4 +126,4 @@ class SerialVersionUIDInSerializableClassSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/SpacingBetweenPackageAndImportsSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/SpacingBetweenPackageAndImportsSpec.kt index 11621456b459..8bdac35d149c 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/SpacingBetweenPackageAndImportsSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/SpacingBetweenPackageAndImportsSpec.kt @@ -5,90 +5,107 @@ import io.gitlab.arturbosch.detekt.api.Config import io.gitlab.arturbosch.detekt.test.compileAndLint 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 +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test -class SpacingBetweenPackageAndImportsSpec : Spek({ - val subject by memoized { SpacingBetweenPackageAndImports(Config.empty) } +class SpacingBetweenPackageAndImportsSpec { + val subject = SpacingBetweenPackageAndImports(Config.empty) - describe("SpacingBetweenPackageAndImports rule") { + @Nested + inner class `SpacingBetweenPackageAndImports rule` { - it("has no blank lines violation") { + @Test + fun `has no blank lines violation`() { val code = "package test\n\nimport a.b\n\nclass A {}" assertThat(subject.lint(code)).isEmpty() } - it("has a package and import declaration") { + @Test + fun `has a package and import declaration`() { val code = "package test\n\nimport a.b" assertThat(subject.lint(code)).isEmpty() } - it("has no import declaration") { + @Test + fun `has no import declaration`() { val code = "package test\n\nclass A {}" assertThat(subject.lint(code)).isEmpty() } - it("has no package declaration") { + @Test + fun `has no package declaration`() { val code = "import a.b\n\nclass A {}" assertThat(subject.lint(code)).isEmpty() } - it("has no package and import declaration") { + @Test + fun `has no package and import declaration`() { val code = "class A {}" assertThat(subject.lint(code)).isEmpty() } - it("has a comment declaration") { + @Test + fun `has a comment declaration`() { val code = "import a.b\n\n// a comment" assertThat(subject.lint(code)).isEmpty() } - it("is an empty kt file") { + @Test + fun `is an empty kt file`() { assertThat(subject.lint("")).isEmpty() } - describe("Kotlin scripts") { + @Nested + inner class `Kotlin scripts` { - it("has no package declaration in script") { + @Test + fun `has no package declaration in script`() { val code = "import a.b\n\nprint(1)" val ktsFile = compileContentForTest(code, "Test.kts") assertThat(subject.lint(ktsFile)).isEmpty() } - it("has no package and import declaration in script") { + @Test + fun `has no package and import declaration in script`() { val code = "print(1)" val ktsFile = compileContentForTest(code, "Test.kts") assertThat(subject.lint(ktsFile)).isEmpty() } - it("has import declarations separated by new line in script") { + @Test + fun `has import declarations separated by new line in script`() { val code = "import a.b\n\nimport a.c\n\nprint(1)" val ktsFile = compileContentForTest(code, "Test.kts") assertThat(subject.lint(ktsFile)).isEmpty() } } - it("has code on new line") { + @Test + fun `has code on new line`() { val code = "package test\nimport a.b\nclass A {}" assertThat(subject.lint(code)).hasSize(2) } - it("has code with spaces") { + @Test + fun `has code with spaces`() { val code = "package test; import a.b; class A {}" assertThat(subject.lint(code)).hasSize(2) } - it("has too many blank lines") { + @Test + fun `has too many blank lines`() { val code = "package test\n\n\nimport a.b\n\n\nclass A {}" assertThat(subject.lint(code)).hasSize(2) } - it("has package declarations in same line") { + @Test + fun `has package declarations in same line`() { val code = "package test;import a.b;class A {}" assertThat(subject.lint(code)).hasSize(2) } - it("has multiple imports in file") { + @Test + fun `has multiple imports in file`() { val code = """ package com.my @@ -100,7 +117,8 @@ class SpacingBetweenPackageAndImportsSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("has no class") { + @Test + fun `has no class`() { val code = """ package com.my.has.no.clazz @@ -110,4 +128,4 @@ class SpacingBetweenPackageAndImportsSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ThrowsCountSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ThrowsCountSpec.kt index e49b6f906b8e..08cc3681dbed 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ThrowsCountSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ThrowsCountSpec.kt @@ -4,37 +4,43 @@ import io.gitlab.arturbosch.detekt.api.Config 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 +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test private const val MAX = "max" private const val EXCLUDE_GUARD_CLAUSES = "excludeGuardClauses" -class ThrowsCountSpec : Spek({ +class ThrowsCountSpec { - describe("ThrowsCount rule") { + @Nested + inner class `ThrowsCount rule` { - context("a function with an empty body") { + @Nested + inner class `a function with an empty body` { val code = """ fun func() {} """ - it("does not report violation by default") { + @Test + fun `does not report violation by default`() { assertThat(ThrowsCount(Config.empty).lint(code)).isEmpty() } } - context("a function without a body") { + @Nested + inner class `a function without a body` { val code = """ fun func() = Unit """ - it("does not report violation by default") { + @Test + fun `does not report violation by default`() { assertThat(ThrowsCount(Config.empty).lint(code)).isEmpty() } } - context("code with 2 throw expressions") { + @Nested + inner class `code with 2 throw expressions` { val code = """ fun f2(x: Int) { when (x) { @@ -43,14 +49,16 @@ class ThrowsCountSpec : Spek({ } } """ - val subject by memoized { ThrowsCount(Config.empty) } + val subject = ThrowsCount(Config.empty) - it("does not report violation") { + @Test + fun `does not report violation`() { assertThat(subject.lint(code)).isEmpty() } } - context("code with 3 throw expressions") { + @Nested + inner class `code with 3 throw expressions` { val code = """ fun f1(x: Int) { when (x) { @@ -60,14 +68,16 @@ class ThrowsCountSpec : Spek({ } } """ - val subject by memoized { ThrowsCount(Config.empty) } + val subject = ThrowsCount(Config.empty) - it("reports violation by default") { + @Test + fun `reports violation by default`() { assertThat(subject.lint(code)).hasSize(1) } } - context("code with an override function with 3 throw expressions") { + @Nested + inner class `code with an override function with 3 throw expressions` { val code = """ override fun f3(x: Int) { // does not report overridden function when (x) { @@ -77,14 +87,16 @@ class ThrowsCountSpec : Spek({ } } """ - val subject by memoized { ThrowsCount(Config.empty) } + val subject = ThrowsCount(Config.empty) - it("reports violation by default") { + @Test + fun `reports violation by default`() { assertThat(subject.lint(code)).isEmpty() } } - context("code with a nested function with 3 throw expressions") { + @Nested + inner class `code with a nested function with 3 throw expressions` { val code = """ import java.io.IOException @@ -99,16 +111,18 @@ class ThrowsCountSpec : Spek({ return bar(x) } """ - val subject by memoized { ThrowsCount(Config.empty) } + val subject = ThrowsCount(Config.empty) - it("reports violation by default") { + @Test + fun `reports violation by default`() { val findings = subject.lint(code) assertThat(findings).hasSize(1) assertThat(findings[0].entity.location.source.line).isEqualTo(4) } } - context("max count == 3") { + @Nested + inner class `max count == 3` { val code = """ fun f4(x: String?) { val denulled = x ?: throw IOException() @@ -117,20 +131,23 @@ class ThrowsCountSpec : Spek({ } """ - it("does not report when max parameter is 3") { + @Test + fun `does not report when max parameter is 3`() { val config = TestConfig(mapOf(MAX to "3")) val subject = ThrowsCount(config) assertThat(subject.lint(code)).isEmpty() } - it("reports violation when max parameter is 2") { + @Test + fun `reports violation when max parameter is 2`() { val config = TestConfig(mapOf(MAX to "2")) val subject = ThrowsCount(config) assertThat(subject.lint(code)).hasSize(1) } } - context("code with ELVIS operator guard clause") { + @Nested + inner class `code with ELVIS operator guard clause` { val codeWithGuardClause = """ fun test(x: Int): Int { val y = x ?: throw Exception() @@ -142,20 +159,23 @@ class ThrowsCountSpec : Spek({ } """ - it("should not report violation with EXCLUDE_GUARD_CLAUSES as true") { + @Test + fun `should not report violation with EXCLUDE_GUARD_CLAUSES as true`() { val config = TestConfig(mapOf(EXCLUDE_GUARD_CLAUSES to "true")) val subject = ThrowsCount(config) assertThat(subject.lint(codeWithGuardClause)).isEmpty() } - it("should report violation with EXCLUDE_GUARD_CLAUSES as false") { + @Test + fun `should report violation with EXCLUDE_GUARD_CLAUSES as false`() { val config = TestConfig(mapOf(EXCLUDE_GUARD_CLAUSES to "false")) val subject = ThrowsCount(config) assertThat(subject.lint(codeWithGuardClause)).hasSize(1) } } - context("code with if condition guard clause") { + @Nested + inner class `code with if condition guard clause` { val codeWithGuardClause = """ fun test(x: Int): Int { if(x == null) throw Exception() @@ -167,20 +187,23 @@ class ThrowsCountSpec : Spek({ } """ - it("should not report violation with EXCLUDE_GUARD_CLAUSES as true") { + @Test + fun `should not report violation with EXCLUDE_GUARD_CLAUSES as true`() { val config = TestConfig(mapOf(EXCLUDE_GUARD_CLAUSES to "true")) val subject = ThrowsCount(config) assertThat(subject.lint(codeWithGuardClause)).isEmpty() } - it("should report violation with EXCLUDE_GUARD_CLAUSES as false") { + @Test + fun `should report violation with EXCLUDE_GUARD_CLAUSES as false`() { val config = TestConfig(mapOf(EXCLUDE_GUARD_CLAUSES to "false")) val subject = ThrowsCount(config) assertThat(subject.lint(codeWithGuardClause)).hasSize(1) } } - context("reports a too-complicated if statement for being a guard clause") { + @Nested + inner class `reports a too-complicated if statement for being a guard clause` { val codeWithIfCondition = """ fun test(x: Int): Int { if (x < 4) { @@ -199,14 +222,16 @@ class ThrowsCountSpec : Spek({ } """ - it("should report violation even with EXCLUDE_GUARD_CLAUSES as true") { + @Test + fun `should report violation even with EXCLUDE_GUARD_CLAUSES as true`() { val config = TestConfig(mapOf(EXCLUDE_GUARD_CLAUSES to "true")) val subject = ThrowsCount(config) assertThat(subject.lint(codeWithIfCondition)).hasSize(1) } } - context("a file with 2 returns and an if condition guard clause which is not the first statement") { + @Nested + inner class `a file with 2 returns and an if condition guard clause which is not the first statement` { val codeWithIfCondition = """ fun test(x: Int): Int { when (x) { @@ -218,13 +243,16 @@ class ThrowsCountSpec : Spek({ } """ - it("should report the violation even with EXCLUDE_GUARD_CLAUSES as true") { + @Test + fun `should report the violation even with EXCLUDE_GUARD_CLAUSES as true`() { val config = TestConfig(mapOf(EXCLUDE_GUARD_CLAUSES to "true")) val subject = ThrowsCount(config) assertThat(subject.lint(codeWithIfCondition)).hasSize(1) } } - context("a file with 2 returns and an ELVIS guard clause which is not the first statement") { + + @Nested + inner class `a file with 2 returns and an ELVIS guard clause which is not the first statement` { val codeWithIfCondition = """ fun test(x: Int): Int { when (x) { @@ -236,14 +264,16 @@ class ThrowsCountSpec : Spek({ } """ - it("should report the violation even with EXCLUDE_GUARD_CLAUSES as true") { + @Test + fun `should report the violation even with EXCLUDE_GUARD_CLAUSES as true`() { val config = TestConfig(mapOf(EXCLUDE_GUARD_CLAUSES to "true")) val subject = ThrowsCount(config) assertThat(subject.lint(codeWithIfCondition)).hasSize(1) } } - context("a file with multiple guard clauses") { + @Nested + inner class `a file with multiple guard clauses` { val codeWithMultipleGuardClauses = """ fun multipleGuards(a: Int?, b: Any?, c: Int?) { if(a == null) throw Exception() @@ -258,17 +288,19 @@ class ThrowsCountSpec : Spek({ } """ - it("should not report violation with EXCLUDE_GUARD_CLAUSES as true") { + @Test + fun `should not report violation with EXCLUDE_GUARD_CLAUSES as true`() { val config = TestConfig(mapOf(EXCLUDE_GUARD_CLAUSES to "true")) val subject = ThrowsCount(config) assertThat(subject.lint(codeWithMultipleGuardClauses)).isEmpty() } - it("should report violation with EXCLUDE_GUARD_CLAUSES as false") { + @Test + fun `should report violation with EXCLUDE_GUARD_CLAUSES as false`() { val config = TestConfig(mapOf(EXCLUDE_GUARD_CLAUSES to "false")) val subject = ThrowsCount(config) assertThat(subject.lint(codeWithMultipleGuardClauses)).hasSize(1) } } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/TrailingWhitespaceSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/TrailingWhitespaceSpec.kt index 915103c3c479..b711567a5985 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/TrailingWhitespaceSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/TrailingWhitespaceSpec.kt @@ -2,41 +2,51 @@ package io.gitlab.arturbosch.detekt.rules.style import io.github.detekt.test.utils.compileContentForTest import io.gitlab.arturbosch.detekt.test.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 TrailingWhitespaceSpec : Spek({ +class TrailingWhitespaceSpec { - val rule by memoized { TrailingWhitespace() } + @Nested + inner class `TrailingWhitespace rule` { - describe("TrailingWhitespace rule") { + @Nested + inner class `positive cases` { - context("positive cases") { - - it("reports a line just with a whitespace") { + @Test + fun `reports a line just with a whitespace`() { + val rule = TrailingWhitespace() rule.visit(" ".toKtFileContent()) assertThat(rule.findings).hasTextLocations(0 to 1) } - it("reports a commented line with a whitespace at the end") { + @Test + fun `reports a commented line with a whitespace at the end`() { + val rule = TrailingWhitespace() rule.visit("// A comment ".toKtFileContent()) assertThat(rule.findings).hasTextLocations(12 to 13) } - it("reports a class declaration with a whitespace at the end") { + @Test + fun `reports a class declaration with a whitespace at the end`() { + val rule = TrailingWhitespace() rule.visit(" class TrailingWhitespacePositive { \n }".toKtFileContent()) assertThat(rule.findings).hasTextLocations(36 to 37) } - it("reports a print statement with a tab at the end") { + @Test + fun `reports a print statement with a tab at the end`() { + val rule = TrailingWhitespace() rule.visit("\t\tprintln(\"A message\")\t".toKtFileContent()) assertThat(rule.findings).hasTextLocations(22 to 23) } } - context("negative cases") { + @Nested + inner class `negative cases` { - it("does not report a class and function declaration with no whitespaces at the end") { + @Test + fun `does not report a class and function declaration with no whitespaces at the end`() { val code = """ class C { @@ -46,11 +56,13 @@ class TrailingWhitespaceSpec : Spek({ } } """.trimIndent() + val rule = TrailingWhitespace() rule.visit(code.toKtFileContent()) assertThat(rule.findings).isEmpty() } - it("does not report an indentation inside multi-line strings") { + @Test + fun `does not report an indentation inside multi-line strings`() { val code = """ val multiLineStringWithIndents = ""${'"'} Should ignore indent on the next line @@ -58,12 +70,13 @@ class TrailingWhitespaceSpec : Spek({ Should ignore indent on the previous line ""${'"'} """.trim() + val rule = TrailingWhitespace() rule.visit(code.toKtFileContent()) assertThat(rule.findings).isEmpty() } } } -}) +} private fun String.toKtFileContent(): KtFileContent { val file = compileContentForTest(this) diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnderscoresInNumericLiteralsSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnderscoresInNumericLiteralsSpec.kt index 0034c2d7a4e8..912f2bfdb209 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnderscoresInNumericLiteralsSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnderscoresInNumericLiteralsSpec.kt @@ -4,31 +4,36 @@ import io.gitlab.arturbosch.detekt.test.TestConfig import io.gitlab.arturbosch.detekt.test.assertThat import io.gitlab.arturbosch.detekt.test.compileAndLint import io.gitlab.arturbosch.detekt.test.lint -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 private const val ACCEPTABLE_DECIMAL_LENGTH = "acceptableDecimalLength" private const val ACCEPTABLE_LENGTH = "acceptableLength" private const val ALLOW_NON_STANDARD_GROUPING = "allowNonStandardGrouping" -class UnderscoresInNumericLiteralsSpec : Spek({ +class UnderscoresInNumericLiteralsSpec { - describe("an Int of 1000") { + @Nested + inner class `an Int of 1000` { val code = "val myInt = 1000" - it("should not be reported by default") { + @Test + fun `should not be reported by default`() { val findings = UnderscoresInNumericLiterals().compileAndLint(code) assertThat(findings).isEmpty() } - it("should be reported if acceptableLength is 3") { + @Test + fun `should be reported if acceptableLength is 3`() { val findings = UnderscoresInNumericLiterals( TestConfig(mapOf(ACCEPTABLE_LENGTH to "3")) ).compileAndLint(code) assertThat(findings).isNotEmpty } - it("should be reported if deprecated acceptableDecimalLength is 4") { + @Test + fun `should be reported if deprecated acceptableDecimalLength is 4`() { val findings = UnderscoresInNumericLiterals( TestConfig(mapOf(ACCEPTABLE_DECIMAL_LENGTH to "4")) ).compileAndLint(code) @@ -36,24 +41,29 @@ class UnderscoresInNumericLiteralsSpec : Spek({ } } - describe("an Int of 1_000_000") { + @Nested + inner class `an Int of 1_000_000` { val code = "val myInt = 1_000_000" - it("should not be reported") { + @Test + fun `should not be reported`() { val findings = UnderscoresInNumericLiterals().compileAndLint(code) assertThat(findings).isEmpty() } } - describe("a const Int of 1000000") { + @Nested + inner class `a const Int of 1000000` { val code = "val myInt = 1000000" - it("should be reported by default") { + @Test + fun `should be reported by default`() { val findings = UnderscoresInNumericLiterals().compileAndLint(code) assertThat(findings).isNotEmpty } - it("should not be reported if acceptableLength is 7") { + @Test + fun `should not be reported if acceptableLength is 7`() { val findings = UnderscoresInNumericLiterals( TestConfig(mapOf(ACCEPTABLE_LENGTH to "7")) ).compileAndLint(code) @@ -61,15 +71,18 @@ class UnderscoresInNumericLiteralsSpec : Spek({ } } - describe("a Float of 1000f") { + @Nested + inner class `a Float of 1000f` { val code = "val myFloat = 1000f" - it("should not be reported by default") { + @Test + fun `should not be reported by default`() { val findings = UnderscoresInNumericLiterals().compileAndLint(code) assertThat(findings).isEmpty() } - it("should be reported if acceptableLength is 3") { + @Test + fun `should be reported if acceptableLength is 3`() { val findings = UnderscoresInNumericLiterals( TestConfig(mapOf(ACCEPTABLE_LENGTH to "3")) ).compileAndLint(code) @@ -77,15 +90,18 @@ class UnderscoresInNumericLiteralsSpec : Spek({ } } - describe("a Float of -1000f") { + @Nested + inner class `a Float of -1000f` { val code = "val myFloat = -1000f" - it("should not be reported by default") { + @Test + fun `should not be reported by default`() { val findings = UnderscoresInNumericLiterals().compileAndLint(code) assertThat(findings).isEmpty() } - it("should be reported if acceptableLength is 3") { + @Test + fun `should be reported if acceptableLength is 3`() { val findings = UnderscoresInNumericLiterals( TestConfig(mapOf(ACCEPTABLE_LENGTH to "3")) ).compileAndLint(code) @@ -93,24 +109,29 @@ class UnderscoresInNumericLiteralsSpec : Spek({ } } - describe("a Float of -1_000f") { + @Nested + inner class `a Float of -1_000f` { val code = "val myFloat = -1_000f" - it("should not be reported") { + @Test + fun `should not be reported`() { val findings = UnderscoresInNumericLiterals().compileAndLint(code) assertThat(findings).isEmpty() } } - describe("a Long of 1000000L") { + @Nested + inner class `a Long of 1000000L` { val code = "val myLong = 1000000L" - it("should be reported by default") { + @Test + fun `should be reported by default`() { val findings = UnderscoresInNumericLiterals().compileAndLint(code) assertThat(findings).isNotEmpty } - it("should not be reported if ignored acceptableLength is 7") { + @Test + fun `should not be reported if ignored acceptableLength is 7`() { val findings = UnderscoresInNumericLiterals( TestConfig(mapOf(ACCEPTABLE_LENGTH to "7")) ).compileAndLint(code) @@ -118,24 +139,30 @@ class UnderscoresInNumericLiteralsSpec : Spek({ } } - describe("a Double of 1_000_000.00_000_000") { + @Nested + @DisplayName("a Double of 1_000_000.00_000_000") + inner class DoubleWithDecimals { val code = "val myDouble = 1_000_000.00_000_000" - it("should not be reported") { + @Test + fun `should not be reported`() { val findings = UnderscoresInNumericLiterals().compileAndLint(code) assertThat(findings).isEmpty() } } - describe("a function with default Int parameter value 1000") { + @Nested + inner class `a function with default Int parameter value 1000` { val code = "fun testFunction(testParam: Int = 1000) {}" - it("should not be reported by default") { + @Test + fun `should not be reported by default`() { val findings = UnderscoresInNumericLiterals().compileAndLint(code) assertThat(findings).isEmpty() } - it("should be reported if acceptableLength is 3") { + @Test + fun `should be reported if acceptableLength is 3`() { val findings = UnderscoresInNumericLiterals( TestConfig(mapOf(ACCEPTABLE_LENGTH to "3")) ).compileAndLint(code) @@ -143,40 +170,48 @@ class UnderscoresInNumericLiteralsSpec : Spek({ } } - describe("an annotation with numeric literals 0 and 10") { + @Nested + inner class `an annotation with numeric literals 0 and 10` { val code = "fun setCustomDimension(@IntRange(from = 0, to = 10) index: Int, value: String?) {}" - it("should not be reported") { + @Test + fun `should not be reported`() { val findings = UnderscoresInNumericLiterals().lint(code) assertThat(findings).isEmpty() } } - describe("an annotation with numeric literals 0 and 1000000") { + @Nested + inner class `an annotation with numeric literals 0 and 1000000` { val code = "fun setCustomDimension(@IntRange(from = 0, to = 1000000) index: Int, value: String?) {}" - it("should be reported by default") { + @Test + fun `should be reported by default`() { val findings = UnderscoresInNumericLiterals().lint(code) assertThat(findings).isNotEmpty } } - describe("an Int of 1000_00_00") { + @Nested + inner class `an Int of 1000_00_00` { val code = "val myInt = 1000_00_00" - it("should be reported by default") { + @Test + fun `should be reported by default`() { val findings = UnderscoresInNumericLiterals().compileAndLint(code) assertThat(findings).isNotEmpty } - it("should still be reported even if acceptableLength is 99") { + @Test + fun `should still be reported even if acceptableLength is 99`() { val findings = UnderscoresInNumericLiterals( TestConfig(mapOf(ACCEPTABLE_LENGTH to "99")) ).compileAndLint(code) assertThat(findings).isNotEmpty } - it("should not be reported if allowNonStandardGrouping is true") { + @Test + fun `should not be reported if allowNonStandardGrouping is true`() { val findings = UnderscoresInNumericLiterals( TestConfig(mapOf(ALLOW_NON_STANDARD_GROUPING to true)) ).compileAndLint(code) @@ -184,34 +219,41 @@ class UnderscoresInNumericLiteralsSpec : Spek({ } } - describe("a binary Int of 0b1011") { + @Nested + inner class `a binary Int of 0b1011` { val code = "val myBinInt = 0b1011" - it("should not be reported") { + @Test + fun `should not be reported`() { val findings = UnderscoresInNumericLiterals().compileAndLint(code) assertThat(findings).isEmpty() } } - describe("a hexadecimal Int of 0x1facdf") { + @Nested + inner class `a hexadecimal Int of 0x1facdf` { val code = "val myHexInt = 0x1facdf" - it("should not be reported") { + @Test + fun `should not be reported`() { val findings = UnderscoresInNumericLiterals().compileAndLint(code) assertThat(findings).isEmpty() } } - describe("a hexadecimal Int of 0xFFFFFF") { + @Nested + inner class `a hexadecimal Int of 0xFFFFFF` { val code = "val myHexInt = 0xFFFFFF" - it("should not be reported") { + @Test + fun `should not be reported`() { val findings = UnderscoresInNumericLiterals().compileAndLint(code) assertThat(findings).isEmpty() } } - describe("a property named serialVersionUID in an object that implements Serializable") { + @Nested + inner class `a property named serialVersionUID in an object that implements Serializable` { val code = """ import java.io.Serializable @@ -220,41 +262,48 @@ class UnderscoresInNumericLiteralsSpec : Spek({ } """ - it("should not be reported") { + @Test + fun `should not be reported`() { val findings = UnderscoresInNumericLiterals().compileAndLint(code) assertThat(findings).isEmpty() } } - describe("a property named serialVersionUID in an object that does not implement Serializable") { + @Nested + inner class `a long property named serialVersionUID in an object that does not implement Serializable` { val code = """ object TestSerializable { private val serialVersionUID = 314159L } """ - it("should be reported by default") { + @Test + fun `should be reported by default`() { val findings = UnderscoresInNumericLiterals().compileAndLint(code) assertThat(findings).isNotEmpty } } - describe("a property named serialVersionUID in an object that does not implement Serializable") { + @Nested + inner class `a long property with underscores named serialVersionUID in an object that does not implement Serializable` { val code = """ object TestSerializable { private val serialVersionUID = 314_159L } """ - it("should not be reported") { + @Test + fun `should not be reported`() { val findings = UnderscoresInNumericLiterals().compileAndLint(code) assertThat(findings).isEmpty() } } - describe("a property named serialVersionUID in a companion object inside a serializable class") { + @Nested + inner class `a property named serialVersionUID in a companion object inside a serializable class` { - it("does not report a negative serialVersionUID number") { + @Test + fun `does not report a negative serialVersionUID number`() { val code = """ import java.io.Serializable @@ -268,7 +317,8 @@ class UnderscoresInNumericLiteralsSpec : Spek({ assertThat(findings).hasSize(0) } - it("does not report a positive serialVersionUID number") { + @Test + fun `does not report a positive serialVersionUID number`() { val code = """ import java.io.Serializable @@ -283,9 +333,11 @@ class UnderscoresInNumericLiteralsSpec : Spek({ } } - describe("a property named serialVersionUID number in a serializable class") { + @Nested + inner class `a property named serialVersionUID number in a serializable class` { - it("does not report a negative serialVersionUID number") { + @Test + fun `does not report a negative serialVersionUID number`() { val code = """ import java.io.Serializable @@ -297,7 +349,8 @@ class UnderscoresInNumericLiteralsSpec : Spek({ assertThat(findings).hasSize(0) } - it("does not report a positive serialVersionUID number") { + @Test + fun `does not report a positive serialVersionUID number`() { val code = """ import java.io.Serializable @@ -310,15 +363,18 @@ class UnderscoresInNumericLiteralsSpec : Spek({ } } - describe("an Int of 10000") { + @Nested + inner class `an Int of 10000` { val code = "val myInt = 10000" - it("should be reported by default") { + @Test + fun `should be reported by default`() { val findings = UnderscoresInNumericLiterals().compileAndLint(code) assertThat(findings).isNotEmpty } - it("should not be reported if acceptableLength is 5") { + @Test + fun `should not be reported if acceptableLength is 5`() { val findings = UnderscoresInNumericLiterals( TestConfig(mapOf(ACCEPTABLE_LENGTH to "5")) ).compileAndLint(code) @@ -326,28 +382,37 @@ class UnderscoresInNumericLiteralsSpec : Spek({ } } - describe("a Float of 30_000_000.1415926535897932385") { + @Nested + @DisplayName("a Float of 30_000_000.1415926535897932385") + inner class FloatWithDecimals { val code = "val myFloat = 30_000_000.1415926535897932385f" - it("should not be reported") { + @Test + fun `should not be reported`() { val findings = UnderscoresInNumericLiterals().compileAndLint(code) assertThat(findings).isEmpty() } } - describe("a Double of 3.1415926535897932385") { + @Nested + @DisplayName("a Double of 3.1415926535897932385") + inner class Pi { val code = "val myDouble = 3.1415926535897932385" - it("should not be reported") { + @Test + fun `should not be reported`() { val findings = UnderscoresInNumericLiterals().compileAndLint(code) assertThat(findings).isEmpty() } } - describe("a Double of 3000.1415926535897932385") { + @Nested + @DisplayName("a Double of 3000.1415926535897932385") + inner class Double { val code = "val myDouble = 3000.1415926535897932385" - it("should be reported if acceptableLength is 3") { + @Test + fun `should be reported if acceptableLength is 3`() { val findings = UnderscoresInNumericLiterals( TestConfig(mapOf(ACCEPTABLE_LENGTH to "3")) ).compileAndLint(code) @@ -355,15 +420,19 @@ class UnderscoresInNumericLiteralsSpec : Spek({ } } - describe("a Float of 1000000.31415926535f") { + @Nested + @DisplayName("a Float of 1000000.31415926535f") + inner class Float { val code = "val myFloat = 1000000.31415926535f" - it("should be reported by default") { + @Test + fun `should be reported by default`() { val findings = UnderscoresInNumericLiterals().compileAndLint(code) assertThat(findings).isNotEmpty } - it("should not be reported if acceptableLength is 7") { + @Test + fun `should not be reported if acceptableLength is 7`() { val findings = UnderscoresInNumericLiterals( TestConfig(mapOf(ACCEPTABLE_LENGTH to "7")) ).compileAndLint(code) @@ -371,12 +440,15 @@ class UnderscoresInNumericLiteralsSpec : Spek({ } } - describe("a String of 1000000.3141592") { + @Nested + @DisplayName("a String of 1000000.3141592") + inner class String { val code = """val myString = "1000000.3141592"""" - it("should not be reported by default") { + @Test + fun `should not be reported by default`() { val findings = UnderscoresInNumericLiterals().compileAndLint(code) assertThat(findings).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryAbstractClassSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryAbstractClassSpec.kt index df2f3ea884b2..8cb567ee33b8 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryAbstractClassSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryAbstractClassSpec.kt @@ -1,30 +1,30 @@ package io.gitlab.arturbosch.detekt.rules.style import io.gitlab.arturbosch.detekt.api.Finding -import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.TestConfig import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext import org.assertj.core.api.Assertions.assertThat import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -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 EXCLUDE_ANNOTATED_CLASSES = "excludeAnnotatedClasses" -class UnnecessaryAbstractClassSpec : Spek({ - setupKotlinEnvironment() - - val env: KotlinCoreEnvironment by memoized() - val subject by memoized { +@KotlinCoreEnvironmentTest +class UnnecessaryAbstractClassSpec(val env: KotlinCoreEnvironment) { + val subject = UnnecessaryAbstractClass(TestConfig(mapOf(EXCLUDE_ANNOTATED_CLASSES to listOf("Deprecated")))) - } - describe("UnnecessaryAbstractClass rule") { + @Nested + inner class `UnnecessaryAbstractClass rule` { - context("abstract classes with no concrete members") { + @Nested + inner class `abstract classes with no concrete members` { val message = "An abstract class without a concrete member can be refactored to an interface." - it("reports an abstract class with no concrete member") { + @Test + fun `reports an abstract class with no concrete member`() { val code = """ abstract class A { abstract val i: Int @@ -36,32 +36,38 @@ class UnnecessaryAbstractClassSpec : Spek({ assertFindingMessage(findings, message) } - context("reports completely-empty abstract classes") { - it("case 1") { + @Nested + inner class `reports completely-empty abstract classes` { + @Test + fun `case 1`() { val code = "abstract class A" val findings = subject.compileAndLintWithContext(env, code) assertFindingMessage(findings, message) } - it("case 2") { + @Test + fun `case 2`() { val code = "abstract class A()" val findings = subject.compileAndLintWithContext(env, code) assertFindingMessage(findings, message) } - it("case 3") { + @Test + fun `case 3`() { val code = "abstract class A {}" val findings = subject.compileAndLintWithContext(env, code) assertFindingMessage(findings, message) } - it("case 4") { + @Test + fun `case 4`() { val code = "abstract class A() {}" val findings = subject.compileAndLintWithContext(env, code) assertFindingMessage(findings, message) } - it("that inherits from an interface") { + @Test + fun `that inherits from an interface`() { val code = """ interface A { val i: Int @@ -72,7 +78,8 @@ class UnnecessaryAbstractClassSpec : Spek({ assertFindingMessage(findings, message) } - it("that inherits from another abstract class") { + @Test + fun `that inherits from another abstract class`() { val code = """ @Deprecated("We don't care about this first class") abstract class A { @@ -85,7 +92,8 @@ class UnnecessaryAbstractClassSpec : Spek({ } } - it("does not report an abstract class with concrete members derived from a base class") { + @Test + fun `does not report an abstract class with concrete members derived from a base class`() { val code = """ abstract class A { abstract fun f() @@ -99,7 +107,8 @@ class UnnecessaryAbstractClassSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report an abstract class with a internal abstract member") { + @Test + fun `does not report an abstract class with a internal abstract member`() { val code = """ abstract class A { internal abstract fun f() @@ -107,7 +116,9 @@ class UnnecessaryAbstractClassSpec : Spek({ """ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report an abstract class with a protected abstract member") { + + @Test + fun `does not report an abstract class with a protected abstract member`() { val code = """ abstract class A { protected abstract fun f() @@ -117,11 +128,13 @@ class UnnecessaryAbstractClassSpec : Spek({ } } - context("abstract classes with no abstract members") { + @Nested + inner class `abstract classes with no abstract members` { val message = "An abstract class without an abstract member can be refactored to a concrete class." - it("reports no abstract members in abstract class") { + @Test + fun `reports no abstract members in abstract class`() { val code = """ abstract class A { val i: Int = 0 @@ -132,7 +145,8 @@ class UnnecessaryAbstractClassSpec : Spek({ assertFindingMessage(findings, message) } - it("reports no abstract members in nested abstract class inside a concrete class") { + @Test + fun `reports no abstract members in nested abstract class inside a concrete class`() { val code = """ class Outer { abstract class Inner { @@ -144,7 +158,8 @@ class UnnecessaryAbstractClassSpec : Spek({ assertFindingMessage(findings, message) } - it("reports no abstract members in nested abstract class inside an interface") { + @Test + fun `reports no abstract members in nested abstract class inside an interface`() { val code = """ interface Inner { abstract class A { @@ -156,19 +171,22 @@ class UnnecessaryAbstractClassSpec : Spek({ assertFindingMessage(findings, message) } - it("reports no abstract members in an abstract class with just a constructor") { + @Test + fun `reports no abstract members in an abstract class with just a constructor`() { val code = "abstract class A(val i: Int)" val findings = subject.compileAndLintWithContext(env, code) assertFindingMessage(findings, message) } - it("reports no abstract members in an abstract class with a body and a constructor") { + @Test + fun `reports no abstract members in an abstract class with a body and a constructor`() { val code = "abstract class A(val i: Int) {}" val findings = subject.compileAndLintWithContext(env, code) assertFindingMessage(findings, message) } - it("reports an abstract class with no abstract member derived from a class with abstract members") { + @Test + fun `reports an abstract class with no abstract member derived from a class with abstract members`() { val code = """ abstract class Base { abstract val i: Int @@ -190,9 +208,11 @@ class UnnecessaryAbstractClassSpec : Spek({ } } - context("abstract classes with members") { + @Nested + inner class `abstract classes with members` { - it("does not report an abstract class with members and an abstract class derived from it") { + @Test + fun `does not report an abstract class with members and an abstract class derived from it`() { val code = """ abstract class A { abstract val i: Int @@ -206,7 +226,8 @@ class UnnecessaryAbstractClassSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report an abstract class with a constructor and an abstract class derived from it") { + @Test + fun `does not report an abstract class with a constructor and an abstract class derived from it`() { val code = """ abstract class A(val i: Int) { abstract fun f() @@ -219,7 +240,8 @@ class UnnecessaryAbstractClassSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report an abstract class with a function derived from an interface") { + @Test + fun `does not report an abstract class with a function derived from an interface`() { val code = """ abstract class A : Interface { fun g() {} @@ -232,7 +254,8 @@ class UnnecessaryAbstractClassSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report abstract classes with module annotation") { + @Test + fun `does not report abstract classes with module annotation`() { val code = """ @Deprecated("test") abstract class A { @@ -247,7 +270,8 @@ class UnnecessaryAbstractClassSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report abstract classes with properties in the primary constructor") { + @Test + fun `does not report abstract classes with properties in the primary constructor`() { val code = """ interface I { fun test(): Int @@ -258,7 +282,7 @@ class UnnecessaryAbstractClassSpec : Spek({ } } } -}) +} private fun assertFindingMessage(findings: List, message: String) { assertThat(findings).hasSize(1) diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryAnnotationUseSiteTargetSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryAnnotationUseSiteTargetSpec.kt index 090f6a1c2bed..7edf5277801a 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryAnnotationUseSiteTargetSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryAnnotationUseSiteTargetSpec.kt @@ -2,14 +2,18 @@ package io.gitlab.arturbosch.detekt.rules.style import io.gitlab.arturbosch.detekt.test.assertThat import io.gitlab.arturbosch.detekt.test.compileAndLint -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 -class UnnecessaryAnnotationUseSiteTargetSpec : Spek({ +class UnnecessaryAnnotationUseSiteTargetSpec { - describe("UnnecessaryAnnotationUseSiteTarget rule") { + @Nested + inner class `UnnecessaryAnnotationUseSiteTarget rule` { - it("Unnecessary @param: in a property constructor") { + @Test + @DisplayName("Unnecessary @param: in a property constructor") + fun unnecessaryParamInPropertyConstructor() { val code = """ class C(@param:Asdf private val foo: String) @@ -18,7 +22,9 @@ class UnnecessaryAnnotationUseSiteTargetSpec : Spek({ assertThat(UnnecessaryAnnotationUseSiteTarget().compileAndLint(code)).hasTextLocations("param:") } - it("Unnecessary @param: in a constructor") { + @Test + @DisplayName("Unnecessary @param: in a constructor") + fun unnecessaryParamInConstructor() { val code = """ class C(@param:Asdf foo: String) @@ -27,7 +33,9 @@ class UnnecessaryAnnotationUseSiteTargetSpec : Spek({ assertThat(UnnecessaryAnnotationUseSiteTarget().compileAndLint(code)).hasTextLocations("param:") } - it("Necessary @get:") { + @Test + @DisplayName("Necessary @get:") + fun unnecessaryGet() { val code = """ class C(@get:Asdf private val foo: String) @@ -36,7 +44,9 @@ class UnnecessaryAnnotationUseSiteTargetSpec : Spek({ assertThat(UnnecessaryAnnotationUseSiteTarget().compileAndLint(code)).isEmpty() } - it("Necessary @property:") { + @Test + @DisplayName("Necessary @property:") + fun necessaryProperty() { val code = """ class C(@property:Asdf private val foo: String) @@ -45,7 +55,9 @@ class UnnecessaryAnnotationUseSiteTargetSpec : Spek({ assertThat(UnnecessaryAnnotationUseSiteTarget().compileAndLint(code)).isEmpty() } - it("Unnecessary @property:") { + @Test + @DisplayName("Unnecessary @property:") + fun unnecessaryProperty() { val code = """ class C { @property:Asdf private val foo: String = "bar" @@ -56,7 +68,9 @@ class UnnecessaryAnnotationUseSiteTargetSpec : Spek({ assertThat(UnnecessaryAnnotationUseSiteTarget().compileAndLint(code)).hasTextLocations("property:") } - it("Unnecessary @property: at a top level property") { + @Test + @DisplayName("Unnecessary @property: at a top level property") + fun unnecessaryPropertyAtTopLevel() { val code = """ @property:Asdf private val foo: String = "bar" @@ -65,4 +79,4 @@ class UnnecessaryAnnotationUseSiteTargetSpec : Spek({ assertThat(UnnecessaryAnnotationUseSiteTarget().compileAndLint(code)).hasTextLocations("property:") } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryApplySpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryApplySpec.kt index a73839ff25fb..55998041eff6 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryApplySpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryApplySpec.kt @@ -1,26 +1,27 @@ package io.gitlab.arturbosch.detekt.rules.style import io.gitlab.arturbosch.detekt.api.Config -import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.compileAndLint import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext import org.assertj.core.api.Assertions.assertThat import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -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 UnnecessaryApplySpec : Spek({ +@KotlinCoreEnvironmentTest +class UnnecessaryApplySpec(val env: KotlinCoreEnvironment) { - setupKotlinEnvironment() + val subject = UnnecessaryApply(Config.empty) - val subject by memoized { UnnecessaryApply(Config.empty) } - val env: KotlinCoreEnvironment by memoized() + @Nested + inner class `UnnecessaryApply rule` { - describe("UnnecessaryApply rule") { + @Nested + inner class `unnecessary apply expressions that can be changed to ordinary method call` { - context("unnecessary apply expressions that can be changed to ordinary method call") { - - it("reports an apply on non-nullable type") { + @Test + fun `reports an apply on non-nullable type`() { val findings = subject.compileAndLintWithContext( env, """ @@ -36,7 +37,8 @@ class UnnecessaryApplySpec : Spek({ assertThat(findings.first().message).isEqualTo("apply expression can be omitted") } - it("reports an apply on nullable type") { + @Test + fun `reports an apply on nullable type`() { val findings = subject.compileAndLintWithContext( env, """ @@ -54,7 +56,8 @@ class UnnecessaryApplySpec : Spek({ assertThat(findings.first().message).isEqualTo("apply can be replaced with let or an if") } - it("reports a false negative apply on nullable type - #1485") { + @Test + fun `reports a false negative apply on nullable type - #1485`() { assertThat( subject.compileAndLintWithContext( env, @@ -72,7 +75,8 @@ class UnnecessaryApplySpec : Spek({ ).hasSize(1) } - it("does not report an apply with lambda block") { + @Test + fun `does not report an apply with lambda block`() { assertThat( subject.compileAndLintWithContext( env, @@ -88,7 +92,8 @@ class UnnecessaryApplySpec : Spek({ ).isEmpty() } - it("does not report single statement in apply used as function argument") { + @Test + fun `does not report single statement in apply used as function argument`() { assertThat( subject.compileAndLintWithContext( env, @@ -106,7 +111,8 @@ class UnnecessaryApplySpec : Spek({ ).isEmpty() } - it("does not report single assignment statement in apply used as function argument - #1517") { + @Test + fun `does not report single assignment statement in apply used as function argument - #1517`() { assertThat( subject.compileAndLintWithContext( env, @@ -132,7 +138,8 @@ class UnnecessaryApplySpec : Spek({ ).isEmpty() } - it("does not report if result of apply is used - #2938") { + @Test + fun `does not report if result of apply is used - #2938`() { assertThat( subject.compileAndLint( """ @@ -145,7 +152,8 @@ class UnnecessaryApplySpec : Spek({ ).isEmpty() } - it("does not report applies with lambda body containing more than one statement") { + @Test + fun `does not report applies with lambda body containing more than one statement`() { assertThat( subject.compileAndLintWithContext( env, @@ -172,7 +180,8 @@ class UnnecessaryApplySpec : Spek({ ).isEmpty() } - it("reports when lambda has a dot qualified expression") { + @Test + fun `reports when lambda has a dot qualified expression`() { val findings = subject.compileAndLintWithContext( env, """ @@ -192,7 +201,8 @@ class UnnecessaryApplySpec : Spek({ assertThat(findings).hasSize(1) } - it("reports when lambda has a dot qualified expression which has 'this' receiver") { + @Test + fun `reports when lambda has a dot qualified expression which has 'this' receiver`() { val findings = subject.compileAndLintWithContext( env, """ @@ -212,7 +222,8 @@ class UnnecessaryApplySpec : Spek({ assertThat(findings).hasSize(1) } - it("reports when lambda has a 'this' expression") { + @Test + fun `reports when lambda has a 'this' expression`() { val findings = subject.compileAndLintWithContext( env, """ @@ -227,9 +238,11 @@ class UnnecessaryApplySpec : Spek({ } } - context("reported false positives - #1305") { + @Nested + inner class `reported false positives - #1305` { - it("is used within an assignment expr itself") { + @Test + fun `is used within an assignment expr itself`() { assertThat( subject.compileAndLintWithContext( env, @@ -244,7 +257,8 @@ class UnnecessaryApplySpec : Spek({ ).isEmpty() } - it("is used as return type of extension function") { + @Test + fun `is used as return type of extension function`() { assertThat( subject.compileAndLintWithContext( env, @@ -257,7 +271,8 @@ class UnnecessaryApplySpec : Spek({ ).isEmpty() } - it("should not flag apply when assigning property on this") { + @Test + fun `should not flag apply when assigning property on this`() { assertThat( subject.compileAndLintWithContext( env, @@ -272,7 +287,8 @@ class UnnecessaryApplySpec : Spek({ ).isEmpty() } - it("should not report apply when using it after returning something") { + @Test + fun `should not report apply when using it after returning something`() { assertThat( subject.compileAndLintWithContext( env, @@ -285,7 +301,8 @@ class UnnecessaryApplySpec : Spek({ ).isEmpty() } - it("should not report apply usage inside safe chained expressions") { + @Test + fun `should not report apply usage inside safe chained expressions`() { assertThat( subject.compileAndLintWithContext( env, @@ -302,9 +319,11 @@ class UnnecessaryApplySpec : Spek({ } } - context("false positive in single nesting expressions - #1473") { + @Nested + inner class `false positive in single nesting expressions - #1473` { - it("should not report the if expression") { + @Test + fun `should not report the if expression`() { assertThat( subject.compileAndLintWithContext( env, @@ -325,7 +344,8 @@ class UnnecessaryApplySpec : Spek({ ).isEmpty() } - it("should report reference expressions") { + @Test + fun `should report reference expressions`() { assertThat( subject.compileAndLintWithContext( env, @@ -349,9 +369,11 @@ class UnnecessaryApplySpec : Spek({ } } - context("false positive when it's used as an expression - #2435") { + @Nested + inner class `false positive when it's used as an expression - #2435` { - it("do not report when it's used as an assignment") { + @Test + fun `do not report when it's used as an assignment`() { assertThat( subject.compileAndLintWithContext( env, @@ -372,7 +394,8 @@ class UnnecessaryApplySpec : Spek({ ).isEmpty() } - it("do not report when it's used as the last statement of a block inside lambda") { + @Test + fun `do not report when it's used as the last statement of a block inside lambda`() { assertThat( subject.compileAndLintWithContext( env, @@ -397,9 +420,11 @@ class UnnecessaryApplySpec : Spek({ } } - context("false positive when lambda has multiple member references - #3561") { + @Nested + inner class `false positive when lambda has multiple member references - #3561` { - it("do not report when lambda has multiple member references") { + @Test + fun `do not report when lambda has multiple member references`() { assertThat( subject.compileAndLintWithContext( env, @@ -424,4 +449,4 @@ class UnnecessaryApplySpec : Spek({ } } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryFilterSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryFilterSpec.kt index ba4124f63d49..ffa282759630 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryFilterSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryFilterSpec.kt @@ -1,19 +1,21 @@ package io.gitlab.arturbosch.detekt.rules.style -import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.assertThat import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -import org.spekframework.spek2.Spek -import org.spekframework.spek2.style.specification.describe - -class UnnecessaryFilterSpec : Spek({ - setupKotlinEnvironment() - val env: KotlinCoreEnvironment by memoized() - val subject by memoized { UnnecessaryFilter() } - - describe("UnnecessaryFilter") { - it("Filter with size") { +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test + +@KotlinCoreEnvironmentTest +class UnnecessaryFilterSpec(val env: KotlinCoreEnvironment) { + val subject = UnnecessaryFilter() + + @Nested + inner class UnnecessaryFilterTest { + @Test + fun `Filter with size`() { val code = """ val x = listOf(1, 2, 3) .filter { it > 1 } @@ -25,7 +27,8 @@ class UnnecessaryFilterSpec : Spek({ assertThat(findings[0]).hasMessage("'filter { it > 1 }' can be replaced by 'size { it > 1 }'") } - it("Filter with count") { + @Test + fun `Filter with count`() { val code = """ val x = listOf(1, 2, 3) .filter { it > 1 } @@ -36,7 +39,8 @@ class UnnecessaryFilterSpec : Spek({ assertThat(findings).hasSize(1) } - it("Sequence with count") { + @Test + fun `Sequence with count`() { val code = """ val x = listOf(1, 2, 3) .asSequence() @@ -49,7 +53,8 @@ class UnnecessaryFilterSpec : Spek({ assertThat(findings).hasSize(1) } - it("None item") { + @Test + fun `None item`() { val code = """ val x = listOf(1, 2, 3) .filter { it > 2 } @@ -60,7 +65,8 @@ class UnnecessaryFilterSpec : Spek({ assertThat(findings).hasSize(1) } - it("Any item") { + @Test + fun `Any item`() { val code = """ val x = listOf(1, 2, 3) .filter { it > 2 } @@ -72,8 +78,10 @@ class UnnecessaryFilterSpec : Spek({ } } - describe("Correct filter") { - it("Not stdlib count list function") { + @Nested + inner class `Correct filter` { + @Test + fun `Not stdlib count list function`() { val code = """ fun List.count() : Any{ return Any() @@ -87,7 +95,8 @@ class UnnecessaryFilterSpec : Spek({ assertThat(findings).isEmpty() } - it("Not stdlib count sequences function") { + @Test + fun `Not stdlib count sequences function`() { val code = """ fun Sequence.count() : Any{ return Any() @@ -101,7 +110,8 @@ class UnnecessaryFilterSpec : Spek({ assertThat(findings).isEmpty() } - it("Not stdlib filter function") { + @Test + fun `Not stdlib filter function`() { val code = """ fun filter() : List{ return emptyList() @@ -114,7 +124,8 @@ class UnnecessaryFilterSpec : Spek({ assertThat(findings).isEmpty() } - it("Filter with count") { + @Test + fun `Filter with count`() { val code = """ val x = listOf(1, 2, 3) .count { it > 2 } @@ -124,7 +135,8 @@ class UnnecessaryFilterSpec : Spek({ assertThat(findings).isEmpty() } - it("None item") { + @Test + fun `None item`() { val code = """ val x = listOf(1, 2, 3) .none { it > 2 } @@ -134,7 +146,8 @@ class UnnecessaryFilterSpec : Spek({ assertThat(findings).isEmpty() } - it("Any item") { + @Test + fun `Any item`() { val code = """ val x = listOf(1, 2, 3) .any { it > 2 } @@ -144,7 +157,8 @@ class UnnecessaryFilterSpec : Spek({ assertThat(findings).isEmpty() } - it("Sequence with count") { + @Test + fun `Sequence with count`() { val code = """ val x = listOf(1, 2, 3) .asSequence() @@ -157,7 +171,8 @@ class UnnecessaryFilterSpec : Spek({ } // https://github.com/detekt/detekt/issues/3541#issuecomment-815136831 - it("Size in another statement") { + @Test + fun `Size in another statement`() { val code = """ fun foo() { val strings = listOf("abc", "cde", "ader") @@ -173,7 +188,9 @@ class UnnecessaryFilterSpec : Spek({ } // https://github.com/detekt/detekt/issues/3541 - it("Size/isEmpty()/isNotEmpty() in another statement") { + @Test + @DisplayName("Size/isEmpty()/isNotEmpty() in another statement") + fun filterUsedInOtherStatement() { val code = """ fun test(queryParts: List, a: List, b: List, c: List) { val dbQueryParts = queryParts.filter { it.length > 1 }.take(3) @@ -186,4 +203,4 @@ class UnnecessaryFilterSpec : Spek({ assertThat(findings).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryInheritanceSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryInheritanceSpec.kt index f59ac8d7ded1..9eb5d6200cc1 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryInheritanceSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryInheritanceSpec.kt @@ -3,15 +3,17 @@ package io.gitlab.arturbosch.detekt.rules.style import io.gitlab.arturbosch.detekt.api.Config 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 +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test -class UnnecessaryInheritanceSpec : Spek({ - val subject by memoized { UnnecessaryInheritance(Config.empty) } +class UnnecessaryInheritanceSpec { + val subject = UnnecessaryInheritance(Config.empty) - describe("check inherit classes") { + @Nested + inner class `check inherit classes` { - it("has unnecessary super type declarations") { + @Test + fun `has unnecessary super type declarations`() { val findings = subject.lint( """ class A : Any() @@ -21,9 +23,10 @@ class UnnecessaryInheritanceSpec : Spek({ assertThat(findings).hasSize(2) } - it("has no unnecessary super type declarations") { + @Test + fun `has no unnecessary super type declarations`() { val findings = subject.lint("class C : An()") assertThat(findings).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryInnerClassSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryInnerClassSpec.kt index 0a7a70c58807..a3623a4c77a0 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryInnerClassSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryInnerClassSpec.kt @@ -1,21 +1,21 @@ package io.gitlab.arturbosch.detekt.rules.style import io.gitlab.arturbosch.detekt.api.Config -import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.assertThat import io.gitlab.arturbosch.detekt.test.lintWithContext import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -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 UnnecessaryInnerClassSpec : Spek({ - setupKotlinEnvironment() +@KotlinCoreEnvironmentTest +class UnnecessaryInnerClassSpec(val env: KotlinCoreEnvironment) { + val subject = UnnecessaryInnerClass(Config.empty) - val env: KotlinCoreEnvironment by memoized() - val subject by memoized { UnnecessaryInnerClass(Config.empty) } - - describe("UnnecessaryInnerClass Rule") { - it("reports when an inner class does not access members of its outer class") { + @Nested + inner class `UnnecessaryInnerClass Rule` { + @Test + fun `reports when an inner class does not access members of its outer class`() { val code = """ val fileFoo = "FILE_FOO" @@ -40,8 +40,10 @@ class UnnecessaryInnerClassSpec : Spek({ assertThat(subject.lintWithContext(env, code)).hasSize(1) } - context("does not report an inner class accessing outer-class members") { - it("as a default argument for a constructor") { + @Nested + inner class `does not report an inner class accessing outer-class members` { + @Test + fun `as a default argument for a constructor`() { val code = """ class A { val foo = "BAR" @@ -53,7 +55,8 @@ class UnnecessaryInnerClassSpec : Spek({ assertThat(subject.lintWithContext(env, code)).isEmpty() } - it("as a property initializer") { + @Test + fun `as a property initializer`() { val code = """ class A { val foo = "BAR" @@ -67,8 +70,10 @@ class UnnecessaryInnerClassSpec : Spek({ assertThat(subject.lintWithContext(env, code)).isEmpty() } - context("in a variable assignment") { - it("where the outer-class variable is on the left") { + @Nested + inner class `in a variable assignment` { + @Test + fun `where the outer-class variable is on the left`() { val code = """ class A { var foo = "BAR" @@ -85,7 +90,8 @@ class UnnecessaryInnerClassSpec : Spek({ assertThat(subject.lintWithContext(env, code)).isEmpty() } - it("where the outer-class variable is on the right") { + @Test + fun `where the outer-class variable is on the right`() { val code = """ class A { val foo = "BAR" @@ -102,7 +108,8 @@ class UnnecessaryInnerClassSpec : Spek({ assertThat(subject.lintWithContext(env, code)).isEmpty() } - it("where the outer-class variable is in a compound statement") { + @Test + fun `where the outer-class variable is in a compound statement`() { val code = """ class A { val foo = "BAR" @@ -120,8 +127,10 @@ class UnnecessaryInnerClassSpec : Spek({ } } - context("in an if-statement") { - it("where the outer-class variable is the only expression") { + @Nested + inner class `in an if-statement` { + @Test + fun `where the outer-class variable is the only expression`() { val code = """ class A(val foo: Boolean) { @@ -138,7 +147,8 @@ class UnnecessaryInnerClassSpec : Spek({ assertThat(subject.lintWithContext(env, code)).isEmpty() } - it("where the outer-class variable is on the left") { + @Test + fun `where the outer-class variable is on the left`() { val code = """ class A { val foo = "BAR" @@ -156,7 +166,8 @@ class UnnecessaryInnerClassSpec : Spek({ assertThat(subject.lintWithContext(env, code)).isEmpty() } - it("where the outer-class variable is on the right") { + @Test + fun `where the outer-class variable is on the right`() { val code = """ class A { val foo = "BAR" @@ -174,7 +185,8 @@ class UnnecessaryInnerClassSpec : Spek({ assertThat(subject.lintWithContext(env, code)).isEmpty() } - it("where the outer-class variable is in a compound statement") { + @Test + fun `where the outer-class variable is in a compound statement`() { val code = """ class A { val foo = "BAR" @@ -194,7 +206,8 @@ class UnnecessaryInnerClassSpec : Spek({ } } - it("as a function initializer") { + @Test + fun `as a function initializer`() { val code = """ class A { fun printFoo() { @@ -210,7 +223,8 @@ class UnnecessaryInnerClassSpec : Spek({ assertThat(subject.lintWithContext(env, code)).isEmpty() } - it("as a function call") { + @Test + fun `as a function call`() { val code = """ class A { fun printFoo() { @@ -228,7 +242,8 @@ class UnnecessaryInnerClassSpec : Spek({ assertThat(subject.lintWithContext(env, code)).isEmpty() } - it("as a function argument") { + @Test + fun `as a function argument`() { val code = """ class A { val foo = "BAR" @@ -244,7 +259,8 @@ class UnnecessaryInnerClassSpec : Spek({ assertThat(subject.lintWithContext(env, code)).isEmpty() } - it("as a default value in a function signature") { + @Test + fun `as a default value in a function signature`() { val code = """ class A { val foo = "BAR" @@ -260,7 +276,8 @@ class UnnecessaryInnerClassSpec : Spek({ assertThat(subject.lintWithContext(env, code)).isEmpty() } - it("to call a function of the member") { + @Test + fun `to call a function of the member`() { val code = """ class FooClass { fun printFoo() { @@ -283,9 +300,11 @@ class UnnecessaryInnerClassSpec : Spek({ } } - context("does not report a double-nested inner class accessing from an outer-class member") { + @Nested + inner class `does not report a double-nested inner class accessing from an outer-class member` { - it("when the innermost class refers a inner class and the inner class refers the outermost class") { + @Test + fun `when the innermost class refers a inner class and the inner class refers the outermost class`() { val code = """ class A { val foo = "BAR" @@ -304,7 +323,8 @@ class UnnecessaryInnerClassSpec : Spek({ assertThat(subject.lintWithContext(env, code)).isEmpty() } - it("when the innermost class refers the outermost class") { + @Test + fun `when the innermost class refers the outermost class`() { val code = """ class A { val foo = "BAR" @@ -323,7 +343,8 @@ class UnnecessaryInnerClassSpec : Spek({ } } - it("does not report anonymous inner classes") { + @Test + fun `does not report anonymous inner classes`() { val code = """ interface FooInterface { fun doFoo() @@ -347,4 +368,4 @@ class UnnecessaryInnerClassSpec : Spek({ assertThat(subject.lintWithContext(env, code)).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryLetSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryLetSpec.kt index 312982174203..6415fd9c4098 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryLetSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryLetSpec.kt @@ -1,22 +1,21 @@ package io.gitlab.arturbosch.detekt.rules.style import io.gitlab.arturbosch.detekt.api.Config -import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext import org.assertj.core.api.Assertions.assertThat import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -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 UnnecessaryLetSpec : Spek({ +@KotlinCoreEnvironmentTest +class UnnecessaryLetSpec(val env: KotlinCoreEnvironment) { + val subject = UnnecessaryLet(Config.empty) - setupKotlinEnvironment() - - val subject by memoized { UnnecessaryLet(Config.empty) } - val env: KotlinCoreEnvironment by memoized() - - describe("UnnecessaryLet rule") { - it("reports unnecessary lets that can be changed to ordinary method call 1") { + @Nested + inner class `UnnecessaryLet rule` { + @Test + fun `reports unnecessary lets that can be changed to ordinary method call 1`() { val findings = subject.compileAndLintWithContext( env, """ @@ -31,7 +30,8 @@ class UnnecessaryLetSpec : Spek({ assertThat(findings).allMatch { it.message == MESSAGE_OMIT_LET } } - it("reports unnecessary lets that can be changed to ordinary method call 2") { + @Test + fun `reports unnecessary lets that can be changed to ordinary method call 2`() { val findings = subject.compileAndLintWithContext( env, """ @@ -46,7 +46,8 @@ class UnnecessaryLetSpec : Spek({ assertThat(findings).allMatch { it.message == MESSAGE_OMIT_LET } } - it("reports unnecessary lets that can be changed to ordinary method call 3") { + @Test + fun `reports unnecessary lets that can be changed to ordinary method call 3`() { val findings = subject.compileAndLintWithContext( env, """ @@ -61,7 +62,8 @@ class UnnecessaryLetSpec : Spek({ assertThat(findings).allMatch { it.message == MESSAGE_OMIT_LET } } - it("reports unnecessary lets that can be changed to ordinary method call 4") { + @Test + fun `reports unnecessary lets that can be changed to ordinary method call 4`() { val findings = subject.compileAndLintWithContext( env, """ @@ -75,7 +77,8 @@ class UnnecessaryLetSpec : Spek({ assertThat(findings).allMatch { it.message == MESSAGE_OMIT_LET } } - it("reports unnecessary lets that can be changed to ordinary method call 5") { + @Test + fun `reports unnecessary lets that can be changed to ordinary method call 5`() { val findings = subject.compileAndLintWithContext( env, """ @@ -90,7 +93,8 @@ class UnnecessaryLetSpec : Spek({ assertThat(findings).allMatch { it.message == MESSAGE_OMIT_LET } } - it("reports unnecessary lets that can be changed to ordinary method call 6") { + @Test + fun `reports unnecessary lets that can be changed to ordinary method call 6`() { val findings = subject.compileAndLintWithContext( env, """ @@ -105,7 +109,8 @@ class UnnecessaryLetSpec : Spek({ assertThat(findings).allMatch { it.message == MESSAGE_OMIT_LET } } - it("reports unnecessary lets that can be replaced with an if") { + @Test + fun `reports unnecessary lets that can be replaced with an if`() { val findings = subject.compileAndLintWithContext( env, """ @@ -120,7 +125,8 @@ class UnnecessaryLetSpec : Spek({ assertThat(findings).allMatch { it.message == MESSAGE_USE_IF } } - it("reports unnecessary lets that can be changed to ordinary method call 7") { + @Test + fun `reports unnecessary lets that can be changed to ordinary method call 7`() { val findings = subject.compileAndLintWithContext( env, """ @@ -135,7 +141,8 @@ class UnnecessaryLetSpec : Spek({ assertThat(findings).allMatch { it.message == MESSAGE_OMIT_LET } } - it("reports use of let without the safe call operator when we use an argument") { + @Test + fun `reports use of let without the safe call operator when we use an argument`() { val findings = subject.compileAndLintWithContext( env, """ @@ -150,7 +157,8 @@ class UnnecessaryLetSpec : Spek({ assertThat(findings).allMatch { it.message == MESSAGE_OMIT_LET } } - it("does not report lets used for function calls 1") { + @Test + fun `does not report lets used for function calls 1`() { val findings = subject.compileAndLintWithContext( env, """ @@ -164,7 +172,8 @@ class UnnecessaryLetSpec : Spek({ assertThat(findings).isEmpty() } - it("does not report lets used for function calls 2") { + @Test + fun `does not report lets used for function calls 2`() { val findings = subject.compileAndLintWithContext( env, """ @@ -177,7 +186,8 @@ class UnnecessaryLetSpec : Spek({ assertThat(findings).isEmpty() } - it("does not report \"can be replaced by if\" because you will need an else too") { + @Test + fun `does not report 'can be replaced by if' because you will need an else too`() { val findings = subject.compileAndLintWithContext( env, """ @@ -191,7 +201,8 @@ class UnnecessaryLetSpec : Spek({ assertThat(findings).isEmpty() } - it("does not report a let where returned value is used - #2987") { + @Test + fun `does not report a let where returned value is used - #2987`() { val findings = subject.compileAndLintWithContext( env, """ @@ -204,7 +215,8 @@ class UnnecessaryLetSpec : Spek({ assertThat(findings).isEmpty() } - it("does not report use of let with the safe call operator when we use an argument") { + @Test + fun `does not report use of let with the safe call operator when we use an argument`() { val findings = subject.compileAndLintWithContext( env, """ @@ -218,7 +230,8 @@ class UnnecessaryLetSpec : Spek({ assertThat(findings).hasSize(0) } - it("does not report lets with lambda body containing more than one statement") { + @Test + fun `does not report lets with lambda body containing more than one statement`() { val findings = subject.compileAndLintWithContext( env, """ @@ -254,7 +267,8 @@ class UnnecessaryLetSpec : Spek({ assertThat(findings).isEmpty() } - it("does not report lets where it is used multiple times") { + @Test + fun `does not report lets where it is used multiple times`() { val findings = subject.compileAndLintWithContext( env, """ @@ -271,8 +285,10 @@ class UnnecessaryLetSpec : Spek({ assertThat(findings).isEmpty() } - context("destructuring declarations") { - it("does not report `let` when parameters are used more than once") { + @Nested + inner class `destructuring declarations` { + @Test + fun `does not report 'let' when parameters are used more than once`() { val content = """ data class Foo(val a: Int, val b: Int) @@ -284,7 +300,8 @@ class UnnecessaryLetSpec : Spek({ assertThat(findings).isEmpty() } - it("does not report `let` with a safe call when a parameter is used more than once") { + @Test + fun `does not report 'let' with a safe call when a parameter is used more than once`() { val content = """ data class Foo(val a: Int, val b: Int) @@ -296,7 +313,8 @@ class UnnecessaryLetSpec : Spek({ assertThat(findings).isEmpty() } - it("does not report `let` when parameters with types are used more than once") { + @Test + fun `does not report 'let' when parameters with types are used more than once`() { val content = """ data class Foo(val a: Int, val b: Int) @@ -308,7 +326,8 @@ class UnnecessaryLetSpec : Spek({ assertThat(findings).isEmpty() } - it("reports `let` when parameters are used only once") { + @Test + fun `reports 'let' when parameters are used only once`() { val content = """ data class Foo(val a: Int, val b: Int) @@ -321,7 +340,8 @@ class UnnecessaryLetSpec : Spek({ assertThat(findings).allMatch { it.message == MESSAGE_OMIT_LET } } - it("reports `let` with a safe call when parameters are used only once") { + @Test + fun `reports 'let' with a safe call when parameters are used only once`() { val content = """ data class Foo(val a: Int, val b: Int) @@ -334,7 +354,8 @@ class UnnecessaryLetSpec : Spek({ assertThat(findings).allMatch { it.message == MESSAGE_OMIT_LET } } - it("reports `let` when parameters are not used") { + @Test + fun `reports 'let' when parameters are not used`() { val content = """ data class Foo(val a: Int, val b: Int) @@ -347,7 +368,8 @@ class UnnecessaryLetSpec : Spek({ assertThat(findings).allMatch { it.message == MESSAGE_OMIT_LET } } - it("reports `let` with a safe call when parameters are not used") { + @Test + fun `reports 'let' with a safe call when parameters are not used`() { val content = """ data class Foo(val a: Int, val b: Int) @@ -361,7 +383,8 @@ class UnnecessaryLetSpec : Spek({ } } - it("reports when implicit parameter isn't used") { + @Test + fun `reports when implicit parameter isn't used`() { val content = """ fun test(value: Int?) { value?.let { @@ -374,7 +397,8 @@ class UnnecessaryLetSpec : Spek({ assertThat(findings).allMatch { it.message == MESSAGE_USE_IF } } - it("does not report when an implicit parameter is used in an inner lambda") { + @Test + fun `does not report when an implicit parameter is used in an inner lambda`() { val content = """ fun callMe(callback: () -> Unit) { callback() @@ -392,7 +416,7 @@ class UnnecessaryLetSpec : Spek({ assertThat(findings).isEmpty() } } -}) +} private const val MESSAGE_OMIT_LET = "let expression can be omitted" private const val MESSAGE_USE_IF = "let expression can be replaced with a simple if" diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryParenthesesSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryParenthesesSpec.kt index 68c45cc0e36c..d8abb8656a17 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryParenthesesSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryParenthesesSpec.kt @@ -3,30 +3,35 @@ package io.gitlab.arturbosch.detekt.rules.style import io.gitlab.arturbosch.detekt.api.Config 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 +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test -class UnnecessaryParenthesesSpec : Spek({ - val subject by memoized { UnnecessaryParentheses(Config.empty) } +class UnnecessaryParenthesesSpec { + val subject = UnnecessaryParentheses(Config.empty) - describe("UnnecessaryParentheses rule") { + @Nested + inner class `UnnecessaryParentheses rule` { - it("with unnecessary parentheses on val assignment") { + @Test + fun `with unnecessary parentheses on val assignment`() { val code = "val local = (5)" assertThat(subject.lint(code)).hasSize(1) } - it("with unnecessary parentheses on val assignment operation") { + @Test + fun `with unnecessary parentheses on val assignment operation`() { val code = "val local = (5 + 3)" assertThat(subject.lint(code)).hasSize(1) } - it("with unnecessary parentheses on function call") { + @Test + fun `with unnecessary parentheses on function call`() { val code = "val local = 3.plus((5))" assertThat(subject.lint(code)).hasSize(1) } - it("unnecessary parentheses in other parentheses") { + @Test + fun `unnecessary parentheses in other parentheses`() { val code = """ fun x(a: String, b: String) { if ((a equals b)) { @@ -37,7 +42,8 @@ class UnnecessaryParenthesesSpec : Spek({ assertThat(subject.lint(code)).hasSize(1) } - it("does not report unnecessary parentheses around lambdas") { + @Test + fun `does not report unnecessary parentheses around lambdas`() { val code = """ fun function (a: (input: String) -> Unit) { a.invoke("TEST") @@ -50,7 +56,8 @@ class UnnecessaryParenthesesSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("doesn't report function calls containing lambdas and other parameters") { + @Test + fun `doesn't report function calls containing lambdas and other parameters`() { val code = """ fun function (integer: Int, a: (input: String) -> Unit) { a.invoke("TEST") @@ -63,7 +70,8 @@ class UnnecessaryParenthesesSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report unnecessary parentheses when assigning a lambda to a val") { + @Test + fun `does not report unnecessary parentheses when assigning a lambda to a val`() { val code = """ fun f() { instance.copy(value = { false }) @@ -72,7 +80,8 @@ class UnnecessaryParenthesesSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report well behaved parentheses") { + @Test + fun `does not report well behaved parentheses`() { val code = """ fun x(a: String, b: String) { if (a equals b) { @@ -83,7 +92,8 @@ class UnnecessaryParenthesesSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report well behaved parentheses in super constructors") { + @Test + fun `does not report well behaved parentheses in super constructors`() { val code = """ class TestSpek : SubjectSpek({ describe("a simple test") { @@ -95,7 +105,8 @@ class UnnecessaryParenthesesSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report well behaved parentheses in constructors") { + @Test + fun `does not report well behaved parentheses in constructors`() { val code = """ class TestSpek({ describe("a simple test") { @@ -107,7 +118,8 @@ class UnnecessaryParenthesesSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("should not report lambdas within super constructor calls") { + @Test + fun `should not report lambdas within super constructor calls`() { val code = """ class Clazz( private val func: (X, Y) -> Z @@ -118,7 +130,8 @@ class UnnecessaryParenthesesSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("should not report call to function with two lambda parameters with one as block body") { + @Test + fun `should not report call to function with two lambda parameters with one as block body`() { val code = """ class Clazz { fun test(first: (Int) -> Unit, second: (Int) -> Unit) { @@ -134,7 +147,8 @@ class UnnecessaryParenthesesSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("should not report call to function with two lambda parameters") { + @Test + fun `should not report call to function with two lambda parameters`() { val code = """ class Clazz { fun test(first: (Int) -> Unit, second: (Int) -> Unit) { @@ -150,7 +164,8 @@ class UnnecessaryParenthesesSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("should not report call to function with multiple lambdas as parameters but also other parameters") { + @Test + fun `should not report call to function with multiple lambdas as parameters but also other parameters`() { val code = """ class Clazz { fun test(text: String, first: () -> Unit, second: () -> Unit) { @@ -166,11 +181,12 @@ class UnnecessaryParenthesesSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("should not report interface delegation with parenthesis - #3851") { + @Test + fun `should not report interface delegation with parenthesis - #3851`() { val code = """ class Clazz: Comparable by ("hello".filter { it != 'l' }) """ assertThat(subject.lint(code)).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UntilInsteadOfRangeToSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UntilInsteadOfRangeToSpec.kt index e1ff8d17e318..dfa4ba796a7d 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UntilInsteadOfRangeToSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UntilInsteadOfRangeToSpec.kt @@ -3,15 +3,19 @@ package io.gitlab.arturbosch.detekt.rules.style import io.gitlab.arturbosch.detekt.api.Config import io.gitlab.arturbosch.detekt.test.assertThat import io.gitlab.arturbosch.detekt.test.lint -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 -class UntilInsteadOfRangeToSpec : Spek({ - val subject by memoized { UntilInsteadOfRangeTo(Config.empty) } +class UntilInsteadOfRangeToSpec { + val subject = UntilInsteadOfRangeTo(Config.empty) - describe("UntilInsteadOfRangeTo rule") { + @Nested + inner class `UntilInsteadOfRangeTo rule` { - it("reports for '..'") { + @Test + @DisplayName("reports for '..'") + fun reportsForDoubleDotsInForIterator() { val code = """ fun f() { for (i in 0 .. 10 - 1) {} @@ -22,7 +26,8 @@ class UntilInsteadOfRangeToSpec : Spek({ assertThat(findings[0]).hasMessage("'..' call can be replaced with 'until'") } - it("does not report if rangeTo not used") { + @Test + fun `does not report if rangeTo not used`() { val code = """ fun f() { for (i in 0 until 10 - 1) {} @@ -32,7 +37,8 @@ class UntilInsteadOfRangeToSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report if upper value isn't a binary expression") { + @Test + fun `does not report if upper value isn't a binary expression`() { val code = """ fun f() { for (i in 0 .. 10) {} @@ -41,7 +47,8 @@ class UntilInsteadOfRangeToSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report if not minus one") { + @Test + fun `does not report if not minus one`() { val code = """ fun f() { for (i in 0 .. 10 + 1) {} @@ -51,21 +58,25 @@ class UntilInsteadOfRangeToSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("reports for '..'") { + @Test + @DisplayName("reports for '..'") + fun reportsForDoubleDots() { val code = "val r = 0 .. 10 - 1" assertThat(subject.lint(code)).hasSize(1) } - it("does not report binary expressions without a range operator") { + @Test + fun `does not report binary expressions without a range operator`() { val code = "val sum = 1 + 2" assertThat(subject.lint(code)).isEmpty() } - it("reports for 'rangeTo'") { + @Test + fun `reports for 'rangeTo'`() { val code = "val r = 0.rangeTo(10 - 1)" val findings = subject.lint(code) assertThat(findings).hasSize(1) assertThat(findings[0]).hasMessage("'rangeTo' call can be replaced with 'until'") } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnusedImportsSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnusedImportsSpec.kt index aead4cd4de63..7fa29ee9585c 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnusedImportsSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnusedImportsSpec.kt @@ -1,22 +1,22 @@ package io.gitlab.arturbosch.detekt.rules.style import io.gitlab.arturbosch.detekt.api.Config -import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.lintWithContext import org.assertj.core.api.Assertions.assertThat import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -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 UnusedImportsSpec : Spek({ - setupKotlinEnvironment() +@KotlinCoreEnvironmentTest +class UnusedImportsSpec(val env: KotlinCoreEnvironment) { + val subject = UnusedImports(Config.empty) - val env: KotlinCoreEnvironment by memoized() - val subject by memoized { UnusedImports(Config.empty) } + @Nested + inner class `UnusedImports rule` { - describe("UnusedImports rule") { - - it("does not report infix operators") { + @Test + fun `does not report infix operators`() { val main = """ import tasks.success @@ -36,7 +36,8 @@ class UnusedImportsSpec : Spek({ assertThat(subject.lintWithContext(env, main, additional)).isEmpty() } - it("does not report imports in documentation") { + @Test + fun `does not report imports in documentation`() { val main = """ import tasks.success import tasks.failure @@ -66,7 +67,8 @@ class UnusedImportsSpec : Spek({ assertThat(subject.lintWithContext(env, main, additional)).isEmpty() } - it("should ignore import for link") { + @Test + fun `should ignore import for link`() { val main = """ import tasks.success import tasks.failure @@ -97,7 +99,8 @@ class UnusedImportsSpec : Spek({ } } - it("reports imports from the current package") { + @Test + fun `reports imports from the current package`() { val main = """ package test import test.SomeClass @@ -116,7 +119,8 @@ class UnusedImportsSpec : Spek({ } } - it("does not report KDoc references with method calls") { + @Test + fun `does not report KDoc references with method calls`() { val main = """ package com.example @@ -141,7 +145,8 @@ class UnusedImportsSpec : Spek({ assertThat(subject.lintWithContext(env, main, additional)).isEmpty() } - it("reports imports with different cases") { + @Test + fun `reports imports with different cases`() { val main = """ import p.a import p.B6 // positive @@ -195,7 +200,8 @@ class UnusedImportsSpec : Spek({ } } - it("does not report imports in same package when inner") { + @Test + fun `does not report imports in same package when inner`() { val main = """ package test @@ -218,7 +224,8 @@ class UnusedImportsSpec : Spek({ } } - it("does not report KDoc @see annotation linking to class") { + @Test + fun `does not report KDoc @see annotation linking to class`() { val main = """ import tasks.success @@ -236,7 +243,8 @@ class UnusedImportsSpec : Spek({ assertThat(subject.lintWithContext(env, main, additional)).isEmpty() } - it("does not report KDoc @see annotation linking to class with description") { + @Test + fun `does not report KDoc @see annotation linking to class with description`() { val main = """ import tasks.success @@ -254,7 +262,8 @@ class UnusedImportsSpec : Spek({ assertThat(subject.lintWithContext(env, main, additional)).isEmpty() } - it("reports KDoc @see annotation that does not link to class") { + @Test + fun `reports KDoc @see annotation that does not link to class`() { val main = """ import tasks.success @@ -272,7 +281,8 @@ class UnusedImportsSpec : Spek({ assertThat(subject.lintWithContext(env, main, additional)).hasSize(1) } - it("reports KDoc @see annotation that links after description") { + @Test + fun `reports KDoc @see annotation that links after description`() { val main = """ import tasks.success @@ -290,7 +300,8 @@ class UnusedImportsSpec : Spek({ assertThat(subject.lintWithContext(env, main, additional)).hasSize(1) } - it("does not report imports in KDoc") { + @Test + fun `does not report imports in KDoc`() { val main = """ import tasks.success // here import tasks.undefined // and here @@ -313,7 +324,8 @@ class UnusedImportsSpec : Spek({ assertThat(subject.lintWithContext(env, main, additional)).isEmpty() } - it("should not report import alias as unused when the alias is used") { + @Test + fun `should not report import alias as unused when the alias is used`() { val main = """ import test.forEach as foreach fun foo() = listOf().iterator().foreach {} @@ -325,7 +337,8 @@ class UnusedImportsSpec : Spek({ assertThat(subject.lintWithContext(env, main, additional)).isEmpty() } - it("should not report used alias even when import is from same package") { + @Test + fun `should not report used alias even when import is from same package`() { val main = """ package com.example @@ -347,7 +360,8 @@ class UnusedImportsSpec : Spek({ assertThat(subject.lintWithContext(env, main, additional1, additional2)).isEmpty() } - it("should not report import of provideDelegate operator overload - #1608") { + @Test + fun `should not report import of provideDelegate operator overload - #1608`() { val main = """ import org.gradle.kotlin.dsl.Foo import org.gradle.kotlin.dsl.provideDelegate // this line specifically should not be reported @@ -371,7 +385,8 @@ class UnusedImportsSpec : Spek({ assertThat(subject.lintWithContext(env, main, additional)).isEmpty() } - it("should not report import of componentN operator") { + @Test + fun `should not report import of componentN operator`() { val main = """ import com.example.MyClass.component1 import com.example.MyClass.component2 @@ -389,7 +404,8 @@ class UnusedImportsSpec : Spek({ assertThat(subject.lintWithContext(env, main, additional)).isEmpty() } - it("should report import of identifiers with component in the name") { + @Test + fun `should report import of identifiers with component in the name`() { val main = """ import com.example.TestComponent import com.example.component1.Unused @@ -421,7 +437,8 @@ class UnusedImportsSpec : Spek({ } } - it("reports when same name identifiers are imported and used") { + @Test + fun `reports when same name identifiers are imported and used`() { val mainFile = """ import foo.test import bar.test @@ -442,7 +459,8 @@ class UnusedImportsSpec : Spek({ assertThat(findings[0].entity.signature).endsWith("import bar.test") } - it("does not report when used as a type") { + @Test + fun `does not report when used as a type`() { val code = """ import java.util.HashMap @@ -453,7 +471,8 @@ class UnusedImportsSpec : Spek({ assertThat(findings).isEmpty() } - it("does not report when used in a class literal expression") { + @Test + fun `does not report when used in a class literal expression`() { val code = """ import java.util.HashMap import kotlin.reflect.KClass @@ -467,7 +486,8 @@ class UnusedImportsSpec : Spek({ assertThat(findings).isEmpty() } - it("does not report when used as a constructor call") { + @Test + fun `does not report when used as a constructor call`() { val mainFile = """ import x.y.z.Foo @@ -482,7 +502,8 @@ class UnusedImportsSpec : Spek({ assertThat(findings).isEmpty() } - it("does not report when used as a annotation") { + @Test + fun `does not report when used as a annotation`() { val mainFile = """ import x.y.z.Ann @@ -498,7 +519,8 @@ class UnusedImportsSpec : Spek({ assertThat(findings).isEmpty() } - it("does not report companion object") { + @Test + fun `does not report companion object`() { val mainFile = """ import x.y.z.Foo @@ -515,7 +537,8 @@ class UnusedImportsSpec : Spek({ assertThat(findings).isEmpty() } - it("does not report companion object that calls function") { + @Test + fun `does not report companion object that calls function`() { val mainFile = """ import x.y.z.Foo @@ -534,7 +557,8 @@ class UnusedImportsSpec : Spek({ assertThat(findings).isEmpty() } - it("does not report companion object that references variable") { + @Test + fun `does not report companion object that references variable`() { val mainFile = """ import x.y.z.Foo @@ -553,7 +577,8 @@ class UnusedImportsSpec : Spek({ assertThat(findings).isEmpty() } - it("does not report static import") { + @Test + fun `does not report static import`() { val mainFile = """ import x.y.z.FetchType @@ -569,7 +594,8 @@ class UnusedImportsSpec : Spek({ assertThat(subject.lintWithContext(env, mainFile, additionalFile)).isEmpty() } - it("does not report annotations used as attributes - #3246") { + @Test + fun `does not report annotations used as attributes - #3246`() { val mainFile = """ import x.y.z.AnnotationA import x.y.z.AnnotationB @@ -588,4 +614,4 @@ class UnusedImportsSpec : Spek({ assertThat(subject.lintWithContext(env, mainFile, additionalFile)).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnusedPrivateClassSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnusedPrivateClassSpec.kt index dfc8aa4e317e..84cc5bacf1db 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnusedPrivateClassSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnusedPrivateClassSpec.kt @@ -3,15 +3,18 @@ package io.gitlab.arturbosch.detekt.rules.style import io.gitlab.arturbosch.detekt.test.assertThat import io.gitlab.arturbosch.detekt.test.compileAndLint import io.gitlab.arturbosch.detekt.test.lint -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 -class UnusedPrivateClassSpec : Spek({ +class UnusedPrivateClassSpec { - val subject by memoized { UnusedPrivateClass() } + val subject = UnusedPrivateClass() - describe("top level interfaces") { - it("should report them if not used") { + @Nested + inner class `top level interfaces` { + @Test + fun `should report them if not used`() { val code = """ private interface Foo class Bar @@ -23,9 +26,11 @@ class UnusedPrivateClassSpec : Spek({ assertThat(findings).hasSourceLocation(1, 1) } - describe("top level private classes") { + @Nested + inner class `top level private classes` { - it("should report them if not used") { + @Test + fun `should report them if not used`() { val code = """ private class Foo class Bar @@ -37,7 +42,8 @@ class UnusedPrivateClassSpec : Spek({ assertThat(findings).hasSourceLocation(1, 1) } - it("should not report them if used as parent") { + @Test + fun `should not report them if used as parent`() { val code = """ private open class Foo private class Bar : Foo() @@ -49,7 +55,8 @@ class UnusedPrivateClassSpec : Spek({ assertThat(findings).hasSourceLocation(2, 1) } - it("should not report them used as generic parent type") { + @Test + fun `should not report them used as generic parent type`() { val code = """ class Bar private interface Foo { @@ -67,7 +74,8 @@ class UnusedPrivateClassSpec : Spek({ } } - it("should not report them if used inside a function") { + @Test + fun `should not report them if used inside a function`() { val code = """ private class Foo fun something() { @@ -80,7 +88,8 @@ class UnusedPrivateClassSpec : Spek({ assertThat(findings).isEmpty() } - it("should not report them if used as function parameter") { + @Test + fun `should not report them if used as function parameter`() { val code = """ private class Foo private object Bar { @@ -93,7 +102,8 @@ class UnusedPrivateClassSpec : Spek({ assertThat(findings).isEmpty() } - it("should not report them if used as nullable variable type") { + @Test + fun `should not report them if used as nullable variable type`() { val code = """ private class Foo private val a: Foo? = null @@ -104,7 +114,8 @@ class UnusedPrivateClassSpec : Spek({ assertThat(findings).isEmpty() } - it("should not report them if used as variable type") { + @Test + fun `should not report them if used as variable type`() { val code = """ private class Foo private lateinit var a: Foo @@ -115,7 +126,8 @@ class UnusedPrivateClassSpec : Spek({ assertThat(findings).isEmpty() } - it("should not report them if used as generic type") { + @Test + fun `should not report them if used as generic type`() { val code = """ private class Foo private lateinit var foos: List @@ -126,7 +138,8 @@ class UnusedPrivateClassSpec : Spek({ assertThat(findings).isEmpty() } - it("should not report them if used as inner type parameter") { + @Test + fun `should not report them if used as inner type parameter`() { val code = """ private val elements = listOf(42).filterIsInstance>() private class Item @@ -137,7 +150,8 @@ class UnusedPrivateClassSpec : Spek({ assertThat(findings).isEmpty() } - it("should not report them if used as outer type parameter") { + @Test + fun `should not report them if used as outer type parameter`() { val code = """ private val elements = listOf(42).filterIsInstance>() private abstract class Something: Collection @@ -148,7 +162,8 @@ class UnusedPrivateClassSpec : Spek({ assertThat(findings).isEmpty() } - it("should not report them if used as generic type in functions") { + @Test + fun `should not report them if used as generic type in functions`() { val code = """ private class Foo private var a = bar() @@ -163,7 +178,8 @@ class UnusedPrivateClassSpec : Spek({ assertThat(findings).isEmpty() } - it("should not report them if used as nested generic type") { + @Test + fun `should not report them if used as nested generic type`() { val code = """ private class Foo private lateinit var foos: List> @@ -174,7 +190,8 @@ class UnusedPrivateClassSpec : Spek({ assertThat(findings).isEmpty() } - it("should not report them if used as type with generics") { + @Test + fun `should not report them if used as type with generics`() { val code = """ private class Foo private lateinit var foos: Foo @@ -185,7 +202,8 @@ class UnusedPrivateClassSpec : Spek({ assertThat(findings).isEmpty() } - it("should not report them if used as nullable type with generics") { + @Test + fun `should not report them if used as nullable type with generics`() { val code = """ private class Foo private var foos: Foo? = Foo() @@ -196,7 +214,8 @@ class UnusedPrivateClassSpec : Spek({ assertThat(findings).isEmpty() } - it("should not report them if used as non-argument constructor") { + @Test + fun `should not report them if used as non-argument constructor`() { val code = """ private class Foo private val a = Foo() @@ -207,7 +226,8 @@ class UnusedPrivateClassSpec : Spek({ assertThat(findings).isEmpty() } - it("should not report them if used as constructor with arguments") { + @Test + fun `should not report them if used as constructor with arguments`() { val code = """ private class Foo(val a: String) private val a = Foo("test") @@ -218,7 +238,8 @@ class UnusedPrivateClassSpec : Spek({ assertThat(findings).isEmpty() } - it("should not report them if used as function return type") { + @Test + fun `should not report them if used as function return type`() { val code = """ private class Foo(val a: String) private object Bar { @@ -231,7 +252,8 @@ class UnusedPrivateClassSpec : Spek({ assertThat(findings).isEmpty() } - it("should not report them if used as lambda declaration parameter") { + @Test + fun `should not report them if used as lambda declaration parameter`() { val code = """ private class Foo private val lambda: ((Foo) -> Unit)? = null @@ -242,7 +264,8 @@ class UnusedPrivateClassSpec : Spek({ assertThat(findings).isEmpty() } - it("should not report them if used as lambda declaration return type") { + @Test + fun `should not report them if used as lambda declaration return type`() { val code = """ private class Foo private val lambda: (() -> Foo)? = null @@ -253,7 +276,8 @@ class UnusedPrivateClassSpec : Spek({ assertThat(findings).isEmpty() } - it("should not report them if used as lambda declaration generic type") { + @Test + fun `should not report them if used as lambda declaration generic type`() { val code = """ private class Foo private val lambda: (() -> List)? = null @@ -264,7 +288,8 @@ class UnusedPrivateClassSpec : Spek({ assertThat(findings).isEmpty() } - it("should not report them if used as inline object type") { + @Test + fun `should not report them if used as inline object type`() { val code = """ private abstract class Foo { abstract fun bar() @@ -283,9 +308,11 @@ class UnusedPrivateClassSpec : Spek({ } } - describe("testcase for reported false positives") { + @Nested + inner class `testcase for reported false positives` { - it("does not crash when using wildcards in generics - #1345") { + @Test + fun `does not crash when using wildcards in generics - #1345`() { val code = """ import kotlin.reflect.KClass @@ -298,7 +325,9 @@ class UnusedPrivateClassSpec : Spek({ assertThat(findings).hasSize(1) } - it("does not report (companion-)object/named-dot references - #1347") { + @Test + @DisplayName("does not report (companion-)object/named-dot references - #1347") + fun doesNotReportObjectNamedDotReferences() { val code = """ class Test { val items = Item.values().map { it.text }.toList() @@ -316,7 +345,9 @@ class UnusedPrivateClassSpec : Spek({ assertThat(findings).isEmpty() } - it("does not report classes that are used with ::class - #1390") { + @Test + @DisplayName("does not report classes that are used with ::class - #1390") + fun doesNotReportClassesUsedWithinClass() { val code = """ class UnusedPrivateClassTest { @@ -339,7 +370,8 @@ class UnusedPrivateClassSpec : Spek({ assertThat(findings).isEmpty() } - it("does not report used private annotations - #2093") { + @Test + fun `does not report used private annotations - #2093`() { val code = """ private annotation class Test1 private annotation class Test2 @@ -357,7 +389,8 @@ class UnusedPrivateClassSpec : Spek({ assertThat(findings).isEmpty() } - it("does not report imported enum class - #2809") { + @Test + fun `does not report imported enum class - #2809`() { val code = """ package com.example @@ -379,7 +412,8 @@ class UnusedPrivateClassSpec : Spek({ assertThat(findings).isEmpty() } - it("should report not imported enum class - #2809, #2816") { + @Test + fun `should report not imported enum class - #2809, #2816`() { val code = """ package com.example @@ -404,4 +438,4 @@ class UnusedPrivateClassSpec : Spek({ assertThat(findings).hasSourceLocation(10, 5) } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnusedPrivateMemberSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnusedPrivateMemberSpec.kt index 8a13f3026eef..c538acf537bf 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnusedPrivateMemberSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnusedPrivateMemberSpec.kt @@ -1,7 +1,7 @@ package io.gitlab.arturbosch.detekt.rules.style import io.gitlab.arturbosch.detekt.api.SourceLocation -import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.TestConfig import io.gitlab.arturbosch.detekt.test.assertThat import io.gitlab.arturbosch.detekt.test.compileAndLint @@ -11,17 +11,15 @@ import io.gitlab.arturbosch.detekt.test.lintWithContext import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatExceptionOfType import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -import org.spekframework.spek2.Spek -import org.spekframework.spek2.style.specification.describe +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test import java.util.regex.PatternSyntaxException private const val ALLOWED_NAMES_PATTERN = "allowedNames" -class UnusedPrivateMemberSpec : Spek({ - setupKotlinEnvironment() - - val env: KotlinCoreEnvironment by memoized() - val subject by memoized { UnusedPrivateMember() } +@KotlinCoreEnvironmentTest +class UnusedPrivateMemberSpec(val env: KotlinCoreEnvironment) { + val subject = UnusedPrivateMember() val regexTestingCode = """ class Test { @@ -34,9 +32,11 @@ class UnusedPrivateMemberSpec : Spek({ } """ - describe("interface functions") { + @Nested + inner class `interface functions` { - it("should not report parameters in interface functions") { + @Test + fun `should not report parameters in interface functions`() { val code = """ interface UserPlugin { fun plug(application: Application) @@ -47,9 +47,11 @@ class UnusedPrivateMemberSpec : Spek({ } } - describe("expect functions and classes") { + @Nested + inner class `expect functions and classes` { - it("should not report parameters in expect class functions") { + @Test + fun `should not report parameters in expect class functions`() { val code = """ expect class Foo { fun bar(i: Int) @@ -59,7 +61,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("should not report parameters in expect object functions") { + @Test + fun `should not report parameters in expect object functions`() { val code = """ expect object Foo { fun bar(i: Int) @@ -69,7 +72,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("should not report parameters in expect functions") { + @Test + fun `should not report parameters in expect functions`() { val code = """ expect fun bar(i: Int) expect fun baz(i: Int, s: String) @@ -77,7 +81,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("should not report parameters in expect class with constructor") { + @Test + fun `should not report parameters in expect class with constructor`() { val code = """ expect class Foo1(private val bar: String) {} expect class Foo2(bar: String) {} @@ -86,9 +91,11 @@ class UnusedPrivateMemberSpec : Spek({ } } - describe("actual functions and classes") { + @Nested + inner class `actual functions and classes` { - it("should not report unused parameters in actual functions") { + @Test + fun `should not report unused parameters in actual functions`() { val code = """ actual class Foo { actual fun bar(i: Int) {} @@ -98,21 +105,24 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("should not report unused parameters in actual constructors") { + @Test + fun `should not report unused parameters in actual constructors`() { val code = """ actual class Foo actual constructor(bar: String) {} """ assertThat(subject.lint(code)).isEmpty() } - it("should not report unused actual fields defined as parameters of primary constructors") { + @Test + fun `should not report unused actual fields defined as parameters of primary constructors`() { val code = """ actual class Foo actual constructor(actual val bar: String) {} """ assertThat(subject.lint(code)).isEmpty() } - it("reports unused private fields defined as parameters of primary constructors") { + @Test + fun `reports unused private fields defined as parameters of primary constructors`() { val code = """ actual class Foo actual constructor(private val bar: String) {} """ @@ -120,17 +130,21 @@ class UnusedPrivateMemberSpec : Spek({ } } - describe("external functions") { + @Nested + inner class `external functions` { - it("should not report parameters in external functions") { + @Test + fun `should not report parameters in external functions`() { val code = "external fun foo(bar: String)" assertThat(subject.lint(code)).isEmpty() } } - describe("external classes") { + @Nested + inner class `external classes` { - it("should not report functions in external classes") { + @Test + fun `should not report functions in external classes`() { val code = """ external class Bugsnag { companion object { @@ -143,9 +157,11 @@ class UnusedPrivateMemberSpec : Spek({ } } - describe("protected functions") { + @Nested + inner class `protected functions` { - it("should not report parameters in protected functions") { + @Test + fun `should not report parameters in protected functions`() { val code = """ open class Foo { protected fun fee(bar: String) {} @@ -155,9 +171,11 @@ class UnusedPrivateMemberSpec : Spek({ } } - describe("overridden functions") { + @Nested + inner class `overridden functions` { - it("should not report parameters in not private functions") { + @Test + fun `should not report parameters in not private functions`() { val code = """ override fun funA() { objectA.resolve(valA, object : MyCallback { @@ -170,7 +188,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("should not report in overriden classes") { + @Test + fun `should not report in overriden classes`() { val code = """ abstract class Parent { abstract fun abstractFun(arg: Any) @@ -192,8 +211,10 @@ class UnusedPrivateMemberSpec : Spek({ } } - describe("non-private classes") { - it("should not report internal classes") { + @Nested + inner class `non-private classes` { + @Test + fun `should not report internal classes`() { val code = """ internal class IC // unused but internal """ @@ -201,9 +222,11 @@ class UnusedPrivateMemberSpec : Spek({ } } - describe("classes accessing constants from companion objects") { + @Nested + inner class `classes accessing constants from companion objects` { - it("should not report used constants") { + @Test + fun `should not report used constants`() { val code = """ class A { companion object { @@ -220,9 +243,11 @@ class UnusedPrivateMemberSpec : Spek({ } } - describe("classes with properties") { + @Nested + inner class `classes with properties` { - it("reports an unused member") { + @Test + fun `reports an unused member`() { val code = """ class Test { private val unused = "This is not used" @@ -235,7 +260,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).hasSize(1) } - it("does not report unused public members") { + @Test + fun `does not report unused public members`() { val code = """ class Test { val unused = "This is not used" @@ -248,7 +274,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report used members") { + @Test + fun `does not report used members`() { val code = """ class Test { private val used = "This is used" @@ -261,7 +288,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report used members but reports unused members") { + @Test + fun `does not report used members but reports unused members`() { val code = """ class Test { private val used = "This is used" @@ -275,7 +303,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).hasSize(1) } - it("does not fail when disabled with invalid regex") { + @Test + fun `does not fail when disabled with invalid regex`() { val configRules = mapOf( "active" to "false", ALLOWED_NAMES_PATTERN to "*foo" @@ -284,7 +313,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(UnusedPrivateMember(config).lint(regexTestingCode)).isEmpty() } - it("does fail when enabled with invalid regex") { + @Test + fun `does fail when enabled with invalid regex`() { val configRules = mapOf(ALLOWED_NAMES_PATTERN to "*foo") val config = TestConfig(configRules) assertThatExceptionOfType(PatternSyntaxException::class.java) @@ -292,9 +322,11 @@ class UnusedPrivateMemberSpec : Spek({ } } - describe("classes with properties and local properties") { + @Nested + inner class `classes with properties and local properties` { - it("reports multiple unused properties") { + @Test + fun `reports multiple unused properties`() { val code = """ class UnusedPrivateMemberPositive { private val unusedField = 5 @@ -308,7 +340,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).hasSize(5) } - it("reports an unused member") { + @Test + fun `reports an unused member`() { val code = """ class Test { private val unused = "This is not used" @@ -322,7 +355,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).hasSize(1) } - it("does not report used members") { + @Test + fun `does not report used members`() { val code = """ class Test { private val used = "This is used" @@ -336,7 +370,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report used members and properties") { + @Test + fun `does not report used members and properties`() { val code = """ class C { val myNumber = 5 @@ -372,7 +407,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report used private classes") { + @Test + fun `does not report used private classes`() { val code = """ private class PC { // used private class companion object { @@ -396,7 +432,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).hasSize(0) } - it("reports unused local properties") { + @Test + fun `reports unused local properties`() { val code = """ class Test { private val used = "This is used" @@ -411,8 +448,10 @@ class UnusedPrivateMemberSpec : Spek({ } } - describe("objects with properties") { - it("reports multiple unused properties") { + @Nested + inner class `objects with properties` { + @Test + fun `reports multiple unused properties`() { val code = """ object UnusedPrivateMemberPositiveObject { private const val unusedObjectConst = 2 @@ -429,7 +468,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).hasSize(4) } - it("does not report public properties") { + @Test + fun `does not report public properties`() { val code = """ object O { // public const val NUMBER = 5 // public @@ -443,9 +483,11 @@ class UnusedPrivateMemberSpec : Spek({ } } - describe("loop iterators") { + @Nested + inner class `loop iterators` { - it("should not depend on evaluation order of functions or properties") { + @Test + fun `should not depend on evaluation order of functions or properties`() { val code = """ fun RuleSetProvider.provided() = ruleSetId in defaultRuleSetIds @@ -455,7 +497,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("doesn't report loop properties") { + @Test + fun `doesn't report loop properties`() { val code = """ class Test { fun use() { @@ -468,7 +511,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("reports unused loop property") { + @Test + fun `reports unused loop property`() { val code = """ class Test { fun use() { @@ -480,7 +524,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).hasSize(1) } - it("reports unused loop property in indexed array") { + @Test + fun `reports unused loop property in indexed array`() { val code = """ class Test { fun use() { @@ -494,7 +539,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).hasSize(1) } - it("reports all unused loop properties in indexed array") { + @Test + fun `reports all unused loop properties in indexed array`() { val code = """ class Test { fun use() { @@ -507,7 +553,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).hasSize(2) } - it("does not report used loop properties in indexed array") { + @Test + fun `does not report used loop properties in indexed array`() { val code = """ class Test { fun use() { @@ -523,9 +570,11 @@ class UnusedPrivateMemberSpec : Spek({ } } - describe("properties used to initialize other properties") { + @Nested + inner class `properties used to initialize other properties` { - it("does not report properties used by other properties") { + @Test + fun `does not report properties used by other properties`() { val code = """ class Test { private val used = "This is used" @@ -539,7 +588,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report properties used by inner classes") { + @Test + fun `does not report properties used by inner classes`() { val code = """ class Test { private val unused = "This is not used" @@ -553,16 +603,19 @@ class UnusedPrivateMemberSpec : Spek({ } } - describe("top level functions") { + @Nested + inner class `top level functions` { - it("reports top-level unused functions") { + @Test + fun `reports top-level unused functions`() { val code = """ private fun unusedTopLevelFunction() = 5 """ assertThat(subject.lint(code)).hasSize(1) } - it("does not report used top level functions") { + @Test + fun `does not report used top level functions`() { val code = """ private fun calledFromMain() {} @@ -574,8 +627,10 @@ class UnusedPrivateMemberSpec : Spek({ } } - describe("top level properties") { - it("reports single parameters if they are unused") { + @Nested + inner class `top level properties` { + @Test + fun `reports single parameters if they are unused`() { val code = """ private val usedTopLevelVal = 1 private const val unusedTopLevelConst = 1 @@ -584,7 +639,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).hasSize(2) } - it("does not report used top level properties") { + @Test + fun `does not report used top level properties`() { val code = """ val stuff = object : Iterator { @@ -611,8 +667,10 @@ class UnusedPrivateMemberSpec : Spek({ } } - describe("unused private functions") { - it("does not report used private functions") { + @Nested + inner class `unused private functions` { + @Test + fun `does not report used private functions`() { val code = """ class Test { val value = usedMethod() @@ -626,7 +684,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("reports unused private functions") { + @Test + fun `reports unused private functions`() { val code = """ class Test { private fun unusedFunction(): Int { @@ -638,7 +697,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).hasSize(1) } - it("does not report function used in interface - #1613") { + @Test + fun `does not report function used in interface - #1613`() { val code = """ interface Bar { fun doSomething() { @@ -652,9 +712,11 @@ class UnusedPrivateMemberSpec : Spek({ } } - describe("private functions only used by unused private functions") { + @Nested + inner class `private functions only used by unused private functions` { - it("reports the non called private function") { + @Test + fun `reports the non called private function`() { val code = """ class Test { private fun unusedFunction(): Int { @@ -671,9 +733,11 @@ class UnusedPrivateMemberSpec : Spek({ } } - describe("unused class declarations which are allowed") { + @Nested + inner class `unused class declarations which are allowed` { - it("does not report the unused private property") { + @Test + fun `does not report the unused private property`() { val code = """ class Test { private val ignored = "" @@ -682,7 +746,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report the unused private function and parameter") { + @Test + fun `does not report the unused private function and parameter`() { val code = """ class Test { private fun ignored(ignored: Int) {} @@ -692,9 +757,11 @@ class UnusedPrivateMemberSpec : Spek({ } } - describe("nested class declarations") { + @Nested + inner class `nested class declarations` { - it("reports unused nested private property") { + @Test + fun `reports unused nested private property`() { val code = """ class Test { class Inner { @@ -705,7 +772,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).hasSize(1) } - it("does not report used nested private property") { + @Test + fun `does not report used nested private property`() { val code = """ class Test { class Inner { @@ -718,22 +786,26 @@ class UnusedPrivateMemberSpec : Spek({ } } - describe("properties in primary constructors") { - it("reports unused private property") { + @Nested + inner class `properties in primary constructors` { + @Test + fun `reports unused private property`() { val code = """ class Test(private val unused: Any) """ assertThat(subject.lint(code)).hasSize(1) } - it("does not report public property") { + @Test + fun `does not report public property`() { val code = """ class Test(val unused: Any) """ assertThat(subject.lint(code)).isEmpty() } - it("does not report private property used in init block") { + @Test + fun `does not report private property used in init block`() { val code = """ class Test(private val used: Any) { init { used.toString() } @@ -742,7 +814,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report private property used in function") { + @Test + fun `does not report private property used in function`() { val code = """ class Test(private val used: Any) { fun something() { @@ -754,8 +827,10 @@ class UnusedPrivateMemberSpec : Spek({ } } - describe("error messages") { - it("are specific for function parameters") { + @Nested + inner class `error messages` { + @Test + fun `are specific for function parameters`() { val code = """ fun foo(unused: Int){} """ @@ -765,7 +840,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(lint.first().message).startsWith("Function parameter") } - it("are specific for local variables") { + @Test + fun `are specific for local variables`() { val code = """ fun foo(){ val unused = 1 } """ @@ -775,7 +851,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(lint.first().message).startsWith("Private property") } - it("are specific for private functions") { + @Test + fun `are specific for private functions`() { val code = """ class Test { private fun unusedFunction(): Int { @@ -790,8 +867,10 @@ class UnusedPrivateMemberSpec : Spek({ } } - describe("suppress unused property warning annotations") { - it("does not report annotated private constructor properties") { + @Nested + inner class `suppress unused property warning annotations` { + @Test + fun `does not report annotated private constructor properties`() { val code = """ class Test(@Suppress("unused") private val foo: String) {} """ @@ -799,7 +878,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("reports private constructor properties without annotation") { + @Test + fun `reports private constructor properties without annotation`() { val code = """ class Test( @Suppress("unused") private val foo: String, @@ -813,7 +893,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(lint[0].entity.signature).isEqualTo("Test.kt\$Test\$private val bar: String") } - it("does not report private constructor properties in annotated class") { + @Test + fun `does not report private constructor properties in annotated class`() { val code = """ @Suppress("unused") class Test( @@ -825,7 +906,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report private constructor properties in class with annotated outer class") { + @Test + fun `does not report private constructor properties in class with annotated outer class`() { val code = """ @Suppress("unused") class Test( @@ -841,7 +923,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report private constructor properties in annotated file") { + @Test + fun `does not report private constructor properties in annotated file`() { val code = """ @file:Suppress("unused") @@ -858,7 +941,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report annotated private properties") { + @Test + fun `does not report annotated private properties`() { val code = """ class Test { @Suppress("unused") private val foo: String @@ -868,7 +952,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("reports private properties without annotation") { + @Test + fun `reports private properties without annotation`() { val code = """ class Test { @Suppress("unused") private val foo: String @@ -882,7 +967,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(lint[0].entity.signature).isEqualTo("Test.kt\$Test\$private val bar: String") } - it("does not report private properties in annotated class") { + @Test + fun `does not report private properties in annotated class`() { val code = """ @Suppress("unused") class Test { @@ -894,7 +980,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report private properties in class with annotated outer class") { + @Test + fun `does not report private properties in class with annotated outer class`() { val code = """ @Suppress("unused") class Test { @@ -910,7 +997,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report private properties in annotated file") { + @Test + fun `does not report private properties in annotated file`() { val code = """ @file:Suppress("unused") @@ -928,8 +1016,10 @@ class UnusedPrivateMemberSpec : Spek({ } } - describe("suppress unused function warning annotations") { - it("does not report annotated private functions") { + @Nested + inner class `suppress unused function warning annotations` { + @Test + fun `does not report annotated private functions`() { val code = """ @Suppress("unused") private fun foo(): String = "" @@ -938,7 +1028,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("reports private functions without annotation") { + @Test + fun `reports private functions without annotation`() { val code = """ private fun foo(): String = "" """ @@ -949,7 +1040,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(findings[0].entity.signature).isEqualTo("Test.kt\$private fun foo(): String") } - it("does not report private functions in annotated class") { + @Test + fun `does not report private functions in annotated class`() { val code = """ @Suppress("unused") class Test { @@ -960,7 +1052,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report private functions in class with annotated outer class") { + @Test + fun `does not report private functions in class with annotated outer class`() { val code = """ @Suppress("unused") class Test { @@ -976,7 +1069,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report private functions in annotated file") { + @Test + fun `does not report private functions in annotated file`() { val code = """ @file:Suppress("unused") class Test { @@ -993,9 +1087,11 @@ class UnusedPrivateMemberSpec : Spek({ } } - describe("main methods") { + @Nested + inner class `main methods` { - it("does not report the args parameter of the main function inside an object") { + @Test + fun `does not report the args parameter of the main function inside an object`() { val code = """ object O { @@ -1008,7 +1104,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report the args parameter of the main function as top level function") { + @Test + fun `does not report the args parameter of the main function as top level function`() { val code = """ fun main(args: Array) { println("b") @@ -1018,9 +1115,11 @@ class UnusedPrivateMemberSpec : Spek({ } } - describe("operators") { + @Nested + inner class `operators` { - it("does not report used plus operator - #1354") { + @Test + fun `does not report used plus operator - #1354`() { val code = """ import java.util.Date class Foo { @@ -1033,7 +1132,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report used plus operator without type solving - #4242") { + @Test + fun `does not report used plus operator without type solving - #4242`() { val code = """ import java.util.Date class Foo { @@ -1046,7 +1146,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report used invoke operator without type solving - #4435") { + @Test + fun `does not report used invoke operator without type solving - #4435`() { val code = """ object Test { private operator fun invoke(i: Int): Int = i @@ -1059,7 +1160,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report used operator methods when used with the equal sign") { + @Test + fun `does not report used operator methods when used with the equal sign`() { val code = """ class Test { fun f() { @@ -1080,7 +1182,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report `contains` operator function that is used as `in`") { + @Test + fun `does not report 'contains' operator function that is used as 'in'`() { val code = """ class C { val isInside = "bar" in listOf("foo".toRegex()) @@ -1093,7 +1196,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report `contains` operator function that is used as `!in`") { + @Test + fun `does not report 'contains' operator function that is used as '!in'`() { val code = """ class C { val isInside = "bar" !in listOf("foo".toRegex()) @@ -1106,7 +1210,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("report unused minus operator") { + @Test + fun `report unused minus operator`() { val code = """ import java.util.Date class Foo { @@ -1119,9 +1224,11 @@ class UnusedPrivateMemberSpec : Spek({ } } - describe("same named functions") { + @Nested + inner class `same named functions` { - it("report it when the file has same named functions") { + @Test + fun `report it when the file has same named functions`() { val code = """ class Test { private fun f(): Int { @@ -1138,7 +1245,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(2) } - it("report it when the class has same named functions") { + @Test + fun `report it when the class has same named functions`() { val code = """ class Test { val value = f(1) @@ -1159,7 +1267,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(2) } - it("report it when the class has same named extension functions") { + @Test + fun `report it when the class has same named extension functions`() { val code = """ class Test { val value = 1.f() @@ -1181,9 +1290,11 @@ class UnusedPrivateMemberSpec : Spek({ } } - describe("operator functions - #2579") { + @Nested + inner class `operator functions - #2579` { - it("Does not report unused operators") { + @Test + fun `Does not report unused operators`() { val code = """ class Test { private operator fun Foo.plus(other: Foo): Foo = Foo(value + other.value) @@ -1196,7 +1307,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(0) } - it("Report unused operators") { + @Test + fun `Report unused operators`() { val code = """ class Test { private operator fun Foo.plus(other: Foo): Foo = Foo(value + other.value) @@ -1214,9 +1326,11 @@ class UnusedPrivateMemberSpec : Spek({ } } - describe("overloaded extension functions - #2579") { + @Nested + inner class `overloaded extension functions - #2579` { - it("Does not report used private extension functions") { + @Test + fun `Does not report used private extension functions`() { val code = """ class A class B @@ -1244,9 +1358,11 @@ class UnusedPrivateMemberSpec : Spek({ } } - describe("getValue/setValue operator functions - #3128") { + @Nested + inner class `getValue and setValue operator functions - #3128` { - it("does not report used private getValue/setValue operator functions") { + @Test + fun `does not report used private getValue and setValue operator functions`() { val code = """ import kotlin.reflect.KProperty @@ -1265,7 +1381,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(0) } - it("does not report getValue/setValue operator function parameters") { + @Test + fun `does not report getValue and setValue operator function parameters`() { val code = """ import kotlin.reflect.KProperty @@ -1282,7 +1399,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(0) } - it("reports unused private getValue/setValue operator functions") { + @Test + fun `reports unused private getValue and setValue operator functions`() { val code = """ import kotlin.reflect.KProperty @@ -1300,9 +1418,11 @@ class UnusedPrivateMemberSpec : Spek({ } } - describe("backtick identifiers - #3825") { + @Nested + inner class `backtick identifiers - #3825` { - it("does report unused variables with keyword name") { + @Test + fun `does report unused variables with keyword name`() { val code = """ fun main() { val `in` = "foo" @@ -1311,7 +1431,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("does not report used variables with keyword name") { + @Test + fun `does not report used variables with keyword name`() { val code = """ fun main() { val `in` = "fee" @@ -1322,7 +1443,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report used variables when referenced with backticks") { + @Test + fun `does not report used variables when referenced with backticks`() { val code = """ fun main() { val actual = "fee" @@ -1333,7 +1455,8 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report used variables when declared with backticks") { + @Test + fun `does not report used variables when declared with backticks`() { val code = """ fun main() { val `actual` = "fee" @@ -1345,8 +1468,10 @@ class UnusedPrivateMemberSpec : Spek({ } } - describe("list get overloaded operator function - #3640") { - it("report used private list get operator function - declared in a class - called by operator") { + @Nested + inner class `list get overloaded operator function - #3640` { + @Test + fun `report used private list get operator function - declared in a class - called by operator`() { val code = """ class StringWrapper( val s: String @@ -1359,7 +1484,9 @@ class UnusedPrivateMemberSpec : Spek({ """ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("doesn't report used private list get operator function - declared in a class - called by operator") { + + @Test + fun `doesn't report used private list get operator function - declared in a class - called by operator`() { val code = """ class StringWrapper( val s: String @@ -1376,7 +1503,9 @@ class UnusedPrivateMemberSpec : Spek({ """ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(0) } - it("doesn't report used private list get operator function - declared in a class - called by operator - multiple parameters") { + + @Test + fun `doesn't report used private list get operator function - declared in a class - called by operator - multiple parameters`() { val code = """ class StringWrapper( val s: String @@ -1393,7 +1522,9 @@ class UnusedPrivateMemberSpec : Spek({ """ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(0) } - it("doesn't report used private list get operator function - declared in a class - called directly") { + + @Test + fun `doesn't report used private list get operator function - declared in a class - called directly`() { val code = """ class StringWrapper( val s: String @@ -1410,7 +1541,9 @@ class UnusedPrivateMemberSpec : Spek({ """ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(0) } - it("report used private list get operator function - declared in a file - called by operator") { + + @Test + fun `report used private list get operator function - declared in a file - called by operator`() { val code = """ class StringWrapper( val s: String @@ -1421,7 +1554,9 @@ class UnusedPrivateMemberSpec : Spek({ """ assertThat(subject.lintWithContext(env, code)).hasSize(1) } - it("doesn't report used private list get operator function - declared in a file - called by operator") { + + @Test + fun `doesn't report used private list get operator function - declared in a file - called by operator`() { val code = """ class StringWrapper( val s: String @@ -1438,7 +1573,9 @@ class UnusedPrivateMemberSpec : Spek({ """ assertThat(subject.lintWithContext(env, code)).hasSize(0) } - it("doesn't report used private list get operator function - declared in a file - called directly") { + + @Test + fun `doesn't report used private list get operator function - declared in a file - called directly`() { val code = """ class StringWrapper( val s: String @@ -1456,4 +1593,4 @@ class UnusedPrivateMemberSpec : Spek({ assertThat(subject.lintWithContext(env, code)).hasSize(0) } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnusedPrivateParameterSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnusedPrivateParameterSpec.kt index 210dde064a28..15c626f1341d 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnusedPrivateParameterSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnusedPrivateParameterSpec.kt @@ -1,19 +1,21 @@ package io.gitlab.arturbosch.detekt.rules.style -import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.assertThat 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 - -class UnusedPrivateParameterSpec : Spek({ - setupKotlinEnvironment() - - val subject by memoized { UnusedPrivateMember() } - - describe("function parameters") { - it("reports single parameters if they are unused") { +import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test + +@KotlinCoreEnvironmentTest +class UnusedPrivateParameterSpec(val env: KotlinCoreEnvironment) { + val subject = UnusedPrivateMember() + + @Nested + inner class `function parameters` { + @Test + fun `reports single parameters if they are unused in public function`() { val code = """ fun function(unusedParameter: Int): Int { return 5 @@ -23,7 +25,8 @@ class UnusedPrivateParameterSpec : Spek({ assertThat(subject.lint(code)).hasSize(1) } - it("does not report single parameters if they used in return statement") { + @Test + fun `does not report single parameters if they used in return statement in public function`() { val code = """ fun function(used: Int): Int { return used @@ -33,7 +36,8 @@ class UnusedPrivateParameterSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report single parameters if they used in function") { + @Test + fun `does not report single parameters if they used in public function`() { val code = """ fun function(used: Int) { println(used) @@ -43,7 +47,8 @@ class UnusedPrivateParameterSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("reports parameters that are unused in return statement") { + @Test + fun `reports parameters that are unused in return statement in public function`() { val code = """ fun function(unusedParameter: Int, usedParameter: Int): Int { return usedParameter @@ -53,7 +58,8 @@ class UnusedPrivateParameterSpec : Spek({ assertThat(subject.lint(code)).hasSize(1) } - it("reports parameters that are unused in function") { + @Test + fun `reports parameters that are unused in public function`() { val code = """ fun function(unusedParameter: Int, usedParameter: Int) { println(usedParameter) @@ -63,7 +69,8 @@ class UnusedPrivateParameterSpec : Spek({ assertThat(subject.lint(code)).hasSize(1) } - it("reports single parameters if they are unused") { + @Test + fun `reports single parameters if they are unused in private function`() { val code = """ class Test { val value = usedMethod(1) @@ -77,7 +84,8 @@ class UnusedPrivateParameterSpec : Spek({ assertThat(subject.lint(code)).hasSize(1) } - it("reports two parameters if they are unused and called the same in different methods") { + @Test + fun `reports two parameters if they are unused and called the same in different methods`() { val code = """ class Test { val value = usedMethod(1) @@ -96,7 +104,8 @@ class UnusedPrivateParameterSpec : Spek({ assertThat(subject.lint(code)).hasSize(2) } - it("does not report single parameters if they used in return statement") { + @Test + fun `does not report single parameters if they used in return statement in private function`() { val code = """ class Test { val value = usedMethod(1) @@ -110,7 +119,8 @@ class UnusedPrivateParameterSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report single parameters if they used in function") { + @Test + fun `does not report single parameters if they used in private function`() { val code = """ class Test { val value = usedMethod(1) @@ -124,7 +134,8 @@ class UnusedPrivateParameterSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("reports parameters that are unused in return statement") { + @Test + fun `reports parameters that are unused in return statement in private function`() { val code = """ class Test { val value = usedMethod(1, 2) @@ -138,7 +149,8 @@ class UnusedPrivateParameterSpec : Spek({ assertThat(subject.lint(code)).hasSize(1) } - it("reports parameters that are unused in function") { + @Test + fun `reports parameters that are unused in private function`() { val code = """ class Test { val value = usedMethod(1, 2) @@ -153,15 +165,18 @@ class UnusedPrivateParameterSpec : Spek({ } } - describe("parameters in primary constructors") { - it("reports unused parameter") { + @Nested + inner class `parameters in primary constructors` { + @Test + fun `reports unused parameter`() { val code = """ class Test(unused: Any) """ assertThat(subject.lint(code)).hasSize(1) } - it("does not report used parameter for calling super") { + @Test + fun `does not report used parameter for calling super`() { val code = """ class Parent(val ignored: Any) class Test(used: Any) : Parent(used) @@ -169,7 +184,8 @@ class UnusedPrivateParameterSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report used parameter in init block") { + @Test + fun `does not report used parameter in init block`() { val code = """ class Test(used: Any) { init { @@ -180,7 +196,8 @@ class UnusedPrivateParameterSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report used parameter to initialize property") { + @Test + fun `does not report used parameter to initialize property`() { val code = """ class Test(used: Any) { val usedString = used.toString() @@ -190,8 +207,10 @@ class UnusedPrivateParameterSpec : Spek({ } } - describe("secondary parameters") { - it("report unused parameters in secondary constructors") { + @Nested + inner class `secondary parameters` { + @Test + fun `report unused parameters in secondary constructors`() { val code = """ private class ClassWithSecondaryConstructor { constructor(used: Any, unused: Any) { @@ -206,8 +225,10 @@ class UnusedPrivateParameterSpec : Spek({ } } - describe("suppress unused parameter warning annotations") { - it("does not report annotated parameters") { + @Nested + inner class `suppress unused parameter warning annotations` { + @Test + fun `does not report annotated parameters`() { val code = """ fun foo(@Suppress("UNUSED_PARAMETER") unused: String){} """ @@ -215,7 +236,8 @@ class UnusedPrivateParameterSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("reports parameters without annotation") { + @Test + fun `reports parameters without annotation`() { val code = """ fun foo(@Suppress("UNUSED_PARAMETER") unused: String, unusedWithoutAnnotation: String){} """ @@ -226,7 +248,8 @@ class UnusedPrivateParameterSpec : Spek({ assertThat(lint[0].entity.signature).isEqualTo("Test.kt\$unusedWithoutAnnotation: String") } - it("does not report parameters in annotated function") { + @Test + fun `does not report parameters in annotated function`() { val code = """ @Suppress("UNUSED_PARAMETER") fun foo(unused: String, otherUnused: String){} @@ -235,7 +258,8 @@ class UnusedPrivateParameterSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report parameters in annotated class") { + @Test + fun `does not report parameters in annotated class`() { val code = """ @Suppress("UNUSED_PARAMETER") class Test { @@ -247,7 +271,8 @@ class UnusedPrivateParameterSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report parameters in annotated object") { + @Test + fun `does not report parameters in annotated object`() { val code = """ @Suppress("UNUSED_PARAMETER") object Test { @@ -258,7 +283,8 @@ class UnusedPrivateParameterSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report parameters in class with annotated outer class") { + @Test + fun `does not report parameters in class with annotated outer class`() { val code = """ @Suppress("UNUSED_PARAMETER") class Test { @@ -273,7 +299,8 @@ class UnusedPrivateParameterSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report parameters in annotated file") { + @Test + fun `does not report parameters in annotated file`() { val code = """ @file:Suppress("UNUSED_PARAMETER") @@ -289,4 +316,4 @@ class UnusedPrivateParameterSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseAnyOrNoneInsteadOfFindSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseAnyOrNoneInsteadOfFindSpec.kt index 14221bce65fd..1aa932300ea6 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseAnyOrNoneInsteadOfFindSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseAnyOrNoneInsteadOfFindSpec.kt @@ -1,84 +1,110 @@ package io.gitlab.arturbosch.detekt.rules.style -import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext import org.assertj.core.api.Assertions.assertThat import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -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 -class UseAnyOrNoneInsteadOfFindSpec : Spek({ - setupKotlinEnvironment() +@KotlinCoreEnvironmentTest +class UseAnyOrNoneInsteadOfFindSpec(val env: KotlinCoreEnvironment) { + val subject = UseAnyOrNoneInsteadOfFind() - val env: KotlinCoreEnvironment by memoized() - val subject by memoized { UseAnyOrNoneInsteadOfFind() } - - describe("UseAnyOrNoneInsteadOfFind rule") { - it("Reports collections.find != null") { + @Nested + inner class `UseAnyOrNoneInsteadOfFind rule` { + @Test + @DisplayName("Reports collections.find != null") + fun reportCollectionsFindNotEqualToNull() { val code = "val x = listOf(1, 2, 3).find { it == 4 } != null" val actual = subject.compileAndLintWithContext(env, code) assertThat(actual).hasSize(1) assertThat(actual[0].message).isEqualTo("Use 'any' instead of 'find'") } - it("Reports sequences.find != null") { + + @Test + @DisplayName("Reports sequences.find != null") + fun reportSequencesFindNotEqualToNull() { val code = "val x = sequenceOf(1, 2, 3).find { it == 4 } != null" val actual = subject.compileAndLintWithContext(env, code) assertThat(actual).hasSize(1) } - it("Reports text.find != null") { + + @Test + @DisplayName("Reports text.find != null") + fun reportTextFindNotEqualToNull() { val code = "val x = \"123\".find { it == '4' } != null" val actual = subject.compileAndLintWithContext(env, code) assertThat(actual).hasSize(1) } - it("Reports collections.firstOrNull != null") { + @Test + @DisplayName("Reports collections.firstOrNull != null") + fun reportCollectionsFirstOrNullNotEqualToNull() { val code = "val x = arrayOf(1, 2, 3).firstOrNull { it == 4 } != null" val actual = subject.compileAndLintWithContext(env, code) assertThat(actual).hasSize(1) assertThat(actual[0].message).isEqualTo("Use 'any' instead of 'firstOrNull'") } - it("Reports sequences.firstOrNull != null") { + + @Test + @DisplayName("Reports sequences.firstOrNull != null") + fun reportSequencesFirstOrNullNotEqualToNull() { val code = "val x = sequenceOf(1, 2, 3).firstOrNull { it == 4 } != null" val actual = subject.compileAndLintWithContext(env, code) assertThat(actual).hasSize(1) } - it("Reports text.firstOrNull != null") { + + @Test + @DisplayName("Reports text.firstOrNull != null") + fun reportTextFirstOrNullNotEqualToNull() { val code = "val x = \"123\".firstOrNull { it == '4' } != null" val actual = subject.compileAndLintWithContext(env, code) assertThat(actual).hasSize(1) } - it("Reports collections.find == null") { + @Test + @DisplayName("Reports collections.find == null") + fun reportCollectionsFindEqualToNull() { val code = "val x = setOf(1, 2, 3).find { it == 4 } == null" val actual = subject.compileAndLintWithContext(env, code) assertThat(actual).hasSize(1) assertThat(actual[0].message).isEqualTo("Use 'none' instead of 'find'") } - it("Reports null != collections.find") { + @Test + @DisplayName("Reports null != collections.find") + fun reportNullNotEqualToCollectionsFind() { val code = "val x = null != listOf(1, 2, 3).find { it == 4 }" val actual = subject.compileAndLintWithContext(env, code) assertThat(actual).hasSize(1) assertThat(actual[0].message).isEqualTo("Use 'any' instead of 'find'") } - it("Reports collections.find != null in extension") { + @Test + @DisplayName("Reports collections.find != null in extension") + fun reportCollectionsFindNotEqualToNullInExtension() { val code = "fun List.test(): Boolean = find { it == 4 } != null" val actual = subject.compileAndLintWithContext(env, code) assertThat(actual).hasSize(1) } - it("Reports collections.lastOrNull != null") { + @Test + @DisplayName("Reports collections.lastOrNull != null") + fun reportCollectionsLastOrNullNotEqualToNull() { val code = "val x = listOf(1, 2, 3).lastOrNull { it == 4 } != null" val actual = subject.compileAndLintWithContext(env, code) assertThat(actual).hasSize(1) assertThat(actual[0].message).isEqualTo("Use 'any' instead of 'lastOrNull'") } - it("Does not report collections.find") { + @Test + @DisplayName("Does not report collections.find") + fun noReportCollectionsFind() { val code = "val x = listOf(1, 2, 3).find { it == 4 }" val actual = subject.compileAndLintWithContext(env, code) assertThat(actual).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseArrayLiteralsInAnnotationsSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseArrayLiteralsInAnnotationsSpec.kt index c79bbb720c04..a8613ac75a6a 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseArrayLiteralsInAnnotationsSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseArrayLiteralsInAnnotationsSpec.kt @@ -2,16 +2,20 @@ package io.gitlab.arturbosch.detekt.rules.style import io.gitlab.arturbosch.detekt.test.assertThat import io.gitlab.arturbosch.detekt.test.compileAndLint -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 -class UseArrayLiteralsInAnnotationsSpec : Spek({ +class UseArrayLiteralsInAnnotationsSpec { - val subject by memoized { UseArrayLiteralsInAnnotations() } + val subject = UseArrayLiteralsInAnnotations() - describe("suggests replacing arrayOf with [] syntax") { + @Nested + @DisplayName("suggests replacing arrayOf with [] syntax`") + inner class ReplaceArrayOfWithSquareBrackets { - it("finds an arrayOf usage") { + @Test + fun `finds an arrayOf usage`() { val findings = subject.compileAndLint( """ annotation class Test(val values: Array) @@ -23,7 +27,9 @@ class UseArrayLiteralsInAnnotationsSpec : Spek({ assertThat(findings).hasSize(1) } - it("expects [] syntax") { + @Test + @DisplayName("expects [] syntax") + fun expectsBracketSyntax() { val findings = subject.compileAndLint( """ annotation class Test(val values: Array) @@ -35,4 +41,4 @@ class UseArrayLiteralsInAnnotationsSpec : Spek({ assertThat(findings).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseCheckNotNullSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseCheckNotNullSpec.kt index d2d8508404a2..749002413a2c 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseCheckNotNullSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseCheckNotNullSpec.kt @@ -1,20 +1,20 @@ package io.gitlab.arturbosch.detekt.rules.style -import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext import org.assertj.core.api.Assertions.assertThat import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -import org.spekframework.spek2.Spek -import org.spekframework.spek2.style.specification.describe +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test -object UseCheckNotNullSpec : Spek({ - setupKotlinEnvironment() +@KotlinCoreEnvironmentTest +class UseCheckNotNullSpec(val env: KotlinCoreEnvironment) { + val subject = UseCheckNotNull() - val env: KotlinCoreEnvironment by memoized() - val subject by memoized { UseCheckNotNull() } - - describe("UseCheckNotNull rule") { - it("reports `check` calls with a non-null check") { + @Nested + inner class `UseCheckNotNull rule` { + @Test + fun `reports 'check' calls with a non-null check`() { val code = """ fun test(i: Int?) { check(i != null) @@ -24,7 +24,8 @@ object UseCheckNotNullSpec : Spek({ assertThat(actual).hasSize(1) } - it("reports `check` calls with a non-null check that has `null` on the left side") { + @Test + fun `reports 'check' calls with a non-null check that has 'null' on the left side`() { val code = """ fun test(i: Int?) { check(null != i) @@ -34,7 +35,8 @@ object UseCheckNotNullSpec : Spek({ assertThat(actual).hasSize(1) } - it("does not report a `check` call without a non-null check") { + @Test + fun `does not report a 'check' call without a non-null check`() { val code = """ fun test(i: Int) { check(i > 0) @@ -44,4 +46,4 @@ object UseCheckNotNullSpec : Spek({ assertThat(actual).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseCheckOrErrorSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseCheckOrErrorSpec.kt index 35453853a415..7dfedc755abf 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseCheckOrErrorSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseCheckOrErrorSpec.kt @@ -1,24 +1,24 @@ package io.gitlab.arturbosch.detekt.rules.style import io.gitlab.arturbosch.detekt.api.Config -import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.assertThat import io.gitlab.arturbosch.detekt.test.compileAndLint import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext import io.gitlab.arturbosch.detekt.test.lint import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -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 UseCheckOrErrorSpec : Spek({ - setupKotlinEnvironment() +@KotlinCoreEnvironmentTest +class UseCheckOrErrorSpec(val env: KotlinCoreEnvironment) { + val subject = UseCheckOrError(Config.empty) - val env: KotlinCoreEnvironment by memoized() - val subject by memoized { UseCheckOrError(Config.empty) } + @Nested + inner class `UseCheckOrError rule` { - describe("UseCheckOrError rule") { - - it("reports if a an IllegalStateException is thrown") { + @Test + fun `reports if a an IllegalStateException is thrown`() { val code = """ fun x() { doSomething() @@ -28,7 +28,8 @@ class UseCheckOrErrorSpec : Spek({ assertThat(subject.lint(code)).hasSourceLocation(3, 16) } - it("reports if a an IllegalStateException is thrown with an error message") { + @Test + fun `reports if a an IllegalStateException is thrown with an error message`() { val code = """ fun x() { doSomething() @@ -38,7 +39,8 @@ class UseCheckOrErrorSpec : Spek({ assertThat(subject.lint(code)).hasSourceLocation(3, 16) } - it("reports if a an IllegalStateException is thrown as default case of a when expression") { + @Test + fun `reports if a an IllegalStateException is thrown as default case of a when expression`() { val code = """ fun x(a: Int) = when (a) { @@ -49,7 +51,8 @@ class UseCheckOrErrorSpec : Spek({ assertThat(subject.lint(code)).hasSourceLocation(4, 17) } - it("reports if an IllegalStateException is thrown by its fully qualified name") { + @Test + fun `reports if an IllegalStateException is thrown by its fully qualified name`() { val code = """ fun x() { doSomething() @@ -59,7 +62,8 @@ class UseCheckOrErrorSpec : Spek({ assertThat(subject.lint(code)).hasSourceLocation(3, 16) } - it("reports if an IllegalStateException is thrown by its fully qualified name using the kotlin type alias") { + @Test + fun `reports if an IllegalStateException is thrown by its fully qualified name using the kotlin type alias`() { val code = """ fun x() { doSomething() @@ -69,7 +73,8 @@ class UseCheckOrErrorSpec : Spek({ assertThat(subject.lint(code)).hasSourceLocation(3, 16) } - it("does not report if any other kind of exception is thrown") { + @Test + fun `does not report if any other kind of exception is thrown`() { val code = """ fun x() { doSomething() @@ -79,7 +84,8 @@ class UseCheckOrErrorSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report an issue if the exception thrown has a message and a cause") { + @Test + fun `does not report an issue if the exception thrown has a message and a cause`() { val code = """ private fun missing(): Nothing { if (cause != null) { @@ -90,7 +96,8 @@ class UseCheckOrErrorSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report an issue if the exception thrown as the only action in a block") { + @Test + fun `does not report an issue if the exception thrown as the only action in a block`() { val code = """ fun unsafeRunSync(): A = unsafeRunTimed(Duration.INFINITE) @@ -99,17 +106,20 @@ class UseCheckOrErrorSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("reports an issue if the exception thrown as the only action in a function") { + @Test + fun `reports an issue if the exception thrown as the only action in a function`() { val code = """fun doThrow() = throw IllegalStateException("message")""" assertThat(subject.lint(code)).hasSourceLocation(1, 17) } - it("reports an issue if the exception thrown as the only action in a function block") { + @Test + fun `reports an issue if the exception thrown as the only action in a function block`() { val code = """fun doThrow() { throw IllegalStateException("message") }""" assertThat(subject.lint(code)).hasSourceLocation(1, 17) } - it("does not report if the exception thrown has a non-String argument") { + @Test + fun `does not report if the exception thrown has a non-String argument`() { val code = """ fun test(throwable: Throwable) { when(throwable) { @@ -121,7 +131,8 @@ class UseCheckOrErrorSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report if the exception thrown has a String literal argument and a non-String argument") { + @Test + fun `does not report if the exception thrown has a String literal argument and a non-String argument`() { val code = """ fun test(throwable: Throwable) { when(throwable) { @@ -133,7 +144,8 @@ class UseCheckOrErrorSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report if the exception thrown has a non-String literal argument") { + @Test + fun `does not report if the exception thrown has a non-String literal argument`() { val code = """ fun test(throwable: Throwable) { when(throwable) { @@ -145,9 +157,11 @@ class UseCheckOrErrorSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - context("with binding context") { + @Nested + inner class `with binding context` { - it("does not report if the exception thrown has a non-String argument") { + @Test + fun `does not report if the exception thrown has a non-String argument`() { val code = """ fun test(throwable: Throwable) { when(throwable) { @@ -159,7 +173,8 @@ class UseCheckOrErrorSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report if the exception thrown has a String literal argument and a non-String argument") { + @Test + fun `does not report if the exception thrown has a String literal argument and a non-String argument`() { val code = """ fun test(throwable: Throwable) { when(throwable) { @@ -171,7 +186,8 @@ class UseCheckOrErrorSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("reports if the exception thrown has a non-String literal argument") { + @Test + fun `reports if the exception thrown has a non-String literal argument`() { val code = """ fun test(throwable: Throwable) { when(throwable) { @@ -183,7 +199,8 @@ class UseCheckOrErrorSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("reports if the exception thrown has a string literal argument") { + @Test + fun `reports if the exception thrown has a string literal argument`() { val code = """ fun test(throwable: Throwable) { when(throwable) { @@ -196,4 +213,4 @@ class UseCheckOrErrorSpec : Spek({ } } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseDataClassSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseDataClassSpec.kt index 7a2602545202..d9439703ef6a 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseDataClassSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseDataClassSpec.kt @@ -1,30 +1,31 @@ package io.gitlab.arturbosch.detekt.rules.style import io.gitlab.arturbosch.detekt.api.Config -import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.TestConfig import io.gitlab.arturbosch.detekt.test.assertThat import io.gitlab.arturbosch.detekt.test.compileAndLint import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext import io.gitlab.arturbosch.detekt.test.lint import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -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 ALLOW_VARS = "allowVars" private const val EXCLUDE_ANNOTATED_CLASSES = "excludeAnnotatedClasses" -class UseDataClassSpec : Spek({ - setupKotlinEnvironment() +@KotlinCoreEnvironmentTest +class UseDataClassSpec(val env: KotlinCoreEnvironment) { + val subject = UseDataClass(Config.empty) - val env: KotlinCoreEnvironment by memoized() - val subject by memoized { UseDataClass(Config.empty) } + @Nested + inner class `UseDataClass rule` { - describe("UseDataClass rule") { + @Nested + inner class `does not report invalid data class candidates` { - describe("does not report invalid data class candidates") { - - it("does not report a valid class") { + @Test + fun `does not report a valid class`() { val code = """ class NoDataClassCandidate(val i: Int) { val i2: Int = 0 @@ -37,7 +38,8 @@ class UseDataClassSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report a candidate class with additional method") { + @Test + fun `does not report a candidate class with additional method`() { val code = """ class NoDataClassCandidateWithAdditionalMethod(val i: Int) { fun f1() { @@ -48,14 +50,16 @@ class UseDataClassSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report a candidate class with a private constructor") { + @Test + fun `does not report a candidate class with a private constructor`() { val code = """ class NoDataClassCandidateWithOnlyPrivateCtor1 private constructor(val i: Int) """ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report a candidate class with a private explicit constructor") { + @Test + fun `does not report a candidate class with a private explicit constructor`() { val code = """ class NoDataClassCandidateWithOnlyPrivateCtor2 { private constructor(i: Int) @@ -64,7 +68,8 @@ class UseDataClassSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report a candidate sealed class") { + @Test + fun `does not report a candidate sealed class`() { val code = """ sealed class NoDataClassBecauseItsSealed { data class Success(val any: Any) : NoDataClassBecauseItsSealed() @@ -74,7 +79,8 @@ class UseDataClassSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report a candidate enum class") { + @Test + fun `does not report a candidate enum class`() { val code = """ enum class EnumNoDataClass(val i: Int) { FIRST(1), SECOND(2); @@ -83,14 +89,16 @@ class UseDataClassSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report a candidate annotation class") { + @Test + fun `does not report a candidate annotation class`() { val code = """ annotation class AnnotationNoDataClass(val i: Int) """ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report a candidate with an interface that has methods") { + @Test + fun `does not report a candidate with an interface that has methods`() { val code = """ interface SomeInterface { fun foo(): Int @@ -104,7 +112,8 @@ class UseDataClassSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report an existing data class candidate with an interface") { + @Test + fun `does not report an existing data class candidate with an interface`() { val code = """ interface SimpleInterface { val i: Int @@ -116,7 +125,8 @@ class UseDataClassSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report a class extending a class and implementing an interface") { + @Test + fun `does not report a class extending a class and implementing an interface`() { val code = """ interface SimpleInterface { val i: Int @@ -130,7 +140,8 @@ class UseDataClassSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report a class with delegating interface") { + @Test + fun `does not report a class with delegating interface`() { val code = """ interface I class B() : I @@ -141,16 +152,19 @@ class UseDataClassSpec : Spek({ } } - describe("does report data class candidates") { + @Nested + inner class `does report data class candidates` { - it("does report a data class candidate") { + @Test + fun `does report a data class candidate`() { val code = """ class DataClassCandidate1(val i: Int) """ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("does report a candidate class with extra property") { + @Test + fun `does report a candidate class with extra property`() { val code = """ class DataClassCandidateWithProperties(val i: Int) { val i2: Int = 0 @@ -159,7 +173,8 @@ class UseDataClassSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("does report a candidate class with extra public constructor") { + @Test + fun `does report a candidate class with extra public constructor`() { val code = """ class DataClassCandidate2(val s: String) { private constructor(i: Int) : this(i.toString()) @@ -168,7 +183,8 @@ class UseDataClassSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("does report a candidate class with both a private and public constructor") { + @Test + fun `does report a candidate class with both a private and public constructor`() { val code = """ class DataClassCandidate3 private constructor(val s: String) { constructor(i: Int) : this(i.toString()) @@ -177,7 +193,8 @@ class UseDataClassSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("does report a candidate class with overridden data class methods") { + @Test + fun `does report a candidate class with overridden data class methods`() { val code = """ class DataClassCandidateWithOverriddenMethods(val i: Int) { override fun equals(other: Any?): Boolean { @@ -194,7 +211,8 @@ class UseDataClassSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("does report a candidate class with a simple interface extension") { + @Test + fun `does report a candidate class with a simple interface extension`() { val code = """ interface SimpleInterface class DataClass(val i: Int): SimpleInterface @@ -203,7 +221,8 @@ class UseDataClassSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("does report a candidate class with an interface extension that overrides vals") { + @Test + fun `does report a candidate class with an interface extension that overrides vals`() { val code = """ interface SimpleInterface { val i: Int @@ -216,9 +235,11 @@ class UseDataClassSpec : Spek({ } } - describe("copy method") { + @Nested + inner class `copy method` { - it("does report with copy method") { + @Test + fun `does report with copy method`() { val code = """ class D(val a: Int, val b: String) { fun copy(a: Int, b: String): D = D(a, b) @@ -227,7 +248,8 @@ class UseDataClassSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("does report with copy method which has an implicit return type") { + @Test + fun `does report with copy method which has an implicit return type`() { val code = """ class D(val a: Int, val b: String) { fun copy(a: Int, b: String) = D(a, b) @@ -236,7 +258,8 @@ class UseDataClassSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("does not report with copy method which has no parameters") { + @Test + fun `does not report with copy method which has no parameters`() { val code = """ class D(val a: Int, val b: String) { fun copy(): D = D(0, "") @@ -245,7 +268,8 @@ class UseDataClassSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report with copy method which has more parameters than the primary constructor") { + @Test + fun `does not report with copy method which has more parameters than the primary constructor`() { val code = """ class D(val a: Int, val b: String) { fun copy(a: Int, b: String, c: String): D = D(a, b + c) @@ -254,7 +278,8 @@ class UseDataClassSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report with copy method which has different parameter types") { + @Test + fun `does not report with copy method which has different parameter types`() { val code = """ class D(val a: Int, val b: String) { fun copy(a: Int, b: Int): D = D(a, b.toString()) @@ -263,7 +288,8 @@ class UseDataClassSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report with copy method which has different parameter types 2") { + @Test + fun `does not report with copy method which has different parameter types 2`() { val code = """ class D(val a: Int, val b: String) { fun copy(a: Int, b: String?): D = D(a, b.toString()) @@ -272,7 +298,8 @@ class UseDataClassSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report with copy method which has a different return type") { + @Test + fun `does not report with copy method which has a different return type`() { val code = """ class D(val a: Int, val b: String) { fun copy(a: Int, b: String) { @@ -283,15 +310,18 @@ class UseDataClassSpec : Spek({ } } - describe("does report class with vars and allowVars") { + @Nested + inner class `does report class with vars and allowVars` { - it("does not report class with mutable constructor parameter") { + @Test + fun `does not report class with mutable constructor parameter`() { val code = """class DataClassCandidateWithVar(var i: Int)""" val config = TestConfig(mapOf(ALLOW_VARS to "true")) assertThat(UseDataClass(config).compileAndLint(code)).isEmpty() } - it("does not report class with mutable properties") { + @Test + fun `does not report class with mutable properties`() { val code = """ class DataClassCandidateWithProperties(var i: Int) { var i2: Int = 0 @@ -301,7 +331,8 @@ class UseDataClassSpec : Spek({ assertThat(UseDataClass(config).compileAndLint(code)).isEmpty() } - it("does not report class with both mutable property and immutable parameters") { + @Test + fun `does not report class with both mutable property and immutable parameters`() { val code = """ class DataClassCandidateWithMixedProperties(val i: Int) { var i2: Int = 0 @@ -311,7 +342,8 @@ class UseDataClassSpec : Spek({ assertThat(UseDataClass(config).compileAndLint(code)).isEmpty() } - it("does not report class with both mutable parameter and immutable property") { + @Test + fun `does not report class with both mutable parameter and immutable property`() { val code = """ class DataClassCandidateWithMixedProperties(var i: Int) { val i2: Int = 0 @@ -322,11 +354,13 @@ class UseDataClassSpec : Spek({ } } - it("does not report inline classes") { + @Test + fun `does not report inline classes`() { assertThat(subject.lint("inline class A(val x: Int)")).isEmpty() } - it("does not report value classes") { + @Test + fun `does not report value classes`() { val code = """ @JvmInline value class A(val x: Int) @@ -334,7 +368,8 @@ class UseDataClassSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report a class which has an ignored annotation") { + @Test + fun `does not report a class which has an ignored annotation`() { val code = """ import kotlin.SinceKotlin @@ -345,7 +380,8 @@ class UseDataClassSpec : Spek({ assertThat(UseDataClass(config).compileAndLint(code)).isEmpty() } - it("does not report a class with a delegated property") { + @Test + fun `does not report a class with a delegated property`() { val code = """ import kotlin.properties.Delegates class C(val i: Int) { @@ -357,7 +393,8 @@ class UseDataClassSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("reports class with nested delegation") { + @Test + fun `reports class with nested delegation`() { val code = """ import kotlin.properties.Delegates class C(val i: Int) { @@ -371,7 +408,8 @@ class UseDataClassSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("does not report inner classes") { + @Test + fun `does not report inner classes`() { val code = """ class Outer { inner class Inner(val x: Int) @@ -380,4 +418,4 @@ class UseDataClassSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseEmptyCounterpartSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseEmptyCounterpartSpec.kt index 88365618aeea..ad4383260783 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseEmptyCounterpartSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseEmptyCounterpartSpec.kt @@ -1,22 +1,22 @@ package io.gitlab.arturbosch.detekt.rules.style import io.gitlab.arturbosch.detekt.api.Config -import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.assertThat import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -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 UseEmptyCounterpartSpec : Spek({ - setupKotlinEnvironment() +@KotlinCoreEnvironmentTest +class UseEmptyCounterpartSpec(val env: KotlinCoreEnvironment) { + val rule = UseEmptyCounterpart(Config.empty) - val env: KotlinCoreEnvironment by memoized() - val rule by memoized { UseEmptyCounterpart(Config.empty) } + @Nested + inner class `UseEmptyCounterpart rule` { - describe("UseEmptyCounterpart rule") { - - it("reports no-arg instantiation") { + @Test + fun `reports no-arg instantiation`() { val code = """ val array = arrayOf() val list = listOf() @@ -28,7 +28,8 @@ class UseEmptyCounterpartSpec : Spek({ assertThat(rule.compileAndLintWithContext(env, code)).hasSize(6) } - it("reports no-arg instantiation with inferred type parameters") { + @Test + fun `reports no-arg instantiation with inferred type parameters`() { val code = """ val array: Array = arrayOf() val list: List = listOf() @@ -40,7 +41,8 @@ class UseEmptyCounterpartSpec : Spek({ assertThat(rule.compileAndLintWithContext(env, code)).hasSize(6) } - it("does not report empty instantiation") { + @Test + fun `does not report empty instantiation`() { val code = """ val array = emptyArray() val list = emptyList() @@ -51,7 +53,8 @@ class UseEmptyCounterpartSpec : Spek({ assertThat(rule.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report instantiation with arguments") { + @Test + fun `does not report instantiation with arguments`() { val code = """ val array = arrayOf(0) val list = listOf(0) @@ -63,7 +66,8 @@ class UseEmptyCounterpartSpec : Spek({ assertThat(rule.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report no-arg custom function with same name as function with empty counterpart") { + @Test + fun `does not report no-arg custom function with same name as function with empty counterpart`() { val code = """ fun arrayOf(): Array = TODO() fun listOf(): List = TODO() @@ -82,4 +86,4 @@ class UseEmptyCounterpartSpec : Spek({ assertThat(rule.compileAndLintWithContext(env, code)).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseIfEmptyOrIfBlankSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseIfEmptyOrIfBlankSpec.kt index 8109b06b8ef1..539199c3f69c 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseIfEmptyOrIfBlankSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseIfEmptyOrIfBlankSpec.kt @@ -1,19 +1,22 @@ package io.gitlab.arturbosch.detekt.rules.style -import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.assertThat import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -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 -class UseIfEmptyOrIfBlankSpec : Spek({ - setupKotlinEnvironment() - val env: KotlinCoreEnvironment by memoized() - val subject by memoized { UseIfEmptyOrIfBlank() } +@KotlinCoreEnvironmentTest +class UseIfEmptyOrIfBlankSpec(val env: KotlinCoreEnvironment) { + val subject = UseIfEmptyOrIfBlank() - describe("report UseIfEmptyOrIfBlank rule") { - it("String.isBlank") { + @Nested + inner class `report UseIfEmptyOrIfBlank rule` { + @Test + @DisplayName("String.isBlank") + fun stringIsBlank() { val code = """ class Api(val name: String) @@ -27,7 +30,9 @@ class UseIfEmptyOrIfBlankSpec : Spek({ assertThat(findings[0]).hasMessage("This 'isBlank' call can be replaced with 'ifBlank'") } - it("String.isNotBlank") { + @Test + @DisplayName("String.isNotBlank") + fun stringIsNotBlank() { val code = """ class Api(val name: String) @@ -44,7 +49,9 @@ class UseIfEmptyOrIfBlankSpec : Spek({ assertThat(findings[0]).hasMessage("This 'isNotBlank' call can be replaced with 'ifBlank'") } - it("String.isEmpty") { + @Test + @DisplayName("String.isEmpty") + fun stringIsEmpty() { val code = """ class Api(val name: String) @@ -58,7 +65,9 @@ class UseIfEmptyOrIfBlankSpec : Spek({ assertThat(findings[0]).hasMessage("This 'isEmpty' call can be replaced with 'ifEmpty'") } - it("String.isNotEmpty") { + @Test + @DisplayName("String.isNotEmpty") + fun stringIsNotEmpty() { val code = """ class Api(val name: String) @@ -75,7 +84,9 @@ class UseIfEmptyOrIfBlankSpec : Spek({ assertThat(findings[0]).hasMessage("This 'isNotEmpty' call can be replaced with 'ifEmpty'") } - it("List.isEmpty") { + @Test + @DisplayName("List.isEmpty") + fun listIsEmpty() { val code = """ fun test(list: List): List { return if (list.isEmpty()) { @@ -89,7 +100,9 @@ class UseIfEmptyOrIfBlankSpec : Spek({ assertThat(findings).hasSize(1) } - it("List.isNotEmpty") { + @Test + @DisplayName("List.isNotEmpty") + fun listIsNotEmpty() { val code = """ fun test(list: List): List { return if (list.isNotEmpty()) { @@ -103,7 +116,9 @@ class UseIfEmptyOrIfBlankSpec : Spek({ assertThat(findings).hasSize(1) } - it("Set.isEmpty") { + @Test + @DisplayName("Set.isEmpty") + fun setIsEmpty() { val code = """ fun test(set: Set): Set { return if (set.isEmpty()) { @@ -117,7 +132,9 @@ class UseIfEmptyOrIfBlankSpec : Spek({ assertThat(findings).hasSize(1) } - it("Set.isNotEmpty") { + @Test + @DisplayName("Set.isNotEmpty") + fun setIsNotEmpty() { val code = """ fun test(set: Set): Set { return if (set.isNotEmpty()) { @@ -131,7 +148,9 @@ class UseIfEmptyOrIfBlankSpec : Spek({ assertThat(findings).hasSize(1) } - it("Map.isEmpty") { + @Test + @DisplayName("Map.isEmpty") + fun mapIsEmpty() { val code = """ fun test(map: Map): Map { return if (map.isEmpty()) { @@ -145,7 +164,9 @@ class UseIfEmptyOrIfBlankSpec : Spek({ assertThat(findings).hasSize(1) } - it("Map.isNotEmpty") { + @Test + @DisplayName("Map.isNotEmpty") + fun mapIsNotEmpty() { val code = """ fun test(map: Map): Map { return if (map.isNotEmpty()) { @@ -159,7 +180,9 @@ class UseIfEmptyOrIfBlankSpec : Spek({ assertThat(findings).hasSize(1) } - it("Collection.isEmpty") { + @Test + @DisplayName("Collection.isEmpty") + fun collectionIsEmpty() { val code = """ fun test(collection: Collection): Collection { return if (collection.isEmpty()) { @@ -173,7 +196,9 @@ class UseIfEmptyOrIfBlankSpec : Spek({ assertThat(findings).hasSize(1) } - it("Collection.isNotEmpty") { + @Test + @DisplayName("Collection.isNotEmpty") + fun collectionIsNotEmpty() { val code = """ fun test(collection: Collection): Collection { return if (collection.isNotEmpty()) { @@ -187,7 +212,8 @@ class UseIfEmptyOrIfBlankSpec : Spek({ assertThat(findings).hasSize(1) } - it("implicit receiver") { + @Test + fun `implicit receiver`() { val code = """ fun String.test(): String { return if (isBlank()) { @@ -201,7 +227,8 @@ class UseIfEmptyOrIfBlankSpec : Spek({ assertThat(findings).hasSize(1) } - it("default value block is not single statement") { + @Test + fun `default value block is not single statement`() { val code = """ fun test(list: List): List { return if (list.isEmpty()) { @@ -216,7 +243,8 @@ class UseIfEmptyOrIfBlankSpec : Spek({ assertThat(findings).hasSize(1) } - it("!isEmpty") { + @Test + fun `!isEmpty`() { val code = """ fun test(list: List): List { return if (!list.isEmpty()) { // list.isNotEmpty() @@ -231,7 +259,8 @@ class UseIfEmptyOrIfBlankSpec : Spek({ assertThat(findings[0]).hasMessage("This 'isEmpty' call can be replaced with 'ifEmpty'") } - it("!isNotEmpty") { + @Test + fun `!isNotEmpty`() { val code = """ fun test(list: List): List { return if (!list.isNotEmpty()) { // list.isEmpty() @@ -247,8 +276,11 @@ class UseIfEmptyOrIfBlankSpec : Spek({ } } - describe("does not report UseIfEmptyOrIfBlank rule") { - it("String.isNullOrBlank") { + @Nested + inner class `does not report UseIfEmptyOrIfBlank rule` { + @Test + @DisplayName("String.isNullOrBlank") + fun stringIsNulLOrBlank() { val code = """ class Api(val name: String?) @@ -260,7 +292,9 @@ class UseIfEmptyOrIfBlankSpec : Spek({ assertThat(findings).isEmpty() } - it("List.isNullOrEmpty") { + @Test + @DisplayName("List.isNullOrEmpty") + fun listIsNullOrEmpty() { val code = """ fun test2(list: List): List { return if (list.isNullOrEmpty()) { @@ -274,7 +308,9 @@ class UseIfEmptyOrIfBlankSpec : Spek({ assertThat(findings).isEmpty() } - it("Array.isEmpty") { + @Test + @DisplayName("Array.isEmpty") + fun arrayIsEmpty() { val code = """ fun test(arr: Array): Array { return if (arr.isEmpty()) { @@ -288,7 +324,9 @@ class UseIfEmptyOrIfBlankSpec : Spek({ assertThat(findings).isEmpty() } - it("Array.isNotEmpty") { + @Test + @DisplayName("Array.isNotEmpty") + fun arrayIsNotEmpty() { val code = """ fun test(arr: Array): Array { return if (arr.isNotEmpty()) { @@ -302,7 +340,9 @@ class UseIfEmptyOrIfBlankSpec : Spek({ assertThat(findings).isEmpty() } - it("IntArray.isEmpty") { + @Test + @DisplayName("IntArray.isEmpty") + fun intArrayIsEmpty() { val code = """ fun test(arr: IntArray): IntArray { return if (arr.isEmpty()) { @@ -316,7 +356,9 @@ class UseIfEmptyOrIfBlankSpec : Spek({ assertThat(findings).isEmpty() } - it("IntArray.isNotEmpty") { + @Test + @DisplayName("IntArray.isNotEmpty") + fun intArrayIsNotEmpty() { val code = """ fun test(arr: IntArray): IntArray { return if (arr.isNotEmpty()) { @@ -330,7 +372,8 @@ class UseIfEmptyOrIfBlankSpec : Spek({ assertThat(findings).isEmpty() } - it("else if") { + @Test + fun `else if`() { val code = """ fun test(list: List, b: Boolean): List { return if (list.isEmpty()) { @@ -346,7 +389,8 @@ class UseIfEmptyOrIfBlankSpec : Spek({ assertThat(findings).isEmpty() } - it("no else") { + @Test + fun `no else`() { val code = """ fun test(list: List) { if (list.isEmpty()) { @@ -358,7 +402,8 @@ class UseIfEmptyOrIfBlankSpec : Spek({ assertThat(findings).isEmpty() } - it("not self value") { + @Test + fun `not self value`() { val code = """ fun test(list: List): List { return if (list.isEmpty()) { @@ -372,7 +417,8 @@ class UseIfEmptyOrIfBlankSpec : Spek({ assertThat(findings).isEmpty() } - it("self value block is not single statement") { + @Test + fun `self value block is not single statement`() { val code = """ fun test(list: List): List { return if (list.isEmpty()) { @@ -387,7 +433,8 @@ class UseIfEmptyOrIfBlankSpec : Spek({ assertThat(findings).isEmpty() } - it("condition is binary expression") { + @Test + fun `condition is binary expression`() { val code = """ fun test(list: List): List { return if (list.isEmpty() == true) { @@ -401,4 +448,4 @@ class UseIfEmptyOrIfBlankSpec : Spek({ assertThat(findings).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseIfInsteadOfWhenSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseIfInsteadOfWhenSpec.kt index fa4a9f3111a3..58c15a336714 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseIfInsteadOfWhenSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseIfInsteadOfWhenSpec.kt @@ -2,16 +2,18 @@ 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 -object UseIfInsteadOfWhenSpec : Spek({ +class UseIfInsteadOfWhenSpec { - val subject by memoized { UseIfInsteadOfWhen() } + val subject = UseIfInsteadOfWhen() - describe("UseIfInsteadOfWhen rule") { + @Nested + inner class `UseIfInsteadOfWhen rule` { - it("reports when using two branches") { + @Test + fun `reports when using two branches`() { val code = """ fun function(): Boolean? { val x = null @@ -24,7 +26,8 @@ object UseIfInsteadOfWhenSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("does not report when using one branch") { + @Test + fun `does not report when using one branch`() { val code = """ fun function(): Boolean? { val x = null @@ -36,7 +39,8 @@ object UseIfInsteadOfWhenSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report when using more than two branches") { + @Test + fun `does not report when using more than two branches`() { val code = """ fun function(): Boolean? { val x = null @@ -50,7 +54,8 @@ object UseIfInsteadOfWhenSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report when second branch is not 'else'") { + @Test + fun `does not report when second branch is not 'else'`() { val code = """ fun function(): Boolean? { val x = null @@ -64,4 +69,4 @@ object UseIfInsteadOfWhenSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseIsNullOrEmptySpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseIsNullOrEmptySpec.kt index 48dfb2ccc18a..a0e436052325 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseIsNullOrEmptySpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseIsNullOrEmptySpec.kt @@ -1,20 +1,22 @@ package io.gitlab.arturbosch.detekt.rules.style -import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.assertThat import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -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 UseIsNullOrEmptySpec : Spek({ - setupKotlinEnvironment() - val env: KotlinCoreEnvironment by memoized() - val subject by memoized { UseIsNullOrEmpty() } +@KotlinCoreEnvironmentTest +class UseIsNullOrEmptySpec(val env: KotlinCoreEnvironment) { + val subject = UseIsNullOrEmpty() - describe("report UseIsNullOrEmpty rule") { - context("List") { - it("null or isEmpty()") { + @Nested + inner class `report UseIsNullOrEmpty rule` { + @Nested + inner class `List` { + @Test + fun `null or isEmpty()`() { val code = """ fun test(x: List?) { if (x == null || x.isEmpty()) return @@ -27,7 +29,9 @@ class UseIsNullOrEmptySpec : Spek({ "This 'x == null || x.isEmpty()' can be replaced with 'isNullOrEmpty()' call" ) } - it("null or count() == 0") { + + @Test + fun `null or count() == 0`() { val code = """ fun test(x: List?) { if (x == null || x.count() == 0) return @@ -36,7 +40,9 @@ class UseIsNullOrEmptySpec : Spek({ val findings = subject.compileAndLintWithContext(env, code) assertThat(findings).hasSize(1) } - it("null or size == 0") { + + @Test + fun `null or size == 0`() { val code = """ fun test(x: List?) { if (x == null || x.size == 0) return @@ -45,7 +51,9 @@ class UseIsNullOrEmptySpec : Spek({ val findings = subject.compileAndLintWithContext(env, code) assertThat(findings).hasSize(1) } - it("flipped null check") { + + @Test + fun `flipped null check`() { val code = """ fun test(x: List?) { if (null == x || x.isEmpty()) return @@ -54,7 +62,9 @@ class UseIsNullOrEmptySpec : Spek({ val findings = subject.compileAndLintWithContext(env, code) assertThat(findings).hasSize(1) } - it("flipped count check") { + + @Test + fun `flipped count check`() { val code = """ fun test(x: List?) { if (x == null || 0 == x.count()) return @@ -63,7 +73,9 @@ class UseIsNullOrEmptySpec : Spek({ val findings = subject.compileAndLintWithContext(env, code) assertThat(findings).hasSize(1) } - it("flipped size check") { + + @Test + fun `flipped size check`() { val code = """ fun test(x: List?) { if (x == null || 0 == x.size) return @@ -74,8 +86,10 @@ class UseIsNullOrEmptySpec : Spek({ } } - context("Set") { - it("null or isEmpty()") { + @Nested + inner class `Set` { + @Test + fun `null or isEmpty()`() { val code = """ fun test(x: Set?) { if (x == null || x.isEmpty()) return @@ -84,7 +98,9 @@ class UseIsNullOrEmptySpec : Spek({ val findings = subject.compileAndLintWithContext(env, code) assertThat(findings).hasSize(1) } - it("null or count() == 0") { + + @Test + fun `null or count() == 0`() { val code = """ fun test(x: Set?) { if (x == null || x.count() == 0) return @@ -93,7 +109,9 @@ class UseIsNullOrEmptySpec : Spek({ val findings = subject.compileAndLintWithContext(env, code) assertThat(findings).hasSize(1) } - it("null or size == 0") { + + @Test + fun `null or size == 0`() { val code = """ fun test(x: Set?) { if (x == null || x.size == 0) return @@ -104,8 +122,10 @@ class UseIsNullOrEmptySpec : Spek({ } } - context("Collection") { - it("null or isEmpty()") { + @Nested + inner class `Collection` { + @Test + fun `null or isEmpty()`() { val code = """ fun test(x: Collection?) { if (x == null || x.isEmpty()) return @@ -114,7 +134,9 @@ class UseIsNullOrEmptySpec : Spek({ val findings = subject.compileAndLintWithContext(env, code) assertThat(findings).hasSize(1) } - it("null or count() == 0") { + + @Test + fun `null or count() == 0`() { val code = """ fun test(x: Collection?) { if (x == null || x.count() == 0) return @@ -123,7 +145,9 @@ class UseIsNullOrEmptySpec : Spek({ val findings = subject.compileAndLintWithContext(env, code) assertThat(findings).hasSize(1) } - it("null or size == 0") { + + @Test + fun `null or size == 0`() { val code = """ fun test(x: Collection?) { if (x == null || x.size == 0) return @@ -134,8 +158,10 @@ class UseIsNullOrEmptySpec : Spek({ } } - context("Map") { - it("null or isEmpty()") { + @Nested + inner class `Map` { + @Test + fun `null or isEmpty()`() { val code = """ fun test(x: Map?) { if (x == null || x.isEmpty()) return @@ -144,7 +170,9 @@ class UseIsNullOrEmptySpec : Spek({ val findings = subject.compileAndLintWithContext(env, code) assertThat(findings).hasSize(1) } - it("null or count() == 0") { + + @Test + fun `null or count() == 0`() { val code = """ fun test(x: Map?) { if (x == null || x.count() == 0) return @@ -153,7 +181,9 @@ class UseIsNullOrEmptySpec : Spek({ val findings = subject.compileAndLintWithContext(env, code) assertThat(findings).hasSize(1) } - it("null or size == 0") { + + @Test + fun `null or size == 0`() { val code = """ fun test(x: Map?) { if (x == null || x.size == 0) return @@ -164,8 +194,10 @@ class UseIsNullOrEmptySpec : Spek({ } } - context("Array") { - it("null or isEmpty()") { + @Nested + inner class `Array` { + @Test + fun `null or isEmpty()`() { val code = """ fun test(x: Array?) { if (x == null || x.isEmpty()) return @@ -174,7 +206,9 @@ class UseIsNullOrEmptySpec : Spek({ val findings = subject.compileAndLintWithContext(env, code) assertThat(findings).hasSize(1) } - it("null or count() == 0") { + + @Test + fun `null or count() == 0`() { val code = """ fun test(x: Array?) { if (x == null || x.count() == 0) return @@ -183,7 +217,9 @@ class UseIsNullOrEmptySpec : Spek({ val findings = subject.compileAndLintWithContext(env, code) assertThat(findings).hasSize(1) } - it("null or size == 0") { + + @Test + fun `null or size == 0`() { val code = """ fun test(x: Array?) { if (x == null || x.size == 0) return @@ -194,8 +230,10 @@ class UseIsNullOrEmptySpec : Spek({ } } - context("String") { - it("null or isEmpty()") { + @Nested + inner class `String` { + @Test + fun `null or isEmpty()`() { val code = """ fun test(x: String?) { if (x == null || x.isEmpty()) return @@ -204,7 +242,9 @@ class UseIsNullOrEmptySpec : Spek({ val findings = subject.compileAndLintWithContext(env, code) assertThat(findings).hasSize(1) } - it("null or count() == 0") { + + @Test + fun `null or count() == 0`() { val code = """ fun test(x: String?) { if (x == null || x.count() == 0) return @@ -213,7 +253,9 @@ class UseIsNullOrEmptySpec : Spek({ val findings = subject.compileAndLintWithContext(env, code) assertThat(findings).hasSize(1) } - it("null or length == 0") { + + @Test + fun `null or length == 0`() { val code = """ fun test(x: String?) { if (x == null || x.length == 0) return @@ -223,7 +265,8 @@ class UseIsNullOrEmptySpec : Spek({ assertThat(findings).hasSize(1) } - it("null or equal empty string") { + @Test + fun `null or equal empty string`() { val code = """ fun test(x: String?) { if (x == null || x == "") return @@ -234,8 +277,10 @@ class UseIsNullOrEmptySpec : Spek({ } } - context("MutableList") { - it("null or isEmpty()") { + @Nested + inner class `MutableList` { + @Test + fun `null or isEmpty()`() { val code = """ fun test(x: MutableList?) { if (x == null || x.isEmpty()) return @@ -246,8 +291,10 @@ class UseIsNullOrEmptySpec : Spek({ } } - context("MutableSet") { - it("null or isEmpty()") { + @Nested + inner class `MutableSet` { + @Test + fun `null or isEmpty()`() { val code = """ fun test(x: MutableSet?) { if (x == null || x.isEmpty()) return @@ -258,8 +305,10 @@ class UseIsNullOrEmptySpec : Spek({ } } - context("MutableCollection") { - it("null or isEmpty()") { + @Nested + inner class `MutableCollection` { + @Test + fun `null or isEmpty()`() { val code = """ fun test(x: MutableCollection?) { if (x == null || x.isEmpty()) return @@ -270,8 +319,10 @@ class UseIsNullOrEmptySpec : Spek({ } } - context("MutableMap") { - it("null or isEmpty()") { + @Nested + inner class `MutableMap` { + @Test + fun `null or isEmpty()`() { val code = """ fun test(x: MutableMap?) { if (x == null || x.isEmpty()) return @@ -283,9 +334,12 @@ class UseIsNullOrEmptySpec : Spek({ } } - describe("does not report UseIsNullOrEmpty rule") { - context("IntArray") { - it("null or isEmpty()") { + @Nested + inner class `does not report UseIsNullOrEmpty rule` { + @Nested + inner class `IntArray` { + @Test + fun `null or isEmpty()`() { val code = """ fun test(x: IntArray?) { if (x == null || x.isEmpty()) return @@ -294,7 +348,9 @@ class UseIsNullOrEmptySpec : Spek({ val findings = subject.compileAndLintWithContext(env, code) assertThat(findings).isEmpty() } - it("null or count() == 0") { + + @Test + fun `null or count() == 0`() { val code = """ fun test(x: IntArray?) { if (x == null || x.count() == 0) return @@ -303,7 +359,9 @@ class UseIsNullOrEmptySpec : Spek({ val findings = subject.compileAndLintWithContext(env, code) assertThat(findings).isEmpty() } - it("null or size == 0") { + + @Test + fun `null or size == 0`() { val code = """ fun test(x: IntArray?) { if (x == null || x.size == 0) return @@ -314,8 +372,10 @@ class UseIsNullOrEmptySpec : Spek({ } } - context("Sequence") { - it("null or count() == 0") { + @Nested + inner class `Sequence` { + @Test + fun `null or count() == 0`() { val code = """ fun test(x: Sequence?) { if (x == null || x.count() == 0) return @@ -326,7 +386,8 @@ class UseIsNullOrEmptySpec : Spek({ } } - it("different variables") { + @Test + fun `different variables`() { val code = """ fun test(x: List?, y: List) { if (x == null || y.isEmpty()) return @@ -336,7 +397,8 @@ class UseIsNullOrEmptySpec : Spek({ assertThat(findings).isEmpty() } - it("not null check") { + @Test + fun `not null check`() { val code = """ fun test(x: List?) { if (x != null && x.isEmpty()) return @@ -346,7 +408,8 @@ class UseIsNullOrEmptySpec : Spek({ assertThat(findings).isEmpty() } - it("not size zero check") { + @Test + fun `not size zero check`() { val code = """ fun test(x: List?) { if (x == null || x.count() == 1) return @@ -356,7 +419,8 @@ class UseIsNullOrEmptySpec : Spek({ assertThat(findings).isEmpty() } - it("not null") { + @Test + fun `not null`() { val code = """ fun test(x: List) { if (x == null || x.isEmpty()) return @@ -366,7 +430,8 @@ class UseIsNullOrEmptySpec : Spek({ assertThat(findings).isEmpty() } - it("var class member") { + @Test + fun `var class member`() { val code = """ class Test { var x: List? = null @@ -380,4 +445,4 @@ class UseIsNullOrEmptySpec : Spek({ assertThat(findings).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseOrEmptySpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseOrEmptySpec.kt index d3fb55c6998b..b4a53be9904b 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseOrEmptySpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseOrEmptySpec.kt @@ -1,19 +1,20 @@ package io.gitlab.arturbosch.detekt.rules.style -import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.assertThat import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -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 UseOrEmptySpec : Spek({ - setupKotlinEnvironment() - val env: KotlinCoreEnvironment by memoized() - val subject by memoized { UseOrEmpty() } +@KotlinCoreEnvironmentTest +class UseOrEmptySpec(val env: KotlinCoreEnvironment) { + val subject = UseOrEmpty() - describe("report UseOrEmptySpec rule") { - it("emptyList") { + @Nested + inner class `report UseOrEmptySpec rule` { + @Test + fun `emptyList`() { val code = """ fun test(x: List?) { val a = x ?: emptyList() @@ -25,7 +26,8 @@ class UseOrEmptySpec : Spek({ assertThat(findings[0]).hasMessage("This '?: emptyList()' can be replaced with 'orEmpty()' call") } - it("emptySet") { + @Test + fun `emptySet`() { val code = """ fun test(x: Set?) { val a = x ?: emptySet() @@ -35,7 +37,8 @@ class UseOrEmptySpec : Spek({ assertThat(findings).hasSize(1) } - it("emptyMap") { + @Test + fun `emptyMap`() { val code = """ fun test(x: Map?) { val a = x ?: emptyMap() @@ -45,7 +48,8 @@ class UseOrEmptySpec : Spek({ assertThat(findings).hasSize(1) } - it("emptySequence") { + @Test + fun `emptySequence`() { val code = """ fun test(x: Sequence?) { val a = x ?: emptySequence() @@ -55,7 +59,8 @@ class UseOrEmptySpec : Spek({ assertThat(findings).hasSize(1) } - it("emptyArray") { + @Test + fun `emptyArray`() { val code = """ fun test(x: Array?) { val a = x ?: emptyArray() @@ -65,7 +70,8 @@ class UseOrEmptySpec : Spek({ assertThat(findings).hasSize(1) } - it("listOf") { + @Test + fun `listOf`() { val code = """ fun test(x: List?) { val a = x ?: listOf() @@ -75,7 +81,8 @@ class UseOrEmptySpec : Spek({ assertThat(findings).hasSize(1) } - it("setOf") { + @Test + fun `setOf`() { val code = """ fun test(x: Set?) { val a = x ?: setOf() @@ -85,7 +92,8 @@ class UseOrEmptySpec : Spek({ assertThat(findings).hasSize(1) } - it("mapOf") { + @Test + fun `mapOf`() { val code = """ fun test(x: Map?) { val a = x ?: mapOf() @@ -95,7 +103,8 @@ class UseOrEmptySpec : Spek({ assertThat(findings).hasSize(1) } - it("sequenceOf") { + @Test + fun `sequenceOf`() { val code = """ fun test(x: Sequence?) { val a = x ?: sequenceOf() @@ -105,7 +114,8 @@ class UseOrEmptySpec : Spek({ assertThat(findings).hasSize(1) } - it("arrayOf") { + @Test + fun `arrayOf`() { val code = """ fun test(x: Array?) { val a = x ?: arrayOf() @@ -115,7 +125,8 @@ class UseOrEmptySpec : Spek({ assertThat(findings).hasSize(1) } - it("empty string") { + @Test + fun `empty string`() { val code = """ fun test(x: String?) { val a = x ?: "" @@ -125,7 +136,8 @@ class UseOrEmptySpec : Spek({ assertThat(findings).hasSize(1) } - it("mutable list") { + @Test + fun `mutable list`() { val code = """ fun test(x: MutableList?) { val a = x ?: emptyList() @@ -136,8 +148,10 @@ class UseOrEmptySpec : Spek({ } } - describe("does not report UseOrEmptySpec rule") { - it("not null") { + @Nested + inner class `does not report UseOrEmptySpec rule` { + @Test + fun `not null`() { val code = """ fun test(x: List) { val a = x ?: emptyList() @@ -147,7 +161,8 @@ class UseOrEmptySpec : Spek({ assertThat(findings).isEmpty() } - it("not empty") { + @Test + fun `not empty`() { val code = """ fun test(x: List?) { val a = x ?: listOf(1) @@ -157,7 +172,8 @@ class UseOrEmptySpec : Spek({ assertThat(findings).isEmpty() } - it("different types") { + @Test + fun `different types`() { val code = """ fun test(x: List?) { val a = x ?: emptySet() @@ -167,7 +183,8 @@ class UseOrEmptySpec : Spek({ assertThat(findings).isEmpty() } - it("mutableListOf") { + @Test + fun `mutableListOf`() { val code = """ fun test(x: MutableList?) { val a = x ?: mutableListOf() @@ -177,7 +194,8 @@ class UseOrEmptySpec : Spek({ assertThat(findings).isEmpty() } - it("intArrayOf") { + @Test + fun `intArrayOf`() { val code = """ fun test(x: IntArray?) { val a = x ?: intArrayOf() @@ -187,4 +205,4 @@ class UseOrEmptySpec : Spek({ assertThat(findings).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseRequireNotNullSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseRequireNotNullSpec.kt index 7d1de268f0d1..7e07718b1d24 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseRequireNotNullSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseRequireNotNullSpec.kt @@ -1,20 +1,20 @@ package io.gitlab.arturbosch.detekt.rules.style -import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext import org.assertj.core.api.Assertions.assertThat import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -import org.spekframework.spek2.Spek -import org.spekframework.spek2.style.specification.describe +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test -object UseRequireNotNullSpec : Spek({ - setupKotlinEnvironment() +@KotlinCoreEnvironmentTest +class UseRequireNotNullSpec(val env: KotlinCoreEnvironment) { + val subject = UseRequireNotNull() - val env: KotlinCoreEnvironment by memoized() - val subject by memoized { UseRequireNotNull() } - - describe("UseRequireNotNull rule") { - it("reports `require` calls with a non-null check") { + @Nested + inner class `UseRequireNotNull rule` { + @Test + fun `reports 'require' calls with a non-null check`() { val code = """ fun test(i: Int?) { require(i != null) @@ -24,7 +24,8 @@ object UseRequireNotNullSpec : Spek({ assertThat(actual).hasSize(1) } - it("reports `require` calls with a non-null check that has `null` on the left side") { + @Test + fun `reports 'require' calls with a non-null check that has 'null' on the left side`() { val code = """ fun test(i: Int?) { require(null != i) @@ -34,7 +35,8 @@ object UseRequireNotNullSpec : Spek({ assertThat(actual).hasSize(1) } - it("does not report a `require` call without a non-null check") { + @Test + fun `does not report a 'require' call without a non-null check`() { val code = """ fun test(i: Int) { require(i > 0) @@ -44,4 +46,4 @@ object UseRequireNotNullSpec : Spek({ assertThat(actual).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseRequireSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseRequireSpec.kt index 05cb1aa02235..572d8a61846f 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseRequireSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UseRequireSpec.kt @@ -1,24 +1,24 @@ package io.gitlab.arturbosch.detekt.rules.style import io.gitlab.arturbosch.detekt.api.Config -import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.assertThat import io.gitlab.arturbosch.detekt.test.compileAndLint import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext import io.gitlab.arturbosch.detekt.test.lint import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -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 UseRequireSpec : Spek({ - setupKotlinEnvironment() +@KotlinCoreEnvironmentTest +class UseRequireSpec(val env: KotlinCoreEnvironment) { + val subject = UseRequire(Config.empty) - val env: KotlinCoreEnvironment by memoized() - val subject by memoized { UseRequire(Config.empty) } + @Nested + inner class `UseRequire rule` { - describe("UseRequire rule") { - - it("reports if a precondition throws an IllegalArgumentException") { + @Test + fun `reports if a precondition throws an IllegalArgumentException`() { val code = """ fun x(a: Int) { if (a < 0) throw IllegalArgumentException() @@ -28,7 +28,8 @@ class UseRequireSpec : Spek({ assertThat(subject.lint(code)).hasSourceLocation(2, 16) } - it("reports if a precondition throws an IllegalArgumentException with more details") { + @Test + fun `reports if a precondition throws an IllegalArgumentException with more details`() { val code = """ fun x(a: Int) { if (a < 0) throw IllegalArgumentException("More details") @@ -38,7 +39,8 @@ class UseRequireSpec : Spek({ assertThat(subject.lint(code)).hasSourceLocation(2, 16) } - it("reports if a precondition throws a fully qualified IllegalArgumentException") { + @Test + fun `reports if a precondition throws a fully qualified IllegalArgumentException`() { val code = """ fun x(a: Int) { if (a < 0) throw java.lang.IllegalArgumentException() @@ -48,7 +50,8 @@ class UseRequireSpec : Spek({ assertThat(subject.lint(code)).hasSourceLocation(2, 16) } - it("reports if a precondition throws a fully qualified IllegalArgumentException using the kotlin type alias") { + @Test + fun `reports if a precondition throws a fully qualified IllegalArgumentException using the kotlin type alias`() { val code = """ fun x(a: Int) { if (a < 0) throw kotlin.IllegalArgumentException() @@ -58,7 +61,8 @@ class UseRequireSpec : Spek({ assertThat(subject.lint(code)).hasSourceLocation(2, 16) } - it("does not report if a precondition throws a different kind of exception") { + @Test + fun `does not report if a precondition throws a different kind of exception`() { val code = """ fun x(a: Int) { if (a < 0) throw SomeBusinessException() @@ -68,7 +72,8 @@ class UseRequireSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report an issue if the exception thrown has a message and a cause") { + @Test + fun `does not report an issue if the exception thrown has a message and a cause`() { val code = """ private fun x(a: Int): Nothing { doSomething() @@ -78,7 +83,8 @@ class UseRequireSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report an issue if the exception thrown as the only action in a block") { + @Test + fun `does not report an issue if the exception thrown as the only action in a block`() { val code = """ fun unsafeRunSync(): A = foo.fold({ throw IllegalArgumentException("message") }, ::identity) @@ -86,17 +92,20 @@ class UseRequireSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report an issue if the exception thrown unconditionally") { + @Test + fun `does not report an issue if the exception thrown unconditionally`() { val code = """fun doThrow() = throw IllegalArgumentException("message")""" assertThat(subject.lint(code)).isEmpty() } - it("does not report an issue if the exception thrown unconditionally in a function block") { + @Test + fun `does not report an issue if the exception thrown unconditionally in a function block`() { val code = """fun doThrow() { throw IllegalArgumentException("message") }""" assertThat(subject.lint(code)).isEmpty() } - it("does not report if the exception thrown has a non-String argument") { + @Test + fun `does not report if the exception thrown has a non-String argument`() { val code = """ fun test(throwable: Throwable) { if (throwable !is NumberFormatException) throw IllegalArgumentException(throwable) @@ -105,7 +114,8 @@ class UseRequireSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report if the exception thrown has a String literal argument and a non-String argument") { + @Test + fun `does not report if the exception thrown has a String literal argument and a non-String argument`() { val code = """ fun test(throwable: Throwable) { if (throwable !is NumberFormatException) throw IllegalArgumentException("a", throwable) @@ -114,7 +124,8 @@ class UseRequireSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report if the exception thrown has a non-String literal argument") { + @Test + fun `does not report if the exception thrown has a non-String literal argument`() { val code = """ fun test(throwable: Throwable) { val s = "" @@ -124,9 +135,11 @@ class UseRequireSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - context("with binding context") { + @Nested + inner class `with binding context` { - it("does not report if the exception thrown has a non-String argument") { + @Test + fun `does not report if the exception thrown has a non-String argument`() { val code = """ fun test(throwable: Throwable) { if (throwable !is NumberFormatException) throw IllegalArgumentException(throwable) @@ -135,7 +148,8 @@ class UseRequireSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report if the exception thrown has a String literal argument and a non-String argument") { + @Test + fun `does not report if the exception thrown has a String literal argument and a non-String argument`() { val code = """ fun test(throwable: Throwable) { if (throwable !is NumberFormatException) throw IllegalArgumentException("a", throwable) @@ -144,7 +158,8 @@ class UseRequireSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("reports if the exception thrown has a non-String literal argument") { + @Test + fun `reports if the exception thrown has a non-String literal argument`() { val code = """ fun test(throwable: Throwable) { val s = "" @@ -153,7 +168,9 @@ class UseRequireSpec : Spek({ """ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("reports if the exception thrown has a String literal argument") { + + @Test + fun `reports if the exception thrown has a String literal argument`() { val code = """ fun test(throwable: Throwable) { if (throwable !is NumberFormatException) throw IllegalArgumentException("a") @@ -163,9 +180,11 @@ class UseRequireSpec : Spek({ } } - context("throw is not after a precondition") { + @Nested + inner class `throw is not after a precondition` { - it("does not report an issue if the exception is inside a when") { + @Test + fun `does not report an issue if the exception is inside a when`() { val code = """ fun whenOrThrow(item : List<*>) = when(item) { is ArrayList<*> -> 1 @@ -176,7 +195,8 @@ class UseRequireSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report an issue if the exception is after a block") { + @Test + fun `does not report an issue if the exception is after a block`() { val code = """ fun doSomethingOrThrow(test: Int): Int { var index = 0 @@ -191,7 +211,8 @@ class UseRequireSpec : Spek({ assertThat(subject.lint(code)).isEmpty() } - it("does not report an issue if the exception is after a elvis operator") { + @Test + fun `does not report an issue if the exception is after a elvis operator`() { val code = """ fun tryToCastOrThrow(list: List<*>) : LinkedList<*> { val subclass = list as? LinkedList @@ -203,4 +224,4 @@ class UseRequireSpec : Spek({ } } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UselessCallOnNotNullSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UselessCallOnNotNullSpec.kt index 105509c83b28..922f2d06db31 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UselessCallOnNotNullSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UselessCallOnNotNullSpec.kt @@ -1,41 +1,44 @@ package io.gitlab.arturbosch.detekt.rules.style -import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext import org.assertj.core.api.Assertions.assertThat import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -import org.spekframework.spek2.Spek -import org.spekframework.spek2.style.specification.describe +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test -object UselessCallOnNotNullSpec : Spek({ - setupKotlinEnvironment() +@KotlinCoreEnvironmentTest +class UselessCallOnNotNullSpec(val env: KotlinCoreEnvironment) { + val subject = UselessCallOnNotNull() - val env: KotlinCoreEnvironment by memoized() - val subject by memoized { UselessCallOnNotNull() } - - describe("UselessCallOnNotNull rule") { - it("reports when calling orEmpty on a list") { + @Nested + inner class `UselessCallOnNotNull rule` { + @Test + fun `reports when calling orEmpty on a list`() { val code = """val testList = listOf("string").orEmpty()""" val findings = subject.compileAndLintWithContext(env, code) assertThat(findings).hasSize(1) assertThat(findings[0].message).isEqualTo("Remove redundant call to orEmpty") } - it("reports when calling orEmpty on a list with a safe call") { + @Test + fun `reports when calling orEmpty on a list with a safe call`() { val code = """val testList = listOf("string")?.orEmpty()""" val findings = subject.compileAndLintWithContext(env, code) assertThat(findings).hasSize(1) assertThat(findings[0].message).isEqualTo("Remove redundant call to orEmpty") } - it("reports when calling orEmpty on a list in a chain") { + @Test + fun `reports when calling orEmpty on a list in a chain`() { val code = """val testList = listOf("string").orEmpty().map { }""" val findings = subject.compileAndLintWithContext(env, code) assertThat(findings).hasSize(1) assertThat(findings[0].message).isEqualTo("Remove redundant call to orEmpty") } - it("reports when calling orEmpty on a list with a platform type") { + @Test + fun `reports when calling orEmpty on a list with a platform type`() { // System.getenv().keys.toList() will be of type List. val code = """val testSequence = System.getenv().keys.toList().orEmpty()""" val findings = subject.compileAndLintWithContext(env, code) @@ -43,7 +46,8 @@ object UselessCallOnNotNullSpec : Spek({ assertThat(findings[0].message).isEqualTo("Remove redundant call to orEmpty") } - it("does not report when calling orEmpty on a nullable list") { + @Test + fun `does not report when calling orEmpty on a nullable list`() { val code = """ val testList: List? = listOf("string") val nonNullableTestList = testList.orEmpty() @@ -51,14 +55,16 @@ object UselessCallOnNotNullSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("reports when calling isNullOrBlank on a string with a safe call") { + @Test + fun `reports when calling isNullOrBlank on a string with a safe call`() { val code = """val testString = ""?.isNullOrBlank()""" val findings = subject.compileAndLintWithContext(env, code) assertThat(findings).hasSize(1) assertThat(findings[0].message).isEqualTo("Replace isNullOrBlank with isBlank") } - it("does not report when calling isNullOrBlank on a nullable string") { + @Test + fun `does not report when calling isNullOrBlank on a nullable string`() { val code = """ val testString: String? = "" val nonNullableTestString = testString.isNullOrBlank() @@ -66,19 +72,22 @@ object UselessCallOnNotNullSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("reports when calling isNullOrEmpty on a string") { + @Test + fun `reports when calling isNullOrEmpty on a string`() { val code = """val testString = "".isNullOrEmpty()""" val findings = subject.compileAndLintWithContext(env, code) assertThat(findings).hasSize(1) assertThat(findings[0].message).isEqualTo("Replace isNullOrEmpty with isEmpty") } - it("reports when calling isNullOrEmpty on a string with a safe call") { + @Test + fun `reports when calling isNullOrEmpty on a string with a safe call`() { val code = """val testString = ""?.isNullOrEmpty()""" assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("does not report when calling isNullOrEmpty on a nullable string") { + @Test + fun `does not report when calling isNullOrEmpty on a nullable string`() { val code = """ val testString: String? = "" val nonNullableTestString = testString.isNullOrEmpty() @@ -86,14 +95,16 @@ object UselessCallOnNotNullSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("reports when calling orEmpty on a string") { + @Test + fun `reports when calling orEmpty on a string`() { val code = """val testString = "".orEmpty()""" val findings = subject.compileAndLintWithContext(env, code) assertThat(findings).hasSize(1) assertThat(findings[0].message).isEqualTo("Remove redundant call to orEmpty") } - it("does not report when calling orEmpty on a nullable string") { + @Test + fun `does not report when calling orEmpty on a nullable string`() { val code = """ val testString: String? = "" val nonNullableTestString = testString.orEmpty() @@ -101,14 +112,16 @@ object UselessCallOnNotNullSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("reports when calling orEmpty on a sequence") { + @Test + fun `reports when calling orEmpty on a sequence`() { val code = """val testSequence = listOf(1).asSequence().orEmpty()""" val findings = subject.compileAndLintWithContext(env, code) assertThat(findings).hasSize(1) assertThat(findings[0].message).isEqualTo("Remove redundant call to orEmpty") } - it("does not report when calling orEmpty on a nullable sequence") { + @Test + fun `does not report when calling orEmpty on a nullable sequence`() { val code = """ val testSequence: Sequence? = listOf(1).asSequence() val nonNullableTestSequence = testSequence.orEmpty() @@ -116,7 +129,8 @@ object UselessCallOnNotNullSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("only reports on a Kotlin list") { + @Test + fun `only reports on a Kotlin list`() { val code = """ fun String.orEmpty(): List = this.toCharArray().asList() @@ -128,7 +142,8 @@ object UselessCallOnNotNullSpec : Spek({ assertThat(findings[0].message).isEqualTo("Remove redundant call to orEmpty") } - it("reports when calling listOfNotNull on all non-nullable arguments") { + @Test + fun `reports when calling listOfNotNull on all non-nullable arguments`() { val code = """ val strings = listOfNotNull("string") """ @@ -137,7 +152,8 @@ object UselessCallOnNotNullSpec : Spek({ assertThat(findings[0].message).isEqualTo("Replace listOfNotNull with listOf") } - it("reports when calling listOfNotNull with no arguments") { + @Test + fun `reports when calling listOfNotNull with no arguments`() { val code = """ val strings = listOfNotNull() """ @@ -146,7 +162,8 @@ object UselessCallOnNotNullSpec : Spek({ assertThat(findings[0].message).isEqualTo("Replace listOfNotNull with listOf") } - it("does not report when calling listOfNotNull on at least one nullable argument") { + @Test + fun `does not report when calling listOfNotNull on at least one nullable argument`() { val code = """ val strings = listOfNotNull("string", null) """ @@ -154,7 +171,8 @@ object UselessCallOnNotNullSpec : Spek({ assertThat(findings).isEmpty() } - it("does not report when calling listOfNotNull with spread operator") { + @Test + fun `does not report when calling listOfNotNull with spread operator`() { val code = """ val nullableArray = arrayOf("string", null) val strings = listOfNotNull(*nullableArray) @@ -163,7 +181,8 @@ object UselessCallOnNotNullSpec : Spek({ assertThat(findings).isEmpty() } - it("reports when calling listOfNotNull with spread operator on all non-nullable arguments") { + @Test + fun `reports when calling listOfNotNull with spread operator on all non-nullable arguments`() { val code = """ val nonNullableArray = arrayOf("string", "bar") val strings = listOfNotNull(*nonNullableArray) @@ -173,7 +192,8 @@ object UselessCallOnNotNullSpec : Spek({ assertThat(findings[0].message).isEqualTo("Replace listOfNotNull with listOf") } - it("does not report when calling listOfNotNull with a mix of null spread and non-null non-spread") { + @Test + fun `does not report when calling listOfNotNull with a mix of null spread and non-null non-spread`() { val code = """ val nullableArray = arrayOf("string", null) val nonNullableArray = arrayOf("string", "bar") @@ -183,7 +203,8 @@ object UselessCallOnNotNullSpec : Spek({ assertThat(findings).isEmpty() } - it("does not report when calling listOfNotNull with a mix of non-null spread and null non-spread") { + @Test + fun `does not report when calling listOfNotNull with a mix of non-null spread and null non-spread`() { val code = """ val nonNullableArray = arrayOf("string", "bar") val strings = listOfNotNull("string", *nonNullableArray, null) @@ -192,7 +213,8 @@ object UselessCallOnNotNullSpec : Spek({ assertThat(findings).isEmpty() } - it("reports when calling listOfNotNull with a mix of spread and non-spread, all non-null") { + @Test + fun `reports when calling listOfNotNull with a mix of spread and non-spread, all non-null`() { val code = """ val nonNullableArray = arrayOf("string", "bar") val otherNonNullableArray = arrayOf("foobar") @@ -203,7 +225,8 @@ object UselessCallOnNotNullSpec : Spek({ assertThat(findings[0].message).isEqualTo("Replace listOfNotNull with listOf") } - it("does not report when calling custom function named listOfNotNull on all non-nullable arguments") { + @Test + fun `does not report when calling custom function named listOfNotNull on all non-nullable arguments`() { val code = """ fun listOfNotNull(vararg elements: T?): List = TODO() @@ -213,7 +236,8 @@ object UselessCallOnNotNullSpec : Spek({ assertThat(findings).isEmpty() } - it("reports when calling isNullOrEmpty on a list") { + @Test + fun `reports when calling isNullOrEmpty on a list`() { val code = """ fun test(list: List) { list.isNullOrEmpty() @@ -224,4 +248,4 @@ object UselessCallOnNotNullSpec : Spek({ assertThat(findings[0].message).isEqualTo("Replace isNullOrEmpty with isEmpty") } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UtilityClassWithPublicConstructorSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UtilityClassWithPublicConstructorSpec.kt index c5b7332d4a15..0dc51595745e 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UtilityClassWithPublicConstructorSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UtilityClassWithPublicConstructorSpec.kt @@ -5,43 +5,55 @@ import io.gitlab.arturbosch.detekt.api.Finding import io.gitlab.arturbosch.detekt.rules.Case 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 +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test -class UtilityClassWithPublicConstructorSpec : Spek({ +class UtilityClassWithPublicConstructorSpec { - val subject by memoized { UtilityClassWithPublicConstructor(Config.empty) } - describe("UtilityClassWithPublicConstructor rule") { + val subject = UtilityClassWithPublicConstructor(Config.empty) - context("several UtilityClassWithPublicConstructor rule violations") { + @Nested + inner class `UtilityClassWithPublicConstructor rule` { + + @Nested + inner class `several UtilityClassWithPublicConstructor rule violations` { lateinit var findings: List - beforeEachTest { + @BeforeEach + fun beforeEachTest() { findings = subject.lint(Case.UtilityClassesPositive.path()) } - it("reports utility classes with a public constructor") { + @Test + fun `reports utility classes with a public constructor`() { assertThat(findings).hasSize(6) } - it("reports utility classes which are marked as open") { - val count = findings.count { it.message.contains("The utility class OpenUtilityClass should be final.") } + @Test + fun `reports utility classes which are marked as open`() { + val count = + findings.count { it.message.contains("The utility class OpenUtilityClass should be final.") } assertThat(count).isEqualTo(1) } } - context("several classes which adhere to the UtilityClassWithPublicConstructor rule") { + @Nested + inner class `several classes which adhere to the UtilityClassWithPublicConstructor rule` { - it("does not report given classes") { + @Test + fun `does not report given classes`() { val findings = subject.lint(Case.UtilityClassesNegative.path()) assertThat(findings).isEmpty() } } - context("annotations class") { + @Nested + inner class `annotations class` { - it("should not get triggered for utility class") { + @Test + fun `should not get triggered for utility class`() { val code = """ @Retention(AnnotationRetention.SOURCE) @StringDef( @@ -59,4 +71,4 @@ class UtilityClassWithPublicConstructorSpec : Spek({ } } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/VarCouldBeValSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/VarCouldBeValSpec.kt index 6421287d2842..e2b4a1c2ce3e 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/VarCouldBeValSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/VarCouldBeValSpec.kt @@ -1,21 +1,21 @@ package io.gitlab.arturbosch.detekt.rules.style -import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.compileAndLint import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext import org.assertj.core.api.Assertions.assertThat import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -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 VarCouldBeValSpec : Spek({ - setupKotlinEnvironment() +@KotlinCoreEnvironmentTest +class VarCouldBeValSpec(val env: KotlinCoreEnvironment) { + val subject = VarCouldBeVal() - val env: KotlinCoreEnvironment by memoized() - val subject by memoized { VarCouldBeVal() } - - describe("file-level declarations") { - it("does not report non-private variables") { + @Nested + inner class `file-level declarations` { + @Test + fun `does not report non-private variables`() { val code = """ var a = 1 internal var b = 2 @@ -24,7 +24,8 @@ class VarCouldBeValSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("reports private variables that are never re-assigned") { + @Test + fun `reports private variables that are never re-assigned`() { val code = """ private var a = 1 @@ -36,7 +37,8 @@ class VarCouldBeValSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("does not report private variables that are re-assigned") { + @Test + fun `does not report private variables that are re-assigned`() { val code = """ private var a = 1 @@ -49,8 +51,10 @@ class VarCouldBeValSpec : Spek({ } } - describe("class-level declarations") { - it("does not report non-private variables in non-private classes") { + @Nested + inner class `class-level declarations` { + @Test + fun `does not report non-private variables in non-private classes`() { val code = """ class A { var a = 1 @@ -64,7 +68,8 @@ class VarCouldBeValSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report non-private variables in non-private objects") { + @Test + fun `does not report non-private variables in non-private objects`() { val code = """ object A { var a = 1 @@ -78,7 +83,8 @@ class VarCouldBeValSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report variables that are re-assigned") { + @Test + fun `does not report variables that are re-assigned`() { val code = """ class A { private var a = 1 @@ -91,7 +97,8 @@ class VarCouldBeValSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("reports variables that are not re-assigned") { + @Test + fun `reports variables that are not re-assigned`() { val code = """ class A { private var a = 1 @@ -101,9 +108,11 @@ class VarCouldBeValSpec : Spek({ } } - describe("local declarations in functions") { + @Nested + inner class `local declarations in functions` { - it("does not report variables that are re-assigned") { + @Test + fun `does not report variables that are re-assigned`() { val code = """ fun test() { var a = 1 @@ -113,7 +122,8 @@ class VarCouldBeValSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report variables that are re-assigned with assignment operator") { + @Test + fun `does not report variables that are re-assigned with assignment operator`() { val code = """ fun test() { var a = 1 @@ -123,7 +133,8 @@ class VarCouldBeValSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report variables that are re-assigned with postfix operators") { + @Test + fun `does not report variables that are re-assigned with postfix operators`() { val code = """ fun test() { var a = 1 @@ -133,7 +144,8 @@ class VarCouldBeValSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report variables that are re-assigned with infix operators") { + @Test + fun `does not report variables that are re-assigned with infix operators`() { val code = """ fun test() { var a = 1 @@ -143,7 +155,8 @@ class VarCouldBeValSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report variables that are re-assigned inside scope functions") { + @Test + fun `does not report variables that are re-assigned inside scope functions`() { val code = """ fun test() { var a = 1 @@ -155,7 +168,8 @@ class VarCouldBeValSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("reports variables that are not re-assigned, but used in expressions") { + @Test + fun `reports variables that are not re-assigned, but used in expressions`() { val code = """ fun test() { var a = 1 @@ -168,7 +182,8 @@ class VarCouldBeValSpec : Spek({ assertThat(findings[0].entity.signature).isEqualTo("Test.kt\$var a = 1") } - it("reports variables that are not re-assigned, but used in function calls") { + @Test + fun `reports variables that are not re-assigned, but used in function calls`() { val code = """ fun test() { var a = 1 @@ -181,7 +196,8 @@ class VarCouldBeValSpec : Spek({ assertThat(findings[0].entity.signature).isEqualTo("Test.kt\$var a = 1") } - it("reports variables that are not re-assigned, but shadowed by one that is") { + @Test + fun `reports variables that are not re-assigned, but shadowed by one that is`() { val code = """ fun test() { var shadowed = 1 @@ -200,9 +216,11 @@ class VarCouldBeValSpec : Spek({ } } - describe("this-prefixed properties - #1257") { + @Nested + inner class `this-prefixed properties - #1257` { - it("finds unused field and local") { + @Test + fun `finds unused field and local`() { val code = """ fun createObject() = object { private var myVar: String? = null @@ -214,7 +232,8 @@ class VarCouldBeValSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(2) } - it("should not report this-prefixed property") { + @Test + fun `should not report this-prefixed property`() { val code = """ fun createObject() = object { private var myVar: String? = null @@ -226,7 +245,8 @@ class VarCouldBeValSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("should report unused local variable") { + @Test + fun `should report unused local variable`() { val code = """ fun createObject() = object { private var myVar: String? = null @@ -242,8 +262,10 @@ class VarCouldBeValSpec : Spek({ } } - describe("properties defined in anonymous object - #3805") { - it("should report unassigned properties") { + @Nested + inner class `properties defined in anonymous object - #3805` { + @Test + fun `should report unassigned properties`() { val code = """ fun test() { val wrapper = object { @@ -254,7 +276,8 @@ class VarCouldBeValSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("should not report assigned properties") { + @Test + fun `should not report assigned properties`() { val code = """ fun test() { val wrapper = object { @@ -266,7 +289,8 @@ class VarCouldBeValSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("should not report assigned properties that have accessors that are accessed") { + @Test + fun `should not report assigned properties that have accessors that are accessed`() { val code = """ interface I { var optionEnabled: Boolean @@ -283,8 +307,10 @@ class VarCouldBeValSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - context("anonymous objects that escape") { - it("does not report when an object initializes a variable directly") { + @Nested + inner class `anonymous objects that escape` { + @Test + fun `does not report when an object initializes a variable directly`() { val code = """ interface I { var optionEnabled: Boolean @@ -299,7 +325,8 @@ class VarCouldBeValSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report an object initializing a variable in an if-statement") { + @Test + fun `does not report an object initializing a variable in an if-statement`() { val code = """ interface I { var optionEnabled: Boolean @@ -318,7 +345,8 @@ class VarCouldBeValSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report when an object is assigned to a variable directly") { + @Test + fun `does not report when an object is assigned to a variable directly`() { val code = """ interface I { var optionEnabled: Boolean @@ -334,7 +362,8 @@ class VarCouldBeValSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report when an object is assigned to a variable in an if-statement") { + @Test + fun `does not report when an object is assigned to a variable in an if-statement`() { val code = """ interface I { var optionEnabled: Boolean @@ -354,7 +383,8 @@ class VarCouldBeValSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report when an object is defined in a return statement directly") { + @Test + fun `does not report when an object is defined in a return statement directly`() { val code = """ interface I { var optionEnabled: Boolean @@ -368,7 +398,8 @@ class VarCouldBeValSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report when an object is when defined in a return statement via an if-statement") { + @Test + fun `does not report when an object is when defined in a return statement via an if-statement`() { val code = """ interface I { var optionEnabled: Boolean @@ -386,7 +417,8 @@ class VarCouldBeValSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report when an object is defined as a function initializer") { + @Test + fun `does not report when an object is defined as a function initializer`() { val code = """ interface I { var optionEnabled: Boolean @@ -398,7 +430,8 @@ class VarCouldBeValSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report when an object is defined as a function initializer via an if-statement") { + @Test + fun `does not report when an object is defined as a function initializer via an if-statement`() { val code = """ interface I { var optionEnabled: Boolean @@ -412,7 +445,8 @@ class VarCouldBeValSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report when an object initializes a variable directly - without type solving") { + @Test + fun `does not report when an object initializes a variable directly - without type solving`() { val code = """ interface I { var optionEnabled: Boolean @@ -428,4 +462,4 @@ class VarCouldBeValSpec : Spek({ } } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/WildcardImportSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/WildcardImportSpec.kt index 46c8ae03a7d3..55448b4b6dc7 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/WildcardImportSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/WildcardImportSpec.kt @@ -5,51 +5,57 @@ import io.gitlab.arturbosch.detekt.test.TestConfig import io.gitlab.arturbosch.detekt.test.compileAndLint 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 +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test private const val EXCLUDED_IMPORTS = "excludeImports" -class WildcardImportSpec : Spek({ +class WildcardImportSpec { - describe("WildcardImport rule") { + @Nested + inner class `WildcardImport rule` { - context("a kt file with wildcard imports") { + @Nested + inner class `a kt file with wildcard imports` { val code = """ import io.gitlab.arturbosch.detekt.* - import org.spekframework.* + import io.mockk.* class Test { } """ - it("should not report anything when the rule is turned off") { + @Test + fun `should not report anything when the rule is turned off`() { val rule = WildcardImport(TestConfig(mapOf(Config.ACTIVE_KEY to "false"))) val findings = rule.compileAndLint(code) assertThat(findings).isEmpty() } - it("should report all wildcard imports") { + @Test + fun `should report all wildcard imports`() { val rule = WildcardImport() val findings = rule.compileAndLint(code) assertThat(findings).hasSize(2) } - it("should not report excluded wildcard imports") { - val rule = WildcardImport(TestConfig(mapOf(EXCLUDED_IMPORTS to listOf("org.spekframework.*")))) + @Test + fun `should not report excluded wildcard imports`() { + val rule = WildcardImport(TestConfig(mapOf(EXCLUDED_IMPORTS to listOf("io.mockk.*")))) val findings = rule.compileAndLint(code) assertThat(findings).hasSize(1) } - it("should not report excluded wildcard imports when multiple are excluded") { + @Test + fun `should not report excluded wildcard imports when multiple are excluded`() { val rule = WildcardImport( TestConfig( mapOf( EXCLUDED_IMPORTS to listOf( - "org.spekframework.*", + "io.mockk.*", "io.gitlab.arturbosch.detekt" ) ) @@ -60,22 +66,25 @@ class WildcardImportSpec : Spek({ assertThat(findings).isEmpty() } - it("should not report excluded wildcard imports when multiple are excluded using config string") { + @Test + fun `should not report excluded wildcard imports when multiple are excluded using config string`() { val rule = - WildcardImport(TestConfig(mapOf(EXCLUDED_IMPORTS to "org.spekframework.*, io.gitlab.arturbosch.detekt"))) + WildcardImport(TestConfig(mapOf(EXCLUDED_IMPORTS to "io.mockk.*, io.gitlab.arturbosch.detekt"))) val findings = rule.compileAndLint(code) assertThat(findings).isEmpty() } - it("ignores excludes that are not matching") { + @Test + fun `ignores excludes that are not matching`() { val rule = WildcardImport(TestConfig(mapOf(EXCLUDED_IMPORTS to listOf("other.test.*")))) val findings = rule.compileAndLint(code) assertThat(findings).hasSize(2) } - it("ignores the default values") { + @Test + fun `ignores the default values`() { val code2 = """ import java.util.* """ @@ -85,20 +94,22 @@ class WildcardImportSpec : Spek({ } } - context("a kt file with no wildcard imports") { + @Nested + inner class `a kt file with no wildcard imports` { val code = """ package org - import org.spekframework.spek2.Spek + import io.mockk.mockk class Test { } """ - it("should not report any issues") { + @Test + fun `should not report any issues`() { val findings = WildcardImport().compileAndLint(code) assertThat(findings).isEmpty() } } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/optional/MandatoryBracesIfStatementsSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/optional/MandatoryBracesIfStatementsSpec.kt index 094ad5bd1324..0b07afee7e46 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/optional/MandatoryBracesIfStatementsSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/optional/MandatoryBracesIfStatementsSpec.kt @@ -3,15 +3,17 @@ package io.gitlab.arturbosch.detekt.rules.style.optional import io.gitlab.arturbosch.detekt.api.Config import io.gitlab.arturbosch.detekt.test.assertThat import io.gitlab.arturbosch.detekt.test.compileAndLint -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 MandatoryBracesIfStatementsSpec : Spek({ - val subject by memoized { MandatoryBracesIfStatements(Config.empty) } +class MandatoryBracesIfStatementsSpec { + val subject = MandatoryBracesIfStatements(Config.empty) - describe("if statements which should have braces") { + @Nested + inner class `if statements which should have braces` { - it("reports a simple if") { + @Test + fun `reports a simple if`() { val findings = subject.compileAndLint( """ fun f() { @@ -25,7 +27,8 @@ class MandatoryBracesIfStatementsSpec : Spek({ assertThat(findings).hasTextLocations(32 to 41) } - it("reports a simple if with a single statement in multiple lines") { + @Test + fun `reports a simple if with a single statement in multiple lines`() { val findings = subject.compileAndLint( """ fun f() { @@ -38,7 +41,8 @@ class MandatoryBracesIfStatementsSpec : Spek({ assertThat(findings).hasSize(1) } - it("reports if-else with a single statement in multiple lines") { + @Test + fun `reports if-else with a single statement in multiple lines`() { val findings = subject.compileAndLint( """ fun f() { @@ -52,7 +56,8 @@ class MandatoryBracesIfStatementsSpec : Spek({ assertThat(findings).hasSize(2) } - it("reports if-else") { + @Test + fun `reports if-else`() { val findings = subject.compileAndLint( """ fun f() { @@ -68,7 +73,8 @@ class MandatoryBracesIfStatementsSpec : Spek({ assertThat(findings).hasTextLocations(32 to 41, 59 to 68) } - it("reports if-else with else-if") { + @Test + fun `reports if-else with else-if`() { val findings = subject.compileAndLint( """ fun f() { @@ -86,7 +92,8 @@ class MandatoryBracesIfStatementsSpec : Spek({ assertThat(findings).hasTextLocations(32 to 41, 70 to 79, 97 to 106) } - it("reports if with braces but else without") { + @Test + fun `reports if with braces but else without`() { val findings = subject.compileAndLint( """ fun f() { @@ -102,7 +109,8 @@ class MandatoryBracesIfStatementsSpec : Spek({ assertThat(findings).hasTextLocations(63 to 72) } - it("reports else with braces but if without") { + @Test + fun `reports else with braces but if without`() { val findings = subject.compileAndLint( """ fun f() { @@ -119,7 +127,8 @@ class MandatoryBracesIfStatementsSpec : Spek({ assertThat(findings).hasTextLocations(32 to 41) } - it("reports else in new line") { + @Test + fun `reports else in new line`() { val findings = subject.compileAndLint( """ fun f() { @@ -133,7 +142,8 @@ class MandatoryBracesIfStatementsSpec : Spek({ assertThat(findings).hasTextLocations(24 to 33) } - it("reports only else body on new line") { + @Test + fun `reports only else body on new line`() { val findings = subject.compileAndLint( """ fun f() { @@ -148,9 +158,11 @@ class MandatoryBracesIfStatementsSpec : Spek({ } } - describe("if statements with braces") { + @Nested + inner class `if statements with braces` { - it("does not report if statements with braces") { + @Test + fun `does not report if statements with braces`() { val code = """ fun f() { if (true) { @@ -167,9 +179,11 @@ class MandatoryBracesIfStatementsSpec : Spek({ } } - describe("single-line if statements which don't need braces") { + @Nested + inner class `single-line if statements which don't need braces` { - it("does not report single-line if statements") { + @Test + fun `does not report single-line if statements`() { val code = """ fun f() { if (true) println() @@ -181,9 +195,11 @@ class MandatoryBracesIfStatementsSpec : Spek({ } } - describe("multi-line when following an else statement without requiring braces") { + @Nested + inner class `multi-line when following an else statement without requiring braces` { - it("does not report multi-line when") { + @Test + fun `does not report multi-line when`() { val code = """ fun f(i: Int) { if (true) { @@ -197,4 +213,4 @@ class MandatoryBracesIfStatementsSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/optional/MandatoryBracesLoopsSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/optional/MandatoryBracesLoopsSpec.kt index a4a6d5bc0d91..d36178071a0b 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/optional/MandatoryBracesLoopsSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/optional/MandatoryBracesLoopsSpec.kt @@ -3,15 +3,17 @@ package io.gitlab.arturbosch.detekt.rules.style.optional import io.gitlab.arturbosch.detekt.api.SourceLocation 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 MandatoryBracesLoopsSpec : Spek({ - val subject by memoized { MandatoryBracesLoops() } +class MandatoryBracesLoopsSpec { + val subject = MandatoryBracesLoops() - describe("MandatoryBracesLoops rule for `for` loops") { + @Nested + inner class `MandatoryBracesLoops rule for 'for' loops` { - it("does not report with braces") { + @Test + fun `does not report with braces`() { val code = """ fun test() { for (i in 0..10) { @@ -23,7 +25,8 @@ class MandatoryBracesLoopsSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report full loop on single line") { + @Test + fun `does not report full loop on single line`() { val code = """ fun test() { for (i in 0..10) println(i) @@ -33,7 +36,8 @@ class MandatoryBracesLoopsSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report full loop on single line with multiple statements") { + @Test + fun `does not report full loop on single line with multiple statements`() { val code = """ fun test() { for (i in 0..10) println(i); print(' ') @@ -43,7 +47,8 @@ class MandatoryBracesLoopsSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("reports multi-line without braces") { + @Test + fun `reports multi-line without braces`() { val code = """ fun test() { for (i in 0..10) @@ -58,7 +63,8 @@ class MandatoryBracesLoopsSpec : Spek({ assertThat(findings[0].entity.ktElement?.text).isEqualTo("println(i)") } - it("does not report on suppression") { + @Test + fun `does not report on suppression`() { val code = """ fun test() { @Suppress("MandatoryBracesLoops") @@ -70,7 +76,8 @@ class MandatoryBracesLoopsSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report nested loops with braces") { + @Test + fun `does not report nested loops with braces`() { val code = """ fun test() { for (i in 0..10) { @@ -84,7 +91,8 @@ class MandatoryBracesLoopsSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report nested loops on single line") { + @Test + fun `does not report nested loops on single line`() { val code = """ fun test() { for (i in 0..10) for (j in 0..10) println() @@ -94,7 +102,8 @@ class MandatoryBracesLoopsSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("reports in nested loop outer") { + @Test + fun `reports in nested loop outer`() { val code = """ fun test() { for (i in 0..10) @@ -111,7 +120,8 @@ class MandatoryBracesLoopsSpec : Spek({ assertThat(findings[0].location.source).isEqualTo(SourceLocation(line = 3, column = 9)) } - it("reports in nested loop inner") { + @Test + fun `reports in nested loop inner`() { val code = """ fun test() { for (i in 0..10) { @@ -127,7 +137,8 @@ class MandatoryBracesLoopsSpec : Spek({ assertThat(findings[0].id).isEqualTo("MandatoryBracesLoops") } - it("reports both violations in nested loop") { + @Test + fun `reports both violations in nested loop`() { val code = """ fun test() { for (i in 0..10) @@ -146,7 +157,8 @@ class MandatoryBracesLoopsSpec : Spek({ io.gitlab.arturbosch.detekt.test.assertThat(findings).hasTextLocations(42 to 80, 71 to 80) } - it("reports with multi-line if statement") { + @Test + fun `reports with multi-line if statement`() { val code = """ fun test() { // because if statements are expressions, this code properly prints "Odd" and "Even" @@ -166,7 +178,8 @@ class MandatoryBracesLoopsSpec : Spek({ assertThat(findings[0].location.source).isEqualTo(SourceLocation(line = 4, column = 9)) } - it("reports inside of if statement without braces") { + @Test + fun `reports inside of if statement without braces`() { val code = """ fun test() { // this if statement would also be reported, but we're only checking the loop @@ -188,9 +201,11 @@ class MandatoryBracesLoopsSpec : Spek({ } } - describe("MandatoryBracesLoops rule for `while` loops") { + @Nested + inner class `MandatoryBracesLoops rule for 'while' loops` { - it("does not report with braces") { + @Test + fun `does not report with braces`() { val code = """ fun test() { while(true) { @@ -202,7 +217,8 @@ class MandatoryBracesLoopsSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report full loop on single line") { + @Test + fun `does not report full loop on single line`() { val code = """ fun test() { while(true) println() @@ -212,7 +228,8 @@ class MandatoryBracesLoopsSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("reports multi-line without braces") { + @Test + fun `reports multi-line without braces`() { val code = """ fun test() { while (true) @@ -227,7 +244,8 @@ class MandatoryBracesLoopsSpec : Spek({ assertThat(findings[0].entity.ktElement?.text).isEqualTo("println()") } - it("does not report on suppression") { + @Test + fun `does not report on suppression`() { val code = """ fun test() { @Suppress("MandatoryBracesLoops") @@ -239,7 +257,8 @@ class MandatoryBracesLoopsSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("reports in nested loop inner") { + @Test + fun `reports in nested loop inner`() { val code = """ fun test() { while (true) { @@ -256,9 +275,11 @@ class MandatoryBracesLoopsSpec : Spek({ } } - describe("MandatoryBracesLoops rule for `do while` loops") { + @Nested + inner class `MandatoryBracesLoops rule for 'do while' loops` { - it("does not report with braces") { + @Test + fun `does not report with braces`() { val code = """ fun test() { do { @@ -270,7 +291,8 @@ class MandatoryBracesLoopsSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report full loop on single line") { + @Test + fun `does not report full loop on single line`() { val code = """ fun test() { do println() while(true) @@ -280,7 +302,8 @@ class MandatoryBracesLoopsSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("reports multi-line without braces") { + @Test + fun `reports multi-line without braces`() { val code = """ fun test() { do @@ -296,7 +319,8 @@ class MandatoryBracesLoopsSpec : Spek({ assertThat(findings[0].entity.ktElement?.text).isEqualTo("println()") } - it("does not report on suppression") { + @Test + fun `does not report on suppression`() { val code = """ fun test() { @Suppress("MandatoryBracesLoops") @@ -309,7 +333,8 @@ class MandatoryBracesLoopsSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report nested loops with braces") { + @Test + fun `does not report nested loops with braces`() { val code = """ fun test() { do { @@ -323,7 +348,8 @@ class MandatoryBracesLoopsSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report nested loops on single line") { + @Test + fun `does not report nested loops on single line`() { val code = """ fun test() { var i = 0 @@ -334,7 +360,8 @@ class MandatoryBracesLoopsSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("reports in nested loop outer") { + @Test + fun `reports in nested loop outer`() { val code = """ fun test() { do @@ -352,7 +379,8 @@ class MandatoryBracesLoopsSpec : Spek({ assertThat(findings[0].location.source).isEqualTo(SourceLocation(line = 3, column = 9)) } - it("reports in nested loop inner") { + @Test + fun `reports in nested loop inner`() { val code = """ fun test() { do { @@ -369,4 +397,4 @@ class MandatoryBracesLoopsSpec : Spek({ assertThat(findings[0].id).isEqualTo("MandatoryBracesLoops") } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/optional/OptionalUnitSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/optional/OptionalUnitSpec.kt index 40d32ef76ded..5887cb6b4d62 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/optional/OptionalUnitSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/optional/OptionalUnitSpec.kt @@ -2,23 +2,24 @@ package io.gitlab.arturbosch.detekt.rules.style.optional import io.gitlab.arturbosch.detekt.api.Config import io.gitlab.arturbosch.detekt.api.Finding -import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.compileAndLint import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext import org.assertj.core.api.Assertions.assertThat import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -import org.spekframework.spek2.Spek -import org.spekframework.spek2.style.specification.describe +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test -class OptionalUnitSpec : Spek({ +@KotlinCoreEnvironmentTest +class OptionalUnitSpec(val env: KotlinCoreEnvironment) { - setupKotlinEnvironment() + val subject = OptionalUnit(Config.empty) - val subject by memoized { OptionalUnit(Config.empty) } - val env: KotlinCoreEnvironment by memoized() - - describe("OptionalUnit rule") { - it("should report when a function has an explicit Unit return type with context") { + @Nested + inner class `OptionalUnit rule` { + @Test + fun `should report when a function has an explicit Unit return type with context`() { val code = """ fun foo(): Unit { } """.trimIndent() @@ -26,7 +27,8 @@ class OptionalUnitSpec : Spek({ assertThat(findings).hasSize(1) } - it("should not report when a function has a non-unit body expression") { + @Test + fun `should not report when a function has a non-unit body expression`() { val code = """ fun foo() = String """.trimIndent() @@ -34,7 +36,8 @@ class OptionalUnitSpec : Spek({ assertThat(findings).isEmpty() } - context("several functions which return Unit") { + @Nested + inner class `several functions which return Unit` { val code = """ fun returnsUnit1(): Unit { @@ -48,15 +51,18 @@ class OptionalUnitSpec : Spek({ """ lateinit var findings: List - beforeEachTest { + @BeforeEach + fun beforeEachTest() { findings = subject.compileAndLint(code) } - it("should report functions returning Unit") { + @Test + fun `should report functions returning Unit`() { assertThat(findings).hasSize(3) } - it("should report the correct violation message") { + @Test + fun `should report the correct violation message`() { findings.forEach { assertThat(it.message).endsWith( " defines a return type of Unit. This is unnecessary and can safely be removed." @@ -65,9 +71,11 @@ class OptionalUnitSpec : Spek({ } } - context("an overridden function which returns Unit") { + @Nested + inner class `an overridden function which returns Unit` { - it("should not report Unit return type in overridden function") { + @Test + fun `should not report Unit return type in overridden function`() { val code = """ interface I { fun returnsUnit() @@ -81,7 +89,8 @@ class OptionalUnitSpec : Spek({ } } - context("several lone Unit statements") { + @Nested + inner class `several lone Unit statements` { val code = """ fun returnsNothing() { @@ -100,24 +109,29 @@ class OptionalUnitSpec : Spek({ """ lateinit var findings: List - beforeEachTest { + @BeforeEach + fun beforeEachTest() { findings = subject.compileAndLint(code) } - it("should report lone Unit statement") { + @Test + fun `should report lone Unit statement`() { assertThat(findings).hasSize(4) } - it("should report the correct violation message") { + @Test + fun `should report the correct violation message`() { findings.forEach { assertThat(it.message).isEqualTo("A single Unit expression is unnecessary and can safely be removed.") } } } - context("several Unit references") { + @Nested + inner class `several Unit references` { - it("should not report Unit reference") { + @Test + fun `should not report Unit reference`() { val findings = subject.compileAndLint( """ fun returnsNothing(u: Unit, us: () -> String) { @@ -133,8 +147,10 @@ class OptionalUnitSpec : Spek({ } } - context("a default interface implementation") { - it("should report Unit as part of default interface implementations") { + @Nested + inner class `a default interface implementation` { + @Test + fun `should report Unit as part of default interface implementations`() { val code = """ interface Foo { fun method(i: Int) = Unit @@ -145,8 +161,10 @@ class OptionalUnitSpec : Spek({ } } - context("last statement in block - #2452") { - it("unused as an expression") { + @Nested + inner class `last statement in block - #2452` { + @Test + fun `unused as an expression`() { val code = """ fun test(i: Int, b: Boolean) { when (i) { @@ -164,7 +182,8 @@ class OptionalUnitSpec : Spek({ assertThat(findings).hasSize(1) } - it("used as an expression and the previous expression is not a Unit type") { + @Test + fun `used as an expression and the previous expression is not a Unit type`() { val code = """ fun T.foo() { println(this) @@ -184,7 +203,8 @@ class OptionalUnitSpec : Spek({ assertThat(findings).isEmpty() } - it("used as an expression and the previous expression cannot be used as a value") { + @Test + fun `used as an expression and the previous expression cannot be used as a value`() { val code = """ fun T.foo() { println(this) @@ -210,7 +230,8 @@ class OptionalUnitSpec : Spek({ assertThat(findings).isEmpty() } - it("used as an expression and the previous expression cannot be used as a value 2") { + @Test + fun `used as an expression and the previous expression cannot be used as a value 2`() { val code = """ fun T.foo() { println(this) @@ -234,7 +255,8 @@ class OptionalUnitSpec : Spek({ assertThat(findings).isEmpty() } - it("used as an expression and the previous expression can be used as a value") { + @Test + fun `used as an expression and the previous expression can be used as a value`() { val code = """ fun T.foo() { println(this) @@ -262,7 +284,8 @@ class OptionalUnitSpec : Spek({ assertThat(findings).hasSize(1) } - it("used as an expression and the previous expression can be used as a value 2") { + @Test + fun `used as an expression and the previous expression can be used as a value 2`() { val code = """ fun T.foo() { println(this) @@ -287,7 +310,8 @@ class OptionalUnitSpec : Spek({ assertThat(findings).hasSize(1) } - it("used as an expression and the previous expression can be used as a value 3") { + @Test + fun `used as an expression and the previous expression can be used as a value 3`() { val code = """ fun T.foo() { println(this) @@ -312,7 +336,8 @@ class OptionalUnitSpec : Spek({ assertThat(findings).hasSize(1) } - it("another object is used as the last expression") { + @Test + fun `another object is used as the last expression`() { val code = """ fun foo() { String @@ -323,8 +348,10 @@ class OptionalUnitSpec : Spek({ } } - context("function initializers") { - it("should not report when function initializer is Nothing") { + @Nested + inner class `function initializers` { + @Test + fun `should not report when function initializer is Nothing`() { val code = """ fun test(): Unit = throw UnsupportedOperationException() """ @@ -332,7 +359,8 @@ class OptionalUnitSpec : Spek({ assertThat(findings).isEmpty() } - it("should not report when the function initializer requires a type") { + @Test + fun `should not report when the function initializer requires a type`() { val code = """ fun foo(block: (List) -> Unit): T { val list = listOf() @@ -346,7 +374,8 @@ class OptionalUnitSpec : Spek({ assertThat(findings).isEmpty() } - it("should report on function initializers when there is no context") { + @Test + fun `should report on function initializers when there is no context`() { val code = """ fun test(): Unit = throw UnsupportedOperationException() """ @@ -354,7 +383,8 @@ class OptionalUnitSpec : Spek({ assertThat(findings).hasSize(1) } - it("should report when the function initializer takes in the type Nothing") { + @Test + fun `should report when the function initializer takes in the type Nothing`() { val code = """ fun foo(block: (List) -> Unit): T { val list = listOf() @@ -368,7 +398,8 @@ class OptionalUnitSpec : Spek({ assertThat(findings).hasSize(1) } - it("should report when the function initializer does not provide a different type") { + @Test + fun `should report when the function initializer does not provide a different type`() { val code = """ fun foo() {} @@ -379,4 +410,4 @@ class OptionalUnitSpec : Spek({ } } } -}) +} diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/optional/PreferToOverPairSyntaxSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/optional/PreferToOverPairSyntaxSpec.kt index c12e9c0a6165..b0760e95ba2f 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/optional/PreferToOverPairSyntaxSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/optional/PreferToOverPairSyntaxSpec.kt @@ -1,22 +1,22 @@ package io.gitlab.arturbosch.detekt.rules.style.optional import io.gitlab.arturbosch.detekt.api.Config -import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment +import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext import org.assertj.core.api.Assertions.assertThat import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -import org.spekframework.spek2.Spek -import org.spekframework.spek2.style.specification.describe +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test -object PreferToOverPairSyntaxSpec : Spek({ - setupKotlinEnvironment() +@KotlinCoreEnvironmentTest +class PreferToOverPairSyntaxSpec(val env: KotlinCoreEnvironment) { + val subject = PreferToOverPairSyntax(Config.empty) - val env: KotlinCoreEnvironment by memoized() - val subject by memoized { PreferToOverPairSyntax(Config.empty) } + @Nested + inner class `PreferToOverPairSyntax rule` { - describe("PreferToOverPairSyntax rule") { - - it("reports if pair is created using pair constructor") { + @Test + fun `reports if pair is created using pair constructor`() { val code = """ val pair1 = Pair(1, 2) val pair2: Pair = Pair(1, 2) @@ -28,7 +28,8 @@ object PreferToOverPairSyntaxSpec : Spek({ assertThat(findings[0].message).endsWith("`1 to 2`.") } - it("reports if pair is created using a function that uses pair constructor") { + @Test + fun `reports if pair is created using a function that uses pair constructor`() { val code = """ val pair = createPair() fun createPair() = Pair(1, 2) @@ -38,12 +39,14 @@ object PreferToOverPairSyntaxSpec : Spek({ assertThat(findings[0].message).endsWith("`1 to 2`.") } - it("does not report if it is created using the to syntax") { + @Test + fun `does not report if it is created using the to syntax`() { val code = "val pair = 1 to 2" assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report if a non-Kotlin Pair class was used") { + @Test + fun `does not report if a non-Kotlin Pair class was used`() { val code = """ val pair1 = Pair(1, 2) val pair2: Pair = Pair(1, 2) @@ -54,7 +57,8 @@ object PreferToOverPairSyntaxSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report if pair is created using a function that uses the to syntax") { + @Test + fun `does not report if pair is created using a function that uses the to syntax`() { val code = """ val pair = createPair() fun createPair() = 1 to 2 @@ -62,4 +66,4 @@ object PreferToOverPairSyntaxSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } } -}) +}