Skip to content

Record deconstruction in exhaustive switch not fully supported #2000

@NolwennD

Description

@NolwennD

Following #1999 I go deeper in exhaustive switch and record deconstruction, so I found that it not fully supported by Jacoco.

Steps to reproduce

  • JaCoCo version: 0.8.14
  • Java 25
  • Operating system: Linux Ubuntu
  • Tool integration: Maven
public sealed interface Coverage {
  record A(int number) implements Coverage {
    A(){
      this(1);
    }
  }
  record B() implements Coverage {}


  default String missingBranches1Of4(){
    return switch (this){
      case A(int zero) when zero == 0 -> "record A with zero";
      case A _ -> "record A";
      case B _ -> "not record A";
    };
  }

  default String missingBranches1Of2(){
    return switch (this){
      case A(int number) -> {
        if (number == 0){
          yield "record A with zero";
        }
        yield "record A";
      }
      case B _ -> "not record A";
    };
  }

  default String allBranche(){
    return switch (this){
      case A a -> {
        if (a.number() == 0){
          yield "record A with zero";
        }
        yield "record A";
      }
      case B _ -> "not record A";
    };
  }
}
class CoverageTest {
  @ParameterizedTest
  @ValueSource(ints = {-1, 1, 2})
  void shouldBeRecordAForA(){
    assertThat(new Coverage.A().missingBranches1Of4()).isEqualTo("record A");
  }
  @Test
  void shouldBeRecordAWithZeroForA(){
    assertThat(new Coverage.A(0).missingBranches1Of4()).isEqualTo("record A with zero");
  }
  @Test
  void shouldNotBeRecordAForB(){
    assertThat(new Coverage.B().missingBranches1Of4()).isEqualTo("not record A");
  }
  @ParameterizedTest
  @ValueSource(ints = {-1, 1, 2})
  void shouldBeRecordAForA1(){
    assertThat(new Coverage.A().allBranche()).isEqualTo("record A");
  }
  @Test
  void shouldBeRecordAWithZeroForA1(){
    assertThat(new Coverage.A(0).allBranche()).isEqualTo("record A with zero");
  }
  @Test
  void shouldNotBeRecordAForB1(){
    assertThat(new Coverage.B().allBranche()).isEqualTo("not record A");
  }
  @ParameterizedTest
  @ValueSource(ints = {-1, 1, 2})
  void shouldBeRecordAForA2(){
    assertThat(new Coverage.A().missingBranches1Of2()).isEqualTo("record A");
  }
  @Test
  void shouldBeRecordAWithZeroForA2(){
    assertThat(new Coverage.A(0).missingBranches1Of2()).isEqualTo("record A with zero");
  }
  @Test
  void shouldNotBeRecordAForB2(){
    assertThat(new Coverage.B().missingBranches1Of2()).isEqualTo("not record A");
  }
}
Image

Expected behaviour

100% coverage

Actual behaviour

Missing some branches.

Metadata

Metadata

Assignees

Type

No type

Projects

Status

In Progress

Relationships

None yet

Development

No branches or pull requests

Issue actions