Improve the performance of tests using type resolution

This commit is contained in:
Artur Bosch
2020-06-02 11:41:06 +02:00
parent 4038774c49
commit e31596ee03
28 changed files with 343 additions and 367 deletions

View File

@@ -0,0 +1,15 @@
package io.gitlab.arturbosch.detekt.rules
import io.github.detekt.test.utils.KtTestCompiler
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.spekframework.spek2.dsl.Root
import org.spekframework.spek2.lifecycle.CachingMode
fun Root.setupKotlinEnvironment() {
val wrapper = KtTestCompiler.createEnvironment()
@Suppress("UNUSED_VARIABLE") // name is used for delegation
val env: KotlinCoreEnvironment by memoized(CachingMode.EACH_GROUP) { wrapper.env }
afterGroup { wrapper.dispose() }
}

View File

@@ -1,19 +1,18 @@
package io.gitlab.arturbosch.detekt.rules.bugs
import io.gitlab.arturbosch.detekt.api.Config
import io.github.detekt.test.utils.KtTestCompiler
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
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
object DeprecationSpec : Spek({
val subject by memoized { Deprecation(Config.empty) }
setupKotlinEnvironment()
val wrapper by memoized(
factory = { KtTestCompiler.createEnvironment() },
destructor = { it.dispose() }
)
val env: KotlinCoreEnvironment by memoized()
val subject by memoized { Deprecation(Config.empty) }
describe("Deprecation detection") {
@@ -32,7 +31,7 @@ object DeprecationSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("does not report when supertype is not deprecated") {
@@ -48,7 +47,7 @@ object DeprecationSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
}
})

View File

@@ -1,19 +1,18 @@
package io.gitlab.arturbosch.detekt.rules.bugs
import io.gitlab.arturbosch.detekt.api.Config
import io.github.detekt.test.utils.KtTestCompiler
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
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
object HasPlatformTypeSpec : Spek({
val subject by memoized { HasPlatformType(Config.empty) }
setupKotlinEnvironment()
val wrapper by memoized(
factory = { KtTestCompiler.createEnvironment() },
destructor = { it.dispose() }
)
val env: KotlinCoreEnvironment by memoized()
val subject by memoized { HasPlatformType(Config.empty) }
describe("Deprecation detection") {
@@ -23,7 +22,7 @@ object HasPlatformTypeSpec : Spek({
fun apiCall() = System.getProperty("propertyName")
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("does not report when private") {
@@ -32,7 +31,7 @@ object HasPlatformTypeSpec : Spek({
private fun apiCall() = System.getProperty("propertyName")
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("does not report when public function returns expression of platform type and type explicitly declared") {
@@ -41,7 +40,7 @@ object HasPlatformTypeSpec : Spek({
fun apiCall(): String = System.getProperty("propertyName")
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("reports when property initiated with platform type") {
@@ -50,7 +49,7 @@ object HasPlatformTypeSpec : Spek({
val name = System.getProperty("name")
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("does not report when private") {
@@ -59,7 +58,7 @@ object HasPlatformTypeSpec : Spek({
private val name = System.getProperty("name")
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("does not report when property initiated with platform type and type explicitly declared") {
@@ -68,7 +67,7 @@ object HasPlatformTypeSpec : Spek({
val name: String = System.getProperty("name")
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
}
})

View File

@@ -1,21 +1,19 @@
package io.gitlab.arturbosch.detekt.rules.bugs
import io.github.detekt.test.utils.KtTestCompiler
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.assertThat
import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
object IgnoredReturnValueSpec : Spek({
setupKotlinEnvironment()
val env: KotlinCoreEnvironment by memoized()
val subject by memoized { IgnoredReturnValue() }
val wrapper by memoized(
factory = { KtTestCompiler.createEnvironment() },
destructor = { it.dispose() }
)
describe("default config with non-annotated return values") {
it("does not report when a function which returns a value is called and the return is ignored") {
val code = """
@@ -23,7 +21,7 @@ object IgnoredReturnValueSpec : Spek({
listOf("hello")
}
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
@@ -34,7 +32,7 @@ object IgnoredReturnValueSpec : Spek({
return 42
}
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
@@ -44,7 +42,7 @@ object IgnoredReturnValueSpec : Spek({
listOf("hello").isEmpty().not()
}
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
@@ -54,7 +52,7 @@ object IgnoredReturnValueSpec : Spek({
listOf("hello");println("foo")
}
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
@@ -64,7 +62,7 @@ object IgnoredReturnValueSpec : Spek({
println("foo");listOf("hello")
}
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
@@ -74,7 +72,7 @@ object IgnoredReturnValueSpec : Spek({
listOf("hello")//foo
}
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
@@ -85,7 +83,7 @@ object IgnoredReturnValueSpec : Spek({
input.isTheAnswer()
}
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
@@ -104,7 +102,7 @@ object IgnoredReturnValueSpec : Spek({
x = listA()
}
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
@@ -116,7 +114,7 @@ object IgnoredReturnValueSpec : Spek({
noReturnValue()
}
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
@@ -128,7 +126,7 @@ object IgnoredReturnValueSpec : Spek({
// no-op
}
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
@@ -140,7 +138,7 @@ object IgnoredReturnValueSpec : Spek({
// no-op
}
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
@@ -150,7 +148,7 @@ object IgnoredReturnValueSpec : Spek({
println(returnsInt())
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
@@ -160,7 +158,7 @@ object IgnoredReturnValueSpec : Spek({
println(message = returnsInt())
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
}
@@ -179,7 +177,7 @@ object IgnoredReturnValueSpec : Spek({
println("foo")
}
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
assertThat(findings).hasSourceLocation(8, 5)
}
@@ -197,7 +195,7 @@ object IgnoredReturnValueSpec : Spek({
return 42
}
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
assertThat(findings).hasSourceLocation(8, 5)
}
@@ -215,7 +213,7 @@ object IgnoredReturnValueSpec : Spek({
return 42
}
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
assertThat(findings).hasSourceLocation(8, 5)
}
@@ -232,7 +230,7 @@ object IgnoredReturnValueSpec : Spek({
listOfChecked("hello");println("foo")
}
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
assertThat(findings).hasSourceLocation(8, 5)
}
@@ -250,7 +248,7 @@ object IgnoredReturnValueSpec : Spek({
return 42
}
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
assertThat(findings).hasSourceLocation(8, 20)
}
@@ -268,7 +266,7 @@ object IgnoredReturnValueSpec : Spek({
return 42
}
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
assertThat(findings).hasSourceLocation(8, 14)
}
@@ -285,7 +283,7 @@ object IgnoredReturnValueSpec : Spek({
return 42
}
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
assertThat(findings).hasSourceLocation(7, 11)
}
@@ -303,7 +301,7 @@ object IgnoredReturnValueSpec : Spek({
return 42
}
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
@@ -319,7 +317,7 @@ object IgnoredReturnValueSpec : Spek({
return 42
}
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
@@ -334,7 +332,7 @@ object IgnoredReturnValueSpec : Spek({
// no-op
}
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
@@ -349,7 +347,7 @@ object IgnoredReturnValueSpec : Spek({
// no-op
}
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
@@ -362,7 +360,7 @@ object IgnoredReturnValueSpec : Spek({
println(returnsInt())
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
@@ -375,7 +373,7 @@ object IgnoredReturnValueSpec : Spek({
println(message = returnsInt())
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
@@ -400,7 +398,7 @@ object IgnoredReturnValueSpec : Spek({
returnsInt()
}
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
}
@@ -421,7 +419,7 @@ object IgnoredReturnValueSpec : Spek({
return 42
}
"""
val findings = IgnoredReturnValue(config).compileAndLintWithContext(wrapper.env, code)
val findings = IgnoredReturnValue(config).compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
assertThat(findings).hasSourceLocation(8, 5)
}
@@ -439,7 +437,7 @@ object IgnoredReturnValueSpec : Spek({
return 42
}
"""
val findings = IgnoredReturnValue(config).compileAndLintWithContext(wrapper.env, code)
val findings = IgnoredReturnValue(config).compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
@@ -452,7 +450,7 @@ object IgnoredReturnValueSpec : Spek({
return 42
}
"""
val findings = IgnoredReturnValue(config).compileAndLintWithContext(wrapper.env, code)
val findings = IgnoredReturnValue(config).compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
}
@@ -473,7 +471,7 @@ object IgnoredReturnValueSpec : Spek({
return 42
}
"""
val findings = IgnoredReturnValue(config).compileAndLintWithContext(wrapper.env, code)
val findings = IgnoredReturnValue(config).compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
assertThat(findings).hasSourceLocation(8, 5)
}
@@ -487,7 +485,7 @@ object IgnoredReturnValueSpec : Spek({
return 42
}
"""
val findings = IgnoredReturnValue(config).compileAndLintWithContext(wrapper.env, code)
val findings = IgnoredReturnValue(config).compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
assertThat(findings).hasSourceLocation(4, 5)
}

View File

@@ -1,19 +1,18 @@
package io.gitlab.arturbosch.detekt.rules.bugs
import io.gitlab.arturbosch.detekt.api.Config
import io.github.detekt.test.utils.KtTestCompiler
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
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
class ImplicitDefaultLocaleSpec : Spek({
val subject by memoized { ImplicitDefaultLocale(Config.empty) }
setupKotlinEnvironment()
val wrapper by memoized(
factory = { KtTestCompiler.createEnvironment() },
destructor = { it.dispose() }
)
val env: KotlinCoreEnvironment by memoized()
val subject by memoized { ImplicitDefaultLocale(Config.empty) }
describe("ImplicitDefault rule") {
@@ -22,7 +21,7 @@ class ImplicitDefaultLocaleSpec : Spek({
fun x() {
String.format("%d", 1)
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("does not report String.format call with explicit locale") {
@@ -31,7 +30,7 @@ class ImplicitDefaultLocaleSpec : Spek({
fun x() {
String.format(Locale.US, "%d", 1)
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("reports String.toUpperCase() call without explicit locale") {
@@ -40,7 +39,7 @@ class ImplicitDefaultLocaleSpec : Spek({
val s = "deadbeef"
s.toUpperCase()
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("does not report String.toUpperCase() call with explicit locale") {
@@ -50,7 +49,7 @@ class ImplicitDefaultLocaleSpec : Spek({
val s = "deadbeef"
s.toUpperCase(Locale.US)
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("reports String.toLowerCase() call without explicit locale") {
@@ -59,7 +58,7 @@ class ImplicitDefaultLocaleSpec : Spek({
val s = "deadbeef"
s.toLowerCase()
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("does not report String.toLowerCase() call with explicit locale") {
@@ -69,7 +68,7 @@ class ImplicitDefaultLocaleSpec : Spek({
val s = "deadbeef"
s.toLowerCase(Locale.US)
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("reports String?.toUpperCase() call without explicit locale") {
@@ -78,7 +77,7 @@ class ImplicitDefaultLocaleSpec : Spek({
val s: String? = "deadbeef"
s?.toUpperCase()
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("reports String?.toLowerCase() call without explicit locale") {
@@ -87,7 +86,7 @@ class ImplicitDefaultLocaleSpec : Spek({
val s: String? = "deadbeef"
s?.toLowerCase()
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
}
})

View File

@@ -1,19 +1,18 @@
package io.gitlab.arturbosch.detekt.rules.bugs
import io.gitlab.arturbosch.detekt.api.Config
import io.github.detekt.test.utils.KtTestCompiler
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
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
class MapGetWithNotNullAssertSpec : Spek({
val subject by memoized { MapGetWithNotNullAssertionOperator(Config.empty) }
setupKotlinEnvironment()
val wrapper by memoized(
factory = { KtTestCompiler.createEnvironment() },
destructor = { it.dispose() }
)
val env: KotlinCoreEnvironment by memoized()
val subject by memoized { MapGetWithNotNullAssertionOperator(Config.empty) }
describe("check for MapGetWithNotNullAssert") {
@@ -23,7 +22,7 @@ class MapGetWithNotNullAssertSpec : Spek({
val map = emptyMap<Any, Any>()
val value = map["key"]!!
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("reports map.get() with not null assertion") {
@@ -32,7 +31,7 @@ class MapGetWithNotNullAssertSpec : Spek({
val map = emptyMap<Any, Any>()
val value = map.get("key")!!
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("does not report map[] call without not-null assert") {
@@ -41,7 +40,7 @@ class MapGetWithNotNullAssertSpec : Spek({
val map = emptyMap<String, String>()
map["key"]
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("does not report map.getValue() call") {
@@ -50,7 +49,7 @@ class MapGetWithNotNullAssertSpec : Spek({
val map = emptyMap<String, String>()
map.getValue("key")
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("does not report map.getOrDefault() call") {
@@ -59,7 +58,7 @@ class MapGetWithNotNullAssertSpec : Spek({
val map = emptyMap<String, String>()
map.getOrDefault("key", "")
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("does not report map.getOrElse() call") {
@@ -68,7 +67,7 @@ class MapGetWithNotNullAssertSpec : Spek({
val map = emptyMap<String, String>()
map.getOrElse("key", { "" })
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
}
})

View File

@@ -1,20 +1,18 @@
package io.gitlab.arturbosch.detekt.rules.bugs
import io.github.detekt.test.utils.KtTestCompiler
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
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
object MissingWhenCaseSpec : Spek({
setupKotlinEnvironment()
val env: KotlinCoreEnvironment by memoized()
val subject by memoized { MissingWhenCase() }
val wrapper by memoized(
factory = { KtTestCompiler.createEnvironment() },
destructor = { it.dispose() }
)
describe("MissingWhenCase rule") {
context("enum") {
it("reports when `when` expression used as statement and not all cases covered") {
@@ -32,7 +30,7 @@ object MissingWhenCaseSpec : Spek({
}
}
"""
val actual = subject.compileAndLintWithContext(wrapper.env, code)
val actual = subject.compileAndLintWithContext(env, code)
assertThat(actual).hasSize(1)
assertThat(actual.first().issue.id).isEqualTo("MissingWhenCase")
assertThat(actual.first().message).isEqualTo("When expression is missing cases: RED. Either add missing cases or a default `else` case.")
@@ -61,7 +59,7 @@ object MissingWhenCaseSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
}
context("sealed classes") {
@@ -80,7 +78,7 @@ object MissingWhenCaseSpec : Spek({
}
}
"""
val actual = subject.compileAndLintWithContext(wrapper.env, code)
val actual = subject.compileAndLintWithContext(env, code)
assertThat(actual).hasSize(1)
assertThat(actual.first().issue.id).isEqualTo("MissingWhenCase")
assertThat(actual.first().message).isEqualTo("When expression is missing cases: VariantC. Either add missing cases or a default `else` case.")
@@ -110,7 +108,7 @@ object MissingWhenCaseSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
}
context("standard when") {
@@ -148,7 +146,7 @@ object MissingWhenCaseSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
}
}

View File

@@ -1,20 +1,18 @@
package io.gitlab.arturbosch.detekt.rules.bugs
import io.github.detekt.test.utils.KtTestCompiler
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
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
object RedundantElseInWhenSpec : Spek({
setupKotlinEnvironment()
val env: KotlinCoreEnvironment by memoized()
val subject by memoized { RedundantElseInWhen() }
val wrapper by memoized(
factory = { KtTestCompiler.createEnvironment() },
destructor = { it.dispose() }
)
describe("RedundantElseInWhen rule") {
context("enum") {
it("reports when `when` expression used as statement contains `else` case when all cases already covered") {
@@ -34,7 +32,7 @@ object RedundantElseInWhenSpec : Spek({
}
}
"""
val actual = subject.compileAndLintWithContext(wrapper.env, code)
val actual = subject.compileAndLintWithContext(env, code)
assertThat(actual).hasSize(1)
}
@@ -55,7 +53,7 @@ object RedundantElseInWhenSpec : Spek({
}
}
"""
val actual = subject.compileAndLintWithContext(wrapper.env, code)
val actual = subject.compileAndLintWithContext(env, code)
assertThat(actual).hasSize(1)
}
@@ -81,7 +79,7 @@ object RedundantElseInWhenSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("does not report when `when` expression does not contain else case") {
@@ -113,7 +111,7 @@ object RedundantElseInWhenSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
}
context("sealed classes") {
@@ -134,8 +132,7 @@ object RedundantElseInWhenSpec : Spek({
}
}
"""
val actual = subject.compileAndLintWithContext(wrapper.env, code)
assertThat(actual).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("reports when `when` expression contains `else` case when all cases already covered") {
@@ -155,8 +152,7 @@ object RedundantElseInWhenSpec : Spek({
}
}
"""
val actual = subject.compileAndLintWithContext(wrapper.env, code)
assertThat(actual).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("does not report when `when` expression contains `else` case when not all cases explicitly covered") {
@@ -181,7 +177,7 @@ object RedundantElseInWhenSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("does not report when `when` expression does not contain else case") {
val code = """
@@ -198,7 +194,7 @@ object RedundantElseInWhenSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
}
context("standard when") {
@@ -236,7 +232,7 @@ object RedundantElseInWhenSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
}
}

View File

@@ -2,24 +2,23 @@ package io.gitlab.arturbosch.detekt.rules.bugs
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.rules.coroutines.RedundantSuspendModifier
import io.github.detekt.test.utils.KtTestCompiler
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
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
object RedundantSuspendModifierSpec : Spek({
setupKotlinEnvironment()
val env: KotlinCoreEnvironment by memoized()
val subject by memoized {
RedundantSuspendModifier(
Config.empty
)
}
val wrapper by memoized(
factory = { KtTestCompiler.createEnvironment() },
destructor = { it.dispose() }
)
describe("RedundantSuspendModifier") {
it("reports when public function returns expression of platform type") {
@@ -38,7 +37,7 @@ object RedundantSuspendModifierSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("does not report when private") {
@@ -55,7 +54,7 @@ object RedundantSuspendModifierSpec : Spek({
suspendCoroutine()
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("does not report when public function returns expression of platform type") {
@@ -66,7 +65,7 @@ object RedundantSuspendModifierSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("ignores when iterator is suspending") {
@@ -81,7 +80,7 @@ object RedundantSuspendModifierSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("ignores when suspending function used in property delegate") {
@@ -101,7 +100,7 @@ object RedundantSuspendModifierSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
}
})

View File

@@ -1,18 +1,17 @@
package io.gitlab.arturbosch.detekt.rules.bugs
import io.github.detekt.test.utils.KtTestCompiler
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
import io.gitlab.arturbosch.detekt.test.assertThat
import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
class UnnecessaryNotNullOperatorSpec : Spek({
val subject by memoized { UnnecessaryNotNullOperator() }
setupKotlinEnvironment()
val wrapper by memoized(
factory = { KtTestCompiler.createEnvironment() },
destructor = { it.dispose() }
)
val env: KotlinCoreEnvironment by memoized()
val subject by memoized { UnnecessaryNotNullOperator() }
describe("check unnecessary not null operators") {
@@ -21,7 +20,7 @@ class UnnecessaryNotNullOperatorSpec : Spek({
val a = 1
val b = a!!
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
assertThat(findings).hasTextLocations(18 to 21)
}
@@ -31,7 +30,7 @@ class UnnecessaryNotNullOperatorSpec : Spek({
val a = 1
val b = a!!.plus(42)
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
assertThat(findings).hasTextLocations(18 to 21)
}
@@ -41,7 +40,7 @@ class UnnecessaryNotNullOperatorSpec : Spek({
val a = 1
val b = a!!.plus(42)!!
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(2)
assertThat(findings).hasTextLocations(18 to 21, 18 to 32)
}
@@ -54,7 +53,7 @@ class UnnecessaryNotNullOperatorSpec : Spek({
val a : Int? = 1
val b = a!!
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
}

View File

@@ -1,18 +1,17 @@
package io.gitlab.arturbosch.detekt.rules.bugs
import io.github.detekt.test.utils.KtTestCompiler
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
import io.gitlab.arturbosch.detekt.test.assertThat
import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
class UnnecessarySafeCallSpec : Spek({
val subject by memoized { UnnecessarySafeCall() }
setupKotlinEnvironment()
val wrapper by memoized(
factory = { KtTestCompiler.createEnvironment() },
destructor = { it.dispose() }
)
val env: KotlinCoreEnvironment by memoized()
val subject by memoized { UnnecessarySafeCall() }
describe("check unnecessary safe operators") {
@@ -22,7 +21,7 @@ class UnnecessarySafeCallSpec : Spek({
val a = 1
val b = a?.toString()
}"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
assertThat(findings).hasTextLocations(48 to 61)
}
@@ -33,7 +32,7 @@ class UnnecessarySafeCallSpec : Spek({
val a = 1
val b = a?.plus(42)
}"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
assertThat(findings).hasTextLocations(48 to 59)
}
@@ -44,7 +43,7 @@ class UnnecessarySafeCallSpec : Spek({
val a = 1
val b = a?.plus(42)?.minus(24)
}"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(2)
assertThat(findings).hasTextLocations(48 to 59, 48 to 70)
}
@@ -58,7 +57,7 @@ class UnnecessarySafeCallSpec : Spek({
val a : Int? = 1
val b = a?.plus(42)
}"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
}

View File

@@ -1,18 +1,17 @@
package io.gitlab.arturbosch.detekt.rules.bugs
import io.github.detekt.test.utils.KtTestCompiler
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
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
class UnsafeCallOnNullableTypeSpec : Spek({
val subject by memoized { UnsafeCallOnNullableType() }
setupKotlinEnvironment()
val wrapper by memoized(
factory = { KtTestCompiler.createEnvironment() },
destructor = { it.dispose() }
)
val env: KotlinCoreEnvironment by memoized()
val subject by memoized { UnsafeCallOnNullableType() }
describe("check all variants of safe/unsafe calls on nullable types") {
@@ -22,7 +21,7 @@ class UnsafeCallOnNullableTypeSpec : Spek({
println(str!!.length)
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("does not report unsafe call on platform type") {
@@ -31,7 +30,7 @@ class UnsafeCallOnNullableTypeSpec : Spek({
val version = UUID.randomUUID()!!
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("does not report safe call on nullable type") {
@@ -40,7 +39,7 @@ class UnsafeCallOnNullableTypeSpec : Spek({
println(str?.length)
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("does not report safe call in combination with the elvis operator") {
@@ -49,7 +48,7 @@ class UnsafeCallOnNullableTypeSpec : Spek({
println(str?.length ?: 0)
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
}
})

View File

@@ -1,18 +1,17 @@
package io.gitlab.arturbosch.detekt.rules.bugs
import io.github.detekt.test.utils.KtTestCompiler
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
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
class UnsafeCastSpec : Spek({
val subject by memoized { UnsafeCast() }
setupKotlinEnvironment()
val wrapper by memoized(
factory = { KtTestCompiler.createEnvironment() },
destructor = { it.dispose() }
)
val env: KotlinCoreEnvironment by memoized()
val subject by memoized { UnsafeCast() }
describe("check safe and unsafe casts") {
@@ -21,7 +20,7 @@ class UnsafeCastSpec : Spek({
fun test(s: String) {
println(s as Int)
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("reports 'safe' cast that cannot succeed") {
@@ -29,7 +28,7 @@ class UnsafeCastSpec : Spek({
fun test(s: String) {
println((s as? Int) ?: 0)
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("does not report cast that might succeed") {
@@ -37,7 +36,7 @@ class UnsafeCastSpec : Spek({
fun test(s: Any) {
println(s as Int)
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("does not report 'safe' cast that might succeed") {
@@ -45,7 +44,7 @@ class UnsafeCastSpec : Spek({
fun test(s: Any) {
println((s as? Int) ?: 0)
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
}
})

View File

@@ -1,18 +1,17 @@
package io.gitlab.arturbosch.detekt.rules.exceptions
import io.github.detekt.test.utils.KtTestCompiler
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
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
class InstanceOfCheckForExceptionSpec : Spek({
val subject by memoized { InstanceOfCheckForException() }
setupKotlinEnvironment()
val wrapper by memoized(
factory = { KtTestCompiler.createEnvironment() },
destructor = { it.dispose() }
)
val env: KotlinCoreEnvironment by memoized()
val subject by memoized { InstanceOfCheckForException() }
describe("InstanceOfCheckForException rule") {
@@ -28,7 +27,7 @@ class InstanceOfCheckForExceptionSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(2)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(2)
}
it("has nested is and as checks") {
@@ -42,7 +41,7 @@ class InstanceOfCheckForExceptionSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(2)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(2)
}
it("has no instance of check") {
@@ -58,7 +57,7 @@ class InstanceOfCheckForExceptionSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("has no checks for the subtype of an exception") {
@@ -73,7 +72,7 @@ class InstanceOfCheckForExceptionSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
}
})

View File

@@ -1,21 +1,20 @@
package io.gitlab.arturbosch.detekt.rules.naming
import io.gitlab.arturbosch.detekt.api.Config
import io.github.detekt.test.utils.KtTestCompiler
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.assertThat
import io.gitlab.arturbosch.detekt.test.compileAndLint
import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
class MemberNameEqualsClassNameSpec : Spek({
val subject by memoized { MemberNameEqualsClassName(Config.empty) }
setupKotlinEnvironment()
val wrapper by memoized(
factory = { KtTestCompiler.createEnvironment() },
destructor = { it.dispose() }
)
val env: KotlinCoreEnvironment by memoized()
val subject by memoized { MemberNameEqualsClassName(Config.empty) }
describe("MemberNameEqualsClassName rule") {
@@ -35,7 +34,7 @@ class MemberNameEqualsClassNameSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("does not report a function with same name in nested class") {
@@ -46,7 +45,7 @@ class MemberNameEqualsClassNameSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("does not report a function with the same name as a companion object") {
@@ -57,7 +56,7 @@ class MemberNameEqualsClassNameSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
}
@@ -216,7 +215,7 @@ class MemberNameEqualsClassNameSpec : Spek({
}
}
"""
assertThat(MemberNameEqualsClassName().compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(MemberNameEqualsClassName().compileAndLintWithContext(env, code)).hasSize(1)
}
it("doesn't report a factory function") {
@@ -233,7 +232,7 @@ class MemberNameEqualsClassNameSpec : Spek({
class C: A()
"""
assertThat(MemberNameEqualsClassName().compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(MemberNameEqualsClassName().compileAndLintWithContext(env, code)).isEmpty()
}
it("doesn't report a body-less factory function") {
@@ -248,7 +247,7 @@ class MemberNameEqualsClassNameSpec : Spek({
class C: A()
"""
assertThat(MemberNameEqualsClassName().compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(MemberNameEqualsClassName().compileAndLintWithContext(env, code)).isEmpty()
}
}
}

View File

@@ -1,13 +1,17 @@
package io.gitlab.arturbosch.detekt.rules.performance
import io.github.detekt.test.utils.KtTestCompiler
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
import io.gitlab.arturbosch.detekt.test.compileAndLint
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
class SpreadOperatorSpec : Spek({
setupKotlinEnvironment()
val env: KotlinCoreEnvironment by memoized()
val subject by memoized { SpreadOperator() }
describe("SpreadOperator rule") {
@@ -17,13 +21,8 @@ class SpreadOperatorSpec : Spek({
*/
context("with type resolution") {
val wrapper by memoized(
factory = { KtTestCompiler.createEnvironment() },
destructor = { it.dispose() }
)
val typeResolutionEnabledMessage = "Used in this way a spread operator causes a full copy of the array to" +
" be created before calling a method which has a very high performance penalty."
" be created before calling a method which has a very high performance penalty."
it("reports when array copy required using named parameters") {
val code = """
@@ -31,7 +30,7 @@ class SpreadOperatorSpec : Spek({
fun foo(vararg xs: Int) {}
val testVal = foo(xs = *xsArray)
"""
val actual = subject.compileAndLintWithContext(wrapper.env, code)
val actual = subject.compileAndLintWithContext(env, code)
assertThat(actual).hasSize(1)
assertThat(actual.first().message).isEqualTo(typeResolutionEnabledMessage)
}
@@ -41,7 +40,7 @@ class SpreadOperatorSpec : Spek({
fun foo(vararg xs: Int) {}
val testVal = foo(*xsArray)
"""
val actual = subject.compileAndLintWithContext(wrapper.env, code)
val actual = subject.compileAndLintWithContext(env, code)
assertThat(actual).hasSize(1)
assertThat(actual.first().message).isEqualTo(typeResolutionEnabledMessage)
}
@@ -50,7 +49,7 @@ class SpreadOperatorSpec : Spek({
fun foo(vararg xs: Int) {}
val testVal = foo(xs = *intArrayOf(1))
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("doesn't report when using array constructor with spread operator when varargs parameter comes first") {
@@ -58,7 +57,7 @@ class SpreadOperatorSpec : Spek({
fun <T> asList(vararg ts: T, stringValue: String): List<Int> = listOf(1,2,3)
val list = asList(-1, 0, *arrayOf(1, 2, 3), 4, stringValue = "5")
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("doesn't report when passing values directly") {
@@ -66,7 +65,7 @@ class SpreadOperatorSpec : Spek({
fun <T> asList(vararg ts: T, stringValue: String): List<Int> = listOf(1,2,3)
val list = asList(-1, 0, 1, 2, 3, 4, stringValue = "5")
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("doesn't report when function doesn't take a vararg parameter") {
@@ -79,7 +78,7 @@ class SpreadOperatorSpec : Spek({
strs.forEach { println(it) }
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("doesn't report with expression inside params") {
@@ -92,14 +91,14 @@ class SpreadOperatorSpec : Spek({
println(test)
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
}
context("without type resolution") {
val typeResolutionDisabledMessage = "In most cases using a spread operator causes a full copy of the " +
"array to be created before calling a method which has a very high performance penalty."
"array to be created before calling a method which has a very high performance penalty."
it("reports when array copy required using named parameters") {
val code = """

View File

@@ -1,21 +1,19 @@
package io.gitlab.arturbosch.detekt.rules.style
import io.gitlab.arturbosch.detekt.api.Config
import io.github.detekt.test.utils.KtTestCompiler
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
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
class ExplicitCollectionElementAccessMethodSpec : Spek({
setupKotlinEnvironment()
val env: KotlinCoreEnvironment by memoized()
val subject by memoized { ExplicitCollectionElementAccessMethod(Config.empty) }
val wrapper by memoized(
factory = { KtTestCompiler.createEnvironment() },
destructor = { it.dispose() }
)
describe("Kotlin map") {
it("reports map element access with get method") {
@@ -24,7 +22,7 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({
val map = mapOf<String, String>()
val value = map.get("key")
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("does not report safe map element access") {
@@ -33,7 +31,7 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({
val map = mapOf<String, String>()
val value = map?.get("key")
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("reports map put method usage") {
@@ -42,7 +40,7 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({
val map = mutableMapOf<String, String>()
map.put("key", "val")
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("reports map element access with get method of non-abstract map") {
@@ -51,7 +49,7 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({
val map = hashMapOf<String, String>()
val value = map.get("key")
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("reports map element insert with put method of non-abstract map") {
@@ -60,7 +58,7 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({
val map = hashMapOf<String, String>()
val value = map.put("key", "value")
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("does not report map access with []") {
@@ -69,7 +67,7 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({
val map = mapOf<String, String>()
val value = map["key"]
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("does not report map insert with []") {
@@ -78,7 +76,7 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({
val map = mutableMapOf<String, String>()
map["key"] = "value"
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("reports map element access with get method from map in a chain") {
@@ -87,7 +85,7 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({
val map = mapOf<String, String>()
val value = listOf("1", "2").associateBy { it }.get("1")
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("reports map element access with get method from non-abstract map") {
@@ -96,7 +94,7 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({
val map = linkedMapOf<String, String>()
val value = map.get("key")
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
}
@@ -107,7 +105,7 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({
val list = listOf<String>()
val value = list.get(0)
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("reports mutable list element access with get method") {
@@ -116,7 +114,7 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({
val list = mutableListOf<String>()
val value = list.get(0)
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("does not report element access with []") {
@@ -125,7 +123,7 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({
val list = listOf<String>()
val value = list[0]
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("reports element access with get method of non-abstract list") {
@@ -134,7 +132,7 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({
val list = arrayListOf<String>()
val value = list.get(0)
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
}
@@ -146,7 +144,7 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({
val map = java.util.HashMap<String, String>()
val value = map.get("key")
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("reports map put method usage") {
@@ -155,7 +153,7 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({
val map = java.util.HashMap<String, String>()
map.put("key", "val")
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("does not report map access with []") {
@@ -164,7 +162,7 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({
val map = java.util.HashMap<String, String>()
val value = map["key"]
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("does not report map insert with []") {
@@ -173,7 +171,7 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({
val map = java.util.HashMap<String, String>()
map["key"] = "value"
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("reports map element access with get method from map in a chain") {
@@ -182,7 +180,7 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({
val map = java.util.HashMap<String, String>()
val value = listOf("1", "2").associateBy { it }.get("1")
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
}
@@ -194,7 +192,7 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({
val list = java.util.ArrayList<String>()
val value = list.get(0)
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("does not report element access with []") {
@@ -203,7 +201,7 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({
val list = java.util.ArrayList<String>()
val value = list[0]
}"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
}
@@ -216,7 +214,7 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({
val c: Char? get() = "".first() ?: throw IllegalArgumentException("getter")
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
}
@@ -227,7 +225,7 @@ class ExplicitCollectionElementAccessMethodSpec : Spek({
val string = ""
.toString()
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
}
})

View File

@@ -1,19 +1,18 @@
package io.gitlab.arturbosch.detekt.rules.style
import io.github.detekt.test.utils.KtTestCompiler
import io.gitlab.arturbosch.detekt.api.SourceLocation
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.assertThat
import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
class ForbiddenMethodCallSpec : Spek({
setupKotlinEnvironment()
val wrapper by memoized(
factory = { KtTestCompiler.createEnvironment() },
destructor = { it.dispose() }
)
val env: KotlinCoreEnvironment by memoized()
describe("ForbiddenMethodCall rule") {
@@ -24,7 +23,7 @@ class ForbiddenMethodCallSpec : Spek({
println("4")
}
"""
val findings = ForbiddenMethodCall(TestConfig()).compileAndLintWithContext(wrapper.env, code)
val findings = ForbiddenMethodCall(TestConfig()).compileAndLintWithContext(env, code)
assertThat(findings).hasSize(2)
assertThat(findings).hasSourceLocations(
SourceLocation(2, 5),
@@ -41,7 +40,7 @@ class ForbiddenMethodCallSpec : Spek({
"""
val findings =
ForbiddenMethodCall(TestConfig(mapOf(ForbiddenMethodCall.METHODS to " "))).compileAndLintWithContext(
wrapper.env,
env,
code
)
assertThat(findings).isEmpty()
@@ -56,7 +55,7 @@ class ForbiddenMethodCallSpec : Spek({
"""
val findings = ForbiddenMethodCall(
TestConfig(mapOf(ForbiddenMethodCall.METHODS to listOf("java.lang.System.gc")))
).compileAndLintWithContext(wrapper.env, code)
).compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
@@ -68,7 +67,7 @@ class ForbiddenMethodCallSpec : Spek({
"""
val findings = ForbiddenMethodCall(
TestConfig(mapOf(ForbiddenMethodCall.METHODS to listOf("java.io.PrintStream.println")))
).compileAndLintWithContext(wrapper.env, code)
).compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
assertThat(findings).hasTextLocations(38 to 54)
}
@@ -82,7 +81,7 @@ class ForbiddenMethodCallSpec : Spek({
"""
val findings = ForbiddenMethodCall(
TestConfig(mapOf(ForbiddenMethodCall.METHODS to listOf("java.io.PrintStream.println")))
).compileAndLintWithContext(wrapper.env, code)
).compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
assertThat(findings).hasTextLocations(49 to 65)
}
@@ -97,7 +96,7 @@ class ForbiddenMethodCallSpec : Spek({
"""
val findings = ForbiddenMethodCall(
TestConfig(mapOf(ForbiddenMethodCall.METHODS to listOf("java.io.PrintStream.println", "java.lang.System.gc")))
).compileAndLintWithContext(wrapper.env, code)
).compileAndLintWithContext(env, code)
assertThat(findings).hasSize(2)
assertThat(findings).hasTextLocations(48 to 64, 76 to 80)
}
@@ -112,7 +111,7 @@ class ForbiddenMethodCallSpec : Spek({
"""
val findings = ForbiddenMethodCall(
TestConfig(mapOf(ForbiddenMethodCall.METHODS to "java.io.PrintStream.println, java.lang.System.gc"))
).compileAndLintWithContext(wrapper.env, code)
).compileAndLintWithContext(env, code)
assertThat(findings).hasSize(2)
assertThat(findings).hasTextLocations(48 to 64, 76 to 80)
}

View File

@@ -1,21 +1,20 @@
package io.gitlab.arturbosch.detekt.rules.style
import io.gitlab.arturbosch.detekt.api.Config
import io.github.detekt.test.utils.KtTestCompiler
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.compileAndLint
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
class ForbiddenVoidSpec : Spek({
val subject by memoized { ForbiddenVoid(Config.empty) }
setupKotlinEnvironment()
val wrapper by memoized(
factory = { KtTestCompiler.createEnvironment() },
destructor = { it.dispose() }
)
val env: KotlinCoreEnvironment by memoized()
val subject by memoized { ForbiddenVoid(Config.empty) }
describe("ForbiddenVoid rule") {
it("should report all Void type usage") {
@@ -28,7 +27,7 @@ class ForbiddenVoidSpec : Spek({
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(4)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(4)
}
it("should not report Void class literal") {
@@ -55,7 +54,7 @@ class ForbiddenVoidSpec : Spek({
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
describe("ignoreOverridden is enabled") {
@@ -75,7 +74,7 @@ class ForbiddenVoidSpec : Spek({
}
"""
val findings = ForbiddenVoid(config).compileAndLintWithContext(wrapper.env, code)
val findings = ForbiddenVoid(config).compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
@@ -95,7 +94,7 @@ class ForbiddenVoidSpec : Spek({
}
"""
val findings = ForbiddenVoid(config).compileAndLintWithContext(wrapper.env, code)
val findings = ForbiddenVoid(config).compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
@@ -112,7 +111,7 @@ class ForbiddenVoidSpec : Spek({
}
"""
val findings = ForbiddenVoid(config).compileAndLintWithContext(wrapper.env, code)
val findings = ForbiddenVoid(config).compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
}
@@ -123,7 +122,7 @@ class ForbiddenVoidSpec : Spek({
}
"""
val findings = ForbiddenVoid(config).compileAndLintWithContext(wrapper.env, code)
val findings = ForbiddenVoid(config).compileAndLintWithContext(env, code)
assertThat(findings).hasSize(2)
}
}
@@ -147,7 +146,7 @@ class ForbiddenVoidSpec : Spek({
class D : A<Void>
"""
val findings = ForbiddenVoid(config).compileAndLintWithContext(wrapper.env, code)
val findings = ForbiddenVoid(config).compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
@@ -158,7 +157,7 @@ class ForbiddenVoidSpec : Spek({
class C : A<B<Void>>
"""
val findings = ForbiddenVoid(config).compileAndLintWithContext(wrapper.env, code)
val findings = ForbiddenVoid(config).compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
@@ -167,7 +166,7 @@ class ForbiddenVoidSpec : Spek({
val foo = mutableMapOf<Int, Void>()
"""
val findings = ForbiddenVoid(config).compileAndLintWithContext(wrapper.env, code)
val findings = ForbiddenVoid(config).compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
@@ -181,7 +180,7 @@ class ForbiddenVoidSpec : Spek({
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(4)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(4)
}
}
}

View File

@@ -1,19 +1,18 @@
package io.gitlab.arturbosch.detekt.rules.style
import io.gitlab.arturbosch.detekt.api.Config
import io.github.detekt.test.utils.KtTestCompiler
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
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
object RedundantExplicitTypeSpec : Spek({
val subject by memoized { RedundantExplicitType(Config.empty) }
setupKotlinEnvironment()
val wrapper by memoized(
factory = { KtTestCompiler.createEnvironment() },
destructor = { it.dispose() }
)
val env: KotlinCoreEnvironment by memoized()
val subject by memoized { RedundantExplicitType(Config.empty) }
describe("RedundantExplicitType") {
@@ -23,7 +22,7 @@ object RedundantExplicitTypeSpec : Spek({
val x: Boolean = true
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("reports explicit type for integer") {
@@ -32,7 +31,7 @@ object RedundantExplicitTypeSpec : Spek({
val x: Int = 3
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("reports explicit type for long") {
@@ -41,7 +40,7 @@ object RedundantExplicitTypeSpec : Spek({
val x: Long = 3L
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("reports explicit type for float") {
@@ -50,7 +49,7 @@ object RedundantExplicitTypeSpec : Spek({
val x: Float = 3.0f
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("reports explicit type for double") {
@@ -59,7 +58,7 @@ object RedundantExplicitTypeSpec : Spek({
val x: Double = 3.0
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("reports explicit type for char") {
@@ -68,7 +67,7 @@ object RedundantExplicitTypeSpec : Spek({
val x: Char = 'f'
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("reports explicit type for string template") {
@@ -79,7 +78,7 @@ object RedundantExplicitTypeSpec : Spek({
val y: String = "$substitute"
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("reports explicit type for name reference expression") {
@@ -90,7 +89,7 @@ object RedundantExplicitTypeSpec : Spek({
val o: Test = Test
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("reports explicit type for call expression") {
@@ -105,7 +104,7 @@ object RedundantExplicitTypeSpec : Spek({
val t: TallPerson = TallPerson("first", 3)
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("does not report explicit type for call expression when type is an interface") {
@@ -120,7 +119,7 @@ object RedundantExplicitTypeSpec : Spek({
val t: Person = TallPerson("first", 3)
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
}
})

View File

@@ -1,26 +1,24 @@
package io.gitlab.arturbosch.detekt.rules.style
import io.gitlab.arturbosch.detekt.api.Finding
import io.github.detekt.test.utils.KtTestCompiler
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
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
class UnnecessaryAbstractClassSpec : Spek({
setupKotlinEnvironment()
val env: KotlinCoreEnvironment by memoized()
val subject by memoized {
UnnecessaryAbstractClass(TestConfig(mapOf(
UnnecessaryAbstractClass.EXCLUDE_ANNOTATED_CLASSES to listOf("jdk.nashorn.internal.ir.annotations.Ignore")
UnnecessaryAbstractClass.EXCLUDE_ANNOTATED_CLASSES to listOf("jdk.nashorn.internal.ir.annotations.Ignore")
)))
}
val wrapper by memoized(
factory = { KtTestCompiler.createEnvironment() },
destructor = { it.dispose() }
)
describe("UnnecessaryAbstractClass rule") {
context("abstract classes with no concrete members") {
@@ -32,7 +30,7 @@ class UnnecessaryAbstractClassSpec : Spek({
abstract fun f()
}
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertFindingMessage(
findings,
"An abstract class without a concrete member can be refactored to an interface."
@@ -50,7 +48,7 @@ class UnnecessaryAbstractClassSpec : Spek({
abstract fun g()
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
}
@@ -65,7 +63,7 @@ class UnnecessaryAbstractClassSpec : Spek({
fun f() {}
}
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertFindingMessage(findings, message)
}
@@ -77,7 +75,7 @@ class UnnecessaryAbstractClassSpec : Spek({
}
}
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertFindingMessage(findings, message)
}
@@ -89,19 +87,19 @@ class UnnecessaryAbstractClassSpec : Spek({
}
}
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertFindingMessage(findings, message)
}
it("reports no abstract members in an abstract class with just a constructor") {
val code = "abstract class A(val i: Int)"
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertFindingMessage(findings, message)
}
it("reports no abstract members in an abstract class with a body and a constructor") {
val code = "abstract class A(val i: Int) {}"
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertFindingMessage(findings, message)
}
@@ -122,7 +120,7 @@ class UnnecessaryAbstractClassSpec : Spek({
fun g() {}
}
"""
val findings = subject.compileAndLintWithContext(wrapper.env, code)
val findings = subject.compileAndLintWithContext(env, code)
assertFindingMessage(findings, message)
}
}
@@ -140,7 +138,7 @@ class UnnecessaryAbstractClassSpec : Spek({
fun g() {}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("does not report an abstract class with a constructor and an abstract class derived from it") {
@@ -153,7 +151,7 @@ class UnnecessaryAbstractClassSpec : Spek({
fun g() {}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("does not report an abstract class with a function derived from an interface") {
@@ -166,7 +164,7 @@ class UnnecessaryAbstractClassSpec : Spek({
fun f()
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("does not report empty abstract classes") {
@@ -174,7 +172,7 @@ class UnnecessaryAbstractClassSpec : Spek({
abstract class A
abstract class B()
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("does not report abstract classes with module annotation") {
@@ -191,7 +189,7 @@ class UnnecessaryAbstractClassSpec : Spek({
abstract fun f()
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
}
}

View File

@@ -1,21 +1,20 @@
package io.gitlab.arturbosch.detekt.rules.style
import io.gitlab.arturbosch.detekt.api.Config
import io.github.detekt.test.utils.KtTestCompiler
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
import io.gitlab.arturbosch.detekt.test.compileAndLint
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
class UnnecessaryApplySpec : Spek({
val subject by memoized { UnnecessaryApply(Config.empty) }
setupKotlinEnvironment()
val wrapper by memoized(
factory = { KtTestCompiler.createEnvironment() },
destructor = { it.dispose() }
)
val subject by memoized { UnnecessaryApply(Config.empty) }
val env: KotlinCoreEnvironment by memoized()
describe("UnnecessaryApply rule") {
@@ -217,7 +216,7 @@ class UnnecessaryApplySpec : Spek({
context("false positive when it's used as an expression - #2435") {
it("do not report when it's used as an assignment") {
assertThat(subject.compileAndLintWithContext(wrapper.env, """
assertThat(subject.compileAndLintWithContext(env, """
class C {
fun f() {}
}
@@ -234,7 +233,7 @@ class UnnecessaryApplySpec : Spek({
}
it("do not report when it's used as the last statement of a block inside lambda") {
assertThat(subject.compileAndLintWithContext(wrapper.env, """
assertThat(subject.compileAndLintWithContext(env, """
class C {
fun f() {}
}

View File

@@ -1,25 +1,23 @@
package io.gitlab.arturbosch.detekt.rules.style
import io.gitlab.arturbosch.detekt.rules.Case
import io.github.detekt.test.utils.KtTestCompiler
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext
import io.gitlab.arturbosch.detekt.test.lint
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatExceptionOfType
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import java.util.regex.PatternSyntaxException
class UnusedPrivateMemberSpec : Spek({
setupKotlinEnvironment()
val env: KotlinCoreEnvironment by memoized()
val subject by memoized { UnusedPrivateMember() }
val wrapper by memoized(
factory = { KtTestCompiler.createEnvironment() },
destructor = { it.dispose() }
)
val regexTestingCode = """
class Test {
private val used = "This is used"
@@ -1051,7 +1049,7 @@ class UnusedPrivateMemberSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("report unused minus operator") {
@@ -1063,7 +1061,7 @@ class UnusedPrivateMemberSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
}
@@ -1083,7 +1081,7 @@ class UnusedPrivateMemberSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(2)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(2)
}
it("report it when the class has same named functions") {
@@ -1104,7 +1102,7 @@ class UnusedPrivateMemberSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(2)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(2)
}
it("report it when the class has same named extension functions") {
@@ -1125,7 +1123,7 @@ class UnusedPrivateMemberSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(2)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(2)
}
}
})

View File

@@ -1,23 +1,21 @@
package io.gitlab.arturbosch.detekt.rules.style
import io.gitlab.arturbosch.detekt.api.Config
import io.github.detekt.test.utils.KtTestCompiler
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
import io.gitlab.arturbosch.detekt.test.assertThat
import io.gitlab.arturbosch.detekt.test.compileAndLint
import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext
import io.gitlab.arturbosch.detekt.test.lint
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
class UseCheckOrErrorSpec : Spek({
setupKotlinEnvironment()
val env: KotlinCoreEnvironment by memoized()
val subject by memoized { UseCheckOrError(Config.empty) }
val wrapper by memoized(
factory = { KtTestCompiler.createEnvironment() },
destructor = { it.dispose() }
)
describe("UseCheckOrError rule") {
it("reports if a an IllegalStateException is thrown") {
@@ -150,7 +148,7 @@ class UseCheckOrErrorSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("does not report if the exception thrown has a String literal argument and a non-String argument") {
@@ -162,7 +160,7 @@ class UseCheckOrErrorSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("reports if the exception thrown has a non-String literal argument") {
@@ -174,7 +172,7 @@ class UseCheckOrErrorSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
}
}

View File

@@ -1,24 +1,22 @@
package io.gitlab.arturbosch.detekt.rules.style
import io.gitlab.arturbosch.detekt.api.Config
import io.github.detekt.test.utils.KtTestCompiler
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.assertThat
import io.gitlab.arturbosch.detekt.test.compileAndLint
import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext
import io.gitlab.arturbosch.detekt.test.lint
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
class UseDataClassSpec : Spek({
setupKotlinEnvironment()
val env: KotlinCoreEnvironment by memoized()
val subject by memoized { UseDataClass(Config.empty) }
val wrapper by memoized(
factory = { KtTestCompiler.createEnvironment() },
destructor = { it.dispose() }
)
describe("UseDataClass rule") {
describe("does not report invalid data class candidates") {
@@ -152,7 +150,7 @@ class UseDataClassSpec : Spek({
fun copy(a: Int, b: String): D = D(a, b)
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("does report with copy method which has an implicit return type") {
@@ -161,7 +159,7 @@ class UseDataClassSpec : Spek({
fun copy(a: Int, b: String) = D(a, b)
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("does not report with copy method which has no parameters") {
@@ -170,7 +168,7 @@ class UseDataClassSpec : Spek({
fun copy(): D = D(0, "")
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("does not report with copy method which has more parameters than the primary constructor") {
@@ -179,7 +177,7 @@ class UseDataClassSpec : Spek({
fun copy(a: Int, b: String, c: String): D = D(a, b + c)
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("does not report with copy method which has different parameter types") {
@@ -188,7 +186,7 @@ class UseDataClassSpec : Spek({
fun copy(a: Int, b: Int): D = D(a, b.toString())
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("does not report with copy method which has different parameter types 2") {
@@ -197,7 +195,7 @@ class UseDataClassSpec : Spek({
fun copy(a: Int, b: String?): D = D(a, b.toString())
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("does not report with copy method which has a different return type") {
@@ -207,7 +205,7 @@ class UseDataClassSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
}

View File

@@ -1,23 +1,21 @@
package io.gitlab.arturbosch.detekt.rules.style
import io.gitlab.arturbosch.detekt.api.Config
import io.github.detekt.test.utils.KtTestCompiler
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
import io.gitlab.arturbosch.detekt.test.assertThat
import io.gitlab.arturbosch.detekt.test.compileAndLint
import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext
import io.gitlab.arturbosch.detekt.test.lint
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
class UseRequireSpec : Spek({
setupKotlinEnvironment()
val env: KotlinCoreEnvironment by memoized()
val subject by memoized { UseRequire(Config.empty) }
val wrapper by memoized(
factory = { KtTestCompiler.createEnvironment() },
destructor = { it.dispose() }
)
describe("UseRequire rule") {
it("reports if a precondition throws an IllegalArgumentException") {
@@ -127,7 +125,7 @@ class UseRequireSpec : Spek({
if (throwable !is NumberFormatException) throw IllegalArgumentException(throwable)
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("does not report if the exception thrown has a String literal argument and a non-String argument") {
@@ -136,7 +134,7 @@ class UseRequireSpec : Spek({
if (throwable !is NumberFormatException) throw IllegalArgumentException("a", throwable)
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("reports if the exception thrown has a non-String literal argument") {
@@ -146,7 +144,7 @@ class UseRequireSpec : Spek({
if (throwable !is NumberFormatException) throw IllegalArgumentException(s)
}
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
}

View File

@@ -1,65 +1,63 @@
package io.gitlab.arturbosch.detekt.rules.style
import io.github.detekt.test.utils.KtTestCompiler
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
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
object UselessCallOnNotNullSpec : Spek({
setupKotlinEnvironment()
val env: KotlinCoreEnvironment by memoized()
val subject by memoized { UselessCallOnNotNull() }
val wrapper by memoized(
factory = { KtTestCompiler.createEnvironment() },
destructor = { it.dispose() }
)
describe("UselessCallOnNotNull rule") {
it("reports when calling orEmpty on a list") {
val code = """val testList = listOf("string").orEmpty()"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("reports when calling orEmpty on a nullable list") {
val code = """val testList = listOf("string")?.orEmpty()"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("reports when calling orEmpty in a chain") {
val code = """val testList = listOf("string").orEmpty().map { }"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("reports when calling isNullOrBlank on a nullable type") {
val code = """val testString = ""?.isNullOrBlank()"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("reports when calling isNullOrEmpty on a nullable type") {
val code = """val testString = ""?.isNullOrEmpty()"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("reports when calling isNullOrEmpty on a string") {
val code = """val testString = "".isNullOrEmpty()"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("reports when calling orEmpty on a string") {
val code = """val testString = "".orEmpty()"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("reports when calling orEmpty on a sequence") {
val code = """val testSequence = listOf(1).asSequence().orEmpty()"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("reports when calling orEmpty on a list with a platform type") {
// System.getenv().keys.toList() will be of type List<String!>.
val code = """val testSequence = System.getenv().keys.toList().orEmpty()"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
it("only reports on a Kotlin list") {
@@ -69,7 +67,7 @@ object UselessCallOnNotNullSpec : Spek({
val noList = "str".orEmpty()
val list = listOf(1, 2, 3).orEmpty()
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(1)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
}
})

View File

@@ -1,19 +1,18 @@
package io.gitlab.arturbosch.detekt.rules.style.optional
import io.gitlab.arturbosch.detekt.api.Config
import io.github.detekt.test.utils.KtTestCompiler
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
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
object PreferToOverPairSyntaxSpec : Spek({
val subject by memoized { PreferToOverPairSyntax(Config.empty) }
setupKotlinEnvironment()
val wrapper by memoized(
factory = { KtTestCompiler.createEnvironment() },
destructor = { it.dispose() }
)
val env: KotlinCoreEnvironment by memoized()
val subject by memoized { PreferToOverPairSyntax(Config.empty) }
describe("PreferToOverPairSyntax rule") {
@@ -23,12 +22,12 @@ object PreferToOverPairSyntaxSpec : Spek({
val pair2: Pair<Int, Int> = Pair(1, 2)
val pair3 = Pair(Pair(1, 2), Pair(3, 4))
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).hasSize(5)
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(5)
}
it("does not report if it is created using the to syntax") {
val code = "val pair = 1 to 2"
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("does not report if a non-Kotlin Pair class was used") {
@@ -39,7 +38,7 @@ object PreferToOverPairSyntaxSpec : Spek({
data class Pair<T, Z>(val int1: T, val int2: Z)
"""
assertThat(subject.compileAndLintWithContext(wrapper.env, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
}
})