inventory_mock tests #17
@@ -1,11 +1,11 @@
|
|||||||
package Action;
|
package Action;
|
||||||
|
|
||||||
import Job.HasJob;
|
import Job.JobHolder;
|
||||||
import Job.Wizard;
|
import Job.Wizard;
|
||||||
|
|
||||||
public class CastAction implements Action {
|
public class CastAction implements Action {
|
||||||
private Wizard requireWizard(Actor actor) {
|
private Wizard requireWizard(Actor actor) {
|
||||||
if (actor instanceof HasJob hasJob && hasJob.getJob() instanceof Wizard wizard) {
|
if (actor instanceof JobHolder hasJob && hasJob.getJob() instanceof Wizard wizard) {
|
||||||
return wizard;
|
return wizard;
|
||||||
}
|
}
|
||||||
throw new IllegalStateException(actor + " cannot perform this action without being a Wizard!");
|
throw new IllegalStateException(actor + " cannot perform this action without being a Wizard!");
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package Action;
|
package Action;
|
||||||
|
|
||||||
import Job.HasJob;
|
import Job.JobHolder;
|
||||||
import Job.Miner;
|
import Job.Miner;
|
||||||
import Terrain.Biome;
|
import Terrain.Biome;
|
||||||
|
|
||||||
@@ -16,7 +16,7 @@ public class DigAction implements Action {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Miner requireMiner(Actor actor) {
|
private Miner requireMiner(Actor actor) {
|
||||||
if (actor instanceof HasJob hasJob
|
if (actor instanceof JobHolder hasJob
|
||||||
&& hasJob.getJob() instanceof Miner miner) {
|
&& hasJob.getJob() instanceof Miner miner) {
|
||||||
return miner;
|
return miner;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package Action;
|
package Action;
|
||||||
|
|
||||||
import Job.HasJob;
|
import Job.JobHolder;
|
||||||
import Job.Wizard;
|
import Job.Wizard;
|
||||||
|
|
||||||
public class LearnSpellAction implements Action {
|
public class LearnSpellAction implements Action {
|
||||||
@@ -11,7 +11,7 @@ public class LearnSpellAction implements Action {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Wizard requireWizard(Actor actor) {
|
private Wizard requireWizard(Actor actor) {
|
||||||
if (actor instanceof HasJob hasJob && hasJob.getJob() instanceof Wizard wizard) {
|
if (actor instanceof JobHolder hasJob && hasJob.getJob() instanceof Wizard wizard) {
|
||||||
return wizard;
|
return wizard;
|
||||||
}
|
}
|
||||||
throw new IllegalStateException(actor + " cannot perform this action without being a Wizard!");
|
throw new IllegalStateException(actor + " cannot perform this action without being a Wizard!");
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
package Character;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public interface HasInventory {
|
|
||||||
List<String> getInventory();
|
|
||||||
}
|
|
||||||
@@ -4,7 +4,7 @@ package Entity;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public abstract class Entity implements HasPosition {
|
public abstract class Entity implements Positionable {
|
||||||
protected String name;
|
protected String name;
|
||||||
protected Position position;
|
protected Position position;
|
||||||
private static final List<Entity> entities = new ArrayList<Entity>();
|
private static final List<Entity> entities = new ArrayList<Entity>();
|
||||||
|
|||||||
@@ -8,14 +8,14 @@ import Combat.HasMana;
|
|||||||
import Combat.Spell;
|
import Combat.Spell;
|
||||||
import Inventory.Inventory;
|
import Inventory.Inventory;
|
||||||
import Job.Job;
|
import Job.Job;
|
||||||
import Job.HasJob;
|
import Job.JobHolder;
|
||||||
import Inventory.HasInventory;
|
import Inventory.InventoryHolder;
|
||||||
import Inventory.HasSpellBook;
|
import Inventory.HasSpellBook;
|
||||||
|
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Player extends Entity implements Movable, Actor, HasInventory, HasSpellBook, HasJob, HasHealth, HasMana, HasConditions {
|
public class Player extends Entity implements Movable, Actor, InventoryHolder, HasSpellBook, JobHolder, HasHealth, HasMana, HasConditions {
|
||||||
protected int health;
|
protected int health;
|
||||||
protected int mana;
|
protected int mana;
|
||||||
protected Job job;
|
protected Job job;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package Entity;
|
package Entity;
|
||||||
|
|
||||||
public interface HasPosition {
|
public interface Positionable {
|
||||||
Position getPosition();
|
Position getPosition();
|
||||||
void setPosition(Position position);
|
void setPosition(Position position);
|
||||||
}
|
}
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
package Inventory;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public interface HasInventory {
|
|
||||||
Inventory getInventory();
|
|
||||||
}
|
|
||||||
5
src/main/java/Inventory/InventoryHolder.java
Normal file
5
src/main/java/Inventory/InventoryHolder.java
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package Inventory;
|
||||||
|
|
||||||
|
public interface InventoryHolder {
|
||||||
|
Inventory getInventory();
|
||||||
|
}
|
||||||
@@ -3,8 +3,6 @@ package Job;
|
|||||||
import Shared.ExperienceTable;
|
import Shared.ExperienceTable;
|
||||||
import Shared.Levelable;
|
import Shared.Levelable;
|
||||||
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
public abstract class Job implements Levelable {
|
public abstract class Job implements Levelable {
|
||||||
protected int level;
|
protected int level;
|
||||||
protected int experience;
|
protected int experience;
|
||||||
@@ -43,21 +41,4 @@ public abstract class Job implements Levelable {
|
|||||||
level++;
|
level++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return String.format("Job: %s. Level: %d", name, level);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object o) {
|
|
||||||
if (this == o) return true;
|
|
||||||
return o != null && getClass() == o.getClass();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return Objects.hash(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package Job;
|
package Job;
|
||||||
|
|
||||||
public interface HasJob {
|
public interface JobHolder {
|
||||||
Job getJob();
|
Job getJob();
|
||||||
void learnJob(Job job);
|
void learnJob(Job job);
|
||||||
}
|
}
|
||||||
@@ -1,19 +1,19 @@
|
|||||||
package Job;
|
package Job;
|
||||||
|
|
||||||
import Inventory.HasInventory;
|
import Inventory.InventoryHolder;
|
||||||
import Item.ItemStack;
|
import Item.ItemStack;
|
||||||
import Terrain.Biome;
|
import Terrain.Biome;
|
||||||
|
|
||||||
|
|
||||||
public class Miner extends Job {
|
public class Miner extends Job {
|
||||||
HasInventory actor;
|
InventoryHolder actor;
|
||||||
|
|
||||||
public Miner(HasInventory actor) {
|
public Miner(InventoryHolder actor) {
|
||||||
super("Miner");
|
super("Miner");
|
||||||
this.actor = actor;
|
this.actor = actor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HasInventory getActor() {
|
public InventoryHolder getActor() {
|
||||||
return actor;
|
return actor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
30
src/test/java/DigActionTest.java
Normal file
30
src/test/java/DigActionTest.java
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import Action.DigAction;
|
||||||
|
import Entity.Player;
|
||||||
|
import Job.Miner;
|
||||||
|
import Terrain.Biome;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
public class DigActionTest {
|
||||||
|
@Test
|
||||||
|
void can_be_executed_as_miner() {
|
||||||
|
var mockMiner = mock(Miner.class);
|
||||||
|
var player = mock(Player.class);
|
||||||
|
when(player.getJob()).thenReturn(mockMiner);
|
||||||
|
var biome = mock(Biome.class);
|
||||||
|
var action = new DigAction(biome);
|
||||||
|
action.execute(player);
|
||||||
|
verify(mockMiner, times(1)).dig(biome);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void digging_when_not_miner_throws_error() {
|
||||||
|
var player = mock(Player.class);
|
||||||
|
when(player.getJob()).thenReturn(null);
|
||||||
|
var biome = mock(Biome.class);
|
||||||
|
var action = new DigAction(biome);
|
||||||
|
assertThrows(IllegalStateException.class, () -> action.execute(player));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user