Skip to content

Add validation test for Kotlin empty when - #2248

Open
Eljees wants to merge 1 commit into
jacoco:masterfrom
Eljees:KotlinWhenEmptyTarget
Open

Add validation test for Kotlin empty when#2248
Eljees wants to merge 1 commit into
jacoco:masterfrom
Eljees:KotlinWhenEmptyTarget

Conversation

@Eljees

@Eljees Eljees commented Aug 7, 2026

Copy link
Copy Markdown

Closes #2039

Adds a validation test for empty when.

Kotlin in this module is 2.4.10, i.e. after KT-82844,
so the subject of an empty when is evaluated. The test pins down three distinct cases:

source bytecode emitted for the line JaCoCo
when (a++) {} IINC 1 1 assertFullyCovered()
when (a) {} NOP assertFullyCovered()
when {} (no subject) nothing, no line number entry assertEmpty()

The second row is the non-obvious one: even when the subject has no side effect, the
compiler still emits a single NOP attributed to that line, so the line is reported as
covered rather than empty.

Bytecode of the two subject cases (javap-style dump produced by the validation harness):

  private final emptyWhenWithSideEffectInSubject()V
   L0
    LINENUMBER 27 L0
    ICONST_1
    ISTORE 1
   L1
    LINENUMBER 28 L1      // when (a++) {}
    IINC 1 1
   L2
    LINENUMBER 29 L2
    ILOAD 1
    INVOKESTATIC org/jacoco/core/test/validation/targets/Stubs.nop (I)V

  private final emptyWhenWithoutSideEffectInSubject(I)V
   L0
    LINENUMBER 33 L0      // when (a) {}
    NOP
   L1
    LINENUMBER 34 L1
    ILOAD 1
    INVOKESTATIC org/jacoco/core/test/validation/targets/Stubs.nop (I)V

when {} produces no line number entry at all, hence assertEmpty().

Verification

The naive expectation — that an empty when executes nothing — fails on two of the three
lines, which is what makes the test worth having:

Instructions (KotlinWhenEmptyTarget.kt:24) expected:<[EMPTY]> but was:<[FULLY_COVERED]>
Instructions (KotlinWhenEmptyTarget.kt:29) expected:<[EMPTY]> but was:<[FULLY_COVERED]>

With the assertions as committed: Tests run: 6, Failures: 0.
Full module run for regressions: Tests run: 293, Failures: 0, Errors: 0, Skipped: 0
(mvn -pl org.jacoco.core.test.validation.kotlin test -Djdk.version=21 -Dbytecode.version=21,
Temurin 21.0.11, Maven 3.9.16 — same version as .mvn/wrapper).
spotless:apply leaves both new files unchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add validation test for Kotlin empty when with side effect in subject

1 participant