Created
January 5, 2018 14:57
-
-
Save TomckySan/cc4232e4d081addb1b00344fa8ad775d to your computer and use it in GitHub Desktop.
「テスト駆動開発」JS(AVA)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Bank { | |
constructor() { | |
this.rates = new Map(); | |
} | |
reduce(source, to) { | |
return source.reduce(this, to); | |
} | |
addRate(from, to, rate) { | |
this.rates.set(`${from}_${to}`, rate); | |
} | |
rate(from, to) { | |
if (from === to) return 1; | |
return this.rates.get(`${from}_${to}`); | |
} | |
} | |
class Sum { | |
constructor(augend, addend) { | |
this.augend = augend; | |
this.addend = addend; | |
} | |
times(multiplier) { | |
return new Sum(this.augend.times(multiplier), this.addend.times(multiplier)); | |
} | |
plus(addend) { | |
return new Sum(this, addend); | |
} | |
reduce(bank, to) { | |
const amount = this.augend.reduce(bank, to).amount + this.addend.reduce(bank, to).amount; | |
return new Money(amount, to); | |
} | |
} | |
class Money { | |
constructor(amount, currency) { | |
this.amount = amount; | |
this.currency = currency; | |
} | |
times(multiplier) { | |
return new Money(this.amount * multiplier, this.currency); | |
} | |
plus(addend) { | |
return new Sum(this, addend); | |
} | |
reduce(bank, to) { | |
const rate = bank.rate(this.currency, to); | |
return new Money(this.amount / rate, to); | |
} | |
getCurrency() { | |
return this.currency; | |
} | |
equals(money) { | |
return this.amount === money.amount && this.currency === money.getCurrency(); | |
} | |
static dollar(amount) { | |
return new Money(amount, 'USD'); | |
} | |
static franc(amount) { | |
return new Money(amount, 'CHF'); | |
} | |
} | |
export { Bank, Sum, Money } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import test from 'ava'; | |
import 'babel-register'; | |
import { Bank, Sum, Money } from '../src/js/main'; | |
test('Test Multiplication.', t => { | |
const five = Money.dollar(5); | |
t.is(Money.dollar(10).amount, five.times(2).amount); | |
t.is(Money.dollar(15).amount, five.times(3).amount); | |
}); | |
test('Test Equality.', t => { | |
t.true(Money.dollar(5).equals(Money.dollar(5))); | |
t.false(Money.dollar(5).equals(Money.dollar(6))); | |
// ドルとフランが一緒じゃないことをテストする | |
t.false(Money.dollar(5).equals(Money.franc(5))); | |
}); | |
test('Test Currency.', t => { | |
t.is('USD', Money.dollar(1).getCurrency()); | |
t.is('CHF', Money.franc(1).getCurrency()); | |
}); | |
test('Test Simple Addition.', t => { | |
const five = Money.dollar(5); | |
const sum = five.plus(five); | |
const bank = new Bank(); | |
const reduced = bank.reduce(sum, 'USD'); | |
t.true(Money.dollar(10).equals(reduced)); | |
}); | |
test('Test Plus Returns Sum.', t => { | |
const five = Money.dollar(5); | |
const sum = five.plus(five); | |
t.true(five.equals(sum.augend)); | |
t.true(five.equals(sum.addend)); | |
}); | |
test('Test Reduce Sum.', t => { | |
const sum = new Sum(Money.dollar(3), Money.dollar(4)); | |
const bank = new Bank(); | |
const result = bank.reduce(sum, 'USD'); | |
t.true(Money.dollar(7).equals(result)); | |
}); | |
test('Test Reduce Money.', t => { | |
const bank = new Bank(); | |
const result = bank.reduce(Money.dollar(1), 'USD'); | |
t.true(Money.dollar(1).equals(result)); | |
}); | |
test('Test Reduce Money Different Currency.', t => { | |
const bank = new Bank(); | |
bank.addRate('CHF', 'USD', 2); | |
const result = bank.reduce(Money.franc(2), 'USD'); | |
t.true(Money.dollar(1).equals(result)); | |
}); | |
test('Test Identity Rate.', t => { | |
t.is(1, new Bank().rate('USD', 'USD')); | |
}); | |
test('Test Identity Rate.', t => { | |
const bank = new Bank(); | |
bank.addRate('CHF', 'USD', 2); | |
t.is(2, bank.rate('CHF', 'USD')); | |
}); | |
test('Test Mixed Addision.', t => { | |
const fiveBucks = Money.dollar(5); | |
const tenFrancs = Money.franc(10); | |
const bank = new Bank(); | |
bank.addRate('CHF', 'USD', 2); | |
const result = bank.reduce(fiveBucks.plus(tenFrancs), 'USD'); | |
t.true(Money.dollar(10).equals(result)); | |
}); | |
test('Test Sum Plus Money.', t => { | |
const fiveBucks = Money.dollar(5); | |
const tenFrancs = Money.franc(10); | |
const bank = new Bank(); | |
bank.addRate('CHF', 'USD', 2); | |
const sum = new Sum(fiveBucks, tenFrancs).plus(fiveBucks); | |
const result = bank.reduce(sum, 'USD'); | |
t.true(Money.dollar(15).equals(result)); | |
}); | |
test('Test Sum Times.', t => { | |
const fiveBucks = Money.dollar(5); | |
const tenFrancs = Money.franc(10); | |
const bank = new Bank(); | |
bank.addRate('CHF', 'USD', 2); | |
const sum = new Sum(fiveBucks, tenFrancs).times(2); | |
const result = bank.reduce(sum, 'USD'); | |
t.true(Money.dollar(20).equals(result)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment