From af283cfa8e96f633ffdd264a98e84c0ee656fc75 Mon Sep 17 00:00:00 2001 From: muller317 Date: Fri, 11 Oct 2024 00:08:35 +0800 Subject: [PATCH] Cover added code by tests. --- .../logic/commands/DeleteCommandTest.java | 78 +++++++++++++++---- 1 file changed, 61 insertions(+), 17 deletions(-) diff --git a/src/test/java/seedu/address/logic/commands/DeleteCommandTest.java b/src/test/java/seedu/address/logic/commands/DeleteCommandTest.java index 4553daaabae..5c685c561d3 100644 --- a/src/test/java/seedu/address/logic/commands/DeleteCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/DeleteCommandTest.java @@ -27,6 +27,7 @@ public class DeleteCommandTest { private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); + // Test for valid index-based deletion in an unfiltered list @Test public void execute_validIndexUnfilteredList_success() { Person personToDelete = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); @@ -41,14 +42,32 @@ public void execute_validIndexUnfilteredList_success() { assertCommandSuccess(deleteCommand, model, expectedMessage, expectedModel); } + // Test for valid name-based deletion in an unfiltered list @Test - public void execute_invalidIndexUnfilteredList_throwsCommandException() { - Index outOfBoundIndex = Index.fromOneBased(model.getFilteredPersonList().size() + 1); - DeleteCommand deleteCommand = new DeleteCommand(outOfBoundIndex); + public void execute_validNameUnfilteredList_success() { + Person personToDelete = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); + String nameToDelete = personToDelete.getName().fullName; + DeleteCommand deleteCommand = new DeleteCommand(nameToDelete); + + String expectedMessage = String.format(DeleteCommand.MESSAGE_DELETE_PERSON_SUCCESS, + Messages.format(personToDelete)); + + ModelManager expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs()); + expectedModel.deletePerson(personToDelete); + + assertCommandSuccess(deleteCommand, model, expectedMessage, expectedModel); + } + + // Test for invalid name-based deletion in an unfiltered list + @Test + public void execute_invalidNameUnfilteredList_throwsCommandException() { + String invalidName = "Non Existent Person"; + DeleteCommand deleteCommand = new DeleteCommand(invalidName); - assertCommandFailure(deleteCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + assertCommandFailure(deleteCommand, model, String.format(DeleteCommand.MESSAGE_PERSON_NOT_FOUND, invalidName)); } + // Test for valid index-based deletion in a filtered list @Test public void execute_validIndexFilteredList_success() { showPersonAtIndex(model, INDEX_FIRST_PERSON); @@ -66,39 +85,65 @@ public void execute_validIndexFilteredList_success() { assertCommandSuccess(deleteCommand, model, expectedMessage, expectedModel); } + // Test for valid name-based deletion in a filtered list @Test - public void execute_invalidIndexFilteredList_throwsCommandException() { + public void execute_validNameFilteredList_success() { showPersonAtIndex(model, INDEX_FIRST_PERSON); - Index outOfBoundIndex = INDEX_SECOND_PERSON; - // ensures that outOfBoundIndex is still in bounds of address book list - assertTrue(outOfBoundIndex.getZeroBased() < model.getAddressBook().getPersonList().size()); + Person personToDelete = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); + String nameToDelete = personToDelete.getName().fullName; + DeleteCommand deleteCommand = new DeleteCommand(nameToDelete); - DeleteCommand deleteCommand = new DeleteCommand(outOfBoundIndex); + String expectedMessage = String.format(DeleteCommand.MESSAGE_DELETE_PERSON_SUCCESS, + Messages.format(personToDelete)); - assertCommandFailure(deleteCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + Model expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs()); + expectedModel.deletePerson(personToDelete); + showNoPerson(expectedModel); + + assertCommandSuccess(deleteCommand, model, expectedMessage, expectedModel); } + // Test for invalid name-based deletion in a filtered list + @Test + public void execute_invalidNameFilteredList_throwsCommandException() { + showPersonAtIndex(model, INDEX_FIRST_PERSON); + + String invalidName = "Non Existent Person"; + DeleteCommand deleteCommand = new DeleteCommand(invalidName); + + assertCommandFailure(deleteCommand, model, String.format(DeleteCommand.MESSAGE_PERSON_NOT_FOUND, invalidName)); + } + + // Equality tests for both index-based and name-based commands @Test public void equals() { DeleteCommand deleteFirstCommand = new DeleteCommand(INDEX_FIRST_PERSON); DeleteCommand deleteSecondCommand = new DeleteCommand(INDEX_SECOND_PERSON); + DeleteCommand deleteNameCommand = new DeleteCommand("John Doe"); - // same object -> returns true + // Same object -> returns true assertTrue(deleteFirstCommand.equals(deleteFirstCommand)); - // same values -> returns true + // Same values -> returns true DeleteCommand deleteFirstCommandCopy = new DeleteCommand(INDEX_FIRST_PERSON); assertTrue(deleteFirstCommand.equals(deleteFirstCommandCopy)); - // different types -> returns false + // Different index -> returns false + assertFalse(deleteFirstCommand.equals(deleteSecondCommand)); + + // Different types -> returns false assertFalse(deleteFirstCommand.equals(1)); - // null -> returns false + // Null -> returns false assertFalse(deleteFirstCommand.equals(null)); - // different person -> returns false - assertFalse(deleteFirstCommand.equals(deleteSecondCommand)); + // Different name -> returns false + assertFalse(deleteFirstCommand.equals(deleteNameCommand)); + + // Same name -> returns true + DeleteCommand deleteNameCommandCopy = new DeleteCommand("John Doe"); + assertTrue(deleteNameCommand.equals(deleteNameCommandCopy)); } @Test @@ -122,7 +167,6 @@ public void toString_nameBasedDeletion() { */ private void showNoPerson(Model model) { model.updateFilteredPersonList(p -> false); - assertTrue(model.getFilteredPersonList().isEmpty()); } }