-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated user guide entry about arbitrary configurators
- Loading branch information
Showing
3 changed files
with
89 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 20 additions & 6 deletions
26
documentation/src/test/java/net/jqwik/docs/arbitraryconfigurator/OddConfigurator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,34 @@ | ||
package net.jqwik.docs.arbitraryconfigurator; | ||
|
||
import java.math.*; | ||
|
||
import net.jqwik.api.*; | ||
import net.jqwik.api.configurators.*; | ||
import net.jqwik.api.providers.*; | ||
|
||
import java.math.*; | ||
|
||
public class OddConfigurator extends ArbitraryConfiguratorBase { | ||
|
||
public Arbitrary<Integer> configureInteger(Arbitrary<Integer> arbitrary, Odd odd) { | ||
return arbitrary.filter(number -> Math.abs(number % 2) == 1); | ||
return arbitrary.filter(number -> Math.abs(number % 2) == 1); | ||
} | ||
|
||
public Arbitrary<BigInteger> configureBigInteger(Arbitrary<BigInteger> arbitrary, Odd odd) { | ||
return arbitrary.filter(number -> { | ||
return number.mod(BigInteger.valueOf(2)).compareTo(BigInteger.ZERO) != 0; | ||
}); | ||
return arbitrary.filter(number -> { | ||
return number.remainder(BigInteger.valueOf(2)).abs().equals(BigInteger.ONE); | ||
}); | ||
} | ||
} | ||
|
||
class PlainOddConfigurator implements ArbitraryConfigurator { | ||
|
||
@Override | ||
public <T> Arbitrary<T> configure(Arbitrary<T> arbitrary, TypeUsage targetType) { | ||
if (!targetType.isOfType(Integer.class) && !targetType.isOfType(int.class)) { | ||
return arbitrary; | ||
} | ||
return targetType.findAnnotation(Odd.class) | ||
.map(odd -> arbitrary.filter(number -> Math.abs(((Integer) number) % 2) == 1)) | ||
.orElse(arbitrary); | ||
} | ||
} | ||
|