Created
December 15, 2017 15:03
-
-
Save yasuabe/664faa0b975c912e1e6fc9af07a9b8ec to your computer and use it in GitHub Desktop.
ch02 degenerate objects
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 org.scalacheck.Prop.forAll | |
import org.scalacheck.Properties | |
object Chapter02 extends Properties("Ch02") { | |
// ======== TODO ======== | |
// $5 + 10 CHF = $10 if rate is 2:1 | |
// Make "amount" private | |
// Money rounding? | |
// ======== DONE ======== | |
// 1. $5 * 2 = $10 | |
// 2. Dollar side effect | |
// 2. times -> `*` | |
// product code ------------------------------------------------------------------- | |
case class Dollar(amount: Int) { | |
def *(multiplier: Int): Dollar = Dollar(amount * multiplier) | |
} | |
// properties ------------------------------------------------------------------- | |
property("$0 * x = $0") = forAll { (x: Int) => | |
Dollar(0) * x == Dollar(0) | |
} | |
property("$1 * x = $x") = forAll { (x: Int) => | |
Dollar(1) * x == Dollar(x) | |
} | |
property("$a * b = $b * a") = forAll { (a: Int, b: Int) => | |
Dollar(a) * b == Dollar(b) * a | |
} | |
property("($a * b) * c = $a * (b * c)") = forAll { (a: Int, b: Int, c: Int) => | |
(Dollar(a) * b) * c == Dollar(a) * (b * c) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment