From eed733ce98902d133dc5ddd973d04f92a2d0a04b Mon Sep 17 00:00:00 2001 From: "Vitaly V. Pinchuk" Date: Thu, 5 May 2022 06:22:19 +0300 Subject: [PATCH] Improve tests --- .../KDocReferencesNonPublicProperty.kt | 6 +- .../KDocReferencesNonPublicPropertySpec.kt | 184 +++++++++++------- 2 files changed, 114 insertions(+), 76 deletions(-) diff --git a/detekt-rules-documentation/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/documentation/KDocReferencesNonPublicProperty.kt b/detekt-rules-documentation/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/documentation/KDocReferencesNonPublicProperty.kt index 81a3a16ea88..f424afc5cd7 100644 --- a/detekt-rules-documentation/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/documentation/KDocReferencesNonPublicProperty.kt +++ b/detekt-rules-documentation/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/documentation/KDocReferencesNonPublicProperty.kt @@ -71,8 +71,8 @@ class KDocReferencesNonPublicProperty(config: Config = Config.empty) : Rule(conf return true } var classOrObject = containingClassOrObject - while (classOrObject != null && classOrObject is KtObjectDeclaration) { - if (!classOrObject.isPublic && !isProtected()) { + while (classOrObject is KtObjectDeclaration) { + if (!classOrObject.isPublic) { return true } classOrObject = classOrObject.containingClassOrObject @@ -83,7 +83,7 @@ class KDocReferencesNonPublicProperty(config: Config = Config.empty) : Rule(conf private fun KtProperty.isReferencedInherited(comment: String): Boolean { var qualifiedName = nameAsSafeName.asString() var classOrObject = containingClassOrObject - while (classOrObject != null && classOrObject is KtObjectDeclaration) { + while (classOrObject is KtObjectDeclaration) { qualifiedName = "${classOrObject.nameAsSafeName.asString()}.$qualifiedName" classOrObject = classOrObject.containingClassOrObject } diff --git a/detekt-rules-documentation/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/documentation/KDocReferencesNonPublicPropertySpec.kt b/detekt-rules-documentation/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/documentation/KDocReferencesNonPublicPropertySpec.kt index 54616c035f8..f877196a198 100644 --- a/detekt-rules-documentation/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/documentation/KDocReferencesNonPublicPropertySpec.kt +++ b/detekt-rules-documentation/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/documentation/KDocReferencesNonPublicPropertySpec.kt @@ -7,90 +7,128 @@ import org.junit.jupiter.api.Test class KDocReferencesNonPublicPropertySpec { val subject = KDocReferencesNonPublicProperty() - inner class `KDocReferencesNonPublicProperty rule` { - - @Test - fun `reports referenced non-public properties`() { - val code = """ - /** - * Comment - * [prop1] - non-public property - * [prop2] - public property - */ - class Test { - private val prop1 = 0 - val prop2 = 0 - } - """.trimIndent() - assertThat(subject.compileAndLint(code)).hasSize(1) + @Test + fun `reports referenced non-public properties`() { + val code = """ + /** + * Comment + * [prop1] - non-public property + * [prop2] - public property + */ + class Test { + private val nonReferencedProp = 0 + private val prop1 = 0 + val prop2 = 0 } + """.trimIndent() + assertThat(subject.compileAndLint(code)).hasSize(1) + } - @Test - fun `reports referenced non-public properties in private class`() { - val code = """ - /** - * Comment - * [prop1] - non-public property - * [prop2] - public property - */ - private class Test { - private val prop1 = 0 - val prop2 = 0 - } - """.trimIndent() - assertThat(subject.compileAndLint(code)).hasSize(1) + @Test + fun `reports referenced non-public properties in private class`() { + val code = """ + /** + * Comment + * [prop1] - non-public property + * [prop2] - public property + */ + private class Test { + private val nonReferencedProp = 0 + private val prop1 = 0 + val prop2 = 0 } + """.trimIndent() + assertThat(subject.compileAndLint(code)).hasSize(1) + } - @Test - fun `reports referenced non-public properties in nested objects`() { - val code = """ - /** - * Comment - * [prop1] - non-public property - * [A.prop2] - non-public property - * [A.B.prop3] - non-public property - * [A.C.prop4] - non-public property - */ - class Test { - private val prop1 = 0 + @Test + fun `reports referenced non-public properties in nested objects`() { + val code = """ + /** + * Comment + * [prop1] - non-public property + * [A.prop2] - non-public property + * [A.B.prop3] - non-public property + * [A.C.prop4] - non-public property + */ + class Test { + private val prop1 = 0 + + object A { + private val nonReferencedProp = 0 + private val prop2 = 0 - object A { - private val prop2 = 0 - - private object B { - val prop3 = 0 - } - object C { - private val prop4 = 0 - } + private object B { + val prop3 = 0 + } + object C { + private val prop4 = 0 } } - """.trimIndent() - assertThat(subject.compileAndLint(code)).hasSize(4) } + """.trimIndent() + assertThat(subject.compileAndLint(code)).hasSize(4) + } - @Test - fun `does not report referenced public properties in nested objects`() { - val code = """ - /** - * Comment - * [prop1] - public property - * [A.B.prop2] - public property - * [C.prop3] - public property - */ - open class Test { - protected val prop1 = 0 - object A { - object B { - val prop2 = 0 - } - } - object C { - val prop3 = 0 + @Test + fun `does not report properties with no KDoc`() { + val code = """ + class Test { + private val prop1 = 0 + val prop2 = 0 + } + """.trimIndent() + assertThat(subject.compileAndLint(code)).isEmpty() + } + + @Test + fun `does not report properties with empty comments`() { + val code = """ + /** + */ + class Test { + private val prop1 = 0 + val prop2 = 0 + } + """.trimIndent() + assertThat(subject.compileAndLint(code)).isEmpty() + } + + @Test + fun `does not report properties not enclosed in a class`() { + val code = """ + /** + * [prop1] + * [prop2] + */ + private val prop1 = 0 + val prop2 = 0 + """.trimIndent() + assertThat(subject.compileAndLint(code)).isEmpty() + } + + @Test + fun `does not report referenced public properties in nested objects`() { + val code = """ + /** + * Comment + * [prop1] - public property + * [A.B.prop2] - public property + * [C.prop3] - public property + */ + open class Test { + protected val prop1 = 0 + object A { + object B { + val nonReferencedProp = 0 + val prop2 = 0 } } - """.trimIndent() - assertThat(subject.compileAndLint(code)).isEmpty() + object C { + val prop3 = 0 + } } + """.trimIndent() + assertThat(subject.compileAndLint(code)).isEmpty() } }