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
public class Addition_properties_with_vavr_test { | |
private final int size = 10_000, tries = 100; | |
@Test | |
public void commutative() { | |
Property.def("Addition is commutative") | |
.forAll(Arbitrary.integer(), Arbitrary.integer()) | |
.suchThat((x, y) -> Calculator.add(x, y) == Calculator.add(y, x)) | |
.check(size, tries) |
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
@RunWith(JUnitQuickcheck.class) | |
public class Account_properties { | |
@Property | |
public void balance_should_be_decremented_of_the_amount( | |
@From(AccountGenerator.class) Account account, | |
@From(WithdrawGenerator.class) Withdraw withdraw) { | |
withEnoughMoney(account, withdraw); | |
withoutReachingMaxWithdrawal(account, withdraw); |
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
public class WithdrawGenerator extends Generator<Withdraw> { | |
public WithdrawGenerator() { | |
super(Withdraw.class); | |
} | |
@Override | |
public Withdraw generate(SourceOfRandomness sourceOfRandomness, GenerationStatus generationStatus) { | |
return new Withdraw(sourceOfRandomness.nextDouble(), new Date()); | |
} |
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
import io.vavr.control.Try; | |
import lombok.AllArgsConstructor; | |
import lombok.Getter; | |
import lombok.NoArgsConstructor; | |
@AllArgsConstructor | |
@Getter | |
public class Account { | |
private final double balance; | |
private final boolean isOverdraftAuthorized; |
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
import com.pholser.junit.quickcheck.Property; | |
import com.pholser.junit.quickcheck.runner.JUnitQuickcheck; | |
import org.junit.Assert; | |
import org.junit.runner.RunWith; | |
@RunWith(JUnitQuickcheck.class) | |
public class Addition_properties { | |
@Property | |
public void commutative(int randomX, int randomY) { | |
int result1 = Calculator.add(randomX, randomY); |
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
<properties> | |
<junit-quickcheck.version>0.9.1</junit-quickcheck.version> | |
</properties> | |
<dependency> | |
<groupId>com.pholser</groupId> | |
<artifactId>junit-quickcheck-core</artifactId> | |
<version>${junit-quickcheck.version}</version> | |
<scope>test</scope> | |
</dependency> |
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
private readonly PersonRepository personRepository; | |
private readonly ILogger logger; | |
private readonly TwitterService twitterService; | |
public PersonService(ILogger logger) | |
{ | |
this.logger = logger; | |
personRepository = new PersonRepository(); | |
twitterService = new TwitterService(); | |
} |
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
public void file_monad_example() | |
{ | |
GetLine() | |
.Bind(ReadFile) | |
.Bind(PrintStrln) | |
.Match(success => Console.WriteLine("SUCCESS"), | |
failure => Console.WriteLine("FAILURE")); | |
} | |
static Try<string> GetLine() |
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
[Fact] | |
public void fold_vs_reduce() | |
{ | |
//fold takes an explicit initial value for the accumulator | |
//Can choose the result type | |
var foldResult = List(1, 2, 3, 4, 5) | |
.Map(x => x * 10) | |
.Fold(0m, (x, s) => s + x); // 150m | |
//reduce uses the first element of the input list as the initial accumulator value |
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
static Func<int, int, int> Multiply = (a, b) => a * b; | |
static Func<int, int> TwoTimes = par(Multiply, 2); | |
[Fact] | |
public void partial_app_example() | |
{ | |
Multiply(3, 4); // 12 | |
TwoTimes(9); // 18 | |
} |
NewerOlder