From 4c29d2dbc7104c3afa013961419ceaeb839c0ec2 Mon Sep 17 00:00:00 2001 From: Brian Norman Date: Fri, 25 Sep 2020 12:21:04 -0500 Subject: [PATCH] Allow constants expression as argument to assertion function --- .../com/bnorm/power/PowerAssertCallTransformer.kt | 7 ++++--- .../src/test/kotlin/com/bnorm/power/test.kt | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/kotlin-power-assert-plugin/src/main/kotlin/com/bnorm/power/PowerAssertCallTransformer.kt b/kotlin-power-assert-plugin/src/main/kotlin/com/bnorm/power/PowerAssertCallTransformer.kt index 9558934f06a..d24fdd01194 100644 --- a/kotlin-power-assert-plugin/src/main/kotlin/com/bnorm/power/PowerAssertCallTransformer.kt +++ b/kotlin-power-assert-plugin/src/main/kotlin/com/bnorm/power/PowerAssertCallTransformer.kt @@ -105,6 +105,10 @@ class PowerAssertCallTransformer( val assertionArgument = expression.getValueArgument(0)!! val messageArgument = if (function.valueParameters.size == 2) expression.getValueArgument(1) else null + // If the tree does not contain any children, the expression is not transformable + val tree = buildAssertTree(assertionArgument) + val root = tree.children.singleOrNull() ?: return super.visitCall(expression) + val symbol = currentScope!!.scope.scopeOwnerSymbol DeclarationIrBuilder(context, symbol).run { at(expression) @@ -129,9 +133,6 @@ class PowerAssertCallTransformer( } } - val tree = buildAssertTree(assertionArgument) - val root = tree.children.single() - // println(assertionArgument.dump()) // println(tree.dump()) diff --git a/kotlin-power-assert-plugin/src/test/kotlin/com/bnorm/power/test.kt b/kotlin-power-assert-plugin/src/test/kotlin/com/bnorm/power/test.kt index e088ad45dbc..86c8b0d0306 100644 --- a/kotlin-power-assert-plugin/src/test/kotlin/com/bnorm/power/test.kt +++ b/kotlin-power-assert-plugin/src/test/kotlin/com/bnorm/power/test.kt @@ -399,6 +399,20 @@ assert(a == 42) 0 """.trimIndent()) } + + @Test + fun constantExpression() { + assertMessage( + """ +fun main() { + assert(true) + assert(false) +}""", + """ +Assertion failed +""".trimIndent() + ) + } } fun assertMessage(