From 3b6634ab59103de600ee70136ec42dcc0769f5e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Brais=20Gab=C3=ADn?= Date: Sat, 27 Apr 2024 07:38:48 +0200 Subject: [PATCH] Improve core tests (#7232) * Don't repeat code * Improve the rule in the tests of Analyzer --- .../arturbosch/detekt/core/AnalyzerSpec.kt | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/detekt-core/src/test/kotlin/io/gitlab/arturbosch/detekt/core/AnalyzerSpec.kt b/detekt-core/src/test/kotlin/io/gitlab/arturbosch/detekt/core/AnalyzerSpec.kt index 21e1474619e..76f5e21124d 100644 --- a/detekt-core/src/test/kotlin/io/gitlab/arturbosch/detekt/core/AnalyzerSpec.kt +++ b/detekt-core/src/test/kotlin/io/gitlab/arturbosch/detekt/core/AnalyzerSpec.kt @@ -16,7 +16,11 @@ import io.gitlab.arturbosch.detekt.test.yamlConfigFromContent import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatThrownBy import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment +import org.jetbrains.kotlin.com.intellij.openapi.util.TextRange +import org.jetbrains.kotlin.com.intellij.psi.PsiElement import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.psi.psiUtil.elementsInRange +import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test import org.junit.jupiter.params.ParameterizedTest @@ -405,31 +409,32 @@ private class NoEmptyFile(config: Config) : Rule(config, "TestDescription") { } } -private class MaxLineLength(config: Config) : Rule(config, "TestDescription") { +private open class MaxLineLength(config: Config) : Rule(config, "TestDescription") { private val lengthThreshold: Int = config.valueOrDefault("maxLineLength", 10) override fun visitKtFile(file: KtFile) { super.visitKtFile(file) + var offset = 0 for (line in file.text.lineSequence()) { if (line.length > lengthThreshold) { - report(CodeSmell(Entity.atPackageOrFirstDecl(file), description)) + val ktElement = file.findFirstMeaningfulKtElementInParents(offset..(offset + line.length)) + report(CodeSmell(Entity.from(ktElement), description)) } + offset += line.length + 1 // \n } } -} -@RequiresTypeResolution -private class RequiresTypeResolutionMaxLineLength(config: Config) : Rule(config, "TestDescription") { - private val lengthThreshold: Int = config.valueOrDefault("maxLineLength", 10) - override fun visitKtFile(file: KtFile) { - super.visitKtFile(file) - for (line in file.text.lineSequence()) { - if (line.length > lengthThreshold) { - report(CodeSmell(Entity.atPackageOrFirstDecl(file), description)) - } - } + private fun KtFile.findFirstMeaningfulKtElementInParents(range: IntRange): PsiElement { + return elementsInRange(TextRange.create(range.first, range.last)) + .asSequence() + .plus(findElementAt(range.last)) + .mapNotNull { it?.getNonStrictParentOfType() } + .first { it.text.isNotBlank() } } } +@RequiresTypeResolution +private class RequiresTypeResolutionMaxLineLength(config: Config) : MaxLineLength(config) + private class FaultyRule(config: Config) : Rule(config, "") { override fun visitKtFile(file: KtFile) { throw object : IllegalStateException("Deliberately triggered error.") {}