Skip to content

Commit

Permalink
Inline Cases enum and inline other external test code into the test c…
Browse files Browse the repository at this point in the history
…lasses (#6107)
  • Loading branch information
TWiStErRob committed May 20, 2023
1 parent 940af07 commit 93de544
Show file tree
Hide file tree
Showing 41 changed files with 1,121 additions and 1,286 deletions.
145 changes: 0 additions & 145 deletions detekt-core/src/test/resources/cases/ComplexClass.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.gitlab.arturbosch.detekt.rules.complexity

import io.github.detekt.test.utils.resourceAsPath
import io.gitlab.arturbosch.detekt.api.SourceLocation
import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.assertThat
Expand Down Expand Up @@ -117,7 +116,56 @@ class CyclomaticComplexMethodSpec {
@Nested
inner class `several complex methods` {

val path = resourceAsPath("ComplexMethods.kt")
val code = """
// reports 1 - only if ignoreSingleWhenExpression = false
fun complexMethodWithSingleWhen1(i: Int) =
when (i) {
1 -> print("one")
2 -> print("two")
3 -> print("three")
else -> print(i)
}
// reports 1 - only if ignoreSingleWhenExpression = false
fun complexMethodWithSingleWhen2(i: Int) {
when (i) {
1 -> print("one")
2 -> print("two")
3 -> print("three")
else -> print(i)
}
}
// reports 1 - only if ignoreSingleWhenExpression = false
fun complexMethodWithSingleWhen3(i: Int): String {
return when (i) {
1 -> "one"
2 -> "two"
3 -> "three"
else -> ""
}
}
// reports 1 - only if ignoreSingleWhenExpression = false
fun complexMethodWithSingleWhen4(i: Int) = when (i) {
1 -> "one"
2 -> "two"
3 -> "three"
else -> ""
}
// reports 1
fun complexMethodWith2Statements(i: Int) {
when (i) {
1 -> print("one")
2 -> print("two")
3 -> print("three")
else -> print(i)
}
if (i == 1) {
}
}
""".trimIndent()

@Test
fun `does not report complex methods with a single when expression`() {
Expand All @@ -127,20 +175,20 @@ class CyclomaticComplexMethodSpec {
)
val subject = CyclomaticComplexMethod(config)

assertThat(subject.lint(path)).hasStartSourceLocations(SourceLocation(43, 5))
assertThat(subject.lint(code)).hasStartSourceLocations(SourceLocation(39, 5))
}

@Test
fun `reports all complex methods`() {
val config = TestConfig("threshold" to "4")
val subject = CyclomaticComplexMethod(config)

assertThat(subject.lint(path)).hasStartSourceLocations(
SourceLocation(6, 5),
SourceLocation(15, 5),
SourceLocation(25, 5),
SourceLocation(35, 5),
SourceLocation(43, 5)
assertThat(subject.lint(code)).hasStartSourceLocations(
SourceLocation(2, 5),
SourceLocation(11, 5),
SourceLocation(21, 5),
SourceLocation(31, 5),
SourceLocation(39, 5)
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.gitlab.arturbosch.detekt.rules.complexity

import io.github.detekt.test.utils.resourceAsPath
import io.gitlab.arturbosch.detekt.api.SourceLocation
import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.assertThat
Expand All @@ -13,10 +12,34 @@ private fun subject(threshold: Int) = LargeClass(TestConfig("threshold" to thres
class LargeClassSpec {

@Test
fun `should detect only the nested large class which exceeds threshold 70`() {
val findings = subject(threshold = 70).lint(resourceAsPath("NestedClasses.kt"))
fun `should detect only the nested large class which exceeds the threshold`() {
val code = """
class NestedClasses {
private val i = 0
class InnerClass {
class NestedInnerClass {
fun nestedMethod() {
fun nestedLocalMethod() {
println()
}
nestedLocalMethod()
}
}
}
}
/**
* Top level members must be skipped for LargeClass rule
*/
val aTopLevelPropertyOfNestedClasses = 0
""".trimIndent()
val findings = subject(threshold = 4).lint(code)
assertThat(findings).hasSize(1)
assertThat(findings).hasStartSourceLocations(SourceLocation(12, 15))
assertThat(findings).hasStartSourceLocations(SourceLocation(7, 15))
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.gitlab.arturbosch.detekt.rules.complexity

import io.github.detekt.test.utils.resourceAsPath
import io.gitlab.arturbosch.detekt.api.ThresholdedCodeSmell
import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.assertThat
Expand All @@ -16,8 +15,48 @@ class NestedBlockDepthSpec {
val subject = NestedBlockDepth(defaultConfig)

@Test
fun `should detect only the nested large class`() {
subject.lint(resourceAsPath("NestedClasses.kt"))
fun `should ignore class nesting levels`() {
val code = """
class NestedClasses {
class InnerClass {
class NestedInnerClass {
fun nestedLongMethod() {
if (true) {
if (true) {
if (true) {
5.run {
this.let {
listOf(1, 2, 3).map { it * 2 }
.groupBy(Int::toString, Int::toString)
}
}
}
}
}
try {
for (i in 1..5) {
when (i) {
1 -> print(1)
}
}
} finally {
}
fun nestedLocalMethod() {
println()
}
nestedLocalMethod()
}
}
}
}
""".trimIndent()
subject.lint(code)
assertThat(subject.findings).hasSize(1)
assertThat((subject.findings[0] as ThresholdedCodeSmell).value).isEqualTo(5)
}
Expand Down
52 changes: 0 additions & 52 deletions detekt-rules-complexity/src/test/resources/ComplexMethods.kt

This file was deleted.

Loading

0 comments on commit 93de544

Please sign in to comment.