Skip to content

Commit

Permalink
Migrate detekt-rules-complexity tests to JUnit (#4566)
Browse files Browse the repository at this point in the history
  • Loading branch information
3flex committed Feb 7, 2022
1 parent a2fd61d commit 780849f
Show file tree
Hide file tree
Showing 14 changed files with 415 additions and 264 deletions.
3 changes: 1 addition & 2 deletions detekt-rules-complexity/build.gradle.kts
Expand Up @@ -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)
}
Expand Up @@ -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 }
Expand All @@ -18,8 +19,9 @@ class ComplexConditionSpec : Spek({
}
"""

it("reports some complex conditions") {
@Test
fun `reports some complex conditions`() {
assertThat(ComplexCondition().compileAndLint(code)).hasSize(3)
}
}
})
}
Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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 {
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
}
}
})
}
Expand Up @@ -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<String, Any> = 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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -71,7 +76,8 @@ class ComplexMethodSpec : Spek({
}
}

context("nesting functions") {
@Nested
inner class `nesting functions` {

val code = """
fun test() {
Expand All @@ -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",
Expand All @@ -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)

Expand All @@ -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 = """
Expand All @@ -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<Any> {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 780849f

Please sign in to comment.