From 387e794116adcc63c2872e1de50a578d6871fa39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Mon, 24 Sep 2018 15:01:29 +0200 Subject: [PATCH] Split pmd-test to isolate kotlin dependencies --- pmd-java/pom.xml | 7 + .../pmd/lang/java/ast/KotlinTestingDsl.kt | 180 +++++++++--------- pmd-lang-test/pom.xml | 94 +++++++++ .../pmd/lang/ast/test/AstMatcherDsl.kt | 0 .../pmd/lang/ast/test/NodeExtensions.kt | 0 .../sourceforge/pmd/lang/ast/test/DslTest.kt | 0 .../pmd/lang/ast/test/ParseUtils.kt | 0 .../pmd/lang/ast/test/TestUtils.kt | 0 pmd-test/pom.xml | 63 ------ .../fxdesigner/util/controls/ASTTreeCell.java | 2 +- pom.xml | 9 + 11 files changed, 201 insertions(+), 154 deletions(-) create mode 100644 pmd-lang-test/pom.xml rename {pmd-test => pmd-lang-test}/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/AstMatcherDsl.kt (100%) rename {pmd-test => pmd-lang-test}/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/NodeExtensions.kt (100%) rename {pmd-test => pmd-lang-test}/src/test/kotlin/net/sourceforge/pmd/lang/ast/test/DslTest.kt (100%) rename {pmd-test => pmd-lang-test}/src/test/kotlin/net/sourceforge/pmd/lang/ast/test/ParseUtils.kt (100%) rename {pmd-test => pmd-lang-test}/src/test/kotlin/net/sourceforge/pmd/lang/ast/test/TestUtils.kt (100%) diff --git a/pmd-java/pom.xml b/pmd-java/pom.xml index 4ceab06d20..04f71f0d98 100644 --- a/pmd-java/pom.xml +++ b/pmd-java/pom.xml @@ -144,6 +144,13 @@ runtime + + + + net.sourceforge.pmd + pmd-lang-test + test + net.sourceforge.pmd pmd-test diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/KotlinTestingDsl.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/KotlinTestingDsl.kt index 4d6973b759..4eba60b657 100644 --- a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/KotlinTestingDsl.kt +++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/KotlinTestingDsl.kt @@ -1,4 +1,3 @@ - package net.sourceforge.pmd.lang.java.ast import io.kotlintest.Matcher @@ -146,7 +145,7 @@ data class ParserTestCtx(val javaVersion: JavaVersion = JavaVersion.Latest, */ inline fun matchExpr(ignoreChildren: Boolean = false, noinline nodeSpec: NWrapper.() -> Unit): Matcher = - makeMatcher(ExpressionParsingCtx(), ignoreChildren, nodeSpec) + makeMatcher(ExpressionParsingCtx(this), ignoreChildren, nodeSpec) /** * Returns a String matcher that parses the node using [parseStatement] with @@ -154,7 +153,7 @@ data class ParserTestCtx(val javaVersion: JavaVersion = JavaVersion.Latest, */ inline fun matchStmt(ignoreChildren: Boolean = false, noinline nodeSpec: NWrapper.() -> Unit) = - makeMatcher(StatementParsingCtx(), ignoreChildren, nodeSpec) + makeMatcher(StatementParsingCtx(this), ignoreChildren, nodeSpec) /** @@ -163,8 +162,7 @@ data class ParserTestCtx(val javaVersion: JavaVersion = JavaVersion.Latest, */ inline fun matchType(ignoreChildren: Boolean = false, noinline nodeSpec: NWrapper.() -> Unit) = - makeMatcher(TypeParsingCtx(), ignoreChildren, nodeSpec) - + makeMatcher(TypeParsingCtx(this), ignoreChildren, nodeSpec) /** @@ -180,98 +178,100 @@ data class ParserTestCtx(val javaVersion: JavaVersion = JavaVersion.Latest, } - fun parseAstExpression(expr: String): ASTExpression = ExpressionParsingCtx().parseNode(expr) + fun parseAstExpression(expr: String): ASTExpression = ExpressionParsingCtx(this).parseNode(expr) - fun parseAstStatement(statement: String): ASTBlockStatement = StatementParsingCtx().parseNode(statement) + fun parseAstStatement(statement: String): ASTBlockStatement = StatementParsingCtx(this).parseNode(statement) - fun parseAstType(type: String): ASTType = TypeParsingCtx().parseNode(type) + fun parseAstType(type: String): ASTType = TypeParsingCtx(this).parseNode(type) - inline fun parseExpression(expr: String): N = ExpressionParsingCtx().parseAndFind(expr) + inline fun parseExpression(expr: String): N = ExpressionParsingCtx(this).parseAndFind(expr) // don't forget the semicolon - inline fun parseStatement(stmt: String): N = StatementParsingCtx().parseAndFind(stmt) + inline fun parseStatement(stmt: String): N = StatementParsingCtx(this).parseAndFind(stmt) - inline fun parseType(type: String): N = TypeParsingCtx().parseAndFind(type) + inline fun parseType(type: String): N = TypeParsingCtx(this).parseAndFind(type) + companion object { - /** - * Finds the first descendant of type [N] of [this] node which is - * accessible in a straight line. The descendant must be accessible - * from the [this] on a path where each node has a single child. - * - * If one node has another child, the search is aborted and the method - * returns null. - */ - fun Node.findFirstNodeOnStraightLine(klass: Class): N? { - return when { - klass.isInstance(this) -> { - @Suppress("UNCHECKED_CAST") - val n = this as N - n + + /** + * Finds the first descendant of type [N] of [this] node which is + * accessible in a straight line. The descendant must be accessible + * from the [this] on a path where each node has a single child. + * + * If one node has another child, the search is aborted and the method + * returns null. + */ + fun Node.findFirstNodeOnStraightLine(klass: Class): N? { + return when { + klass.isInstance(this) -> { + @Suppress("UNCHECKED_CAST") + val n = this as N + n + } + this.numChildren == 1 -> getChild(0).findFirstNodeOnStraightLine(klass) + else -> null } - this.numChildren == 1 -> getChild(0).findFirstNodeOnStraightLine(klass) - else -> null - } - } - - - /** - * Describes a kind of node that can be found commonly in the same contexts. - * This type defines some machinery to parse a string to this kind of node - * without much ado by placing it in a specific parsing context. - */ - abstract inner class NodeParsingCtx(val constructName: String) { - - abstract fun getTemplate(construct: String): String - - abstract fun retrieveNode(acu: ASTCompilationUnit): T - - /** - * Parse the string in the context described by this object. The parsed node is usually - * the child of the returned [T] node. Note that [parseAndFind] can save you some keystrokes - * because it finds a descendant of the wanted type. - * - * @param construct The construct to parse - * - * @return A [T] whose child is the given statement - * - * @throws ParseException If the argument is no valid construct of this kind (mind the language version) - */ - fun parseNode(construct: String): T { - val root = ParserTstUtil.parseAndTypeResolveJava(javaVersion.pmdName, getTemplate(construct)) - - return retrieveNode(root) } /** - * Parse the string the context described by this object, and finds the first descendant of type [N]. - * The descendant is searched for by [findFirstNodeOnStraightLine], to prevent accidental - * mis-selection of a node. In such a case, a [NoSuchElementException] is thrown, and you - * should fix your test case. - * - * @param construct The construct to parse - * @param N The type of node to find - * - * @return The first descendant of type [N] found in the parsed expression - * - * @throws NoSuchElementException If no node of type [N] is found by [findFirstNodeOnStraightLine] - * @throws ParseException If the argument is no valid construct of this kind - * + * Describes a kind of node that can be found commonly in the same contexts. + * This type defines some machinery to parse a string to this kind of node + * without much ado by placing it in a specific parsing context. */ - inline fun parseAndFind(construct: String): N = - parseNode(construct).findFirstNodeOnStraightLine(N::class.java) - ?: throw NoSuchElementException("No node of type ${N::class.java.simpleName} in the given $constructName:\n\t$construct") + abstract class NodeParsingCtx(val constructName: String, protected val ctx: ParserTestCtx) { - } + abstract fun getTemplate(construct: String): String - inner class ExpressionParsingCtx : NodeParsingCtx("expression") { + abstract fun retrieveNode(acu: ASTCompilationUnit): T - override fun getTemplate(construct: String): String = + /** + * Parse the string in the context described by this object. The parsed node is usually + * the child of the returned [T] node. Note that [parseAndFind] can save you some keystrokes + * because it finds a descendant of the wanted type. + * + * @param construct The construct to parse + * + * @return A [T] whose child is the given statement + * + * @throws ParseException If the argument is no valid construct of this kind (mind the language version) + */ + fun parseNode(construct: String): T { + val root = ParserTstUtil.parseAndTypeResolveJava(ctx.javaVersion.pmdName, getTemplate(construct)) + + return retrieveNode(root) + } + + /** + * Parse the string the context described by this object, and finds the first descendant of type [N]. + * The descendant is searched for by [findFirstNodeOnStraightLine], to prevent accidental + * mis-selection of a node. In such a case, a [NoSuchElementException] is thrown, and you + * should fix your test case. + * + * @param construct The construct to parse + * @param N The type of node to find + * + * @return The first descendant of type [N] found in the parsed expression + * + * @throws NoSuchElementException If no node of type [N] is found by [findFirstNodeOnStraightLine] + * @throws ParseException If the argument is no valid construct of this kind + * + */ + inline fun parseAndFind(construct: String): N = + parseNode(construct).findFirstNodeOnStraightLine(N::class.java) + ?: throw NoSuchElementException("No node of type ${N::class.java.simpleName} in the given $constructName:\n\t$construct") + + } + + + class ExpressionParsingCtx(ctx: ParserTestCtx) : NodeParsingCtx("expression", ctx) { + + override fun getTemplate(construct: String): String = """ - ${imports.joinToString(separator = "\n")} + ${ctx.imports.joinToString(separator = "\n")} class Foo { { Object o = $construct; @@ -280,14 +280,14 @@ data class ParserTestCtx(val javaVersion: JavaVersion = JavaVersion.Latest, """.trimIndent() - override fun retrieveNode(acu: ASTCompilationUnit): ASTExpression = acu.getFirstDescendantOfType(ASTVariableInitializer::class.java).getChild(0) as ASTExpression - } + override fun retrieveNode(acu: ASTCompilationUnit): ASTExpression = acu.getFirstDescendantOfType(ASTVariableInitializer::class.java).getChild(0) as ASTExpression + } - inner class StatementParsingCtx : NodeParsingCtx("statement") { + class StatementParsingCtx(ctx: ParserTestCtx) : NodeParsingCtx("statement", ctx) { - override fun getTemplate(construct: String): String = + override fun getTemplate(construct: String): String = """ - ${imports.joinToString(separator = "\n")} + ${ctx.imports.joinToString(separator = "\n")} class Foo { { $construct @@ -296,21 +296,21 @@ data class ParserTestCtx(val javaVersion: JavaVersion = JavaVersion.Latest, """.trimIndent() - override fun retrieveNode(acu: ASTCompilationUnit): ASTBlockStatement = acu.getFirstDescendantOfType(ASTBlockStatement::class.java) - } + override fun retrieveNode(acu: ASTCompilationUnit): ASTBlockStatement = acu.getFirstDescendantOfType(ASTBlockStatement::class.java) + } - inner class TypeParsingCtx : NodeParsingCtx("type") { - override fun getTemplate(construct: String): String = + class TypeParsingCtx(ctx: ParserTestCtx) : NodeParsingCtx("type", ctx) { + override fun getTemplate(construct: String): String = """ - ${imports.joinToString(separator = "\n")} + ${ctx.imports.joinToString(separator = "\n")} class Foo { $construct foo; } """.trimIndent() - override fun retrieveNode(acu: ASTCompilationUnit): ASTType = acu.getFirstDescendantOfType(ASTType::class.java) + override fun retrieveNode(acu: ASTCompilationUnit): ASTType = acu.getFirstDescendantOfType(ASTType::class.java) + } + } - - } diff --git a/pmd-lang-test/pom.xml b/pmd-lang-test/pom.xml new file mode 100644 index 0000000000..e7b6f1305e --- /dev/null +++ b/pmd-lang-test/pom.xml @@ -0,0 +1,94 @@ + + + + 4.0.0 + pmd-lang-test + PMD language module testing utilities + + Module containing utilities to test language implementations, + including parsers and ASTs. This module uses Kotlin. + + + + pmd + net.sourceforge.pmd + 6.8.0-SNAPSHOT + + + + + + + kotlin-maven-plugin + org.jetbrains.kotlin + ${kotlin.version} + + + kotlin-compile + + compile + + process-sources + + + ${project.basedir}/src/main/kotlin + ${project.basedir}/src/main/java + + + + + + + + + + + + + junit + junit + + + + net.sourceforge.pmd + pmd-core + + + + org.jetbrains.kotlin + kotlin-stdlib + ${kotlin.version} + compile + + + + org.jetbrains.kotlin + kotlin-stdlib-jdk8 + ${kotlin.version} + compile + + + + org.jetbrains.kotlin + kotlin-test-junit + ${kotlin.version} + compile + + + + io.kotlintest + kotlintest-runner-junit5 + 3.1.8 + compile + + + + + net.sourceforge.pmd + pmd-java + 6.7.0 + test + + + diff --git a/pmd-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/AstMatcherDsl.kt b/pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/AstMatcherDsl.kt similarity index 100% rename from pmd-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/AstMatcherDsl.kt rename to pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/AstMatcherDsl.kt diff --git a/pmd-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/NodeExtensions.kt b/pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/NodeExtensions.kt similarity index 100% rename from pmd-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/NodeExtensions.kt rename to pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/NodeExtensions.kt diff --git a/pmd-test/src/test/kotlin/net/sourceforge/pmd/lang/ast/test/DslTest.kt b/pmd-lang-test/src/test/kotlin/net/sourceforge/pmd/lang/ast/test/DslTest.kt similarity index 100% rename from pmd-test/src/test/kotlin/net/sourceforge/pmd/lang/ast/test/DslTest.kt rename to pmd-lang-test/src/test/kotlin/net/sourceforge/pmd/lang/ast/test/DslTest.kt diff --git a/pmd-test/src/test/kotlin/net/sourceforge/pmd/lang/ast/test/ParseUtils.kt b/pmd-lang-test/src/test/kotlin/net/sourceforge/pmd/lang/ast/test/ParseUtils.kt similarity index 100% rename from pmd-test/src/test/kotlin/net/sourceforge/pmd/lang/ast/test/ParseUtils.kt rename to pmd-lang-test/src/test/kotlin/net/sourceforge/pmd/lang/ast/test/ParseUtils.kt diff --git a/pmd-test/src/test/kotlin/net/sourceforge/pmd/lang/ast/test/TestUtils.kt b/pmd-lang-test/src/test/kotlin/net/sourceforge/pmd/lang/ast/test/TestUtils.kt similarity index 100% rename from pmd-test/src/test/kotlin/net/sourceforge/pmd/lang/ast/test/TestUtils.kt rename to pmd-lang-test/src/test/kotlin/net/sourceforge/pmd/lang/ast/test/TestUtils.kt diff --git a/pmd-test/pom.xml b/pmd-test/pom.xml index 6a3b78228d..873e8a371b 100644 --- a/pmd-test/pom.xml +++ b/pmd-test/pom.xml @@ -38,68 +38,5 @@ 1.10.19 test - - - org.jetbrains.kotlin - kotlin-stdlib - ${kotlin.version} - compile - - - - org.jetbrains.kotlin - kotlin-stdlib-jdk8 - ${kotlin.version} - compile - - - - org.jetbrains.kotlin - kotlin-test-junit - ${kotlin.version} - compile - - - - io.kotlintest - kotlintest-runner-junit5 - 3.1.8 - compile - - - - - net.sourceforge.pmd - pmd-java - 6.6.0 - test - - - - - - - - kotlin-maven-plugin - org.jetbrains.kotlin - ${kotlin.version} - - - kotlin-compile - - compile - - process-sources - - - ${project.basedir}/src/main/kotlin - ${project.basedir}/src/main/java - - - - - - - diff --git a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/controls/ASTTreeCell.java b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/controls/ASTTreeCell.java index e254927433..e8afbcd7d5 100644 --- a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/controls/ASTTreeCell.java +++ b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/controls/ASTTreeCell.java @@ -111,7 +111,7 @@ public class ASTTreeCell extends TreeCell { for (int i = 0; i < node.jjtGetNumChildren(); i++) { Node child = node.jjtGetChild(i); - if (value == child) { + if (value.equals(child)) { // The array contains name of corresponding properties childrenProps[i] = prop.getName(); break; diff --git a/pom.xml b/pom.xml index 0d067a58db..1857f0f7d9 100644 --- a/pom.xml +++ b/pom.xml @@ -939,6 +939,14 @@ Additionally it includes CPD, the copy-paste-detector. CPD finds duplicated code + + + net.sourceforge.pmd + pmd-lang-test + ${project.version} + test + + @@ -1161,5 +1169,6 @@ Additionally it includes CPD, the copy-paste-detector. CPD finds duplicated code pmd-java8 pmd-ui pmd-doc + pmd-lang-test