sageserpent-open / americium

Generation of test case data for Scala and Java, in the spirit of QuickCheck. When your test fails, it gives you a minimised failing test case and a way of reproducing the failure immediately.
MIT License
15 stars 1 forks source link

Conveniently support trials of options and eithers. #37

Closed sageserpent-open closed 2 years ago

sageserpent-open commented 2 years ago

What it says on the tin - syntax like this:

trialsOfX.options - Scala Trials[Option[X]] trialsOfLeft.or(trialsOfRight) - Scala Trials[Either[Left, Right]]

trialsOfX.options - Java Trials<Option<X>> - the Cyclops Option. trialsOfX.optionals - Java Trials<Optional<X>> - standard Java Optional. trialsOfLeft.or(trialsOfRight) - Java Trials<Either<Left, Right>> - Cyclops Either.

sageserpent-open commented 2 years ago

While we at at, let's have this too, inspired by Vavr....

final Trials<ImmutableSet<Integer>> immutableSets = api.integers.immutableSets().covariantly()

final Trials<Set<Integer>> sets = immutableSets

Not sure if this is possible given the limitations of Java's type system, as the wildcard would be at the top level of the type...

sageserpent-open commented 2 years ago

First casualty - as long as a common implementation underpins both the Java and Scala forms of Trials, then we can't have competing declarations of Trials.options - different return types. For now, the end user will have to map a Trials.optionals using <optionals>.map(Option::fromOptional).

Sorry.

sageserpent-open commented 2 years ago

Also dropped is the Trials.covariantly - just use a wildcard type, grin and bear it:

final Trials<? extends Set<Integer>> narrowed = api.integers().immutableSortedSets(Integer::compare);

sageserpent-open commented 2 years ago

Proof from JShell:

import com.sageserpent.americium.java.Trials;

import java.util.Collection;

  final Trials<? extends Collection<Integer>>
          narrowed = Trials.api().integers().immutableSortedSets(Integer::compare);

  narrowed.withLimit(3).supplyTo(System.out::println);

  narrowed.map(Object::getClass).withLimit(3).supplyTo(System.out::println);

Output:

import com.sageserpent.americium.java.Trials
import java.util.Collection

field Trials<? extends Collection<Integer>> narrowed = TrialsImplementation(Free(...))

narrowed.withLimit(3).supplyTo(System.out::println)
[-1975667456, -54163312]
[]
[1610305737]

narrowed.map(Object::getClass).withLimit(3).supplyTo(System.out::println)
class com.google.common.collect.RegularImmutableSortedSet
class com.google.common.collect.RegularImmutableSortedSet
class com.google.common.collect.RegularImmutableSortedSet
sageserpent-open commented 2 years ago

This is available in release 1.2.5 .