From f2b737e80eea6835870c9a94805624c7610517ab Mon Sep 17 00:00:00 2001 From: ryam Date: Fri, 20 Oct 2023 14:20:47 +0800 Subject: [PATCH] Add Test Cases and Covered Abstraction --- data/duke.txt | 4 +- src/main/java/duke/parser/Parser.java | 10 +- src/main/java/duke/storage/Storage.java | 3 +- src/main/java/duke/tasks/Deadline.java | 6 +- src/main/java/duke/tasks/Event.java | 10 +- src/test/java/duke/parser/ParserTest.java | 123 +++++++++++++++++++++ src/test/java/duke/tasks/DeadlineTest.java | 27 +++++ src/test/java/duke/tasks/EventTest.java | 41 +++++++ 8 files changed, 215 insertions(+), 9 deletions(-) create mode 100644 src/test/java/duke/parser/ParserTest.java create mode 100644 src/test/java/duke/tasks/DeadlineTest.java create mode 100644 src/test/java/duke/tasks/EventTest.java diff --git a/data/duke.txt b/data/duke.txt index e0c50509e7..4602423109 100644 --- a/data/duke.txt +++ b/data/duke.txt @@ -1,4 +1,6 @@ -T | 0 | Go to the gym +T | 1 | Go to the gym E | 1 | Go to Gym | Dec 12 2023 06:00 PM - Dec 12 2023 08:00 PM T | 0 | Head to school T | 1 | Say hello to Ann +T | 0 | asdsad +D | 0 | sdsdsd | Dec 10 2022 07:20 PM diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java index 078e2ae511..e0c3bd4e00 100644 --- a/src/main/java/duke/parser/Parser.java +++ b/src/main/java/duke/parser/Parser.java @@ -18,7 +18,14 @@ * Represents a parser */ public class Parser { - private static final String DATE_TIME_FORMAT = "dd/MM/yy HHmm"; + /** + * The date time format that user has to type in in order to add a deadline or event + */ + public static final String DATE_TIME_FORMAT = "dd/MM/yy HHmm"; + /** + * The date time formatter for toFile storage + */ + public static final String DATE_TIME_FORMAT_FILE_VER = "MMM d yyyy hh:mm a"; private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern(DATE_TIME_FORMAT); private static final String INVALID_COMMAND = "OOPS!!! I'm sorry, but I don't know what that means :-(\n" + "If you need help with commands, please type 'help'!"; @@ -310,4 +317,5 @@ static boolean isNumeric(String str) { } } + } diff --git a/src/main/java/duke/storage/Storage.java b/src/main/java/duke/storage/Storage.java index 09fd0f1154..22e310c205 100644 --- a/src/main/java/duke/storage/Storage.java +++ b/src/main/java/duke/storage/Storage.java @@ -9,6 +9,7 @@ import java.util.ArrayList; import java.util.Scanner; +import duke.parser.Parser; import duke.tasks.Deadline; import duke.tasks.Event; import duke.tasks.Task; @@ -133,7 +134,7 @@ public Task formatStringToTask(String line) { Task task; - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a"); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern(Parser.DATE_TIME_FORMAT_FILE_VER); switch (type) { case "T": diff --git a/src/main/java/duke/tasks/Deadline.java b/src/main/java/duke/tasks/Deadline.java index f5b38d3acb..de207b61d5 100644 --- a/src/main/java/duke/tasks/Deadline.java +++ b/src/main/java/duke/tasks/Deadline.java @@ -3,6 +3,8 @@ import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import duke.parser.Parser; + /** * Represents a deadline */ @@ -43,7 +45,7 @@ public Deadline(String description, LocalDateTime deadline, String status) { @Override public String toString() { return "[D]" + super.toString() + " (by: " - + this.deadline.format(DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a")) + ")"; + + this.deadline.format(DateTimeFormatter.ofPattern(Parser.DATE_TIME_FORMAT_FILE_VER)) + ")"; } /** @@ -53,6 +55,6 @@ public String toString() { */ public String toFile() { return "D" + super.toFile() + " | " - + this.deadline.format(DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a")); + + this.deadline.format(DateTimeFormatter.ofPattern(Parser.DATE_TIME_FORMAT_FILE_VER)); } } diff --git a/src/main/java/duke/tasks/Event.java b/src/main/java/duke/tasks/Event.java index 877565c5e9..bc81a1c1ce 100644 --- a/src/main/java/duke/tasks/Event.java +++ b/src/main/java/duke/tasks/Event.java @@ -3,6 +3,8 @@ import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import duke.parser.Parser; + /** * Represents an event */ @@ -53,8 +55,8 @@ public Event(String description, LocalDateTime from, LocalDateTime to, String st @Override public String toString() { return "[E]" + super.toString() - + " (from: " + this.from.format(DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a")) - + " to: " + this.to.format(DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a")) + ")"; + + " (from: " + this.from.format(DateTimeFormatter.ofPattern(Parser.DATE_TIME_FORMAT_FILE_VER)) + + " to: " + this.to.format(DateTimeFormatter.ofPattern(Parser.DATE_TIME_FORMAT_FILE_VER)) + ")"; } /** @@ -65,7 +67,7 @@ public String toString() { @Override public String toFile() { return "E" + super.toFile() + " | " - + this.from.format(DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a")) - + " - " + this.to.format(DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a")); + + this.from.format(DateTimeFormatter.ofPattern(Parser.DATE_TIME_FORMAT_FILE_VER)) + + " - " + this.to.format(DateTimeFormatter.ofPattern(Parser.DATE_TIME_FORMAT_FILE_VER)); } } diff --git a/src/test/java/duke/parser/ParserTest.java b/src/test/java/duke/parser/ParserTest.java new file mode 100644 index 0000000000..f7eb77f917 --- /dev/null +++ b/src/test/java/duke/parser/ParserTest.java @@ -0,0 +1,123 @@ +package duke.parser; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +import duke.commands.AddCommand; +import duke.commands.Command; +import duke.commands.ExitCommand; +import duke.commands.IncorrectCommand; +import duke.commands.ListCommand; + +public class ParserTest { + + @Test + public void list_incorrectCommand() { + Command c = Parser.parse("list xx"); + assertTrue(c instanceof IncorrectCommand); + } + + @Test + public void list_correctCommand() { + Command c = Parser.parse("list"); + assertTrue(c instanceof ListCommand); + } + + @Test + public void exit_incorrectCommand() { + Command c = Parser.parse("bye "); + assertTrue(c instanceof IncorrectCommand); + } + + @Test + void exit_correctCommand() { + Command c = Parser.parse("bye"); + assertTrue(c instanceof ExitCommand); + } + + @Test + void todo_invalidTask_incorrectCommand() { + Command c = Parser.parse("todo "); + assertTrue(c instanceof IncorrectCommand); + } + + @Test + void todo_validTask_correctCommand() { + Command c = Parser.parse("todo xxx"); + assertTrue(c instanceof AddCommand); + } + + @Test + void deadline_validTaskAndInvalidDate_correctCommand() { + Command c = Parser.parse("deadline xxx /by "); + assertTrue(c instanceof IncorrectCommand); + } + + @Test + void deadline_invalidDeadline_incorrectCommand() { + Command c = Parser.parse("deadline "); + assertTrue(c instanceof IncorrectCommand); + } + + @Test + void deadline_missingTaskAndValidDate_incorrectCommand() { + Command c = Parser.parse("deadline /by 21/06/02 1200"); + assertTrue(c instanceof IncorrectCommand); + } + + @Test + void deadline_validTaskAndInvalidDate_incorrectCommand() { + Command c = Parser.parse("deadline xx /by lorem"); + assertTrue(c instanceof IncorrectCommand); + } + + @Test + void deadline_validDeadline_correctCommand() { + Command c = Parser.parse("deadline xx /by 21/08/02 1900"); + assertTrue(c instanceof AddCommand); + } + + @Test + void event_invalidEvent_incorrectCommand() { + Command c = Parser.parse("event"); + assertTrue(c instanceof IncorrectCommand); + } + + @Test + void event_validTaskAndInvalidDate_incorrectCommand() { + Command c = Parser.parse("event xx"); + assertTrue(c instanceof IncorrectCommand); + } + + @Test + void event_validTaskAndInvalidTo_incorrectCommand() { + Command c = Parser.parse("event xx /from 21/05/02 1100"); + assertTrue(c instanceof IncorrectCommand); + } + + @Test + void event_validEvent_correctCommand() { + Command c = Parser.parse("event xx /from 21/05/02 1100 /to 21/05/02 1200"); + assertTrue(c instanceof AddCommand); + } + + @Test + void event_invalidTaskAndValidDates_incorrectCommand() { + Command c = Parser.parse("event /from 21/05/02 1100 /to 21/05/02 1200"); + assertTrue(c instanceof IncorrectCommand); + } + + @Test + void event_validTaskAndInvalidFromAndInvalidTo_incorrectCommand() { + Command c = Parser.parse("event go to the cafe /from xx /to yy"); + assertTrue(c instanceof IncorrectCommand); + } + + @Test + void event_validTaskAndInvalidRange_incorrectCommand() { + Command c = Parser.parse("event go to library /from 21/05/02 1100 /to 21/05/02 1000"); + assertTrue(c instanceof IncorrectCommand); + } + +} diff --git a/src/test/java/duke/tasks/DeadlineTest.java b/src/test/java/duke/tasks/DeadlineTest.java new file mode 100644 index 0000000000..6627edfde6 --- /dev/null +++ b/src/test/java/duke/tasks/DeadlineTest.java @@ -0,0 +1,27 @@ +package duke.tasks; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +import org.junit.jupiter.api.Test; + +public class DeadlineTest { + @Test + public void toString_correctOutput() { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yy HHmm"); + LocalDateTime by = LocalDateTime.parse("21/08/02 1900", formatter); + Task task = new Deadline("Finish HW", by); + task.completeTask(true); + assertEquals("[D][X] Finish HW (by: Aug 21 2002 07:00 PM)", task.toString()); + } + + @Test + public void toFile_correctOutput() { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yy HHmm"); + LocalDateTime by = LocalDateTime.parse("21/08/02 1900", formatter); + Task task = new Deadline("Finish HW", by, "0"); + assertEquals("D | 0 | Finish HW | Aug 21 2002 07:00 PM", task.toFile()); + } +} diff --git a/src/test/java/duke/tasks/EventTest.java b/src/test/java/duke/tasks/EventTest.java new file mode 100644 index 0000000000..933e86962c --- /dev/null +++ b/src/test/java/duke/tasks/EventTest.java @@ -0,0 +1,41 @@ +package duke.tasks; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +import org.junit.jupiter.api.Test; + +public class EventTest { + @Test + public void toString_correctOutput() { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yy HHmm"); + LocalDateTime from = LocalDateTime.parse("21/08/02 1900", formatter); + LocalDateTime to = LocalDateTime.parse("21/08/02 2000", formatter); + Task task = new Event("Group Meeting", from, to, "0"); + task.completeTask(true); + assertEquals("[E][X] Group Meeting (from: Aug 21 2002 07:00 PM to: Aug 21 2002 08:00 PM)", + task.toString()); + } + + @Test + public void toFile_correctOutput() { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yy HHmm"); + LocalDateTime from = LocalDateTime.parse("21/08/02 1900", formatter); + LocalDateTime to = LocalDateTime.parse("21/08/02 2000", formatter); + Task task = new Event("Group Meeting", from, to, "0"); + assertEquals("E | 0 | Group Meeting | Aug 21 2002 07:00 PM - Aug 21 2002 08:00 PM", task.toFile()); + } + + @Test + public void toString_wrongOutputIfFromDateIsAfterToDate() { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yy HHmm"); + LocalDateTime from = LocalDateTime.parse("21/08/02 2000", formatter); + LocalDateTime to = LocalDateTime.parse("21/08/02 1900", formatter); + Task task = new Event("Group Meeting", from, to, "0"); + task.completeTask(true); + assertEquals("[E][X] Group Meeting (from: Aug 21 2002 08:00 PM to: Aug 21 2002 07:00 PM)", + task.toString()); + } +}