Merge branch 'pr-2657'

[lang-test] Upgrade kotlintest to Kotest #2657
This commit is contained in:
Andreas Dangel
2020-07-30 10:31:43 +02:00
17 changed files with 107 additions and 92 deletions

View File

@ -18,6 +18,7 @@ This is a {{ site.pmd.release_type }} release.
* core
* [#724](https://github.com/pmd/pmd/issues/724): \[core] Avoid parsing rulesets multiple times
* [#2653](https://github.com/pmd/pmd/issues/2653): \[lang-test] Upgrade kotlintest to Kotest
* java-performance
* [#2441](https://github.com/pmd/pmd/issues/2441): \[java] RedundantFieldInitializer can not detect a special case for char initialize: `char foo = '\0';`

View File

@ -172,13 +172,14 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.kotlintest</groupId>
<artifactId>kotlintest-assertions</artifactId>
<groupId>io.kotest</groupId>
<artifactId>kotest-assertions-core-jvm</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.kotlintest</groupId>
<artifactId>kotlintest-core</artifactId>
<!-- Contains stuff like FunSpec, etc -->
<groupId>io.kotest</groupId>
<artifactId>kotest-runner-junit5-jvm</artifactId>
<scope>test</scope>
</dependency>
<dependency>
@ -193,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

@ -1,8 +1,8 @@
package net.sourceforge.pmd.lang.java.ast
import io.kotlintest.matchers.collections.shouldContainExactly
import io.kotlintest.should
import io.kotlintest.shouldBe
import io.kotest.matchers.collections.shouldContainExactly
import io.kotest.matchers.should
import io.kotest.matchers.shouldBe
import net.sourceforge.pmd.lang.java.ast.JavaVersion.*
import net.sourceforge.pmd.lang.java.ast.JavaVersion.Companion.Earliest
import net.sourceforge.pmd.lang.java.ast.JavaVersion.Companion.Latest

View File

@ -1,5 +1,5 @@
import io.kotlintest.shouldBe
import io.kotest.matchers.shouldBe
import net.sourceforge.pmd.lang.java.ast.*
import net.sourceforge.pmd.lang.java.ast.JavaVersion.*
import net.sourceforge.pmd.lang.java.ast.JavaVersion.Companion.Latest
@ -88,4 +88,4 @@ class Java11Test : ParserTestSpec({
}
}
})
})

View File

