From 6462745d87bcccbed4d0333c1272e5fb352ed4b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Brais=20Gab=C3=ADn?= Date: Tue, 26 Apr 2022 00:18:23 +0200 Subject: [PATCH] Add more tests for Annotation Suppressor (#4774) * Add more tests for Annotation Suppressor * Update detekt-core/src/test/kotlin/io/gitlab/arturbosch/detekt/core/suppressors/AnnotationSuppressorSpec.kt Co-authored-by: Nicola Corti * Update detekt-core/src/test/kotlin/io/gitlab/arturbosch/detekt/core/suppressors/AnnotationSuppressorSpec.kt Co-authored-by: Nicola Corti Co-authored-by: Nicola Corti --- .../suppressors/AnnotationSuppressorSpec.kt | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/detekt-core/src/test/kotlin/io/gitlab/arturbosch/detekt/core/suppressors/AnnotationSuppressorSpec.kt b/detekt-core/src/test/kotlin/io/gitlab/arturbosch/detekt/core/suppressors/AnnotationSuppressorSpec.kt index 514afd60460..483bbe6c34b 100644 --- a/detekt-core/src/test/kotlin/io/gitlab/arturbosch/detekt/core/suppressors/AnnotationSuppressorSpec.kt +++ b/detekt-core/src/test/kotlin/io/gitlab/arturbosch/detekt/core/suppressors/AnnotationSuppressorSpec.kt @@ -466,4 +466,59 @@ class AnnotationSuppressorSpec(private val env: KotlinCoreEnvironment) { assertThat(suppressor.shouldSuppress(buildFinding(ktFunction))).isTrue() } } + + @Nested + inner class `Annotation with parameters` { + val composableFiles = arrayOf( + compileContentForTest( + """ + package androidx.compose.runtime + + annotation class Composable + """.trimIndent() + ), + compileContentForTest( + """ + package androidx.compose.ui.tooling.preview + + annotation class Preview(showBackground: Boolean = true) + """.trimIndent() + ), + ) + + val root = compileContentForTest( + """ + import androidx.compose.runtime.Composable + import androidx.compose.ui.tooling.preview.Preview + + @Composable + @Preview(showBackground = true) + fun function() = Unit + """.trimIndent() + ) + + @Test + fun `suppress if it has parameters with type solving`() { + val suppressor = annotationSuppressorFactory( + buildConfigAware("ignoreAnnotated" to listOf("Preview")), + env.getContextForPaths(listOf(root, *composableFiles)), + )!! + + val ktFunction = root.findChildByClass(KtFunction::class.java)!! + + assertThat(suppressor.shouldSuppress(buildFinding(ktFunction))).isTrue() + } + + @Test + fun `suppress if it has parameters without type solving`() { + val suppressor = annotationSuppressorFactory( + buildConfigAware("ignoreAnnotated" to listOf("Preview")), + BindingContext.EMPTY, + )!! + + val ktFunction = root.findChildByClass(KtFunction::class.java)!! + + assertThat(suppressor.shouldSuppress(buildFinding(ktFunction))).isTrue() + } + } }