소스 ¶
~cpp package dooly.tdd.vending; import junit.framework.Test; import junit.framework.TestSuite; public class VendingMachineTest extends TestSuite { public static Test suite() { TestSuite suite = new TestSuite("Test for dooly.tdd.vending"); //$JUnit-BEGIN$ suite.addTestSuite(PerchaseItemTest.class); suite.addTestSuite(RegistItemTest.class); //$JUnit-END$ return suite; } } package dooly.tdd.vending; import junit.framework.TestCase; public class PerchaseItemTest extends TestCase { private static final int GREEN_TEA_PRICE = 500; private static final int COFFEE_PRICE = 400; private static final int TEA_PRICE = 300; private VendingMachine vm; public void setUp() { vm = new VendingMachine(); vm.add("녹차", GREEN_TEA_PRICE); vm.add("커피", COFFEE_PRICE); vm.add("홍차", TEA_PRICE); } public void testWaitState() { assertEquals(0, vm.getMoney()); assertEquals(3, vm.getItems().size()); } public void testPutMoney() { vm.putMoney(1000); assertEquals(1000, vm.getMoney()); vm.putMoney(2000); assertEquals(1000 + 2000, vm.getMoney()); } public void testPerchase() { vm.putMoney(1000); assertEquals("녹차", vm.getItem("녹차")); assertEquals(1000 - GREEN_TEA_PRICE, vm.getMoney()); assertEquals("커피", vm.getItem("커피")); assertEquals(1000 - GREEN_TEA_PRICE - COFFEE_PRICE, vm.getMoney()); } public void testPerchaseWrongItem() { vm.putMoney(1000); assertEquals("", vm.getItem("cola")); assertEquals(1000, vm.getMoney()); assertEquals("커피", vm.getItem("커피")); assertEquals(1000 - COFFEE_PRICE, vm.getMoney()); } public void testShortMoney() { vm.putMoney(200); assertEquals("", vm.getItem("녹차")); assertEquals(200, vm.getMoney()); vm.putMoney(500); assertEquals("녹차", vm.getItem("녹차")); assertEquals(200 + 500 - GREEN_TEA_PRICE, vm.getMoney()); assertEquals("", vm.getItem("녹차")); assertEquals(200 + 500 - GREEN_TEA_PRICE, vm.getMoney()); } public void testRefund(){ vm.putMoney(100); assertEquals(100, vm.refund()); assertEquals(0, vm.getMoney()); vm.putMoney(200); assertEquals(200, vm.getMoney()); } } package dooly.tdd.vending; import junit.framework.TestCase; public class RegistItemTest extends TestCase { private VendingMachine vm; public void setUp() { vm = new VendingMachine(); } public void tearDown() { vm = null; } public void testEmptyMatchine() { assertEquals(0, vm.getItems().size()); } public void testAddItem() { vm.add("커피", 500); assertEquals(1, vm.getItems().size()); } public void testGetItem() { vm.add("커피", 500); assertEquals(500, vm.getPrice("커피")); } public void testGetWrongItem() { vm.add("커피", 500); assertEquals(0, vm.getPrice("녹차")); assertEquals(500, vm.getPrice("커피")); vm.add("녹차", 100); assertEquals(100, vm.getPrice("녹차")); } } package dooly.tdd.vending; import java.util.HashMap; import java.util.Map; public class VendingMachine { private Map itemMap = new HashMap(); private int money; public Map getItems() { return itemMap; } public void add(String item, int price) { itemMap.put(item, new Integer(price)); } public int getPrice(String item) { if (itemMap.containsKey(item)) return ((Integer) itemMap.get(item)).intValue(); return 0; } public int getMoney() { return money; } public void putMoney(int money) { if (money >= 0) { this.money += money; } } public String getItem(String item) { if (notExist(item)) return ""; if (shortMoneyFor(item)) return ""; this.money -= getPrice(item); return item; } private boolean shortMoneyFor(String item) { return money < getPrice(item); } private boolean notExist(String item) { return !itemMap.containsKey(item); } public int refund() { int refund = money; money = 0; return refund; } }
자바 버전으로 친구와 같이 tdd를 시도해보았습니다.