Skip to content

Commit

Permalink
Migrate detekt-rules-coroutines tests to JUnit (#4567)
Browse files Browse the repository at this point in the history
  • Loading branch information
3flex committed Feb 7, 2022
1 parent 1e406db commit 04006ec
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 80 deletions.
3 changes: 1 addition & 2 deletions detekt-rules-coroutines/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ plugins {
dependencies {
compileOnly(projects.detektApi)
testImplementation(projects.detektTest)
testImplementation(libs.bundles.testImplementation)
testRuntimeOnly(libs.spek.runner)
testImplementation(libs.assertj)
testRuntimeOnly(libs.kotlinx.coroutines)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ package io.gitlab.arturbosch.detekt.rules.coroutines
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.DisplayName
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test

object GlobalCoroutineUsageSpec : Spek({
val subject by memoized { GlobalCoroutineUsage(Config.empty) }
class GlobalCoroutineUsageSpec {
val subject = GlobalCoroutineUsage(Config.empty)

describe("GlobalCoroutineUsage rule") {
@Nested
inner class `GlobalCoroutineUsage rule` {

it("should report GlobalScope.launch") {
@Test
@DisplayName("should report GlobalScope.launch")
fun reportGlobalScopeLaunch() {
val code = """
import kotlinx.coroutines.delay
import kotlinx.coroutines.GlobalScope
Expand All @@ -24,7 +28,9 @@ object GlobalCoroutineUsageSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("should report GlobalScope.async") {
@Test
@DisplayName("should report GlobalScope.async")
fun reportGlobalScopeAsync() {
val code = """
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
Expand All @@ -37,7 +43,8 @@ object GlobalCoroutineUsageSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("should not report bar(GlobalScope)") {
@Test
fun `should not report bar(GlobalScope)`() {
val code = """
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.GlobalScope
Expand All @@ -51,7 +58,8 @@ object GlobalCoroutineUsageSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}

it("should not report `val scope = GlobalScope`") {
@Test
fun `should not report 'val scope = GlobalScope'`() {
val code = """
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.GlobalScope
Expand All @@ -66,4 +74,4 @@ object GlobalCoroutineUsageSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}
}
})
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package io.gitlab.arturbosch.detekt.rules.coroutines

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 InjectDispatcherSpec : Spek({
@KotlinCoreEnvironmentTest
class InjectDispatcherSpec(val env: KotlinCoreEnvironment) {

setupKotlinEnvironment()
val env: KotlinCoreEnvironment by memoized()
@Nested
inner class `InjectDispatcher with default config` {

describe("InjectDispatcher with default config") {
val subject = InjectDispatcher(Config.empty)

val subject by memoized { InjectDispatcher(Config.empty) }

it("reports when dispatchers is used inside a function") {
@Test
fun `reports when dispatchers is used inside a function`() {
val code = """
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
Expand All @@ -33,7 +33,8 @@ object InjectDispatcherSpec : Spek({
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}

it("does not report when dispatcher is used as a function parameter") {
@Test
fun `does not report when dispatcher is used as a function parameter`() {
val code = """
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
Expand All @@ -49,7 +50,8 @@ object InjectDispatcherSpec : Spek({
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}

it("does not report when dispatcher is used as a constructor parameter") {
@Test
fun `does not report when dispatcher is used as a constructor parameter`() {
val code = """
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
Expand All @@ -59,7 +61,8 @@ object InjectDispatcherSpec : Spek({
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}

it("does not report when dispatcher is used as a property") {
@Test
fun `does not report when dispatcher is used as a property`() {
val code = """
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
Expand All @@ -69,7 +72,8 @@ object InjectDispatcherSpec : Spek({
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}

it("does not report when dispatcher main is used") {
@Test
fun `does not report when dispatcher main is used`() {
val code = """
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
Expand All @@ -85,11 +89,13 @@ object InjectDispatcherSpec : Spek({
}
}

describe("InjectDispatcher with config specifying dispatcher names") {
@Nested
inner class `InjectDispatcher with config specifying dispatcher names` {

val subject by memoized { InjectDispatcher(TestConfig("dispatcherNames" to listOf("Main", "IO", "Default", "Confined"))) }
val subject = InjectDispatcher(TestConfig("dispatcherNames" to listOf("Main", "IO", "Default", "Confined")))

it("reports when dispatcher main is used") {
@Test
fun `reports when dispatcher main is used`() {
val code = """
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
Expand All @@ -104,4 +110,4 @@ object InjectDispatcherSpec : Spek({
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
}
})
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package io.gitlab.arturbosch.detekt.rules.coroutines

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 RedundantSuspendModifierSpec : Spek({
setupKotlinEnvironment()
@KotlinCoreEnvironmentTest
class RedundantSuspendModifierSpec(val env: KotlinCoreEnvironment) {

val env: KotlinCoreEnvironment by memoized()
val subject by memoized { RedundantSuspendModifier(Config.empty) }
val subject = RedundantSuspendModifier(Config.empty)

describe("RedundantSuspendModifier") {
@Nested
inner class `RedundantSuspendModifier` {

it("reports when public function returns expression of platform type") {
@Test
fun `reports when public function returns expression of platform type`() {
val code = """
import kotlin.coroutines.Continuation
import kotlin.coroutines.resume
Expand All @@ -35,7 +36,8 @@ object RedundantSuspendModifierSpec : Spek({
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}

it("does not report when private") {
@Test
fun `does not report when private`() {
val code = """
import kotlin.coroutines.Continuation
import kotlin.coroutines.resume
Expand All @@ -52,7 +54,8 @@ object RedundantSuspendModifierSpec : Spek({
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}

it("does not report when public function returns expression of platform type") {
@Test
fun `does not report when public function returns expression of platform type`() {
val code = """
class RedundantClass {
open suspend fun redundantSuspend() {
Expand All @@ -63,7 +66,8 @@ object RedundantSuspendModifierSpec : Spek({
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}

it("does not report suspend function without body") {
@Test
fun `does not report suspend function without body`() {
val code = """
interface SuspendInterface {
suspend fun empty()
Expand All @@ -72,7 +76,8 @@ object RedundantSuspendModifierSpec : Spek({
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}

it("does not report overridden suspend function") {
@Test
fun `does not report overridden suspend function`() {
val code = """
interface SuspendInterface {
suspend fun empty()
Expand All @@ -87,7 +92,8 @@ object RedundantSuspendModifierSpec : Spek({
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}

it("ignores when iterator is suspending") {
@Test
fun `ignores when iterator is suspending`() {
val code = """
class SuspendingIterator {
suspend operator fun iterator(): Iterator<Any> = iterator { yield("value") }
Expand All @@ -102,7 +108,8 @@ object RedundantSuspendModifierSpec : Spek({
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}

it("ignores when suspending function used in property delegate") {
@Test
fun `ignores when suspending function used in property delegate`() {
val code = """
class SuspendingIterator {
suspend operator fun iterator(): Iterator<Any> = iterator { yield("value") }
Expand All @@ -122,4 +129,4 @@ object RedundantSuspendModifierSpec : Spek({
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
}
})
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package io.gitlab.arturbosch.detekt.rules.coroutines

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.DisplayName
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test

@Suppress("BlockingMethodInNonBlockingContext", "RedundantSuspendModifier")
object SleepInsteadOfDelaySpec : Spek({
setupKotlinEnvironment()
@KotlinCoreEnvironmentTest
class SleepInsteadOfDelaySpec(val env: KotlinCoreEnvironment) {

val env: KotlinCoreEnvironment by memoized()
val subject by memoized { SleepInsteadOfDelay(Config.empty) }
val subject = SleepInsteadOfDelay(Config.empty)

describe("SleepInsteadOfDelay rule") {
it("should report no issue for delay() in suspend functions") {
@Nested
inner class `SleepInsteadOfDelay rule` {
@Test
fun `should report no issue for delay() in suspend functions`() {
val code = """
import kotlinx.coroutines.delay
Expand All @@ -27,7 +29,9 @@ object SleepInsteadOfDelaySpec : Spek({
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(0)
}

it("should report Thread.sleep() in suspend functions") {
@Test
@DisplayName("should report Thread.sleep() in suspend functions")
fun reportThreadSleepInSuspendFunctions() {
val code = """
suspend fun foo() {
Thread.sleep(1000L)
Expand All @@ -36,7 +40,9 @@ object SleepInsteadOfDelaySpec : Spek({
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}

it("should report Thread.sleep() in CoroutineScope.launch()") {
@Test
@DisplayName("should report Thread.sleep() in CoroutineScope.launch()")
fun reportThreadSleepInCoroutineScopeLaunch() {
val code = """
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand All @@ -51,7 +57,9 @@ object SleepInsteadOfDelaySpec : Spek({
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}

it("should report Thread.sleep() in CoroutineScope.async()") {
@Test
@DisplayName("should report Thread.sleep() in CoroutineScope.async()")
fun reportThreadSleepInCoroutineScopeAsync() {
@Suppress("DeferredResultUnused")
val code = """
import kotlinx.coroutines.CoroutineScope
Expand All @@ -67,4 +75,4 @@ object SleepInsteadOfDelaySpec : Spek({
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
}
})
}
Loading

0 comments on commit 04006ec

Please sign in to comment.