From dea32b511c647b085d049179ae3b3c05519b77de Mon Sep 17 00:00:00 2001 From: nixonwidjaja Date: Fri, 13 Oct 2023 04:21:59 +0800 Subject: [PATCH] Add leavecommand tests --- .../address/logic/commands/LeaveCommand.java | 4 +- .../logic/commands/LeaveCommandTest.java | 51 +++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 src/test/java/seedu/address/logic/commands/LeaveCommandTest.java diff --git a/src/main/java/seedu/address/logic/commands/LeaveCommand.java b/src/main/java/seedu/address/logic/commands/LeaveCommand.java index e8817512175..743831b608b 100644 --- a/src/main/java/seedu/address/logic/commands/LeaveCommand.java +++ b/src/main/java/seedu/address/logic/commands/LeaveCommand.java @@ -30,7 +30,7 @@ public class LeaveCommand extends Command { public static final String MESSAGE_AMBIGUOUS = "Please check your MONTHS. Ambiguous leave(s) assignment\n"; public static final String MESSAGE_INVALID_MONTH = "Please check your MONTHS. Invalid month provided.\n"; - public static final String MESSAGE_EDIT_PERSON_SUCCESS = "Leave(s) successfully updated for employee: %1$s"; + public static final String MESSAGE_LEAVE_SUCCESS = "Leave(s) successfully updated for employee: %1$s"; public static final String MESSAGE_NOT_A_NUMBER = "Please check your MONTHS. Some is not a number.\n"; public static final String MESSAGE_NOT_EDITED = "The employee's leave(s) does not change from previous state: %1$s"; public static final String MESSAGE_SPACES_DETECTED = "Spaces detected in your MONTHS.\n"; @@ -75,7 +75,7 @@ public CommandResult execute(Model model) throws CommandException { model.setPerson(oldPerson, newPerson); model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); - return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(newPerson))); + return new CommandResult(String.format(MESSAGE_LEAVE_SUCCESS, Messages.format(newPerson))); } @Override diff --git a/src/test/java/seedu/address/logic/commands/LeaveCommandTest.java b/src/test/java/seedu/address/logic/commands/LeaveCommandTest.java new file mode 100644 index 00000000000..601ccc233c2 --- /dev/null +++ b/src/test/java/seedu/address/logic/commands/LeaveCommandTest.java @@ -0,0 +1,51 @@ +package seedu.address.logic.commands; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; +import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; +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.Test; + +import seedu.address.commons.core.index.Index; +import seedu.address.logic.Messages; +import seedu.address.model.Model; +import seedu.address.model.ModelManager; +import seedu.address.model.UserPrefs; +import seedu.address.model.person.Person; + +public class LeaveCommandTest { + private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); + private LeaveCommand leaveFirstCommand = new LeaveCommand(INDEX_FIRST_PERSON, "0000+0000000"); + private LeaveCommand leaveSecondCommand = new LeaveCommand(INDEX_SECOND_PERSON, "0+0000000000"); + + @Test + public void equals() { + assertFalse(leaveFirstCommand.equals(null)); + assertEquals(leaveFirstCommand, leaveFirstCommand); + assertFalse(leaveFirstCommand.equals(leaveSecondCommand)); + assertFalse(leaveFirstCommand.toString().equals("")); + } + + @Test + public void execute_success() { + Person person = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); + String expected = String.format(LeaveCommand.MESSAGE_LEAVE_SUCCESS, Messages.format(person)); + ModelManager expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs()); + assertCommandSuccess(leaveFirstCommand, model, expected, expectedModel); + } + + @Test + public void execute_failure() { + Index outOfBoundIndex = Index.fromOneBased(model.getFilteredPersonList().size() + 1); + LeaveCommand command = new LeaveCommand(outOfBoundIndex, "0000+0000000"); + assertCommandFailure(command, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + + Person person = model.getFilteredPersonList().get(INDEX_SECOND_PERSON.getZeroBased()); + assertCommandFailure(leaveSecondCommand, model, + String.format(LeaveCommand.MESSAGE_NOT_EDITED, person.getLeave().toString())); + } +}