Migrate detekt-rules-documentation tests to JUnit (#4568)

This commit is contained in:
Matthew Haughton
2022-02-07 19:38:36 +11:00
committed by GitHub
parent 04006ece92
commit efcc0d95de
11 changed files with 401 additions and 232 deletions

View File

@@ -5,6 +5,5 @@ plugins {
dependencies { dependencies {
compileOnly(projects.detektApi) compileOnly(projects.detektApi)
testImplementation(projects.detektTest) testImplementation(projects.detektTest)
testImplementation(libs.bundles.testImplementation) testImplementation(libs.assertj)
testRuntimeOnly(libs.spek.runner)
} }

View File

@@ -11,18 +11,21 @@ import io.gitlab.arturbosch.detekt.test.assertThat
import io.gitlab.arturbosch.detekt.test.lint import io.gitlab.arturbosch.detekt.test.lint
import io.gitlab.arturbosch.detekt.test.yamlConfig import io.gitlab.arturbosch.detekt.test.yamlConfig
import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.BindingContext
import org.spekframework.spek2.Spek import org.junit.jupiter.api.Nested
import org.spekframework.spek2.style.specification.describe import org.junit.jupiter.api.Test
import java.io.PrintStream import java.io.PrintStream
import java.net.URI import java.net.URI
internal class AbsentOrWrongFileLicenseSpec : Spek({ class AbsentOrWrongFileLicenseSpec {
describe("AbsentOrWrongFileLicense rule") { @Nested
inner class `AbsentOrWrongFileLicense rule` {
context("file with correct license header") { @Nested
inner class `file with correct license header` {
it("reports nothing") { @Test
fun `reports nothing`() {
val findings = checkLicence( val findings = checkLicence(
""" """
/* LICENSE */ /* LICENSE */
@@ -34,9 +37,11 @@ internal class AbsentOrWrongFileLicenseSpec : Spek({
} }
} }
context("file with incorrect license header") { @Nested
inner class `file with incorrect license header` {
it("reports missed license header") { @Test
fun `reports missed license header`() {
val findings = checkLicence( val findings = checkLicence(
""" """
/* WRONG LICENSE */ /* WRONG LICENSE */
@@ -48,9 +53,11 @@ internal class AbsentOrWrongFileLicenseSpec : Spek({
} }
} }
context("file with absent license header") { @Nested
inner class `file with absent license header` {
it("reports missed license header") { @Test
fun `reports missed license header`() {
val findings = checkLicence( val findings = checkLicence(
""" """
package cases package cases
@@ -61,9 +68,11 @@ internal class AbsentOrWrongFileLicenseSpec : Spek({
} }
} }
context("file with correct license header using regex matching") { @Nested
inner class `file with correct license header using regex matching` {
it("reports nothing for 2016") { @Test
fun `reports nothing for 2016`() {
val findings = checkLicence( val findings = checkLicence(
""" """
// //
@@ -80,7 +89,8 @@ internal class AbsentOrWrongFileLicenseSpec : Spek({
assertThat(findings).isEmpty() assertThat(findings).isEmpty()
} }
it("reports nothing for 2021") { @Test
fun `reports nothing for 2021`() {
val findings = checkLicence( val findings = checkLicence(
""" """
// //
@@ -98,9 +108,11 @@ internal class AbsentOrWrongFileLicenseSpec : Spek({
} }
} }
context("file with incorrect license header using regex matching") { @Nested
inner class `file with incorrect license header using regex matching` {
it("file with missing license header") { @Test
fun `file with missing license header`() {
val findings = checkLicence( val findings = checkLicence(
""" """
package cases package cases
@@ -111,7 +123,8 @@ internal class AbsentOrWrongFileLicenseSpec : Spek({
assertThat(findings).hasSize(1) assertThat(findings).hasSize(1)
} }
it("file with license header not on the first line") { @Test
fun `file with license header not on the first line`() {
val findings = checkLicence( val findings = checkLicence(
""" """
package cases package cases
@@ -128,7 +141,8 @@ internal class AbsentOrWrongFileLicenseSpec : Spek({
assertThat(findings).hasSize(1) assertThat(findings).hasSize(1)
} }
it("file with incomplete license header") { @Test
fun `file with incomplete license header`() {
val findings = checkLicence( val findings = checkLicence(
""" """
// //
@@ -142,7 +156,8 @@ internal class AbsentOrWrongFileLicenseSpec : Spek({
assertThat(findings).hasSize(1) assertThat(findings).hasSize(1)
} }
it("file with too many empty likes in license header") { @Test
fun `file with too many empty likes in license header`() {
val findings = checkLicence( val findings = checkLicence(
""" """
// //
@@ -160,7 +175,8 @@ internal class AbsentOrWrongFileLicenseSpec : Spek({
assertThat(findings).hasSize(1) assertThat(findings).hasSize(1)
} }
it("file with incorrect year in license header") { @Test
fun `file with incorrect year in license header`() {
val findings = checkLicence( val findings = checkLicence(
""" """
// //
@@ -178,7 +194,7 @@ internal class AbsentOrWrongFileLicenseSpec : Spek({
} }
} }
} }
}) }
@OptIn(UnstableApi::class) @OptIn(UnstableApi::class)
private fun checkLicence(content: String, isRegexLicense: Boolean = false): List<Finding> { private fun checkLicence(content: String, isRegexLicense: Boolean = false): List<Finding> {

View File

@@ -2,15 +2,17 @@ package io.gitlab.arturbosch.detekt.rules.documentation
import io.gitlab.arturbosch.detekt.test.compileAndLint import io.gitlab.arturbosch.detekt.test.compileAndLint
import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThat
import org.spekframework.spek2.Spek import org.junit.jupiter.api.Nested
import org.spekframework.spek2.style.specification.describe import org.junit.jupiter.api.Test
class CommentOverPrivateMethodSpec : Spek({ class CommentOverPrivateMethodSpec {
val subject by memoized { CommentOverPrivateFunction() } val subject = CommentOverPrivateFunction()
describe("CommentOverPrivateFunction rule") { @Nested
inner class `CommentOverPrivateFunction rule` {
it("reports private method with a comment") { @Test
fun `reports private method with a comment`() {
val code = """ val code = """
class Test { class Test {
/** /**
@@ -21,7 +23,8 @@ class CommentOverPrivateMethodSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
it("does not report public method with a comment") { @Test
fun `does not report public method with a comment`() {
val code = """ val code = """
/** /**
* asdf * asdf
@@ -30,7 +33,8 @@ class CommentOverPrivateMethodSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("does not report public method in a class with a comment") { @Test
fun `does not report public method in a class with a comment`() {
val code = """ val code = """
class Test { class Test {
/** /**
@@ -41,4 +45,4 @@ class CommentOverPrivateMethodSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
} }
}) }

View File

@@ -2,15 +2,17 @@ package io.gitlab.arturbosch.detekt.rules.documentation
import io.gitlab.arturbosch.detekt.test.compileAndLint import io.gitlab.arturbosch.detekt.test.compileAndLint
import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThat
import org.spekframework.spek2.Spek import org.junit.jupiter.api.Nested
import org.spekframework.spek2.style.specification.describe import org.junit.jupiter.api.Test
class CommentOverPrivatePropertiesSpec : Spek({ class CommentOverPrivatePropertiesSpec {
val subject by memoized { CommentOverPrivateProperty() } val subject = CommentOverPrivateProperty()
describe("CommentOverPrivateProperty rule") { @Nested
inner class `CommentOverPrivateProperty rule` {
it("reports private property with a comment") { @Test
fun `reports private property with a comment`() {
val code = """ val code = """
/** /**
* asdf * asdf
@@ -19,7 +21,8 @@ class CommentOverPrivatePropertiesSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
it("does not report public property with a comment") { @Test
fun `does not report public property with a comment`() {
val code = """ val code = """
/** /**
* asdf * asdf
@@ -28,7 +31,8 @@ class CommentOverPrivatePropertiesSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("reports private property in class with a comment") { @Test
fun `reports private property in class with a comment`() {
val code = """ val code = """
class Test { class Test {
/** /**
@@ -39,7 +43,8 @@ class CommentOverPrivatePropertiesSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
it("does not report public property with a comment") { @Test
fun `does not report public property in class with a comment`() {
val code = """ val code = """
class Test { class Test {
/** /**
@@ -50,4 +55,4 @@ class CommentOverPrivatePropertiesSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
} }
}) }

View File

@@ -2,13 +2,16 @@ package io.gitlab.arturbosch.detekt.rules.documentation
import io.gitlab.arturbosch.detekt.test.assertThat import io.gitlab.arturbosch.detekt.test.assertThat
import io.gitlab.arturbosch.detekt.test.compileAndLint import io.gitlab.arturbosch.detekt.test.compileAndLint
import org.spekframework.spek2.Spek import org.junit.jupiter.api.Nested
import org.spekframework.spek2.style.specification.describe import org.junit.jupiter.api.Test
class DeprecatedBlockTagSpec : Spek({ class DeprecatedBlockTagSpec {
val subject by memoized { DeprecatedBlockTag() } val subject = DeprecatedBlockTag()
describe("DeprecatedBlockTag rule") {
it("does not report regular kdoc block") { @Nested
inner class `DeprecatedBlockTag rule` {
@Test
fun `does not report regular kdoc block`() {
val code = """ val code = """
/** /**
* This is just a regular kdoc block. * This is just a regular kdoc block.
@@ -20,7 +23,8 @@ class DeprecatedBlockTagSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(0) assertThat(subject.compileAndLint(code)).hasSize(0)
} }
describe("reporting deprecation tag on kdoc block") { @Nested
inner class `reporting deprecation tag on kdoc block` {
val code = """ val code = """
/** /**
* I am a KDoc block * I am a KDoc block
@@ -30,11 +34,13 @@ class DeprecatedBlockTagSpec : Spek({
fun ohNo() { } fun ohNo() { }
""" """
it("has found something") { @Test
fun `has found something`() {
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
it("correct message") { @Test
fun `correct message`() {
assertThat(subject.compileAndLint(code)[0]).hasMessage( assertThat(subject.compileAndLint(code)[0]).hasMessage(
"@deprecated tag block does not properly report " + "@deprecated tag block does not properly report " +
"deprecation in Kotlin, use @Deprecated annotation instead" "deprecation in Kotlin, use @Deprecated annotation instead"
@@ -42,9 +48,11 @@ class DeprecatedBlockTagSpec : Spek({
} }
} }
describe("reporting deprecation tag wherever @Deprecated is available") { @Nested
inner class `reporting deprecation tag wherever @Deprecated is available` {
it("report deprecation tag on class") { @Test
fun `report deprecation tag on class`() {
val code = """ val code = """
/** /**
* Hello there * Hello there
@@ -56,7 +64,8 @@ class DeprecatedBlockTagSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
it("report deprecation tag on property") { @Test
fun `report deprecation tag on property`() {
val code = """ val code = """
class Thing { class Thing {
/** /**
@@ -70,7 +79,8 @@ class DeprecatedBlockTagSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
it("report deprecation tag on annotation class") { @Test
fun `report deprecation tag on annotation class`() {
val code = """ val code = """
/** /**
* An annotation you should not use * An annotation you should not use
@@ -82,7 +92,8 @@ class DeprecatedBlockTagSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
it("report deprecation tag on constructor") { @Test
fun `report deprecation tag on constructor`() {
val code = """ val code = """
class Thing { class Thing {
/** /**
@@ -96,7 +107,8 @@ class DeprecatedBlockTagSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
it("report deprecation tag on property setter") { @Test
fun `report deprecation tag on property setter`() {
val code = """ val code = """
class Thing { class Thing {
var someProperty: Int var someProperty: Int
@@ -112,7 +124,8 @@ class DeprecatedBlockTagSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
it("report deprecation tag on property getter") { @Test
fun `report deprecation tag on property getter`() {
val code = """ val code = """
class Thing { class Thing {
var someProperty: Int var someProperty: Int
@@ -128,7 +141,8 @@ class DeprecatedBlockTagSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
it("report deprecation tag on typealias") { @Test
fun `report deprecation tag on typealias`() {
val code = """ val code = """
/** /**
* This alias is pointless, do not use it * This alias is pointless, do not use it
@@ -141,4 +155,4 @@ class DeprecatedBlockTagSpec : Spek({
} }
} }
} }
}) }

View File

@@ -2,15 +2,17 @@ package io.gitlab.arturbosch.detekt.rules.documentation
import io.gitlab.arturbosch.detekt.test.compileAndLint import io.gitlab.arturbosch.detekt.test.compileAndLint
import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThat
import org.spekframework.spek2.Spek import org.junit.jupiter.api.Nested
import org.spekframework.spek2.style.specification.describe import org.junit.jupiter.api.Test
class EndOfSentenceFormatSpec : Spek({ class EndOfSentenceFormatSpec {
val subject by memoized { EndOfSentenceFormat() } val subject = EndOfSentenceFormat()
describe("KDocStyle rule") { @Nested
inner class `KDocStyle rule` {
it("reports invalid KDoc endings on classes") { @Test
fun `reports invalid KDoc endings on classes`() {
val code = """ val code = """
/** Some doc */ /** Some doc */
class Test { class Test {
@@ -19,7 +21,8 @@ class EndOfSentenceFormatSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
it("reports invalid KDoc endings on function with expression body") { @Test
fun `reports invalid KDoc endings on function with expression body`() {
val code = """ val code = """
/** Some doc */ /** Some doc */
fun f(x: Int, y: Int, z: Int) = fun f(x: Int, y: Int, z: Int) =
@@ -28,7 +31,8 @@ class EndOfSentenceFormatSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
it("reports invalid KDoc endings on properties") { @Test
fun `reports invalid KDoc endings on properties`() {
val code = """ val code = """
class Test { class Test {
/** Some doc */ /** Some doc */
@@ -38,7 +42,8 @@ class EndOfSentenceFormatSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
it("reports invalid KDoc endings on top-level functions") { @Test
fun `reports invalid KDoc endings on top-level functions`() {
val code = """ val code = """
/** Some doc */ /** Some doc */
fun test() = 3 fun test() = 3
@@ -46,7 +51,8 @@ class EndOfSentenceFormatSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
it("reports invalid KDoc endings on functions") { @Test
fun `reports invalid KDoc endings on functions`() {
val code = """ val code = """
class Test { class Test {
/** Some doc */ /** Some doc */
@@ -56,7 +62,8 @@ class EndOfSentenceFormatSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
it("reports invalid KDoc endings") { @Test
fun `reports invalid KDoc endings`() {
val code = """ val code = """
class Test { class Test {
/** Some doc-- */ /** Some doc-- */
@@ -66,7 +73,8 @@ class EndOfSentenceFormatSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
it("reports invalid KDoc endings in block") { @Test
fun `reports invalid KDoc endings in block`() {
val code = """ val code = """
/** /**
* Something off abc@@ * Something off abc@@
@@ -77,7 +85,8 @@ class EndOfSentenceFormatSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
it("does not validate first sentence KDoc endings in a multi sentence comment") { @Test
fun `does not validate first sentence KDoc endings in a multi sentence comment`() {
val code = """ val code = """
/** /**
* This sentence is correct. * This sentence is correct.
@@ -90,7 +99,8 @@ class EndOfSentenceFormatSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("does not report KDoc which doesn't contain any real sentence") { @Test
fun `does not report KDoc which doesn't contain any real sentence`() {
val code = """ val code = """
/** /**
*/ */
@@ -100,7 +110,8 @@ class EndOfSentenceFormatSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("does not report KDoc which doesn't contain any real sentence but many tags") { @Test
fun `does not report KDoc which doesn't contain any real sentence but many tags`() {
val code = """ val code = """
/** /**
* @configuration this - just an example (default: `150`) * @configuration this - just an example (default: `150`)
@@ -113,7 +124,8 @@ class EndOfSentenceFormatSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("does not report KDoc which doesn't contain any real sentence but html tags") { @Test
fun `does not report KDoc which doesn't contain any real sentence but html tags`() {
val code = """ val code = """
/** /**
* *
@@ -132,7 +144,8 @@ class EndOfSentenceFormatSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("does not report KDoc ending with periods") { @Test
fun `does not report KDoc ending with periods`() {
val code = """ val code = """
/** /**
* Something correct. * Something correct.
@@ -143,7 +156,8 @@ class EndOfSentenceFormatSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("does not report KDoc ending with questionmarks") { @Test
fun `does not report KDoc ending with questionmarks`() {
val code = """ val code = """
/** /**
* Something correct? * Something correct?
@@ -154,7 +168,8 @@ class EndOfSentenceFormatSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("does not report KDoc ending with exclamation marks") { @Test
fun `does not report KDoc ending with exclamation marks`() {
val code = """ val code = """
/** /**
* Something correct! * Something correct!
@@ -165,7 +180,8 @@ class EndOfSentenceFormatSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("does not report KDoc ending with colon") { @Test
fun `does not report KDoc ending with colon`() {
val code = """ val code = """
/** /**
* Something correct: * Something correct:
@@ -176,7 +192,8 @@ class EndOfSentenceFormatSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("does not report URLs in comments") { @Test
fun `does not report URLs in comments`() {
val code = """ val code = """
/** http://www.google.com */ /** http://www.google.com */
class Test1 { class Test1 {
@@ -190,4 +207,4 @@ class EndOfSentenceFormatSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
} }
}) }

View File

@@ -2,15 +2,17 @@ package io.gitlab.arturbosch.detekt.rules.documentation
import io.gitlab.arturbosch.detekt.test.compileAndLint import io.gitlab.arturbosch.detekt.test.compileAndLint
import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThat
import org.spekframework.spek2.Spek import org.junit.jupiter.api.Nested
import org.spekframework.spek2.style.specification.describe import org.junit.jupiter.api.Test
class KDocStyleSpec : Spek({ class KDocStyleSpec {
val subject by memoized { KDocStyle() } val subject = KDocStyle()
describe("check referenced multi rule to only lint errors once per case") { @Nested
inner class `check referenced multi rule to only lint errors once per case` {
it("does only lint once") { @Test
fun `does only lint once`() {
val code = """ val code = """
/** Some doc */ /** Some doc */
class Test { class Test {
@@ -19,4 +21,4 @@ class KDocStyleSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
} }
}) }

View File

@@ -3,23 +3,27 @@ package io.gitlab.arturbosch.detekt.rules.documentation
import io.gitlab.arturbosch.detekt.test.TestConfig import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.compileAndLint import io.gitlab.arturbosch.detekt.test.compileAndLint
import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThat
import org.spekframework.spek2.Spek import org.junit.jupiter.api.Nested
import org.spekframework.spek2.style.specification.describe import org.junit.jupiter.api.Test
class OutdatedDocumentationSpec : Spek({ class OutdatedDocumentationSpec {
val subject by memoized { OutdatedDocumentation() } val subject = OutdatedDocumentation()
describe("OutdatedDocumentation rule") { @Nested
inner class `OutdatedDocumentation rule` {
describe("general") { @Nested
it("should not report when doc is missing") { inner class `general` {
@Test
fun `should not report when doc is missing`() {
val withoutDoc = """ val withoutDoc = """
class MyClass(someParam: String, val someProp: String) class MyClass(someParam: String, val someProp: String)
""" """
assertThat(subject.compileAndLint(withoutDoc)).isEmpty() assertThat(subject.compileAndLint(withoutDoc)).isEmpty()
} }
it("should not report when doc does not contain any property or param tags") { @Test
fun `should not report when doc does not contain any property or param tags`() {
val docWithoutParamAndPropertyTags = """ val docWithoutParamAndPropertyTags = """
/** /**
* Some class description without referring to tags or properties * Some class description without referring to tags or properties
@@ -30,8 +34,10 @@ class OutdatedDocumentationSpec : Spek({
} }
} }
describe("class") { @Nested
it("should not report when doc match class params") { inner class `class` {
@Test
fun `should not report when doc match class params`() {
val correctParam = """ val correctParam = """
/** /**
* @param someParam Description of param * @param someParam Description of param
@@ -41,7 +47,8 @@ class OutdatedDocumentationSpec : Spek({
assertThat(subject.compileAndLint(correctParam)).isEmpty() assertThat(subject.compileAndLint(correctParam)).isEmpty()
} }
it("should report when doc mismatch class param name") { @Test
fun `should report when doc mismatch class param name`() {
val incorrectParamName = """ val incorrectParamName = """
/** /**
* @param someParam Description of param * @param someParam Description of param
@@ -51,7 +58,8 @@ class OutdatedDocumentationSpec : Spek({
assertThat(subject.compileAndLint(incorrectParamName)).hasSize(1) assertThat(subject.compileAndLint(incorrectParamName)).hasSize(1)
} }
it("should report when doc mismatch class param list") { @Test
fun `should report when doc mismatch class param list`() {
val incorrectListOfParams = """ val incorrectListOfParams = """
/** /**
* @param someParam Description of param * @param someParam Description of param
@@ -62,7 +70,8 @@ class OutdatedDocumentationSpec : Spek({
assertThat(subject.compileAndLint(incorrectListOfParams)).hasSize(1) assertThat(subject.compileAndLint(incorrectListOfParams)).hasSize(1)
} }
it("should report when doc mismatch class param list order") { @Test
fun `should report when doc mismatch class param list order`() {
val incorrectParamOrder = """ val incorrectParamOrder = """
/** /**
* @param someParam Description of param * @param someParam Description of param
@@ -73,7 +82,8 @@ class OutdatedDocumentationSpec : Spek({
assertThat(subject.compileAndLint(incorrectParamOrder)).hasSize(1) assertThat(subject.compileAndLint(incorrectParamOrder)).hasSize(1)
} }
it("should not report when doc match class params and props") { @Test
fun `should not report when doc match class params and props`() {
val correctParamAndProp = """ val correctParamAndProp = """
/** /**
* @param someParam Description of param * @param someParam Description of param
@@ -84,7 +94,8 @@ class OutdatedDocumentationSpec : Spek({
assertThat(subject.compileAndLint(correctParamAndProp)).isEmpty() assertThat(subject.compileAndLint(correctParamAndProp)).isEmpty()
} }
it("should report when doc match class params but mismatch props") { @Test
fun `should report when doc match class params but mismatch props`() {
val correctParamIncorrectProp = """ val correctParamIncorrectProp = """
/** /**
* @param someParam Description of param * @param someParam Description of param
@@ -95,7 +106,8 @@ class OutdatedDocumentationSpec : Spek({
assertThat(subject.compileAndLint(correctParamIncorrectProp)).hasSize(1) assertThat(subject.compileAndLint(correctParamIncorrectProp)).hasSize(1)
} }
it("should report when doc mismatch class params and match props") { @Test
fun `should report when doc mismatch class params and match props`() {
val incorrectParamCorrectProp = """ val incorrectParamCorrectProp = """
/** /**
* @param someParam Description of param * @param someParam Description of param
@@ -106,7 +118,8 @@ class OutdatedDocumentationSpec : Spek({
assertThat(subject.compileAndLint(incorrectParamCorrectProp)).hasSize(1) assertThat(subject.compileAndLint(incorrectParamCorrectProp)).hasSize(1)
} }
it("should report when doc for constructor is incorrect") { @Test
fun `should report when doc for constructor is incorrect`() {
val incorrectConstructorDoc = """ val incorrectConstructorDoc = """
class MyClass { class MyClass {
/** /**
@@ -118,7 +131,8 @@ class OutdatedDocumentationSpec : Spek({
assertThat(subject.compileAndLint(incorrectConstructorDoc)).hasSize(1) assertThat(subject.compileAndLint(incorrectConstructorDoc)).hasSize(1)
} }
it("should report when property is documented as param") { @Test
fun `should report when property is documented as param`() {
val propertyAsParam = """ val propertyAsParam = """
/** /**
* @property someParam Description of param * @property someParam Description of param
@@ -129,7 +143,8 @@ class OutdatedDocumentationSpec : Spek({
assertThat(subject.compileAndLint(propertyAsParam)).hasSize(1) assertThat(subject.compileAndLint(propertyAsParam)).hasSize(1)
} }
it("should report when declarations order is incorrect") { @Test
fun `should report when declarations order is incorrect`() {
val incorrectDeclarationsOrder = """ val incorrectDeclarationsOrder = """
/** /**
* @property someProp Description of property * @property someProp Description of property
@@ -141,9 +156,11 @@ class OutdatedDocumentationSpec : Spek({
} }
} }
describe("class with type params") { @Nested
inner class `class with type params` {
it("should not report when doc match class params") { @Test
fun `should not report when doc match class params`() {
val correctTypeParam = """ val correctTypeParam = """
/** /**
* @param T Description of type param * @param T Description of type param
@@ -154,7 +171,8 @@ class OutdatedDocumentationSpec : Spek({
assertThat(subject.compileAndLint(correctTypeParam)).isEmpty() assertThat(subject.compileAndLint(correctTypeParam)).isEmpty()
} }
it("should report when doc misses type param") { @Test
fun `should report when doc misses type param`() {
val missingTypeParam = """ val missingTypeParam = """
/** /**
* @param someParam Description of param * @param someParam Description of param
@@ -164,7 +182,8 @@ class OutdatedDocumentationSpec : Spek({
assertThat(subject.compileAndLint(missingTypeParam)).hasSize(1) assertThat(subject.compileAndLint(missingTypeParam)).hasSize(1)
} }
it("should report when doc mismatch type param name") { @Test
fun `should report when doc mismatch type param name`() {
val incorrectTypeParamName = """ val incorrectTypeParamName = """
/** /**
* @param S Description of type param * @param S Description of type param
@@ -175,7 +194,8 @@ class OutdatedDocumentationSpec : Spek({
assertThat(subject.compileAndLint(incorrectTypeParamName)).hasSize(1) assertThat(subject.compileAndLint(incorrectTypeParamName)).hasSize(1)
} }
it("should report when doc mismatch type param list") { @Test
fun `should report when doc mismatch type param list`() {
val incorrectTypeParamList = """ val incorrectTypeParamList = """
/** /**
* @param T Description of type param * @param T Description of type param
@@ -187,9 +207,11 @@ class OutdatedDocumentationSpec : Spek({
} }
} }
describe("function") { @Nested
inner class `function` {
it("should not report when doc match function params") { @Test
fun `should not report when doc match function params`() {
val correctDoc = """ val correctDoc = """
/** /**
* @param someParam Description of param * @param someParam Description of param
@@ -199,7 +221,8 @@ class OutdatedDocumentationSpec : Spek({
assertThat(subject.compileAndLint(correctDoc)).isEmpty() assertThat(subject.compileAndLint(correctDoc)).isEmpty()
} }
it("should report when doc mismatch function param name") { @Test
fun `should report when doc mismatch function param name`() {
val incorrectParamName = """ val incorrectParamName = """
/** /**
* @param someParam Description of param * @param someParam Description of param
@@ -210,9 +233,11 @@ class OutdatedDocumentationSpec : Spek({
} }
} }
describe("function with type params") { @Nested
inner class `function with type params` {
it("should not report when doc match function params") { @Test
fun `should not report when doc match function params`() {
val correctTypeParam = """ val correctTypeParam = """
/** /**
* @param T Description of type param * @param T Description of type param
@@ -223,7 +248,8 @@ class OutdatedDocumentationSpec : Spek({
assertThat(subject.compileAndLint(correctTypeParam)).isEmpty() assertThat(subject.compileAndLint(correctTypeParam)).isEmpty()
} }
it("should report when doc misses type param") { @Test
fun `should report when doc misses type param`() {
val missingTypeParam = """ val missingTypeParam = """
/** /**
* @param someParam Description of param * @param someParam Description of param
@@ -233,7 +259,8 @@ class OutdatedDocumentationSpec : Spek({
assertThat(subject.compileAndLint(missingTypeParam)).hasSize(1) assertThat(subject.compileAndLint(missingTypeParam)).hasSize(1)
} }
it("should report when doc mismatch type param name") { @Test
fun `should report when doc mismatch type param name`() {
val incorrectTypeParamName = """ val incorrectTypeParamName = """
/** /**
* @param S Description of type param * @param S Description of type param
@@ -244,7 +271,8 @@ class OutdatedDocumentationSpec : Spek({
assertThat(subject.compileAndLint(incorrectTypeParamName)).hasSize(1) assertThat(subject.compileAndLint(incorrectTypeParamName)).hasSize(1)
} }
it("should report when doc mismatch type param list") { @Test
fun `should report when doc mismatch type param list`() {
val incorrectTypeParamList = """ val incorrectTypeParamList = """
/** /**
* @param T Description of type param * @param T Description of type param
@@ -255,7 +283,8 @@ class OutdatedDocumentationSpec : Spek({
assertThat(subject.compileAndLint(incorrectTypeParamList)).hasSize(1) assertThat(subject.compileAndLint(incorrectTypeParamList)).hasSize(1)
} }
it("should report when not all type params are first declarations of doc") { @Test
fun `should report when not all type params are first declarations of doc`() {
val incorrectTypeParamsOrder = """ val incorrectTypeParamsOrder = """
/** /**
* @param T Description of type param * @param T Description of type param
@@ -268,9 +297,11 @@ class OutdatedDocumentationSpec : Spek({
} }
} }
describe("advanced scenarios") { @Nested
inner class `advanced scenarios` {
it("should not report when doc match all signatures") { @Test
fun `should not report when doc match all signatures`() {
val correctClassWithFunction = """ val correctClassWithFunction = """
/** /**
* @param someParam Description of param * @param someParam Description of param
@@ -285,7 +316,8 @@ class OutdatedDocumentationSpec : Spek({
assertThat(subject.compileAndLint(correctClassWithFunction)).isEmpty() assertThat(subject.compileAndLint(correctClassWithFunction)).isEmpty()
} }
it("should report for every class and function with incorrect doc") { @Test
fun `should report for every class and function with incorrect doc`() {
val incorrectClassWithTwoIncorrectFunctions = """ val incorrectClassWithTwoIncorrectFunctions = """
/** /**
* @param someParam Description of param * @param someParam Description of param
@@ -309,12 +341,13 @@ class OutdatedDocumentationSpec : Spek({
} }
} }
describe("configuration matchTypeParameters") { @Nested
val configuredSubject by memoized { inner class `configuration matchTypeParameters` {
val configuredSubject =
OutdatedDocumentation(TestConfig(mapOf("matchTypeParameters" to "false"))) OutdatedDocumentation(TestConfig(mapOf("matchTypeParameters" to "false")))
}
it("should not report when class type parameters mismatch and configuration is off") { @Test
fun `should not report when class type parameters mismatch and configuration is off`() {
val incorrectClassTypeParams = """ val incorrectClassTypeParams = """
/** /**
* @param someParam Description of param * @param someParam Description of param
@@ -324,7 +357,8 @@ class OutdatedDocumentationSpec : Spek({
assertThat(configuredSubject.compileAndLint(incorrectClassTypeParams)).isEmpty() assertThat(configuredSubject.compileAndLint(incorrectClassTypeParams)).isEmpty()
} }
it("should not report when function type parameters mismatch and configuration is off") { @Test
fun `should not report when function type parameters mismatch and configuration is off`() {
val incorrectFunctionTypeParams = """ val incorrectFunctionTypeParams = """
/** /**
* @param someParam Description of param * @param someParam Description of param
@@ -335,12 +369,13 @@ class OutdatedDocumentationSpec : Spek({
} }
} }
describe("configuration matchDeclarationsOrder") { @Nested
val configuredSubject by memoized { inner class `configuration matchDeclarationsOrder` {
val configuredSubject =
OutdatedDocumentation(TestConfig(mapOf("matchDeclarationsOrder" to "false"))) OutdatedDocumentation(TestConfig(mapOf("matchDeclarationsOrder" to "false")))
}
it("should not report when declarations order mismatch and configuration is off") { @Test
fun `should not report when declarations order mismatch and configuration is off`() {
val incorrectDeclarationsOrder = """ val incorrectDeclarationsOrder = """
/** /**
* @param someParam Description of param * @param someParam Description of param
@@ -351,7 +386,8 @@ class OutdatedDocumentationSpec : Spek({
assertThat(configuredSubject.compileAndLint(incorrectDeclarationsOrder)).isEmpty() assertThat(configuredSubject.compileAndLint(incorrectDeclarationsOrder)).isEmpty()
} }
it("should not report when declarations with types order mismatch and configuration is off") { @Test
fun `should not report when declarations with types order mismatch and configuration is off`() {
val incorrectDeclarationsOrderWithType = """ val incorrectDeclarationsOrderWithType = """
/** /**
* @param S Description of type param * @param S Description of type param
@@ -364,12 +400,14 @@ class OutdatedDocumentationSpec : Spek({
assertThat(configuredSubject.compileAndLint(incorrectDeclarationsOrderWithType)).isEmpty() assertThat(configuredSubject.compileAndLint(incorrectDeclarationsOrderWithType)).isEmpty()
} }
} }
describe("configuration allowParamOnConstructorProperties") {
val configuredSubject by memoized {
OutdatedDocumentation(TestConfig(mapOf("allowParamOnConstructorProperties" to "true")))
}
it("should not report when property is documented as param") { @Nested
inner class `configuration allowParamOnConstructorProperties` {
val configuredSubject =
OutdatedDocumentation(TestConfig(mapOf("allowParamOnConstructorProperties" to "true")))
@Test
fun `should not report when property is documented as param`() {
val propertyAsParam = """ val propertyAsParam = """
/** /**
* @param someParam Description of param * @param someParam Description of param
@@ -380,7 +418,8 @@ class OutdatedDocumentationSpec : Spek({
assertThat(configuredSubject.compileAndLint(propertyAsParam)).isEmpty() assertThat(configuredSubject.compileAndLint(propertyAsParam)).isEmpty()
} }
it("should not report when property is documented as property") { @Test
fun `should not report when property is documented as property`() {
val propertyAsParam = """ val propertyAsParam = """
/** /**
* @param someParam Description of param * @param someParam Description of param
@@ -392,8 +431,9 @@ class OutdatedDocumentationSpec : Spek({
} }
} }
describe("configuration matchDeclarationsOrder and allowParamOnConstructorProperties") { @Nested
val configuredSubject by memoized { inner class `configuration matchDeclarationsOrder and allowParamOnConstructorProperties` {
val configuredSubject =
OutdatedDocumentation( OutdatedDocumentation(
TestConfig( TestConfig(
mapOf( mapOf(
@@ -402,9 +442,9 @@ class OutdatedDocumentationSpec : Spek({
) )
) )
) )
}
it("should not report when property is documented as param") { @Test
fun `should not report when property is documented as param`() {
val propertyAsParam = """ val propertyAsParam = """
/** /**
* @param someParam Description of param * @param someParam Description of param
@@ -415,7 +455,8 @@ class OutdatedDocumentationSpec : Spek({
assertThat(configuredSubject.compileAndLint(propertyAsParam)).isEmpty() assertThat(configuredSubject.compileAndLint(propertyAsParam)).isEmpty()
} }
it("should not report when property is documented as property") { @Test
fun `should not report when property is documented as property`() {
val propertyAsParam = """ val propertyAsParam = """
/** /**
* @param someParam Description of param * @param someParam Description of param
@@ -427,4 +468,4 @@ class OutdatedDocumentationSpec : Spek({
} }
} }
} }
}) }

View File

@@ -3,16 +3,16 @@ package io.gitlab.arturbosch.detekt.rules.documentation
import io.gitlab.arturbosch.detekt.test.TestConfig import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.compileAndLint import io.gitlab.arturbosch.detekt.test.compileAndLint
import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThat
import org.spekframework.spek2.Spek import org.junit.jupiter.api.Nested
import org.spekframework.spek2.style.specification.describe import org.junit.jupiter.api.Test
private const val SEARCH_IN_NESTED_CLASS = "searchInNestedClass" private const val SEARCH_IN_NESTED_CLASS = "searchInNestedClass"
private const val SEARCH_IN_INNER_CLASS = "searchInInnerClass" private const val SEARCH_IN_INNER_CLASS = "searchInInnerClass"
private const val SEARCH_IN_INNER_OBJECT = "searchInInnerObject" private const val SEARCH_IN_INNER_OBJECT = "searchInInnerObject"
private const val SEARCH_IN_INNER_INTERFACE = "searchInInnerInterface" private const val SEARCH_IN_INNER_INTERFACE = "searchInInnerInterface"
class UndocumentedPublicClassSpec : Spek({ class UndocumentedPublicClassSpec {
val subject by memoized { UndocumentedPublicClass() } val subject = UndocumentedPublicClass()
val inner = """ val inner = """
/** Some doc */ /** Some doc */
@@ -55,53 +55,65 @@ class UndocumentedPublicClassSpec : Spek({
val privateClass = "private class TestNested {}" val privateClass = "private class TestNested {}"
val internalClass = "internal class TestNested {}" val internalClass = "internal class TestNested {}"
describe("UndocumentedPublicClass rule") { @Nested
inner class `UndocumentedPublicClass rule` {
it("should report inner classes by default") { @Test
fun `should report inner classes by default`() {
assertThat(subject.compileAndLint(inner)).hasSize(1) assertThat(subject.compileAndLint(inner)).hasSize(1)
} }
it("should report inner object by default") { @Test
fun `should report inner object by default`() {
assertThat(subject.compileAndLint(innerObject)).hasSize(1) assertThat(subject.compileAndLint(innerObject)).hasSize(1)
} }
it("should report inner interfaces by default") { @Test
fun `should report inner interfaces by default`() {
assertThat(subject.compileAndLint(innerInterface)).hasSize(1) assertThat(subject.compileAndLint(innerInterface)).hasSize(1)
} }
it("should report nested classes by default") { @Test
fun `should report nested classes by default`() {
assertThat(subject.compileAndLint(nested)).hasSize(1) assertThat(subject.compileAndLint(nested)).hasSize(1)
} }
it("should report explicit public nested classes by default") { @Test
fun `should report explicit public nested classes by default`() {
assertThat(subject.compileAndLint(nestedPublic)).hasSize(1) assertThat(subject.compileAndLint(nestedPublic)).hasSize(1)
} }
it("should not report internal classes") { @Test
fun `should not report internal classes`() {
assertThat(subject.compileAndLint(internalClass)).isEmpty() assertThat(subject.compileAndLint(internalClass)).isEmpty()
} }
it("should not report private classes") { @Test
fun `should not report private classes`() {
assertThat(subject.compileAndLint(privateClass)).isEmpty() assertThat(subject.compileAndLint(privateClass)).isEmpty()
} }
it("should not report nested private classes") { @Test
fun `should not report nested private classes`() {
assertThat(subject.compileAndLint(nestedPrivate)).isEmpty() assertThat(subject.compileAndLint(nestedPrivate)).isEmpty()
} }
it("should not report inner classes when turned off") { @Test
fun `should not report inner classes when turned off`() {
val findings = val findings =
UndocumentedPublicClass(TestConfig(mapOf(SEARCH_IN_INNER_CLASS to "false"))).compileAndLint(inner) UndocumentedPublicClass(TestConfig(mapOf(SEARCH_IN_INNER_CLASS to "false"))).compileAndLint(inner)
assertThat(findings).isEmpty() assertThat(findings).isEmpty()
} }
it("should not report inner objects when turned off") { @Test
fun `should not report inner objects when turned off`() {
val findings = val findings =
UndocumentedPublicClass(TestConfig(mapOf(SEARCH_IN_INNER_OBJECT to "false"))).compileAndLint(innerObject) UndocumentedPublicClass(TestConfig(mapOf(SEARCH_IN_INNER_OBJECT to "false"))).compileAndLint(innerObject)
assertThat(findings).isEmpty() assertThat(findings).isEmpty()
} }
it("should not report inner interfaces when turned off") { @Test
fun `should not report inner interfaces when turned off`() {
val findings = val findings =
UndocumentedPublicClass(TestConfig(mapOf(SEARCH_IN_INNER_INTERFACE to "false"))).compileAndLint( UndocumentedPublicClass(TestConfig(mapOf(SEARCH_IN_INNER_INTERFACE to "false"))).compileAndLint(
innerInterface innerInterface
@@ -109,17 +121,20 @@ class UndocumentedPublicClassSpec : Spek({
assertThat(findings).isEmpty() assertThat(findings).isEmpty()
} }
it("should not report nested classes when turned off") { @Test
fun `should not report nested classes when turned off`() {
val findings = val findings =
UndocumentedPublicClass(TestConfig(mapOf(SEARCH_IN_NESTED_CLASS to "false"))).compileAndLint(nested) UndocumentedPublicClass(TestConfig(mapOf(SEARCH_IN_NESTED_CLASS to "false"))).compileAndLint(nested)
assertThat(findings).isEmpty() assertThat(findings).isEmpty()
} }
it("should report missing doc over object declaration") { @Test
fun `should report missing doc over object declaration`() {
assertThat(subject.compileAndLint("object o")).hasSize(1) assertThat(subject.compileAndLint("object o")).hasSize(1)
} }
it("should not report non-public nested classes") { @Test
fun `should not report non-public nested classes`() {
val code = """ val code = """
internal class Outer { internal class Outer {
class Nested class Nested
@@ -129,7 +144,8 @@ class UndocumentedPublicClassSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("should not report non-public nested interfaces") { @Test
fun `should not report non-public nested interfaces`() {
val code = """ val code = """
internal class Outer { internal class Outer {
interface Inner interface Inner
@@ -138,7 +154,8 @@ class UndocumentedPublicClassSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("should not report non-public nested objects") { @Test
fun `should not report non-public nested objects`() {
val code = """ val code = """
internal class Outer { internal class Outer {
object Inner object Inner
@@ -147,7 +164,8 @@ class UndocumentedPublicClassSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("should not report for documented public object") { @Test
fun `should not report for documented public object`() {
val code = """ val code = """
/** /**
* Class docs not being recognized. * Class docs not being recognized.
@@ -166,7 +184,8 @@ class UndocumentedPublicClassSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("should not report for anonymous objects") { @Test
fun `should not report for anonymous objects`() {
val code = """ val code = """
fun main(args: Array<String>) { fun main(args: Array<String>) {
val value = object : Iterator<Int> { val value = object : Iterator<Int> {
@@ -178,7 +197,8 @@ class UndocumentedPublicClassSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("should report for enum classes") { @Test
fun `should report for enum classes`() {
val code = """ val code = """
enum class Enum { enum class Enum {
CONSTANT CONSTANT
@@ -187,7 +207,8 @@ class UndocumentedPublicClassSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
it("should not report for enum constants") { @Test
fun `should not report for enum constants`() {
val code = """ val code = """
/** Some doc */ /** Some doc */
enum class Enum { enum class Enum {
@@ -197,7 +218,8 @@ class UndocumentedPublicClassSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("should not report for fun interfaces") { @Test
fun `should not report for fun interfaces`() {
val code = """ val code = """
/** /**
* This interface is an example * This interface is an example
@@ -212,4 +234,4 @@ class UndocumentedPublicClassSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
} }
}) }

View File

@@ -2,22 +2,25 @@ package io.gitlab.arturbosch.detekt.rules.documentation
import io.gitlab.arturbosch.detekt.test.compileAndLint import io.gitlab.arturbosch.detekt.test.compileAndLint
import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThat
import org.spekframework.spek2.Spek import org.junit.jupiter.api.Nested
import org.spekframework.spek2.style.specification.describe import org.junit.jupiter.api.Test
class UndocumentedPublicFunctionSpec : Spek({ class UndocumentedPublicFunctionSpec {
val subject by memoized { UndocumentedPublicFunction() } val subject = UndocumentedPublicFunction()
describe("UndocumentedPublicFunction rule") { @Nested
inner class `UndocumentedPublicFunction rule` {
it("reports undocumented public functions") { @Test
fun `reports undocumented public functions`() {
val code = """ val code = """
fun noComment1() {} fun noComment1() {}
""" """
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
it("reports undocumented public function in object") { @Test
fun `reports undocumented public function in object`() {
val code = """ val code = """
object Test { object Test {
fun noComment1() {} fun noComment1() {}
@@ -26,7 +29,8 @@ class UndocumentedPublicFunctionSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
it("reports undocumented public function in nested object") { @Test
fun `reports undocumented public function in nested object`() {
val code = """ val code = """
class Test { class Test {
object Test2 { object Test2 {
@@ -37,7 +41,8 @@ class UndocumentedPublicFunctionSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
it("reports undocumented public functions in companion object") { @Test
fun `reports undocumented public functions in companion object`() {
val code = """ val code = """
class Test { class Test {
companion object { companion object {
@@ -49,7 +54,8 @@ class UndocumentedPublicFunctionSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(2) assertThat(subject.compileAndLint(code)).hasSize(2)
} }
it("reports undocumented public function in an interface") { @Test
fun `reports undocumented public function in an interface`() {
val code = """ val code = """
interface Test { interface Test {
fun noComment1() fun noComment1()
@@ -58,7 +64,8 @@ class UndocumentedPublicFunctionSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
it("does not report documented public function") { @Test
fun `does not report documented public function`() {
val code = """ val code = """
/** /**
* Comment * Comment
@@ -68,7 +75,8 @@ class UndocumentedPublicFunctionSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("does not report documented public function in class") { @Test
fun `does not report documented public function in class`() {
val code = """ val code = """
class Test { class Test {
/** /**
@@ -80,7 +88,8 @@ class UndocumentedPublicFunctionSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("does not report undocumented internal and private function") { @Test
fun `does not report undocumented internal and private function`() {
val code = """ val code = """
class Test { class Test {
internal fun no1(){} internal fun no1(){}
@@ -90,7 +99,8 @@ class UndocumentedPublicFunctionSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("does not report undocumented nested function") { @Test
fun `does not report undocumented nested function`() {
val code = """ val code = """
/** /**
* Comment * Comment
@@ -102,7 +112,8 @@ class UndocumentedPublicFunctionSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("does not report public functions in internal class") { @Test
fun `does not report public functions in internal class`() {
val code = """ val code = """
internal class NoComments { internal class NoComments {
fun nope1() {} fun nope1() {}
@@ -112,7 +123,8 @@ class UndocumentedPublicFunctionSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("does not report public functions in private class") { @Test
fun `does not report public functions in private class`() {
val code = """ val code = """
private class NoComments { private class NoComments {
fun nope1() {} fun nope1() {}
@@ -122,7 +134,8 @@ class UndocumentedPublicFunctionSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("does not report public functions in private object") { @Test
fun `does not report public functions in private object`() {
val code = """ val code = """
private object Test { private object Test {
fun noComment1() {} fun noComment1() {}
@@ -131,8 +144,10 @@ class UndocumentedPublicFunctionSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
context("nested class") { @Nested
it("does not report public functions in internal interface") { inner class `nested class` {
@Test
fun `does not report public functions in internal interface`() {
val code = """ val code = """
internal interface Foo { internal interface Foo {
interface Bar { interface Bar {
@@ -144,7 +159,8 @@ class UndocumentedPublicFunctionSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("does not report public functions in private class") { @Test
fun `does not report public functions in private class`() {
val code = """ val code = """
class Foo { class Foo {
private class Bar { private class Bar {
@@ -158,7 +174,8 @@ class UndocumentedPublicFunctionSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("does not report public functions in private object") { @Test
fun `does not report public functions in private object`() {
val code = """ val code = """
private object Foo { private object Foo {
class Bar { class Bar {
@@ -171,4 +188,4 @@ class UndocumentedPublicFunctionSpec : Spek({
} }
} }
} }
}) }

View File

@@ -2,20 +2,23 @@ package io.gitlab.arturbosch.detekt.rules.documentation
import io.gitlab.arturbosch.detekt.test.compileAndLint import io.gitlab.arturbosch.detekt.test.compileAndLint
import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThat
import org.spekframework.spek2.Spek import org.junit.jupiter.api.Nested
import org.spekframework.spek2.style.specification.describe import org.junit.jupiter.api.Test
class UndocumentedPublicPropertySpec : Spek({ class UndocumentedPublicPropertySpec {
val subject by memoized { UndocumentedPublicProperty() } val subject = UndocumentedPublicProperty()
describe("UndocumentedPublicProperty rule") { @Nested
inner class `UndocumentedPublicProperty rule` {
it("reports undocumented public property") { @Test
fun `reports undocumented public property`() {
val code = "val a = 1" val code = "val a = 1"
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
it("reports undocumented public property in objects") { @Test
fun `reports undocumented public property in objects`() {
val code = """ val code = """
object Test { object Test {
val a = 1 val a = 1
@@ -24,7 +27,8 @@ class UndocumentedPublicPropertySpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
it("reports undocumented public property in nested objects") { @Test
fun `reports undocumented public property in nested objects`() {
val code = """ val code = """
class Test { class Test {
object NestedTest { object NestedTest {
@@ -35,7 +39,8 @@ class UndocumentedPublicPropertySpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
it("reports undocumented public properties in companion object") { @Test
fun `reports undocumented public properties in companion object`() {
val code = """ val code = """
class Test { class Test {
companion object { companion object {
@@ -47,7 +52,8 @@ class UndocumentedPublicPropertySpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(2) assertThat(subject.compileAndLint(code)).hasSize(2)
} }
it("reports undocumented public property in an interface") { @Test
fun `reports undocumented public property in an interface`() {
val code = """ val code = """
interface Test { interface Test {
val a: Int val a: Int
@@ -56,17 +62,20 @@ class UndocumentedPublicPropertySpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
it("reports undocumented public properties in a primary constructor") { @Test
fun `reports undocumented public properties in a primary constructor`() {
val code = "class Test(val a: Int)" val code = "class Test(val a: Int)"
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
it("reports undocumented public property in a primary constructor") { @Test
fun `reports undocumented public property in a primary constructor`() {
val code = "/* comment */ class Test(val a: Int)" val code = "/* comment */ class Test(val a: Int)"
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
it("does not report documented public property") { @Test
fun `does not report documented public property`() {
val code = """ val code = """
/** /**
* Comment * Comment
@@ -76,7 +85,8 @@ class UndocumentedPublicPropertySpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("does not report documented public property in class") { @Test
fun `does not report documented public property in class`() {
val code = """ val code = """
class Test { class Test {
/** /**
@@ -88,7 +98,8 @@ class UndocumentedPublicPropertySpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("does not report undocumented, public and overridden property in class") { @Test
fun `does not report undocumented, public and overridden property in class`() {
val code = """ val code = """
interface I { interface I {
/** /**
@@ -104,7 +115,8 @@ class UndocumentedPublicPropertySpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("does not report undocumented internal and private property") { @Test
fun `does not report undocumented internal and private property`() {
val code = """ val code = """
class Test { class Test {
internal val a = 1 internal val a = 1
@@ -114,7 +126,8 @@ class UndocumentedPublicPropertySpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("does not report local variables") { @Test
fun `does not report local variables`() {
val code = """ val code = """
fun commented(x: Int) { fun commented(x: Int) {
var a = x var a = x
@@ -123,7 +136,8 @@ class UndocumentedPublicPropertySpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("does not report public properties in internal class") { @Test
fun `does not report public properties in internal class`() {
val code = """ val code = """
internal class NoComments { internal class NoComments {
public val a = 1 public val a = 1
@@ -133,7 +147,8 @@ class UndocumentedPublicPropertySpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("does not report public properties in private class") { @Test
fun `does not report public properties in private class`() {
val code = """ val code = """
private class NoComments { private class NoComments {
public val a = 1 public val a = 1
@@ -143,7 +158,8 @@ class UndocumentedPublicPropertySpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("does not report properties in a secondary constructor") { @Test
fun `does not report properties in a secondary constructor`() {
val code = """ val code = """
class Test() { class Test() {
constructor(a: Int) : this() constructor(a: Int) : this()
@@ -152,7 +168,8 @@ class UndocumentedPublicPropertySpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("does not report undocumented non-public properties in a primary constructor") { @Test
fun `does not report undocumented non-public properties in a primary constructor`() {
val code = """ val code = """
class Test1(internal val a: Int) class Test1(internal val a: Int)
class Test2(b: Int) class Test2(b: Int)
@@ -160,12 +177,14 @@ class UndocumentedPublicPropertySpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("does not report undocumented public properties in a primary constructor for an internal class") { @Test
fun `does not report undocumented public properties in a primary constructor for an internal class`() {
val code = "internal class Test(val a: Int)" val code = "internal class Test(val a: Int)"
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("does not report documented public properties in a primary constructor") { @Test
fun `does not report documented public properties in a primary constructor`() {
val code = """ val code = """
/** /**
* @property a int1 * @property a int1
@@ -187,7 +206,8 @@ class UndocumentedPublicPropertySpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("does not report undocumented public properties in private object") { @Test
fun `does not report undocumented public properties in private object`() {
val code = """ val code = """
private object Test { private object Test {
val a = 1 val a = 1
@@ -196,9 +216,11 @@ class UndocumentedPublicPropertySpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
describe("public properties in nested classes") { @Nested
inner class `public properties in nested classes` {
it("reports undocumented public properties in nested classes") { @Test
fun `reports undocumented public properties in nested classes`() {
val code = """ val code = """
class Outer { class Outer {
class Inner { class Inner {
@@ -213,7 +235,8 @@ class UndocumentedPublicPropertySpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(2) assertThat(subject.compileAndLint(code)).hasSize(2)
} }
it("reports undocumented public properties in inner classes") { @Test
fun `reports undocumented public properties in inner classes`() {
val code = """ val code = """
class Outer { class Outer {
inner class Inner { inner class Inner {
@@ -224,7 +247,8 @@ class UndocumentedPublicPropertySpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
it("reports undocumented public properties in classes nested in objects") { @Test
fun `reports undocumented public properties in classes nested in objects`() {
val code = """ val code = """
object Outer { object Outer {
class Inner { class Inner {
@@ -235,7 +259,8 @@ class UndocumentedPublicPropertySpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
it("does not report undocumented and non-public properties in nested classes") { @Test
fun `does not report undocumented and non-public properties in nested classes`() {
val code = """ val code = """
internal class Outer { internal class Outer {
class Inner { class Inner {
@@ -246,7 +271,8 @@ class UndocumentedPublicPropertySpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("does not report undocumented and non-public properties in inner classes") { @Test
fun `does not report undocumented and non-public properties in inner classes`() {
val code = """ val code = """
internal class Outer { internal class Outer {
inner class Inner { inner class Inner {
@@ -258,9 +284,11 @@ class UndocumentedPublicPropertySpec : Spek({
} }
} }
describe("public properties in primary constructors inside nested classes") { @Nested
inner class `public properties in primary constructors inside nested classes` {
it("reports undocumented public properties in nested classes") { @Test
fun `reports undocumented public properties in nested classes`() {
val code = """ val code = """
class Outer(val a: Int) { class Outer(val a: Int) {
class Inner(val b: Int) { class Inner(val b: Int) {
@@ -271,7 +299,8 @@ class UndocumentedPublicPropertySpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(3) assertThat(subject.compileAndLint(code)).hasSize(3)
} }
it("reports undocumented public properties in inner classes") { @Test
fun `reports undocumented public properties in inner classes`() {
val code = """ val code = """
class Outer(val a: Int) { class Outer(val a: Int) {
inner class Inner(val b: Int) inner class Inner(val b: Int)
@@ -280,7 +309,8 @@ class UndocumentedPublicPropertySpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(2) assertThat(subject.compileAndLint(code)).hasSize(2)
} }
it("reports undocumented public properties in classes nested in objects") { @Test
fun `reports undocumented public properties in classes nested in objects`() {
val code = """ val code = """
object Outer { object Outer {
class Inner(val a: Int) class Inner(val a: Int)
@@ -289,7 +319,8 @@ class UndocumentedPublicPropertySpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1) assertThat(subject.compileAndLint(code)).hasSize(1)
} }
it("does not report undocumented and non-public properties in nested classes") { @Test
fun `does not report undocumented and non-public properties in nested classes`() {
val code = """ val code = """
internal class Outer(val a: Int) { internal class Outer(val a: Int) {
class Inner(val b: Int) class Inner(val b: Int)
@@ -298,7 +329,8 @@ class UndocumentedPublicPropertySpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty() assertThat(subject.compileAndLint(code)).isEmpty()
} }
it("does not report undocumented and non-public properties in inner classes") { @Test
fun `does not report undocumented and non-public properties in inner classes`() {
val code = """ val code = """
internal class Outer(val a: Int) { internal class Outer(val a: Int) {
inner class Inner(val b: Int) inner class Inner(val b: Int)
@@ -308,4 +340,4 @@ class UndocumentedPublicPropertySpec : Spek({
} }
} }
} }
}) }