Fix custom parser spec

Add kotest-runner-junit5-jvm as a dependency,
in pmd-java. This makes kotest discoverable
as Junit tests, which IDEs can pick up on (at
least IntelliJ does).

Update kotlin version to latest stable (1.3.72)
This commit is contained in:
Clément Fournier
2020-07-20 16:06:13 +02:00
parent 68ae318f02
commit 6d73eaf3c7
8 changed files with 55 additions and 39 deletions

View File

@@ -176,6 +176,12 @@
<artifactId>kotest-assertions-core-jvm</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<!-- Contains stuff like FunSpec, etc -->
<groupId>io.kotest</groupId>
<artifactId>kotest-runner-junit5-jvm</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
@@ -188,7 +194,7 @@
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<artifactId>kotlin-test-junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>

View File

@@ -2,9 +2,14 @@ package net.sourceforge.pmd.lang.java.ast
import io.kotest.core.config.Project
import io.kotest.core.spec.style.DslDrivenSpec
import io.kotest.core.spec.style.scopes.Lifecycle
import io.kotest.core.spec.style.scopes.RootScope
import io.kotest.core.spec.style.scopes.RootTestRegistration
import io.kotest.core.test.TestCaseConfig
import io.kotest.core.test.TestContext
import io.kotest.core.test.TestName
import io.kotest.core.test.TestType
import io.kotest.runner.junit.platform.IntelliMarker
import net.sourceforge.pmd.lang.ast.test.Assertions
import io.kotest.matchers.should as kotlintestShould
@@ -16,14 +21,23 @@ import io.kotest.matchers.should as kotlintestShould
*
* @author Clément Fournier
*/
abstract class ParserTestSpec(body: ParserTestSpec.() -> Unit) : AbstractSpec() {
abstract class ParserTestSpec(body: ParserTestSpec.() -> Unit) : DslDrivenSpec(), RootScope, IntelliMarker {
init {
body()
}
fun test(name: String, test: TestContext.() -> Unit) =
addTestCase(name, test, defaultTestCaseConfig, TestType.Test)
override fun lifecycle(): Lifecycle = Lifecycle.from(this)
override fun defaultConfig(): TestCaseConfig = actualDefaultConfig()
override fun registration(): RootTestRegistration = RootTestRegistration.from(this)
fun test(name: String, disabled: Boolean = false, test: suspend TestContext.() -> Unit) =
registration().addTest(
name = TestName(name),
xdisabled = disabled,
test = test,
config = actualDefaultConfig()
)
/**
* Defines a group of tests that should be named similarly,
@@ -36,14 +50,19 @@ abstract class ParserTestSpec(body: ParserTestSpec.() -> Unit) : AbstractSpec()
* regression tests without bothering to find a name.
*
* @param name Name of the container test
* @param spec Assertions. Each call to [io.kotlintest.should] on a string
* @param spec Assertions. Each call to [io.kotest.matchers.should] on a string
* receiver is replaced by a [GroupTestCtx.should], which creates a
* new parser test.
*
*/
fun parserTestGroup(name: String,
spec: GroupTestCtx.() -> Unit) =
addTestCase(name, { GroupTestCtx(this).spec() }, defaultTestCaseConfig, TestType.Container)
disabled: Boolean = false,
spec: suspend GroupTestCtx.() -> Unit) =
registration().addContainerTest(
name = TestName(name),
test = { GroupTestCtx(this).spec() },
xdisabled = disabled
)
/**
* Defines a group of tests that should be named similarly.
@@ -55,14 +74,14 @@ abstract class ParserTestSpec(body: ParserTestSpec.() -> Unit) : AbstractSpec()
*
* @param name Name of the container test
* @param javaVersion Language versions to use when parsing
* @param spec Assertions. Each call to [io.kotlintest.should] on a string
* @param spec Assertions. Each call to [io.kotest.matchers.should] on a string
* receiver is replaced by a [GroupTestCtx.should], which creates a
* new parser test.
*
*/
fun parserTest(name: String,
javaVersion: JavaVersion = JavaVersion.Latest,
spec: GroupTestCtx.VersionedTestCtx.() -> Unit) =
spec: suspend GroupTestCtx.VersionedTestCtx.() -> Unit) =
parserTest(name, listOf(javaVersion), spec)
/**
@@ -76,44 +95,46 @@ abstract class ParserTestSpec(body: ParserTestSpec.() -> Unit) : AbstractSpec()
*
* @param name Name of the container test
* @param javaVersions Language versions for which to generate tests
* @param spec Assertions. Each call to [io.kotlintest.should] on a string
* @param spec Assertions. Each call to [io.kotest.matchers.should] on a string
* receiver is replaced by a [GroupTestCtx.should], which creates a
* new parser test.
*/
fun parserTest(name: String,
javaVersions: List<JavaVersion>,
spec: GroupTestCtx.VersionedTestCtx.() -> Unit) =
spec: suspend GroupTestCtx.VersionedTestCtx.() -> Unit) =
parserTestGroup(name) {
onVersions(javaVersions) {
spec()
}
}
private fun containedParserTestImpl(
private suspend fun containedParserTestImpl(
context: TestContext,
name: String,
javaVersion: JavaVersion,
assertions: ParserTestCtx.() -> Unit) {
context.registerTestCase(
name = name,
spec = this,
name = TestName(name),
test = { ParserTestCtx(javaVersion).assertions() },
config = defaultTestCaseConfig,
config = actualDefaultConfig(),
type = TestType.Test
)
}
private fun actualDefaultConfig() =
defaultTestConfig ?: defaultTestCaseConfig()
?: Project.testCaseConfig()
inner class GroupTestCtx(private val context: TestContext) {
fun onVersions(javaVersions: List<JavaVersion>, spec: VersionedTestCtx.() -> Unit) {
suspend fun onVersions(javaVersions: List<JavaVersion>, spec: suspend VersionedTestCtx.() -> Unit) {
javaVersions.forEach { javaVersion ->
context.registerTestCase(
name = "Java ${javaVersion.pmdName}",
spec = this@ParserTestSpec,
name = TestName("Java ${javaVersion.pmdName}"),
test = { VersionedTestCtx(this, javaVersion).spec() },
config = defaultTestCaseConfig,
config = actualDefaultConfig(),
type = TestType.Container
)
}
@@ -121,11 +142,11 @@ abstract class ParserTestSpec(body: ParserTestSpec.() -> Unit) : AbstractSpec()
inner class VersionedTestCtx(private val context: TestContext, javaVersion: JavaVersion) : ParserTestCtx(javaVersion) {
infix fun String.should(matcher: Assertions<String>) {
suspend infix fun String.should(matcher: Assertions<String>) {
containedParserTestImpl(context, "'$this'", javaVersion = javaVersion) {
this@should kotlintestShould matcher
}
}
}
}
}
}

View File

@@ -115,11 +115,6 @@
<artifactId>kotest-runner-junit5-jvm</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>

View File

@@ -111,7 +111,7 @@
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<artifactId>kotlin-test-junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>

View File

@@ -4,16 +4,16 @@
package net.sourceforge.pmd.lang.modelica.ast
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.should
import io.kotest.matchers.shouldBe
import io.kotest.specs.AbstractFunSpec
import net.sourceforge.pmd.lang.LanguageRegistry
import net.sourceforge.pmd.lang.ast.Node
import net.sourceforge.pmd.lang.ast.test.matchNode
import net.sourceforge.pmd.lang.ast.test.shouldBe
import java.io.StringReader
class ModelicaCoordsTest : AbstractFunSpec({
class ModelicaCoordsTest : FunSpec({
test("Test line/column numbers for implicit nodes") {

View File

@@ -151,7 +151,7 @@
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<artifactId>kotlin-test-junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>

View File

@@ -4,15 +4,15 @@
package net.sourceforge.pmd.lang.scala.ast
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.should
import io.kotest.specs.AbstractFunSpec
import net.sourceforge.pmd.lang.LanguageRegistry
import net.sourceforge.pmd.lang.ast.Node
import net.sourceforge.pmd.lang.ast.test.matchNode
import net.sourceforge.pmd.lang.ast.test.shouldBe
import java.io.StringReader
class ScalaTreeTests : AbstractFunSpec({
class ScalaTreeTests : FunSpec({
test("Test line/column numbers") {

View File

@@ -85,7 +85,7 @@
<kotlin.compiler.jvmTarget>${maven.compiler.test.target}</kotlin.compiler.jvmTarget>
<kotlin.version>1.3.0</kotlin.version>
<kotlin.version>1.3.72</kotlin.version>
<kotest.version>4.1.2</kotest.version>
<dokka.version>0.10.1</dokka.version>
@@ -820,12 +820,6 @@
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.kotest</groupId>
<artifactId>kotest-runner-junit5-jvm</artifactId>