mirror of
https://github.com/jlengrand/detekt.git
synced 2026-03-10 08:11:23 +00:00
Reduce test discovery for rules-complexity module (#2949)
This commit is contained in:
@@ -9,8 +9,8 @@ import org.spekframework.spek2.style.specification.describe
|
||||
class ComplexInterfaceSpec : Spek({
|
||||
|
||||
val subject by memoized { ComplexInterface(threshold = THRESHOLD) }
|
||||
val staticDeclarationsConfig = TestConfig(mapOf(ComplexInterface.INCLUDE_STATIC_DECLARATIONS to "true"))
|
||||
val privateDeclarationsConfig = TestConfig(mapOf(ComplexInterface.INCLUDE_PRIVATE_DECLARATIONS to "true"))
|
||||
val staticDeclarationsConfig by memoized { TestConfig(mapOf(ComplexInterface.INCLUDE_STATIC_DECLARATIONS to "true")) }
|
||||
val privateDeclarationsConfig by memoized { TestConfig(mapOf(ComplexInterface.INCLUDE_PRIVATE_DECLARATIONS to "true")) }
|
||||
|
||||
describe("ComplexInterface rule positives") {
|
||||
|
||||
|
||||
@@ -72,40 +72,29 @@ class ComplexMethodSpec : Spek({
|
||||
}
|
||||
""".trimIndent()
|
||||
|
||||
fun execute(config: TestConfig, expectedValue: Int) {
|
||||
val findings = ComplexMethod(config, threshold = 1).lint(code)
|
||||
|
||||
assertThat(findings).hasSourceLocations(SourceLocation(1, 5))
|
||||
|
||||
assertThat(findings.first())
|
||||
.isThresholded()
|
||||
.withValue(expectedValue)
|
||||
.withThreshold(1)
|
||||
}
|
||||
|
||||
it("counts three with nesting function 'forEach'") {
|
||||
val config = TestConfig(mapOf(ComplexMethod.IGNORE_NESTING_FUNCTIONS to "false"))
|
||||
execute(config, expectedValue = 3)
|
||||
assertExpectedComplexityValue(code, config, expectedValue = 3)
|
||||
}
|
||||
|
||||
it("can ignore nesting functions like 'forEach'") {
|
||||
val config = TestConfig(mapOf(ComplexMethod.IGNORE_NESTING_FUNCTIONS to "true"))
|
||||
execute(config, expectedValue = 2)
|
||||
assertExpectedComplexityValue(code, config, expectedValue = 2)
|
||||
}
|
||||
|
||||
it("skips all if if the nested functions is empty") {
|
||||
val config = TestConfig(mapOf(ComplexMethod.NESTING_FUNCTIONS to ""))
|
||||
execute(config, expectedValue = 2)
|
||||
assertExpectedComplexityValue(code, config, expectedValue = 2)
|
||||
}
|
||||
|
||||
it("skips 'forEach' as it is not specified") {
|
||||
val config = TestConfig(mapOf(ComplexMethod.NESTING_FUNCTIONS to "let,apply,also"))
|
||||
execute(config, expectedValue = 2)
|
||||
assertExpectedComplexityValue(code, config, expectedValue = 2)
|
||||
}
|
||||
|
||||
it("skips 'forEach' as it is not specified list") {
|
||||
val config = TestConfig(mapOf(ComplexMethod.NESTING_FUNCTIONS to listOf("let", "apply", "also")))
|
||||
execute(config, expectedValue = 2)
|
||||
assertExpectedComplexityValue(code, config, expectedValue = 2)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -216,3 +205,14 @@ class ComplexMethodSpec : Spek({
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
private fun assertExpectedComplexityValue(code: String, config: TestConfig, expectedValue: Int) {
|
||||
val findings = ComplexMethod(config, threshold = 1).lint(code)
|
||||
|
||||
assertThat(findings).hasSourceLocations(SourceLocation(1, 5))
|
||||
|
||||
assertThat(findings.first())
|
||||
.isThresholded()
|
||||
.withValue(expectedValue)
|
||||
.withThreshold(1)
|
||||
}
|
||||
|
||||
@@ -9,19 +9,21 @@ import org.spekframework.spek2.style.specification.describe
|
||||
class LongParameterListSpec : Spek({
|
||||
|
||||
val defaultThreshold = 2
|
||||
val defaultConfig = TestConfig(mapOf(
|
||||
LongParameterList.FUNCTION_THRESHOLD to defaultThreshold,
|
||||
LongParameterList.CONSTRUCTOR_THRESHOLD to defaultThreshold
|
||||
))
|
||||
val defaultConfig by memoized {
|
||||
TestConfig(mapOf(
|
||||
LongParameterList.FUNCTION_THRESHOLD to defaultThreshold,
|
||||
LongParameterList.CONSTRUCTOR_THRESHOLD to defaultThreshold
|
||||
))
|
||||
}
|
||||
|
||||
val subject by memoized { LongParameterList(defaultConfig) }
|
||||
|
||||
describe("LongParameterList rule") {
|
||||
|
||||
val reportMessageForFunction = "The function long(a: Int, b: Int) has too many parameters. " +
|
||||
"The current threshold is set to $defaultThreshold."
|
||||
"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."
|
||||
"The current threshold is set to $defaultThreshold."
|
||||
|
||||
it("reports too long parameter list") {
|
||||
val code = "fun long(a: Int, b: Int) {}"
|
||||
@@ -90,12 +92,15 @@ class LongParameterListSpec : Spek({
|
||||
|
||||
describe("constructors and functions with ignored annotations") {
|
||||
|
||||
val config = TestConfig(mapOf(
|
||||
LongParameterList.IGNORE_ANNOTATED to listOf("Generated", "kotlin.Deprecated", "kotlin.jvm.JvmName"),
|
||||
LongParameterList.FUNCTION_THRESHOLD to 1,
|
||||
LongParameterList.CONSTRUCTOR_THRESHOLD to 1
|
||||
))
|
||||
val rule = LongParameterList(config)
|
||||
val config by memoized {
|
||||
TestConfig(mapOf(
|
||||
LongParameterList.IGNORE_ANNOTATED to listOf("Generated", "kotlin.Deprecated", "kotlin.jvm.JvmName"),
|
||||
LongParameterList.FUNCTION_THRESHOLD to 1,
|
||||
LongParameterList.CONSTRUCTOR_THRESHOLD to 1
|
||||
))
|
||||
}
|
||||
|
||||
val rule by memoized { LongParameterList(config) }
|
||||
|
||||
it("does not report long parameter list for constructors if file is annotated with ignored annotation") {
|
||||
val code = """
|
||||
|
||||
@@ -10,13 +10,15 @@ class TooManyFunctionsSpec : Spek({
|
||||
|
||||
describe("different declarations with one function as threshold") {
|
||||
|
||||
val rule = TooManyFunctions(TestConfig(mapOf(
|
||||
val rule by memoized {
|
||||
TooManyFunctions(TestConfig(mapOf(
|
||||
TooManyFunctions.THRESHOLD_IN_CLASSES to "1",
|
||||
TooManyFunctions.THRESHOLD_IN_ENUMS to "1",
|
||||
TooManyFunctions.THRESHOLD_IN_FILES to "1",
|
||||
TooManyFunctions.THRESHOLD_IN_INTERFACES to "1",
|
||||
TooManyFunctions.THRESHOLD_IN_OBJECTS to "1"
|
||||
)))
|
||||
)))
|
||||
}
|
||||
|
||||
it("finds one function in class") {
|
||||
val code = """
|
||||
@@ -120,9 +122,9 @@ class TooManyFunctionsSpec : Spek({
|
||||
|
||||
it("finds no deprecated functions") {
|
||||
val configuredRule = TooManyFunctions(TestConfig(mapOf(
|
||||
TooManyFunctions.THRESHOLD_IN_CLASSES to "1",
|
||||
TooManyFunctions.THRESHOLD_IN_FILES to "1",
|
||||
TooManyFunctions.IGNORE_DEPRECATED to "true"
|
||||
TooManyFunctions.THRESHOLD_IN_CLASSES to "1",
|
||||
TooManyFunctions.THRESHOLD_IN_FILES to "1",
|
||||
TooManyFunctions.IGNORE_DEPRECATED to "true"
|
||||
)))
|
||||
assertThat(configuredRule.compileAndLint(code)).isEmpty()
|
||||
}
|
||||
@@ -142,9 +144,9 @@ class TooManyFunctionsSpec : Spek({
|
||||
|
||||
it("finds no private functions") {
|
||||
val configuredRule = TooManyFunctions(TestConfig(mapOf(
|
||||
TooManyFunctions.THRESHOLD_IN_CLASSES to "1",
|
||||
TooManyFunctions.THRESHOLD_IN_FILES to "1",
|
||||
TooManyFunctions.IGNORE_PRIVATE to "true"
|
||||
TooManyFunctions.THRESHOLD_IN_CLASSES to "1",
|
||||
TooManyFunctions.THRESHOLD_IN_FILES to "1",
|
||||
TooManyFunctions.IGNORE_PRIVATE to "true"
|
||||
)))
|
||||
assertThat(configuredRule.compileAndLint(code)).isEmpty()
|
||||
}
|
||||
@@ -172,11 +174,11 @@ class TooManyFunctionsSpec : Spek({
|
||||
}
|
||||
"""
|
||||
val configuredRule = TooManyFunctions(TestConfig(mapOf(
|
||||
TooManyFunctions.THRESHOLD_IN_CLASSES to "1",
|
||||
TooManyFunctions.THRESHOLD_IN_FILES to "1",
|
||||
TooManyFunctions.IGNORE_PRIVATE to "true",
|
||||
TooManyFunctions.IGNORE_DEPRECATED to "true",
|
||||
TooManyFunctions.IGNORE_OVERRIDDEN to "true"
|
||||
TooManyFunctions.THRESHOLD_IN_CLASSES to "1",
|
||||
TooManyFunctions.THRESHOLD_IN_FILES to "1",
|
||||
TooManyFunctions.IGNORE_PRIVATE to "true",
|
||||
TooManyFunctions.IGNORE_DEPRECATED to "true",
|
||||
TooManyFunctions.IGNORE_OVERRIDDEN to "true"
|
||||
)))
|
||||
assertThat(configuredRule.compileAndLint(code)).isEmpty()
|
||||
}
|
||||
@@ -198,18 +200,18 @@ class TooManyFunctionsSpec : Spek({
|
||||
|
||||
it("should not report class with overridden functions, if ignoreOverridden is enabled") {
|
||||
val configuredRule = TooManyFunctions(TestConfig(mapOf(
|
||||
TooManyFunctions.THRESHOLD_IN_CLASSES to "1",
|
||||
TooManyFunctions.THRESHOLD_IN_FILES to "1",
|
||||
TooManyFunctions.IGNORE_OVERRIDDEN to "true"
|
||||
TooManyFunctions.THRESHOLD_IN_CLASSES to "1",
|
||||
TooManyFunctions.THRESHOLD_IN_FILES to "1",
|
||||
TooManyFunctions.IGNORE_OVERRIDDEN to "true"
|
||||
)))
|
||||
assertThat(configuredRule.compileAndLint(code)).isEmpty()
|
||||
}
|
||||
|
||||
it("should count overridden functions, if ignoreOverridden is disabled") {
|
||||
val configuredRule = TooManyFunctions(TestConfig(mapOf(
|
||||
TooManyFunctions.THRESHOLD_IN_CLASSES to "1",
|
||||
TooManyFunctions.THRESHOLD_IN_FILES to "1",
|
||||
TooManyFunctions.IGNORE_OVERRIDDEN to "false"
|
||||
TooManyFunctions.THRESHOLD_IN_CLASSES to "1",
|
||||
TooManyFunctions.THRESHOLD_IN_FILES to "1",
|
||||
TooManyFunctions.IGNORE_OVERRIDDEN to "false"
|
||||
)))
|
||||
assertThat(configuredRule.compileAndLint(code)).hasSize(1)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user