From 9adb5ba4e86b23db72892a7449b25206dc87f552 Mon Sep 17 00:00:00 2001 From: Brian Norman Date: Sat, 8 Feb 2020 13:22:17 -0600 Subject: [PATCH] Fix a few bugs in the temporary variable stack building --- .../bnorm/power/PowerAssertCallTransformer.kt | 18 +++---- .../src/test/kotlin/com/bnorm/power/test.kt | 49 +++++++++++++++++++ 2 files changed, 56 insertions(+), 11 deletions(-) diff --git a/kotlin-power-assert/src/main/kotlin/com/bnorm/power/PowerAssertCallTransformer.kt b/kotlin-power-assert/src/main/kotlin/com/bnorm/power/PowerAssertCallTransformer.kt index 4dbf0bcdcb7..9f826a53fb2 100644 --- a/kotlin-power-assert/src/main/kotlin/com/bnorm/power/PowerAssertCallTransformer.kt +++ b/kotlin-power-assert/src/main/kotlin/com/bnorm/power/PowerAssertCallTransformer.kt @@ -128,13 +128,13 @@ fun IrBlockBuilder.buildAssert( node: Node, stack: MutableList = mutableListOf(), constructor: IrConstructorSymbol = context.ir.symbols.assertionErrorConstructor, - thenPart: IrBlockBuilder.(stack: MutableList) -> IrExpression = { stack -> buildThrow(constructor, buildMessage(title, stack, callSource)) } + thenPart: IrBlockBuilder.(stack: MutableList) -> IrExpression = { subStack -> buildThrow(constructor, buildMessage(title, subStack, callSource)) } ) { fun IrBlockBuilder.nest(children: List, index: Int, stack: MutableList) { val child = children[index] - buildAssert(context, file, fileSource, callSource, callIndent, title, child, stack, constructor) { stack -> - if (index + 1 == children.size) buildThrow(constructor, buildMessage(title, stack, callSource)) - else irBlock { nest(children, index + 1, stack) } + buildAssert(context, file, fileSource, callSource, callIndent, title, child, stack, constructor) { subStack -> + if (index + 1 == children.size) buildThrow(constructor, buildMessage(title, subStack, callSource)) + else irBlock { nest(children, index + 1, subStack) } } } @@ -159,11 +159,11 @@ private inline fun IrBlockBuilder.irIfNotThan( fileSource: String, callIndent: Int, node: ExpressionNode, - thenPart: IrBlockBuilder.(stack: MutableList) -> IrExpression + thenPart: IrBlockBuilder.(subStack: MutableList) -> IrExpression ): IrWhen { val stackTransformer = StackBuilder(this, stack, file, fileSource, callIndent, node.expressions) val transformed = node.expressions.first().transform(stackTransformer, null) - return irIfThen(irNot(transformed), thenPart(stack)) + return irIfThen(irNot(transformed), thenPart(stack.toMutableList())) } class StackBuilder( @@ -197,11 +197,7 @@ class StackBuilder( } override fun visitExpression(expression: IrExpression): IrExpression { - return if (expression in transform) { - push(super.visitExpression(expression)) - } else { - super.visitExpression(expression) - } + return super.visitExpression(expression).also { if (expression in transform) push(it) } } } diff --git a/kotlin-power-assert/src/test/kotlin/com/bnorm/power/test.kt b/kotlin-power-assert/src/test/kotlin/com/bnorm/power/test.kt index d515bf10384..fe1dd767f63 100644 --- a/kotlin-power-assert/src/test/kotlin/com/bnorm/power/test.kt +++ b/kotlin-power-assert/src/test/kotlin/com/bnorm/power/test.kt @@ -175,6 +175,29 @@ assert(text != null && (text.length == 1 || text.toLowerCase() == text)) ) } + @Test + fun booleanMixAndLast() { + assertMessage( + """ +fun main() { + val text = "Hello" + assert((text.length == 1 || text.toLowerCase() == text) && text.length == 1) +}""", + """ +Assertion failed +assert((text.length == 1 || text.toLowerCase() == text) && text.length == 1) + | | | | | | | + | | | | | | Hello + | | | | | false + | | | | hello + | | | Hello + | | false + | 5 + Hello +""".trimIndent() + ) + } + @Test fun booleanMixOrFirst() { assertMessage( @@ -200,6 +223,32 @@ assert(text == null || (text.length == 5 && text.toLowerCase() == text)) ) } + @Test + fun booleanMixOrLast() { + assertMessage( + """ +fun main() { + val text = "Hello" + assert((text.length == 5 && text.toLowerCase() == text) || text.length == 1) +}""", + """ +Assertion failed +assert((text.length == 5 && text.toLowerCase() == text) || text.length == 1) + | | | | | | | | | | + | | | | | | | | | false + | | | | | | | | 5 + | | | | | | | Hello + | | | | | | Hello + | | | | | false + | | | | hello + | | | Hello + | | true + | 5 + Hello +""".trimIndent() + ) + } + @Test fun conditionalAccess() { assertMessage(