Migrate detekt-rules-performance tests to JUnit (#4569)

This commit is contained in:
Matthew Haughton
2022-02-08 07:49:00 +11:00
committed by GitHub
parent 780849fc09
commit 7126e4ba3e
5 changed files with 191 additions and 102 deletions

View File

@@ -5,6 +5,5 @@ plugins {
dependencies {
compileOnly(projects.detektApi)
testImplementation(projects.detektTest)
testImplementation(libs.bundles.testImplementation)
testRuntimeOnly(libs.spek.runner)
testImplementation(libs.assertj)
}

View File

@@ -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()
}
}
})
}

View File

@@ -2,16 +2,18 @@ package io.gitlab.arturbosch.detekt.rules.performance
import io.gitlab.arturbosch.detekt.test.compileAndLint
import org.assertj.core.api.Assertions.assertThat
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
class ForEachOnRangeSpec : Spek({
class ForEachOnRangeSpec {
val subject by memoized { ForEachOnRange() }
val subject = ForEachOnRange()
describe("ForEachOnRange rule") {
@Nested
inner class `ForEachOnRange rule` {
context("using a forEach on a range") {
@Nested
inner class `using a forEach on a range` {
val code = """
fun test() {
(1..10).forEach {
@@ -29,26 +31,30 @@ class ForEachOnRangeSpec : Spek({
}
"""
it("should report the forEach usage") {
@Test
fun `should report the forEach usage`() {
val findings = subject.compileAndLint(code)
assertThat(findings).hasSize(4)
}
}
context("using any other method on a range") {
@Nested
inner class `using any other method on a range` {
val code = """
fun test() {
(1..10).isEmpty()
}
"""
it("should not report any issues") {
@Test
fun `should not report any issues`() {
val findings = subject.compileAndLint(code)
assertThat(findings).isEmpty()
}
}
context("using a forEach on a list") {
@Nested
inner class `using a forEach on a list` {
val code = """
fun test() {
listOf(1, 2, 3).forEach {
@@ -57,10 +63,11 @@ class ForEachOnRangeSpec : Spek({
}
"""
it("should not report any issues") {
@Test
fun `should not report any issues`() {
val findings = subject.compileAndLint(code)
assertThat(findings).isEmpty()
}
}
}
})
}

View File

@@ -1,31 +1,33 @@
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.Nested
import org.junit.jupiter.api.Test
class SpreadOperatorSpec : Spek({
setupKotlinEnvironment()
@KotlinCoreEnvironmentTest
class SpreadOperatorSpec(val env: KotlinCoreEnvironment) {
val env: KotlinCoreEnvironment by memoized()
val subject by memoized { SpreadOperator() }
val subject = SpreadOperator()
describe("SpreadOperator rule") {
@Nested
inner class `SpreadOperator rule` {
/**
* This rule has different behaviour depending on whether type resolution is enabled in detekt or not. The two
* `context` blocks are there to test behaviour when type resolution is enabled and type resolution is disabled
* as different warning messages are shown in each case.
*/
context("with type resolution") {
@Nested
inner class `with type resolution` {
val typeResolutionEnabledMessage = "Used in this way a spread operator causes a full copy of the array to" +
" be created before calling a method. This may result in a performance penalty."
it("reports when array copy required using named parameters") {
@Test
fun `reports when array copy required using named parameters`() {
val code = """
val xsArray = intArrayOf(1)
fun foo(vararg xs: Int) {}
@@ -35,7 +37,9 @@ class SpreadOperatorSpec : Spek({
assertThat(actual).hasSize(1)
assertThat(actual.first().message).isEqualTo(typeResolutionEnabledMessage)
}
it("reports when array copy required without using named parameters") {
@Test
fun `reports when array copy required without using named parameters`() {
val code = """
val xsArray = intArrayOf(1)
fun foo(vararg xs: Int) {}
@@ -45,7 +49,9 @@ class SpreadOperatorSpec : Spek({
assertThat(actual).hasSize(1)
assertThat(actual.first().message).isEqualTo(typeResolutionEnabledMessage)
}
it("doesn't report when using array constructor with spread operator") {
@Test
fun `doesn't report when using array constructor with spread operator`() {
val code = """
fun foo(vararg xs: Int) {}
val testVal = foo(xs = *intArrayOf(1))
@@ -53,7 +59,8 @@ class SpreadOperatorSpec : Spek({
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("doesn't report when using array constructor with spread operator when varargs parameter comes first") {
@Test
fun `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")
@@ -61,7 +68,8 @@ class SpreadOperatorSpec : Spek({
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("doesn't report when passing values directly") {
@Test
fun `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")
@@ -69,7 +77,8 @@ class SpreadOperatorSpec : Spek({
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("doesn't report when function doesn't take a vararg parameter") {
@Test
fun `doesn't report when function doesn't take a vararg parameter`() {
val code = """
fun test0(strs: Array<String>) {
test(strs)
@@ -82,7 +91,8 @@ class SpreadOperatorSpec : Spek({
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("doesn't report with expression inside params") {
@Test
fun `doesn't report with expression inside params`() {
val code = """
fun test0(strs: Array<String>) {
test(2*2)
@@ -95,7 +105,8 @@ class SpreadOperatorSpec : Spek({
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("respects pass through of vararg parameter - #3145") {
@Test
fun `respects pass through of vararg parameter - #3145`() {
val code = """
fun b(vararg bla: Int) = Unit
fun a(vararg bla: Int) {
@@ -105,7 +116,8 @@ class SpreadOperatorSpec : Spek({
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
it("reports shadowed vararg declaration which may lead to array copy - #3145") {
@Test
fun `reports shadowed vararg declaration which may lead to array copy - #3145`() {
val code = """
fun b(vararg bla: String) = Unit
@@ -118,12 +130,14 @@ class SpreadOperatorSpec : Spek({
}
}
context("without type resolution") {
@Nested
inner class `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. This may result in a performance penalty."
it("reports when array copy required using named parameters") {
@Test
fun `reports when array copy required using named parameters`() {
val code = """
val xsArray = intArrayOf(1)
fun foo(vararg xs: Int) {}
@@ -133,7 +147,9 @@ class SpreadOperatorSpec : Spek({
assertThat(actual).hasSize(1)
assertThat(actual.first().message).isEqualTo(typeResolutionDisabledMessage)
}
it("reports when array copy required without using named parameters") {
@Test
fun `reports when array copy required without using named parameters`() {
val code = """
val xsArray = intArrayOf(1)
fun foo(vararg xs: Int) {}
@@ -143,7 +159,9 @@ class SpreadOperatorSpec : Spek({
assertThat(actual).hasSize(1)
assertThat(actual.first().message).isEqualTo(typeResolutionDisabledMessage)
}
it("doesn't report when using array constructor with spread operator") {
@Test
fun `doesn't report when using array constructor with spread operator`() {
val code = """
fun foo(vararg xs: Int) {}
val testVal = foo(xs = *intArrayOf(1))
@@ -153,7 +171,8 @@ class SpreadOperatorSpec : Spek({
assertThat(actual.first().message).isEqualTo(typeResolutionDisabledMessage)
}
it("doesn't report when using array constructor with spread operator when varargs parameter comes first") {
@Test
fun `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")
@@ -163,7 +182,8 @@ class SpreadOperatorSpec : Spek({
assertThat(actual.first().message).isEqualTo(typeResolutionDisabledMessage)
}
it("doesn't report when passing values directly") {
@Test
fun `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")
@@ -171,7 +191,8 @@ class SpreadOperatorSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}
it("doesn't report when function doesn't take a vararg parameter") {
@Test
fun `doesn't report when function doesn't take a vararg parameter`() {
val code = """
fun test0(strs: Array<String>) {
test(strs)
@@ -184,7 +205,8 @@ class SpreadOperatorSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}
it("doesn't report with expression inside params") {
@Test
fun `doesn't report with expression inside params`() {
val code = """
fun test0(strs: Array<String>) {
test(2*2)
@@ -197,7 +219,8 @@ class SpreadOperatorSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}
it("respects pass through of vararg parameter - #3145") {
@Test
fun `respects pass through of vararg parameter - #3145`() {
val code = """
fun b(vararg bla: Int) = Unit
fun a(vararg bla: Int) {
@@ -207,7 +230,8 @@ class SpreadOperatorSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}
it("does not report shadowed vararg declaration, we except this false negative here - #3145") {
@Test
fun `does not report shadowed vararg declaration, we except this false negative here - #3145`() {
val code = """
fun b(vararg bla: String) = Unit
@@ -220,4 +244,4 @@ class SpreadOperatorSpec : Spek({
}
}
}
})
}

View File

@@ -2,22 +2,25 @@ package io.gitlab.arturbosch.detekt.rules.performance
import io.gitlab.arturbosch.detekt.test.compileAndLint
import org.assertj.core.api.Assertions.assertThat
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
class UnnecessaryTemporaryInstantiationSpec : Spek({
val subject by memoized { UnnecessaryTemporaryInstantiation() }
class UnnecessaryTemporaryInstantiationSpec {
val subject = UnnecessaryTemporaryInstantiation()
describe("UnnecessaryTemporaryInstantiation rule") {
@Nested
inner class `UnnecessaryTemporaryInstantiation rule` {
it("temporary instantiation for conversion") {
@Test
fun `temporary instantiation for conversion`() {
val code = "val i = Integer(1).toString()"
assertThat(subject.compileAndLint(code)).hasSize(1)
}
it("right conversion without instantiation") {
@Test
fun `right conversion without instantiation`() {
val code = "val i = Integer.toString(1)"
assertThat(subject.compileAndLint(code)).isEmpty()
}
}
})
}