Migrate tests in detekt-rules-empty to junit (#4522)

This commit is contained in:
marschwar
2022-01-25 14:19:43 +01:00
committed by GitHub
parent 576ae5f540
commit 4a0076f164
8 changed files with 185 additions and 107 deletions

View File

@@ -5,6 +5,5 @@ plugins {
dependencies {
compileOnly(projects.detektApi)
testImplementation(projects.detektTest)
testImplementation(libs.bundles.testImplementation)
testRuntimeOnly(libs.spek.runner)
testImplementation(libs.assertj)
}

View File

@@ -6,25 +6,34 @@ import io.gitlab.arturbosch.detekt.test.compileAndLint
import io.gitlab.arturbosch.detekt.test.lint
import io.gitlab.arturbosch.detekt.test.yamlConfig
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 EmptyBlocksMultiRuleSpec : Spek({
class EmptyBlocksMultiRuleSpec {
val subject by memoized { EmptyBlocks() }
private val file = compileForTest(resourceAsPath("Empty.kt"))
val file by memoized { compileForTest(resourceAsPath("Empty.kt")) }
private lateinit var subject: EmptyBlocks
describe("multi rule with all empty block rules") {
@BeforeEach
fun createSubject() {
subject = EmptyBlocks()
}
it("should report one finding per rule") {
@Nested
inner class `multi rule with all empty block rules` {
@Test
fun `should report one finding per rule`() {
val findings = subject.lint(file)
// -1 because the empty kt file rule doesn't get triggered in the 'Empty' test file
val rulesSize = subject.rules.size - 1
assertThat(findings).hasSize(rulesSize)
}
it("should not report any as all empty block rules are deactivated") {
@Test
fun `should not report any as all empty block rules are deactivated`() {
val config = yamlConfig("deactivated-empty-blocks.yml")
val ruleSet = EmptyCodeProvider().instance(config)
@@ -34,11 +43,13 @@ class EmptyBlocksMultiRuleSpec : Spek({
assertThat(findings).isEmpty()
}
it("reports an empty kt file") {
@Test
fun `reports an empty kt file`() {
assertThat(subject.compileAndLint("")).hasSize(1)
}
it("reports no duplicated findings - issue #1605") {
@Test
fun `reports no duplicated findings - issue #1605`() {
val findings = subject.compileAndLint(
"""
class EmptyBlocks {
@@ -55,4 +66,4 @@ class EmptyBlocksMultiRuleSpec : Spek({
assertThat(findings).hasSize(2)
}
}
})
}

View File

@@ -3,21 +3,24 @@ package io.gitlab.arturbosch.detekt.rules.empty
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 EmptyClassBlockSpec : Spek({
class EmptyClassBlockSpec {
val subject by memoized { EmptyClassBlock(Config.empty) }
private val subject = EmptyClassBlock(Config.empty)
describe("EmptyClassBlock rule") {
@Nested
inner class `EmptyClassBlock rule` {
it("reports the empty class body") {
@Test
fun `reports the empty class body`() {
val code = "class SomeClass {}"
assertThat(subject.compileAndLint(code)).hasSize(1)
}
it("does not report class with comments in the body") {
@Test
fun `does not report class with comments in the body`() {
val code = """
class SomeClass {
// Some comment to explain what this class is supposed to do
@@ -26,7 +29,8 @@ class EmptyClassBlockSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}
it("does not report class with multiline comments in the body") {
@Test
fun `does not report class with multiline comments in the body`() {
val code = """
class SomeClass {
/*
@@ -37,7 +41,8 @@ class EmptyClassBlockSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}
it("reports the empty nested class body") {
@Test
fun `reports the empty nested class body`() {
val code = """
class SomeClass {
class EmptyClass {}
@@ -46,14 +51,16 @@ class EmptyClassBlockSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1)
}
it("reports the empty object body") {
@Test
fun `reports the empty object body`() {
val code = "object SomeObject {}"
val findings = subject.compileAndLint(code)
assertThat(findings).hasSize(1)
assertThat(findings).hasTextLocations(18 to 20)
}
it("does not report the object if it is of an anonymous class") {
@Test
fun `does not report the object if it is of an anonymous class`() {
val code = """
open class Open
@@ -64,4 +71,4 @@ class EmptyClassBlockSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}
}
})
}

View File

@@ -9,13 +9,13 @@ import io.gitlab.arturbosch.detekt.test.compileAndLint
import io.gitlab.arturbosch.detekt.test.lint
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 ALLOWED_EXCEPTION_NAME_REGEX = "allowedExceptionNameRegex"
class EmptyCodeSpec : Spek({
class EmptyCodeSpec {
val regexTestingCode = """
fun f() {
@@ -24,13 +24,16 @@ class EmptyCodeSpec : Spek({
}
}"""
describe("EmptyCatchBlock rule") {
@Nested
inner class `EmptyCatchBlock rule` {
it("findsEmptyCatch") {
@Test
fun `findsEmptyCatch`() {
test { EmptyCatchBlock(Config.empty) }
}
it("findsEmptyNestedCatch") {
@Test
fun `findsEmptyNestedCatch`() {
val code = """
fun f() {
try {
@@ -43,7 +46,8 @@ class EmptyCodeSpec : Spek({
assertThat(EmptyCatchBlock(Config.empty).compileAndLint(code)).hasSize(1)
}
it("doesNotReportIgnoredOrExpectedException") {
@Test
fun `doesNotReportIgnoredOrExpectedException`() {
val code = """
fun f() {
try {
@@ -54,7 +58,8 @@ class EmptyCodeSpec : Spek({
assertThat(EmptyCatchBlock(Config.empty).compileAndLint(code)).isEmpty()
}
it("doesNotReportEmptyCatchWithConfig") {
@Test
fun `doesNotReportEmptyCatchWithConfig`() {
val code = """
fun f() {
try {
@@ -65,55 +70,68 @@ class EmptyCodeSpec : Spek({
assertThat(EmptyCatchBlock(config).compileAndLint(code)).isEmpty()
}
it("findsEmptyFinally") {
@Test
fun `findsEmptyFinally`() {
test { EmptyFinallyBlock(Config.empty) }
}
it("findsEmptyIf") {
@Test
fun `findsEmptyIf`() {
test { EmptyIfBlock(Config.empty) }
}
it("findsEmptyElse") {
@Test
fun `findsEmptyElse`() {
test { EmptyElseBlock(Config.empty) }
}
it("findsEmptyFor") {
@Test
fun `findsEmptyFor`() {
test { EmptyForBlock(Config.empty) }
}
it("findsEmptyWhile") {
@Test
fun `findsEmptyWhile`() {
test { EmptyWhileBlock(Config.empty) }
}
it("findsEmptyDoWhile") {
@Test
fun `findsEmptyDoWhile`() {
test { EmptyDoWhileBlock(Config.empty) }
}
it("findsEmptyFun") {
@Test
fun `findsEmptyFun`() {
test { EmptyFunctionBlock(Config.empty) }
}
it("findsEmptyClass") {
@Test
fun `findsEmptyClass`() {
test { EmptyClassBlock(Config.empty) }
}
it("findsEmptyTry") {
@Test
fun `findsEmptyTry`() {
test { EmptyTryBlock(Config.empty) }
}
it("findsEmptyWhen") {
@Test
fun `findsEmptyWhen`() {
test { EmptyWhenBlock(Config.empty) }
}
it("findsEmptyInit") {
@Test
fun `findsEmptyInit`() {
test { EmptyInitBlock(Config.empty) }
}
it("findsOneEmptySecondaryConstructor") {
@Test
fun `findsOneEmptySecondaryConstructor`() {
test { EmptySecondaryConstructor(Config.empty) }
}
it("doesNotFailWithInvalidRegexWhenDisabled") {
@Test
fun `doesNotFailWithInvalidRegexWhenDisabled`() {
val configValues = mapOf(
"active" to "false",
ALLOWED_EXCEPTION_NAME_REGEX to "*foo"
@@ -122,7 +140,8 @@ class EmptyCodeSpec : Spek({
assertThat(EmptyCatchBlock(config).compileAndLint(regexTestingCode)).isEmpty()
}
it("doesFailWithInvalidRegex") {
@Test
fun `doesFailWithInvalidRegex`() {
val configValues = mapOf(ALLOWED_EXCEPTION_NAME_REGEX to "*foo")
val config = TestConfig(configValues)
assertThatExceptionOfType(PatternSyntaxException::class.java).isThrownBy {
@@ -130,7 +149,7 @@ class EmptyCodeSpec : Spek({
}
}
}
})
}
private fun test(block: () -> Rule) {
val rule = block()

View File

@@ -4,56 +4,64 @@ 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
internal class EmptyDefaultConstructorSpec : Spek({
internal class EmptyDefaultConstructorSpec {
describe("EmptyDefaultConstructor rule") {
@Nested
inner class `EmptyDefaultConstructor rule` {
it("EmptyConstructor") {
@Test
fun `EmptyConstructor`() {
val code = """
class EmptyConstructor()
"""
assertThat(EmptyDefaultConstructor(Config.empty).compileAndLint(code)).hasSize(1)
}
it("EmptyPrimaryConstructor") {
@Test
fun `EmptyPrimaryConstructor`() {
val code = """
class EmptyPrimaryConstructor constructor()
"""
assertThat(EmptyDefaultConstructor(Config.empty).compileAndLint(code)).hasSize(1)
}
it("EmptyPublicPrimaryConstructor") {
@Test
fun `EmptyPublicPrimaryConstructor`() {
val code = """
class EmptyPublicPrimaryConstructor public constructor()
"""
assertThat(EmptyDefaultConstructor(Config.empty).compileAndLint(code)).hasSize(1)
}
it("PrimaryConstructorWithParameter") {
@Test
fun `PrimaryConstructorWithParameter`() {
val code = """
class PrimaryConstructorWithParameter constructor(x: Int)
"""
assertThat(EmptyDefaultConstructor(Config.empty).compileAndLint(code)).isEmpty()
}
it("PrimaryConstructorWithAnnotation") {
@Test
fun `PrimaryConstructorWithAnnotation`() {
val code = """
class PrimaryConstructorWithAnnotation @SafeVarargs constructor()
"""
assertThat(EmptyDefaultConstructor(Config.empty).compileAndLint(code)).isEmpty()
}
it("PrivatePrimaryConstructor") {
@Test
fun `PrivatePrimaryConstructor`() {
val code = """
class PrivatePrimaryConstructor private constructor()
"""
assertThat(EmptyDefaultConstructor(Config.empty).compileAndLint(code)).isEmpty()
}
it("EmptyConstructorIsCalled") {
@Test
fun `EmptyConstructorIsCalled`() {
val code = """
class EmptyConstructorIsCalled() {
@@ -63,32 +71,36 @@ internal class EmptyDefaultConstructorSpec : Spek({
assertThat(EmptyDefaultConstructor(Config.empty).compileAndLint(code)).isEmpty()
}
it("should not report empty constructors for classes with expect keyword - #1362") {
@Test
fun `should not report empty constructors for classes with expect keyword - #1362`() {
val code = """
expect class NeedsConstructor()
"""
assertThat(EmptyDefaultConstructor(Config.empty).lint(code)).isEmpty()
}
it("should not report empty constructors for classes with actual - #1362") {
@Test
fun `should not report empty constructors for classes with actual - #1362`() {
val code = """
actual class NeedsConstructor actual constructor()
"""
assertThat(EmptyDefaultConstructor(Config.empty).lint(code)).isEmpty()
}
it("should not report empty constructors for annotation classes with expect keyword - #1362") {
@Test
fun `should not report empty constructors for annotation classes with expect keyword - #1362`() {
val code = """
expect annotation class NeedsConstructor()
"""
assertThat(EmptyDefaultConstructor(Config.empty).lint(code)).isEmpty()
}
it("should not report empty constructors for annotation classes with actual - #1362") {
@Test
fun `should not report empty constructors for annotation classes with actual - #1362`() {
val code = """
actual annotation class NeedsConstructor actual constructor()
"""
assertThat(EmptyDefaultConstructor(Config.empty).lint(code)).isEmpty()
}
}
})
}

View File

@@ -3,15 +3,17 @@ package io.gitlab.arturbosch.detekt.rules.empty
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 EmptyElseBlockSpec : Spek({
class EmptyElseBlockSpec {
val subject by memoized { EmptyElseBlock(Config.empty) }
private val subject = EmptyElseBlock(Config.empty)
describe("EmptyElseBlock rule") {
it("reports empty else block") {
@Nested
inner class `EmptyElseBlock rule` {
@Test
fun `reports empty else block`() {
val code = """
fun f() {
val i = 0
@@ -25,7 +27,8 @@ class EmptyElseBlockSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1)
}
it("reports empty else blocks with trailing semicolon") {
@Test
fun `reports empty else blocks with trailing semicolon`() {
val code = """
fun f() {
val i = 0
@@ -36,7 +39,9 @@ class EmptyElseBlockSpec : Spek({
"""
assertThat(subject.compileAndLint(code)).hasSize(1)
}
it("reports empty else with trailing semicolon on new line") {
@Test
fun `reports empty else with trailing semicolon on new line`() {
val code = """
fun f() {
var i = 0
@@ -50,7 +55,8 @@ class EmptyElseBlockSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1)
}
it("reports empty else with trailing semicolon and braces") {
@Test
fun `reports empty else with trailing semicolon and braces`() {
val code = """
fun f() {
var i = 0
@@ -64,7 +70,8 @@ class EmptyElseBlockSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1)
}
it("does not report nonempty else with braces") {
@Test
fun `does not report nonempty else with braces`() {
val code = """
fun f() {
var i = 0
@@ -78,7 +85,8 @@ class EmptyElseBlockSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}
it("does not report nonempty else without braces") {
@Test
fun `does not report nonempty else without braces`() {
val code = """
fun f() {
var i = 0
@@ -90,7 +98,8 @@ class EmptyElseBlockSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}
it("does not report nonempty else without braces but semicolon") {
@Test
fun `does not report nonempty else without braces but semicolon`() {
val code = """
fun f() {
var i = 0
@@ -102,4 +111,4 @@ class EmptyElseBlockSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}
}
})
}

View File

@@ -4,19 +4,21 @@ 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
private const val IGNORE_OVERRIDDEN_FUNCTIONS = "ignoreOverriddenFunctions"
private const val IGNORE_OVERRIDDEN = "ignoreOverridden"
class EmptyFunctionBlockSpec : Spek({
class EmptyFunctionBlockSpec {
val subject by memoized { EmptyFunctionBlock(Config.empty) }
private val subject = EmptyFunctionBlock(Config.empty)
describe("EmptyFunctionBlock rule") {
@Nested
inner class `EmptyFunctionBlock rule` {
it("should flag function with protected modifier") {
@Test
fun `should flag function with protected modifier`() {
val code = """
class A {
protected fun stuff() {}
@@ -24,7 +26,8 @@ class EmptyFunctionBlockSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSourceLocation(2, 27)
}
it("should not flag function with open modifier") {
@Test
fun `should not flag function with open modifier`() {
val code = """
open class A {
open fun stuff() {}
@@ -32,7 +35,8 @@ class EmptyFunctionBlockSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}
it("should not flag a default function in an interface") {
@Test
fun `should not flag a default function in an interface`() {
val code = """
interface I {
fun stuff() {}
@@ -40,7 +44,8 @@ class EmptyFunctionBlockSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}
it("should flag the nested empty function") {
@Test
fun `should flag the nested empty function`() {
val code = """
fun a() {
fun b() {}
@@ -48,7 +53,8 @@ class EmptyFunctionBlockSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSourceLocation(2, 13)
}
context("some overridden functions") {
@Nested
inner class `some overridden functions` {
val code = """
fun empty() {}
@@ -73,17 +79,20 @@ class EmptyFunctionBlockSpec : Spek({
}
}"""
it("should flag empty block in overridden function") {
@Test
fun `should flag empty block in overridden function`() {
assertThat(subject.compileAndLint(code)).hasSize(2)
}
it("should not flag overridden functions") {
@Test
fun `should not flag overridden functions`() {
val config = TestConfig(mapOf(IGNORE_OVERRIDDEN_FUNCTIONS to "true"))
assertThat(EmptyFunctionBlock(config).compileAndLint(code)).hasSourceLocation(1, 13)
}
}
context("some overridden functions") {
@Nested
inner class `some overridden functions when implementing interfaces` {
val code = """
private interface Listener {
fun listenThis()
@@ -101,14 +110,17 @@ class EmptyFunctionBlockSpec : Spek({
}
}
"""
it("should not flag overridden functions with commented body") {
@Test
fun `should not flag overridden functions with commented body`() {
assertThat(subject.compileAndLint(code)).hasSourceLocation(12, 31)
}
it("should not flag overridden functions with ignoreOverridden") {
@Test
fun `should not flag overridden functions with ignoreOverridden`() {
val config = TestConfig(mapOf(IGNORE_OVERRIDDEN to "true"))
assertThat(EmptyFunctionBlock(config).compileAndLint(code)).isEmpty()
}
}
}
})
}

View File

@@ -3,16 +3,18 @@ package io.gitlab.arturbosch.detekt.rules.empty
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 EmptyIfBlockSpec : Spek({
class EmptyIfBlockSpec {
val subject by memoized { EmptyIfBlock(Config.empty) }
private val subject = EmptyIfBlock(Config.empty)
describe("EmptyIfBlock rule") {
@Nested
inner class `EmptyIfBlock rule` {
it("reports empty if with trailing semicolon") {
@Test
fun `reports empty if with trailing semicolon`() {
val code = """
fun f() {
var i = 0
@@ -23,7 +25,8 @@ class EmptyIfBlockSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1)
}
it("reports empty if with trailing semicolon on new line") {
@Test
fun `reports empty if with trailing semicolon on new line`() {
val code = """
fun f() {
var i = 0
@@ -35,7 +38,8 @@ class EmptyIfBlockSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1)
}
it("reports empty if with trailing semicolon and braces") {
@Test
fun `reports empty if with trailing semicolon and braces`() {
val code = """
fun f() {
var i = 0
@@ -47,7 +51,8 @@ class EmptyIfBlockSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1)
}
it("does not report nonempty if with braces") {
@Test
fun `does not report nonempty if with braces`() {
val code = """
fun f() {
var i = 0
@@ -59,7 +64,8 @@ class EmptyIfBlockSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}
it("does not report nonempty if without braces") {
@Test
fun `does not report nonempty if without braces`() {
val code = """
fun f() {
var i = 0
@@ -69,7 +75,8 @@ class EmptyIfBlockSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}
it("does not report nonempty if without braces but semicolon") {
@Test
fun `does not report nonempty if without braces but semicolon`() {
val code = """
fun f() {
var i = 0
@@ -79,7 +86,8 @@ class EmptyIfBlockSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}
it("does not report empty if but nonempty else") {
@Test
fun `does not report empty if but nonempty else`() {
val code = """
fun f() {
var i = 0
@@ -90,7 +98,8 @@ class EmptyIfBlockSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}
it("does not report empty if and else-if but nonempty else") {
@Test
fun `does not report empty if and else-if but nonempty else`() {
val code = """
fun f() {
var i = 0
@@ -102,4 +111,4 @@ class EmptyIfBlockSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}
}
})
}