Refactor ParserTestSpec to not depend on internal kotest API

This adds custom test scopes and makes the ParserTestCtx a container.
This commit is contained in:
Andreas Dangel committed 2023-01-20 20:10:38 +01:00
1 parent bc7b0f8e2f
commit 67c04ef42b
2 files changed
+20 -15

No files matched your search

@@ -5,6 +5,8 @@
package net.sourceforge.pmd.lang.java.ast
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.scopes.AbstractContainerScope
import io.kotest.core.test.TestScope
import io.kotest.matchers.string.shouldContain
import net.sourceforge.pmd.lang.ast.Node
import net.sourceforge.pmd.lang.ast.test.Assertions
@@ -96,10 +98,12 @@ enum class JavaVersion : Comparable<JavaVersion> {
* @property otherImports Other imports, without the `import` and semicolon
* @property genClassHeader Header of the enclosing class used in parsing contexts like parseExpression, etc. E.g. "class Foo"
*/
open class ParserTestCtx(val javaVersion: JavaVersion = JavaVersion.Latest,
val importedTypes: MutableList<Class<*>> = mutableListOf(),
val otherImports: MutableList<String> = mutableListOf(),
var genClassHeader: String = "class Foo") {
open class ParserTestCtx(
testScope : TestScope,
val javaVersion: JavaVersion = JavaVersion.Latest,
val importedTypes: MutableList<Class<*>> = mutableListOf(),
val otherImports: MutableList<String> = mutableListOf(),
var genClassHeader: String = "class Foo") : AbstractContainerScope(testScope) {
/** Imports to add to the top of the parsing contexts. */
internal val imports: List<String>
@@ -7,6 +7,7 @@ package net.sourceforge.pmd.lang.java.ast
import io.kotest.core.names.TestName
import io.kotest.core.source.sourceRef
import io.kotest.core.spec.DslDrivenSpec
import io.kotest.core.spec.style.scopes.AbstractContainerScope
import io.kotest.core.spec.style.scopes.RootScope
import io.kotest.core.spec.style.scopes.addContainer
import io.kotest.core.spec.style.scopes.addTest
@@ -112,43 +113,43 @@ abstract class ParserTestSpec(body: ParserTestSpec.() -> Unit) : DslDrivenSpec()
}
private suspend fun containedParserTestImpl(
scope: TestScope,
name: String,
javaVersion: JavaVersion,
assertions: ParserTestCtx.() -> Unit) {
testScope: GroupTestCtx.VersionedTestCtx,
name: String,
javaVersion: JavaVersion,
assertions: ParserTestCtx.() -> Unit) {
val nested = NestedTest(
name = TestName(name),
test = { ParserTestCtx(javaVersion).assertions() },
test = { ParserTestCtx(testScope, javaVersion).assertions() },
config = null,
type = TestType.Test,
disabled = false,
source = sourceRef()
)
scope.registerTestCase(nested)
testScope.registerTestCase(nested)
}
inner class GroupTestCtx(private val scope: TestScope) {
inner class GroupTestCtx(testScope: TestScope) : AbstractContainerScope(testScope) {
suspend fun onVersions(javaVersions: List<JavaVersion>, spec: suspend VersionedTestCtx.() -> Unit) {
javaVersions.forEach { javaVersion ->
val nested = NestedTest(
name = TestName("Java ${javaVersion.pmdName}"),
test = { VersionedTestCtx(this, javaVersion).spec() },
test = { this@GroupTestCtx.VersionedTestCtx(this, javaVersion).spec() },
config = null,
type = TestType.Container,
disabled = false,
source = sourceRef()
)
scope.registerTestCase(nested)
this.registerTestCase(nested)
}
}
inner class VersionedTestCtx(private val scope: TestScope, javaVersion: JavaVersion) : ParserTestCtx(javaVersion) {
inner class VersionedTestCtx(testScope: TestScope, javaVersion: JavaVersion) : ParserTestCtx(testScope, javaVersion) {
suspend infix fun String.should(matcher: Assertions<String>) {
containedParserTestImpl(scope, "'$this'", javaVersion = javaVersion) {
containedParserTestImpl(this@VersionedTestCtx, "'$this'", javaVersion = javaVersion) {
this@should kotlintestShould matcher
}
}