@ -1,7 +1,7 @@
package net.sourceforge.pmd.lang.java.ast
import io.kotlintest.matchers.string.shouldContain
import io.kotlintest.shouldThrow
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.matchers.string.shouldContain
import net.sourceforge.pmd.lang.ast.Node
import net.sourceforge.pmd.lang.ast.test.Assertions
import net.sourceforge.pmd.lang.ast.test.NodeSpec
@ -56,7 +56,7 @@ enum class JavaVersion : Comparable<JavaVersion> {
*
* These are implicitly used by [matchExpr] and [matchStmt], which specify a matcher directly
* on the strings, using their type parameter and the info in this test context to parse, find
* the node, and execute the matcher in a single call. These may be used by [io.kotlintest.should],
* the node, and execute the matcher in a single call. These may be used by [io.kotest.matchers.should],
* e.g.
*
* parserTest("Test ShiftExpression operator") {
@ -69,7 +69,7 @@ enum class JavaVersion : Comparable<JavaVersion> {
* Import statements in the parsing contexts can be configured by adding types to [importedTypes],
* or strings to [otherImports].
*
* Technically the utilities provided by this class may be used outside of [io.kotlintest.specs.FunSpec]s,
* Technically the utilities provided by this class may be used outside of [io.kotest.specs.FunSpec]s,
* e.g. in regular JUnit tests, but I think we should strive to uniformize our testing style,
* especially since KotlinTest defines so many.
*

View File

@ -1,10 +1,17 @@
package net.sourceforge.pmd.lang.java.ast
import io.kotlintest.AbstractSpec
import io.kotlintest.TestContext
import io.kotlintest.TestType
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.kotlintest.should as kotlintestShould
import io.kotest.matchers.should as kotlintestShould
/**
* Base class for grammar tests that use the DSL. Tests are layered into
@ -14,14 +21,23 @@ import io.kotlintest.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,
@ -34,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.
@ -53,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)
/**
@ -74,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
)
}
@ -119,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

@ -4,7 +4,7 @@
package net.sourceforge.pmd.lang.java.ast
import io.kotlintest.shouldBe
import io.kotest.matchers.shouldBe
/**
* @author Clément Fournier

View File

@ -1,6 +1,6 @@
package net.sourceforge.pmd.lang.java.ast
import io.kotlintest.shouldBe
import io.kotest.matchers.shouldBe
class WildcardBoundsTest : ParserTestSpec({

View File

@ -105,13 +105,14 @@
of the pmd-lang-test module
-->
<dependency>
<groupId>io.kotlintest</groupId>
<artifactId>kotlintest-assertions</artifactId>
<groupId>io.kotest</groupId>
<artifactId>kotest-assertions-core-jvm</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<!-- Contains stuff like FunSpec, etc -->
<groupId>io.kotest</groupId>
<artifactId>kotest-runner-junit5-jvm</artifactId>
<scope>compile</scope>
</dependency>

View File

@ -4,7 +4,7 @@
package net.sourceforge.pmd.cpd.test
import io.kotlintest.shouldThrow
import io.kotest.assertions.throwables.shouldThrow
import net.sourceforge.pmd.cpd.SourceCode
import net.sourceforge.pmd.cpd.TokenEntry
import net.sourceforge.pmd.cpd.Tokenizer

View File

@ -26,7 +26,7 @@ object NodeTreeLikeAdapter : DoublyLinkedTreeLikeAdapter<Node> {
/** A subtree matcher written in the DSL documented on [TreeNodeWrapper]. */
typealias NodeSpec<N> = TreeNodeWrapper<Node, N>.() -> Unit
/** A function feedable to [io.kotlintest.should], which fails the test if an [AssertionError] is thrown. */
/** A function feedable to [io.kotest.matchers.should], which fails the test if an [AssertionError] is thrown. */
typealias Assertions<M> = (M) -> Unit
/** A shorthand for [baseShouldMatchSubtree] providing the [NodeTreeLikeAdapter]. */
@ -37,7 +37,7 @@ inline fun <reified N : Node> Node?.shouldMatchNode(ignoreChildren: Boolean = fa
/**
* Returns [an assertion function][Assertions] asserting that its parameter conforms to the given [NodeSpec].
*
* Use it with [io.kotlintest.should], e.g. `node should matchNode<ASTExpression> {}`.
* Use it with [io.kotest.matchers.should], e.g. `node should matchNode<ASTExpression> {}`.
*
* See also the samples on [TreeNodeWrapper].
*
@ -50,7 +50,7 @@ inline fun <reified N : Node> Node?.shouldMatchNode(ignoreChildren: Boolean = fa
* Assertions may consist of [NWrapper.child] calls, which perform the same type of node
* matching on a child of the tested node.
*
* @return A matcher for AST nodes, suitable for use by [io.kotlintest.should].
* @return A matcher for AST nodes, suitable for use by [io.kotest.matchers.should].
*/
inline fun <reified N : Node> matchNode(ignoreChildren: Boolean = false, noinline nodeSpec: NodeSpec<N>)
: Assertions<Node?> = { it.shouldMatchNode(ignoreChildren, nodeSpec) }

View File

@ -4,10 +4,10 @@
package net.sourceforge.pmd.lang.ast.test
import io.kotlintest.should
import io.kotest.matchers.should
import kotlin.reflect.KCallable
import kotlin.reflect.jvm.isAccessible
import io.kotlintest.shouldBe as ktShouldBe
import io.kotest.matchers.shouldBe as ktShouldBe
/**
* Extension to add the name of a property to error messages.
@ -48,7 +48,7 @@ private fun <N, V> assertWrapper(callable: KCallable<N>, right: V, asserter: (N,
* have to use the name of the getter instead of that of the generated
* property (with the get prefix).
*
* If this conflicts with [io.kotlintest.shouldBe], use the equivalent [shouldEqual]
* If this conflicts with [io.kotest.matchers.shouldBe], use the equivalent [shouldEqual]
*
*/
infix fun <N, V : N> KCallable<N>.shouldBe(expected: V?) = this.shouldEqual(expected)

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>
@ -120,13 +120,8 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.kotlintest</groupId>
<artifactId>kotlintest-assertions</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.kotlintest</groupId>
<artifactId>kotlintest-core</artifactId>
<groupId>io.kotest</groupId>
<artifactId>kotest-assertions-core-jvm</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

View File

@ -4,16 +4,16 @@
package net.sourceforge.pmd.lang.modelica.ast
import io.kotlintest.should
import io.kotlintest.shouldBe
import io.kotlintest.specs.AbstractFunSpec
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.should
import io.kotest.matchers.shouldBe
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

@ -140,13 +140,8 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.kotlintest</groupId>
<artifactId>kotlintest-assertions</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.kotlintest</groupId>
<artifactId>kotlintest-core</artifactId>
<groupId>io.kotest</groupId>
<artifactId>kotest-assertions-core-jvm</artifactId>
<scope>test</scope>
</dependency>
<dependency>
@ -156,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.kotlintest.should
import io.kotlintest.specs.AbstractFunSpec
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.should
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") {

35
pom.xml
View File

@ -85,8 +85,8 @@
<kotlin.compiler.jvmTarget>${maven.compiler.test.target}</kotlin.compiler.jvmTarget>
<kotlin.version>1.3.0</kotlin.version>
<kotlintest.version>3.1.8</kotlintest.version>
<kotlin.version>1.3.72</kotlin.version>
<kotest.version>4.1.2</kotest.version>
<dokka.version>0.10.1</dokka.version>
@ -269,9 +269,9 @@
</dependency>
<!-- Junit5 Platform Engine for Kotlin Tests -->
<dependency>
<groupId>io.kotlintest</groupId>
<artifactId>kotlintest-runner-junit5</artifactId>
<version>${kotlintest.version}</version>
<groupId>io.kotest</groupId>
<artifactId>kotest-runner-junit5-jvm</artifactId>
<version>${kotest.version}</version>
</dependency>
</dependencies>
</plugin>
@ -827,22 +827,21 @@
<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.kotlintest</groupId>
<artifactId>kotlintest-assertions</artifactId>
<version>${kotlintest.version}</version>
<groupId>io.kotest</groupId>
<artifactId>kotest-runner-junit5-jvm</artifactId>
<version>${kotest.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.kotlintest</groupId>
<artifactId>kotlintest-core</artifactId>
<version>${kotlintest.version}</version>
<groupId>io.kotest</groupId>
<artifactId>kotest-assertions-core-jvm</artifactId>
<version>${kotest.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.kotest</groupId>
<artifactId>kotest-property-jvm</artifactId>
<version>${kotest.version}</version>
<scope>test</scope>
</dependency>