Skip to content

Commit

Permalink
✨ 【第6章 テスト不足に気づいたら】 重複コード排除のため Money クラスに equals メソッドを追加する
Browse files Browse the repository at this point in the history
  • Loading branch information
dodonki1223 committed Dec 12, 2021
1 parent ce47286 commit e8384a9
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 43 deletions.
12 changes: 0 additions & 12 deletions src/__tests__/dollar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,3 @@ test('times', () => {
expect(five.times(2)).toEqual(new Dollar(10));
expect(five.times(3)).toEqual(new Dollar(15));
});

test('equals', () => {
expect(new Dollar(5).equals(new Dollar(5))).toBeTruthy();
expect(new Dollar(5).equals(new Dollar(6))).toBeFalsy();
});

test('null equals', () => {
const five = new Dollar(5);
const ten = five.times(2);

expect(ten.equals(null)).toBeFalsy();
});
12 changes: 0 additions & 12 deletions src/__tests__/franc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,3 @@ test('times', () => {
expect(five.times(2)).toEqual(new Franc(10));
expect(five.times(3)).toEqual(new Franc(15));
});

test('equals', () => {
expect(new Franc(5).equals(new Franc(5))).toBeTruthy();
expect(new Franc(5).equals(new Franc(6))).toBeFalsy();
});

test('null equals', () => {
const five = new Franc(5);
const ten = five.times(2);

expect(ten.equals(null)).toBeFalsy();
});
12 changes: 12 additions & 0 deletions src/__tests__/money.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Money } from '../money';

test('equals', () => {
expect(new Money(5).equals(new Money(5))).toBeTruthy();
expect(new Money(5).equals(new Money(6))).toBeFalsy();
});

test('null equals', () => {
const five = new Money(5);

expect(five.equals(null)).toBeFalsy();
});
10 changes: 1 addition & 9 deletions src/dollar.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import { Money } from "./money";

export class Dollar extends Money {
constructor(amount: number) {
constructor(amount: number) {
super(amount);
}

times(multiplier: number) {
return new Dollar(this.amount * multiplier)
}

equals(dollar: Dollar | null) {
if (dollar === null) {
return false;
}

return this.amount === dollar.amount
}
}
12 changes: 2 additions & 10 deletions src/franc.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import { Money } from "./money";

export class Franc extends Money {
export class Franc extends Money {
constructor(amount: number) {
super(amount);
 }
}

times(multiplier: number) {
return new Franc(this.amount * multiplier)
}

equals(franc: Franc | null) {
if (franc === null) {
return false;
}

return this.amount === franc.amount
}
}
8 changes: 8 additions & 0 deletions src/money.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
export class Money {
constructor(protected readonly amount: number) {
}

equals(money: Money | null) {
if (money === null) {
return false;
}

return this.amount === money.amount
}
}

0 comments on commit e8384a9

Please sign in to comment.