kotlinx-kover icon indicating copy to clipboard operation
kotlinx-kover copied to clipboard

Branch coverage incorrect when using elvis operator

Open driessamyn opened this issue 10 months ago • 2 comments

Kover incorrectly reports a gap in branch coverage

Consider this class:

class KoverExample {
    private val map = mapOf<Int, () -> String>(
        1 to { "Fred" },
        2 to { "Joe" }
    )
    fun greeting(i: Int): String =
        map[i]
            ?.invoke()
            ?: throw IllegalStateException("No greeting for $i")
}

and these tests:

class KoverExampleTest {
    val koverExample = KoverExample()

    @Test
    fun `when index exists, greet`() {
        koverExample.greeting(1) shouldBe "Fred"
    }

    @Test
    fun `when index does not exist, throw`() {
        shouldThrow<IllegalStateException> {
            koverExample.greeting(-1)
        }
    }
}

Kover reports only 75% branch coverage, highlighting map[i] as not covered.

Expected behavior

Kover should report full coverage in this example, in my opinion, given there is no code path not exercised with the test.

Reproducer

See code above.

Reports

Image

Environment

  • Kover Gradle Plugin version: 0.9.1
  • Gradle version: 8.12
  • Kotlin project type: Kotlin/JVM
  • Coverage Toolset (if customized in build script): Kove

driessamyn avatar Jan 29 '25 09:01 driessamyn