Created
December 15, 2017 19:00
-
-
Save yasuabe/6e1cc0bb4ae9cbac1b0e355742b8b66b to your computer and use it in GitHub Desktop.
ch14 make it
This file contains hidden or 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
package fp_tdd | |
import cats.Apply | |
import cats.syntax.apply._ | |
import org.scalacheck.Prop.forAll | |
import org.scalacheck.{Arbitrary, Gen, Properties} | |
object Chapter13 extends Properties("Ch13") { | |
// ======== TODO ======== | |
// $5 + 10 CHF = $10 if rate is 2:1 | |
// $5 + $5 = $10 | |
// Return Money from $5 + $5 | |
// Reduce Money with conversion | |
// Reduce(Bank, String) | |
// ======== DONE ======== | |
// Bank.reduce(Money) | |
// product code ------------------------------------------------------------------- | |
trait Expression { | |
def reduce(to: String): Money | |
} | |
class Money(private[Chapter13] val amount: Int, val currency: String) extends Expression { | |
def *(multiplier: Int): Money = copy(amount * multiplier) | |
def +(addend: Money): Expression = copy(amount + addend.amount) | |
private def copy(amount: Int) = Money(amount, currency) | |
def reduce(to: String): Money = this | |
override def equals(a: Any): Boolean = { | |
val another = a.asInstanceOf[Money] | |
another.amount == amount && | |
another.currency == currency | |
} | |
} | |
case class Sum(augend: Money, append: Money) extends Expression { | |
def reduce(to: String): Money = Money(augend.amount + append.amount, to) | |
} | |
object Money { | |
def apply(amount: Int, currency: String) = new Money(amount, currency) | |
def dollar(amount: Int): Money = Money(amount, "USD") | |
def franc(amount: Int): Money = Money(amount, "CHF") | |
} | |
class Bank { | |
def reduce(source: Expression, to: String): Money = source.reduce(to) | |
} | |
// test code -------------------------------------------------------------------- | |
import Money.{dollar, franc} | |
implicit val intGen: Gen[Int] = Gen.chooseNum(-1000000, 1000000) | |
implicit val consGen: Gen[Int => Money] = Gen.oneOf(dollar(_:Int): Money, franc(_:Int): Money) | |
val currencyGen: Gen[String] = Gen.oneOf("USD", "CHF") | |
def double[G[_]: Apply, A](ga: G[A]): G[(A, A)] = (ga, ga).mapN((_, _)) | |
def triple[G[_]: Apply, A](ga: G[A]): G[(A, A, A)] = (ga, ga, ga).mapN((_, _, _)) | |
implicit val genApply: Apply[Gen] = new Apply[Gen] { | |
override def ap[A, B](gf: Gen[A => B])(ga: Gen[A]): Gen[B] = gf flatMap ga.map | |
override def map[A, B](ga: Gen[A])(f: A => B): Gen[B] = ga map f | |
} | |
implicit val moneyGen: Gen[Money] = (consGen, intGen).mapN(_(_)) | |
implicit val moneyConsGen: Gen[Int => Money] = currencyGen map constructor | |
implicit val arbCons: Arbitrary[Int => Money] = Arbitrary(moneyConsGen) | |
implicit val arbMoney: Arbitrary[Money] = Arbitrary(moneyGen) | |
def constructor(currency: String)(n: Int): Money = currency match { | |
case "USD" => dollar(n) | |
case "CHF" => franc(n) | |
} | |
implicit class MoneyTestOp(m: Money) { | |
def equiv(e: Expression): Boolean = m == new Bank().reduce(e, m.currency) | |
} | |
def reduce(m: Money): Money = new Bank().reduce(m, m.currency) | |
def mapPair[A, B](a1: A, a2: A)(f: A => B): (B, B) = (f(a1), f(a2)) | |
val unique2: Gen[(Money, Money)] = | |
double(moneyGen).suchThat { case (a, b) => a != b } | |
val unique3: Gen[(Money, Money, Money)] = | |
triple(moneyGen).suchThat { case (a, b, c) => a != b && b != c && c != a } | |
// properties ------------------------------------------------------------------- | |
property("m(0) * x = m(0)") = forAll { (x: Int, m: Int => Money) => | |
m(0) * x == m(0) | |
} | |
property("m(1) * x = m(x)") = forAll { (x: Int, m: Int => Money) => | |
m(1) * x == m(x) | |
} | |
property("m(a) * b = m(b) * a") = forAll { (a: Int, b: Int, m: Int => Money) => | |
m(a) * b == m(b) * a | |
} | |
property("(m(a) * b) * c = m(a) * (b * c)") = forAll { (a: Int, b: Int, c: Int, m: Int => Money) => | |
(m(a) * b) * c == m(a) * (b * c) | |
} | |
property("m(a) == m(b) <=> a == b") = forAll(double(intGen), moneyConsGen) { case ((a, b), m) => | |
(m(a) == m(b)) == (a == b) | |
} | |
property("m(a) == m(b) <=> m(a) equals m(b)") = forAll(double(moneyGen)) { case (a: Money, b: Money) => | |
(a equals b) == (a == b) | |
} | |
property("equals is reflexive") = forAll { (a: Money) => | |
a equals a | |
} | |
property("equals is symmetric") = forAll(double(intGen)) { case (a, b) => | |
(a equals b) == (b equals a) | |
} | |
property("equals is transitive") = forAll( for { | |
oneOf3 <- unique3.map { case (a, b, c)=> Gen.oneOf(a, b, c) } | |
x <- oneOf3 | |
y <- oneOf3.suchThat(_ equals x) | |
z <- oneOf3.retryUntil(_ equals y) | |
} yield (x, z)) { case ((x, z)) => x equals z } | |
property("(m1==m2) == (m1.currency==m2.currency)") = | |
forAll(double(currencyGen), intGen) { case ((c1, c2), n) => | |
val (m1, m2) = mapPair(c1, c2)(constructor(_)(n)) | |
(m1 == m2) == (m1.currency == m2.currency) | |
} | |
property("m(a + b) == m(a) + m(b)") = | |
forAll(double(intGen), moneyConsGen) { case ((a, b), m) => | |
m(a + b) equiv m(a) + m(b) | |
} | |
property("m == reduce(m, m.currency) where m = money(n)") = | |
forAll(intGen, moneyConsGen) { (n, m) => | |
reduce(m(n)) == m(n) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment