-
-
Notifications
You must be signed in to change notification settings - Fork 755
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate detekt-rules-performance tests to JUnit (#4569)
- Loading branch information
Showing
5 changed files
with
191 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 107 additions & 51 deletions
158
...mance/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/performance/ArrayPrimitiveSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,210 +1,266 @@ | ||
package io.gitlab.arturbosch.detekt.rules.performance | ||
|
||
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment | ||
import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest | ||
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 | ||
import org.junit.jupiter.api.DisplayName | ||
import org.junit.jupiter.api.Nested | ||
import org.junit.jupiter.api.Test | ||
|
||
class ArrayPrimitiveSpec : Spek({ | ||
setupKotlinEnvironment() | ||
@KotlinCoreEnvironmentTest | ||
class ArrayPrimitiveSpec(val env: KotlinCoreEnvironment) { | ||
|
||
val env: KotlinCoreEnvironment by memoized() | ||
val subject by memoized { ArrayPrimitive() } | ||
val subject = ArrayPrimitive() | ||
|
||
describe("one function parameter") { | ||
it("is an array of primitive type") { | ||
@Nested | ||
inner class `one function parameter` { | ||
@Test | ||
fun `is an array of primitive type`() { | ||
val code = "fun function(array: Array<Int>) {}" | ||
assertThat(subject.compileAndLint(code)).hasSize(1) | ||
} | ||
|
||
it("is not an array") { | ||
@Test | ||
fun `is not an array`() { | ||
val code = "fun function(i: Int) {}" | ||
assertThat(subject.compileAndLint(code)).isEmpty() | ||
} | ||
|
||
it("is a specialized array") { | ||
@Test | ||
fun `is a specialized array`() { | ||
val code = "fun function(array: ByteArray) {}" | ||
assertThat(subject.compileAndLint(code)).isEmpty() | ||
} | ||
|
||
it("is a star-projected array") { | ||
@Test | ||
fun `is a star-projected array`() { | ||
val code = "fun function(array: Array<*>) {}" | ||
assertThat(subject.compileAndLint(code)).isEmpty() | ||
} | ||
|
||
it("is not present") { | ||
@Test | ||
fun `is not present`() { | ||
val code = "fun function() {}" | ||
assertThat(subject.compileAndLint(code)).isEmpty() | ||
} | ||
|
||
it("is an array of a non-primitive type") { | ||
@Test | ||
fun `is an array of a non-primitive type`() { | ||
val code = "fun function(array: Array<String>) {}" | ||
assertThat(subject.compileAndLint(code)).isEmpty() | ||
} | ||
|
||
it("is an array of an array of a primitive type") { | ||
@Test | ||
fun `is an array of an array of a primitive type`() { | ||
val code = "fun function(array: Array<Array<Int>>) {}" | ||
assertThat(subject.compileAndLint(code)).hasSize(1) | ||
} | ||
|
||
it("is a dictionary with an array of a primitive type as key") { | ||
@Test | ||
fun `is a dictionary with an array of a primitive type as key`() { | ||
val code = "fun function(dict: java.util.Dictionary<Int, Array<Int>>) {}" | ||
assertThat(subject.compileAndLint(code)).hasSize(1) | ||
} | ||
} | ||
|
||
describe("multiple function parameters") { | ||
it("one is Array<Primitive> and the other is not") { | ||
@Nested | ||
inner class `multiple function parameters` { | ||
@Test | ||
@DisplayName("one is Array<Primitive> and the other is not") | ||
fun oneArrayPrimitive() { | ||
val code = "fun function(array: Array<Int>, array2: IntArray) {}" | ||
assertThat(subject.compileAndLint(code)).hasSize(1) | ||
} | ||
|
||
it("both are arrays of primitive types") { | ||
@Test | ||
fun `both are arrays of primitive types`() { | ||
val code = "fun function(array: Array<Int>, array2: Array<Double>) {}" | ||
assertThat(subject.compileAndLint(code)).hasSize(2) | ||
} | ||
} | ||
|
||
describe("return type") { | ||
it("is Array<Primitive>") { | ||
@Nested | ||
inner class `return type` { | ||
@Test | ||
@DisplayName("is Array<Primitive>") | ||
fun isArrayPrimitive() { | ||
val code = "fun returningFunction(): Array<Float> { return emptyArray() }" | ||
assertThat(subject.compileAndLint(code)).hasSize(1) | ||
} | ||
|
||
it("is not an array") { | ||
@Test | ||
fun `is not an array`() { | ||
val code = "fun returningFunction(): Int { return 1 }" | ||
assertThat(subject.compileAndLint(code)).isEmpty() | ||
} | ||
|
||
it("is a specialized array") { | ||
@Test | ||
fun `is a specialized array`() { | ||
val code = "fun returningFunction(): CharArray { return CharArray(0) }" | ||
assertThat(subject.compileAndLint(code)).isEmpty() | ||
} | ||
|
||
it("is a star-projected array") { | ||
@Test | ||
fun `is a star-projected array`() { | ||
val code = "fun returningFunction(): Array<*> { return emptyArray<Any>() }" | ||
assertThat(subject.compileAndLint(code)).isEmpty() | ||
} | ||
|
||
it("is not explicitly set") { | ||
@Test | ||
fun `is not explicitly set`() { | ||
val code = "fun returningFunction() {}" | ||
assertThat(subject.compileAndLint(code)).isEmpty() | ||
} | ||
} | ||
|
||
describe("variable type") { | ||
it("is Array<Primitive>") { | ||
@Nested | ||
inner class `variable type` { | ||
@Test | ||
@DisplayName("is Array<Primitive>") | ||
fun isArrayPrimitive() { | ||
val code = "val foo: Array<Int>? = null" | ||
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) | ||
} | ||
} | ||
|
||
describe("receiver type") { | ||
it("is Array<Primitive>") { | ||
@Nested | ||
inner class `receiver type` { | ||
@Test | ||
@DisplayName("is Array<Primitive>") | ||
fun isArrayPrimitive() { | ||
val code = "fun Array<Boolean>.foo() { println(this) }" | ||
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) | ||
} | ||
} | ||
|
||
describe("arrayOf") { | ||
it("is arrayOf(Char)") { | ||
@Nested | ||
inner class `arrayOf` { | ||
@Test | ||
fun `is arrayOf(Char)`() { | ||
val code = "fun foo(x: Char) = arrayOf(x)" | ||
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) | ||
} | ||
|
||
it("is arrayOf(Byte)") { | ||
@Test | ||
fun `is arrayOf(Byte)`() { | ||
val code = "fun foo(x: Byte) = arrayOf(x)" | ||
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) | ||
} | ||
|
||
it("is arrayOf(Short)") { | ||
@Test | ||
fun `is arrayOf(Short)`() { | ||
val code = "fun foo(x: Short) = arrayOf(x)" | ||
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) | ||
} | ||
|
||
it("is arrayOf(Int)") { | ||
@Test | ||
fun `is arrayOf(Int)`() { | ||
val code = "fun foo(x: Int) = arrayOf(x)" | ||
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) | ||
} | ||
|
||
it("is arrayOf(Long)") { | ||
@Test | ||
fun `is arrayOf(Long)`() { | ||
val code = "fun foo(x: Long) = arrayOf(x)" | ||
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) | ||
} | ||
|
||
it("is arrayOf(Float)") { | ||
@Test | ||
fun `is arrayOf(Float)`() { | ||
val code = "fun foo(x: Float) = arrayOf(x)" | ||
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) | ||
} | ||
|
||
it("is arrayOf(Double)") { | ||
@Test | ||
fun `is arrayOf(Double)`() { | ||
val code = "fun foo(x: Double) = arrayOf(x)" | ||
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) | ||
} | ||
|
||
it("is arrayOf(Boolean)") { | ||
@Test | ||
fun `is arrayOf(Boolean)`() { | ||
val code = "fun foo(x: Boolean) = arrayOf(x)" | ||
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) | ||
} | ||
|
||
it("is arrayOf(String)") { | ||
@Test | ||
fun `is arrayOf(String)`() { | ||
val code = "fun foo(x: String) = arrayOf(x)" | ||
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() | ||
} | ||
|
||
it("is intArrayOf()") { | ||
@Test | ||
fun `is intArrayOf()`() { | ||
val code = "fun test(x: Int) = intArrayOf(x)" | ||
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() | ||
} | ||
} | ||
|
||
describe("emptyArray") { | ||
it("is emptyArray<Char>()") { | ||
@Nested | ||
inner class `emptyArray` { | ||
@Test | ||
@DisplayName("is emptyArray<Char>()") | ||
fun isEmptyArrayChar() { | ||
val code = "val a = emptyArray<Char>()" | ||
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) | ||
} | ||
|
||
it("is emptyArray<Byte>()") { | ||
@Test | ||
@DisplayName("is emptyArray<Byte>()") | ||
fun isEmptyArrayByte() { | ||
val code = "val a = emptyArray<Byte>()" | ||
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) | ||
} | ||
|
||
it("is emptyArray<Short>()") { | ||
@Test | ||
@DisplayName("is emptyArray<Short>()") | ||
fun isEmptyArrayShort() { | ||
val code = "val a = emptyArray<Short>()" | ||
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) | ||
} | ||
|
||
it("is emptyArray<Int>()") { | ||
@Test | ||
@DisplayName("is emptyArray<Int>()") | ||
fun isEmptyArrayInt() { | ||
val code = "val a = emptyArray<Int>()" | ||
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) | ||
} | ||
|
||
it("is emptyArray<Long>()") { | ||
@Test | ||
@DisplayName("is emptyArray<Long>()") | ||
fun isEmptyArrayLong() { | ||
val code = "val a = emptyArray<Long>()" | ||
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) | ||
} | ||
|
||
it("is emptyArray<Float>()") { | ||
@Test | ||
@DisplayName("is emptyArray<Float>()") | ||
fun isEmptyArrayFloat() { | ||
val code = "val a = emptyArray<Float>()" | ||
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) | ||
} | ||
|
||
it("is emptyArray<Double>()") { | ||
@Test | ||
@DisplayName("is emptyArray<Double>()") | ||
fun isEmptyArrayDouble() { | ||
val code = "val a = emptyArray<Double>()" | ||
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) | ||
} | ||
|
||
it("is emptyArray<Boolean>()") { | ||
@Test | ||
@DisplayName("is emptyArray<Boolean>()") | ||
fun isEmptyArrayBoolean() { | ||
val code = "val a = emptyArray<Boolean>()" | ||
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) | ||
} | ||
|
||
it("is emptyArray<String>()") { | ||
@Test | ||
@DisplayName("is emptyArray<String>()") | ||
fun isEmptyArrayString() { | ||
val code = "val a = emptyArray<String>()" | ||
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.