From 045dad6ccf0d6e22f3506650babfaf9f67df749d Mon Sep 17 00:00:00 2001 From: JJtan <95962077+JJtan2002@users.noreply.github.com> Date: Mon, 4 Nov 2024 23:38:11 +0800 Subject: [PATCH] Add working tests for AddGameCommand --- .../logic/commands/AddGameCommandTest.java | 55 +++++++++++-- .../logic/commands/CommandTestUtil.java | 2 +- .../seedu/address/testutil/GameBuilder.java | 78 +++++++++++++++++++ 3 files changed, 128 insertions(+), 7 deletions(-) create mode 100644 src/test/java/seedu/address/testutil/GameBuilder.java diff --git a/src/test/java/seedu/address/logic/commands/AddGameCommandTest.java b/src/test/java/seedu/address/logic/commands/AddGameCommandTest.java index cf3371929e3..698f5dbeb93 100644 --- a/src/test/java/seedu/address/logic/commands/AddGameCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/AddGameCommandTest.java @@ -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; @@ -15,7 +20,6 @@ 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; /** @@ -23,17 +27,56 @@ */ 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); } } diff --git a/src/test/java/seedu/address/logic/commands/CommandTestUtil.java b/src/test/java/seedu/address/logic/commands/CommandTestUtil.java index 03a313fd736..82b1816bfc4 100644 --- a/src/test/java/seedu/address/logic/commands/CommandTestUtil.java +++ b/src/test/java/seedu/address/logic/commands/CommandTestUtil.java @@ -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"; diff --git a/src/test/java/seedu/address/testutil/GameBuilder.java b/src/test/java/seedu/address/testutil/GameBuilder.java new file mode 100644 index 00000000000..d6222552829 --- /dev/null +++ b/src/test/java/seedu/address/testutil/GameBuilder.java @@ -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 = "amy@gmail.com"; + 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); + } + +}