From 07b3b23abd3f5f0a4d8e30b0fb8e716ccb3101b1 Mon Sep 17 00:00:00 2001 From: Ritika Joshi Date: Fri, 27 Jan 2023 16:18:23 +0800 Subject: [PATCH] Add JUnit tests for DeadlineCommand and EventCommand classes --- .../java/command/DeadlineCommandTest.java | 29 ++++++-- src/test/java/command/EventCommandTest.java | 74 +++++++++++++++++++ 2 files changed, 95 insertions(+), 8 deletions(-) create mode 100644 src/test/java/command/EventCommandTest.java diff --git a/src/test/java/command/DeadlineCommandTest.java b/src/test/java/command/DeadlineCommandTest.java index b0cc0eb25e..e8eb5567e2 100644 --- a/src/test/java/command/DeadlineCommandTest.java +++ b/src/test/java/command/DeadlineCommandTest.java @@ -2,6 +2,7 @@ import org.junit.jupiter.api.Test; import task.TaskManager; +import util.DukeException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.fail; @@ -11,29 +12,41 @@ public class DeadlineCommandTest { //creates new task array TaskManager tm = new TaskManager(); @Test - public void executeCommand_checkAddToList_success(){ - DeadlineCommand dc = new DeadlineCommand(tm, "test program /by 25/12/23 11:50PM"); + public void executeCommand_checkAddToList_success() throws DukeException { + DeadlineCommand dc = new DeadlineCommand(tm, "test program /by 25/12/23 1150"); dc.executeCommand(); assertEquals(1, tm.getTaskArraySize()); + } @Test - public void executeCommand_checkTaskAddedToList_success(){ - DeadlineCommand dc = new DeadlineCommand(tm, "test program /by 25/12/23 11:50PM"); + public void executeCommand_checkTaskAddedToList_success() throws DukeException { + DeadlineCommand dc = new DeadlineCommand(tm, "test program /by 25/12/23 1150"); dc.executeCommand(); - assertEquals("[D][ ] test program (by: 25/12/23 11:50PM)", tm.printTask(0)); + assertEquals("[D][ ] test program (by: 25 Dec 2023 11:50 AM)", tm.printTask(0)); } @Test - public void executeCommand_invalidUserInput_exceptionThrown(){ + public void executeCommand_invalidDateTime_exceptionThrown() { try { DeadlineCommand dc = new DeadlineCommand(tm, "test /by 25/12/23 11:50PM"); dc.executeCommand(); - assertEquals("[D][ ] test program (by: 25/12/23 11:50PM)", tm.printTask(0)); + assertEquals("[D][ ] test program (by: 25/12/23 11:50 AM)", tm.printTask(0)); fail(); } catch (Exception e) { - //assertEquals("no"); + assertEquals("Please enter date in dd/mm/yy and time in hhmm 24hr format!", e.getMessage()); } + } + @Test + public void executeCommand_invalidUserInput_exceptionThrown() { + try { + DeadlineCommand dc = new DeadlineCommand(tm, "test /by"); + dc.executeCommand(); + assertEquals("[D][ ] test program (by: 25/12/23 11:50 AM)", tm.printTask(0)); + fail(); + } catch (Exception e) { + assertEquals("Please add a description, date and time e.g. homework /by 12/12/12 2359", e.getMessage()); + } } } diff --git a/src/test/java/command/EventCommandTest.java b/src/test/java/command/EventCommandTest.java new file mode 100644 index 0000000000..a286e34485 --- /dev/null +++ b/src/test/java/command/EventCommandTest.java @@ -0,0 +1,74 @@ +package command; + +import org.junit.jupiter.api.Test; +import task.TaskManager; +import util.DukeException; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; +public class EventCommandTest { + TaskManager tm = new TaskManager(); + @Test + public void executeCommand_checkAddToList_success() throws DukeException { + EventCommand ec = new EventCommand(tm, "party /from 12/2/23 0600 /to 12/2/23 1000"); + ec.executeCommand(); + assertEquals(1, tm.getTaskArraySize()); + + } + + @Test + public void executeCommand_checkTaskAddedToList_success() throws DukeException { + EventCommand ec = new EventCommand(tm, "party /from 12/2/23 0600 /to 12/2/23 1000"); + ec.executeCommand(); + assertEquals("[E][ ] party (Start: 12 Feb 2023 06:00 AM | End: 12 Feb 2023 10:00 AM)", tm.printTask(0)); + } + + @Test + public void executeCommand_invalidDateTime_exceptionThrown() { + try { + EventCommand ec = new EventCommand(tm, "party /from 12/2/23 0600 /to 12/2/2 1000"); + ec.executeCommand(); + assertEquals("[E][ ] party (Start: 12 Feb 2023 06:00 AM | End: 12 Feb 2023 10:00 AM)", + tm.printTask(0)); + fail(); + } catch (Exception e) { + assertEquals("Please enter date in dd/mm/yy and time in hhmm 24hr format!", e.getMessage()); + } + } + + @Test + public void executeCommand_invalidUserInputV1_exceptionThrown() { + try { + EventCommand ec = new EventCommand(tm, "party /from 12/2/23 0600"); + ec.executeCommand(); + fail(); + } catch (Exception e) { + assertEquals("Please add a description, date and time e.g. party /from 12/2/23 1800 /to 12/2/23 2200", + e.getMessage()); + } + } + + @Test + public void executeCommand_invalidUserInputV2_exceptionThrown() { + try { + EventCommand ec = new EventCommand(tm, "party /to 12/2/2"); + ec.executeCommand(); + fail(); + } catch (Exception e) { + assertEquals("Please add a description, date and time e.g. party /from 12/2/23 1800 /to 12/2/23 2200", + e.getMessage()); + } + } + + @Test + public void executeCommand_invalidUserInputV3_exceptionThrown() { + try { + EventCommand ec = new EventCommand(tm, "party"); + ec.executeCommand(); + fail(); + } catch (Exception e) { + assertEquals("Please add a description, date and time e.g. party /from 12/2/23 1800 /to 12/2/23 2200", + e.getMessage()); + } + } +}