package au.id.zancanaro.javacheck; import au.id.zancanaro.javacheck.annotations.DataSource; import au.id.zancanaro.javacheck.annotations.Property; import au.id.zancanaro.javacheck.junit.Properties; import org.junit.runner.RunWith; import java.util.Optional; import java.util.function.Function; import static au.id.zancanaro.javacheck.Generator.pure; import static au.id.zancanaro.javacheck.Generators.integer; import static au.id.zancanaro.javacheck.Generators.oneOf; import static org.junit.Assert.assertEquals; @RunWith(Properties.class) public class OptionalFunctorRulesTest { private final static int runs = 1000; private final static int maxSize = 1000; @DataSource public static Generator> listOfIntegers = oneOf( pure(Optional.empty()), integer().map(Optional::of)); @DataSource public static Generator> integerFunction = oneOf( integer().map(OptionalFunctorRulesTest::plusI), integer().map(OptionalFunctorRulesTest::timesI), integer().map(OptionalFunctorRulesTest::constantlyI)); @Property(maxSize = maxSize, runs = runs) public void mappingCompositionsWithStreams( Optional optional, Function f, Function g) { Optional left = optional .map(g) .map(f); Optional right = optional .map(x -> f.apply(g.apply(x))); assertEquals(left, right); } @Property(maxSize = maxSize, runs = runs) public void mappingCompositionsWithIntermediateList( Optional optional, Function f, Function g) { Optional intermediate = optional.map(g); Optional left = intermediate.map(f); Optional right = optional .map(x -> f.apply(g.apply(x))); assertEquals(left, right); } @Property(maxSize = maxSize, runs = runs) public void mapIdentityIsIdentity(Optional optional) { Optional mapped = optional.map(x -> x); assertEquals(optional, mapped); } private static Function plusI(final int i) { return new Function() { @Override public Integer apply(Integer integer) { return i + integer; } @Override public String toString() { return "x -> x + " + i; } }; } private static Function timesI(final int i) { return new Function() { @Override public Integer apply(Integer integer) { return i * integer; } @Override public String toString() { return "x -> x * " + i; } }; } private static Function constantlyI(final int i) { return new Function() { @Override public Integer apply(Integer integer) { return i; } @Override public String toString() { return "x -> " + i; } }; } }