Skip to content

Instantly share code, notes, and snippets.

View ythirion's full-sized avatar

Yoan Thirion ythirion

View GitHub Profile
@ythirion
ythirion / try.cs
Created December 18, 2019 15:48
Try
public void file_monad_example()
{
GetLine()
.Bind(ReadFile)
.Bind(PrintStrln)
.Match(success => Console.WriteLine("SUCCESS"),
failure => Console.WriteLine("FAILURE"));
}
static Try<string> GetLine()
@ythirion
ythirion / real_life.cs
Created December 18, 2019 15:56
Real life example
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();
}
<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>
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);
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;
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());
}
@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);
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)