Skip to content

Commit

Permalink
Add some missing model tests (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
hjaremko committed Jun 10, 2020
1 parent 0dee3ef commit 637320f
Show file tree
Hide file tree
Showing 16 changed files with 452 additions and 17 deletions.
12 changes: 7 additions & 5 deletions src/main/java/pl/uj/io/cuteanimals/location/LocationBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,27 @@
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import pl.uj.io.cuteanimals.action.*;
import pl.uj.io.cuteanimals.model.Backpack;
import pl.uj.io.cuteanimals.model.DefaultLocation;
import pl.uj.io.cuteanimals.model.NPC;
import pl.uj.io.cuteanimals.model.interfaces.IAction;
import pl.uj.io.cuteanimals.model.interfaces.IEquipment;
import pl.uj.io.cuteanimals.model.interfaces.IItem;

/** Helper class used to build locations during WorldMap initialization */
public class LocationBuilder {
private final DefaultLocation location;
private final Map<String, IAction> actionMap;
private final List<NPC> npcList;
private final List<IEquipment> equipmentList;
private final IEquipment equipment;
private String description;
private IAction actionOnEnter;

public LocationBuilder(DefaultLocation location) {
this.description = "";
this.actionMap = new HashMap<>();
this.npcList = new ArrayList<>();
this.equipmentList = new ArrayList<>();
this.equipment = new Backpack();
this.actionOnEnter = null;
this.location = location;
}
Expand Down Expand Up @@ -68,8 +70,8 @@ public LocationBuilder addNPC(@NotNull NPC npc) {
return this;
}

public LocationBuilder addItem(@NotNull IEquipment equipment) {
this.equipmentList.add(equipment);
public LocationBuilder addItem(@NotNull IItem item) {
this.equipment.putItem(item);
return this;
}

Expand All @@ -81,7 +83,7 @@ public LocationBuilder addActionOnEnter(@NotNull IAction action) {
public DefaultLocation build() {
location.setDescription(description);
location.setAvailableActions(actionMap);
location.setItems(equipmentList);
location.setItems(equipment);
location.setNPCs(npcList);
location.setActionOnEnter(actionOnEnter);
return location;
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/pl/uj/io/cuteanimals/model/DefaultLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ public class DefaultLocation implements ILocation {
protected String description;
protected Map<String, IAction> actionMap;
protected List<NPC> npcList;
protected List<IEquipment> equipmentList;
// TODO: remove probably
protected IEquipment equipment;
protected IAction actionOnEnter;

public DefaultLocation() {
this.description = "";
this.actionMap = new HashMap<>();
this.npcList = new ArrayList<>();
this.equipmentList = new ArrayList<>();
this.equipment = new Backpack();
this.actionOnEnter = null;
}

Expand Down Expand Up @@ -55,12 +56,12 @@ public void setNPCs(List<NPC> npcList) {
}

@Override
public List<IEquipment> getItems() {
return equipmentList;
public IEquipment getItems() {
return equipment;
}

public void setItems(List<IEquipment> items) {
this.equipmentList = items;
public void setItems(IEquipment items) {
this.equipment = items;
}

@Override
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/pl/uj/io/cuteanimals/model/PlayerAttributes.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public PlayerAttributes(ICharacter owner) {
}

public PlayerAttributes(ICharacter owner, RandomInteger random) {
super(30, 1, 1, 0, 100);
super(50, 1, 1, 0, 100);
this.owner = owner;
this.experience = 0;
this.random = random;
Expand Down Expand Up @@ -79,14 +79,19 @@ public void addExperience(int experience) {
if (levelUp) {
addAttack(getLevel());
addDefence(getLevel());
// addMana(getMana() * getLevel());
addMana(100);
addHealth(getLevel() * 10);
addLevel(1);
}
}

public int getMana() {
return mana;
return mana
+ owner.getArmor()
.getItems()
.stream()
.mapToInt(i -> i.getAttributes().getMana())
.sum();
}

public void addMana(int mana) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public interface ILocation {
* Gives a list of items that are currently in the Location that the Player can integrate with
* (take them, use them, drink/eat them etc.).
*
* @return list of elements of IEquipment type.
* @return IEquipment containing items.
*/
List<IEquipment> getItems();
IEquipment getItems();

/**
* Lets location execute any action on player when entering it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ void setUp() {

@Test
void shouldDrain20Mana() {
attrs.addLevel(10); // Prevent level-up
var manaBefore = attrs.getMana();
player.getFightManager().attack();
assertThat(attrs.getMana()).isEqualTo(manaBefore - 20);
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/pl/uj/io/cuteanimals/model/ArcherTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package pl.uj.io.cuteanimals.model;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class ArcherTest {
private Archer m;

@BeforeEach
void setUp() {
m = new Archer();
}

@Test
void testToString() {
assertThat(m.toString()).isEqualTo("Archer");
}

@Test
void getAbilities() {
assertThat(m.getAbilities()).isNotEmpty();
assertThat(m.getAbilities()).containsKeys("focus");
assertThat(m.getAbilities()).containsKeys("bullseye");
}
}
15 changes: 14 additions & 1 deletion src/test/java/pl/uj/io/cuteanimals/model/ArmorBackpackTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ void removeArmor() {

@Test
void removeNotEquippedShouldFail() {
when(attrMock.getLevel()).thenReturn(1);

var weapon =
new Item(1, "pach", "aaa", 1, new Attributes(1, 1, 1, 1, 1, 1), ItemType.WEAPON);

Expand All @@ -174,10 +176,21 @@ void removeNotEquippedShouldFail() {
assertThat(!eq.removeItem(armor));
assertThat(eq.getItems().contains(weapon));
assertThat(!eq.getItems().contains(armor));
assertThat(eq.getItems().size()).isEqualTo(1);
}

@Test
void showItemsDoesntCrash() {
assertThat(!eq.showItems().isBlank());
when(attrMock.getLevel()).thenReturn(1);

assertThat(eq.showItems()).isEqualTo("");

var weapon =
new Item(1, "pach", "aaa", 1, new Attributes(1, 1, 1, 1, 1, 1), ItemType.WEAPON);
var armor = new Item(1, "pach", "aaa", 1, new Attributes(1, 1, 1, 1, 1, 1), ItemType.ARMOR);
assertThat(eq.putItem(weapon));
assertThat(eq.showItems()).isNotEqualTo("");
assertThat(eq.putItem(armor));
assertThat(eq.showItems()).isNotEqualTo("");
}
}
80 changes: 80 additions & 0 deletions src/test/java/pl/uj/io/cuteanimals/model/DefaultLocationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package pl.uj.io.cuteanimals.model;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;

import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import pl.uj.io.cuteanimals.model.entity.Attributes;
import pl.uj.io.cuteanimals.model.entity.Item;
import pl.uj.io.cuteanimals.model.interfaces.IAction;
import pl.uj.io.cuteanimals.model.interfaces.IPlayer;

@ExtendWith(MockitoExtension.class)
class DefaultLocationTest {
@Mock IAction ac;
@Mock IPlayer p;
private DefaultLocation loc;

@BeforeEach
void setUp() {
loc = new DefaultLocation();
loc.setDescription("desc");
}

@Test
void getDescription() {
assertThat(loc.getDescription()).isEqualTo("desc");
}

@Test
void getAvailableActions() {}

@Test
void setAvailableActions() {}

@Test
void getNPCs() {
var claudius =
new NPC(
null,
null,
"Claudius",
List.of("If I were you I would run as fast as I can..."));
var mag1 = new NPC(null, null, "Herschel", List.of("Please... help us!"));

loc.setNPCs(List.of(claudius, mag1));
assertThat(loc.getNPCs()).contains(mag1);
assertThat(loc.getNPCs()).contains(claudius);
}

@Test
void getItems() {
var armor = new Item(1, "pach", "aaa", 1, new Attributes(1, 1, 1, 1, 1, 1), ItemType.ARMOR);
var armor2 =
new Item(2, "pach", "aaa", 1, new Attributes(1, 1, 1, 1, 1, 1), ItemType.ARMOR);
var bp = new Backpack();
bp.putItem(armor2);
bp.putItem(armor);
loc.setItems(bp);
assertThat(loc.getItems().getItems()).contains(armor);
assertThat(loc.getItems().getItems()).contains(armor2);
}

@Test
void actionOnEnter() {
loc.setActionOnEnter(ac);
assertThat(loc.getActionOnEnter()).isEqualTo(ac);
loc.onEnter(p);
verify(ac).execute(p);
}

@Test
void nullActionOnEnter() {
assertThat(loc.onEnter(p).getMessage()).isBlank();
}
}
27 changes: 27 additions & 0 deletions src/test/java/pl/uj/io/cuteanimals/model/MagicianTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package pl.uj.io.cuteanimals.model;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class MagicianTest {
private Magician m;

@BeforeEach
void setUp() {
m = new Magician();
}

@Test
void testToString() {
assertThat(m.toString()).isEqualTo("Magician");
}

@Test
void getAbilities() {
assertThat(m.getAbilities()).isNotEmpty();
assertThat(m.getAbilities()).containsKeys("focus");
assertThat(m.getAbilities()).containsKeys("heal");
}
}
27 changes: 27 additions & 0 deletions src/test/java/pl/uj/io/cuteanimals/model/MonsterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package pl.uj.io.cuteanimals.model;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import pl.uj.io.cuteanimals.model.entity.Attributes;

// Sweet 100% coverage
class MonsterTest {
private Monster mob;

@BeforeEach
void setUp() {
mob = new Monster("mob", new Attributes(1, 1, 1, 1, 1, 1));
}

@Test
void getEquipment() {
assertThat(mob.getEquipment()).isNotNull();
}

@Test
void getArmor() {
assertThat(mob.getArmor()).isNull();
}
}
Loading

0 comments on commit 637320f

Please sign in to comment.