Last active
December 26, 2019 23:25
-
-
Save zoldar/8778b6777906c5a1370028fa9fe605b0 to your computer and use it in GitHub Desktop.
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
// pickaroo: Money | |
pub type Currency { | |
USD | |
EUR | |
PLN | |
} | |
pub type Money { | |
Money(cents: Int, currency: Currency) | |
} | |
pub type MoneyError { | |
CurrenciesMismatch(c1: Currency, c2: Currency) | |
} | |
pub fn add(m1: Money, m2: Money) { | |
case m1, m2 { | |
Money(cents1, currency), Money(cents2, currency) -> Ok(Money(cents1 + cents2, currency)) | |
Money(currency: c1), Money(currency: c2) -> Error(CurrenciesMismatch(c1, c2)) | |
} | |
} |
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 pickaroo | |
import gleam/expect | |
pub fn add_ok_test() { | |
pickaroo.add(pickaroo.Money(100, pickaroo.USD), pickaroo.Money(200, pickaroo.USD)) | |
|> expect.equal(_, Ok(pickaroo.Money(300, pickaroo.USD))) | |
} | |
pub fn add_mismatch_test() { | |
pickaroo.add(pickaroo.Money(100, pickaroo.USD), pickaroo.Money(200, pickaroo.EUR)) | |
|> expect.equal(_, Error(pickaroo.CurrenciesMismatch(pickaroo.USD, pickaroo.EUR))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment