Skip to content

Consider supporting @UseType for sealed interfaces #523

@dlesl

Description

@dlesl

Testing Problem

Here is an example of testing a sealed interface which represents a data type.

class ExampleProperty {
  sealed interface JsonValue {
    record Text(String text) implements JsonValue {}
    record Number(double value) implements JsonValue {}
  }

  @Property
  @Domain(DomainContext.Global.class)
  @Domain(JsonDomain.class)
  void example(@ForAll @UseType JsonValue jsonValue) {
    System.out.println(jsonValue);
  }

  static class JsonDomain extends DomainContextBase {
    @Provide
    Arbitrary<JsonValue> jsonValue() {
      return Arbitraries.of(JsonValue.class.getPermittedSubclasses())
          .flatMap(c -> Arbitraries.forType(c).map(j -> (JsonValue) j));
    }
  }
}

Perhaps this strategy could be attempted by jqwik's @UseType by default?

Discussion

The current behaviour is that generation fails immediately for a sealed interface. Having this as the default behaviour could potentially be worse if it fails to discover/generate some of the subclasses and they get missed in testing?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions