Skip to content

Commit

Permalink
Migrate JUnit tests to Spek (#1213)
Browse files Browse the repository at this point in the history
  • Loading branch information
3flex authored and Mauin committed Oct 12, 2018
1 parent 538fd0c commit 712dedd
Show file tree
Hide file tree
Showing 30 changed files with 306 additions and 371 deletions.
4 changes: 0 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ subprojects {
}
}

val junitEngineVersion: String by project
val assertjVersion: String by project
val spekVersion: String by project
val kotlinTest by configurations.creating
Expand All @@ -230,12 +229,9 @@ subprojects {

detekt(project(":detekt-cli"))

kotlinTest(kotlin("test"))
kotlinTest("org.junit.jupiter:junit-jupiter-api:$junitEngineVersion")
kotlinTest("org.assertj:assertj-core:$assertjVersion")
kotlinTest("org.jetbrains.spek:spek-api:$spekVersion")
kotlinTest("org.jetbrains.spek:spek-subject-extension:$spekVersion")
kotlinTest("org.junit.jupiter:junit-jupiter-engine:$junitEngineVersion")
}

sourceSets["main"].java.srcDirs("src/main/kotlin")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,77 @@ package io.gitlab.arturbosch.detekt.cli

import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.YamlConfig
import org.junit.jupiter.api.Test
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.it
import java.io.File
import java.nio.file.Paths

