Skip to content

Commit

Permalink
Add working tests for AddGameCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
JJtan2002 committed Nov 4, 2024
1 parent 5fde3d7 commit 045dad6
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 7 deletions.
55 changes: 49 additions & 6 deletions src/test/java/seedu/address/logic/commands/AddGameCommandTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package seedu.address.logic.commands;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static seedu.address.logic.commands.CommandTestUtil.VALID_GAME;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.logic.commands.CommandTestUtil.showPersonAtIndex;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON;
import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_PERSON;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import seedu.address.logic.Messages;
Expand All @@ -15,25 +20,63 @@
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());
@AfterEach
public void setUp() {
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);
Person personInFilteredList = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
Person editedPerson = new PersonBuilder(personInFilteredList).withGames(VALID_GAME).build();

Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs());
expectedModel.setPerson(model.getFilteredPersonList().get(0), editedPerson);

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);
assertCommandSuccess(addCommand, model, expectedMessage, expectedModel);
assertEquals(expectedGame.getGameName(), VALID_GAME);
}

@Test
public void execute_duplicatePersonUnfilteredList_failure() {
Person personInFilteredList = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
Person editedPerson = new PersonBuilder(personInFilteredList).withGames(VALID_GAME).build();

model.setPerson(model.getFilteredPersonList().get(0), editedPerson);

// edit person in filtered list into a duplicate in address book
Person personInList = model.getAddressBook().getPersonList().get(INDEX_SECOND_PERSON.getZeroBased());
AddGameCommand addCommand = new AddGameCommand(INDEX_FIRST_PERSON, VALID_GAME,
new GameDescriptorBuilder().build());

assertCommandFailure(addCommand, model, AddGameCommand.MESSAGE_GAME_EXISTS);
}
@Test
public void execute_duplicatePersonFilteredList_failure() {
showPersonAtIndex(model, INDEX_FIRST_PERSON);

Person personInFilteredList = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
Person editedPerson = new PersonBuilder(personInFilteredList).withGames(VALID_GAME).build();

model.setPerson(model.getFilteredPersonList().get(0), editedPerson);

// edit person in filtered list into a duplicate in address book
Person personInList = model.getAddressBook().getPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
AddGameCommand addCommand = new AddGameCommand(INDEX_FIRST_PERSON, VALID_GAME,
new GameDescriptorBuilder().withGame(VALID_GAME).build());

assertCommandFailure(addCommand, model, AddGameCommand.MESSAGE_GAME_EXISTS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ 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 = "Legend League";
public static final String VALID_GAME_USERNAME = "Potato";
public static final String VALID_GAME_SKILLLEVEL = "Pro";
public static final String VALID_GAME_ROLE = "Support";
Expand Down
78 changes: 78 additions & 0 deletions src/test/java/seedu/address/testutil/GameBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package seedu.address.testutil;


import seedu.address.model.game.Game;
import seedu.address.model.game.Role;
import seedu.address.model.game.SkillLevel;
import seedu.address.model.game.Username;
/**
* A utility class to help with building Game objects.
*/
public class GameBuilder {

public static final String DEFAULT_GAME_NAME = "LoL";
public static final String DEFAULT_PHONE = "85355255";
public static final String DEFAULT_EMAIL = "[email protected]";
public static final String DEFAULT_ADDRESS = "123, Jurong West Ave 6, #08-111";

private String gameName;
private Role role;
private SkillLevel skillLevel;
private Username username;
/**
* Creates a {@code PersonBuilder} with the default details.
*/
public GameBuilder() {
gameName = DEFAULT_GAME_NAME;
role = new Role("");
skillLevel = new SkillLevel("");
username = new Username("");
}

/**
* Initializes the PersonBuilder with the data of {@code personToCopy}.
*/
public GameBuilder(Game gameToCopy) {
gameName = gameToCopy.getGameName();
role = gameToCopy.getRole();
skillLevel = gameToCopy.getSkillLevel();
username = gameToCopy.getUsername();
}

/**
* Sets the {@code gameName} of the {@code Game} that we are building.
*/
public GameBuilder withGameName(String gameName) {
this.gameName = gameName;
return this;
}

/**
* Sets the {@code Role} of the {@code Game} that we are building.
*/
public GameBuilder withRole(String role) {
this.role = new Role(role);
return this;
}

/**
* Sets the {@code Role} of the {@code Game} that we are building.
*/
public GameBuilder withSkillLevel(String skillLevel) {
this.skillLevel = new SkillLevel(skillLevel);
return this;
}

/**
* Sets the {@code Role} of the {@code Game} that we are building.
*/
public GameBuilder withUsername(String username) {
this.username = new Username(username);
return this;
}

public Game build() {
return new Game(gameName, username, skillLevel, role, false);
}

}

0 comments on commit 045dad6

Please sign in to comment.