Skip to content
Snippets Groups Projects
Commit d60fa2d2 authored by Nicolai Hollup Brand's avatar Nicolai Hollup Brand :penguin:
Browse files

Merge branch 'feat/part-one' into 'master'

feat: part one

Closes #13, #11, #10, #9, #8, #7, and #3

See merge request !3
parents 060cc0d1 b2584c6e
No related branches found
No related tags found
1 merge request!3feat: part one
Pipeline #214698 passed
Showing
with 1102 additions and 0 deletions
package edu.ntnu.idatt2001.group_30;
import edu.ntnu.idatt2001.group_30.goals.Goal;
import java.util.List;
/**
* This class represents the facade of a game of paths.
* This class connects a Player to a Story and has useful methods to start and maneuver through a game.
*
* @author Nicolai H. Brand, Trym Hamer Gudvangen
*/
public class Game {
private final Player player;
private final Story story;
private final List<Goal> goals;
/**
* This method constructs a Game object with the given parameters.
* @param player The player who will be playing game.
* @param story The story for the game that the player will play through.
* @param goals A list of goals that determines the desired goals of the game:
* @throws IllegalArgumentException This exception is thrown if any argument is null.
*/
public Game(Player player, Story story, List<Goal> goals) throws IllegalArgumentException{
if (player == null || story == null || goals == null) {
throw new IllegalArgumentException("Player, story, and goals cannot be null");
}
this.player = player;
this.story = story;
this.goals = goals;
}
/**
* This method starts a game.
* @return the first passage of the story.
*/
public Passage begin() {
return this.story.getOpeningPassage();
//TODO: Should we make sure the game can not begin more than one time?
// It depends whether or not the end user itself has a way to call begin(), or if this is something
// exclusively done internally. In that case, unit-tests to ensure this method is only called once may
// suffice.
}
/**
* This method takes in a link and returns the corresponding passage of the story.
* @param link a link to a passage in the story.
* @return the corresponding passage for the link.
*/
public Passage go(Link link) {
return this.story.getPassage(link);
}
/**
* This method returns the player for the game.
* @return the player object for the game.
*/
public Player getPlayer() {
return player;
}
/**
* This method returns the story for the game.
* @return the story object for the game
*/
public Story getStory() {
return story;
}
/**
* This method returns all the goals of the game.
* @return all the goals of the game as a List{@code <Goal>}
*/
public List<Goal> getGoals() {
return goals;
}
}
package edu.ntnu.idatt2001.group_30;
import edu.ntnu.idatt2001.group_30.actions.Action;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* Link class. A Link makes it possible to move from one Passage to another.
* Links bind the different parts of a Story together
*
* @author Nicolai H. Brand, Trym Hamer Gudvangen
*/
public class Link {
private final String text;
private final String reference;
private final List<Action> actions;
/**
* This constructor creates a Link object, which contains information surrounding a linking point in the story.
* @param text, a describing text that indicates a choice or action in a story
* @param reference, a string that unambiguously identify a passage
* @throws IllegalArgumentException This exception is thrown if text or reference is invalid
*/
public Link(String text, String reference) throws IllegalArgumentException{
if (text.isBlank()) throw new IllegalArgumentException("Text cannot be blank or empty.");
this.text = text;
if (reference.isBlank()) throw new IllegalArgumentException("Reference cannot be blank or empty.");
this.reference = reference;
this.actions = new ArrayList<>();
}
/**
* Adds an action to the list of actions
* @param action, the action to be added to the list
*/
public void addAction(Action action) {
this.actions.add(action);
}
/**
* This method retrieves the text of a link.
* @return The text of the link, given as a String.
*/
public String getText() {
return this.text;
}
/**
* This method retrieves the reference of the link.
* @return The reference of the link object, represented as a String.
*/
public String getReference() {
return this.reference;
}
/**
* This method retrieves the list of actions attached to the Link object.
* @return The actions of the Link object, given as a List{@code <Action>}.
*/
public List<Action> getActions() {
return this.actions;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Link link = (Link) o;
return Objects.equals(text, link.text) && Objects.equals(reference, link.reference) && Objects.equals(actions, link.actions);
}
@Override
public int hashCode() {
return Objects.hash(text, reference, actions);
}
@Override
public String toString() {
return "Link{" +
"text='" + text + '\'' +
", reference='" + reference + '\'' +
", actions=" + actions +
'}';
}
}
package edu.ntnu.idatt2001.group_30;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* The Passage class represents a part of the story. It, therefore, contains information surrounding that part, as well
* as the different directions the story may take.
*
* @author Trym Hamer Gudvangen
*/
public class Passage {
private final String title;
private final String content;
private final List<Link> links;
/**
* This constructor creates a passage object, which represents a single point in the story.
* @param title The title of the passage, represented using a String.
* @param content The content of the passage, represented using a String.
* @throws IllegalArgumentException This exception is thrown if title or content is invalid.
*/
public Passage(String title, String content) throws IllegalArgumentException{
if (title.isBlank()) throw new IllegalArgumentException("Title cannot be blank or empty");
this.title = title;
if (content.isBlank()) throw new IllegalArgumentException("Content cannot be blank or empty");
this.content = content;
this.links = new ArrayList<>();
}
/**
* This method retrieves the title of the passage.
* @return The title of the passage, represented as a String.
*/
public String getTitle() {
return this.title;
}
/**
* This method retrieves the content of the passage.
* @return The content of the passage, represented as a String.
*/
public String getContent() {
return this.content;
}
/**
* This method adds another link to the passage.
* @param link The link to be added, represented using a Link object.
* @return Status of adding the link. {@code true} if link was successfully added, otherwise {@code false}.
*/
public boolean addLink(Link link) {
boolean successful = false;
this.links.add(link);
return successful;
}
/**
* This method retrieves the list of links of the passage.
* @return The links attached to this passage, given as a List{@code <Link>}.
*/
public List<Link> getLinks() {
return this.links;
}
/**
* This method checks whether the passage has any links in its links list.
* @return Status of links list. {@code true} if there are links in the list, else {@code false}.
*/
public boolean hasLinks() {
return this.links.size() > 0;
}
@Override
public String toString() {
return "Passage{" +
"title='" + title + '\'' +
", content='" + content + '\'' +
", links=" + links +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Passage passage)) return false;
if (!Objects.equals(title, passage.title)) return false;
if (!Objects.equals(content, passage.content)) return false;
return Objects.equals(links, passage.links);
}
@Override
public int hashCode() {
int result = title != null ? title.hashCode() : 0;
result = 31 * result + (content != null ? content.hashCode() : 0);
result = 31 * result + (links != null ? links.hashCode() : 0);
return result;
}
}
package edu.ntnu.idatt2001.group_30;
import java.util.ArrayList;
import java.util.List;
/**
* This class represents a Player who will experience the story. Therefore, it contains information surrounding their
* character, such as health, score, gold, and inventory.
*
* @author Trym Hamer Gudvangen
*/
public class Player {
private final String name;
private int health;
private int score;
private int gold;
private final List<String> inventory;
/**
* This constructor creates a Player object with status information.
* @param name The name of the player, represented using a String.
* @param health Health of the player, represented using an int.
* @param score Score of the player, represented using an int.
* @param gold Amount of gold the player has, represented using an int.
* @throws IllegalArgumentException This exception is thrown if the health, score, gold or name arguments are invalid.
*/
public Player(String name, int health, int score, int gold) throws IllegalArgumentException{
if (name.isBlank()) throw new IllegalArgumentException("The name cannot be blank or empty");
this.name = name;
if (health < 0) throw new IllegalArgumentException("Initial health cannot be less than 0");
this.health = health;
if (score < 0) throw new IllegalArgumentException("Initial score cannot be less than 0");
this.score = score;
if (gold < 0) throw new IllegalArgumentException("Initial gold cannot be less than 0");
this.gold = gold;
this.inventory = new ArrayList<>();
}
/**
* This method retrieves the name of the player.
* @return Name of the player, represented using a String.
*/
public String getName() {
return this.name;
}
/**
* This method adds a given amount of health to the player's health.
* @param health Amount of health to be added, represented using an int.
*/
public void addHealth(int health) {
setHealth(this.health + health);
}
/**
* This method retrieves the health of the player.
* @return Health of the player, given as an int.
*/
public int getHealth() {
return this.health;
}
/**
* This method sets the players health to be the new health or 0, if the given health is less than 0.
* @param newHealth The new health of the player, given as an int.
*/
public void setHealth(int newHealth) {
this.health = Math.max(newHealth, 0);
}
/**
* This method adds a given amount of points to the Player's score.
* @param points Points to be added, given as an int.
*/
public void addScore(int points) {
this.score += points;
//TODO: Can score be negative?
}
/**
* This method retrieves the score of the player.
* @return The player's score, given as an int.
*/
public int getScore() {
return this.score;
}
/**
* This method adds a given amount of gold to the player's gold.
* @param gold Amount of gold to be added, given as an int.
*/
public void addGold(int gold) {
this.gold += gold;
}
/**
* This method retrieves the player's amount of gold.
* @return Player's amount of gold, represented using an int.
*/
public int getGold() {
return this.gold;
}
/**
* This method adds a given item to the player's inventory.
* @param item Item to be added, represented as a String.
*/
public void addToInventory(String item) {
this.inventory.add(item);
}
/**
* This method retrieves the inventory of the player.
* @return Inventory of the player, given as a List{@code <String>}.
*/
public List<String> getInventory() {
return this.inventory;
}
}
package edu.ntnu.idatt2001.group_30;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* A Story is a non-linear narrative consisting of a collection of passages
*
* @author Nicolai H. Brand, Trym Hamer Gudvangen
*/
public class Story {
private final String title;
private final Map<Link, Passage> passages;
private final Passage openingPassage;
/**
* Constructor for Story
*
* @param title the title of the story
* @param openingPassage the first passage of the story. Also added to the passage.
* @throws IllegalArgumentException This exception is thrown if title or openingPassage is invalid
*/
public Story(String title, Passage openingPassage) throws IllegalArgumentException{
//if (title.isBlank() || !title.matches("[a-zA-Z]")) {
// throw new IllegalArgumentException("Title cannot be blank, empty, or contain special characters.");
//}
if (title.isBlank()) throw new IllegalArgumentException("Title cannot be blank, empty, or contain special characters.");
this.title = title;
if (openingPassage == null) throw new IllegalArgumentException("Opening passage cannot be null");
this.openingPassage = openingPassage;
this.passages = new HashMap<>();
addPassage(openingPassage);
}
/**
* Adds a passage to the passages map
* The link object used as the key in the map is made from properties of the passage
*
* @param passage, the passage to be added to the passages map
*/
public void addPassage(Passage passage) {
Link link = new Link(passage.getTitle(), passage.getTitle());
this.passages.put(link, passage);
}
/**
* Gets the passage with the given link
* @param link, the key to the map of passages
* @return the passage stored with the value link
*/
public Passage getPassage(Link link) {
return this.passages.get(link);
}
/**
* @return the title of the Story
*/
public String getTitle() {
return title;
}
/**
* This method retrieves all the passages of a story.
* @return All the pages of the Story as a {@code Collection<Passages>}.
*/
public Collection<Passage> getPassages() {
return this.passages.values();
}
/**
* @return the opening passage of the Story
*/
public Passage getOpeningPassage() {
return openingPassage;
}
@Override
public String toString() {
return "Story{" +
"title='" + title + '\'' +
", passages=" + passages +
", openingPassage=" + openingPassage +
'}';
}
}
package edu.ntnu.idatt2001.group_30.actions;
import edu.ntnu.idatt2001.group_30.Player;
/**
* The functional interface Action provides the method signature for executing an attribute
* action on the player.
*
* @author Trym Hamer Gudvangen
*/
@FunctionalInterface
public interface Action {
/**
* This method changes a given player's attribute:
* @param player The player, given as a Player object.
*/
void execute(Player player);
}
package edu.ntnu.idatt2001.group_30.actions;
import edu.ntnu.idatt2001.group_30.Player;
import java.util.Objects;
/**
* This class represents a change in the health attribute for any player.
*
* @author Trym Hamer Gudvangen
*/
public class GoldAction implements Action {
private final int gold;
/**
* The constructor defines how much the player's gold will be changed by.
* @param gold Amount of gold, given as an int.
*/
public GoldAction(int gold) {
this.gold = gold;
//TODO: Add exception?
}
/**
* {@inheritDoc} gold.
* @param player The player, given as a Player object.
*/
@Override
public void execute(Player player) {
Objects.requireNonNull(player);
player.addGold(this.gold);
}
}
package edu.ntnu.idatt2001.group_30.actions;
import edu.ntnu.idatt2001.group_30.Player;
import java.util.Objects;
/**
* This class represents a change in the health attribute for any player.
*
* @author Trym Hamer Gudvangen
*/
public class HealthAction implements Action {
private final int health;
/**
* The constructor defines how much the player's health will be changed by.
* @param health Amount of health, given as an int.
*/
public HealthAction(int health) {
this.health = health;
//TODO: Add exception?
}
/**
* {@inheritDoc} health.
* @param player The player, given as a Player object.
*/
@Override
public void execute(Player player) {
Objects.requireNonNull(player);
player.addHealth(this.health);
}
}
package edu.ntnu.idatt2001.group_30.actions;
import edu.ntnu.idatt2001.group_30.Player;
import java.util.Objects;
/**
* This class represents a change in the inventory attribute for any player.
*
* @author Trym Hamer Gudvangen
*/
public class InventoryAction implements Action {
private final String item;
/**
* The constructor defines the item involved in changing the player's inventory.
* @param item The item, given as a String.
*/
public InventoryAction(String item) {
this.item = item;
//TODO: Add exception?
}
/**
* {@inheritDoc} inventory.
* @param player The player, given as a Player object.
*/
@Override
public void execute(Player player) {
Objects.requireNonNull(player);
player.addToInventory(this.item);
}
}
package edu.ntnu.idatt2001.group_30.actions;
import edu.ntnu.idatt2001.group_30.Player;
import java.util.Objects;
/**
* This class represents a change in the score attribute for any player.
*
* @author Trym Hamer Gudvangen
*/
public class ScoreAction implements Action {
private final int points;
/**
* The constructor defines how many points the player's score will be changed by.
* @param points Amount of points, given as an int.
*/
public ScoreAction(int points) {
this.points = points;
//TODO: Add exception?
}
/**
* {@inheritDoc} score.
* @param player The player, given as a Player object.
*/
@Override
public void execute(Player player) {
Objects.requireNonNull(player);
player.addScore(this.points);
}
}
package edu.ntnu.idatt2001.group_30.goals;
import edu.ntnu.idatt2001.group_30.Player;
/**
* The functional interface Goal represents a threshold, desirable result, or condition connected to the player's
* condition.
*
* @author Trym Hamer Gudvangen
*/
public interface Goal {
/**
* This method checks that the player has fulfilled a certain condition of:
* @param player The player to be checked, given as a Player object.
* @return Status of player, {@code true} if the player fulfills the condition, else {@code false}.
*/
boolean isFulfilled(Player player);
}
package edu.ntnu.idatt2001.group_30.goals;
import edu.ntnu.idatt2001.group_30.Player;
import java.util.Objects;
/**
* This class represents a minimum gold threshold.
*
* @author Trym Hamer Gudvangen
*/
public class GoldGoal implements Goal {
private final int minimumGold;
/**
* The constructor defines the minimum amount of gold a given player can have.
* @param minimumGold Minimum amount of gold, given as an int.
*/
public GoldGoal(int minimumGold) {
this.minimumGold = minimumGold;
//TODO: Add exception?
}
/**
* {@inheritDoc} gold.
* @param player The player to be checked, given as a Player object.
* @return Status of player, {@code true} if the player has enough gold, else {@code false}.
*/
@Override
public boolean isFulfilled(Player player) {
Objects.requireNonNull(player);
return player.getGold() >= this.minimumGold;
}
}
package edu.ntnu.idatt2001.group_30.goals;
import edu.ntnu.idatt2001.group_30.Player;
import java.util.Objects;
/**
* This class represents a minimum health threshold.
*
* @author Trym Hamer Gudvangen
*/
public class HealthGoal implements Goal {
private final int minimumHealth;
/**
* The constructor defines the minimum amount of health a given player can have.
* @param minimumHealth Minimum amount of health, given as an int.
* @throws IllegalArgumentException This exception is thrown if minimum health less than 0.
*/
public HealthGoal(int minimumHealth) throws IllegalArgumentException{
if (minimumHealth < 0) throw new IllegalArgumentException("Minimum health cannot be less than 0");
this.minimumHealth = minimumHealth;
}
/**
* {@inheritDoc} health.
* @param player The player to be checked, given as a Player object.
* @return Status of player, {@code true} if the player has enough health, else {@code false}.
*/
@Override
public boolean isFulfilled(Player player) {
Objects.requireNonNull(player);
return player.getHealth() >= this.minimumHealth;
}
}
package edu.ntnu.idatt2001.group_30.goals;
import edu.ntnu.idatt2001.group_30.Player;
import java.util.List;
import java.util.Objects;
/**
* This class represents the items that are expected in a player's inventory.
*
* @author Trym Hamer Gudvangen
*/
public class InventoryGoal implements Goal {
private final List<String> mandatoryItems;
/**
* The constructor defines the items a player must have.
* @param mandatoryItems Expected items, given as a List{@code <String>}.
*/
public InventoryGoal(List<String> mandatoryItems) {
this.mandatoryItems = mandatoryItems;
//TODO: Add exception?
}
/**
* {@inheritDoc} inventory.
* @param player The player to be checked, given as a Player object.
* @return Status of player, {@code true} if the player has the items, else {@code false}.
*/
@Override
public boolean isFulfilled(Player player) {
Objects.requireNonNull(player);
return player.getInventory().containsAll(mandatoryItems);
}
}
package edu.ntnu.idatt2001.group_30.goals;
import edu.ntnu.idatt2001.group_30.Player;
import java.util.Objects;
/**
* This class represents a minimum goal threshold.
*
* @author Trym Hamer Gudvangen
*/
public class ScoreGoal implements Goal {
private final int minimumPoints;
/**
* The constructor defines the minimum amount of points a given player can have.
* @param minimumPoints Minimum amount of points, given as an int.
*/
public ScoreGoal(int minimumPoints) {
this.minimumPoints = minimumPoints;
//TODO: Add exception?
}
/**
* {@inheritDoc} score.
* @param player The player to be checked, given as a Player object.
* @return Status of player, {@code true} if the player has enough points, else {@code false}.
*/
@Override
public boolean isFulfilled(Player player) {
Objects.requireNonNull(player);
return player.getScore() >= this.minimumPoints;
}
}
package edu.ntnu.idatt2001.group_30;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import edu.ntnu.idatt2001.group_30.actions.GoldAction;
class LinkTest {
@Nested
class An_instantiated_Link_object {
@Test
void can_be_constructed() {
Link link = new Link("text", "ref");
assertEquals("text", link.getText());
assertEquals("ref", link.getReference());
}
@Test
void cannot_be_constructed_with_empty_text() {
/* here the text is completely empty */
assertThrows(IllegalArgumentException.class, () -> new Link("", "ref"));
/* here the text contains some spaces */
assertThrows(IllegalArgumentException.class, () -> new Link(" ", "ref"));
}
@Test
void cannot_be_constructed_with_empty_reference() {
/* here the reference is completely empty */
assertThrows(IllegalArgumentException.class, () -> new Link("text", ""));
/* here the reference contains some spaces */
assertThrows(IllegalArgumentException.class, () -> new Link("text", " "));
}
}
@Nested
class A_valid_Link_object {
@Test
void can_add_action() {
Link link = new Link("text", "ref");
GoldAction goldAction = new GoldAction(10);
link.addAction(goldAction);
assertEquals(goldAction, link.getActions().get(0));
assertEquals(1, link.getActions().size());
}
@Test
void can_get_all_actions() {
Link link = new Link("text", "ref");
GoldAction goldAction = new GoldAction(10);
GoldAction goldAction2 = new GoldAction(20);
link.addAction(goldAction);
link.addAction(goldAction2);
assertEquals(2, link.getActions().size());
}
}
}
\ No newline at end of file
package edu.ntnu.idatt2001.group_30;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class PassageTest {
@Nested
class An_instantiated_Passage_object {
@Test
void can_be_constructed() {
Passage passage = new Passage("title", "nothing much :-)");
assertEquals("title", passage.getTitle());
assertEquals("nothing much :-)", passage.getContent());
}
@Test
void cannot_be_constructed_with_empty_text() {
/* here the text is completely empty */
assertThrows(IllegalArgumentException.class, () -> new Passage("", "ref"));
/* here the text contains some spaces */
assertThrows(IllegalArgumentException.class, () -> new Passage(" ", "ref"));
}
@Test
void cannot_be_constructed_with_empty_reference() {
/* here the reference is completely empty */
assertThrows(IllegalArgumentException.class, () -> new Passage("text", ""));
/* here the reference contains some spaces */
assertThrows(IllegalArgumentException.class, () -> new Passage("text", " "));
}
}
@Nested
class A_valid_Passage_object {
@Test
void can_add_link() {
Passage passage = new Passage("title", "nothing much :-)");
Link link = new Link("text", "ref");
passage.addLink(link);
assertEquals(link, passage.getLinks().get(0));
assertEquals(1, passage.getLinks().size());
}
@Test
void can_get_all_links() {
Passage passage = new Passage("title", "nothing much :-)");
Link link = new Link("text", "ref");
Link link2 = new Link("text2", "ref2");
passage.addLink(link);
passage.addLink(link2);
assertEquals(2, passage.getLinks().size());
}
@Test
void has_link_only_returns_true_if_link_exists() {
Passage passage = new Passage("title", "nothing much :-)");
Link link = new Link("text", "ref");
passage.addLink(link);
assertTrue(passage.hasLinks());
}
@Test
void has_link_returns_false_if_no_links() {
Passage passage = new Passage("title", "nothing much :-)");
assertFalse(passage.hasLinks());
}
}
}
package edu.ntnu.idatt2001.group_30;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class PlayerTest {
@Nested
class An_instantiated_Player_object {
@Test
void can_be_constructed() {
Player player = new Player("Ola Nordmann", 1, 2, 3);
assertEquals("Ola Nordmann", player.getName());
assertEquals(1, player.getHealth());
assertEquals(2, player.getScore());
assertEquals(3, player.getGold());
}
@Test
void cannot_be_constructed_with_negative_health() {
assertThrows(IllegalArgumentException.class, () -> new Player("Ola Nordmann", -1, 2, 3));
}
@Test
void cannot_be_constructed_with_negative_score() {
assertThrows(IllegalArgumentException.class, () -> new Player("Ola Nordmann", 1, -2, 3));
}
@Test
void cannot_be_constructed_with_negative_gold() {
assertThrows(IllegalArgumentException.class, () -> new Player("Ola Nordmann", 1, 2, -3));
}
@Test
void cannot_be_constructed_with_empty_name() {
/* here the name is completely empty */
assertThrows(IllegalArgumentException.class, () -> new Player("", 1, 2, 3));
/* here the name contains some spaces */
assertThrows(IllegalArgumentException.class, () -> new Player(" ", 1, 2, 3));
}
@Test
void can_have_health_added() {
Player player = new Player("Ola Nordmann", 1, 2, 3);
player.addHealth(1);
assertEquals(2, player.getHealth());
}
@Test
void will_have_zero_health_if_gets_dealth_more_damage_than_current_health() {
/* set start health to 5 */
Player player = new Player("Ola Nordmann", 1, 2, 3);
/* subtract health with 6, so more than initial starting health */
player.addHealth(-6);
assertEquals(0, player.getHealth());
}
}
}
package edu.ntnu.idatt2001.group_30;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class StoryTest {
@Nested
class An_instantiated_Story_object {
@Test
void can_be_constructed() {
Story story = new Story("title", new Passage("valid", "valid"));
assertEquals("title", story.getTitle());
}
@Test
void cannot_be_constructed_with_empty_title() {
Passage passage = new Passage("valid", "valid");
/* here the title is completely empty */
assertThrows(IllegalArgumentException.class, () -> new Story("", passage));
/* here the title contains some spaces */
assertThrows(IllegalArgumentException.class, () -> new Story(" ", passage));
}
@Test
void cannot_be_constructed_with_null_passage() {
assertThrows(IllegalArgumentException.class, () -> new Story("title", null));
}
}
@Nested
class A_valid_Story_object {
@Test
void can_get_all_passages() {
Story story = new Story("title", new Passage("valid", "valid"));
Passage passage = new Passage("valid", "valid");
Passage passage2 = new Passage("valid2", "valid2");
story.addPassage(passage);
story.addPassage(passage2);
assertEquals(2, story.getPassages().size());
}
@Test
void can_get_passage_by_reference() {
Passage passageInitial = new Passage("valid", "valid");
Passage passage = new Passage("still valid", "still valid");
Story story = new Story("title", passageInitial);
story.addPassage(passage);
Link linkForInitialPassage = new Link("valid", "valid");
assertEquals(passageInitial, story.getPassage(linkForInitialPassage));
}
@Test
void can_get_opening_passage() {
Passage passage = new Passage("valid", "valid");
Story story = new Story("title", passage);
assertEquals(passage, story.getOpeningPassage());
}
}
}
package edu.ntnu.idatt2001.group_30.actions;
import edu.ntnu.idatt2001.group_30.Player;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.*;
class GoldActionTest {
@Nested
public class A_GoldAction_object {
@Test
public void instantiates_properly_with_valid_argument() {
GoldAction goldAction;
try{
goldAction = new GoldAction(10);
}
catch (Exception e) {
fail();
}
}
@ParameterizedTest
@ValueSource(ints = {-1, 0, 1})
public void properly_adds_gold_amount_to_Player_gold(int goldAmount) {
int playerStartGold = 10;
GoldAction goldAction = new GoldAction(goldAmount);
Player player = new Player("Trym", 10, 10, playerStartGold);
int expectedGoldAfter = playerStartGold + goldAmount;
goldAction.execute(player);
int actualGoldAfter = player.getGold();
Assertions.assertEquals(expectedGoldAfter, actualGoldAfter);
}
@Test
public void throws_NullPointerException_executing_with_null_argument() {
GoldAction goldAction = new GoldAction(10);
Player player = null;
assertThrows(NullPointerException.class, () -> goldAction.execute(player));
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment