Skip to content

Commit

Permalink
✨ 【第14章 学習用テストと回帰テスト】 Money の変換を行うために無理やりテストを通す
Browse files Browse the repository at this point in the history
  • Loading branch information
dodonki1223 committed Dec 30, 2021
1 parent 5422b3a commit 0b95524
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/__tests__/money.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,10 @@ test('test reduce money', () => {
const result = bank.reduce(Money.dollar(1), 'USD');
expect(Money.dollar(1)).toEqual(result);
})

test('test reduce money different currency', () => {
const bank = new Bank();
bank.addRate('CHF', 'USD', 2);
const result = bank.reduce(Money.franc(2), 'USD');
expect(Money.dollar(1)).toEqual(result);
})
3 changes: 3 additions & 0 deletions src/bank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ export class Bank {
reduce(source: Expression, to: string):Money {
return source.reduce(to);
}

addRate(from: string, to: string, rate: number) {
}
}
3 changes: 2 additions & 1 deletion src/money.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class Money implements Expression {
}

reduce(to: string): Money {
return this;
const rate = this.currency === 'CHF' && to === 'USD' ? 2 : 1;
return new Money(this.amount / rate, to)
}
}

0 comments on commit 0b95524

Please sign in to comment.