Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Refactor KotlinCoreEnvironment test setup #1880

Merged
merged 3 commits into from
Sep 12, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -3,24 +3,17 @@ package io.gitlab.arturbosch.detekt.rules.bugs
import io.gitlab.arturbosch.detekt.test.KtTestCompiler
import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.com.intellij.openapi.Disposable
import org.jetbrains.kotlin.com.intellij.openapi.util.Disposer
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe

object MissingWhenCaseSpec : Spek({

val subject by memoized { MissingWhenCase() }

var environment: KotlinCoreEnvironment? = null
lateinit var disposable: Disposable

beforeEachTest {
val pair = KtTestCompiler.createEnvironment()
disposable = pair.first
environment = pair.second
}
val wrapper by memoized(
factory = { KtTestCompiler.createEnvironment() },
destructor = { it.dispose() }
)

describe("MissingWhenCase rule") {
context("enum") {
Expand All @@ -39,7 +32,7 @@ object MissingWhenCaseSpec : Spek({
}
}
"""
val actual = subject.compileAndLintWithContext(environment!!, code)
val actual = subject.compileAndLintWithContext(wrapper.getEnvironment(), 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.")
Expand Down Expand Up @@ -68,7 +61,7 @@ object MissingWhenCaseSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(environment!!, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(wrapper.getEnvironment(), code)).isEmpty()
}
}
context("sealed classes") {
Expand All @@ -87,7 +80,7 @@ object MissingWhenCaseSpec : Spek({
}
}
"""
val actual = subject.compileAndLintWithContext(environment!!, code)
val actual = subject.compileAndLintWithContext(wrapper.getEnvironment(), 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.")
Expand Down Expand Up @@ -117,7 +110,7 @@ object MissingWhenCaseSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(environment!!, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(wrapper.getEnvironment(), code)).isEmpty()
}
}
context("standard when") {
Expand Down Expand Up @@ -155,13 +148,8 @@ object MissingWhenCaseSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(environment!!, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(wrapper.getEnvironment(), code)).isEmpty()
}
}
}

afterEachTest {
Disposer.dispose(disposable)
environment = null
}
})
Expand Up @@ -3,24 +3,17 @@ package io.gitlab.arturbosch.detekt.rules.bugs
import io.gitlab.arturbosch.detekt.test.KtTestCompiler
import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.com.intellij.openapi.Disposable
import org.jetbrains.kotlin.com.intellij.openapi.util.Disposer
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe

object RedundantElseInWhenSpec : Spek({

val subject by memoized { RedundantElseInWhen() }

var environment: KotlinCoreEnvironment? = null
lateinit var disposable: Disposable

beforeEachTest {
val pair = KtTestCompiler.createEnvironment()
disposable = pair.first
environment = pair.second
}
val wrapper by memoized(
factory = { KtTestCompiler.createEnvironment() },
destructor = { it.dispose() }
)

describe("RedundantElseInWhen rule") {
context("enum") {
Expand All @@ -41,7 +34,7 @@ object RedundantElseInWhenSpec : Spek({
}
}
"""
val actual = subject.compileAndLintWithContext(environment!!, code)
val actual = subject.compileAndLintWithContext(wrapper.getEnvironment(), code)
assertThat(actual).hasSize(1)
}

Expand All @@ -62,7 +55,7 @@ object RedundantElseInWhenSpec : Spek({
}
}
"""
val actual = subject.compileAndLintWithContext(environment!!, code)
val actual = subject.compileAndLintWithContext(wrapper.getEnvironment(), code)
assertThat(actual).hasSize(1)
}

Expand All @@ -88,7 +81,7 @@ object RedundantElseInWhenSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(environment!!, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(wrapper.getEnvironment(), code)).isEmpty()
}

it("does not report when `when` expression does not contain else case") {
Expand Down Expand Up @@ -120,7 +113,7 @@ object RedundantElseInWhenSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(environment!!, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(wrapper.getEnvironment(), code)).isEmpty()
}
}
context("sealed classes") {
Expand All @@ -141,7 +134,7 @@ object RedundantElseInWhenSpec : Spek({
}
}
"""
val actual = subject.compileAndLintWithContext(environment!!, code)
val actual = subject.compileAndLintWithContext(wrapper.getEnvironment(), code)
assertThat(actual).hasSize(1)
}

Expand All @@ -162,7 +155,7 @@ object RedundantElseInWhenSpec : Spek({
}
}
"""
val actual = subject.compileAndLintWithContext(environment!!, code)
val actual = subject.compileAndLintWithContext(wrapper.getEnvironment(), code)
assertThat(actual).hasSize(1)
}

Expand All @@ -188,7 +181,7 @@ object RedundantElseInWhenSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(environment!!, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(wrapper.getEnvironment(), code)).isEmpty()
}
it("does not report when `when` expression does not contain else case") {
val code = """
Expand All @@ -205,7 +198,7 @@ object RedundantElseInWhenSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(environment!!, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(wrapper.getEnvironment(), code)).isEmpty()
}
}
context("standard when") {
Expand Down Expand Up @@ -243,13 +236,8 @@ object RedundantElseInWhenSpec : Spek({
}
}
"""
assertThat(subject.compileAndLintWithContext(environment!!, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(wrapper.getEnvironment(), code)).isEmpty()
}
}
}

