|
1 | 1 | package com.codedifferently.lesson16.ShawnDunsmore;
|
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
4 |
| - |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | +import java.util.Collections; |
5 | 6 | import java.util.ArrayList;
|
6 | 7 | import org.junit.jupiter.api.Test;
|
7 | 8 |
|
@@ -61,4 +62,99 @@ public void testPayOut() {
|
61 | 62 | SlotMachine slotMachine2 = new SlotMachine(1, 10, "Test Slot 2", BuyType.BONUS_BUY, icons, 10);
|
62 | 63 | assertEquals(30, slotMachine2.payOut(), "Expected payout for BONUS_BUY to be 30");
|
63 | 64 | }
|
| 65 | + @Test |
| 66 | + public void testSetNumOfSlots() { |
| 67 | + SlotMachine slotMachine = new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, new ArrayList<>(), 10); |
| 68 | + |
| 69 | + slotMachine.setNumOfSlots(5); |
| 70 | + |
| 71 | + assertEquals(5, slotMachine.getNumOfSlots(), "The number of slots should be updated to 5"); |
| 72 | + } |
| 73 | + @Test |
| 74 | + public void testSetPayAmount() { |
| 75 | + SlotMachine slotMachine = new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, new ArrayList<>(), 10); |
| 76 | + |
| 77 | + slotMachine.setPayAmount(20); |
| 78 | + |
| 79 | + assertEquals(20, slotMachine.getPayAmount(), "The pay amount should be updated to 20"); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testSetName() { |
| 84 | + SlotMachine slotMachine = new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, new ArrayList<>(), 10); |
| 85 | + |
| 86 | + slotMachine.setName("Lucky Slot"); |
| 87 | + |
| 88 | + assertEquals("Lucky Slot", slotMachine.getName(), "The name should be updated to 'Lucky Slot'"); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testSetBuyType() { |
| 93 | + SlotMachine slotMachine = new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, new ArrayList<>(), 10); |
| 94 | + |
| 95 | + slotMachine.setBuyType(BuyType.BONUS_BUY); |
| 96 | + |
| 97 | + assertEquals(BuyType.BONUS_BUY, slotMachine.getBuyType(), "The buy type should be updated to BONUS_BUY"); |
| 98 | + } |
| 99 | + @Test |
| 100 | + public void testSetIconList() { |
| 101 | + SlotMachine slotMachine = new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, new ArrayList<>(), 10); |
| 102 | + |
| 103 | + ArrayList<String> newIcons = new ArrayList<>(); |
| 104 | + newIcons.add("π"); |
| 105 | + newIcons.add("π"); |
| 106 | + newIcons.add("π"); |
| 107 | + |
| 108 | + slotMachine.setIconList(newIcons); |
| 109 | + |
| 110 | + assertEquals(newIcons, slotMachine.getIconList(), "The icon list should be updated with the new icons"); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void testSetMoneyNeeded() { |
| 115 | + SlotMachine slotMachine = new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, new ArrayList<>(), 10); |
| 116 | + |
| 117 | + slotMachine.setMoneyNeeded(15); |
| 118 | + |
| 119 | + assertEquals(15, slotMachine.getMoneyNeeded(), "The money needed should be updated to 15"); |
| 120 | + } |
| 121 | + @Test |
| 122 | + public void testSpinThrowsExceptionWhenMoneyIsInsufficient() { |
| 123 | + ArrayList<String> icons = new ArrayList<>(); |
| 124 | + icons.add("π"); |
| 125 | + icons.add("π"); |
| 126 | + icons.add("π"); |
| 127 | + |
| 128 | + SlotMachine slotMachine = new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, icons, 10); |
| 129 | + |
| 130 | + Exception exception = assertThrows(InvalidPayAmountException.class, () -> { |
| 131 | + slotMachine.spin(5); |
| 132 | + }); |
| 133 | + |
| 134 | + assertEquals("Amount inavalid", exception.getMessage()); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void testSpinShufflesIconList() throws InvalidPayAmountException { |
| 139 | + ArrayList<String> icons = new ArrayList<>(); |
| 140 | + icons.add("π"); |
| 141 | + icons.add("π"); |
| 142 | + icons.add("π"); |
| 143 | + |
| 144 | + SlotMachine slotMachine = new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, icons, 10); |
| 145 | + |
| 146 | + ArrayList<String> originalIcons = new ArrayList<>(slotMachine.getIconList()); |
| 147 | + |
| 148 | + slotMachine.spin(10); |
| 149 | + |
| 150 | + ArrayList<String> shuffledIcons = slotMachine.getIconList(); |
| 151 | + |
| 152 | + Collections.sort(originalIcons); |
| 153 | + Collections.sort(shuffledIcons); |
| 154 | + |
| 155 | + assertEquals(originalIcons, shuffledIcons, "The icon list should have the same contents after spinning"); |
| 156 | + } |
64 | 157 | }
|
| 158 | + |
| 159 | + |
| 160 | + |
0 commit comments