Skip to content

Instantly share code, notes, and snippets.

@up1
Last active August 29, 2015 14:16
Show Gist options
  • Save up1/b46464f4621fe5e658f7 to your computer and use it in GitHub Desktop.
Save up1/b46464f4621fe5e658f7 to your computer and use it in GitHub Desktop.
Unit test :: demo 2
public class Money {
...
public Money add(Money money) {
return new Money(amount() + money.amount(), currency());
}
...
}
public class Money {
...
public Money add(Money money) {
if (money.currency().equals(currency())) {
return new Money(amount() + money.amount(), currency());
}
return new MoneyBag(this, money);
}
...
}
public class MoneyBag {
private List<Money> monies = new ArrayList<Money>();
public MoneyBag(Money firstMoney, Money secondMoney) {
appendMoney(firstMoney);
appendMoney(secondMoney);
}
public MoneyBag(Money[] moneyBag) {
for (Money money : moneyBag) {
appendMoney(money);
}
}
private void appendMoney(Money money) {
monies.add(money);
}
}
public class MoneyBag {
...
public boolean equals(Object anObject) {
if (anObject instanceof MoneyBag) {
List<Money> target = ((MoneyBag) anObject).monies;
if( target.size() == monies.size()) {
return target.equals(monies);
}
}
return false;
}
...
}
public class MoneyBag implements Money {
public Money add(Money money) {
return money.addMoneyBag(this);
}
public Money addSimpleMoney(SimpleMoney simpleMoney) {
return new MoneyBag(simpleMoney, this);
}
public Money addMoneyBag(MoneyBag moneyBag) {
return new MoneyBag(moneyBag, this);
}
}
public class MoneyBagTest {
private Money money5THB;
private Money money10THB;
private Money money3USD;
private Money money8USD;
private MoneyBag firstMoneyBag;
private MoneyBag secondMoneyBag;
@Before
public void setupFixture() {
money5THB = new Money(5, "THB");
money10THB = new Money(10, "THB");
money3USD = new Money(3, "USD");
money8USD = new Money(8, "USD");
firstMoneyBag = new MoneyBag(money5THB, money3USD);
secondMoneyBag = new MoneyBag(new Money[] { money10THB, money8USD });
}
@Test
public void equalsMoneyBag() throws Exception {
assertTrue(!firstMoneyBag.equals(null));
assertEquals(firstMoneyBag, firstMoneyBag);
assertEquals(firstMoneyBag, new MoneyBag(money5THB, money3USD));
assertTrue(!firstMoneyBag.equals(money5THB));
assertTrue(!money5THB.equals(firstMoneyBag));
assertTrue(!firstMoneyBag.equals(secondMoneyBag));
}
}
public class MoneyBagTest {
...
@Test
public void mixedSimpleMoneyInBag() throws Exception {
Money[] bag = {money5THB, money3USD};
MoneyBag expectedMoneyBag = new MoneyBag(bag);
assertEquals(expectedMoneyBag, money5THB.add(money3USD));
}
...
}
public interface Money {
Money add(Money money);
}
public interface Money {
Money add(Money money);
Money addSimpleMoney(SimpleMoney simpleMoney);
Money addMoneyBag(MoneyBag moneyBag);
}
public class SimpleMoney implements Money {
public Money add(Money money) {
return money.addSimpleMoney(this);
}
public Money addSimpleMoney(SimpleMoney simpleMoney) {
if (simpleMoney.currency().equals(currency())) {
return new SimpleMoney(amount() + simpleMoney.amount(), currency());
}
return new MoneyBag(simpleMoney, this);
}
public Money addMoneyBag(MoneyBag moneyBag) {
return moneyBag.addSimpleMoney(this);
}
}
public class SimpleMoney implements Money {
...
}
public class MoneyBag implements Money {
...
}
class SimpleMoney implements Money {
public Money add(Money money) {
return money.addMoney(this);
}
}
class MoneyBag implements Money {
public Money add(Money money) {
return money.addMoneyBag(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment