Skip to content

Commit

Permalink
Merge pull request #15 from Khigashiguchi/chapter/12
Browse files Browse the repository at this point in the history
Chapter/12 設計とメタファー
  • Loading branch information
Kazuki Higashiguchi authored Jan 2, 2018
2 parents cb2e34f + 8efe1c9 commit 3e08286
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 19 deletions.
47 changes: 28 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
著書「テスト駆動開発」の第1章〜第17章の写経をrustで書いたものです。

- [書籍「テスト駆動開発」をRustで書く #1 (chapter1...3)](https://qiita.com/Khigashiguchi/items/dfc382d9d2f988522721)
- [書籍「テスト駆動開発」をRustで書く #2 (chapter4...11)]()
- [書籍「テスト駆動開発」をRustで書く #2 (chapter4...11)](https://qiita.com/Khigashiguchi/items/6041308a46b30e849648)
- [書籍「テスト駆動開発」をRustで書く #3 (chapter7...11)](https://qiita.com/Khigashiguchi/items/61ffa5da34d957df01f1)

# Chapters
# 著書内容
## Chapters

- Chapter/1 仮実装
- Chapter/2 明白な実装
Expand All @@ -18,20 +20,27 @@
- Chapter/10 テストに聞いてみる
- Chapter/11 不要になったら消す

## TODO LIST in Chapters
- [ ]$5 + 10CHF = $10
- [x]$5 * 2 = $10
- [x]amountをprivateにする(すでにprivateだった)
- [x]Dollarの副作用どうする
- [ ]Moneyの丸め処理どうする
- [x]equals()の実装
- [ ]hashCode()の実装
- [ ]nullとの等価性比較
- [ ]他のオブジェクトとの等価性比較
- [x]5CHF * 2 = 10CHF
- [x]DollarとFrancの重複
- [x]equalsの一般化
- [x]timesの一般化
- [x]FrancとDollarを比較する
- [x]通貨の概念
- [x]testFrancMultiplicationを削除する?
### TODO LIST in Chapters
著書内でTDD進めていく際のTODOリストです。

#### TODO After chapter/12
- [ ] $5 + 10CHF = $10
- [ ] $5 + $5 = $10

#### TODO Before chapter/11
- [ ] $5 + 10CHF = $10
- [x] $5 * 2 = $10
- [x] amountをprivateにする(すでにprivateだった)
- [x] Dollarの副作用どうする
- [ ] Moneyの丸め処理どうする
- [x] equals()の実装
- [ ] hashCode()の実装
- [ ] nullとの等価性比較
- [ ] 他のオブジェクトとの等価性比較
- [x] 5CHF * 2 = 10CHF
- [x] DollarとFrancの重複
- [x] equalsの一般化
- [x] timesの一般化
- [x] FrancとDollarを比較する
- [x] 通貨の概念
- [x] testFrancMultiplicationを削除する?
30 changes: 30 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use std::ops::Mul;
use std::ops::Add;

pub trait Bank {
fn reduce(source: Money, to: &'static str) -> Money;
}

#[derive(Debug)]
pub struct Money {
Expand Down Expand Up @@ -41,6 +46,25 @@ impl Mul<u32> for Money {
}
}

impl Add for Money {
type Output = Self;

fn add(self, other: Self) -> Self {
Self {
amount: self.amount + other.amount,
currency: self.currency
}
}
}

impl Bank for Money {
fn reduce(source: Self, to: &'static str) -> Self {
Self {
amount: 10,
currency: "USD"
}
}
}
#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -62,4 +86,10 @@ mod tests {
assert_eq!("USD", Money::dollar(1).currency());
assert_eq!("CHF", Money::franc(1).currency());
}
#[test]
fn test_simple_addition() {
let sum = Money::dollar(5) + Money::dollar(5);
let reduced = Money::reduce(sum, "USD");
assert_eq!(Money::dollar(10), reduced);
}
}

0 comments on commit 3e08286

Please sign in to comment.