Skip to content

Commit

Permalink
Add action tests (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
hjaremko committed Jun 10, 2020
1 parent 5729e39 commit 3f737f9
Show file tree
Hide file tree
Showing 13 changed files with 255 additions and 89 deletions.
26 changes: 0 additions & 26 deletions src/main/java/pl/uj/io/cuteanimals/action/MessageAction.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.List;
import java.util.Map;
import pl.uj.io.cuteanimals.action.ActionBuilder;
import pl.uj.io.cuteanimals.action.MessageAction;
import pl.uj.io.cuteanimals.action.InvestigateAction;
import pl.uj.io.cuteanimals.exception.InvalidCommandException;
import pl.uj.io.cuteanimals.model.interfaces.IAction;

Expand Down Expand Up @@ -43,7 +43,7 @@ static Expression argument(String arg) {
context.getOrDefault(
arg,
new ActionBuilder()
.addAction(new MessageAction())
.addAction(new InvestigateAction(""))
.addArgs(List.of(arg))
.collect());
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/pl/uj/io/cuteanimals/model/ArmorBackpack.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.ArrayList;
import java.util.List;
import pl.uj.io.cuteanimals.model.interfaces.ICharacter;
import pl.uj.io.cuteanimals.model.interfaces.IAttributes;
import pl.uj.io.cuteanimals.model.interfaces.IEquipment;
import pl.uj.io.cuteanimals.model.interfaces.IItem;

Expand All @@ -14,12 +14,12 @@
* @since 0.0.1-SNAPSHOT
*/
public class ArmorBackpack implements IEquipment {
private final ICharacter owner;
private final IAttributes ownerAttributes;
private IItem weapon;
private IItem armor;

public ArmorBackpack(ICharacter owner) {
this.owner = owner;
public ArmorBackpack(IAttributes ownerAttributes) {
this.ownerAttributes = ownerAttributes;
}

@Override
Expand All @@ -39,7 +39,7 @@ public List<IItem> getItems() {

@Override
public boolean putItem(IItem item) {
if (item.getAttributes().getLevel() > owner.getAttributes().getLevel()) {
if (item.getAttributes().getLevel() > ownerAttributes.getLevel()) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/pl/uj/io/cuteanimals/model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public Player(int id, WorldMap world) {
this.world = world;
this.stats = new PlayerAttributes(this);
this.currentLocation = world.getLocation("town");
this.armorBackpack = new ArmorBackpack(this);
this.armorBackpack = new ArmorBackpack(this.stats);
this.backpack = new PlayerBackpack(this.stats);
this.gameState = GameState.LIMBO;
this.fightManager = new FightManager(this);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package pl.uj.io.cuteanimals.action;

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

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.GameState;
import pl.uj.io.cuteanimals.model.Player;

@ExtendWith(MockitoExtension.class)
class InvestigateActionTest {
@Mock private Player player;
private InvestigateAction investigateAction;

@BeforeEach
void setUp() {
investigateAction = new InvestigateAction("Message");
}

@Test
void actionBodyShouldReturnInfoMessage() {
when(player.getCurrentGameState()).thenReturn(GameState.EXPLORATION);
assertThat(investigateAction.execute(player).getMessage()).isEqualTo("Message");
}

@Test
void actionShouldBeAvailableOnlyInExploration() {
assertThat(investigateAction.getAcceptableStates())
.isEqualTo(List.of(GameState.EXPLORATION));
}
}
45 changes: 45 additions & 0 deletions src/test/java/pl/uj/io/cuteanimals/action/ShowAbilitiesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package pl.uj.io.cuteanimals.action;

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

import java.util.HashMap;
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.action.ability.Focus;
import pl.uj.io.cuteanimals.model.GameState;
import pl.uj.io.cuteanimals.model.Player;
import pl.uj.io.cuteanimals.model.interfaces.IAction;

@ExtendWith(MockitoExtension.class)
class ShowAbilitiesTest {
@Mock private Player player;
private ShowAbilities showAbilities;

@BeforeEach
void setUp() {
showAbilities = new ShowAbilities();
}

@Test
void actionBodyShouldReturnInfoMessage() {
var skills = new HashMap<String, IAction>();
skills.put("focus", new Focus());
when(player.getCurrentGameState()).thenReturn(GameState.EXPLORATION);
when(player.getAbilities()).thenReturn(skills);

var out = showAbilities.execute(player).getMessage();
assertThat(out).isNotBlank();
assertThat(out).contains("focus");
}

@Test
void actionShouldBeAvailableOnlyInExplorationAndFight() {
assertThat(showAbilities.getAcceptableStates())
.isEqualTo(List.of(GameState.EXPLORATION, GameState.FIGHT));
}
}
43 changes: 43 additions & 0 deletions src/test/java/pl/uj/io/cuteanimals/action/ShowArmorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package pl.uj.io.cuteanimals.action;

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

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.GameState;
import pl.uj.io.cuteanimals.model.Player;
import pl.uj.io.cuteanimals.model.PlayerBackpack;

@ExtendWith(MockitoExtension.class)
class ShowArmorTest {
@Mock private Player player;
private ShowArmor showArmor;

@BeforeEach
void setUp() {
showArmor = new ShowArmor();
}

@Test
void actionBodyShouldReturnInfoMessage() {
var bp = mock(PlayerBackpack.class);
when(player.getCurrentGameState()).thenReturn(GameState.EXPLORATION);
when(player.getArmor()).thenReturn(bp);
showArmor.execute(player);
var thisVariableIsHereToSilenceFalsePositiveWarningReportedBySpotBugs =
verify(bp).showItems();
assertThat(thisVariableIsHereToSilenceFalsePositiveWarningReportedBySpotBugs)
.isNull(); // silence spotbugs
}

@Test
void actionShouldBeAvailableOnlyInExploration() {
assertThat(showArmor.getAcceptableStates())
.isEqualTo(List.of(GameState.EXPLORATION, GameState.FIGHT));
}
}
46 changes: 46 additions & 0 deletions src/test/java/pl/uj/io/cuteanimals/action/ShowBackpackTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package pl.uj.io.cuteanimals.action;

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

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.GameState;
import pl.uj.io.cuteanimals.model.Player;
import pl.uj.io.cuteanimals.model.PlayerBackpack;

@ExtendWith(MockitoExtension.class)
class ShowBackpackTest {
@Mock private Player player;
private ShowBackpack showBackpack;

@BeforeEach
void setUp() {
showBackpack = new ShowBackpack();
}

@Test
void actionBodyShouldReturnInfoMessage() {
when(player.getCurrentGameState()).thenReturn(GameState.EXPLORATION);
var playerBackpack = mock(PlayerBackpack.class);
when(player.getEquipment()).thenReturn(playerBackpack);

assertThat(showBackpack.execute(player).getMessage()).isNotBlank();
var thisVariableIsHereToSilenceFalsePositiveWarningReportedBySpotBugs =
verify(playerBackpack).showItems();
var thisOneToo = verify(playerBackpack).getRemainingCapacity();
assertThat(thisVariableIsHereToSilenceFalsePositisveWarningReportedBySpotBugs)
.isNull(); // silence spotbugs
assertThat(thisOneToo).isNotNull(); // silence spotbugs
}

@Test
void actionShouldBeAvailableOnlyInExplorationAndFight() {
assertThat(showBackpack.getAcceptableStates())
.isEqualTo(List.of(GameState.EXPLORATION, GameState.FIGHT));
}
}
43 changes: 43 additions & 0 deletions src/test/java/pl/uj/io/cuteanimals/action/ShowStatsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package pl.uj.io.cuteanimals.action;

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

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.GameState;
import pl.uj.io.cuteanimals.model.Player;
import pl.uj.io.cuteanimals.model.entity.Attributes;

@ExtendWith(MockitoExtension.class)
class ShowStatsTest {
@Mock private Player player;
private ShowStats showStats;

@BeforeEach
void setUp() {
showStats = new ShowStats();
}

@Test
void actionBodyShouldReturnInfoMessage() {
when(player.getCurrentGameState()).thenReturn(GameState.EXPLORATION);
when(player.getAttributes()).thenReturn(new Attributes(1, 1, 1, 1, 1, 1));

assertThat(showStats.execute(player).getMessage()).isNotBlank();
var thisVariableIsHereToSilenceFalsePositiveWarningReportedBySpotBugs =
verify(player).getAttributes();
assertThat(thisVariableIsHereToSilenceFalsePositiveWarningReportedBySpotBugs)
.isNull(); // silence spotbugs
}

@Test
void actionShouldBeAvailableOnlyInExploration() {
assertThat(showStats.getAcceptableStates())
.isEqualTo(List.of(GameState.EXPLORATION, GameState.FIGHT));
}
}
23 changes: 1 addition & 22 deletions src/test/java/pl/uj/io/cuteanimals/model/ArmorBackpackTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,15 @@
import pl.uj.io.cuteanimals.model.entity.Attributes;
import pl.uj.io.cuteanimals.model.entity.Item;
import pl.uj.io.cuteanimals.model.interfaces.IAttributes;
import pl.uj.io.cuteanimals.model.interfaces.ICharacter;
import pl.uj.io.cuteanimals.model.interfaces.IEquipment;

@ExtendWith(MockitoExtension.class)
class ArmorBackpackTest {
private ArmorBackpack eq;
@Mock IAttributes attrMock;
ICharacter playerMock;

@BeforeEach
void setUp() {
playerMock =
new ICharacter() {
@Override
public IEquipment getEquipment() {
return null;
}

@Override
public IEquipment getArmor() {
return null;
}

@Override
public IAttributes getAttributes() {
return attrMock;
}
};

eq = new ArmorBackpack(playerMock);
eq = new ArmorBackpack(attrMock);
}

@Test
Expand Down
28 changes: 14 additions & 14 deletions src/test/java/pl/uj/io/cuteanimals/model/BackpackTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import pl.uj.io.cuteanimals.model.entity.Item;

class BackpackTest {
private Backpack bp;
private Backpack backpack;

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

@Test
Expand All @@ -36,22 +36,22 @@ void putAndRemoveItem() {
ItemType.ARMOR,
ItemClass.ANY);

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();
assertThat(backpack.putItem(weapon));
assertThat(backpack.putItem(heavy));
assertThat(backpack.getItems().size()).isEqualTo(2);
assertThat(backpack.getItems()).contains(heavy);
assertThat(backpack.getItems()).contains(weapon);
assertThat(backpack.removeItem(weapon));
assertThat(backpack.getItems().size()).isEqualTo(1);
assertThat(backpack.removeItem(heavy));
assertThat(backpack.getItems()).isEmpty();
}

@Test
void showItemsDoesntCrash() {
assertThat(bp.showItems()).isNotBlank();
assertThat(backpack.showItems()).isNotBlank();
assertThat(
bp.putItem(
backpack.putItem(
new Item(
1,
"pach",
Expand All @@ -60,6 +60,6 @@ void showItemsDoesntCrash() {
new Attributes(1, 1, 1, 1, 1, 1),
ItemType.WEAPON,
ItemClass.ANY)));
assertThat(bp.showItems()).isNotBlank();
assertThat(backpack.showItems()).isNotBlank();
}
}
Loading

0 comments on commit 3f737f9

Please sign in to comment.