Remove Unnecesary @Nested (#4740)

This commit is contained in:
Brais Gabín
2022-04-25 23:04:26 +02:00
committed by GitHub
parent 7880f4f237
commit a2734b18c1
238 changed files with 27537 additions and 28638 deletions

View File

@@ -10,64 +10,60 @@ class ForEachOnRangeSpec {
val subject = ForEachOnRange()
@Nested
inner class `ForEachOnRange rule` {
@Nested
inner class `using a forEach on a range` {
val code = """
fun test() {
(1..10).forEach {
println(it)
}
(1 until 10).forEach {
println(it)
}
(10 downTo 1).forEach {
println(it)
}
(10 downTo 1 step 2).forEach {
println(it)
}
inner class `using a forEach on a range` {
val code = """
fun test() {
(1..10).forEach {
println(it)
}
"""
@Test
fun `should report the forEach usage`() {
val findings = subject.compileAndLint(code)
assertThat(findings).hasSize(4)
(1 until 10).forEach {
println(it)
}
(10 downTo 1).forEach {
println(it)
}
(10 downTo 1 step 2).forEach {
println(it)
}
}
"""
@Nested
inner class `using any other method on a range` {
val code = """
fun test() {
(1..10).isEmpty()
}
"""
@Test
fun `should report the forEach usage`() {
val findings = subject.compileAndLint(code)
assertThat(findings).hasSize(4)
}
}
@Test
fun `should not report any issues`() {
val findings = subject.compileAndLint(code)
assertThat(findings).isEmpty()
@Nested
inner class `using any other method on a range` {
val code = """
fun test() {
(1..10).isEmpty()
}
"""
@Test
fun `should not report any issues`() {
val findings = subject.compileAndLint(code)
assertThat(findings).isEmpty()
}
}
@Nested
inner class `using a forEach on a list` {
val code = """
fun test() {
listOf(1, 2, 3).forEach {
println(it)
}
}
"""
@Nested
inner class `using a forEach on a list` {
val code = """
fun test() {
listOf(1, 2, 3).forEach {
println(it)
}
}
"""
@Test
fun `should not report any issues`() {
val findings = subject.compileAndLint(code)
assertThat(findings).isEmpty()
}
@Test
fun `should not report any issues`() {
val findings = subject.compileAndLint(code)
assertThat(findings).isEmpty()
}
}
}

View File

@@ -13,235 +13,232 @@ class SpreadOperatorSpec(val env: KotlinCoreEnvironment) {
val subject = SpreadOperator()
/**
* 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.
*/
@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.
*/
@Nested
inner class `with type resolution` {
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."
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."
@Test
fun `reports when array copy required using named parameters`() {
val code = """
val xsArray = intArrayOf(1)
fun foo(vararg xs: Int) {}
val testVal = foo(xs = *xsArray)
"""
val actual = subject.compileAndLintWithContext(env, code)
assertThat(actual).hasSize(1)
assertThat(actual.first().message).isEqualTo(typeResolutionEnabledMessage)
}
@Test
fun `reports when array copy required without using named parameters`() {
val code = """
val xsArray = intArrayOf(1)
fun foo(vararg xs: Int) {}
val testVal = foo(*xsArray)
"""
val actual = subject.compileAndLintWithContext(env, code)
assertThat(actual).hasSize(1)
assertThat(actual.first().message).isEqualTo(typeResolutionEnabledMessage)
}
@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))
"""
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
@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")
"""
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
@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")
"""
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
@Test
fun `doesn't report when function doesn't take a vararg parameter`() {
val code = """
fun test0(strs: Array<String>) {
test(strs)
}
fun test(strs: Array<String>) {
strs.forEach { println(it) }
}
"""
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
@Test
fun `doesn't report with expression inside params`() {
val code = """
fun test0(strs: Array<String>) {
test(2*2)
}
fun test(test : Int) {
println(test)
}
"""
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
@Test
fun `respects pass through of vararg parameter - #3145`() {
val code = """
fun b(vararg bla: Int) = Unit
fun a(vararg bla: Int) {
b(*bla)
}
"""
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
@Test
fun `reports shadowed vararg declaration which may lead to array copy - #3145`() {
val code = """
fun b(vararg bla: String) = Unit
fun a(vararg bla: Int) {
val bla = arrayOf("")
b(*bla)
}
"""
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
@Test
fun `reports when array copy required using named parameters`() {
val code = """
val xsArray = intArrayOf(1)
fun foo(vararg xs: Int) {}
val testVal = foo(xs = *xsArray)
"""
val actual = subject.compileAndLintWithContext(env, code)
assertThat(actual).hasSize(1)
assertThat(actual.first().message).isEqualTo(typeResolutionEnabledMessage)
}
@Nested
inner class `without type resolution` {
@Test
fun `reports when array copy required without using named parameters`() {
val code = """
val xsArray = intArrayOf(1)
fun foo(vararg xs: Int) {}
val testVal = foo(*xsArray)
"""
val actual = subject.compileAndLintWithContext(env, code)
assertThat(actual).hasSize(1)
assertThat(actual.first().message).isEqualTo(typeResolutionEnabledMessage)
}
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."
@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))
"""
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
@Test
fun `reports when array copy required using named parameters`() {
val code = """
val xsArray = intArrayOf(1)
fun foo(vararg xs: Int) {}
val testVal = foo(xs = *xsArray)
"""
val actual = subject.compileAndLint(code)
assertThat(actual).hasSize(1)
assertThat(actual.first().message).isEqualTo(typeResolutionDisabledMessage)
@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")
"""
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
@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")
"""
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
@Test
fun `doesn't report when function doesn't take a vararg parameter`() {
val code = """
fun test0(strs: Array<String>) {
test(strs)
}
@Test
fun `reports when array copy required without using named parameters`() {
val code = """
val xsArray = intArrayOf(1)
fun foo(vararg xs: Int) {}
val testVal = foo(*xsArray)
"""
val actual = subject.compileAndLint(code)
assertThat(actual).hasSize(1)
assertThat(actual.first().message).isEqualTo(typeResolutionDisabledMessage)
fun test(strs: Array<String>) {
strs.forEach { println(it) }
}
"""
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
@Test
fun `doesn't report with expression inside params`() {
val code = """
fun test0(strs: Array<String>) {
test(2*2)
}
@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))
"""
val actual = subject.compileAndLint(code)
assertThat(actual).hasSize(1)
assertThat(actual.first().message).isEqualTo(typeResolutionDisabledMessage)
fun test(test : Int) {
println(test)
}
"""
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
@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")
"""
val actual = subject.compileAndLint(code)
assertThat(actual).hasSize(1)
assertThat(actual.first().message).isEqualTo(typeResolutionDisabledMessage)
}
@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")
"""
assertThat(subject.compileAndLint(code)).isEmpty()
}
@Test
fun `doesn't report when function doesn't take a vararg parameter`() {
val code = """
fun test0(strs: Array<String>) {
test(strs)
@Test
fun `respects pass through of vararg parameter - #3145`() {
val code = """
fun b(vararg bla: Int) = Unit
fun a(vararg bla: Int) {
b(*bla)
}
"""
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
fun test(strs: Array<String>) {
strs.forEach { println(it) }
@Test
fun `reports shadowed vararg declaration which may lead to array copy - #3145`() {
val code = """
fun b(vararg bla: String) = Unit
fun a(vararg bla: Int) {
val bla = arrayOf("")
b(*bla)
}
"""
assertThat(subject.compileAndLint(code)).isEmpty()
"""
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
}
@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."
@Test
fun `reports when array copy required using named parameters`() {
val code = """
val xsArray = intArrayOf(1)
fun foo(vararg xs: Int) {}
val testVal = foo(xs = *xsArray)
"""
val actual = subject.compileAndLint(code)
assertThat(actual).hasSize(1)
assertThat(actual.first().message).isEqualTo(typeResolutionDisabledMessage)
}
@Test
fun `reports when array copy required without using named parameters`() {
val code = """
val xsArray = intArrayOf(1)
fun foo(vararg xs: Int) {}
val testVal = foo(*xsArray)
"""
val actual = subject.compileAndLint(code)
assertThat(actual).hasSize(1)
assertThat(actual.first().message).isEqualTo(typeResolutionDisabledMessage)
}
@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))
"""
val actual = subject.compileAndLint(code)
assertThat(actual).hasSize(1)
assertThat(actual.first().message).isEqualTo(typeResolutionDisabledMessage)
}
@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")
"""
val actual = subject.compileAndLint(code)
assertThat(actual).hasSize(1)
assertThat(actual.first().message).isEqualTo(typeResolutionDisabledMessage)
}
@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")
"""
assertThat(subject.compileAndLint(code)).isEmpty()
}
@Test
fun `doesn't report when function doesn't take a vararg parameter`() {
val code = """
fun test0(strs: Array<String>) {
test(strs)
}
@Test
fun `doesn't report with expression inside params`() {
val code = """
fun test0(strs: Array<String>) {
test(2*2)
fun test(strs: Array<String>) {
strs.forEach { println(it) }
}
"""
assertThat(subject.compileAndLint(code)).isEmpty()
}
@Test
fun `doesn't report with expression inside params`() {
val code = """
fun test0(strs: Array<String>) {
test(2*2)
}
fun test(test : Int) {
println(test)
}
"""
assertThat(subject.compileAndLint(code)).isEmpty()
}
@Test
fun `respects pass through of vararg parameter - #3145`() {
val code = """
fun b(vararg bla: Int) = Unit
fun a(vararg bla: Int) {
b(*bla)
}
"""
assertThat(subject.compileAndLint(code)).isEmpty()
}
fun test(test : Int) {
println(test)
@Test
fun `does not report shadowed vararg declaration, we except this false negative here - #3145`() {
val code = """
fun b(vararg bla: String) = Unit
fun a(vararg bla: Int) {
val bla = arrayOf("")
b(*bla)
}
"""
assertThat(subject.compileAndLint(code)).isEmpty()
}
@Test
fun `respects pass through of vararg parameter - #3145`() {
val code = """
fun b(vararg bla: Int) = Unit
fun a(vararg bla: Int) {
b(*bla)
}
"""
assertThat(subject.compileAndLint(code)).isEmpty()
}
@Test
fun `does not report shadowed vararg declaration, we except this false negative here - #3145`() {
val code = """
fun b(vararg bla: String) = Unit
fun a(vararg bla: Int) {
val bla = arrayOf("")
b(*bla)
}
"""
assertThat(subject.compileAndLint(code)).isEmpty()
}
"""
assertThat(subject.compileAndLint(code)).isEmpty()
}
}
}

View File

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