Last active
August 29, 2015 14:16
-
-
Save up1/b46464f4621fe5e658f7 to your computer and use it in GitHub Desktop.
Unit test :: demo 2
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
public class Money { | |
... | |
public Money add(Money money) { | |
return new Money(amount() + money.amount(), currency()); | |
} | |
... | |
} |
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
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); | |
} | |
... | |
} |
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
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); | |
} | |
} |
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
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; | |
} | |
... | |
} |
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
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); | |
} | |
} |
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
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)); | |
} | |
} |
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
public class MoneyBagTest { | |
... | |
@Test | |
public void mixedSimpleMoneyInBag() throws Exception { | |
Money[] bag = {money5THB, money3USD}; | |
MoneyBag expectedMoneyBag = new MoneyBag(bag); | |
assertEquals(expectedMoneyBag, money5THB.add(money3USD)); | |
} | |
... | |
} |
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
public interface Money { | |
Money add(Money 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
public interface Money { | |
Money add(Money money); | |
Money addSimpleMoney(SimpleMoney simpleMoney); | |
Money addMoneyBag(MoneyBag moneyBag); | |
} |
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
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); | |
} | |
} |
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
public class SimpleMoney implements Money { | |
... | |
} | |
public class MoneyBag implements 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
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