Skip to content

Commit 3e08286

Browse files
author
Kazuki Higashiguchi
authored
Merge pull request #15 from Khigashiguchi/chapter/12
Chapter/12 設計とメタファー
2 parents cb2e34f + 8efe1c9 commit 3e08286

2 files changed

Lines changed: 58 additions & 19 deletions

File tree

README.md

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
著書「テスト駆動開発」の第1章〜第17章の写経をrustで書いたものです。
33

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

7-
# Chapters
8+
# 著書内容
9+
## Chapters
810

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

21-
## TODO LIST in Chapters
22-
- [ ]$5 + 10CHF = $10
23-
- [x]$5 * 2 = $10
24-
- [x]amountをprivateにする(すでにprivateだった)
25-
- [x]Dollarの副作用どうする
26-
- [ ]Moneyの丸め処理どうする
27-
- [x]equals()の実装
28-
- [ ]hashCode()の実装
29-
- [ ]nullとの等価性比較
30-
- [ ]他のオブジェクトとの等価性比較
31-
- [x]5CHF * 2 = 10CHF
32-
- [x]DollarとFrancの重複
33-
- [x]equalsの一般化
34-
- [x]timesの一般化
35-
- [x]FrancとDollarを比較する
36-
- [x]通貨の概念
37-
- [x]testFrancMultiplicationを削除する?
23+
### TODO LIST in Chapters
24+
著書内でTDD進めていく際のTODOリストです。
25+
26+
#### TODO After chapter/12
27+
- [ ] $5 + 10CHF = $10
28+
- [ ] $5 + $5 = $10
29+
30+
#### TODO Before chapter/11
31+
- [ ] $5 + 10CHF = $10
32+
- [x] $5 * 2 = $10
33+
- [x] amountをprivateにする(すでにprivateだった)
34+
- [x] Dollarの副作用どうする
35+
- [ ] Moneyの丸め処理どうする
36+
- [x] equals()の実装
37+
- [ ] hashCode()の実装
38+
- [ ] nullとの等価性比較
39+
- [ ] 他のオブジェクトとの等価性比較
40+
- [x] 5CHF * 2 = 10CHF
41+
- [x] DollarとFrancの重複
42+
- [x] equalsの一般化
43+
- [x] timesの一般化
44+
- [x] FrancとDollarを比較する
45+
- [x] 通貨の概念
46+
- [x] testFrancMultiplicationを削除する?

src/lib.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
use std::ops::Mul;
2+
use std::ops::Add;
3+
4+
pub trait Bank {
5+
fn reduce(source: Money, to: &'static str) -> Money;
6+
}
27

38
#[derive(Debug)]
49
pub struct Money {
@@ -41,6 +46,25 @@ impl Mul<u32> for Money {
4146
}
4247
}
4348

49+
impl Add for Money {
50+
type Output = Self;
51+
52+
fn add(self, other: Self) -> Self {
53+
Self {
54+
amount: self.amount + other.amount,
55+
currency: self.currency
56+
}
57+
}
58+
}
59+
60+
impl Bank for Money {
61+
fn reduce(source: Self, to: &'static str) -> Self {
62+
Self {
63+
amount: 10,
64+
currency: "USD"
65+
}
66+
}
67+
}
4468
#[cfg(test)]
4569
mod tests {
4670
use super::*;
@@ -62,4 +86,10 @@ mod tests {
6286
assert_eq!("USD", Money::dollar(1).currency());
6387
assert_eq!("CHF", Money::franc(1).currency());
6488
}
89+
#[test]
90+
fn test_simple_addition() {
91+
let sum = Money::dollar(5) + Money::dollar(5);
92+
let reduced = Money::reduce(sum, "USD");
93+
assert_eq!(Money::dollar(10), reduced);
94+
}
6595
}

0 commit comments

Comments
 (0)