Skip to content

Commit

Permalink
Migrate tests in detekt-rules-errorprone to junit (#4523)
Browse files Browse the repository at this point in the history
* Migrate tests in detekt-rules-errorprone to junit

* change names of classes and function that may be a problem on windows

* WIP: remove test class that is causing issues

* re-add IgnoredReturnValueSpec.kt

* Fix merge

Co-authored-by: Markus Schwarz <post@markus-schwarz.net>
  • Loading branch information
marschwar and Markus Schwarz committed Feb 1, 2022
1 parent fb1b604 commit c34f5fa
Show file tree
Hide file tree
Showing 35 changed files with 1,129 additions and 738 deletions.
3 changes: 1 addition & 2 deletions detekt-rules-errorprone/build.gradle.kts
Expand Up @@ -6,6 +6,5 @@ dependencies {
compileOnly(projects.detektApi)
implementation(projects.detektTooling)
testImplementation(projects.detektTest)
testImplementation(libs.bundles.testImplementation)
testRuntimeOnly(libs.spek.runner)
testImplementation(libs.assertj)
}
@@ -1,22 +1,23 @@
package io.gitlab.arturbosch.detekt.rules.bugs

import io.gitlab.arturbosch.detekt.api.Config
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

object AvoidReferentialEqualitySpec : Spek({
setupKotlinEnvironment()
@KotlinCoreEnvironmentTest
class AvoidReferentialEqualitySpec(private val env: KotlinCoreEnvironment) {

val env: KotlinCoreEnvironment by memoized()
@Nested
inner class `ReferentialEquality with defaults` {
private val subject = AvoidReferentialEquality(Config.empty)

describe("ReferentialEquality with defaults") {
val subject by memoized { AvoidReferentialEquality(Config.empty) }
it("reports usage of === for strings") {
@Test
fun `reports usage of === for strings`() {
val code = """
val s = "a string"
val b = s === "something"
Expand All @@ -28,7 +29,9 @@ object AvoidReferentialEqualitySpec : Spek({

assertThat(actual).hasSize(3)
}
it("reports usage of === with nullable") {

@Test
fun `reports usage of === with nullable`() {
val code = """
var s: String? = "a string"
val b1 = s === "something"
Expand All @@ -41,7 +44,9 @@ object AvoidReferentialEqualitySpec : Spek({

assertThat(actual).hasSize(4)
}
it("reports usage of !== for strings") {

@Test
fun `reports usage of !== for strings`() {
val code = """
var s: String = "a string"
val b = s !== "something"
Expand All @@ -51,7 +56,9 @@ object AvoidReferentialEqualitySpec : Spek({

assertThat(actual).hasSize(1)
}
it("ignores usage of === for non strings") {

@Test
fun `ignores usage of === for non strings`() {
val code = """
val i = 42
val l = 99L
Expand All @@ -63,7 +70,9 @@ object AvoidReferentialEqualitySpec : Spek({

assertThat(actual).isEmpty()
}
it("ignores usage of == for strings") {

@Test
fun `ignores usage of == for strings`() {
val code = """
val s = "a string"
val b = s == "something"
Expand All @@ -75,7 +84,9 @@ object AvoidReferentialEqualitySpec : Spek({

assertThat(actual).isEmpty()
}
it("ignores usage of === with generic parameters") {

@Test
fun `ignores usage of === with generic parameters`() {
val code = """
fun <T : Any> same(one: T, two: T): Boolean = one === two
val b = same("this", "that")
Expand All @@ -87,9 +98,12 @@ object AvoidReferentialEqualitySpec : Spek({
}
}

describe("ReferentialEquality enabled for all types") {
val subject by memoized { AvoidReferentialEquality(TestConfig("forbiddenTypePatterns" to "*")) }
it("reports usage of === for strings") {
@Nested
inner class `ReferentialEquality enabled for all types` {
private val subject = AvoidReferentialEquality(TestConfig("forbiddenTypePatterns" to "*"))

@Test
fun `reports usage of === for strings`() {
val code = """
val s = "a string"
val i = 1
Expand All @@ -103,10 +117,13 @@ object AvoidReferentialEqualitySpec : Spek({
}
}

describe("ReferentialEquality enabled for all lists") {
val pattern = """kotlin.collections.*List"""
val subject by memoized { AvoidReferentialEquality(TestConfig("forbiddenTypePatterns" to pattern)) }
it("reports usage of ===") {
@Nested
inner class `ReferentialEquality enabled for all lists` {
private val pattern = """kotlin.collections.*List"""
private val subject = AvoidReferentialEquality(TestConfig("forbiddenTypePatterns" to pattern))

@Test
fun `reports usage of ===`() {
val code = """
val listA = listOf(1)
val listB = listOf(1)
Expand All @@ -118,7 +135,9 @@ object AvoidReferentialEqualitySpec : Spek({

assertThat(actual).hasSize(3)
}
it("ignores usage of ==") {

@Test
fun `ignores usage of ==`() {
val code = """
val listA = listOf(1)
val listB = listOf(1)
Expand All @@ -131,4 +150,4 @@ object AvoidReferentialEqualitySpec : Spek({
assertThat(actual).isEmpty()
}
}
})
}
Expand Up @@ -2,14 +2,16 @@ package io.gitlab.arturbosch.detekt.rules.bugs

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 CastToNullableTypeSpec : Spek({
val subject by memoized { CastToNullableType() }
class CastToNullableTypeSpec {
private val subject = CastToNullableType()

describe("CastToNullableTypeSpec rule") {
it("casting to nullable types") {
@Nested
inner class `CastToNullableTypeSpec rule` {
@Test
fun `casting to nullable types`() {
val code = """
fun foo(a: Any?) {
val x: String? = a as String?
Expand All @@ -21,7 +23,8 @@ class CastToNullableTypeSpec : Spek({
assertThat(findings[0]).hasMessage("Use the safe cast ('as? String') instead of 'as String?'.")
}

it("safe casting") {
@Test
fun `safe casting`() {
val code = """
fun foo(a: Any?) {
val x: String? = a as? String
Expand All @@ -31,7 +34,8 @@ class CastToNullableTypeSpec : Spek({
assertThat(findings).isEmpty()
}

it("type checking") {
@Test
fun `type checking`() {
val code = """
fun foo(a: Any?) {
val x = a is String?
Expand All @@ -41,4 +45,4 @@ class CastToNullableTypeSpec : Spek({
assertThat(findings).isEmpty()
}
}
})
}
@@ -1,22 +1,22 @@
package io.gitlab.arturbosch.detekt.rules.bugs

import io.gitlab.arturbosch.detekt.api.Config
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 DeprecationSpec : Spek({
setupKotlinEnvironment()
@KotlinCoreEnvironmentTest
class DeprecationSpec(private val env: KotlinCoreEnvironment) {
private val subject = Deprecation(Config.empty)

val env: KotlinCoreEnvironment by memoized()
val subject by memoized { Deprecation(Config.empty) }
@Nested
inner class `Deprecation detection` {

describe("Deprecation detection") {

it("reports when supertype is deprecated") {
@Test
fun `reports when supertype is deprecated`() {
val code = """
@Deprecated("deprecation message")
abstract class Foo {
Expand All @@ -36,7 +36,8 @@ object DeprecationSpec : Spek({
assertThat(findings.first().message).isEqualTo("Foo is deprecated.")
}

it("does not report when supertype is not deprecated") {
@Test
fun `does not report when supertype is not deprecated`() {
val code = """
abstract class Oof : Foo() {
fun spam() {
Expand All @@ -52,4 +53,4 @@ object DeprecationSpec : Spek({
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
}
})
}

0 comments on commit c34f5fa

Please sign in to comment.