Skip to content

Commit

Permalink
Migrate detekt-rules-documentation tests to JUnit (detekt#4568)
Browse files Browse the repository at this point in the history
  • Loading branch information
3flex authored and Goooler committed Feb 10, 2022
1 parent 669c976 commit ebb78c1
Show file tree
Hide file tree
Showing 11 changed files with 400 additions and 231 deletions.
3 changes: 1 addition & 2 deletions detekt-rules-documentation/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ plugins {
dependencies {
compileOnly(projects.detektApi)
testImplementation(projects.detektTest)
testImplementation(libs.bundles.testImplementation)
testRuntimeOnly(libs.spek.runner)
testImplementation(libs.assertj)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@ import io.gitlab.arturbosch.detekt.test.assertThat
import io.gitlab.arturbosch.detekt.test.lint
import io.gitlab.arturbosch.detekt.test.yamlConfig
import org.jetbrains.kotlin.resolve.BindingContext
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import java.io.PrintStream
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(
"""
/* LICENSE */
Expand All @@ -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(
"""
/* WRONG LICENSE */
Expand All @@ -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(
"""
package cases
Expand All @@ -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(
"""
//
Expand All @@ -80,7 +89,8 @@ internal class AbsentOrWrongFileLicenseSpec : Spek({
assertThat(findings).isEmpty()
}

it("reports nothing for 2021") {
@Test
fun `reports nothing for 2021`() {
val findings = checkLicence(
"""
//
Expand All @@ -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(
"""
package cases
Expand All @@ -111,7 +123,8 @@ internal class AbsentOrWrongFileLicenseSpec : Spek({
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(
"""
package cases
Expand All @@ -128,7 +141,8 @@ internal class AbsentOrWrongFileLicenseSpec : Spek({
assertThat(findings).hasSize(1)
}

it("file with incomplete license header") {
@Test
fun `file with incomplete license header`() {
val findings = checkLicence(
"""
//
Expand All @@ -142,7 +156,8 @@ internal class AbsentOrWrongFileLicenseSpec : Spek({
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(
"""
//
Expand All @@ -160,7 +175,8 @@ internal class AbsentOrWrongFileLicenseSpec : Spek({
assertThat(findings).hasSize(1)
}

it("file with incorrect year in license header") {
@Test
fun `file with incorrect year in license header`() {
val findings = checkLicence(
"""
//
Expand All @@ -178,7 +194,7 @@ internal class AbsentOrWrongFileLicenseSpec : Spek({
}
}
}
})
}

@OptIn(UnstableApi::class)
private fun checkLicence(content: String, isRegexLicense: Boolean = false): List<Finding> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package io.gitlab.arturbosch.detekt.rules.documentation

import io.gitlab.arturbosch.detekt.test.compileAndLint
import org.assertj.core.api.Assertions.assertThat
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test

class CommentOverPrivateMethodSpec : Spek({
val subject by memoized { CommentOverPrivateFunction() }
class CommentOverPrivateMethodSpec {
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 = """
class Test {
/**
Expand All @@ -21,7 +23,8 @@ class CommentOverPrivateMethodSpec : Spek({
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 = """
/**
* asdf
Expand All @@ -30,7 +33,8 @@ class CommentOverPrivateMethodSpec : Spek({
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 = """
class Test {
/**
Expand All @@ -41,4 +45,4 @@ class CommentOverPrivateMethodSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}
}
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package io.gitlab.arturbosch.detekt.rules.documentation

import io.gitlab.arturbosch.detekt.test.compileAndLint
import org.assertj.core.api.Assertions.assertThat
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test

class CommentOverPrivatePropertiesSpec : Spek({
val subject by memoized { CommentOverPrivateProperty() }
class CommentOverPrivatePropertiesSpec {
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 = """
/**
* asdf
Expand All @@ -19,7 +21,8 @@ class CommentOverPrivatePropertiesSpec : Spek({
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 = """
/**
* asdf
Expand All @@ -28,7 +31,8 @@ class CommentOverPrivatePropertiesSpec : Spek({
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 = """
class Test {
/**
Expand All @@ -39,7 +43,8 @@ class CommentOverPrivatePropertiesSpec : Spek({
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 = """
class Test {
/**
Expand All @@ -50,4 +55,4 @@ class CommentOverPrivatePropertiesSpec : Spek({
assertThat(subject.compileAndLint(code)).isEmpty()
}
}
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package io.gitlab.arturbosch.detekt.rules.documentation

import io.gitlab.arturbosch.detekt.test.assertThat
import io.gitlab.arturbosch.detekt.test.compileAndLint
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test

class DeprecatedBlockTagSpec : Spek({
val subject by memoized { DeprecatedBlockTag() }
describe("DeprecatedBlockTag rule") {
it("does not report regular kdoc block") {
class DeprecatedBlockTagSpec {
val subject = DeprecatedBlockTag()

@Nested
inner class `DeprecatedBlockTag rule` {
@Test
fun `does not report regular kdoc block`() {
val code = """
/**
* This is just a regular kdoc block.
Expand All @@ -20,7 +23,8 @@ class DeprecatedBlockTagSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(0)
}

describe("reporting deprecation tag on kdoc block") {
@Nested
inner class `reporting deprecation tag on kdoc block` {
val code = """
/**
* I am a KDoc block
Expand All @@ -30,21 +34,25 @@ class DeprecatedBlockTagSpec : Spek({
fun ohNo() { }
"""

it("has found something") {
@Test
fun `has found something`() {
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("correct message") {
@Test
fun `correct message`() {
assertThat(subject.compileAndLint(code)[0]).hasMessage(
"@deprecated tag block does not properly report " +
"deprecation in Kotlin, use @Deprecated annotation instead"
)
}
}

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 = """
/**
* Hello there
Expand All @@ -56,7 +64,8 @@ class DeprecatedBlockTagSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("report deprecation tag on property") {
@Test
fun `report deprecation tag on property`() {
val code = """
class Thing {
/**
Expand All @@ -70,7 +79,8 @@ class DeprecatedBlockTagSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("report deprecation tag on annotation class") {
@Test
fun `report deprecation tag on annotation class`() {
val code = """
/**
* An annotation you should not use
Expand All @@ -82,7 +92,8 @@ class DeprecatedBlockTagSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("report deprecation tag on constructor") {
@Test
fun `report deprecation tag on constructor`() {
val code = """
class Thing {
/**
Expand All @@ -96,7 +107,8 @@ class DeprecatedBlockTagSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("report deprecation tag on property setter") {
@Test
fun `report deprecation tag on property setter`() {
val code = """
class Thing {
var someProperty: Int
Expand All @@ -112,7 +124,8 @@ class DeprecatedBlockTagSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("report deprecation tag on property getter") {
@Test
fun `report deprecation tag on property getter`() {
val code = """
class Thing {
var someProperty: Int
Expand All @@ -128,7 +141,8 @@ class DeprecatedBlockTagSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("report deprecation tag on typealias") {
@Test
fun `report deprecation tag on typealias`() {
val code = """
/**
* This alias is pointless, do not use it
Expand All @@ -141,4 +155,4 @@ class DeprecatedBlockTagSpec : Spek({
}
}
}
})
}

0 comments on commit ebb78c1

Please sign in to comment.