afterEachTest {
Disposer.dispose(disposable)
environment = null
}
})
Expand Up @@ -4,9 +4,6 @@ import io.gitlab.arturbosch.detekt.test.KtTestCompiler
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.jetbrains.kotlin.com.intellij.openapi.Disposable
import org.jetbrains.kotlin.com.intellij.openapi.util.Disposer
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe

Expand All @@ -20,14 +17,10 @@ class SpreadOperatorSpec : Spek({
*/
context("with type resolution") {

var environment: KotlinCoreEnvironment? = null
lateinit var disposable: Disposable

beforeEachTest {
val pair = KtTestCompiler.createEnvironment()
disposable = pair.first
environment = pair.second
}
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."
Expand All @@ -38,7 +31,7 @@ class SpreadOperatorSpec : Spek({
fun foo(vararg xs: Int) {}
val testVal = foo(xs = *xsArray)
"""
val actual = subject.compileAndLintWithContext(environment!!, code)
val actual = subject.compileAndLintWithContext(wrapper.getEnvironment(), code)
assertThat(actual).hasSize(1)
assertThat(actual.first().message).isEqualTo(typeResolutionEnabledMessage)
}
Expand All @@ -48,7 +41,7 @@ class SpreadOperatorSpec : Spek({
fun foo(vararg xs: Int) {}
val testVal = foo(*xsArray)
"""
val actual = subject.compileAndLintWithContext(environment!!, code)
val actual = subject.compileAndLintWithContext(wrapper.getEnvironment(), code)
assertThat(actual).hasSize(1)
assertThat(actual.first().message).isEqualTo(typeResolutionEnabledMessage)
}
Expand All @@ -57,23 +50,23 @@ class SpreadOperatorSpec : Spek({
fun foo(vararg xs: Int) {}
val testVal = foo(xs = *intArrayOf(1))
"""
assertThat(subject.compileAndLintWithContext(environment!!, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(wrapper.getEnvironment(), code)).isEmpty()
}

it("doesn't report when using array constructor with spread operator when varargs parameter comes first") {
val code = """
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(environment!!, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(wrapper.getEnvironment(), code)).isEmpty()
}

it("doesn't report when passing values directly") {
val code = """
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(environment!!, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(wrapper.getEnvironment(), code)).isEmpty()
}

it("doesn't report when function doesn't take a vararg parameter") {
Expand All @@ -86,7 +79,7 @@ class SpreadOperatorSpec : Spek({
strs.forEach { println(it) }
}
"""
assertThat(subject.compileAndLintWithContext(environment!!, code)).isEmpty()
assertThat(subject.compileAndLintWithContext(wrapper.getEnvironment(), code)).isEmpty()
}

it("doesn't report with expression inside params") {
Expand All @@ -99,12 +92,7 @@ class SpreadOperatorSpec : Spek({
println(test)
}
"""
assertThat(subject.compileAndLintWithContext(environment!!, code)).isEmpty()
}

afterEachTest {
Disposer.dispose(disposable)
environment = null
assertThat(subject.compileAndLintWithContext(wrapper.getEnvironment(), code)).isEmpty()
}
}

Expand Down