Unit tests

This commit is contained in:
iryupnik 2020-05-27 18:52:56 +02:00
parent 1f184dfe06
commit 1b6eb4d50a
10 changed files with 237 additions and 81 deletions

View File

@ -15,6 +15,8 @@ public class Battle {
private Timer timer; private Timer timer;
private BattlePerson turnBelongTo; private BattlePerson turnBelongTo;
private LinkedList<BattlePerson> turnOrder;
private int turnCount; private int turnCount;
private int delay; private int delay;
@ -67,6 +69,7 @@ public class Battle {
windowOfBattle.getMenuOfActions().setVisible(true); windowOfBattle.getMenuOfActions().setVisible(true);
windowOfBattle.getEnemyDescription().setVisible(true); windowOfBattle.getEnemyDescription().setVisible(true);
windowOfBattle.repaint(); windowOfBattle.repaint();
turnOrder = new LinkedList<>();
timer = new Timer(1000, actionEvent -> { timer = new Timer(1000, actionEvent -> {
for (Enemy e: enemies) System.out.println(e.health); for (Enemy e: enemies) System.out.println(e.health);
if (checkHealthOfEnemies()) endBattle(true); if (checkHealthOfEnemies()) endBattle(true);
@ -111,6 +114,7 @@ public class Battle {
//TODO Animation of Death //TODO Animation of Death
if (e.health <= 0) enemies.remove(e); if (e.health <= 0) enemies.remove(e);
turnOrder.add(turnBelongTo);
playerAttack = false; playerAttack = false;
turnBelongTo = null; turnBelongTo = null;
delay = 2; delay = 2;
@ -123,10 +127,18 @@ public class Battle {
LinkedList<BattlePerson> turns = new LinkedList<>(enemies); LinkedList<BattlePerson> turns = new LinkedList<>(enemies);
turns.addAll(heroes); turns.addAll(heroes);
//TODO Check turns (maybe List)
if (checkIfTurnOrderIsFull(turns))
turnOrder = new LinkedList<>();
Collections.shuffle(turns); Collections.shuffle(turns);
turnBelongTo = turns.getFirst(); turnBelongTo = turns.getFirst();
while (turnOrder.contains(turnBelongTo)){
Collections.shuffle(turns);
turnBelongTo = turns.getFirst();
}
System.out.println(turnBelongTo.getClass().getName()); System.out.println(turnBelongTo.getClass().getName());
} }
@ -138,6 +150,8 @@ public class Battle {
//TODO Animation of Attack //TODO Animation of Attack
//TODO Animation of Death //TODO Animation of Death
if (e.health <= 0) heroes.remove(e); if (e.health <= 0) heroes.remove(e);
turnOrder.add(turnBelongTo);
delay = 2; delay = 2;
turnBelongTo = null; turnBelongTo = null;
} }
@ -161,4 +175,11 @@ public class Battle {
public boolean isPlayerAttack() { public boolean isPlayerAttack() {
return playerAttack; return playerAttack;
} }
private boolean checkIfTurnOrderIsFull(LinkedList<BattlePerson> allBattlePerson){
for(BattlePerson battlePerson: allBattlePerson){
if (!turnOrder.contains(battlePerson)) return false;
}
return true;
}
} }

View File

@ -269,7 +269,7 @@ public class InteractionCell extends Cell {
} }
public void useItem(Item item) { public void useItem(Item item) {
InventoryManager.removeItem(item); InventoryManager.removeItem(item, 1);
} }
@Override @Override

View File

@ -5,24 +5,38 @@ import java.util.LinkedList;
public class InventoryManager { public class InventoryManager {
private static LinkedList<Item> inventory = new LinkedList<Item>(); private static LinkedList<Item> inventory = new LinkedList<Item>();
public static void addItem(Item item) { public static void addItem(Item item) {
for(Item item1: inventory){
if(item1.equals(item)) {
item1.setMultiplicity(item1.getMultiplicity() + item.getMultiplicity());
return;
}
}
inventory.add(item); inventory.add(item);
} }
public static void flushInventory() { public static void flushInventory() {
inventory = null; inventory = new LinkedList<>();
} }
public static boolean isInInventory(Item item) { public static boolean isInInventory(Item item) {
for (Item inventoryItem : inventory) { for (Item inventoryItem : inventory) {
if (inventoryItem == item) return true; if (inventoryItem.equals(item)) return true;
} }
return false; return false;
} }
public static void removeItem(Item item) { public static boolean removeItem(Item item, int count) {
inventory.remove(item); if (isInInventory(item)) {
Item itemIn = findItemByName(item.getName());
if (itemIn.getMultiplicity() > count)
itemIn.setMultiplicity(itemIn.getMultiplicity() - count);
else if (itemIn.getMultiplicity() == count)
inventory.remove(item);
else return false;
return true;
}
return false;
} }
public static LinkedList<Item> getInventory() { return inventory; } public static LinkedList<Item> getInventory() { return inventory; }

View File

@ -1,5 +1,7 @@
package ruscz.game.gameOfProd.Inventory; package ruscz.game.gameOfProd.Inventory;
import java.util.Objects;
public class Item { public class Item {
private ItemAction itemAction; private ItemAction itemAction;
private String name; private String name;
@ -52,7 +54,24 @@ public class Item {
public int getMultiplicity() { return multiplicity; } public int getMultiplicity() { return multiplicity; }
public void setMultiplicity(int multiplicity) { this.multiplicity = multiplicity; }
public ItemAction getItemAction() { public ItemAction getItemAction() {
return itemAction; return itemAction;
} }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Item item = (Item) o;
return image == item.image &&
itemAction == item.itemAction &&
name.equals(item.name);
}
@Override
public int hashCode() {
return Objects.hash(itemAction, name, image);
}
} }

View File

@ -1,5 +1,5 @@
36 36 36 36
EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK SPAWN BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY
EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY
EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY
EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY
@ -23,7 +23,7 @@ EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EM
EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY
EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY
BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK EMPTY BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK EMPTY BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK EMPTY BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK EMPTY BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK
EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY SPAWN EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY
BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK EMPTY BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK EMPTY BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK EMPTY BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK EMPTY BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK
BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY BLOCK EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY BLOCK EMPTY EMPTY EMPTY BLOCK EMPTY EMPTY BLOCK BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY BLOCK EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY BLOCK EMPTY EMPTY EMPTY BLOCK EMPTY EMPTY BLOCK
BLOCK EMPTY BLOCK EMPTY BLOCK EMPTY BLOCK EMPTY BLOCK EMPTY BLOCK EMPTY BLOCK EMPTY BLOCK EMPTY BLOCK BLOCK BLOCK BLOCK BLOCK EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY BLOCK EMPTY EMPTY EMPTY BLOCK EMPTY EMPTY BLOCK BLOCK EMPTY BLOCK EMPTY BLOCK EMPTY BLOCK EMPTY BLOCK EMPTY BLOCK EMPTY BLOCK EMPTY BLOCK EMPTY BLOCK BLOCK BLOCK BLOCK BLOCK EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY BLOCK EMPTY EMPTY EMPTY BLOCK EMPTY EMPTY BLOCK

View File

@ -1,65 +0,0 @@
<map>
<mapName>tutorial</mapName>
<events>
<trigger>
<x>11</x>
<y>6</y>
<active>false</active>
</trigger>
</events>
</map>
<map>
<mapName>openspace</mapName>
<events>
<trigger>
<x>6</x>
<y>4</y>
<active>true</active>
</trigger>
<trigger>
<x>14</x>
<y>16</y>
<active>false</active>
</trigger>
<chest>
<x>15</x>
<y>7</y>
<active>true</active>
</chest>
</events>
</map>
<map>
<mapName>office</mapName>
<events>
<trigger>
<x>4</x>
<y>2</y>
<active>false</active>
</trigger>
<chest>
<x>1</x>
<y>9</y>
<active>true</active>
</chest>
</events>
</map>
<map>
<mapName>boss</mapName>
<events>
<trigger>
<x>4</x>
<y>18</y>
<active>true</active>
</trigger>
</events>
</map>
<map>
<mapName>treasure</mapName>
<events>
<trigger>
<x>6</x>
<y>1</y>
<active>true</active>
</trigger>
</events>
</map>

View File

@ -1,6 +0,0 @@
<location>
<map>office</map>
<x>8</x>
<y>7</y>
<direction>3</direction>
</location>

View File

@ -1 +0,0 @@
TODO: Village

View File

@ -55,6 +55,41 @@ public class BattleTest {
} }
@Test
public void startBattle_battleWithAMightEnemy_winOfEnemy(){
//Arrange
Battle battle;
Hero h;
Enemy e;
LinkedList<Hero> llh;
LinkedList<Enemy> lle;
Game game;
//Act
game = new Game();
game.setVisible(false);
h = new Hero(1, 1, "123", 1, 1, 0);
e = new Enemy(2,100,"2", 2, 1);
llh = new LinkedList<>(Collections.singleton(h));
lle = new LinkedList<>(Collections.singleton(e));
battle = new Battle(llh, lle, new WindowOfBattle(game));
battle.startBattle();
battle.setPlayerAttack(true);
battle.setTimerStopped(false);
battle.getTimer().start();
timerOfWaiting(battle);
//Assertion
Assertions.assertTrue(battle.checkHealthOfHeroes());
Assertions.assertFalse(battle.checkHealthOfEnemies());
}
private void timerOfWaiting(Battle battle){ private void timerOfWaiting(Battle battle){
while (!battle.isTimerStopped()){ while (!battle.isTimerStopped()){
sleep(1000); sleep(1000);

View File

@ -0,0 +1,139 @@
package ruscz.game.gameOfProd.Inventory;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class InventoryManagerTest {
@AfterEach
public void flush(){
InventoryManager.flushInventory();
}
@Test
public void addItem_addItemInInventory_successfulAdd(){
//Arrange
Item i1, i2, i3;
//Act
i1 = new Item("Soda", 10, 1, "heal");
i2 = new Item("sODa", 1, 2, "mana");
i3 = new Item("Key", 1, 3, "key");
InventoryManager.addItem(i1);
InventoryManager.addItem(i2);
InventoryManager.addItem(i3);
//Assert
Assertions.assertTrue(InventoryManager.getInventory().contains(i1));
Assertions.assertTrue(InventoryManager.getInventory().contains(i2));
Assertions.assertTrue(InventoryManager.getInventory().contains(i3));
}
@Test
public void removeItem_removeItemFromInventory_successfulRemove(){
//Arrange
Item i1, i2, i3;
//Act
i1 = new Item("Soda", 10, 1, "heal");
i2 = new Item("sODa", 1, 2, "mana");
i3 = new Item("Key", 1, 3, "key");
InventoryManager.addItem(i1);
InventoryManager.addItem(i2);
InventoryManager.addItem(i3);
InventoryManager.removeItem(i2, 1);
//Assert
Assertions.assertTrue(InventoryManager.getInventory().contains(i1));
Assertions.assertFalse(InventoryManager.getInventory().contains(i2));
Assertions.assertTrue(InventoryManager.getInventory().contains(i3));
}
@Test
public void addItem_increaseMultiplicityOfItem_successfulIncreasing(){
//Arrange
Item i1, i2, i3;
//Act
i1 = new Item("Soda", 10, 1, "heal");
i2 = new Item("Soda", 1, 1, "heal");
i3 = new Item("Soda", 1, 1, "heal");
InventoryManager.addItem(i1);
InventoryManager.addItem(i2);
InventoryManager.addItem(i3);
//Assert
Assertions.assertTrue(InventoryManager.getInventory().contains(i1));
Assertions.assertEquals(12, InventoryManager.getInventory().getFirst().getMultiplicity());
}
@Test
public void removeItem_decreaseMultiplicityOfItem_successfulReduce(){
//Arrange
Item i1, i2, i3;
//Act
i1 = new Item("Soda", 10, 1, "heal");
i2 = new Item("Soda", 1, 1, "heal");
i3 = new Item("Soda", 1, 1, "heal");
InventoryManager.addItem(i1);
InventoryManager.addItem(i2);
InventoryManager.addItem(i3);
InventoryManager.removeItem(i1, 1);
//Assert
Assertions.assertTrue(InventoryManager.getInventory().contains(i1));
Assertions.assertEquals(11, InventoryManager.getInventory().getFirst().getMultiplicity());
}
@Test
public void removeItem_tryDecreaseNonExistedItemOrDecreaseTooMuch_unsuccessfulReduce(){
//Arrange
Item i1, i2;
//Act
i1 = new Item("Soda", 10, 1, "heal");
i2 = new Item("SoDa", 1, 1, "mana");
InventoryManager.addItem(i1);
//Assert
Assertions.assertFalse(InventoryManager.removeItem(i2, 10));
Assertions.assertFalse(InventoryManager.removeItem(i1, 100));
Assertions.assertEquals(10, InventoryManager.getInventory().getFirst().getMultiplicity());
}
@Test
public void findItemByName_searchingExistedItem_Item(){
//Arrange
Item i1;
//Act
i1 = new Item("Soda", 10, 1, "heal");
InventoryManager.addItem(i1);
//Assert
Assertions.assertEquals(i1, InventoryManager.findItemByName(i1.getName()));
}
@Test
public void findItemByName_trySearchingExistedItem_null(){
//Arrange
Item i1;
//Act
i1 = new Item("Soda", 10, 1, "heal");
//Assert
Assertions.assertNull(InventoryManager.findItemByName(i1.getName()));
}
}