From ee27db8c601f3e3b5d7950799b5bcb27f8a601f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Wed, 22 May 2019 19:55:36 +0200 Subject: [PATCH] Add type alias to shorten signatures --- .../pmd/lang/java/ast/ASTLiteralTest.kt | 32 ++++++------ .../pmd/lang/java/ast/ParserTestSpec.kt | 7 +-- .../pmd/lang/java/ast/TestExtensions.kt | 51 ++++++++++--------- .../pmd/lang/ast/test/AstMatcherDslAdapter.kt | 16 ++++-- 4 files changed, 58 insertions(+), 48 deletions(-) diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTLiteralTest.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTLiteralTest.kt index d0d1346d7a..0246f1ce2c 100644 --- a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTLiteralTest.kt +++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTLiteralTest.kt @@ -1,9 +1,8 @@ package net.sourceforge.pmd.lang.java.ast -import com.github.oowekyala.treeutils.matchers.TreeNodeWrapper import io.kotlintest.shouldBe -import net.sourceforge.pmd.lang.ast.Node import net.sourceforge.pmd.lang.ast.test.NodeSpec +import net.sourceforge.pmd.lang.ast.test.ValuedNodeSpec import net.sourceforge.pmd.lang.ast.test.shouldBe import net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType.PrimitiveType import net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType.PrimitiveType.* @@ -270,7 +269,7 @@ class ASTLiteralTest : ParserTestSpec({ inContext(ExpressionParsingCtx) { - fun hex15(type: PrimitiveType): TreeNodeWrapper.() -> ASTNumericLiteral = { + fun hex15(type: PrimitiveType): ValuedNodeSpec<*, ASTNumericLiteral> = { number(type) { it::getValueAsDouble shouldBe 15.0 it::getValueAsFloat shouldBe 15f @@ -279,21 +278,24 @@ class ASTLiteralTest : ParserTestSpec({ } } - "0x0fl" should parseAs(hex15(LONG)) - "0x0fL" should parseAs(hex15(LONG)) - "0X0fl" should parseAs(hex15(LONG)) - "0X0fL" should parseAs(hex15(LONG)) - "0X0FL" should parseAs(hex15(LONG)) - "0x0f" should parseAs(hex15(INT)) - "0x0F" should parseAs(hex15(INT)) - "0X0f" should parseAs(hex15(INT)) - "0X0F" should parseAs(hex15(INT)) - "0x0_0__0f" should parseAs(hex15(INT)) - "0x0_0__0F" should parseAs(hex15(INT)) + val hex15l = hex15(LONG) + val hex15i = hex15(INT) + + "0x0fl" should parseAs(hex15l) + "0x0fL" should parseAs(hex15l) + "0X0fl" should parseAs(hex15l) + "0X0fL" should parseAs(hex15l) + "0X0FL" should parseAs(hex15l) + "0x0f" should parseAs(hex15i) + "0x0F" should parseAs(hex15i) + "0X0f" should parseAs(hex15i) + "0X0F" should parseAs(hex15i) + "0x0_0__0f" should parseAs(hex15i) + "0x0_0__0F" should parseAs(hex15i) "-0X0000_000f" should parseAs { unaryExpr(UnaryOp.UNARY_MINUS) { - hex15(INT)() + hex15i() } } } diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ParserTestSpec.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ParserTestSpec.kt index 1ba2428bbd..291a334d35 100644 --- a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ParserTestSpec.kt +++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ParserTestSpec.kt @@ -4,10 +4,7 @@ import com.github.oowekyala.treeutils.matchers.TreeNodeWrapper import io.kotlintest.* import io.kotlintest.specs.IntelliMarker import net.sourceforge.pmd.lang.ast.Node -import net.sourceforge.pmd.lang.ast.test.Assertions -import net.sourceforge.pmd.lang.ast.test.matchNode -import net.sourceforge.pmd.lang.ast.test.numChildren -import net.sourceforge.pmd.lang.ast.test.parent +import net.sourceforge.pmd.lang.ast.test.* import io.kotlintest.should as kotlintestShould /** @@ -149,7 +146,7 @@ abstract class ParserTestSpec(body: ParserTestSpec.() -> Unit) : AbstractSpec(), */ fun parse(): Matcher = this@VersionedTestCtx.parseIn(nodeParsingCtx) - fun parseAs(matcher: TreeNodeWrapper.() -> Any): Assertions = { str -> + fun parseAs(matcher: ValuedNodeSpec): Assertions = { str -> val node = nodeParsingCtx.parseNode(str, this@VersionedTestCtx) val idx = node.jjtGetChildIndex() node.parent kotlintestShould matchNode { diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/TestExtensions.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/TestExtensions.kt index e7c29829c1..0e1a5b2976 100644 --- a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/TestExtensions.kt +++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/TestExtensions.kt @@ -2,6 +2,8 @@ package net.sourceforge.pmd.lang.java.ast import com.github.oowekyala.treeutils.matchers.TreeNodeWrapper import net.sourceforge.pmd.lang.ast.Node +import net.sourceforge.pmd.lang.ast.test.NodeSpec +import net.sourceforge.pmd.lang.ast.test.ValuedNodeSpec import net.sourceforge.pmd.lang.ast.test.shouldBe import net.sourceforge.pmd.lang.ast.test.shouldMatch import net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType.PrimitiveType.* @@ -31,18 +33,19 @@ fun String.addArticle() = when (this[0].toLowerCase()) { else -> "a $this" } -fun TreeNodeWrapper.annotation(spec: TreeNodeWrapper.() -> Unit = EmptyAssertions) = + +fun TreeNodeWrapper.annotation(spec: ValuedNodeSpec = EmptyAssertions) = child(ignoreChildren = spec == EmptyAssertions, nodeSpec = spec) -fun TreeNodeWrapper.annotation(name: String, spec: TreeNodeWrapper.() -> Unit = EmptyAssertions) = +fun TreeNodeWrapper.annotation(name: String, spec: NodeSpec = EmptyAssertions) = child(ignoreChildren = spec == EmptyAssertions) { it::getAnnotationName shouldBe name spec() } -fun TreeNodeWrapper.enumConstant(name: String, spec: TreeNodeWrapper.() -> Unit = EmptyAssertions) = +fun TreeNodeWrapper.enumConstant(name: String, spec: NodeSpec = EmptyAssertions) = child { it::getName shouldBe name spec() @@ -54,7 +57,7 @@ fun TreeNodeWrapper.variableId(name: String, otherAssertions: (ASTVaria otherAssertions(it) } -fun TreeNodeWrapper.variableDeclarator(name: String, spec: TreeNodeWrapper.() -> Unit = EmptyAssertions) = +fun TreeNodeWrapper.variableDeclarator(name: String, spec: NodeSpec = EmptyAssertions) = child { it::getVariableId shouldBe variableId(name) spec() @@ -66,13 +69,13 @@ fun TreeNodeWrapper.variableRef(name: String, otherAssertions: (ASTVari it::getVariableName shouldBe name otherAssertions(it) } -fun TreeNodeWrapper.fieldAccess(name: String, otherAssertions: TreeNodeWrapper.() -> Unit) = +fun TreeNodeWrapper.fieldAccess(name: String, otherAssertions: NodeSpec) = child { it::getFieldName shouldBe name otherAssertions() } -fun TreeNodeWrapper.parenthesized(inside: TreeNodeWrapper.() -> ASTExpression) = +fun TreeNodeWrapper.parenthesized(inside: ValuedNodeSpec) = child { it::getWrappedExpression shouldBe inside() } @@ -93,16 +96,16 @@ fun TreeNodeWrapper.unaryExpr(op: UnaryOp, baseExpr: TreeNodeWrapper.postfixExpr(op: UnaryOp, baseExpr: TreeNodeWrapper.() -> ASTPrimaryExpression) = +fun TreeNodeWrapper.postfixExpr(op: UnaryOp, baseExpr: ValuedNodeSpec) = child { it::getOp shouldBe op it::getBaseExpression shouldBe baseExpr() } -fun TreeNodeWrapper.typeParamList(contents: TreeNodeWrapper.() -> Unit) = +fun TreeNodeWrapper.typeParamList(contents: NodeSpec) = child(nodeSpec = contents) -fun TreeNodeWrapper.typeArgList(contents: TreeNodeWrapper.() -> Unit = EmptyAssertions) = +fun TreeNodeWrapper.typeArgList(contents: NodeSpec = EmptyAssertions) = child(ignoreChildren = contents == EmptyAssertions, nodeSpec = contents) fun TreeNodeWrapper.diamond() = @@ -110,24 +113,24 @@ fun TreeNodeWrapper.diamond() = it::isDiamond shouldBe true } -fun TreeNodeWrapper.block(contents: TreeNodeWrapper.() -> Unit = EmptyAssertions) = +fun TreeNodeWrapper.block(contents: NodeSpec = EmptyAssertions) = child(ignoreChildren = contents == EmptyAssertions) { contents() } -fun TreeNodeWrapper.fieldDecl(contents: TreeNodeWrapper.() -> Unit) = +fun TreeNodeWrapper.fieldDecl(contents: NodeSpec) = child { contents() } -fun TreeNodeWrapper.typeParam(name: String, contents: TreeNodeWrapper.() -> ASTType? = { null }) = +fun TreeNodeWrapper.typeParam(name: String, contents: ValuedNodeSpec = { null }) = child { it::getParameterName shouldBe name it::getTypeBoundNode shouldBe contents() } -fun TreeNodeWrapper.classType(simpleName: String, contents: TreeNodeWrapper.() -> Unit = EmptyAssertions) = +fun TreeNodeWrapper.classType(simpleName: String, contents: NodeSpec = EmptyAssertions) = child(ignoreChildren = contents == EmptyAssertions) { it::getSimpleName shouldBe simpleName contents() @@ -141,63 +144,63 @@ fun TreeNodeWrapper.primitiveType(type: ASTPrimitiveType.PrimitiveType) } -fun TreeNodeWrapper.castExpr(contents: TreeNodeWrapper.() -> Unit) = +fun TreeNodeWrapper.castExpr(contents: NodeSpec) = child { contents() } -fun TreeNodeWrapper.stringLit(image: String, contents: TreeNodeWrapper.() -> Unit = EmptyAssertions) = +fun TreeNodeWrapper.stringLit(image: String, contents: NodeSpec = EmptyAssertions) = child { it::getImage shouldBe image contents() } -fun TreeNodeWrapper.ambiguousName(image: String, contents: TreeNodeWrapper.() -> Unit = EmptyAssertions) = +fun TreeNodeWrapper.ambiguousName(image: String, contents: NodeSpec = EmptyAssertions) = child { it::getImage shouldBe image contents() } -fun TreeNodeWrapper.memberValuePair(name: String, contents: TreeNodeWrapper.() -> ASTMemberValue) = +fun TreeNodeWrapper.memberValuePair(name: String, contents: ValuedNodeSpec) = child { it::getMemberName shouldBe name it::getMemberValue shouldBe contents() } -fun TreeNodeWrapper.additiveExpr(op: BinaryOp, assertions: TreeNodeWrapper.() -> Unit) = +fun TreeNodeWrapper.additiveExpr(op: BinaryOp, assertions: NodeSpec) = child { it::getOp shouldBe op assertions() } -fun TreeNodeWrapper.equalityExpr(op: BinaryOp, assertions: TreeNodeWrapper.() -> Unit) = +fun TreeNodeWrapper.equalityExpr(op: BinaryOp, assertions: NodeSpec) = child { it::getOp shouldBe op assertions() } -fun TreeNodeWrapper.andExpr(assertions: TreeNodeWrapper.() -> Unit) = +fun TreeNodeWrapper.andExpr(assertions: NodeSpec) = child { assertions() } -fun TreeNodeWrapper.multiplicativeExpr(op: BinaryOp, assertions: TreeNodeWrapper.() -> Unit) = +fun TreeNodeWrapper.multiplicativeExpr(op: BinaryOp, assertions: NodeSpec) = child { it::getOp shouldBe op assertions() } -private val EmptyAssertions: TreeNodeWrapper.() -> Unit = {} +private val EmptyAssertions: NodeSpec = {} -fun TreeNodeWrapper.switchExpr(assertions: TreeNodeWrapper.() -> Unit = EmptyAssertions): ASTSwitchExpression = +fun TreeNodeWrapper.switchExpr(assertions: NodeSpec = EmptyAssertions): ASTSwitchExpression = child(ignoreChildren = assertions == EmptyAssertions) { assertions() } -fun TreeNodeWrapper.number(primitiveType: ASTPrimitiveType.PrimitiveType? = null, assertions: TreeNodeWrapper.() -> Unit = EmptyAssertions) = +fun TreeNodeWrapper.number(primitiveType: ASTPrimitiveType.PrimitiveType? = null, assertions: NodeSpec = EmptyAssertions) = child { if (primitiveType != null) { it::getPrimitiveType shouldBe primitiveType diff --git a/pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/AstMatcherDslAdapter.kt b/pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/AstMatcherDslAdapter.kt index fc9d2ac255..f4a9ca95bd 100644 --- a/pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/AstMatcherDslAdapter.kt +++ b/pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/AstMatcherDslAdapter.kt @@ -20,12 +20,20 @@ object NodeTreeLikeAdapter : DoublyLinkedTreeLikeAdapter { override fun getParent(node: Node): Node? = node.parent } +/** A [NodeSpec] that returns a value. */ +typealias ValuedNodeSpec = TreeNodeWrapper.() -> O + /** A subtree matcher written in the DSL documented on [TreeNodeWrapper]. */ -typealias NodeSpec = TreeNodeWrapper.() -> Unit +typealias NodeSpec = ValuedNodeSpec /** A function feedable to [io.kotlintest.should], which fails the test if an [AssertionError] is thrown. */ typealias Assertions = (M) -> Unit +fun ValuedNodeSpec.ignoreResult(): NodeSpec { + val me = this + return { this.me() } +} + val DefaultMatchingConfig = MatchingConfig( adapter = NodeTreeLikeAdapter, errorPrinter = KotlintestBeanTreePrinter(NodeTreeLikeAdapter), @@ -33,8 +41,8 @@ val DefaultMatchingConfig = MatchingConfig( ) /** A shorthand for [baseShouldMatchSubtree] providing the [NodeTreeLikeAdapter]. */ -inline fun Node?.shouldMatchNode(ignoreChildren: Boolean = false, noinline nodeSpec: NodeSpec) { - this.baseShouldMatchSubtree(DefaultMatchingConfig, ignoreChildren, nodeSpec) +inline fun Node?.shouldMatchNode(ignoreChildren: Boolean = false, noinline nodeSpec: ValuedNodeSpec) { + this.baseShouldMatchSubtree(DefaultMatchingConfig, ignoreChildren, nodeSpec.ignoreResult()) } /** @@ -55,5 +63,5 @@ inline fun Node?.shouldMatchNode(ignoreChildren: Boolean = fa * * @return A matcher for AST nodes, suitable for use by [io.kotlintest.should]. */ -inline fun matchNode(ignoreChildren: Boolean = false, noinline nodeSpec: NodeSpec) +inline fun matchNode(ignoreChildren: Boolean = false, noinline nodeSpec: ValuedNodeSpec) : Assertions = { it.shouldMatchNode(ignoreChildren, nodeSpec) }