From 6f1dc2fdb2ac611cc4abe4b0f187906b73696e56 Mon Sep 17 00:00:00 2001 From: Markus Schwarz Date: Mon, 25 Apr 2022 20:55:18 +0200 Subject: [PATCH 1/7] Allow additionalJavaSourceRootPaths to be defined on @KotlinCoreEnvironmentTest --- ...plicitCollectionElementAccessMethodSpec.kt | 476 +++++++++--------- .../rules/style/ObjectLiteralToLambdaSpec.kt | 119 ++--- .../rules/KotlinEnvironmentTestSetup.kt | 14 +- 3 files changed, 300 insertions(+), 309 deletions(-) diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExplicitCollectionElementAccessMethodSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExplicitCollectionElementAccessMethodSpec.kt index 6d8ba99b97a..4f21c034773 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExplicitCollectionElementAccessMethodSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExplicitCollectionElementAccessMethodSpec.kt @@ -1,450 +1,442 @@ package io.gitlab.arturbosch.detekt.rules.style -import io.github.detekt.test.utils.createEnvironment -import io.github.detekt.test.utils.resourceAsPath import io.gitlab.arturbosch.detekt.api.Config import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext import io.gitlab.arturbosch.detekt.test.lintWithContext import org.assertj.core.api.Assertions.assertThat import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -import org.junit.jupiter.api.AfterAll import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test -@KotlinCoreEnvironmentTest -class ExplicitCollectionElementAccessMethodSpec(val env: KotlinCoreEnvironment) { +class ExplicitCollectionElementAccessMethodSpec() { val subject = ExplicitCollectionElementAccessMethod(Config.empty) @Nested - inner class `Kotlin map` { + @KotlinCoreEnvironmentTest + inner class DefaultSourceTests(val env: KotlinCoreEnvironment) { + @Nested + inner class `Kotlin map` { - @Test - fun `reports map element access with get method`() { - val code = """ + @Test + fun `reports map element access with get method`() { + val code = """ fun f() { val map = mapOf() val value = map.get("key") } - """ - assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) - } + """ + assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) + } - @Test - fun `does not report safe map element access`() { - val code = """ + @Test + fun `does not report safe map element access`() { + val code = """ fun f() { val map = mapOf() val value = map?.get("key") } - """ - assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() - } + """ + assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() + } - @Test - fun `reports map set method usage with unused return value`() { - val code = """ + @Test + fun `reports map set method usage with unused return value`() { + val code = """ fun f() { val map = mutableMapOf() map.set("key", "value") } - """ - assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) - } + """ + assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) + } - @Test - fun `reports map put method usage with unused return value`() { - val code = """ + @Test + fun `reports map put method usage with unused return value`() { + val code = """ fun f() { val map = mutableMapOf() map.put("key", "val") } - """ - assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) - } + """ + assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) + } - @Test - fun `does not report map put method usage with variable assignment`() { - val code = """ + @Test + fun `does not report map put method usage with variable assignment`() { + val code = """ fun f() { val map = mutableMapOf() val oldValue = map.put("key", "val") } - """ - assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() - } + """ + assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() + } - @Test - fun `does not report map put method with used return value`() { - val code = """ + @Test + fun `does not report map put method with used return value`() { + val code = """ fun f(): Boolean { val map = mutableMapOf() return map.put("key", "val") == null } - """ - assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() - } + """ + assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() + } - @Test - fun `reports map element access with get method of non-abstract map`() { - val code = """ + @Test + fun `reports map element access with get method of non-abstract map`() { + val code = """ fun f() { val map = hashMapOf() val value = map.get("key") } - """ - assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) - } + """ + assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) + } - @Test - fun `reports map element insert with put method of non-abstract map`() { - val code = """ + @Test + fun `reports map element insert with put method of non-abstract map`() { + val code = """ fun f() { val map = hashMapOf() map.put("key", "value") } - """ - assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) - } + """ + assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) + } - @Test - @DisplayName("does not report map access with []") - fun noReportMapAccessWithBrackets() { - val code = """ + @Test + @DisplayName("does not report map access with []") + fun noReportMapAccessWithBrackets() { + val code = """ fun f() { val map = mapOf() val value = map["key"] } - """ - assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() - } + """ + assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() + } - @Test - @DisplayName("does not report map insert with []") - fun noReportMapInsertWithBrackets() { - val code = """ + @Test + @DisplayName("does not report map insert with []") + fun noReportMapInsertWithBrackets() { + val code = """ fun f() { val map = mutableMapOf() map["key"] = "value" } - """ - assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() - } + """ + assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() + } - @Test - fun `reports map element access with get method from map in a chain`() { - val code = """ + @Test + fun `reports map element access with get method from map in a chain`() { + val code = """ fun f() { val map = mapOf() val value = listOf("1", "2").associateBy { it }.get("1") } - """ - assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) - } + """ + assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) + } - @Test - fun `reports map element access with get method from non-abstract map`() { - val code = """ + @Test + fun `reports map element access with get method from non-abstract map`() { + val code = """ fun f() { val map = linkedMapOf() val value = map.get("key") } - """ - assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) - } + """ + assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) + } - @Test - fun `does not report calls on implicit receiver`() { - val code = """ + @Test + fun `does not report calls on implicit receiver`() { + val code = """ fun f() { val map = mapOf() with(map) { get("a") } } - """ - assertThat(subject.compileAndLintWithContext(env, code)).hasSize(0) + """ + assertThat(subject.compileAndLintWithContext(env, code)).hasSize(0) + } } - } - @Nested - inner class `Kotlin list` { - @Test - fun `reports list element access with get method`() { - val code = """ + @Nested + inner class `Kotlin list` { + @Test + fun `reports list element access with get method`() { + val code = """ fun f() { val list = listOf() val value = list.get(0) } - """ - assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) - } + """ + assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) + } - @Test - fun `reports mutable list element access with get method`() { - val code = """ + @Test + fun `reports mutable list element access with get method`() { + val code = """ fun f() { val list = mutableListOf() val value = list.get(0) } - """ - assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) - } + """ + assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) + } - @Test - @DisplayName("does not report element access with []") - fun noReportElementAccessWithBrackets() { - val code = """ + @Test + @DisplayName("does not report element access with []") + fun noReportElementAccessWithBrackets() { + val code = """ fun f() { val list = listOf() val value = list[0] } - """ - assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() - } + """ + assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() + } - @Test - fun `reports element access with get method of non-abstract list`() { - val code = """ + @Test + fun `reports element access with get method of non-abstract list`() { + val code = """ fun f() { val list = arrayListOf() val value = list.get(0) } - """ - assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) - } + """ + assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) + } - @Test - fun `does not report calls on implicit receiver`() { - val code = """ + @Test + fun `does not report calls on implicit receiver`() { + val code = """ fun f() { val list = listOf() val value = with(list) { get(0) } } - """ - assertThat(subject.compileAndLintWithContext(env, code)).hasSize(0) + """ + assertThat(subject.compileAndLintWithContext(env, code)).hasSize(0) + } } - } - @Nested - inner class `Java map` { + @Nested + inner class `Java map` { - @Test - fun `reports map element access with get method`() { - val code = """ + @Test + fun `reports map element access with get method`() { + val code = """ fun f() { val map = java.util.HashMap() val value = map.get("key") } - """ - assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) - } + """ + assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) + } - @Test - fun `reports map set method usage with unused return value`() { - val code = """ + @Test + fun `reports map set method usage with unused return value`() { + val code = """ fun f() { val map = java.util.HashMap() map.set("key", "val") } - """ - assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) - } + """ + assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) + } - @Test - fun `reports map put method usage with unused return value`() { - val code = """ + @Test + fun `reports map put method usage with unused return value`() { + val code = """ fun f() { val map = java.util.HashMap() map.put("key", "val") } - """ - assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) - } + """ + assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) + } - @Test - @DisplayName("does not report map access with []") - fun noReportMapAccessWithBrackets() { - val code = """ + @Test + @DisplayName("does not report map access with []") + fun noReportMapAccessWithBrackets() { + val code = """ fun f() { val map = java.util.HashMap() val value = map["key"] } - """ - assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() - } + """ + assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() + } - @Test - @DisplayName("does not report map insert with []") - fun noReportMapInsertWithBrackets() { - val code = """ + @Test + @DisplayName("does not report map insert with []") + fun noReportMapInsertWithBrackets() { + val code = """ fun f() { val map = java.util.HashMap() map["key"] = "value" } - """ - assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() - } + """ + assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() + } - @Test - fun `reports map element access with get method from map in a chain`() { - val code = """ + @Test + fun `reports map element access with get method from map in a chain`() { + val code = """ fun f() { val map = java.util.HashMap() val value = listOf("1", "2").associateBy { it }.get("1") } - """ - assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) + """ + assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) + } } - } - @Nested - inner class `custom operators` { + @Nested + inner class `custom operators` { - @Test - fun `reports custom get operator`() { - val code = """ + @Test + fun `reports custom get operator`() { + val code = """ class Custom { operator fun get(i: Int) = 42 } fun f() { val custom = Custom() val value = custom.get(0) } - """ - assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) - } + """ + assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) + } - @Test - fun `does not report non-operator get method`() { - val code = """ + @Test + fun `does not report non-operator get method`() { + val code = """ class Custom { fun get(i: Int) = 42 } fun f() { val custom = Custom() val value = custom.get(0) } - """ - assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() - } + """ + assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() + } - @Test - fun `reports custom set operator with unused return value`() { - val code = """ + @Test + fun `reports custom set operator with unused return value`() { + val code = """ class Custom { operator fun set(key: String, value: String) {} } fun f() { val custom = Custom() custom.set("key", "value") } - """ - assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) - } + """ + assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) + } - @Test - fun `does not report non-operator set method`() { - val code = """ + @Test + fun `does not report non-operator set method`() { + val code = """ class Custom { fun set(key: String, value: String) {} } fun f() { val custom = Custom() custom.set("key", "value") } - """ - assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() + """ + assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() + } } - } - @Nested - inner class `Java list` { + @Nested + inner class `Java list` { - @Test - fun `reports list element access with get method`() { - val code = """ + @Test + fun `reports list element access with get method`() { + val code = """ fun f() { val list = java.util.ArrayList() val value = list.get(0) } - """ - assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) - } + """ + assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1) + } - @Test - @DisplayName("does not report element access with []") - fun noReportElementAccessWithBrackets() { - val code = """ + @Test + @DisplayName("does not report element access with []") + fun noReportElementAccessWithBrackets() { + val code = """ fun f() { val list = java.util.ArrayList() val value = list[0] } - """ - assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() + """ + assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() + } } - } - @Nested - inner class `edge cases` { + @Nested + inner class `edge cases` { - @Test - fun `does not crash for getter`() { - val code = """ + @Test + fun `does not crash for getter`() { + val code = """ class A { val i: Int get() = 1 + 2 val c: Char? get() = "".first() ?: throw IllegalArgumentException("getter") } - """ - assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() - } + """ + assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() + } - @Test - fun `does not crash for fluent api`() { - val code = """ + @Test + fun `does not crash for fluent api`() { + val code = """ val string = "" .toString() - """ - assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() - } + """ + assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() + } - @Test - fun `does not report for unresolvable code`() { - val code = """ + @Test + fun `does not report for unresolvable code`() { + val code = """ fun f() { val unknownType = UnknownType() val value = unknownType.put("answer", 42) } - """ - assertThat(subject.lintWithContext(env, code)).isEmpty() - } + """ + assertThat(subject.lintWithContext(env, code)).isEmpty() + } - @Test - fun `does not report for put functions without caller`() { - val code = """ + @Test + fun `does not report for put functions without caller`() { + val code = """ fun put() { } fun f() { put() } - """ - assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() + """ + assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() + } } + } - @Nested - inner class JavaSourceTests { - - private val environmentWrapper = - createEnvironment(additionalJavaSourceRootPaths = listOf(resourceAsPath("java").toFile())) - private val customEnv = environmentWrapper.env - - @AfterAll - fun disposeEnvironment() { - environmentWrapper.dispose() - } + @Nested + @KotlinCoreEnvironmentTest(additionalJavaSourcePaths = ["java"]) + inner class JavaSourceTests(val env: KotlinCoreEnvironment) { - @Test - fun `does not report if the function has 3 or more arguments and it's defined in java - #4288`() { - val code = """ + @Test + fun `does not report if the function has 3 or more arguments and it's defined in java - #4288`() { + val code = """ import com.example.fromjava.Rect fun foo() { val rect = Rect() rect.set(0, 1, 2) } - """ - assertThat(subject.lintWithContext(customEnv, code)).isEmpty() - } + """ + assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } } } diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ObjectLiteralToLambdaSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ObjectLiteralToLambdaSpec.kt index 1dd4bbed6c9..c2d89179e83 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ObjectLiteralToLambdaSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ObjectLiteralToLambdaSpec.kt @@ -1,7 +1,5 @@ package io.gitlab.arturbosch.detekt.rules.style -import io.github.detekt.test.utils.createEnvironment -import io.github.detekt.test.utils.resourceAsPath import io.gitlab.arturbosch.detekt.api.SourceLocation import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest import io.gitlab.arturbosch.detekt.test.assert @@ -9,16 +7,15 @@ import io.gitlab.arturbosch.detekt.test.compileAndLint import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext import io.gitlab.arturbosch.detekt.test.lintWithContext import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -import org.junit.jupiter.api.AfterAll import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test -@KotlinCoreEnvironmentTest -class ObjectLiteralToLambdaSpec(val env: KotlinCoreEnvironment) { +class ObjectLiteralToLambdaSpec { val subject = ObjectLiteralToLambda() @Nested - inner class `ObjectLiteralToLambda rule` { + @KotlinCoreEnvironmentTest + inner class DefaultSourceTests(val env: KotlinCoreEnvironment) { @Nested inner class `report convertible expression` { @@ -387,65 +384,6 @@ class ObjectLiteralToLambdaSpec(val env: KotlinCoreEnvironment) { """ subject.compileAndLintWithContext(env, code).assert().isEmpty() } - - @Nested - inner class JavaSourceTests { - - private val environmentWrapper = - createEnvironment(additionalJavaSourceRootPaths = listOf(resourceAsPath("java").toFile())) - private val customEnv = environmentWrapper.env - - @AfterAll - fun disposeEnvironment() { - environmentWrapper.dispose() - } - - @Test - fun `has other default methods`() { - val code = """ - import com.example.fromjava.SamWithDefaultMethods - - fun main() { - val x = object : SamWithDefaultMethods { - override fun foo() { - println() - } - } - } - """ - - subject.lintWithContext(customEnv, code).assert().hasSize(1) - } - - @Test - fun `has only default methods`() { - val code = """ - import com.example.fromjava.OnlyDefaultMethods - - fun main() { - val x = object : OnlyDefaultMethods { - } - } - """ - subject.lintWithContext(customEnv, code).assert().isEmpty() - } - - @Test - fun `implements a default method`() { - val code = """ - import com.example.fromjava.OnlyDefaultMethods - - fun main() { - val x = object : OnlyDefaultMethods { - override fun foo() { - println() - } - } - } - """ - subject.lintWithContext(customEnv, code).assert().isEmpty() - } - } } @Nested @@ -600,4 +538,55 @@ class ObjectLiteralToLambdaSpec(val env: KotlinCoreEnvironment) { } } } + + @Nested + @KotlinCoreEnvironmentTest(additionalJavaSourcePaths = ["java"]) + inner class JavaSourceTests(val env: KotlinCoreEnvironment) { + + @Test + fun `has other default methods`() { + val code = """ + import com.example.fromjava.SamWithDefaultMethods + + fun main() { + val x = object : SamWithDefaultMethods { + override fun foo() { + println() + } + } + } + """ + + subject.lintWithContext(env, code).assert().hasSize(1) + } + + @Test + fun `has only default methods`() { + val code = """ + import com.example.fromjava.OnlyDefaultMethods + + fun main() { + val x = object : OnlyDefaultMethods { + } + } + """ + subject.lintWithContext(env, code).assert().isEmpty() + } + + @Test + fun `implements a default method`() { + val code = """ + import com.example.fromjava.OnlyDefaultMethods + + fun main() { + val x = object : OnlyDefaultMethods { + override fun foo() { + println() + } + } + } + """ + subject.lintWithContext(env, code).assert().isEmpty() + } + } } diff --git a/detekt-test-utils/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/KotlinEnvironmentTestSetup.kt b/detekt-test-utils/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/KotlinEnvironmentTestSetup.kt index 7daf4b41b84..08945d77dc7 100644 --- a/detekt-test-utils/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/KotlinEnvironmentTestSetup.kt +++ b/detekt-test-utils/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/KotlinEnvironmentTestSetup.kt @@ -2,6 +2,7 @@ package io.gitlab.arturbosch.detekt.rules import io.github.detekt.test.utils.KotlinCoreEnvironmentWrapper import io.github.detekt.test.utils.createEnvironment +import io.github.detekt.test.utils.resource import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment import org.junit.jupiter.api.extension.ExtendWith import org.junit.jupiter.api.extension.ExtensionContext @@ -9,6 +10,7 @@ import org.junit.jupiter.api.extension.ParameterContext import org.junit.jupiter.api.extension.ParameterResolver import org.spekframework.spek2.dsl.Root import org.spekframework.spek2.lifecycle.CachingMode +import java.io.File import java.nio.file.Path @Deprecated( @@ -30,7 +32,9 @@ fun Root.setupKotlinEnvironment(additionalJavaSourceRootPath: Path? = null) { @Retention(AnnotationRetention.RUNTIME) @Target(AnnotationTarget.CLASS) @ExtendWith(KotlinEnvironmentResolver::class) -annotation class KotlinCoreEnvironmentTest +annotation class KotlinCoreEnvironmentTest( + val additionalJavaSourcePaths: Array = [] +) internal class KotlinEnvironmentResolver : ParameterResolver { private var ExtensionContext.wrapper: CloseableWrapper? @@ -43,13 +47,19 @@ internal class KotlinEnvironmentResolver : ParameterResolver { override fun resolveParameter(parameterContext: ParameterContext, extensionContext: ExtensionContext): Any { val closeableWrapper = extensionContext.wrapper - ?: CloseableWrapper(createEnvironment()).also { extensionContext.wrapper = it } + ?: CloseableWrapper(createEnvironment(additionalJavaSourceRootPaths = extensionContext.additionalJavaSourcePaths())) + .also { extensionContext.wrapper = it } return closeableWrapper.wrapper.env } companion object { private val NAMESPACE = ExtensionContext.Namespace.create("KotlinCoreEnvironment") private const val WRAPPER_KEY = "wrapper" + private fun ExtensionContext.additionalJavaSourcePaths(): List { + val annotation = requiredTestClass.annotations + .find { it is KotlinCoreEnvironmentTest } as? KotlinCoreEnvironmentTest ?: return emptyList() + return annotation.additionalJavaSourcePaths.map { Path.of(resource(it)).toFile() } + } } private class CloseableWrapper(val wrapper: KotlinCoreEnvironmentWrapper) : From 749363ec04f95f048b0e3928cbab558da25ec42b Mon Sep 17 00:00:00 2001 From: Markus Schwarz Date: Mon, 25 Apr 2022 21:00:17 +0200 Subject: [PATCH 2/7] update api --- detekt-test-utils/api/detekt-test-utils.api | 1 + 1 file changed, 1 insertion(+) diff --git a/detekt-test-utils/api/detekt-test-utils.api b/detekt-test-utils/api/detekt-test-utils.api index c57ff6aed14..6e61e268505 100644 --- a/detekt-test-utils/api/detekt-test-utils.api +++ b/detekt-test-utils/api/detekt-test-utils.api @@ -47,6 +47,7 @@ public final class io/github/detekt/test/utils/StringPrintStream : java/io/Print } public abstract interface annotation class io/gitlab/arturbosch/detekt/rules/KotlinCoreEnvironmentTest : java/lang/annotation/Annotation { + public abstract fun additionalJavaSourcePaths ()[Ljava/lang/String; } public final class io/gitlab/arturbosch/detekt/rules/KotlinEnvironmentTestSetupKt { From 900b570e36c18515d57283063f446f69205c04d5 Mon Sep 17 00:00:00 2001 From: Markus Schwarz Date: Mon, 25 Apr 2022 21:15:20 +0200 Subject: [PATCH 3/7] Rename top level nested classes to WithDefaultSources and WithAdditionalJavaSources --- .../style/ExplicitCollectionElementAccessMethodSpec.kt | 6 +++--- .../detekt/rules/style/ObjectLiteralToLambdaSpec.kt | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExplicitCollectionElementAccessMethodSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExplicitCollectionElementAccessMethodSpec.kt index 4f21c034773..92c0cbc5aa5 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExplicitCollectionElementAccessMethodSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExplicitCollectionElementAccessMethodSpec.kt @@ -10,12 +10,12 @@ import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test -class ExplicitCollectionElementAccessMethodSpec() { +class ExplicitCollectionElementAccessMethodSpec { val subject = ExplicitCollectionElementAccessMethod(Config.empty) @Nested @KotlinCoreEnvironmentTest - inner class DefaultSourceTests(val env: KotlinCoreEnvironment) { + inner class WithDefaultSources(val env: KotlinCoreEnvironment) { @Nested inner class `Kotlin map` { @@ -424,7 +424,7 @@ class ExplicitCollectionElementAccessMethodSpec() { @Nested @KotlinCoreEnvironmentTest(additionalJavaSourcePaths = ["java"]) - inner class JavaSourceTests(val env: KotlinCoreEnvironment) { + inner class WithAdditionalJavaSources(val env: KotlinCoreEnvironment) { @Test fun `does not report if the function has 3 or more arguments and it's defined in java - #4288`() { diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ObjectLiteralToLambdaSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ObjectLiteralToLambdaSpec.kt index c2d89179e83..5f2a499fd3d 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ObjectLiteralToLambdaSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ObjectLiteralToLambdaSpec.kt @@ -15,7 +15,7 @@ class ObjectLiteralToLambdaSpec { @Nested @KotlinCoreEnvironmentTest - inner class DefaultSourceTests(val env: KotlinCoreEnvironment) { + inner class WithDefaultSources(val env: KotlinCoreEnvironment) { @Nested inner class `report convertible expression` { @@ -541,7 +541,7 @@ class ObjectLiteralToLambdaSpec { @Nested @KotlinCoreEnvironmentTest(additionalJavaSourcePaths = ["java"]) - inner class JavaSourceTests(val env: KotlinCoreEnvironment) { + inner class WithAdditionalJavaSources(val env: KotlinCoreEnvironment) { @Test fun `has other default methods`() { From 173860770c7aea2ea5b326c81f67f8c23f4bd3ee Mon Sep 17 00:00:00 2001 From: Markus Schwarz Date: Mon, 25 Apr 2022 21:50:43 +0200 Subject: [PATCH 4/7] fix compilation error in java < 11 --- .../arturbosch/detekt/rules/KotlinEnvironmentTestSetup.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/detekt-test-utils/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/KotlinEnvironmentTestSetup.kt b/detekt-test-utils/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/KotlinEnvironmentTestSetup.kt index 08945d77dc7..df0e8eb6824 100644 --- a/detekt-test-utils/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/KotlinEnvironmentTestSetup.kt +++ b/detekt-test-utils/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/KotlinEnvironmentTestSetup.kt @@ -2,7 +2,7 @@ package io.gitlab.arturbosch.detekt.rules import io.github.detekt.test.utils.KotlinCoreEnvironmentWrapper import io.github.detekt.test.utils.createEnvironment -import io.github.detekt.test.utils.resource +import io.github.detekt.test.utils.resourceAsPath import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment import org.junit.jupiter.api.extension.ExtendWith import org.junit.jupiter.api.extension.ExtensionContext @@ -58,7 +58,7 @@ internal class KotlinEnvironmentResolver : ParameterResolver { private fun ExtensionContext.additionalJavaSourcePaths(): List { val annotation = requiredTestClass.annotations .find { it is KotlinCoreEnvironmentTest } as? KotlinCoreEnvironmentTest ?: return emptyList() - return annotation.additionalJavaSourcePaths.map { Path.of(resource(it)).toFile() } + return annotation.additionalJavaSourcePaths.map { resourceAsPath(it).toFile() } } } From 39f38d72ce5a7f547ab7421f774b15fbb7f83e03 Mon Sep 17 00:00:00 2001 From: Markus Schwarz Date: Tue, 26 Apr 2022 07:50:33 +0200 Subject: [PATCH 5/7] fix upstream merge --- ...plicitCollectionElementAccessMethodSpec.kt | 20 +- .../rules/style/ObjectLiteralToLambdaSpec.kt | 429 +++++++++--------- .../rules/KotlinEnvironmentTestSetup.kt | 5 +- 3 files changed, 228 insertions(+), 226 deletions(-) diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExplicitCollectionElementAccessMethodSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExplicitCollectionElementAccessMethodSpec.kt index f449143d8f7..cf9768b9749 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExplicitCollectionElementAccessMethodSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExplicitCollectionElementAccessMethodSpec.kt @@ -426,23 +426,23 @@ class ExplicitCollectionElementAccessMethodSpec { @KotlinCoreEnvironmentTest(additionalJavaSourcePaths = ["java"]) inner class WithAdditionalJavaSources(val env: KotlinCoreEnvironment) { - @Test - fun `reports setter from java with 2 or less parameters`() { - // this test case ensures that the test environment are set up correctly. - val code = """ + @Test + fun `reports setter from java with 2 or less parameters`() { + // this test case ensures that the test environment are set up correctly. + val code = """ import com.example.fromjava.Rect fun foo() { val rect = Rect() rect.set(0, 1) } - """ - assertThat(subject.lintWithContext(env, code)).hasSize(1) - } + """ + assertThat(subject.lintWithContext(env, code)).hasSize(1) + } - @Test - fun `does not report if the function has 3 or more arguments and it's defined in java - #4288`() { - val code = """ + @Test + fun `does not report if the function has 3 or more arguments and it's defined in java - #4288`() { + val code = """ import com.example.fromjava.Rect fun foo() { diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ObjectLiteralToLambdaSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ObjectLiteralToLambdaSpec.kt index e99bf9cb6d8..e603b0df24e 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ObjectLiteralToLambdaSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ObjectLiteralToLambdaSpec.kt @@ -36,9 +36,9 @@ class ObjectLiteralToLambdaSpec { .hasSourceLocations(SourceLocation(4, 9)) } - @Test - fun `is in function`() { - val code = """ + @Test + fun `is in function`() { + val code = """ fun interface Sam { fun foo() } @@ -48,16 +48,16 @@ class ObjectLiteralToLambdaSpec { } } } - """ - subject.compileAndLintWithContext(env, code) - .assert() - .hasSize(1) - .hasSourceLocations(SourceLocation(5, 5)) - } + """ + subject.compileAndLintWithContext(env, code) + .assert() + .hasSize(1) + .hasSourceLocations(SourceLocation(5, 5)) + } - @Test - fun `is in init`() { - val code = """ + @Test + fun `is in init`() { + val code = """ fun interface Sam { fun foo() } @@ -69,16 +69,16 @@ class ObjectLiteralToLambdaSpec { } } } - """ - subject.compileAndLintWithContext(env, code) - .assert() - .hasSize(1) - .hasSourceLocations(SourceLocation(6, 9)) - } + """ + subject.compileAndLintWithContext(env, code) + .assert() + .hasSize(1) + .hasSourceLocations(SourceLocation(6, 9)) + } - @Test - fun `is generic`() { - val code = """ + @Test + fun `is generic`() { + val code = """ fun interface Sam { fun foo(): T } @@ -87,16 +87,16 @@ class ObjectLiteralToLambdaSpec { return 1 } } - """ - subject.compileAndLintWithContext(env, code) - .assert() - .hasSize(1) - .hasSourceLocations(SourceLocation(4, 9)) - } + """ + subject.compileAndLintWithContext(env, code) + .assert() + .hasSize(1) + .hasSourceLocations(SourceLocation(4, 9)) + } - @Test - fun `has other default method`() { - val code = """ + @Test + fun `has other default method`() { + val code = """ fun interface Sam { fun foo() fun bar() {} @@ -105,16 +105,16 @@ class ObjectLiteralToLambdaSpec { override fun foo() { } } - """ - subject.compileAndLintWithContext(env, code) - .assert() - .hasSize(1) - .hasSourceLocations(SourceLocation(5, 9)) - } + """ + subject.compileAndLintWithContext(env, code) + .assert() + .hasSize(1) + .hasSourceLocations(SourceLocation(5, 9)) + } - @Test - fun `nested declaration`() { - val code = """ + @Test + fun `nested declaration`() { + val code = """ interface First { fun foo() } @@ -126,35 +126,35 @@ class ObjectLiteralToLambdaSpec { } } } - """ - subject.compileAndLintWithContext(env, code) - .assert() - .hasSize(1) - .hasSourceLocations(SourceLocation(7, 5)) - } + """ + subject.compileAndLintWithContext(env, code) + .assert() + .hasSize(1) + .hasSourceLocations(SourceLocation(7, 5)) + } - @Test - fun `expression body syntax`() { - val code = """ + @Test + fun `expression body syntax`() { + val code = """ fun interface Sam { fun foo(): Int } val a = object : Sam { override fun foo() = 3 } - """ - subject.compileAndLintWithContext(env, code) - .assert() - .hasSize(1) - .hasSourceLocations(SourceLocation(4, 9)) + """ + subject.compileAndLintWithContext(env, code) + .assert() + .hasSize(1) + .hasSourceLocations(SourceLocation(4, 9)) + } } - } - @Nested - inner class `is not correct implement` { - @Test - fun `without type resolution`() { - val code = """ + @Nested + inner class `is not correct implement` { + @Test + fun `without type resolution`() { + val code = """ fun interface Sam { fun foo() } @@ -162,58 +162,58 @@ class ObjectLiteralToLambdaSpec { override fun foo() { } } - """ - subject.compileAndLint(code).assert().isEmpty() - } + """ + subject.compileAndLint(code).assert().isEmpty() + } - @Test - fun `is empty interface`() { - val code = """ + @Test + fun `is empty interface`() { + val code = """ interface Sam val a = object : Sam {} - """ - subject.compileAndLintWithContext(env, code).assert().isEmpty() - } + """ + subject.compileAndLintWithContext(env, code).assert().isEmpty() + } - @Test - fun `is empty interface and has own function`() { - val code = """ + @Test + fun `is empty interface and has own function`() { + val code = """ interface Sam val a = object : Sam { fun foo() { } } - """ - subject.compileAndLintWithContext(env, code).assert().isEmpty() - } + """ + subject.compileAndLintWithContext(env, code).assert().isEmpty() + } - @Test - fun `is single property interface`() { - val code = """ + @Test + fun `is single property interface`() { + val code = """ interface Sam { val foo: Int } val a = object : Sam { override val foo = 1 } - """ - subject.compileAndLintWithContext(env, code).assert().isEmpty() - } + """ + subject.compileAndLintWithContext(env, code).assert().isEmpty() + } - @Test - fun `is empty interface and has own property`() { - val code = """ + @Test + fun `is empty interface and has own property`() { + val code = """ interface Sam val a = object : Sam { val b = 1 } - """ - subject.compileAndLintWithContext(env, code).assert().isEmpty() - } + """ + subject.compileAndLintWithContext(env, code).assert().isEmpty() + } - @Test - fun `is not fun interface`() { - val code = """ + @Test + fun `is not fun interface`() { + val code = """ interface Sam { fun foo() } @@ -221,13 +221,13 @@ class ObjectLiteralToLambdaSpec { override fun foo() { } } - """ - subject.compileAndLintWithContext(env, code).assert().isEmpty() - } + """ + subject.compileAndLintWithContext(env, code).assert().isEmpty() + } - @Test - fun `is not interface`() { - val code = """ + @Test + fun `is not interface`() { + val code = """ abstract class Something { abstract fun foo() } @@ -235,13 +235,13 @@ class ObjectLiteralToLambdaSpec { override fun foo() { } } - """ - subject.compileAndLintWithContext(env, code).assert().isEmpty() - } + """ + subject.compileAndLintWithContext(env, code).assert().isEmpty() + } - @Test - fun `has multi implement`() { - val code = """ + @Test + fun `has multi implement`() { + val code = """ fun interface First { fun foo() } @@ -251,13 +251,13 @@ class ObjectLiteralToLambdaSpec { override fun foo() { } } - """ - subject.compileAndLintWithContext(env, code).assert().isEmpty() - } + """ + subject.compileAndLintWithContext(env, code).assert().isEmpty() + } - @Test - fun `has complex implement`() { - val code = """ + @Test + fun `has complex implement`() { + val code = """ abstract class First { abstract fun foo() } @@ -269,16 +269,16 @@ class ObjectLiteralToLambdaSpec { override fun foo() { } } - """ - subject.compileAndLintWithContext(env, code).assert().isEmpty() + """ + subject.compileAndLintWithContext(env, code).assert().isEmpty() + } } - } - @Nested - inner class `has impurities` { - @Test - fun `has more than one method`() { - val code = """ + @Nested + inner class `has impurities` { + @Test + fun `has more than one method`() { + val code = """ fun interface Sam { fun foo() } @@ -288,13 +288,13 @@ class ObjectLiteralToLambdaSpec { fun bar() { } } - """ - subject.compileAndLintWithContext(env, code).assert().isEmpty() - } + """ + subject.compileAndLintWithContext(env, code).assert().isEmpty() + } - @Test - fun `has property`() { - val code = """ + @Test + fun `has property`() { + val code = """ fun interface Sam { fun foo() } @@ -303,13 +303,13 @@ class ObjectLiteralToLambdaSpec { override fun foo() { } } - """ - subject.compileAndLintWithContext(env, code).assert().isEmpty() - } + """ + subject.compileAndLintWithContext(env, code).assert().isEmpty() + } - @Test - fun `has init`() { - val code = """ + @Test + fun `has init`() { + val code = """ fun interface Sam { fun foo() } @@ -319,58 +319,58 @@ class ObjectLiteralToLambdaSpec { override fun foo() { } } - """ - subject.compileAndLintWithContext(env, code).assert().isEmpty() + """ + subject.compileAndLintWithContext(env, code).assert().isEmpty() + } } - } - @Nested - inner class `java interface` { - @Test - fun `is convertible`() { - val code = """ + @Nested + inner class `java interface` { + @Test + fun `is convertible`() { + val code = """ val a = object : Runnable { override fun run(){ } } - """ - subject.compileAndLintWithContext(env, code) - .assert() - .hasSize(1) - .hasSourceLocations(SourceLocation(1, 9)) - } + """ + subject.compileAndLintWithContext(env, code) + .assert() + .hasSize(1) + .hasSourceLocations(SourceLocation(1, 9)) + } - @Test - fun `is convertible Callable generic`() { - val code = """ + @Test + fun `is convertible Callable generic`() { + val code = """ import java.util.concurrent.Callable val a = object : Callable { override fun call(): Int { return 1 } } - """ - subject.compileAndLintWithContext(env, code) - .assert() - .hasSize(1) - .hasSourceLocations(SourceLocation(2, 9)) - } + """ + subject.compileAndLintWithContext(env, code) + .assert() + .hasSize(1) + .hasSourceLocations(SourceLocation(2, 9)) + } - @Test - fun `empty interface`() { - val code = """ + @Test + fun `empty interface`() { + val code = """ import java.util.EventListener val a = object : EventListener { fun foo() { } } - """ - subject.compileAndLintWithContext(env, code).assert().isEmpty() - } + """ + subject.compileAndLintWithContext(env, code).assert().isEmpty() + } - @Test - fun `is convertible Enumeration generic`() { - val code = """ + @Test + fun `is convertible Enumeration generic`() { + val code = """ import java.util.Enumeration val a = object : Enumeration { override fun hasMoreElements(): Boolean { @@ -386,11 +386,11 @@ class ObjectLiteralToLambdaSpec { } } - @Nested - inner class `object use itself` { - @Test - fun `call 'this'`() { - val code = """ + @Nested + inner class `object use itself` { + @Test + fun `call 'this'`() { + val code = """ fun interface Sam { fun foo() } @@ -402,13 +402,13 @@ class ObjectLiteralToLambdaSpec { } } } - """ - subject.compileAndLintWithContext(env, code).assert().isEmpty() - } + """ + subject.compileAndLintWithContext(env, code).assert().isEmpty() + } - @Test - fun `use 'this'`() { - val code = """ + @Test + fun `use 'this'`() { + val code = """ fun interface Sam { fun foo() } @@ -422,13 +422,13 @@ class ObjectLiteralToLambdaSpec { } } } - """ - subject.compileAndLintWithContext(env, code).assert().isEmpty() - } + """ + subject.compileAndLintWithContext(env, code).assert().isEmpty() + } - @Test - fun `use class method`() { - val code = """ + @Test + fun `use class method`() { + val code = """ fun interface Sam { fun foo() } @@ -440,13 +440,13 @@ class ObjectLiteralToLambdaSpec { } } } - """ - subject.compileAndLintWithContext(env, code).assert().isEmpty() - } + """ + subject.compileAndLintWithContext(env, code).assert().isEmpty() + } - @Test - fun `call 'this' inside nested object`() { - val code = """ + @Test + fun `call 'this' inside nested object`() { + val code = """ fun interface Sam { fun foo() } @@ -462,16 +462,16 @@ class ObjectLiteralToLambdaSpec { } } } - """ - subject.compileAndLintWithContext(env, code) - .assert() - .hasSize(1) - .hasSourceLocations(SourceLocation(6, 5)) - } + """ + subject.compileAndLintWithContext(env, code) + .assert() + .hasSize(1) + .hasSourceLocations(SourceLocation(6, 5)) + } - @Test - fun `call labeled 'this'`() { - val code = """ + @Test + fun `call labeled 'this'`() { + val code = """ fun interface Sam { fun foo() } @@ -485,16 +485,16 @@ class ObjectLiteralToLambdaSpec { } } } - """ - subject.compileAndLintWithContext(env, code) - .assert() - .hasSize(1) - .hasSourceLocations(SourceLocation(7, 9)) - } + """ + subject.compileAndLintWithContext(env, code) + .assert() + .hasSize(1) + .hasSourceLocations(SourceLocation(7, 9)) + } - @Test - fun `recursive call`() { - val code = """ + @Test + fun `recursive call`() { + val code = """ fun interface Sam { fun foo() } @@ -506,17 +506,17 @@ class ObjectLiteralToLambdaSpec { } } } - """ - subject.compileAndLintWithContext(env, code).assert().isEmpty() + """ + subject.compileAndLintWithContext(env, code).assert().isEmpty() + } } - } - @Nested - inner class `Edge case` { - // https://github.com/detekt/detekt/pull/3599#issuecomment-806389701 - @Test - fun `Anonymous objects are always newly created, but lambdas are singletons, so they have the same reference`() { - val code = """ + @Nested + inner class `Edge case` { + // https://github.com/detekt/detekt/pull/3599#issuecomment-806389701 + @Test + fun `Anonymous objects are always newly created, but lambdas are singletons, so they have the same reference`() { + val code = """ fun interface Sam { fun foo() } @@ -530,11 +530,12 @@ class ObjectLiteralToLambdaSpec { val a = newObject() === newObject() // false val b = lambda() === lambda() // true - """ - subject.compileAndLintWithContext(env, code) - .assert() - .hasSize(1) - .hasSourceLocations(SourceLocation(5, 19)) + """ + subject.compileAndLintWithContext(env, code) + .assert() + .hasSize(1) + .hasSourceLocations(SourceLocation(5, 19)) + } } } diff --git a/detekt-test-utils/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/KotlinEnvironmentTestSetup.kt b/detekt-test-utils/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/KotlinEnvironmentTestSetup.kt index df0e8eb6824..97f59ef5e36 100644 --- a/detekt-test-utils/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/KotlinEnvironmentTestSetup.kt +++ b/detekt-test-utils/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/KotlinEnvironmentTestSetup.kt @@ -47,8 +47,9 @@ internal class KotlinEnvironmentResolver : ParameterResolver { override fun resolveParameter(parameterContext: ParameterContext, extensionContext: ExtensionContext): Any { val closeableWrapper = extensionContext.wrapper - ?: CloseableWrapper(createEnvironment(additionalJavaSourceRootPaths = extensionContext.additionalJavaSourcePaths())) - .also { extensionContext.wrapper = it } + ?: CloseableWrapper( + createEnvironment(additionalJavaSourceRootPaths = extensionContext.additionalJavaSourcePaths()) + ).also { extensionContext.wrapper = it } return closeableWrapper.wrapper.env } From e08b6c3135ce828ad14fca175b71285a6accf0a3 Mon Sep 17 00:00:00 2001 From: Markus Schwarz Date: Wed, 27 Apr 2022 20:16:51 +0200 Subject: [PATCH 6/7] fix: do not attempt to compile snippet with java class from resource --- .../rules/style/ExplicitCollectionElementAccessMethodSpec.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExplicitCollectionElementAccessMethodSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExplicitCollectionElementAccessMethodSpec.kt index cf9768b9749..84d339b76ce 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExplicitCollectionElementAccessMethodSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExplicitCollectionElementAccessMethodSpec.kt @@ -450,7 +450,7 @@ class ExplicitCollectionElementAccessMethodSpec { rect.set(0, 1, 2) } """ - assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() + assertThat(subject.lintWithContext(env, code)).isEmpty() } } } From 86206294955ef36762496afc6410d1ffe4abf488 Mon Sep 17 00:00:00 2001 From: Markus Schwarz Date: Sun, 29 May 2022 22:01:14 +0200 Subject: [PATCH 7/7] manually fix indentation of test snippets --- ...plicitCollectionElementAccessMethodSpec.kt | 68 +-- .../rules/style/ObjectLiteralToLambdaSpec.kt | 520 +++++++++--------- 2 files changed, 294 insertions(+), 294 deletions(-) diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExplicitCollectionElementAccessMethodSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExplicitCollectionElementAccessMethodSpec.kt index 506e0bc3d01..c6d0394e1cb 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExplicitCollectionElementAccessMethodSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ExplicitCollectionElementAccessMethodSpec.kt @@ -156,10 +156,10 @@ class ExplicitCollectionElementAccessMethodSpec { @Test fun `does not report calls on implicit receiver`() { val code = """ - fun f() { - val map = mapOf() - with(map) { get("a") } - } + fun f() { + val map = mapOf() + with(map) { get("a") } + } """ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(0) } @@ -215,10 +215,10 @@ class ExplicitCollectionElementAccessMethodSpec { @Test fun `does not report calls on implicit receiver`() { val code = """ - fun f() { - val list = listOf() - val value = with(list) { get(0) } - } + fun f() { + val list = listOf() + val value = with(list) { get(0) } + } """ assertThat(subject.compileAndLintWithContext(env, code)).hasSize(0) } @@ -394,10 +394,10 @@ class ExplicitCollectionElementAccessMethodSpec { @Test fun `does not crash for getter`() { val code = """ - class A { - val i: Int get() = 1 + 2 - val c: Char? get() = "".first() ?: throw IllegalArgumentException("getter") - } + class A { + val i: Int get() = 1 + 2 + val c: Char? get() = "".first() ?: throw IllegalArgumentException("getter") + } """ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } @@ -405,8 +405,8 @@ class ExplicitCollectionElementAccessMethodSpec { @Test fun `does not crash for fluent api`() { val code = """ - val string = "" - .toString() + val string = "" + .toString() """ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } @@ -414,10 +414,10 @@ class ExplicitCollectionElementAccessMethodSpec { @Test fun `does not report for unresolvable code`() { val code = """ - fun f() { - val unknownType = UnknownType() - val value = unknownType.put("answer", 42) - } + fun f() { + val unknownType = UnknownType() + val value = unknownType.put("answer", 42) + } """ assertThat(subject.lintWithContext(env, code)).isEmpty() } @@ -425,10 +425,10 @@ class ExplicitCollectionElementAccessMethodSpec { @Test fun `does not report for put functions without caller`() { val code = """ - fun put() { } - fun f() { - put() - } + fun put() { } + fun f() { + put() + } """ assertThat(subject.compileAndLintWithContext(env, code)).isEmpty() } @@ -443,12 +443,12 @@ class ExplicitCollectionElementAccessMethodSpec { fun `reports setter from java with 2 or less parameters`() { // this test case ensures that the test environment are set up correctly. val code = """ - import com.example.fromjava.Rect - - fun foo() { - val rect = Rect() - rect.set(0, 1) - } + import com.example.fromjava.Rect + + fun foo() { + val rect = Rect() + rect.set(0, 1) + } """ assertThat(subject.lintWithContext(env, code)).hasSize(1) } @@ -456,12 +456,12 @@ class ExplicitCollectionElementAccessMethodSpec { @Test fun `does not report if the function has 3 or more arguments and it's defined in java - #4288`() { val code = """ - import com.example.fromjava.Rect - - fun foo() { - val rect = Rect() - rect.set(0, 1, 2) - } + import com.example.fromjava.Rect + + fun foo() { + val rect = Rect() + rect.set(0, 1, 2) + } """ assertThat(subject.lintWithContext(env, code)).isEmpty() } diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ObjectLiteralToLambdaSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ObjectLiteralToLambdaSpec.kt index e603b0df24e..7452591d296 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ObjectLiteralToLambdaSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/ObjectLiteralToLambdaSpec.kt @@ -22,13 +22,13 @@ class ObjectLiteralToLambdaSpec { @Test fun `is property`() { val code = """ - fun interface Sam { - fun foo() - } - val a = object : Sam { - override fun foo() { + fun interface Sam { + fun foo() } - } + val a = object : Sam { + override fun foo() { + } + } """ subject.compileAndLintWithContext(env, code) .assert() @@ -39,15 +39,15 @@ class ObjectLiteralToLambdaSpec { @Test fun `is in function`() { val code = """ - fun interface Sam { - fun foo() - } - fun bar() { - object : Sam { - override fun foo() { + fun interface Sam { + fun foo() + } + fun bar() { + object : Sam { + override fun foo() { + } + } } - } - } """ subject.compileAndLintWithContext(env, code) .assert() @@ -58,17 +58,17 @@ class ObjectLiteralToLambdaSpec { @Test fun `is in init`() { val code = """ - fun interface Sam { - fun foo() - } - object B { - init { - object : Sam { - override fun foo() { + fun interface Sam { + fun foo() + } + object B { + init { + object : Sam { + override fun foo() { + } + } } } - } - } """ subject.compileAndLintWithContext(env, code) .assert() @@ -79,14 +79,14 @@ class ObjectLiteralToLambdaSpec { @Test fun `is generic`() { val code = """ - fun interface Sam { - fun foo(): T - } - val a = object : Sam { - override fun foo(): Int { - return 1 - } - } + fun interface Sam { + fun foo(): T + } + val a = object : Sam { + override fun foo(): Int { + return 1 + } + } """ subject.compileAndLintWithContext(env, code) .assert() @@ -97,14 +97,14 @@ class ObjectLiteralToLambdaSpec { @Test fun `has other default method`() { val code = """ - fun interface Sam { - fun foo() - fun bar() {} - } - val a = object : Sam { - override fun foo() { - } - } + fun interface Sam { + fun foo() + fun bar() {} + } + val a = object : Sam { + override fun foo() { + } + } """ subject.compileAndLintWithContext(env, code) .assert() @@ -115,17 +115,17 @@ class ObjectLiteralToLambdaSpec { @Test fun `nested declaration`() { val code = """ - interface First { - fun foo() - } - fun interface Second: First - - fun bar() { - object : Second { - override fun foo(){ + interface First { + fun foo() + } + fun interface Second: First + + fun bar() { + object : Second { + override fun foo(){ + } + } } - } - } """ subject.compileAndLintWithContext(env, code) .assert() @@ -136,12 +136,12 @@ class ObjectLiteralToLambdaSpec { @Test fun `expression body syntax`() { val code = """ - fun interface Sam { - fun foo(): Int - } - val a = object : Sam { - override fun foo() = 3 - } + fun interface Sam { + fun foo(): Int + } + val a = object : Sam { + override fun foo() = 3 + } """ subject.compileAndLintWithContext(env, code) .assert() @@ -155,13 +155,13 @@ class ObjectLiteralToLambdaSpec { @Test fun `without type resolution`() { val code = """ - fun interface Sam { - fun foo() - } - val a = object : Sam { - override fun foo() { - } - } + fun interface Sam { + fun foo() + } + val a = object : Sam { + override fun foo() { + } + } """ subject.compileAndLint(code).assert().isEmpty() } @@ -169,8 +169,8 @@ class ObjectLiteralToLambdaSpec { @Test fun `is empty interface`() { val code = """ - interface Sam - val a = object : Sam {} + interface Sam + val a = object : Sam {} """ subject.compileAndLintWithContext(env, code).assert().isEmpty() } @@ -178,11 +178,11 @@ class ObjectLiteralToLambdaSpec { @Test fun `is empty interface and has own function`() { val code = """ - interface Sam - val a = object : Sam { - fun foo() { - } - } + interface Sam + val a = object : Sam { + fun foo() { + } + } """ subject.compileAndLintWithContext(env, code).assert().isEmpty() } @@ -190,12 +190,12 @@ class ObjectLiteralToLambdaSpec { @Test fun `is single property interface`() { val code = """ - interface Sam { - val foo: Int - } - val a = object : Sam { - override val foo = 1 - } + interface Sam { + val foo: Int + } + val a = object : Sam { + override val foo = 1 + } """ subject.compileAndLintWithContext(env, code).assert().isEmpty() } @@ -203,10 +203,10 @@ class ObjectLiteralToLambdaSpec { @Test fun `is empty interface and has own property`() { val code = """ - interface Sam - val a = object : Sam { - val b = 1 - } + interface Sam + val a = object : Sam { + val b = 1 + } """ subject.compileAndLintWithContext(env, code).assert().isEmpty() } @@ -214,13 +214,13 @@ class ObjectLiteralToLambdaSpec { @Test fun `is not fun interface`() { val code = """ - interface Sam { - fun foo() - } - val a = object : Sam { - override fun foo() { - } - } + interface Sam { + fun foo() + } + val a = object : Sam { + override fun foo() { + } + } """ subject.compileAndLintWithContext(env, code).assert().isEmpty() } @@ -228,13 +228,13 @@ class ObjectLiteralToLambdaSpec { @Test fun `is not interface`() { val code = """ - abstract class Something { - abstract fun foo() - } - val a = object : Something() { - override fun foo() { - } - } + abstract class Something { + abstract fun foo() + } + val a = object : Something() { + override fun foo() { + } + } """ subject.compileAndLintWithContext(env, code).assert().isEmpty() } @@ -242,15 +242,15 @@ class ObjectLiteralToLambdaSpec { @Test fun `has multi implement`() { val code = """ - fun interface First { - fun foo() - } - interface Second - - val a: First = object : First, Second { - override fun foo() { - } - } + fun interface First { + fun foo() + } + interface Second + + val a: First = object : First, Second { + override fun foo() { + } + } """ subject.compileAndLintWithContext(env, code).assert().isEmpty() } @@ -258,17 +258,17 @@ class ObjectLiteralToLambdaSpec { @Test fun `has complex implement`() { val code = """ - abstract class First { - abstract fun foo() - } - fun interface Second { - fun foo() - } - - val a: First = object : First(), Second { - override fun foo() { - } - } + abstract class First { + abstract fun foo() + } + fun interface Second { + fun foo() + } + + val a: First = object : First(), Second { + override fun foo() { + } + } """ subject.compileAndLintWithContext(env, code).assert().isEmpty() } @@ -279,15 +279,15 @@ class ObjectLiteralToLambdaSpec { @Test fun `has more than one method`() { val code = """ - fun interface Sam { - fun foo() - } - val a = object : Sam { - override fun foo() { - } - fun bar() { - } - } + fun interface Sam { + fun foo() + } + val a = object : Sam { + override fun foo() { + } + fun bar() { + } + } """ subject.compileAndLintWithContext(env, code).assert().isEmpty() } @@ -295,14 +295,14 @@ class ObjectLiteralToLambdaSpec { @Test fun `has property`() { val code = """ - fun interface Sam { - fun foo() - } - val a = object : Sam { - private var bar = 0 - override fun foo() { - } - } + fun interface Sam { + fun foo() + } + val a = object : Sam { + private var bar = 0 + override fun foo() { + } + } """ subject.compileAndLintWithContext(env, code).assert().isEmpty() } @@ -310,15 +310,15 @@ class ObjectLiteralToLambdaSpec { @Test fun `has init`() { val code = """ - fun interface Sam { - fun foo() - } - val a = object : Sam { - init { - } - override fun foo() { - } - } + fun interface Sam { + fun foo() + } + val a = object : Sam { + init { + } + override fun foo() { + } + } """ subject.compileAndLintWithContext(env, code).assert().isEmpty() } @@ -329,10 +329,10 @@ class ObjectLiteralToLambdaSpec { @Test fun `is convertible`() { val code = """ - val a = object : Runnable { - override fun run(){ - } - } + val a = object : Runnable { + override fun run(){ + } + } """ subject.compileAndLintWithContext(env, code) .assert() @@ -343,12 +343,12 @@ class ObjectLiteralToLambdaSpec { @Test fun `is convertible Callable generic`() { val code = """ - import java.util.concurrent.Callable - val a = object : Callable { - override fun call(): Int { - return 1 - } - } + import java.util.concurrent.Callable + val a = object : Callable { + override fun call(): Int { + return 1 + } + } """ subject.compileAndLintWithContext(env, code) .assert() @@ -359,11 +359,11 @@ class ObjectLiteralToLambdaSpec { @Test fun `empty interface`() { val code = """ - import java.util.EventListener - val a = object : EventListener { - fun foo() { - } - } + import java.util.EventListener + val a = object : EventListener { + fun foo() { + } + } """ subject.compileAndLintWithContext(env, code).assert().isEmpty() } @@ -371,16 +371,16 @@ class ObjectLiteralToLambdaSpec { @Test fun `is convertible Enumeration generic`() { val code = """ - import java.util.Enumeration - val a = object : Enumeration { - override fun hasMoreElements(): Boolean { - return true - } - - override fun nextElement(): Int { - return 1 + import java.util.Enumeration + val a = object : Enumeration { + override fun hasMoreElements(): Boolean { + return true + } + + override fun nextElement(): Int { + return 1 + } } - } """ subject.compileAndLintWithContext(env, code).assert().isEmpty() } @@ -391,17 +391,17 @@ class ObjectLiteralToLambdaSpec { @Test fun `call 'this'`() { val code = """ - fun interface Sam { - fun foo() - } - - fun aa() { - object : Sam { - override fun foo() { - this + fun interface Sam { + fun foo() + } + + fun aa() { + object : Sam { + override fun foo() { + this + } + } } - } - } """ subject.compileAndLintWithContext(env, code).assert().isEmpty() } @@ -409,19 +409,19 @@ class ObjectLiteralToLambdaSpec { @Test fun `use 'this'`() { val code = """ - fun interface Sam { - fun foo() - } - - fun Sam.bar() {} + fun interface Sam { + fun foo() + } - fun aa() { - object : Sam { - override fun foo() { - bar() + fun Sam.bar() {} + + fun aa() { + object : Sam { + override fun foo() { + bar() + } + } } - } - } """ subject.compileAndLintWithContext(env, code).assert().isEmpty() } @@ -429,17 +429,17 @@ class ObjectLiteralToLambdaSpec { @Test fun `use class method`() { val code = """ - fun interface Sam { - fun foo() - } - - fun aa() { - object : Sam { - override fun foo() { - hashCode() + fun interface Sam { + fun foo() + } + + fun aa() { + object : Sam { + override fun foo() { + hashCode() + } + } } - } - } """ subject.compileAndLintWithContext(env, code).assert().isEmpty() } @@ -447,21 +447,21 @@ class ObjectLiteralToLambdaSpec { @Test fun `call 'this' inside nested object`() { val code = """ - fun interface Sam { - fun foo() - } - - fun aa() { - object : Sam { - override fun foo() { + fun interface Sam { + fun foo() + } + + fun aa() { object : Sam { override fun foo() { - this + object : Sam { + override fun foo() { + this + } + } } } } - } - } """ subject.compileAndLintWithContext(env, code) .assert() @@ -472,19 +472,19 @@ class ObjectLiteralToLambdaSpec { @Test fun `call labeled 'this'`() { val code = """ - fun interface Sam { - fun foo() - } - - class Target { - init { - object : Sam { - override fun foo() { - this@Target + fun interface Sam { + fun foo() + } + + class Target { + init { + object : Sam { + override fun foo() { + this@Target + } + } } } - } - } """ subject.compileAndLintWithContext(env, code) .assert() @@ -495,17 +495,17 @@ class ObjectLiteralToLambdaSpec { @Test fun `recursive call`() { val code = """ - fun interface Sam { - fun foo() - } - - fun a() { - object : Sam { - override fun foo() { - foo() + fun interface Sam { + fun foo() + } + + fun a() { + object : Sam { + override fun foo() { + foo() + } + } } - } - } """ subject.compileAndLintWithContext(env, code).assert().isEmpty() } @@ -517,19 +517,19 @@ class ObjectLiteralToLambdaSpec { @Test fun `Anonymous objects are always newly created, but lambdas are singletons, so they have the same reference`() { val code = """ - fun interface Sam { - fun foo() - } - - fun newObject() = object : Sam { - override fun foo() { - } - } - - fun lambda() = Sam {} - - val a = newObject() === newObject() // false - val b = lambda() === lambda() // true + fun interface Sam { + fun foo() + } + + fun newObject() = object : Sam { + override fun foo() { + } + } + + fun lambda() = Sam {} + + val a = newObject() === newObject() // false + val b = lambda() === lambda() // true """ subject.compileAndLintWithContext(env, code) .assert() @@ -546,15 +546,15 @@ class ObjectLiteralToLambdaSpec { @Test fun `has other default methods`() { val code = """ - import com.example.fromjava.SamWithDefaultMethods - - fun main() { - val x = object : SamWithDefaultMethods { - override fun foo() { - println() - } + import com.example.fromjava.SamWithDefaultMethods + + fun main() { + val x = object : SamWithDefaultMethods { + override fun foo() { + println() } - } + } + } """ subject.lintWithContext(env, code).assert().hasSize(1) @@ -563,12 +563,12 @@ class ObjectLiteralToLambdaSpec { @Test fun `has only default methods`() { val code = """ - import com.example.fromjava.OnlyDefaultMethods - - fun main() { - val x = object : OnlyDefaultMethods { - } - } + import com.example.fromjava.OnlyDefaultMethods + + fun main() { + val x = object : OnlyDefaultMethods { + } + } """ subject.lintWithContext(env, code).assert().isEmpty() } @@ -576,15 +576,15 @@ class ObjectLiteralToLambdaSpec { @Test fun `implements a default method`() { val code = """ - import com.example.fromjava.OnlyDefaultMethods - - fun main() { - val x = object : OnlyDefaultMethods { - override fun foo() { - println() - } + import com.example.fromjava.OnlyDefaultMethods + + fun main() { + val x = object : OnlyDefaultMethods { + override fun foo() { + println() } - } + } + } """ subject.lintWithContext(env, code).assert().isEmpty() }