Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Discussion: Combining Arbitraries #3

Open
jlink opened this issue Aug 13, 2024 · 0 comments
Open

API Discussion: Combining Arbitraries #3

jlink opened this issue Aug 13, 2024 · 0 comments

Comments

@jlink
Copy link
Contributor

jlink commented Aug 13, 2024

Combining two or more arbitraries into a new one by using generated values from the individual arbitraries is a crucial capability when design more complex properties. Jqwik 1 has two approaches: Combinators and Builders.

For jqwik 2 I suggest to keep the Combinators approach but replace Builders with a more generic approach that allows to "sample" values from any available arbitrary and use it in creating a new one.

Combinators

Combine two or more arbitraries by providing a function that takes a value per arbitrary and uses the taken values to create another arbitrary. Example:

Arbitrary<String> names = ...;
Arbitrary<Integer> ages = ...;
Arbitrary<Person> people = Combinators.combine(names, ages)
                                      .as((name, age) -> new Person(name, age));

This is straightforward and seems to be intuitive to most developers.
Jqwik 1 defines combinator functions for up to 8 arbitraries.

Generic Sampling

Arbitrary<String> names = ...;
Arbitrary<Integer> ages = ...;

Arbitrary<Person> people = Sampler.use((Sampler sampler) -> {
	int age = sampler.draw(ages);
	String name = sampler.draw(names);
	return new Person(name, age);
});

This approach has two advantages:

  • It allows for any number of arbitraries to be used
  • It allwos for any number and nesting of combination steps

Mind that this approach cannot be implemented in Jqwik 1 due to how Jqwik 1 implements shrinking and randomized generation.

Discussion

In principle the Generic Sampling approach makes the Combinators approach redundant, because it's more powerful.
I'd still keep it, since combinators seem to be more intuitive and provide better discoverability.

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

No branches or pull requests

1 participant