Skip to content

Commit

Permalink
Merge pull request #44 from bnorm/lambdas
Browse files Browse the repository at this point in the history
Do not transform lambda expression bodies
  • Loading branch information
bnorm authored May 12, 2021
2 parents c867863 + 6e1d51a commit 077b641
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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")))
)
}
}

0 comments on commit 077b641

Please sign in to comment.