From 77a387f06fa964ebd534a1097fa8fc7ab92c3275 Mon Sep 17 00:00:00 2001 From: Brian Norman Date: Sat, 8 Feb 2020 18:25:11 -0600 Subject: [PATCH] Adjust display column based on infix operator Fixes #2 --- .../com/bnorm/power/PowerAssertGenerator.kt | 15 ++++++--------- .../src/test/kotlin/com/bnorm/power/test.kt | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/kotlin-power-assert/src/main/kotlin/com/bnorm/power/PowerAssertGenerator.kt b/kotlin-power-assert/src/main/kotlin/com/bnorm/power/PowerAssertGenerator.kt index 091afb7970c..addd1203cb8 100644 --- a/kotlin-power-assert/src/main/kotlin/com/bnorm/power/PowerAssertGenerator.kt +++ b/kotlin-power-assert/src/main/kotlin/com/bnorm/power/PowerAssertGenerator.kt @@ -36,14 +36,6 @@ abstract class PowerAssertGenerator( private val file: IrFile, private val fileSource: String ) { - -// private fun IrStatementsBuilder<*>.buildAssertThrow( -// callSource: String, -// title: IrExpression, -// subStack: MutableList, -// callIndent: Int = 0 -// ) = buildThrow(constructor, buildMessage(title, subStack, callSource, callIndent)) - abstract fun IrBuilderWithScope.buildAssertThrow(subStack: List): IrExpression fun buildAssert( @@ -107,7 +99,12 @@ abstract class PowerAssertGenerator( var startColumnNumber = file.info(expression).startColumnNumber if (expression is IrMemberAccessExpression) { - // TODO Is this the best way to fix indentation of infix operators? + val descriptor = expression.descriptor + if (descriptor is FunctionDescriptor && descriptor.isInfix) { + startColumnNumber += source.indexOf(descriptor.name.asString()) + } + + // TODO handle equality and comparison better? startColumnNumber += when (expression.origin) { IrStatementOrigin.EQEQ, IrStatementOrigin.EQEQEQ -> source.indexOf("==") IrStatementOrigin.EXCLEQ, IrStatementOrigin.EXCLEQEQ -> source.indexOf("!=") 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 fe1dd767f63..634e89245f9 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 @@ -19,6 +19,7 @@ package com.bnorm.power import com.tschuchort.compiletesting.KotlinCompilation import com.tschuchort.compiletesting.SourceFile import org.intellij.lang.annotations.Language +import org.jetbrains.kotlin.descriptors.FunctionDescriptor import org.junit.Test import java.lang.reflect.InvocationTargetException import kotlin.test.assertEquals @@ -265,6 +266,23 @@ assert(text?.length?.minus(2) == 1) | | 3 | 5 Hello +""".trimIndent() + ) + } + + @Test + fun infixFunctions() { + assertMessage( + """ +fun main() { + assert(1 shl 1 == 4) +}""", + """ +Assertion failed +assert(1 shl 1 == 4) + | | + | false + 2 """.trimIndent() ) }