-
-
Notifications
You must be signed in to change notification settings - Fork 66
Description
Hello!
One way I use jqwik is to generate instances of messages in message protocols. A message protocol is declared as a sealed interface, and the sealed interface is implemented by other sealed interfaces and record types:
sealed interface Message {}
sealed interface Command implements Message {}
sealed interface Response implements Message {}
record CommandHello(String text) implements Command {}
record CommandPing() implements Command {}
record ResponseHello(String text) implements Response {}
record ResponsePing() implements Response {}
... and so on. The tests then generate an arbitrary message, serialize it, and deserialize it, and check it against the original message as a round-trip test of the serializers.
For this kind of setup, it's a lot of work to manually write arbitraries for each message type. If we assume that all record components are either value types, records, or collection types (including Optional), and that all interfaces are sealed, it feels like it would be relatively easy to generate arbitrary instances reflectively. I have done similar work generating JSON schemas for sealed "sum and product" hierarchies like this:
https://github.com/io7m-com/sumjack
I wonder if there's some kind of Arbitraries.createForSimpleRecord() method that could do this, or some way that I could register types for generation in this manner.
Doing it by hand results in having to write a lot of arbitraries: https://github.com/io7m-com/cardant/tree/develop/com.io7m.cardant.tests.arbitraries/src/main/java/com/io7m/cardant/tests/arbitraries