Skip to content

API Suggestion: Arbitraries, Generators and Transformations #1

@jlink

Description

@jlink

Arbitraries and generators are fundamental concepts; so is transforming them into other arbitraries.
Jqwik 1 already has a well-working API for that, so I suggest keeping the API similar where appropriate
and making it simpler where possible.

Arbitrary and Generator

Let's start with the two fundamental interfaces:

public interface Arbitrary<T> {
    Generator<T> generator();
}

public interface Generator<T> {
    T generate(GenSource source);
}

Differences to Jqwik 1

  • Arbitrary.generator() no longer takes a genSize: This parameter turned out to be of little use in most cases and made caching of generators much more difficult.

  • Generator - used to be called RandomGenerator - can now directly generate values through generate(GenSource source) method. The detour over a Shrinkable type is no longer necessary.

  • The concept of GenSource, which is new, will be discussed in other issues.

Standard Transformations

Mapping, filtering, flat-mapping etc. should work as before. The same is true for creating lists, sets etc.
Thus, there'll be a couple of default methods in Arbitrary:

public interface Arbitrary<T> {

	Generator<T> generator();

	default <R> Arbitrary<R> map(Function<T, R> mapper) { ... }

	default <T> Arbitrary<T> filter(Predicate<T> filter) { ... }

	default <R> Arbitrary<R> flatMap(Function<T, Arbitrary<R>> mapper) { ... }

	default ListArbitrary<T> list() { ... }

	default SetArbitrary<T> set() { ... }
}

Filter or Include / Exclude?

Since the term filter is somewhat ambiguous, a few libraries have switched to provide include, exclude, filterIn, filterOutor similar clarifying terms. Is this a clarification or does it go against expectations?

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