forked from nus-cs2103-AY2425S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
139 additions
and
10 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/main/java/seedu/address/logic/commands/UndoCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.AgentAssist; | ||
import seedu.address.model.Model; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
/** | ||
* Clears the address book. | ||
*/ | ||
public class UndoCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "undo"; | ||
public static final String MESSAGE_SUCCESS = "Your latest command has been undone."; | ||
public static final String MESSAGE_NO_COMMAND_TO_UNDO = "Please input a command first in order to undo it."; | ||
|
||
@Override | ||
protected CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
if (!model.hasPreviousCommand()) { | ||
throw new CommandException(MESSAGE_NO_COMMAND_TO_UNDO); | ||
} | ||
model.undoCommand(); | ||
return new CommandResult(MESSAGE_SUCCESS); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/test/java/seedu/address/logic/commands/UndoCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import seedu.address.logic.Messages; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.AgentAssist; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.ModelManager; | ||
import seedu.address.model.UserPrefs; | ||
import seedu.address.model.person.Person; | ||
import seedu.address.testutil.EditPersonDescriptorBuilder; | ||
import seedu.address.testutil.PersonBuilder; | ||
|
||
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.TypicalPersons.getTypicalAgentAssist; | ||
|
||
public class UndoCommandTest { | ||
|
||
private Model model = new ModelManager(getTypicalAgentAssist(), new UserPrefs()); | ||
|
||
@Test | ||
public void execute_undo_success() { | ||
Person editedPerson = new PersonBuilder().build(); | ||
EditCommand.EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder(editedPerson).build(); | ||
EditCommand editCommand = new EditCommand(INDEX_FIRST_PERSON, descriptor); | ||
try { | ||
CommandResult result = editCommand.execute(model); | ||
} catch (CommandException ce) { | ||
throw new AssertionError("Execution of command should not fail.", ce); | ||
} | ||
|
||
UndoCommand undoCommand = new UndoCommand(); | ||
Model expectedModel = new ModelManager(getTypicalAgentAssist(), new UserPrefs()); | ||
|
||
assertCommandSuccess(undoCommand, model, UndoCommand.MESSAGE_SUCCESS, expectedModel); | ||
} | ||
|
||
@Test | ||
public void execute_noPreviousCommand_failure() { | ||
Model model = new ModelManager(); | ||
assertCommandFailure(new UndoCommand(), model, UndoCommand.MESSAGE_NO_COMMAND_TO_UNDO); | ||
} | ||
|
||
} |