class DetektYmlConfigTest {
class DetektYmlConfigTest : Spek({

private val config = loadConfig()
val config = loadConfig()

@Test
fun complexitySection() {
it("complexitySection") {
ConfigAssert(
config,
"complexity",
"io.gitlab.arturbosch.detekt.rules.complexity"
).assert()
}

@Test
fun documentationSection() {
it("documentationSection") {
ConfigAssert(
config,
"comments",
"io.gitlab.arturbosch.detekt.rules.documentation"
).assert()
}

@Test
fun emptyBlocksSection() {
it("emptyBlocksSection") {
ConfigAssert(
config,
"empty-blocks",
"io.gitlab.arturbosch.detekt.rules.empty"
).assert()
}

@Test
fun exceptionsSection() {
it("exceptionsSection") {
ConfigAssert(
config,
"exceptions",
"io.gitlab.arturbosch.detekt.rules.exceptions"
).assert()
}

@Test
fun performanceSection() {
it("performanceSection") {
ConfigAssert(
config,
"performance",
"io.gitlab.arturbosch.detekt.rules.performance"
).assert()
}

@Test
fun potentialBugsSection() {
it("potentialBugsSection") {
ConfigAssert(
config,
"potential-bugs",
"io.gitlab.arturbosch.detekt.rules.bugs"
).assert()
}

@Test
fun styleSection() {
it("styleSection") {
ConfigAssert(
config,
"style",
"io.gitlab.arturbosch.detekt.rules.style"
).assert()
}

private fun loadConfig(): Config {
val workingDirectory = Paths.get(".").toAbsolutePath().normalize().toString()
val file = File("$workingDirectory/src/main/resources/$CONFIG_FILE")
val url = file.toURI().toURL()
return YamlConfig.loadResource(url)
}
}
})

internal const val CONFIG_FILE = "default-detekt-config.yml"

private fun loadConfig(): Config {
val workingDirectory = Paths.get(".").toAbsolutePath().normalize().toString()
val file = File("$workingDirectory/src/main/resources/$CONFIG_FILE")
val url = file.toURI().toURL()
return YamlConfig.loadResource(url)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@ import io.gitlab.arturbosch.detekt.core.ProcessingSettings
import io.gitlab.arturbosch.detekt.test.resource
import org.assertj.core.api.Assertions
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.it
import org.reflections.Reflections
import java.lang.reflect.Modifier
import java.nio.file.Paths

/**
* This tests the existence of all metric processors in the META-INF config file in the core package
*/
class FileProcessorLocatorTest {
class FileProcessorLocatorTest : Spek({

private val packageName = "io.gitlab.arturbosch.detekt.core.processors"

@Test
fun containsAllProcessors() {
it("containsAllProcessors") {
val path = Paths.get(resource(""))
val locator = FileProcessorLocator(ProcessingSettings(path))
val processors = locator.load()
Expand All @@ -30,10 +28,10 @@ class FileProcessorLocatorTest {
.filter { clazz -> processors.firstOrNull { clazz == it.javaClass } == null }
.forEach { Assertions.fail("$it processor is not loaded by the FileProcessorLocator") }
}
})

private fun getProcessorClasses(): List<Class<out FileProcessListener>> {
return Reflections(packageName)
.getSubTypesOf(FileProcessListener::class.java)
.filter { !Modifier.isAbstract(it.modifiers) }
}
private fun getProcessorClasses(): List<Class<out FileProcessListener>> {
return Reflections("io.gitlab.arturbosch.detekt.core.processors")
.getSubTypesOf(FileProcessListener::class.java)
.filter { !Modifier.isAbstract(it.modifiers) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import io.gitlab.arturbosch.detekt.cli.out.XmlOutputReport
import io.gitlab.arturbosch.detekt.core.DetektResult
import io.gitlab.arturbosch.detekt.core.ProcessingSettings
import io.gitlab.arturbosch.detekt.test.resource
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.spek.api.dsl.describe
import org.jetbrains.spek.subject.SubjectSpek
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.PrintStream
import java.nio.file.Path
import java.nio.file.Paths
import kotlin.test.assertTrue

/**
* @author Sebastiano Poggi
Expand Down Expand Up @@ -51,5 +51,5 @@ internal class OutputFacadeSpec : SubjectSpek<OutputFacade>({

fun ByteArrayOutputStream.assertThatItPrintsReportPath(reportName: String) {
val outputString = toString(Charsets.UTF_8.name())
assertTrue { outputString.contains("Successfully generated $reportName at ") }
assertThat(outputString.contains("Successfully generated $reportName at ")).isTrue()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import io.gitlab.arturbosch.detekt.cli.createFinding
import io.gitlab.arturbosch.detekt.test.resource
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.ListAssert
import org.junit.jupiter.api.Test
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.it
import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
Expand All @@ -17,12 +18,11 @@ import java.nio.file.Paths
* @author Artur Bosch
* @author schalkms
*/
internal class BaselineFacadeTest {
class BaselineFacadeTest : Spek({

private val dir = Files.createTempDirectory("baseline_format")
val dir = Files.createTempDirectory("baseline_format")

@Test
fun create() {
it("create") {
val fullPath = dir.resolve("baseline.xml")
val baselineFacade = BaselineFacade(fullPath)
baselineFacade.create(emptyList())
Expand All @@ -32,20 +32,18 @@ internal class BaselineFacadeTest {
}
}

@Test
fun filterWithExistingBaseline() {
it("filterWithExistingBaseline") {
assertFilter(dir)
}

@Test
fun filterWithoutExistingBaseline() {
it("filterWithoutExistingBaseline") {
val path = Paths.get(resource("/smell-baseline.xml"))
assertFilter(path)
}
})

private fun assertFilter(path: Path) {
val findings = listOf<Finding>(createFinding())
val result = BaselineFacade(path).filter(findings)
assertThat(result).isEqualTo(findings)
}
private fun assertFilter(path: Path) {
val findings = listOf<Finding>(createFinding())
val result = BaselineFacade(path).filter(findings)
assertThat(result).isEqualTo(findings)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package io.gitlab.arturbosch.detekt.cli.baseline
import io.gitlab.arturbosch.detekt.test.resource
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.junit.jupiter.api.Test
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.it
import java.nio.file.Files
import java.nio.file.Paths
import java.time.Instant
Expand All @@ -12,10 +13,9 @@ import java.time.Instant
* @author Artur Bosch
* @author schalkms
*/
internal class BaselineFormatTest {
class BaselineFormatTest : Spek({

@Test
fun loadBaseline() {
it("loadBaseline") {
val path = Paths.get(resource("/smell-baseline.xml"))
val (blacklist, whitelist) = BaselineFormat().read(path)

Expand All @@ -28,8 +28,7 @@ internal class BaselineFormatTest {
assertThat(whitelist.timestamp).isEqualTo("987654321")
}

@Test
fun savedAndLoadedXmlAreEqual() {
it("savedAndLoadedXmlAreEqual") {
val now = Instant.now().toEpochMilli().toString()
val tempFile = Files.createTempFile("baseline", now)

Expand All @@ -44,9 +43,8 @@ internal class BaselineFormatTest {
assertThat(loadedBaseline).isEqualTo(savedBaseline)
}

@Test
fun loadInvalidBaseline() {
it("loadInvalidBaseline") {
val path = Paths.get(resource("/invalid-smell-baseline.txt"))
assertThatThrownBy { BaselineFormat().read(path) }.isInstanceOf(InvalidBaselineState::class.java)
}
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package io.gitlab.arturbosch.detekt.cli.out

import io.gitlab.arturbosch.detekt.api.*
import io.gitlab.arturbosch.detekt.cli.TestDetektion
import org.junit.jupiter.api.Test
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.it

internal class HtmlOutputFormatTest {
class HtmlOutputFormatTest : Spek({

private val outputFormat = HtmlOutputReport()
val outputFormat = HtmlOutputReport()

@Test
fun testRenderResultLooksLikeHtml() {
it("testRenderResultLooksLikeHtml") {
val result = outputFormat.render(TestDetektion())

assertThat(result).startsWith("<!DOCTYPE html>\n<html lang=\"en\">")
Expand All @@ -21,56 +21,52 @@ internal class HtmlOutputFormatTest {
assertThat(result).contains("<h2>Findings</h2>")
}

@Test
fun testRenderResultContainsFileLocations() {
it("testRenderResultContainsFileLocations") {
val result = outputFormat.render(createTestDetektionWithMultipleSmells())

assertThat(result).contains("<span class=\"location\">\nsrc/main/com/sample/Sample1.kt:11:1\n</span>")
assertThat(result).contains("<span class=\"location\">\nsrc/main/com/sample/Sample2.kt:22:2\n</span>")
assertThat(result).contains("<span class=\"location\">\nsrc/main/com/sample/Sample3.kt:33:3\n</span>")
}

@Test
fun testRenderResultContainsRules() {
it("testRenderResultContainsRules") {
val result = outputFormat.render(createTestDetektionWithMultipleSmells())

assertThat(result).contains("<span class=\"rule\">\nid_a\n</span>")
assertThat(result).contains("<span class=\"rule\">\nid_a\n</span>")
assertThat(result).contains("<span class=\"rule\">\nid_a\n</span>")
}

@Test
fun testRenderResultContainsMessages() {
it("testRenderResultContainsMessages") {
val result = outputFormat.render(createTestDetektionWithMultipleSmells())

assertThat(result).contains("<span class=\"message\">\nB1\n</span>")
assertThat(result).contains("<span class=\"message\">\nB2\n</span>")
assertThat(result).contains("<span class=\"message\">\nB3\n</span>")
}

@Test
fun testRenderResultContainsDescriptions() {
it("testRenderResultContainsDescriptions") {
val result = outputFormat.render(createTestDetektionWithMultipleSmells())

assertThat(result).contains("<span class=\"description\">\nA1\n</span>")
assertThat(result).contains("<span class=\"description\">\nA2\n</span>")
assertThat(result).contains("<span class=\"description\">\nA3\n</span>")
}

private fun createTestDetektionWithMultipleSmells(): Detektion {
val entity1 = Entity("Sample1", "com.sample.Sample1", "",
Location(SourceLocation(11, 1), TextLocation(0, 10),
"abcd", "src/main/com/sample/Sample1.kt"))
val entity2 = Entity("Sample2", "com.sample.Sample2", "",
Location(SourceLocation(22, 2), TextLocation(0, 20),
"efgh", "src/main/com/sample/Sample2.kt"))
val entity3 = Entity("Sample3", "com.sample.Sample3", "",
Location(SourceLocation(33, 3), TextLocation(0, 30),
"ijkl", "src/main/com/sample/Sample3.kt"))

return TestDetektion(
CodeSmell(Issue("id_a", Severity.CodeSmell, "A1", Debt.TWENTY_MINS), entity1, message = "B1"),
CodeSmell(Issue("id_b", Severity.CodeSmell, "A2", Debt.TWENTY_MINS), entity2, message = "B2"),
CodeSmell(Issue("id_c", Severity.CodeSmell, "A3", Debt.TWENTY_MINS), entity3, message = "B3"))
}
})

private fun createTestDetektionWithMultipleSmells(): Detektion {
val entity1 = Entity("Sample1", "com.sample.Sample1", "",
Location(SourceLocation(11, 1), TextLocation(0, 10),
"abcd", "src/main/com/sample/Sample1.kt"))
val entity2 = Entity("Sample2", "com.sample.Sample2", "",
Location(SourceLocation(22, 2), TextLocation(0, 20),
"efgh", "src/main/com/sample/Sample2.kt"))
val entity3 = Entity("Sample3", "com.sample.Sample3", "",
Location(SourceLocation(33, 3), TextLocation(0, 30),
"ijkl", "src/main/com/sample/Sample3.kt"))

return TestDetektion(
CodeSmell(Issue("id_a", Severity.CodeSmell, "A1", Debt.TWENTY_MINS), entity1, message = "B1"),
CodeSmell(Issue("id_b", Severity.CodeSmell, "A2", Debt.TWENTY_MINS), entity2, message = "B2"),
CodeSmell(Issue("id_c", Severity.CodeSmell, "A3", Debt.TWENTY_MINS), entity3, message = "B3"))
}
Loading

0 comments on commit 712dedd

Please sign in to comment.