Skip to content

Commit

Permalink
Add more unit tests (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
hjaremko committed Jun 10, 2020
1 parent 772e139 commit 25b0b18
Show file tree
Hide file tree
Showing 16 changed files with 380 additions and 24 deletions.
2 changes: 2 additions & 0 deletions src/main/java/pl/uj/io/cuteanimals/action/BuffCharacter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ public IResult actionBody(IPlayer player) {
player.getAttributes().addHealth(attributes.getHealth());
player.getAttributes().addAttack(attributes.getAttack());
player.getAttributes().addDefence(attributes.getDefence());
player.getAttributes().addMana(attributes.getMana());

var message = "";
message += (attributes.getHealth() != 0 ? "+" + attributes.getHealth() + " health " : "");
message += (attributes.getAttack() != 0 ? "+" + attributes.getAttack() + " attack " : "");
message +=
(attributes.getDefence() != 0 ? "+" + attributes.getDefence() + " defence " : "");
message += (attributes.getMana() != 0 ? "+" + attributes.getMana() + " mana " : "");

return new Result(message, Color.GREEN);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected IResult actionBody(IPlayer player) {
Color.YELLOW);
}

if (player.getFightManager().getState() instanceof Focus) {
if (player.getFightManager().getState() instanceof IAbility) {
return new FightLog("You are already focusing.", Color.YELLOW);
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/pl/uj/io/cuteanimals/model/DefaultLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Component;
import pl.uj.io.cuteanimals.model.interfaces.*;

/**
Expand All @@ -12,6 +13,7 @@
* @version %I%
* @since 0.0.1-SNAPSHOT
*/
@Component
public class DefaultLocation implements ILocation {
protected String description;
protected Map<String, IAction> actionMap;
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/pl/uj/io/cuteanimals/model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Player(int id, WorldMap world) {
this.stats = new PlayerAttributes(this);
this.currentLocation = world.getLocation("town");
this.armorBackpack = new ArmorBackpack(this);
this.backpack = new PlayerBackpack(this);
this.backpack = new PlayerBackpack(this.stats);
this.gameState = GameState.LIMBO;
this.fightManager = new FightManager(this);
this.playerClass = new Slave();
Expand Down Expand Up @@ -57,6 +57,10 @@ public IAttributes getAttributes() {

@Override
public IResult use(IItem item) {
if (item.getType() != ItemType.USABLE) {
return new Result("You can't use that.");
}

var eatingResult = new BuffCharacter(item.getAttributes()).execute(this);
getEquipment().removeItem(item);
var attackResult = fightManager.contrAttack();
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/pl/uj/io/cuteanimals/model/PlayerBackpack.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package pl.uj.io.cuteanimals.model;

import pl.uj.io.cuteanimals.model.interfaces.ICharacter;
import pl.uj.io.cuteanimals.model.interfaces.IAttributes;
import pl.uj.io.cuteanimals.model.interfaces.IItem;

/**
Expand All @@ -10,12 +10,12 @@
* @since 0.2.0-SNAPSHOT
*/
public class PlayerBackpack extends Backpack {
private final ICharacter owner;
private final IAttributes ownerAttrs;
private int currentWeight;

public PlayerBackpack(ICharacter owner) {
public PlayerBackpack(IAttributes ownerAttrs) {
super();
this.owner = owner;
this.ownerAttrs = ownerAttrs;
this.currentWeight = 0;
}

Expand All @@ -34,7 +34,6 @@ public boolean putItem(IItem item) {
public boolean removeItem(IItem item) {
getItems().remove(item);
currentWeight -= item.getSize();
// TODO: check of possible (e.g. soul bounded items?)
return true;
}

Expand All @@ -45,7 +44,7 @@ public boolean removeItem(IItem item) {
*/
public int getCapacity() {
int baseCapacity = 10;
return baseCapacity + 3 * owner.getAttributes().getAttack();
return baseCapacity + 3 * ownerAttrs.getAttack();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@

import java.util.List;
import java.util.Map;
import java.util.Random;
import pl.uj.io.cuteanimals.model.GameState;
import pl.uj.io.cuteanimals.model.RandomIntegerImpl;
import pl.uj.io.cuteanimals.model.Result;
import pl.uj.io.cuteanimals.model.interfaces.ContainerArgumentAction;
import pl.uj.io.cuteanimals.model.interfaces.ILocation;
import pl.uj.io.cuteanimals.model.interfaces.IPlayer;
import pl.uj.io.cuteanimals.model.interfaces.IResult;
import pl.uj.io.cuteanimals.model.interfaces.*;

/**
* Provides methods that moves Character to the MedicalCabin in case of him falling from the bridge.
*
* @version %I%
* @since 0.0.1-SNAPSHOT
*/
// @Component
public class DungeonEntranceGoAction extends ContainerArgumentAction<ILocation> {
private final RandomInteger rand;

public DungeonEntranceGoAction(Map<String, ILocation> wheres) {
super(wheres);
this.rand = new RandomIntegerImpl();
}

public DungeonEntranceGoAction(Map<String, ILocation> wheres, RandomInteger rand) {
super(wheres);
this.rand = rand;
}

@Override
Expand All @@ -29,9 +35,7 @@ public IResult actionBody(IPlayer player, String toGoName) {
return new Result("You want to go... where?");
}

Random rand = new Random();
int result = rand.nextInt(10);
if (result < 4) {
if (rand.nextInt(10) < 4) {
toGo = player.getWorld().getLocation("medical");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public IResult actionBody(IPlayer player) {
}

for (IItem item : player.getArmor().getItems()) {
if (item.getName().equals("torch")) {
if (item.getName().toLowerCase().equals("torch")) {
return new Result(infoMessage);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
<script>
// For localhost testing
// const proxyURL = "https://cors-anywhere.herokuapp.com/";
// const URLprefix = "http://localhost:8080/game/";
const URLprefix = "http://localhost:8080/game/";
const proxyURL = "";
const URLprefix = "https://io-rpg.herokuapp.com/game/";
// const URLprefix = "https://io-rpg.herokuapp.com/game/";
const URLsuffix = "/msg";

var requestURL;
Expand Down
18 changes: 15 additions & 3 deletions src/test/java/pl/uj/io/cuteanimals/action/ability/FocusTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void setUp() {
player = new Player(0, world, random);
attrs = (PlayerAttributes) player.getAttributes();

dummy = new Monster("dummy", new NPCAttributes(0, 1, 1, 0, 0));
dummy = new Monster("dummy", new NPCAttributes(100, 1, 1, 0, 0));
player.getFightManager().beginFight(dummy);
var focus = new Focus();
focus.execute(player);
Expand All @@ -50,7 +50,7 @@ void atLeast20ManaNeeded() {
@Test
void attackShouldInflictAdditionalDamageLevelOneTest() {
player.getFightManager().attack();
assertThat(dummy.getAttributes().getHealth()).isEqualTo(-3);
assertThat(dummy.getAttributes().getHealth()).isEqualTo(100 - 3);
}

@Test
Expand All @@ -59,6 +59,18 @@ void attackShouldInflictAdditionalDamageLevel100Test() {

attrs.addAttack(49);
player.getFightManager().attack();
assertThat(dummy.getAttributes().getHealth()).isEqualTo(-175);
assertThat(dummy.getAttributes().getHealth()).isEqualTo(100 - 175);
}

@Test
void cannotCastMultipleTimes() {
assertThat(new Focus().execute(player).getMessage())
.isEqualTo("* You are already focusing.");
}

@Test
void cannotCastSomethingElse() {
assertThat(new DoubleDown().execute(player).getMessage())
.isEqualTo("* You need to unleash your rage now.");
}
}
50 changes: 50 additions & 0 deletions src/test/java/pl/uj/io/cuteanimals/model/BackpackTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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;
import pl.uj.io.cuteanimals.model.entity.Item;

class BackpackTest {
private Backpack bp;

@BeforeEach
void setUp() {
bp = new Backpack();
}

@Test
void putAndRemoveItem() {
var weapon =
new Item(1, "pach", "aaa", 1, new Attributes(1, 1, 1, 1, 1, 1), ItemType.WEAPON);
var heavy =
new Item(1, "pach", "aaa", 40, new Attributes(1, 1, 1, 1, 1, 1), ItemType.ARMOR);

assertThat(bp.putItem(weapon));
assertThat(bp.putItem(heavy));
assertThat(bp.getItems().size()).isEqualTo(2);
assertThat(bp.getItems()).contains(heavy);
assertThat(bp.getItems()).contains(weapon);
assertThat(bp.removeItem(weapon));
assertThat(bp.getItems().size()).isEqualTo(1);
assertThat(bp.removeItem(heavy));
assertThat(bp.getItems()).isEmpty();
}

@Test
void showItemsDoesntCrash() {
assertThat(bp.showItems()).isNotBlank();
assertThat(
bp.putItem(
new Item(
1,
"pach",
"aaa",
1,
new Attributes(1, 1, 1, 1, 1, 1),
ItemType.WEAPON)));
assertThat(bp.showItems()).isNotBlank();
}
}
34 changes: 34 additions & 0 deletions src/test/java/pl/uj/io/cuteanimals/model/NPCTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package pl.uj.io.cuteanimals.model;

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

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;

@ExtendWith(MockitoExtension.class)
class NPCTest {
@Mock private ArmorBackpack abp;
@Mock private Backpack bp;
private NPC npc;

@BeforeEach
void setUp() {
npc = new NPC(abp, bp, "test", List.of("t1", "t2", "t3"));
}

@Test
void getQuote() {
assertThat(npc.getQuote()).isEqualTo("t1");
assertThat(npc.getQuote()).isEqualTo("t2");
assertThat(npc.getQuote()).isEqualTo("t3");
assertThat(npc.getQuote()).isEqualTo("This character can't tell you anything interesting");
assertThat(npc.getQuote()).isEqualTo("This character can't tell you anything interesting");
assertThat(npc.getQuote()).isEqualTo("This character can't tell you anything interesting");
assertThat(npc.getQuote()).isEqualTo("This character can't tell you anything interesting");
assertThat(npc.getQuote()).isEqualTo("This character can't tell you anything interesting");
}
}
58 changes: 58 additions & 0 deletions src/test/java/pl/uj/io/cuteanimals/model/PlayerBackpackTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package pl.uj.io.cuteanimals.model;

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

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.IAttributes;

@ExtendWith(MockitoExtension.class)
class PlayerBackpackTest {
@Mock private IAttributes attrs;
private PlayerBackpack bp;

@BeforeEach
void setUp() {
bp = new PlayerBackpack(attrs);
}

@Test
void putItem() {
when(attrs.getAttack()).thenReturn(1);
var weapon =
new Item(1, "pach", "aaa", 1, new Attributes(1, 1, 1, 1, 1, 1), ItemType.WEAPON);
var heavy =
new Item(1, "pach", "aaa", 40, new Attributes(1, 1, 1, 1, 1, 1), ItemType.WEAPON);

assertThat(bp.putItem(weapon));
assertThat(!bp.putItem(heavy));
assertThat(bp.getItems().size()).isEqualTo(1);
assertThat(bp.getItems()).contains(weapon);
}

@Test
void removeItem() {
var weapon =
new Item(1, "pach", "aaa", 1, new Attributes(1, 1, 1, 1, 1, 1), ItemType.WEAPON);

assertThat(bp.putItem(weapon));
assertThat(bp.getItems()).contains(weapon);
assertThat(bp.removeItem(weapon));
assertThat(bp.getItems()).isEmpty();
}

@Test
void getCapacity() {
when(attrs.getAttack()).thenReturn(0);
assertThat(bp.getCapacity()).isEqualTo(10);

when(attrs.getAttack()).thenReturn(10);
assertThat(bp.getCapacity()).isEqualTo(40);
}
}
Loading

0 comments on commit 25b0b18

Please sign in to comment.