Skip to content

Commit

Permalink
✨ 【第13章 実装を導くテスト】 reduce メソッドで為替の変更ができるようにする
Browse files Browse the repository at this point in the history
・plus の結果を確かめるために Sum クラスの被加算数と加数が一致することのテストを追加している
  • Loading branch information
dodonki1223 committed Dec 19, 2021
1 parent 9717542 commit 6b434b2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/__tests__/money.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ test('times', () => {
expect(five.times(3)).toEqual(Money.dollar(15));
});

test('simple addition', () => {
const sum = Money.dollar(5).plus(Money.dollar(5));
test('test reduce sum', () => {
const sum = Money.dollar(3).plus(Money.dollar(4));
const bank = new Bank();
const reduced = bank.reduce(sum, 'USD');
expect(Money.dollar(10)).toEqual(reduced);
expect(Money.dollar(7)).toEqual(reduced);
});

test('testPlusReturnsSum', () => {
test('test plus returns sum', () => {
const five = Money.dollar(5);
const result = five.plus(five)
const sum = result as Sum
Expand Down
7 changes: 5 additions & 2 deletions src/bank.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Expression } from './expression';
import { Money } from './money';
import { Sum } from './sum';

export class Bank {
reduce(sum: Expression, currency: string):Money {
return Money.dollar(10);
reduce(source: Expression, to: string):Money {
const sum = source as Sum
const amount = sum.augend.amount + sum.addend.amount;
return new Money(amount, to);
}
}
2 changes: 1 addition & 1 deletion src/money.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Expression } from "./expression";
import { Sum } from "./sum";

export class Money implements Expression {
constructor(protected readonly amount: number, public readonly currency: string) {
constructor(public readonly amount: number, public readonly currency: string) {
}

static dollar(amount: number):Money {
Expand Down

0 comments on commit 6b434b2

Please sign in to comment.