Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate tests in detekt-rules-errorprone to junit #4523

Merged
merged 7 commits into from Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()
}
}
})
}