Job + Spell merge #9
14
pom.xml
14
pom.xml
@@ -20,6 +20,20 @@
|
||||
<version>5.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest</artifactId>
|
||||
<version>2.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>5.11.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -3,7 +3,7 @@ package Action;
|
||||
import Job.HasJob;
|
||||
import Job.Wizard;
|
||||
|
||||
public class learnSpellAction implements Action {
|
||||
public class LearnSpellAction implements Action {
|
||||
@Override
|
||||
public void exectue(Actor actor) {
|
||||
Wizard wizard = requireWizard(actor);
|
||||
@@ -10,8 +10,15 @@ import Combat.Spell;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Wizard extends Job {
|
||||
private static final Scanner scanner = new Scanner(System.in);
|
||||
public Wizard() {super("Wizard");}
|
||||
private final Scanner scanner;
|
||||
public Wizard(Scanner scanner) {
|
||||
super("Wizard");
|
||||
this.scanner = scanner;
|
||||
}
|
||||
|
||||
public Wizard() {
|
||||
this(new Scanner(System.in));
|
||||
}
|
||||
|
||||
public void learnSpell(Actor actor) {
|
||||
if(actor instanceof HasSpellBook a ) {
|
||||
|
||||
73
src/test/java/InterestingTests.java
Normal file
73
src/test/java/InterestingTests.java
Normal file
@@ -0,0 +1,73 @@
|
||||
import Action.Actor;
|
||||
import Combat.HasMana;
|
||||
import Combat.OffensiveDamageSpell;
|
||||
import Combat.Spell;
|
||||
import Entity.Entity;
|
||||
import Entity.Player;
|
||||
import Inventory.HasSpellBook;
|
||||
import Job.Wizard;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
public class InterestingTests {
|
||||
|
||||
private Wizard wizard;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
wizard = new Wizard();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCastSpell_successfulCast() {
|
||||
Scanner testScanner = new Scanner(new ByteArrayInputStream("1\n".getBytes()));
|
||||
wizard = new Wizard(testScanner);
|
||||
|
||||
Player player = new Player("Gandalf", wizard);
|
||||
|
||||
player.setMana(50);
|
||||
player.setHealth(100);
|
||||
|
||||
OffensiveDamageSpell fireball = new OffensiveDamageSpell("Fireball", 20, 15);
|
||||
player.getSpellBook().add(fireball);
|
||||
|
||||
int initialMana = player.getMana();
|
||||
int initialHealth = player.getHealth();
|
||||
|
||||
wizard.castSpell(player);
|
||||
|
||||
assertThat(player.getMana(), is(initialMana - fireball.getCost()));
|
||||
assertThat(player.getHealth(), is(initialHealth - fireball.getPotency()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCastSpell_notEnoughMana() {
|
||||
Scanner testScanner = new Scanner(new ByteArrayInputStream("1\n".getBytes()));
|
||||
wizard = new Wizard(testScanner);
|
||||
|
||||
Player player = new Player("Merlin", wizard);
|
||||
player.setMana(10);
|
||||
player.setHealth(100);
|
||||
|
||||
OffensiveDamageSpell fireball = new OffensiveDamageSpell("Fireball", 20, 15);
|
||||
player.getSpellBook().add(fireball);
|
||||
|
||||
int initialMana = player.getMana();
|
||||
int initialHealth = player.getHealth();
|
||||
|
||||
wizard.castSpell(player);
|
||||
|
||||
assertThat(player.getMana(),is(initialMana));
|
||||
assertThat(player.getHealth(),is(initialHealth));
|
||||
}
|
||||
}
|
||||
61
src/test/java/MagicSystemTest.java
Normal file
61
src/test/java/MagicSystemTest.java
Normal file
@@ -0,0 +1,61 @@
|
||||
import Action.CastAction;
|
||||
import Action.LearnSpellAction;
|
||||
import Combat.OffensiveDamageSpell;
|
||||
import Entity.Player;
|
||||
import Job.Wizard;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class MagicSystemTest {
|
||||
|
||||
private Player defaultPlayer() {
|
||||
return new Player("Alex");
|
||||
}
|
||||
|
||||
@Test void cant_cast_spell_without_being_wizard(){
|
||||
var p = defaultPlayer();
|
||||
CastAction action = new CastAction();
|
||||
IllegalStateException exception = assertThrows(
|
||||
IllegalStateException.class,
|
||||
() -> action.exectue(p)
|
||||
);
|
||||
|
||||
assertTrue(exception.getMessage().contains("cannot perform this action without being a Wizard"));
|
||||
}
|
||||
|
||||
@Test void cant_learn_spell_without_being_wizard(){
|
||||
var p = defaultPlayer();
|
||||
LearnSpellAction action = new LearnSpellAction();
|
||||
IllegalStateException exception = assertThrows(
|
||||
IllegalStateException.class,
|
||||
() -> action.exectue(p)
|
||||
);
|
||||
assertTrue(exception.getMessage().contains("cannot perform this action without being a Wizard"));
|
||||
}
|
||||
|
||||
@Test void damage_spell_damages_target(){
|
||||
var defaultSpell = new OffensiveDamageSpell("fireball", 20, 20);
|
||||
var p = defaultPlayer();
|
||||
p.setMana(30);
|
||||
p.setHealth(30);
|
||||
defaultSpell.cast(p, p);
|
||||
assertEquals(10, p.getHealth());
|
||||
}
|
||||
|
||||
@Test void damage_spell_drains_mana_from_caster(){
|
||||
var defaultSpell = new OffensiveDamageSpell("fireball", 20, 20);
|
||||
var p = defaultPlayer();
|
||||
p.setMana(30);
|
||||
p.setHealth(30);
|
||||
defaultSpell.cast(p, p);
|
||||
assertEquals(10, p.getMana());
|
||||
}
|
||||
|
||||
@Test void cant_cast_spell_with_empty_spellbook(){
|
||||
var p = defaultPlayer();
|
||||
var job = new Wizard();
|
||||
p.learnJob(job);
|
||||
p.performAction(new CastAction());
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import Action.DigAction;
|
||||
import Action.learnSpellAction;
|
||||
import Action.LearnSpellAction;
|
||||
import Combat.OffensiveDamageSpell;
|
||||
import Entity.Position;
|
||||
import Job.Miner;
|
||||
@@ -21,6 +21,18 @@ class PlayerTest {
|
||||
assertFalse(p.isAlive());
|
||||
}
|
||||
|
||||
@Test void get_health_returns_health() {
|
||||
var p = defaultPlayer();
|
||||
p.setHealth(10);
|
||||
assertEquals(10, p.getHealth());
|
||||
}
|
||||
|
||||
@Test void get_mana_returns_mana() {
|
||||
var p = defaultPlayer();
|
||||
p.setMana(10);
|
||||
assertEquals(10, p.getMana());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void can_change_position() {
|
||||
var p = defaultPlayer();
|
||||
@@ -53,7 +65,7 @@ class PlayerTest {
|
||||
var job = new Wizard();
|
||||
var defaultSpell = new OffensiveDamageSpell("fireball", 20, 20);
|
||||
p.learnJob(job);
|
||||
p.performAction(new learnSpellAction());
|
||||
p.performAction(new LearnSpellAction());
|
||||
System.out.println(p.getSpellBook());
|
||||
assertTrue(p.getSpellBook().contains(defaultSpell));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user