Skip to content

Branch coverage incorrect when using elvis operator #729

@driessamyn

Description

@driessamyn

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

Metadata

Metadata

Assignees

Labels

BugBug issue typeCheck In JaCoCoS: postponedStatus: work on the issue is not in the short term plans

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions