diff --git a/detekt-rules-complexity/build.gradle.kts b/detekt-rules-complexity/build.gradle.kts index a401e57acf7..2db788f8ab8 100644 --- a/detekt-rules-complexity/build.gradle.kts +++ b/detekt-rules-complexity/build.gradle.kts @@ -7,6 +7,5 @@ dependencies { compileOnly(projects.detektMetrics) testImplementation(projects.detektMetrics) testImplementation(projects.detektTest) - testImplementation(libs.bundles.testImplementation) - testRuntimeOnly(libs.spek.runner) + testImplementation(libs.assertj) } diff --git a/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/ComplexConditionSpec.kt b/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/ComplexConditionSpec.kt index 880a381a561..c3aae0b8fe4 100644 --- a/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/ComplexConditionSpec.kt +++ b/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/ComplexConditionSpec.kt @@ -2,12 +2,13 @@ package io.gitlab.arturbosch.detekt.rules.complexity 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 ComplexConditionSpec : Spek({ +class ComplexConditionSpec { - describe("ComplexCondition rule") { + @Nested + inner class `ComplexCondition rule` { val code = """ val a = if (5 > 4 && 4 < 6 || (3 < 5 || 2 < 5)) { 42 } else { 24 } @@ -18,8 +19,9 @@ class ComplexConditionSpec : Spek({ } """ - it("reports some complex conditions") { + @Test + fun `reports some complex conditions`() { assertThat(ComplexCondition().compileAndLint(code)).hasSize(3) } } -}) +} diff --git a/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/ComplexInterfaceSpec.kt b/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/ComplexInterfaceSpec.kt index 90c9cf1cc2d..e2e56bf7368 100644 --- a/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/ComplexInterfaceSpec.kt +++ b/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/ComplexInterfaceSpec.kt @@ -3,8 +3,8 @@ package io.gitlab.arturbosch.detekt.rules.complexity 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 THRESHOLD = 4 private val defaultConfigMap = mapOf("threshold" to THRESHOLD) @@ -15,13 +15,15 @@ private val privateDeclarationsConfig = TestConfig( defaultConfigMap + ("includePrivateDeclarations" to true) ) -class ComplexInterfaceSpec : Spek({ +class ComplexInterfaceSpec { - val subject by memoized { ComplexInterface(TestConfig(defaultConfigMap)) } + private val subject = ComplexInterface(TestConfig(defaultConfigMap)) - describe("ComplexInterface rule positives") { + @Nested + inner class `ComplexInterface rule positives` { - context("interface members") { + @Nested + inner class `interface members` { val code = """ interface I { fun f1() @@ -31,17 +33,20 @@ class ComplexInterfaceSpec : Spek({ } """ - it("reports complex interface") { + @Test + fun `reports complex interface`() { assertThat(subject.compileAndLint(code)).hasSize(1) } - it("reports complex interface with includeStaticDeclarations config") { + @Test + fun `reports complex interface with includeStaticDeclarations config`() { val rule = ComplexInterface(staticDeclarationsConfig) assertThat(rule.compileAndLint(code)).hasSize(1) } } - context("nested interface members") { + @Nested + inner class `nested interface members` { val code = """ class I { interface Nested { @@ -53,17 +58,20 @@ class ComplexInterfaceSpec : Spek({ } """ - it("reports complex interface") { + @Test + fun `reports complex interface`() { assertThat(subject.compileAndLint(code)).hasSize(1) } - it("reports complex interface with includeStaticDeclarations config") { + @Test + fun `reports complex interface with includeStaticDeclarations config`() { val rule = ComplexInterface(staticDeclarationsConfig) assertThat(rule.compileAndLint(code)).hasSize(1) } } - context("interface with static declarations") { + @Nested + inner class `interface with static declarations` { val code = """ interface I { fun f1() @@ -75,17 +83,20 @@ class ComplexInterfaceSpec : Spek({ } """ - it("does not report static declarations per default") { + @Test + fun `does not report static declarations per default`() { assertThat(subject.compileAndLint(code)).isEmpty() } - it("reports complex interface with includeStaticDeclarations config") { + @Test + fun `reports complex interface with includeStaticDeclarations config`() { val rule = ComplexInterface(staticDeclarationsConfig) assertThat(rule.compileAndLint(code)).hasSize(1) } } - context("private functions") { + @Nested + inner class `private functions` { val code = """ interface I { fun f1() @@ -95,17 +106,20 @@ class ComplexInterfaceSpec : Spek({ } """ - it("does not report complex interface") { + @Test + fun `does not report complex interface`() { assertThat(subject.compileAndLint(code)).isEmpty() } - it("does report complex interface with includePrivateDeclarations config") { + @Test + fun `does report complex interface with includePrivateDeclarations config`() { val rule = ComplexInterface(privateDeclarationsConfig) assertThat(rule.compileAndLint(code)).hasSize(1) } } - context("private members") { + @Nested + inner class `private members` { val code = """ interface I { fun f1() @@ -116,20 +130,24 @@ class ComplexInterfaceSpec : Spek({ } """ - it("does not report complex interface") { + @Test + fun `does not report complex interface`() { assertThat(subject.compileAndLint(code)).isEmpty() } - it("does report complex interface with includePrivateDeclarations config") { + @Test + fun `does report complex interface with includePrivateDeclarations config`() { val rule = ComplexInterface(privateDeclarationsConfig) assertThat(rule.compileAndLint(code)).hasSize(1) } } } - describe("ComplexInterface rule negatives") { + @Nested + inner class `ComplexInterface rule negatives` { - it("does not report a simple interface ") { + @Test + fun `does not report a simple interface `() { val code = """ interface I { fun f() @@ -144,7 +162,8 @@ class ComplexInterfaceSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report a simple interface with a companion object") { + @Test + fun `does not report a simple interface with a companion object`() { val code = """ interface I { fun f() @@ -158,9 +177,10 @@ class ComplexInterfaceSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report an empty interface") { + @Test + fun `does not report an empty interface`() { val code = "interface Empty" assertThat(subject.compileAndLint(code)).isEmpty() } } -}) +} diff --git a/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/ComplexMethodSpec.kt b/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/ComplexMethodSpec.kt index e1f893a0af9..4ee38990975 100644 --- a/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/ComplexMethodSpec.kt +++ b/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/ComplexMethodSpec.kt @@ -7,20 +7,23 @@ import io.gitlab.arturbosch.detekt.test.assertThat import io.gitlab.arturbosch.detekt.test.compileAndLint import io.gitlab.arturbosch.detekt.test.isThresholded 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 private val defaultConfigMap: Map = mapOf("threshold" to "1") -class ComplexMethodSpec : Spek({ +class ComplexMethodSpec { val defaultComplexity = 1 - describe("ComplexMethod rule") { + @Nested + inner class `ComplexMethod rule` { - context("different complex constructs") { + @Nested + inner class `different complex constructs` { - it("counts different loops") { + @Test + fun `counts different loops`() { val findings = ComplexMethod(TestConfig(defaultConfigMap)).compileAndLint( """ fun test() { @@ -35,7 +38,8 @@ class ComplexMethodSpec : Spek({ assertThat(findings.first()).isThresholded().withValue(defaultComplexity + 4) } - it("counts catch blocks") { + @Test + fun `counts catch blocks`() { val findings = ComplexMethod(TestConfig(defaultConfigMap)).compileAndLint( """ fun test() { @@ -47,7 +51,8 @@ class ComplexMethodSpec : Spek({ assertThat(findings.first()).isThresholded().withValue(defaultComplexity + 2) } - it("counts nested conditional statements") { + @Test + fun `counts nested conditional statements`() { val findings = ComplexMethod(TestConfig(defaultConfigMap)).compileAndLint( """ fun test() { @@ -71,7 +76,8 @@ class ComplexMethodSpec : Spek({ } } - context("nesting functions") { + @Nested + inner class `nesting functions` { val code = """ fun test() { @@ -80,37 +86,44 @@ class ComplexMethodSpec : Spek({ } """ - it("counts three with nesting function 'forEach'") { + @Test + fun `counts three with nesting function 'forEach'`() { val config = TestConfig(defaultConfigMap.plus("ignoreNestingFunctions" to "false")) assertExpectedComplexityValue(code, config, expectedValue = 3) } - it("can ignore nesting functions like 'forEach'") { + @Test + fun `can ignore nesting functions like 'forEach'`() { val config = TestConfig(defaultConfigMap.plus("ignoreNestingFunctions" to "true")) assertExpectedComplexityValue(code, config, expectedValue = 2) } - it("skips all if if the nested functions is empty") { + @Test + fun `skips all if if the nested functions is empty`() { val config = TestConfig(defaultConfigMap.plus("nestingFunctions" to "")) assertExpectedComplexityValue(code, config, expectedValue = 2) } - it("skips 'forEach' as it is not specified") { + @Test + fun `skips 'forEach' as it is not specified`() { val config = TestConfig(defaultConfigMap.plus("nestingFunctions" to "let,apply,also")) assertExpectedComplexityValue(code, config, expectedValue = 2) } - it("skips 'forEach' as it is not specified list") { + @Test + fun `skips 'forEach' as it is not specified list`() { val config = TestConfig(defaultConfigMap.plus("nestingFunctions" to listOf("let", "apply", "also"))) assertExpectedComplexityValue(code, config, expectedValue = 2) } } - context("several complex methods") { + @Nested + inner class `several complex methods` { val path = resourceAsPath("ComplexMethods.kt") - it("does not report complex methods with a single when expression") { + @Test + fun `does not report complex methods with a single when expression`() { val config = TestConfig( mapOf( "threshold" to "4", @@ -122,7 +135,8 @@ class ComplexMethodSpec : Spek({ assertThat(subject.lint(path)).hasSourceLocations(SourceLocation(43, 5)) } - it("reports all complex methods") { + @Test + fun `reports all complex methods`() { val config = TestConfig(mapOf("threshold" to "4")) val subject = ComplexMethod(config) @@ -135,7 +149,8 @@ class ComplexMethodSpec : Spek({ ) } - it("does not trip for a reasonable amount of simple when entries when ignoreSimpleWhenEntries is true") { + @Test + fun `does not trip for a reasonable amount of simple when entries when ignoreSimpleWhenEntries is true`() { val config = TestConfig(mapOf("ignoreSimpleWhenEntries" to "true")) val subject = ComplexMethod(config) val code = """ @@ -162,7 +177,8 @@ class ComplexMethodSpec : Spek({ } } - context("function containing object literal with many overridden functions") { + @Nested + inner class `function containing object literal with many overridden functions` { val code = """ fun f(): List { @@ -212,12 +228,13 @@ class ComplexMethodSpec : Spek({ } """ - it("should not count these overridden functions to base functions complexity") { + @Test + fun `should not count these overridden functions to base functions complexity`() { assertThat(ComplexMethod().compileAndLint(code)).isEmpty() } } } -}) +} private fun assertExpectedComplexityValue(code: String, config: TestConfig, expectedValue: Int) { val findings = ComplexMethod(config).lint(code) diff --git a/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/LabeledExpressionSpec.kt b/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/LabeledExpressionSpec.kt index 7c63626aa8e..f5d10775f3a 100644 --- a/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/LabeledExpressionSpec.kt +++ b/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/LabeledExpressionSpec.kt @@ -3,16 +3,18 @@ package io.gitlab.arturbosch.detekt.rules.complexity 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 LabeledExpressionSpec : Spek({ +class LabeledExpressionSpec { - val subject by memoized { LabeledExpression() } + private val subject = LabeledExpression() - describe("LabeledExpression rule") { + @Nested + inner class `LabeledExpression rule` { - it("reports break and continue labels") { + @Test + fun `reports break and continue labels`() { val code = """ fun f() { loop@ for (i in 1..3) { @@ -26,7 +28,8 @@ class LabeledExpressionSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(3) } - it("reports implicit return label") { + @Test + fun `reports implicit return label`() { val code = """ fun f(range: IntRange) { range.forEach { @@ -38,7 +41,8 @@ class LabeledExpressionSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("reports explicit return label") { + @Test + fun `reports explicit return label`() { val code = """ fun f(range: IntRange) { range.forEach label@{ @@ -50,7 +54,8 @@ class LabeledExpressionSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(2) } - it("reports labels referencing inner and outer class") { + @Test + fun `reports labels referencing inner and outer class`() { val code = """ class Outer { inner class Inner { @@ -68,7 +73,8 @@ class LabeledExpressionSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(3) } - it("does not report inner class referencing outer class") { + @Test + fun `does not report inner class referencing outer class`() { val code = """ class Outer { inner class Inner { @@ -81,7 +87,8 @@ class LabeledExpressionSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report inner class referencing outer class in extension function") { + @Test + fun `does not report inner class referencing outer class in extension function`() { val code = """ class Outer { inner class Inner { @@ -95,7 +102,8 @@ class LabeledExpressionSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report nested class referencing outer class in extension function") { + @Test + fun `does not report nested class referencing outer class in extension function`() { val code = """ class Outer { class Nested { @@ -108,7 +116,8 @@ class LabeledExpressionSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report inner classes referencing outer class in extension function") { + @Test + fun `does not report inner classes referencing outer class in extension function`() { val code = """ class Outer { inner class Inner { @@ -128,7 +137,8 @@ class LabeledExpressionSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report excluded label") { + @Test + fun `does not report excluded label`() { val code = """ fun f() { loop@ for (i in 1..5) {} @@ -139,7 +149,8 @@ class LabeledExpressionSpec : Spek({ assertThat(findings).isEmpty() } - it("does not report excluded label config with string") { + @Test + fun `does not report excluded label config with string`() { val code = """ fun f() { loop@ for (i in 1..5) {} @@ -150,7 +161,8 @@ class LabeledExpressionSpec : Spek({ assertThat(findings).isEmpty() } - it("does not report excluded label config with leading and trailing wildcard") { + @Test + fun `does not report excluded label config with leading and trailing wildcard`() { val code = """ fun f() { loop@ for (i in 1..5) {} @@ -161,4 +173,4 @@ class LabeledExpressionSpec : Spek({ assertThat(findings).isEmpty() } } -}) +} diff --git a/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/LargeClassSpec.kt b/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/LargeClassSpec.kt index 7ba8f96b974..e8cc3e4b6f0 100644 --- a/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/LargeClassSpec.kt +++ b/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/LargeClassSpec.kt @@ -6,35 +6,30 @@ 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.Test private fun subject(threshold: Int) = LargeClass(TestConfig(mapOf("threshold" to threshold))) -class LargeClassSpec : Spek({ +class LargeClassSpec { - describe("nested classes are also considered") { - - it("should detect only the nested large class which exceeds threshold 70") { - val findings = subject(threshold = 70).lint(resourceAsPath("NestedClasses.kt")) - assertThat(findings).hasSize(1) - assertThat(findings).hasSourceLocations(SourceLocation(12, 15)) - } + @Test + fun `should detect only the nested large class which exceeds threshold 70`() { + val findings = subject(threshold = 70).lint(resourceAsPath("NestedClasses.kt")) + assertThat(findings).hasSize(1) + assertThat(findings).hasSourceLocations(SourceLocation(12, 15)) } - describe("files without classes should not be considered") { - - it("should not report anything in files without classes") { - val code = """ - val i = 0 - - fun f() { - println() - println() - } - """ - val rule = subject(threshold = 2) - assertThat(rule.compileAndLint(code)).isEmpty() - } + @Test + fun `should not report anything in files without classes`() { + val code = """ + val i = 0 + + fun f() { + println() + println() + } + """ + val rule = subject(threshold = 2) + assertThat(rule.compileAndLint(code)).isEmpty() } -}) +} diff --git a/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/LongMethodSpec.kt b/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/LongMethodSpec.kt index 922be48d571..17bda728070 100644 --- a/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/LongMethodSpec.kt +++ b/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/LongMethodSpec.kt @@ -4,16 +4,18 @@ import io.gitlab.arturbosch.detekt.api.ThresholdedCodeSmell 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 -class LongMethodSpec : Spek({ +class LongMethodSpec { - val subject by memoized { LongMethod(TestConfig(mapOf("threshold" to 5))) } + val subject = LongMethod(TestConfig(mapOf("threshold" to 5))) - describe("nested functions can be long") { + @Nested + inner class `nested functions can be long` { - it("should find two long methods") { + @Test + fun `should find two long methods`() { val code = """ fun longMethod() { // 5 lines println() @@ -33,7 +35,8 @@ class LongMethodSpec : Spek({ assertThat(findings).hasTextLocations("longMethod", "nestedLongMethod") } - it("should not find too long methods") { + @Test + fun `should not find too long methods`() { val code = """ fun methodOk() { // 3 lines println() @@ -47,7 +50,8 @@ class LongMethodSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("should not find too long method with params on newlines") { + @Test + fun `should not find too long method with params on newlines`() { val code = """ fun methodWithParams( param1: String @@ -60,7 +64,8 @@ class LongMethodSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("should find too long method with params on newlines") { + @Test + fun `should find too long method with params on newlines`() { val code = """ fun longMethodWithParams( param1: String @@ -77,7 +82,8 @@ class LongMethodSpec : Spek({ assertThat(findings[0] as ThresholdedCodeSmell).hasValue(5) } - it("should find long method with method call with params on separate lines") { + @Test + fun `should find long method with method call with params on separate lines`() { val code = """ fun longMethod( x1: Int, @@ -100,7 +106,8 @@ class LongMethodSpec : Spek({ assertThat(findings[0] as ThresholdedCodeSmell).hasValue(8) } - it("should find two long methods with params on separate lines") { + @Test + fun `should find two long methods with params on separate lines`() { val code = """ fun longMethod( param1: String @@ -125,7 +132,8 @@ class LongMethodSpec : Spek({ assertThat(findings).hasTextLocations("longMethod", "nestedLongMethod") } - it("should find nested long methods with params on separate lines") { + @Test + fun `should find nested long methods with params on separate lines`() { val code = """ fun longMethod( param1: String @@ -150,4 +158,4 @@ class LongMethodSpec : Spek({ assertThat(findings[0] as ThresholdedCodeSmell).hasValue(5) } } -}) +} diff --git a/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/LongParameterListSpec.kt b/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/LongParameterListSpec.kt index c34313b60c4..e9be5404f70 100644 --- a/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/LongParameterListSpec.kt +++ b/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/LongParameterListSpec.kt @@ -3,86 +3,96 @@ package io.gitlab.arturbosch.detekt.rules.complexity 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 LongParameterListSpec : Spek({ +class LongParameterListSpec { val defaultThreshold = 2 - val defaultConfig by memoized { + val defaultConfig = TestConfig( mapOf( "functionThreshold" to defaultThreshold, "constructorThreshold" to defaultThreshold ) ) - } - val subject by memoized { LongParameterList(defaultConfig) } + val subject = LongParameterList(defaultConfig) - describe("LongParameterList rule") { + @Nested + inner class `LongParameterList rule` { val reportMessageForFunction = "The function long(a: Int, b: Int) has too many parameters. " + "The current threshold is set to $defaultThreshold." val reportMessageForConstructor = "The constructor(a: Int, b: Int) has too many parameters. " + "The current threshold is set to $defaultThreshold." - it("reports too long parameter list") { + @Test + fun `reports too long parameter list`() { val code = "fun long(a: Int, b: Int) {}" val findings = subject.compileAndLint(code) assertThat(findings).hasSize(1) assertThat(findings.first().message).isEqualTo(reportMessageForFunction) } - it("does not report short parameter list") { + @Test + fun `does not report short parameter list`() { val code = "fun long(a: Int) {}" assertThat(subject.compileAndLint(code)).isEmpty() } - it("reports too long parameter list event for parameters with defaults") { + @Test + fun `reports too long parameter list event for parameters with defaults`() { val code = "fun long(a: Int, b: Int = 1) {}" assertThat(subject.compileAndLint(code)).hasSize(1) } - it("does not report long parameter list if parameters with defaults should be ignored") { + @Test + fun `does not report long parameter list if parameters with defaults should be ignored`() { val config = TestConfig(mapOf("ignoreDefaultParameters" to "true")) val rule = LongParameterList(config) val code = "fun long(a: Int, b: Int, c: Int = 2) {}" assertThat(rule.compileAndLint(code)).isEmpty() } - it("reports too long parameter list for primary constructors") { + @Test + fun `reports too long parameter list for primary constructors`() { val code = "class LongCtor(a: Int, b: Int)" val findings = subject.compileAndLint(code) assertThat(findings).hasSize(1) assertThat(findings.first().message).isEqualTo(reportMessageForConstructor) } - it("does not report short parameter list for primary constructors") { + @Test + fun `does not report short parameter list for primary constructors`() { val code = "class LongCtor(a: Int)" assertThat(subject.compileAndLint(code)).isEmpty() } - it("reports too long parameter list for secondary constructors") { + @Test + fun `reports too long parameter list for secondary constructors`() { val code = "class LongCtor() { constructor(a: Int, b: Int) : this() }" val findings = subject.compileAndLint(code) assertThat(findings).hasSize(1) assertThat(findings.first().message).isEqualTo(reportMessageForConstructor) } - it("does not report short parameter list for secondary constructors") { + @Test + fun `does not report short parameter list for secondary constructors`() { val code = "class LongCtor() { constructor(a: Int) : this() }" assertThat(subject.compileAndLint(code)).isEmpty() } - it("reports long parameter list if custom threshold is set") { + @Test + fun `reports long parameter list if custom threshold is set`() { val config = TestConfig(mapOf("constructorThreshold" to "1")) val rule = LongParameterList(config) val code = "class LongCtor(a: Int)" assertThat(rule.compileAndLint(code)).hasSize(1) } - it("does not report long parameter list for constructors of data classes if asked") { + @Test + fun `does not report long parameter list for constructors of data classes if asked`() { val config = TestConfig( mapOf( "ignoreDataClasses" to "true", @@ -94,9 +104,10 @@ class LongParameterListSpec : Spek({ assertThat(rule.compileAndLint(code)).isEmpty() } - describe("constructors and functions with ignored annotations") { + @Nested + inner class `constructors and functions with ignored annotations` { - val config by memoized { + val config = TestConfig( mapOf( "ignoreAnnotatedParameter" to listOf( @@ -109,11 +120,11 @@ class LongParameterListSpec : Spek({ "constructorThreshold" to 1 ) ) - } - val rule by memoized { LongParameterList(config) } + val rule = LongParameterList(config) - it("reports long parameter list for constructors if constructor parameters are annotated with annotation that is not ignored") { + @Test + fun `reports long parameter list for constructors if constructor parameters are annotated with annotation that is not ignored`() { val code = """ @Target(AnnotationTarget.VALUE_PARAMETER) annotation class CustomAnnotation @@ -123,7 +134,8 @@ class LongParameterListSpec : Spek({ assertThat(rule.compileAndLint(code)).hasSize(1) } - it("reports long parameter list for functions if enough function parameters are annotated with annotation that is not ignored") { + @Test + fun `reports long parameter list for functions if enough function parameters are annotated with annotation that is not ignored`() { val code = """ @Target(AnnotationTarget.VALUE_PARAMETER) annotation class CustomAnnotation @@ -133,12 +145,14 @@ class LongParameterListSpec : Spek({ assertThat(rule.compileAndLint(code)).hasSize(1) } - it("does not report long parameter list for constructors if enough constructor parameters are annotated with ignored annotation") { + @Test + fun `does not report long parameter list for constructors if enough constructor parameters are annotated with ignored annotation`() { val code = "class Data constructor(@kotlin.Suppress(\"\") val a: Int)" assertThat(rule.compileAndLint(code)).isEmpty() } - it("does not report long parameter list for functions if enough function parameters are annotated with ignored annotation") { + @Test + fun `does not report long parameter list for functions if enough function parameters are annotated with ignored annotation`() { val code = """class Data { fun foo(@kotlin.Suppress("") a: Int) {} } """ @@ -146,4 +160,4 @@ class LongParameterListSpec : Spek({ } } } -}) +} diff --git a/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/MethodOverloadingSpec.kt b/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/MethodOverloadingSpec.kt index d25c9af64fb..eec71bd8e6a 100644 --- a/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/MethodOverloadingSpec.kt +++ b/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/MethodOverloadingSpec.kt @@ -3,20 +3,23 @@ package io.gitlab.arturbosch.detekt.rules.complexity 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 MethodOverloadingSpec : Spek({ +class MethodOverloadingSpec { val defaultThreshold = 3 - val defaultConfig by memoized { TestConfig(mapOf("threshold" to defaultThreshold)) } + val defaultConfig = TestConfig(mapOf("threshold" to defaultThreshold)) - val subject by memoized { MethodOverloading(defaultConfig) } + val subject = MethodOverloading(defaultConfig) - describe("MethodOverloading rule") { + @Nested + inner class `MethodOverloading rule` { - context("several overloaded methods") { + @Nested + inner class `several overloaded methods` { - it("reports overloaded methods which exceed the threshold") { + @Test + fun `reports overloaded methods which exceed the threshold`() { val code = """ class Test { fun x() {} @@ -29,7 +32,8 @@ class MethodOverloadingSpec : Spek({ assertThat(findings[0].message).isEqualTo("The method 'x' is overloaded 3 times.") } - it("reports overloaded top level methods which exceed the threshold") { + @Test + fun `reports overloaded top level methods which exceed the threshold`() { val code = """ fun x() {} fun x(i: Int) {} @@ -38,7 +42,8 @@ class MethodOverloadingSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("does not report overloaded methods which do not exceed the threshold") { + @Test + fun `does not report overloaded methods which do not exceed the threshold`() { subject.compileAndLint( """ class Test { @@ -50,9 +55,11 @@ class MethodOverloadingSpec : Spek({ } } - context("several overloaded extensions methods") { + @Nested + inner class `several overloaded extensions methods` { - it("does not report extension methods with a different receiver") { + @Test + fun `does not report extension methods with a different receiver`() { subject.compileAndLint( """ fun Boolean.foo() {} @@ -62,7 +69,8 @@ class MethodOverloadingSpec : Spek({ assertThat(subject.findings.size).isZero() } - it("reports extension methods with the same receiver") { + @Test + fun `reports extension methods with the same receiver`() { subject.compileAndLint( """ fun Int.foo() {} @@ -73,9 +81,11 @@ class MethodOverloadingSpec : Spek({ } } - context("several nested overloaded methods") { + @Nested + inner class `several nested overloaded methods` { - it("reports nested overloaded methods which exceed the threshold") { + @Test + fun `reports nested overloaded methods which exceed the threshold`() { val code = """ class Outer { internal class Inner { @@ -88,7 +98,8 @@ class MethodOverloadingSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("does not report nested overloaded methods which do not exceed the threshold") { + @Test + fun `does not report nested overloaded methods which do not exceed the threshold`() { val code = """ class Outer { @@ -104,9 +115,11 @@ class MethodOverloadingSpec : Spek({ } } - context("several overloaded methods inside objects") { + @Nested + inner class `several overloaded methods inside objects` { - it("reports overloaded methods inside an object which exceed the threshold") { + @Test + fun `reports overloaded methods inside an object which exceed the threshold`() { val code = """ object Test { fun f() {} @@ -117,7 +130,8 @@ class MethodOverloadingSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("does not report overloaded methods inside an object which do not exceed the threshold") { + @Test + fun `does not report overloaded methods inside an object which do not exceed the threshold`() { val code = """ object Test { fun f() {} @@ -127,7 +141,8 @@ class MethodOverloadingSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("reports overloaded methods inside a companion object which exceed the threshold") { + @Test + fun `reports overloaded methods inside a companion object which exceed the threshold`() { val code = """ class Test { companion object { @@ -140,7 +155,8 @@ class MethodOverloadingSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("does not report overloaded methods in a companion object that do not exceed the threshold") { + @Test + fun `does not report overloaded methods in a companion object that do not exceed the threshold`() { val code = """ class Test { companion object { @@ -152,7 +168,8 @@ class MethodOverloadingSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report overloaded methods in classes/objects that do not exceed the threshold") { + @Test + fun `does not report overloaded methods in classes or objects that do not exceed the threshold`() { val code = """ class Test { @@ -167,7 +184,8 @@ class MethodOverloadingSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("reports overloaded methods inside an anonymous object expression") { + @Test + fun `reports overloaded methods inside an anonymous object expression`() { val code = """ class A { @@ -184,9 +202,11 @@ class MethodOverloadingSpec : Spek({ } } - context("several overloaded methods inside enum classes") { + @Nested + inner class `several overloaded methods inside enum classes` { - it("does not report overridden methods inside enum entries") { + @Test + fun `does not report overridden methods inside enum entries`() { val code = """ enum class Test { E1 { @@ -205,7 +225,8 @@ class MethodOverloadingSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("reports overloaded methods in enum entry") { + @Test + fun `reports overloaded methods in enum entry`() { val code = """ enum class Test { E { @@ -218,7 +239,8 @@ class MethodOverloadingSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("reports overloaded methods in enum class") { + @Test + fun `reports overloaded methods in enum class`() { val code = """ enum class Test { E; @@ -232,12 +254,14 @@ class MethodOverloadingSpec : Spek({ } } - it("does not report a class without a body") { + @Test + fun `does not report a class without a body`() { val code = "class A" assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report overloaded local functions") { + @Test + fun `does not report overloaded local functions`() { val code = """ fun top() { fun f() {} @@ -248,4 +272,4 @@ class MethodOverloadingSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } } -}) +} diff --git a/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/NamedArgumentsSpec.kt b/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/NamedArgumentsSpec.kt index 41247055d33..544924e712d 100644 --- a/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/NamedArgumentsSpec.kt +++ b/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/NamedArgumentsSpec.kt @@ -1,24 +1,24 @@ package io.gitlab.arturbosch.detekt.rules.complexity -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 -class NamedArgumentsSpec : Spek({ - setupKotlinEnvironment() - - val env: KotlinCoreEnvironment by memoized() +@KotlinCoreEnvironmentTest +class NamedArgumentsSpec(val env: KotlinCoreEnvironment) { val defaultThreshold = 2 - val defaultConfig by memoized { TestConfig(mapOf("threshold" to defaultThreshold)) } - val subject by memoized { NamedArguments(defaultConfig) } + val defaultConfig = TestConfig(mapOf("threshold" to defaultThreshold)) + val subject = NamedArguments(defaultConfig) - describe("NameArguments rule") { + @Nested + inner class `NameArguments rule` { - it("invocation with more than 2 parameters should throw error") { + @Test + fun `invocation with more than 2 parameters should throw error`() { val code = """ fun sum(a: Int, b:Int, c:Int) { println(a + b + c) @@ -31,7 +31,8 @@ class NamedArgumentsSpec : Spek({ assertThat(findings).hasSize(1) } - it("Function invocation with more than 2 parameters should not throw error if named") { + @Test + fun `Function invocation with more than 2 parameters should not throw error if named`() { val code = """ fun sum(a: Int, b:Int, c:Int) { println(a + b + c) @@ -44,7 +45,8 @@ class NamedArgumentsSpec : Spek({ assertThat(findings).hasSize(0) } - it("invocation with more than 2 parameters should throw error if even one is not named") { + @Test + fun `invocation with more than 2 parameters should throw error if even one is not named`() { val code = """ fun sum(a: Int, b:Int, c:Int) { println(a + b + c) @@ -57,7 +59,8 @@ class NamedArgumentsSpec : Spek({ assertThat(findings).hasSize(1) } - it("invocation with less than 3 parameters should not throw error") { + @Test + fun `invocation with less than 3 parameters should not throw error`() { val code = """ fun sum(a: Int, b:Int) { println(a + b) @@ -70,7 +73,8 @@ class NamedArgumentsSpec : Spek({ assertThat(findings).hasSize(0) } - it("invocation with less than 3 named parameters should not throw error") { + @Test + fun `invocation with less than 3 named parameters should not throw error`() { val code = """ fun sum(a: Int, b:Int) { println(a + b) @@ -83,7 +87,8 @@ class NamedArgumentsSpec : Spek({ assertThat(findings).hasSize(0) } - it("constructor invocation with more than 3 non-named parameters should throw error") { + @Test + fun `constructor invocation with more than 3 non-named parameters should throw error`() { val code = """ class C(val a: Int, val b:Int, val c:Int) @@ -93,7 +98,8 @@ class NamedArgumentsSpec : Spek({ assertThat(findings).hasSize(1) } - it("constructor invocation with more than 3 named parameters should not throw error") { + @Test + fun `constructor invocation with more than 3 named parameters should not throw error`() { val code = """ class C(val a: Int, val b:Int, val c:Int) @@ -103,7 +109,8 @@ class NamedArgumentsSpec : Spek({ assertThat(findings).hasSize(0) } - it("constructor invocation with less than 3 non-named parameters should not throw error") { + @Test + fun `constructor invocation with less than 3 non-named parameters should not throw error`() { val code = """ class C(val a: Int, val b:Int) @@ -113,7 +120,8 @@ class NamedArgumentsSpec : Spek({ assertThat(findings).hasSize(0) } - it("java method invocation should not be flagged") { + @Test + fun `java method invocation should not be flagged`() { val code = """ import java.time.LocalDateTime @@ -125,7 +133,8 @@ class NamedArgumentsSpec : Spek({ assertThat(findings).hasSize(0) } - it("invocation with varargs should not be flagged") { + @Test + fun `invocation with varargs should not be flagged`() { val code = """ fun foo(vararg i: Int) {} fun bar(a: Int, b: Int, c: Int, vararg s: String) {} @@ -138,7 +147,8 @@ class NamedArgumentsSpec : Spek({ assertThat(findings).hasSize(0) } - it("invocation with spread operator should be flagged") { + @Test + fun `invocation with spread operator should be flagged`() { val code = """ fun bar(a: Int, b: Int, c: Int, vararg s: String) {} fun test() { @@ -149,8 +159,10 @@ class NamedArgumentsSpec : Spek({ assertThat(findings).hasSize(1) } - context("lambda argument") { - it("inner lambda argument") { + @Nested + inner class `lambda argument` { + @Test + fun `inner lambda argument`() { val code = """ fun foo(a: Int, b: Int, c: Int, block: ((Int) -> Int)) {} @@ -162,7 +174,8 @@ class NamedArgumentsSpec : Spek({ assertThat(findings).hasSize(1) } - it("outer lambda argument") { + @Test + fun `outer lambda argument`() { val code = """ fun foo(a: Int, b: Int, c: Int, block: ((Int) -> Int)) {} @@ -174,7 +187,8 @@ class NamedArgumentsSpec : Spek({ assertThat(findings).hasSize(0) } - it("unnamed argument and outer argument") { + @Test + fun `unnamed argument and outer argument`() { val code = """ fun foo(a: Int, b: Int, c: Int, block: ((Int) -> Int)) {} @@ -187,4 +201,4 @@ class NamedArgumentsSpec : Spek({ } } } -}) +} diff --git a/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/NestedBlockDepthSpec.kt b/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/NestedBlockDepthSpec.kt index a45f494e216..e36c669a73f 100644 --- a/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/NestedBlockDepthSpec.kt +++ b/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/NestedBlockDepthSpec.kt @@ -7,23 +7,26 @@ 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.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 NestedBlockDepthSpec : Spek({ +class NestedBlockDepthSpec { val defaultThreshold = 4 - val defaultConfig by memoized { TestConfig(mapOf("threshold" to defaultThreshold)) } - val subject by memoized { NestedBlockDepth(defaultConfig) } + val defaultConfig = TestConfig(mapOf("threshold" to defaultThreshold)) + val subject = NestedBlockDepth(defaultConfig) - describe("nested classes are also considered") { - it("should detect only the nested large class") { + @Nested + inner class `nested classes are also considered` { + @Test + fun `should detect only the nested large class`() { subject.lint(resourceAsPath("NestedClasses.kt")) assertThat(subject.findings).hasSize(1) assertThat((subject.findings[0] as ThresholdedCodeSmell).value).isEqualTo(5) } - it("should detect too nested block depth") { + @Test + fun `should detect too nested block depth`() { val code = """ fun f() { if (true) { @@ -41,7 +44,8 @@ class NestedBlockDepthSpec : Spek({ assertThat(findings).hasTextLocations(4 to 5) } - it("should not detect valid nested block depth") { + @Test + fun `should not detect valid nested block depth`() { val code = """ fun f() { if (true) { @@ -54,7 +58,8 @@ class NestedBlockDepthSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("does not report valid nested if else branches") { + @Test + fun `does not report valid nested if else branches`() { val code = """ fun f() { if (true) { @@ -68,7 +73,8 @@ class NestedBlockDepthSpec : Spek({ assertThat(subject.compileAndLint(code)).isEmpty() } - it("reports deeply nested if else branches") { + @Test + fun `reports deeply nested if else branches`() { val code = """ fun f() { if (true) { @@ -84,4 +90,4 @@ class NestedBlockDepthSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } } -}) +} diff --git a/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/ReplaceSafeCallChainWithRunSpec.kt b/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/ReplaceSafeCallChainWithRunSpec.kt index 9428b199a5d..1541b5d7f84 100644 --- a/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/ReplaceSafeCallChainWithRunSpec.kt +++ b/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/ReplaceSafeCallChainWithRunSpec.kt @@ -1,21 +1,22 @@ package io.gitlab.arturbosch.detekt.rules.complexity -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 ReplaceSafeCallChainWithRunSpec : Spek({ - setupKotlinEnvironment() +@KotlinCoreEnvironmentTest +class ReplaceSafeCallChainWithRunSpec(val env: KotlinCoreEnvironment) { - val env: KotlinCoreEnvironment by memoized() - val subject by memoized { ReplaceSafeCallChainWithRun() } + val subject = ReplaceSafeCallChainWithRun() - describe("ReplaceSafeChainWithRun rule") { + @Nested + inner class `ReplaceSafeChainWithRun rule` { - it("reports long chain of unnecessary safe qualified expressions") { + @Test + fun `reports long chain of unnecessary safe qualified expressions`() { val code = """ val x: String? = "string" @@ -30,7 +31,8 @@ object ReplaceSafeCallChainWithRunSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("reports short chain of unnecessary safe qualified expressions") { + @Test + fun `reports short chain of unnecessary safe qualified expressions`() { val code = """ val x: String? = "string" @@ -42,7 +44,8 @@ object ReplaceSafeCallChainWithRunSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) } - it("does not report a safe call chain which is too short to benefit") { + @Test + fun `does not report a safe call chain which is too short to benefit`() { val code = """ val x: String? = "string" @@ -53,7 +56,8 @@ object ReplaceSafeCallChainWithRunSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } - it("does not report a safe call chain on left side of assignment") { + @Test + fun `does not report a safe call chain on left side of assignment`() { val code = """ class Something { var element: Element? = null @@ -71,4 +75,4 @@ object ReplaceSafeCallChainWithRunSpec : Spek({ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } } -}) +} diff --git a/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/StringLiteralDuplicationSpec.kt b/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/StringLiteralDuplicationSpec.kt index 63d74f30437..0acbed8b751 100644 --- a/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/StringLiteralDuplicationSpec.kt +++ b/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/StringLiteralDuplicationSpec.kt @@ -4,23 +4,26 @@ import io.gitlab.arturbosch.detekt.test.TestConfig import io.gitlab.arturbosch.detekt.test.compileAndLint import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatExceptionOfType -import org.spekframework.spek2.Spek -import org.spekframework.spek2.style.specification.describe +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test import java.util.regex.PatternSyntaxException private const val IGNORE_ANNOTATION = "ignoreAnnotation" private const val EXCLUDE_SHORT_STRING = "excludeStringsWithLessThan5Characters" private const val IGNORE_STRINGS_REGEX = "ignoreStringsRegex" -class StringLiteralDuplicationSpec : Spek({ +class StringLiteralDuplicationSpec { - val subject by memoized { StringLiteralDuplication() } + val subject = StringLiteralDuplication() - describe("StringLiteralDuplication rule") { + @Nested + inner class `StringLiteralDuplication rule` { - context("many hardcoded strings") { + @Nested + inner class `many hardcoded strings` { - it("reports 3 equal hardcoded strings") { + @Test + fun `reports 3 equal hardcoded strings`() { val code = """ class Duplication { var s1 = "lorem" @@ -31,13 +34,15 @@ class StringLiteralDuplicationSpec : Spek({ assertThat(subject.compileAndLint(code)).hasSize(1) } - it("does not report 2 equal hardcoded strings") { + @Test + fun `does not report 2 equal hardcoded strings`() { val code = """val str = "lorem" + "lorem" + "ipsum"""" assertThat(subject.compileAndLint(code)).isEmpty() } } - context("strings in annotations") { + @Nested + inner class `strings in annotations` { val code = """ @Suppress("unused") @@ -48,38 +53,45 @@ class StringLiteralDuplicationSpec : Spek({ class C """ - it("does not report strings in annotations") { + @Test + fun `does not report strings in annotations`() { assertThat(subject.compileAndLint(code)).isEmpty() } - it("reports strings in annotations according to config") { + @Test + fun `reports strings in annotations according to config`() { val config = TestConfig(mapOf(IGNORE_ANNOTATION to "false")) assertFindingWithConfig(code, config, 1) } } - context("strings with less than 5 characters") { + @Nested + inner class `strings with less than 5 characters` { val code = """val str = "amet" + "amet" + "amet"""" - it("does not report strings with 4 characters") { + @Test + fun `does not report strings with 4 characters`() { assertThat(subject.compileAndLint(code)).isEmpty() } - it("reports string with 4 characters") { + @Test + fun `reports string with 4 characters`() { val config = TestConfig(mapOf(EXCLUDE_SHORT_STRING to "false")) assertFindingWithConfig(code, config, 1) } } - context("strings with values to match for the regex") { + @Nested + inner class `strings with values to match for the regex` { val regexTestingCode = """ val str1 = "lorem" + "lorem" + "lorem" val str2 = "ipsum" + "ipsum" + "ipsum" """ - it("does not report lorem or ipsum according to config in regex") { + @Test + fun `does not report lorem or ipsum according to config in regex`() { val code = """ val str1 = "lorem" + "lorem" + "lorem" val str2 = "ipsum" + "ipsum" + "ipsum" @@ -88,7 +100,8 @@ class StringLiteralDuplicationSpec : Spek({ assertFindingWithConfig(code, config, 0) } - it("should not fail with invalid regex when disabled") { + @Test + fun `should not fail with invalid regex when disabled`() { val configValues = mapOf( "active" to "false", IGNORE_STRINGS_REGEX to "*lorem" @@ -97,7 +110,8 @@ class StringLiteralDuplicationSpec : Spek({ assertFindingWithConfig(regexTestingCode, config, 0) } - it("should fail with invalid regex") { + @Test + fun `should fail with invalid regex`() { val config = TestConfig(mapOf(IGNORE_STRINGS_REGEX to "*lorem")) assertThatExceptionOfType(PatternSyntaxException::class.java).isThrownBy { StringLiteralDuplication(config).compileAndLint(regexTestingCode) @@ -105,9 +119,11 @@ class StringLiteralDuplicationSpec : Spek({ } } - describe("saves string literal references") { + @Nested + inner class `saves string literal references` { - it("reports 3 locations for 'lorem'") { + @Test + fun `reports 3 locations for 'lorem'`() { val code = """ class Duplication { var s1 = "lorem" @@ -121,9 +137,11 @@ class StringLiteralDuplicationSpec : Spek({ } } - describe("multiline strings with string interpolation") { + @Nested + inner class `multiline strings with string interpolation` { - it("does not report duplicated parts in multiline strings") { + @Test + fun `does not report duplicated parts in multiline strings`() { val code = """ // does not report because it treats the multiline string parts as one string val str = ""${'"'} @@ -136,7 +154,7 @@ class StringLiteralDuplicationSpec : Spek({ } } } -}) +} private fun assertFindingWithConfig(code: String, config: TestConfig, expected: Int) { val findings = StringLiteralDuplication(config).compileAndLint(code) diff --git a/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/TooManyFunctionsSpec.kt b/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/TooManyFunctionsSpec.kt index b32421a0d31..94fdf210aa3 100644 --- a/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/TooManyFunctionsSpec.kt +++ b/detekt-rules-complexity/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/TooManyFunctionsSpec.kt @@ -3,8 +3,8 @@ package io.gitlab.arturbosch.detekt.rules.complexity 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 private const val THRESHOLD_IN_FILES = "thresholdInFiles" private const val THRESHOLD_IN_CLASSES = "thresholdInClasses" @@ -15,10 +15,11 @@ private const val IGNORE_DEPRECATED = "ignoreDeprecated" private const val IGNORE_PRIVATE = "ignorePrivate" private const val IGNORE_OVERRIDDEN = "ignoreOverridden" -object TooManyFunctionsSpec : Spek({ - describe("different declarations with one function as threshold") { +class TooManyFunctionsSpec { + @Nested + inner class `different declarations with one function as threshold` { - val rule by memoized { + val rule = TooManyFunctions( TestConfig( mapOf( @@ -30,9 +31,9 @@ object TooManyFunctionsSpec : Spek({ ) ) ) - } - it("finds one function in class") { + @Test + fun `finds one function in class`() { val code = """ class A { fun a() = Unit @@ -44,7 +45,8 @@ object TooManyFunctionsSpec : Spek({ assertThat(findings).hasTextLocations(6 to 7) } - it("finds one function in object") { + @Test + fun `finds one function in object`() { val code = """ object O { fun o() = Unit @@ -56,7 +58,8 @@ object TooManyFunctionsSpec : Spek({ assertThat(findings).hasTextLocations(7 to 8) } - it("finds one function in interface") { + @Test + fun `finds one function in interface`() { val code = """ interface I { fun i() @@ -68,7 +71,8 @@ object TooManyFunctionsSpec : Spek({ assertThat(findings).hasTextLocations(10 to 11) } - it("finds one function in enum") { + @Test + fun `finds one function in enum`() { val code = """ enum class E { A; @@ -81,13 +85,15 @@ object TooManyFunctionsSpec : Spek({ assertThat(findings).hasTextLocations(11 to 12) } - it("finds one function in file") { + @Test + fun `finds one function in file`() { val code = "fun f() = Unit" assertThat(rule.compileAndLint(code)).hasSize(1) } - it("finds one function in file ignoring other declarations") { + @Test + fun `finds one function in file ignoring other declarations`() { val code = """ fun f1() = Unit class C @@ -101,7 +107,8 @@ object TooManyFunctionsSpec : Spek({ assertThat(rule.compileAndLint(code)).hasSize(1) } - it("finds one function in nested class") { + @Test + fun `finds one function in nested class`() { val code = """ class A { class B { @@ -115,7 +122,8 @@ object TooManyFunctionsSpec : Spek({ assertThat(findings).hasTextLocations(20 to 21) } - describe("different deprecated functions") { + @Nested + inner class `different deprecated functions` { val code = """ @Deprecated("") fun f() { @@ -127,12 +135,14 @@ object TooManyFunctionsSpec : Spek({ } } """ - it("finds all deprecated functions per default") { + @Test + fun `finds all deprecated functions per default`() { assertThat(rule.compileAndLint(code)).hasSize(2) } - it("finds no deprecated functions") { + @Test + fun `finds no deprecated functions`() { val configuredRule = TooManyFunctions( TestConfig( mapOf( @@ -146,7 +156,8 @@ object TooManyFunctionsSpec : Spek({ } } - describe("different private functions") { + @Nested + inner class `different private functions` { val code = """ class A { @@ -154,11 +165,13 @@ object TooManyFunctionsSpec : Spek({ } """ - it("finds the private function per default") { + @Test + fun `finds the private function per default`() { assertThat(rule.compileAndLint(code)).hasSize(1) } - it("finds no private functions") { + @Test + fun `finds no private functions`() { val configuredRule = TooManyFunctions( TestConfig( mapOf( @@ -172,9 +185,11 @@ object TooManyFunctionsSpec : Spek({ } } - describe("false negative when private and deprecated functions are ignored - #1439") { + @Nested + inner class `false negative when private and deprecated functions are ignored - #1439` { - it("should not report when file has no public functions") { + @Test + fun `should not report when file has no public functions`() { val code = """ class A { private fun a() = Unit @@ -208,7 +223,8 @@ object TooManyFunctionsSpec : Spek({ } } - describe("overridden functions") { + @Nested + inner class `overridden functions` { val code = """ interface I1 { @@ -222,7 +238,8 @@ object TooManyFunctionsSpec : Spek({ } """ - it("should not report class with overridden functions, if ignoreOverridden is enabled") { + @Test + fun `should not report class with overridden functions, if ignoreOverridden is enabled`() { val configuredRule = TooManyFunctions( TestConfig( mapOf( @@ -235,7 +252,8 @@ object TooManyFunctionsSpec : Spek({ assertThat(configuredRule.compileAndLint(code)).isEmpty() } - it("should count overridden functions, if ignoreOverridden is disabled") { + @Test + fun `should count overridden functions, if ignoreOverridden is disabled`() { val configuredRule = TooManyFunctions( TestConfig( mapOf( @@ -249,4 +267,4 @@ object TooManyFunctionsSpec : Spek({ } } } -}) +}