bnorm/kotlin-power-assert

Infix transformation doesn't capture full context

knizamov opened this issue · 2 comments

Hi, thanks for your great plugin.

Given infix function:

// some com.my.Assertions.kt file
infix fun <T> T.eq(expected: T) = assertEquals(this, expected)
fun <T> T.eq(expected: T, msg: String) {
    println(msg)
    assertEquals(this, expected)
}

class Tests {
  data class Person(val name: String, val age: Int, val address: Address?)
  data class Address(val street: String, val zipCode: Int)
  
  @Test
  fun test() {
    val person = Person(
        name = "Name",
        age = 30,
        address = Address(
            street = "street",
            zipCode = 1234,
        ),
    )
  
    person.address?.street eq "123"
  }
}

It captures only street, not full person.address?.street context:

street eq "123"
      |        |
      |        street
      Address(street=street, zipCode=1234)
Person(name=Name, age=30, address=Address(street=street, zipCode=1234))

While using assert(person.address?.street == "123") prints:

assert(person.address?.street == "123")
       |      |        |      |
       |      |        |      false
       |      |        street
       |      Address(street=street, zipCode=1234)
       Person(name=Name, age=30, address=Address(street=street, zipCode=1234))
bnorm commented

It does look like the person, address, and street information is all being captured and printed in your example but the information is not aligned properly because only street eq "123" is displayed. Probably a bug in the start offset for the expression. Thanks for the report!

This compiler plugin is now bundled with Kotlin, starting with version 2.0.0-Beta5, where this issue has been fixed: https://youtrack.jetbrains.com/issue/KT-65640/PowerAssert-Infix-function-not-aligned-correctly.

import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi

plugins {
    kotlin("jvm") version "2.0.0-Beta5"
    kotlin("plugin.power-assert") version "2.0.0-Beta5"
}

@OptIn(ExperimentalKotlinGradlePluginApi::class)
powerAssert {
    functions = listOf("kotlin.assert", "kotlin.test.assertTrue")
    excludedSourceSets = listOf("main")
}