Skip to content

Commit

Permalink
Merge pull request #32 from crystal-bys/JUnit_testing
Browse files Browse the repository at this point in the history
Add Junit tests for delete function
  • Loading branch information
Yisoong authored Oct 14, 2024
2 parents 41e9020 + 290b818 commit 67a6eb0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/seedu/planpal/utility/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ default void addToList(ArrayList<T> list, T element){
* @throws PlanPalExceptions if index is out of bounds
*/
default void deleteList(ArrayList<T> list, String index) throws PlanPalExceptions {
if (index.isEmpty()) {
throw new PlanPalExceptions("Description cannot be empty!");
}
int listIndex = Integer.parseInt(index);
if (listIndex < 1 || listIndex > list.size()) {
throw new PlanPalExceptions(
Expand Down
46 changes: 46 additions & 0 deletions src/test/java/seedu/planpal/contacts/ContactManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,52 @@ public void addContact_invalidCategory_fail() {
}
}

@Test
public void deleteContact_validFormat_success() {
ContactManager manager = new ContactManager();
try {
manager.addContact("/name:Alice");
manager.addContact("/name:Bob");
manager.addContact("/name:Charlie");
manager.addContact("/name:Johnny");

manager.deleteContact("1");
assertEquals("[Name = Bob]", manager.getContactList().get(0).toString());
assertEquals("[Name = Charlie]", manager.getContactList().get(1).toString());
assertEquals("[Name = Johnny]", manager.getContactList().get(2).toString());
} catch (PlanPalExceptions e) {
fail(e.getMessage());
}
}

@Test
public void deleteContact_invalidIndex_exceptionThrown() {
ContactManager manager = new ContactManager();
try {
manager.addContact("/name:Alice");
manager.addContact("/name:Bob");

manager.deleteContact("0");
fail();
} catch (PlanPalExceptions e) {
assertEquals("Invalid index. Please input a valid number.", e.getMessage());
}
}

@Test
public void deleteContact_emptyInput_exceptionThrown() {
ContactManager manager = new ContactManager();
try {
manager.addContact("/name:Alice");
manager.addContact("/name:Bob");

manager.deleteContact("");
fail();
} catch (PlanPalExceptions e) {
assertEquals("Description cannot be empty!", e.getMessage());
}
}

@Test
public void editContact_validIndex_success() {
ContactManager manager = new ContactManager();
Expand Down

0 comments on commit 67a6eb0

Please sign in to comment.