Skip to content

Commit

Permalink
Add some testing for addGameCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
JJtan2002 committed Nov 4, 2024
1 parent 1c635eb commit 5fde3d7
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 108 deletions.
33 changes: 21 additions & 12 deletions src/main/java/seedu/address/logic/commands/AddGameCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class AddGameCommand extends Command {
public static final String MESSAGE_GAME_EXISTS = "The game provided already exists for that person.";

private final Index index;
private final AddGameDescriptor addGameDescriptor;
private final GameDescriptor addGameDescriptor;
private final String gameName;

private Person personToEdit;
Expand All @@ -57,13 +57,13 @@ public class AddGameCommand extends Command {
* @param index of the person in the filtered person list to edit
* @param addGameDescriptor details to edit the person with
*/
public AddGameCommand(Index index, String gameName, AddGameDescriptor addGameDescriptor) {
public AddGameCommand(Index index, String gameName, GameDescriptor addGameDescriptor) {
requireNonNull(index);
requireNonNull(addGameDescriptor);

this.index = index;
this.gameName = gameName;
this.addGameDescriptor = new AddGameDescriptor(addGameDescriptor);
this.addGameDescriptor = new GameDescriptor(addGameDescriptor);
}

@Override
Expand Down Expand Up @@ -105,7 +105,7 @@ public void undo(Model model) {
/**
* Creates and returns a {@code Game} with the details of {@code addGameDescriptor}
*/
private static Game createNewGame(String gameName, AddGameCommand.AddGameDescriptor addGameDescriptor) {
private static Game createNewGame(String gameName, AddGameCommand.GameDescriptor addGameDescriptor) {
Username updatedUsername = addGameDescriptor.getUsername().orElse(new Username(""));
SkillLevel updatedSkillLevel = addGameDescriptor.getSkillLevel().orElse(new SkillLevel(""));
Role updatedRole = addGameDescriptor.getRole().orElse(new Role(""));
Expand Down Expand Up @@ -140,18 +140,20 @@ public String toString() {
/**
* Stores the details for the game to be added.
*/
public static class AddGameDescriptor {
public static class GameDescriptor {
private String game;
private Username username;
private SkillLevel skillLevel;
private Role role;
private boolean isFavourite;

public AddGameDescriptor() {}
public GameDescriptor() {}

/**
* Copy constructor.
*/
public AddGameDescriptor(AddGameCommand.AddGameDescriptor toCopy) {
public GameDescriptor(AddGameCommand.GameDescriptor toCopy) {
setGame(toCopy.game);
setUsername(toCopy.username);
setSkillLevel(toCopy.skillLevel);
setRole(toCopy.role);
Expand All @@ -163,6 +165,13 @@ public AddGameDescriptor(AddGameCommand.AddGameDescriptor toCopy) {
public boolean isAnyFieldEdited() {
return CollectionUtil.isAnyNonNull(username, skillLevel, role);
}
public void setGame(String game) {
this.game = game;
}

public Optional<String> getGame() {
return Optional.ofNullable(game);
}
public void setUsername(Username username) {
this.username = username;
}
Expand Down Expand Up @@ -198,14 +207,14 @@ public boolean equals(Object other) {
}

// instanceof handles nulls
if (!(other instanceof AddGameCommand.AddGameDescriptor)) {
if (!(other instanceof AddGameCommand.GameDescriptor)) {
return false;
}

AddGameCommand.AddGameDescriptor otherAddGameDescriptor = (AddGameCommand.AddGameDescriptor) other;
return Objects.equals(username, otherAddGameDescriptor.username)
&& Objects.equals(skillLevel, otherAddGameDescriptor.skillLevel)
&& Objects.equals(role, otherAddGameDescriptor.role);
AddGameCommand.GameDescriptor otherGameDescriptor = (AddGameCommand.GameDescriptor) other;
return Objects.equals(username, otherGameDescriptor.username)
&& Objects.equals(skillLevel, otherGameDescriptor.skillLevel)
&& Objects.equals(role, otherGameDescriptor.role);
}

@Override
Expand Down
93 changes: 4 additions & 89 deletions src/main/java/seedu/address/logic/commands/EditGameCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.CollectionUtil;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.AddGameCommand.GameDescriptor;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.game.Game;
Expand Down Expand Up @@ -51,7 +48,7 @@ public class EditGameCommand extends Command {

private final Index index;
private final String gameName;
private final EditGameDescriptor editGameDescriptor;
private final GameDescriptor editGameDescriptor;

private Game gameToEdit;
private Person personToEdit;
Expand All @@ -60,7 +57,7 @@ public class EditGameCommand extends Command {
* @param index of the person in the filtered person list to edit
* @param editGameDescriptor details to edit the game with
*/
public EditGameCommand(Index index, String gameName, EditGameDescriptor editGameDescriptor) {
public EditGameCommand(Index index, String gameName, GameDescriptor editGameDescriptor) {
requireNonNull(index);
requireNonNull(editGameDescriptor);

Expand Down Expand Up @@ -108,94 +105,12 @@ public void undo(Model model) {
* Creates and returns a {@code Game} with the details of {@code gameToEdit}
* edited with {@code editGameDescriptor}.
*/
private static Game createEditedGame(Game gameToEdit, EditGameDescriptor editGameDescriptor) {
private static Game createEditedGame(Game gameToEdit, GameDescriptor editGameDescriptor) {
assert gameToEdit != null;
Username updatedUsername = editGameDescriptor.getUsername().orElse(gameToEdit.getUsername());
SkillLevel updatedSkillLevel = editGameDescriptor.getSkillLevel().orElse(gameToEdit.getSkillLevel());
Role updatedRole = editGameDescriptor.getRole().orElse(gameToEdit.getRole());
boolean updatedFavouriteStatus = gameToEdit.getFavouriteStatus();
return new Game(gameToEdit.gameName, updatedUsername, updatedSkillLevel, updatedRole, updatedFavouriteStatus);
}

/**
* Stores the details to edit the game with. Each non-empty field will
* replace the corresponding field of the game.
*/
public static class EditGameDescriptor {
private Username username;
private SkillLevel skillLevel;
private Role role;
private boolean isFavourite;

public EditGameDescriptor() {}

/**
* Copy constructor.
*/
public EditGameDescriptor(EditGameDescriptor toCopy) {
setUsername(toCopy.username);
setSkillLevel(toCopy.skillLevel);
setRole(toCopy.role);
}

/**
* Returns true if at least one field is edited.
*/
public boolean isAnyFieldEdited() {
return CollectionUtil.isAnyNonNull(username, skillLevel, role);
}
public void setUsername(Username username) {
this.username = username;
}

public Optional<Username> getUsername() {
return Optional.ofNullable(username);
}

public void setSkillLevel(SkillLevel skillLevel) {
this.skillLevel = skillLevel;
}

public Optional<SkillLevel> getSkillLevel() {
return Optional.ofNullable(skillLevel);
}

public void setRole(Role role) {
this.role = role;
}

public Optional<Role> getRole() {
return Optional.ofNullable(role);
}

public boolean getFavouriteStatus() {
return this.isFavourite;
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof EditGameCommand.EditGameDescriptor)) {
return false;
}

EditGameCommand.EditGameDescriptor otherEditGameDescriptor = (EditGameCommand.EditGameDescriptor) other;
return Objects.equals(username, otherEditGameDescriptor.username)
&& Objects.equals(skillLevel, otherEditGameDescriptor.skillLevel)
&& Objects.equals(role, otherEditGameDescriptor.role);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("username", username)
.add("skillLevel", skillLevel)
.add("role", role)
.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.AddGameCommand;
import seedu.address.logic.commands.AddGameCommand.AddGameDescriptor;
import seedu.address.logic.commands.AddGameCommand.GameDescriptor;
import seedu.address.logic.parser.exceptions.ParseException;


Expand Down Expand Up @@ -40,7 +40,7 @@ public AddGameCommand parse(String args) throws ParseException {

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_GAME, PREFIX_USERNAME, PREFIX_SKILLLEVEL, PREFIX_ROLE);

AddGameDescriptor addGameDescriptor = new AddGameDescriptor();
GameDescriptor addGameDescriptor = new GameDescriptor();

if (argMultimap.getValue(PREFIX_USERNAME).isPresent()) {
addGameDescriptor.setUsername(ParserUtil.parseUsername(argMultimap.getValue(PREFIX_USERNAME).get()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_USERNAME;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.AddGameCommand.GameDescriptor;
import seedu.address.logic.commands.EditGameCommand;
import seedu.address.logic.commands.EditGameCommand.EditGameDescriptor;
import seedu.address.logic.parser.exceptions.ParseException;

/**
Expand Down Expand Up @@ -38,7 +38,7 @@ public EditGameCommand parse(String args) throws ParseException {

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_GAME, PREFIX_USERNAME, PREFIX_SKILLLEVEL, PREFIX_ROLE);

EditGameDescriptor editGameDescriptor = new EditGameDescriptor();
GameDescriptor editGameDescriptor = new GameDescriptor();

if (argMultimap.getValue(PREFIX_USERNAME).isPresent()) {
editGameDescriptor.setUsername(ParserUtil.parseUsername(argMultimap.getValue(PREFIX_USERNAME).get()));
Expand Down
39 changes: 39 additions & 0 deletions src/test/java/seedu/address/logic/commands/AddGameCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package seedu.address.logic.commands;

import static seedu.address.logic.commands.CommandTestUtil.VALID_GAME;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

import org.junit.jupiter.api.Test;

import seedu.address.logic.Messages;
import seedu.address.logic.commands.AddGameCommand.GameDescriptor;
import seedu.address.model.AddressBook;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.game.Game;
import seedu.address.model.person.Person;
import seedu.address.testutil.EditPersonDescriptorBuilder;
import seedu.address.testutil.GameDescriptorBuilder;
import seedu.address.testutil.PersonBuilder;
/**
* Contains integration tests (interaction with the Model) and unit tests for AddGameCommand.
*/
public class AddGameCommandTest {
private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
@Test
public void execute_allFieldsSpecifiedUnfilteredList_success() {
Person editedPerson = new PersonBuilder().withGames(VALID_GAME).build();
GameDescriptor descriptor = new GameDescriptorBuilder().withGame(VALID_GAME).build();

AddGameCommand addCommand = new AddGameCommand(INDEX_FIRST_PERSON, VALID_GAME, descriptor);

Game expectedGame = editedPerson.getGames().get(VALID_GAME);
String expectedMessage = String.format(AddGameCommand.MESSAGE_ADD_GAME_SUCCESS, Messages.format(expectedGame));
Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs());

assertCommandSuccess(addCommand, model, expectedMessage, model);
}
}
16 changes: 13 additions & 3 deletions src/test/java/seedu/address/logic/commands/CommandTestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_GAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
Expand All @@ -21,6 +20,7 @@
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.model.person.Person;
import seedu.address.testutil.EditPersonDescriptorBuilder;
import seedu.address.testutil.GameDescriptorBuilder;

/**
* Contains helper methods for testing commands.
Expand All @@ -37,6 +37,10 @@ public class CommandTestUtil {
public static final String VALID_ADDRESS_BOB = "Block 123, Bobby Street 3";
public static final String VALID_TAG_HUSBAND = "husband";
public static final String VALID_TAG_FRIEND = "friend";
public static final String VALID_GAME = "LoL";
public static final String VALID_GAME_USERNAME = "Potato";
public static final String VALID_GAME_SKILLLEVEL = "Pro";
public static final String VALID_GAME_ROLE = "Support";

public static final String NAME_DESC_AMY = " " + PREFIX_NAME + VALID_NAME_AMY;
public static final String NAME_DESC_BOB = " " + PREFIX_NAME + VALID_NAME_BOB;
Expand All @@ -49,8 +53,6 @@ public class CommandTestUtil {
public static final String TAG_DESC_FRIEND = " " + PREFIX_TAG + VALID_TAG_FRIEND;
public static final String TAG_DESC_HUSBAND = " " + PREFIX_TAG + VALID_TAG_HUSBAND;

public static final String GAME_DESC = " " + PREFIX_GAME + "LoL";

public static final String INVALID_NAME_DESC = " " + PREFIX_NAME + "James&"; // '&' not allowed in names
public static final String INVALID_PHONE_DESC = " " + PREFIX_PHONE + "911a"; // 'a' not allowed in phones
public static final String INVALID_EMAIL_DESC = " " + PREFIX_EMAIL + "bob!yahoo"; // missing '@' symbol
Expand All @@ -72,6 +74,14 @@ public class CommandTestUtil {
.withTags(VALID_TAG_HUSBAND, VALID_TAG_FRIEND).build();
}

public static final AddGameCommand.GameDescriptor DESC_GAME;

static {
DESC_GAME = new GameDescriptorBuilder().withGame(VALID_GAME)
.withUsername(VALID_GAME_USERNAME).withSkillLevel(VALID_GAME_SKILLLEVEL)
.withRole(VALID_GAME_ROLE).build();
}

/**
* Executes the given {@code command}, confirms that <br>
* - the returned {@link CommandResult} matches {@code expectedCommandResult} <br>
Expand Down
41 changes: 41 additions & 0 deletions src/test/java/seedu/address/logic/commands/GameDescriptorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package seedu.address.logic.commands;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.logic.commands.CommandTestUtil.DESC_GAME;

import org.junit.jupiter.api.Test;

import seedu.address.testutil.GameDescriptorBuilder;

public class GameDescriptorTest {

@Test
public void equals() {
// same values -> returns true
AddGameCommand.GameDescriptor descriptorWithSameValues = new AddGameCommand.GameDescriptor(DESC_GAME);
assertTrue(DESC_GAME.equals(descriptorWithSameValues));

// same object -> returns true
assertTrue(DESC_GAME.equals(DESC_GAME));

// null -> returns false
assertFalse(DESC_GAME.equals(null));

// different types -> returns false
assertFalse(DESC_GAME.equals(5));

// different username -> returns false
AddGameCommand.GameDescriptor editedGame = new GameDescriptorBuilder(DESC_GAME).withUsername("Bob").build();
assertFalse(DESC_GAME.equals(editedGame));

// different role -> returns false
editedGame = new GameDescriptorBuilder(DESC_GAME).withRole("DPS").build();
assertFalse(DESC_GAME.equals(editedGame));

// different skillLevel -> returns false
editedGame = new GameDescriptorBuilder(DESC_GAME).withSkillLevel("Noob").build();
assertFalse(DESC_GAME.equals(editedGame));
}

}
Loading

0 comments on commit 5fde3d7

Please sign in to comment.