diff --git a/kotlin-power-assert-plugin/src/main/kotlin/com/bnorm/power/diagram/ExpressionTree.kt b/kotlin-power-assert-plugin/src/main/kotlin/com/bnorm/power/diagram/ExpressionTree.kt index 11efe60fb87..b8715719ba5 100644 --- a/kotlin-power-assert-plugin/src/main/kotlin/com/bnorm/power/diagram/ExpressionTree.kt +++ b/kotlin-power-assert-plugin/src/main/kotlin/com/bnorm/power/diagram/ExpressionTree.kt @@ -21,6 +21,7 @@ import org.jetbrains.kotlin.ir.expressions.IrCall import org.jetbrains.kotlin.ir.expressions.IrConst import org.jetbrains.kotlin.ir.expressions.IrContainerExpression import org.jetbrains.kotlin.ir.expressions.IrExpression +import org.jetbrains.kotlin.ir.expressions.IrFunctionExpression import org.jetbrains.kotlin.ir.expressions.IrStatementOrigin import org.jetbrains.kotlin.ir.expressions.IrWhen import org.jetbrains.kotlin.ir.util.dumpKotlinLike @@ -84,6 +85,8 @@ fun buildTree(expression: IrExpression): Node? { } override fun visitExpression(expression: IrExpression, data: Node) { + if (expression is IrFunctionExpression) return // Do not transform lambda expressions, especially their body + val node = data as? ExpressionNode ?: ExpressionNode().also { data.addChild(it) } node.add(expression) expression.acceptChildren(this, node) diff --git a/kotlin-power-assert-plugin/src/test/kotlin/com/bnorm/power/LamdaTest.kt b/kotlin-power-assert-plugin/src/test/kotlin/com/bnorm/power/LamdaTest.kt new file mode 100644 index 00000000000..9e2dce2f85e --- /dev/null +++ b/kotlin-power-assert-plugin/src/test/kotlin/com/bnorm/power/LamdaTest.kt @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2021 Brian Norman + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.bnorm.power + +import org.jetbrains.kotlin.name.FqName +import org.junit.Test + +class LamdaTest { + @Test + fun `list operations assert`() { + assertMessage( + """ + fun main() { + val list = listOf("Jane", "John") + assert(list.map { "Doe, ${'$'}it" }.any { it == "Scott, Michael" }) + }""", + """ + Assertion failed + assert(list.map { "Doe, ${'$'}it" }.any { it == "Scott, Michael" }) + | | | + | | false + | [Doe, Jane, Doe, John] + [Jane, John] + """.trimIndent() + ) + } + + @Test + fun `list operations require`() { + assertMessage( + """ + fun main() { + val list = listOf("Jane", "John") + require( + value = list + .map { "Doe, ${'$'}it" } + .any { it == "Scott, Michael" } + ) + }""", + """ + Assertion failed + require( + value = list + | + [Jane, John] + .map { "Doe, ${'$'}it" } + | + [Doe, Jane, Doe, John] + .any { it == "Scott, Michael" } + | + false + ) + """.trimIndent(), + PowerAssertComponentRegistrar(setOf(FqName("kotlin.require"))) + ) + } +}