Domains and choices
Every input proptastic tries comes from a domain, and every domain works the same way underneath. That one rule is why a counterexample shrinks to something readable, why a seed draws the same input every run, and why a saved counterexample still replays after the code has changed. It’s worth knowing even if you never write a domain of your own.
A domain draws choices
Section titled “A domain draws choices”A domain such as integers() or lists(strings()) doesn’t draw values directly. It draws
choices, whole numbers from 0 up to a limit the domain sets, and turns each choice into a value.
booleans() makes one choice, 0 or 1, and turns it into false or true. lists(…) makes a
choice for whether there’s another element, then the element’s own choices, and again, until a
choice says the list ends.
Every input is a sequence of choices, and the domains read it in order.
One value, and its choices
Section titled “One value, and its choices”Here’s a property that’s wrong: it says digits never add up to ten.
@Testvoid digitsAddUpToUnderTen() { forAll(lists(integers().atLeast(0).atMost(9))) .check(digits -> assertThat(total(digits)).isLessThan(10));}Expecting actual: 10to be less than: 10
digitsAddUpToUnderTen failed after 1 try (seed 0x5eed) shrunk counterexample: digits = [1, 9] pin it: .example(List.of(1, 9)) rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'ChoicesTest.digitsAddUpToUnderTen'The run saves the counterexample as the choices that drew it, each as the value chosen, a slash, and the most it could have been:
id=cf655a394seed=24301choices=1/1 1/9 1/1 9/9 0/1Read in order, the choices line is the list:
| Choice | Read by | As |
|---|---|---|
1/1 |
the list: is there another element? | yes |
1/9 |
the element, integers().atLeast(0).atMost(9) |
1 |
1/1 |
the list: another? | yes |
9/9 |
the element | 9 |
0/1 |
the list: another? | no, so it ends: [1, 9] |
The list never saw a 1 or a 9, and the numbers never saw a list: each domain read the choices it needed, in turn. A record of a list and a string would read the list’s choices, then the string’s, from the same sequence.
Choice 0 is the simplest value
Section titled “Choice 0 is the simplest value”In every domain, choice 0 is the simplest value it can make, and smaller choices make simpler values:
| Domain | Choice 0 |
|---|---|
| a number | 0, or the bound nearest 0 |
elements(…), oneOf(…), an enum |
the first |
booleans() |
false |
| a list, set or map | as few elements as allowed |
| a string | as short as allowed, each character a |
optionals(…) |
Optional.empty() |
That’s what lets proptastic simplify any input without knowing what it means: lower its choices and drop some, and the domains read back a simpler value. Shrinking is exactly that.
Any sequence of choices is an input
Section titled “Any sequence of choices is an input”A domain can read any sequence of choices. A choice past its limit is read as the limit, and a sequence that runs out is read as if it went on with 0s. So every sequence of choices, even one cut short or changed at random, is still an input the domain could have drawn, and never one it couldn’t.
That’s what the rest rests on:
- Shrinking changes choices freely, and every change is still a valid input. From
[3, 7], taking out the first1/1 3/9leaves1/1 7/9 0/1, the list[7]; lowering7/9to6/9gives[3, 6]. Both add up to under ten and pass, so neither is kept. Shrinking never knew it was holding a list of digits, so every domain shrinks this way, your own included: one made with.map(…)ordomain(…)needs no shrinking code of its own. - A seed decides the choices, so the same seed draws the same input every run (Seeds and replay).
- A saved counterexample is its choices, so it replays after you’ve changed the code, or the domain, and still means something close to what it did.
target(score)and searching longer change choices to climb toward an input, and never leave the domain.
How often each value comes up
Section titled “How often each value comes up”A random try doesn’t draw each choice evenly. It leans toward small choices, and toward the largest, because bugs live at the edges. How values are drawn says how often each domain draws what, and how to change it.
Shrinking choices rather than values is from MacIver and Donaldson, 2020.