From b2278c4442e122387a6f078af80f4b2d99f688d9 Mon Sep 17 00:00:00 2001 From: DesSnowy <156517235+DesSnowy@users.noreply.github.com> Date: Fri, 30 Aug 2024 19:35:23 +0800 Subject: [PATCH] Add JUnit test cases --- src/main/java/snowy/Deadline.java | 4 +- src/main/java/snowy/Event.java | 2 +- src/main/java/snowy/Snowy.java | 1 + src/main/java/snowy/SnowyException.java | 5 +- src/main/java/snowy/Storage.java | 4 +- src/main/java/snowy/Task.java | 2 +- src/main/java/snowy/TaskList.java | 26 +++--- src/test/java/snowy/DeadlineTest.java | 30 +++++++ src/test/java/snowy/EventTest.java | 29 +++++++ src/test/java/snowy/ParserTest.java | 16 ++++ src/test/java/snowy/TaskListTest.java | 101 ++++++++++++++++++++++++ src/test/java/snowy/ToDoTest.java | 16 ++++ 12 files changed, 216 insertions(+), 20 deletions(-) create mode 100644 src/test/java/snowy/DeadlineTest.java create mode 100644 src/test/java/snowy/EventTest.java create mode 100644 src/test/java/snowy/ParserTest.java create mode 100644 src/test/java/snowy/TaskListTest.java create mode 100644 src/test/java/snowy/ToDoTest.java diff --git a/src/main/java/snowy/Deadline.java b/src/main/java/snowy/Deadline.java index 5fafb17fe1..1e3da1ac5a 100644 --- a/src/main/java/snowy/Deadline.java +++ b/src/main/java/snowy/Deadline.java @@ -12,14 +12,14 @@ public Deadline(String name, String date) throws SnowyException { try { this.date = LocalDate.parse(date); } catch (DateTimeException e) { - throw new SnowyException(); + throw new SnowyException("Wrong date format"); } } @Override public String toString() { String temp = super.toString(); - return String.format("[D]%s (by %s )", temp, date.format(super.FORMATTER)); + return String.format("[D]%s (by %s)", temp, date.format(super.FORMATTER)); } @Override diff --git a/src/main/java/snowy/Event.java b/src/main/java/snowy/Event.java index 0c531c7a01..9a7d7e20f5 100644 --- a/src/main/java/snowy/Event.java +++ b/src/main/java/snowy/Event.java @@ -13,7 +13,7 @@ public Event(String name, String fromDate, String toDate) throws SnowyException this.fromDate = LocalDate.parse(fromDate); this.toDate = LocalDate.parse(toDate); } catch (DateTimeException e) { - throw new SnowyException(); + throw new SnowyException("Wrong date format"); } } diff --git a/src/main/java/snowy/Snowy.java b/src/main/java/snowy/Snowy.java index e726acacc5..e58c21bfc0 100644 --- a/src/main/java/snowy/Snowy.java +++ b/src/main/java/snowy/Snowy.java @@ -19,6 +19,7 @@ private Snowy(String filePath) { this.tasks = new TaskList(storage.load()); } catch (SnowyException e) { this.tasks = new TaskList(); + System.out.println(e.getMessage()); } } diff --git a/src/main/java/snowy/SnowyException.java b/src/main/java/snowy/SnowyException.java index ef7a1d729f..d00131797a 100644 --- a/src/main/java/snowy/SnowyException.java +++ b/src/main/java/snowy/SnowyException.java @@ -1,4 +1,7 @@ package snowy; -public class SnowyException extends Exception { +public class SnowyException extends RuntimeException { + public SnowyException(String message) { + super(message); + } } diff --git a/src/main/java/snowy/Storage.java b/src/main/java/snowy/Storage.java index d5926ed74e..bfcfbe0319 100644 --- a/src/main/java/snowy/Storage.java +++ b/src/main/java/snowy/Storage.java @@ -25,7 +25,7 @@ public ArrayList load() throws SnowyException{ } catch (IOException e) { - throw new SnowyException(); + throw new SnowyException("Unable to create new file"); } return lines; } @@ -36,7 +36,7 @@ public void save(String saveLines) throws SnowyException{ writer.write(saveLines); writer.close(); } catch (IOException e) { - throw new SnowyException(); + throw new SnowyException("Unable to update file"); } } diff --git a/src/main/java/snowy/Task.java b/src/main/java/snowy/Task.java index 1948b04414..a252bedab7 100644 --- a/src/main/java/snowy/Task.java +++ b/src/main/java/snowy/Task.java @@ -3,7 +3,7 @@ import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; -public class Task { +abstract public class Task { private boolean isCompleted = false; public final DateTimeFormatter FORMATTER = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG); diff --git a/src/main/java/snowy/TaskList.java b/src/main/java/snowy/TaskList.java index 7344201182..4ca052e58c 100644 --- a/src/main/java/snowy/TaskList.java +++ b/src/main/java/snowy/TaskList.java @@ -73,7 +73,7 @@ private void initializeTask(String description) { public void addToDo(String description) throws SnowyException{ if (description.isEmpty()) { - throw new SnowyException(); + throw new SnowyException("Invalid input for Todo"); } Task newTask = new ToDo(description); tasks.add(newTask); @@ -82,23 +82,23 @@ public void addToDo(String description) throws SnowyException{ public void addDeadline(String description) throws SnowyException{ if (description.isEmpty()) { - throw new SnowyException(); + throw new SnowyException("Invalid input for Deadline"); } int byIndex = description.indexOf("/by "); if (byIndex == -1) { - throw new SnowyException(); + throw new SnowyException("Invalid input for Deadline"); } String deadlineName = description.substring(0, byIndex).trim(); String date = description.substring(byIndex + 4); if (deadlineName.isEmpty()) { - throw new SnowyException(); + throw new SnowyException("Invalid input for Deadline"); } if (date.isEmpty()) { - throw new SnowyException(); + throw new SnowyException("Invalid input for Deadline"); } Task newTask = new Deadline(deadlineName, date); tasks.add(newTask); @@ -107,26 +107,26 @@ public void addDeadline(String description) throws SnowyException{ public void addEvent(String description) throws SnowyException{ if (description.isEmpty()) { - throw new SnowyException(); + throw new SnowyException("Invalid input for Event"); } int fromIndex = description.indexOf("/from "); int toIndex = description.indexOf("/to "); if (toIndex == -1 || fromIndex == -1) { - throw new SnowyException(); + throw new SnowyException("Invalid input for Event"); } String eventName = description.substring(0, fromIndex); String fromDate = description.substring(fromIndex + 6, toIndex).trim(); String toDate = description.substring(toIndex + 4); if (eventName.isEmpty()) { - throw new SnowyException(); + throw new SnowyException("Invalid input for Event"); } if (fromDate.isEmpty()) { - throw new SnowyException(); + throw new SnowyException("Invalid input for Event"); } if (toDate.isEmpty()) { - throw new SnowyException(); + throw new SnowyException("Invalid input for Event"); } Task newTask = new Event(eventName, fromDate, toDate); tasks.add(newTask); @@ -137,7 +137,7 @@ public Task deleteTask(int index) throws SnowyException { try { return tasks.remove(index - 1); } catch (IndexOutOfBoundsException e) { - throw new SnowyException(); + throw new SnowyException("Invalid index input"); } } @@ -146,7 +146,7 @@ public Task markTask(int index) throws SnowyException { tasks.get(index - 1).markComplete(); return tasks.get(index - 1); } catch (IndexOutOfBoundsException e) { - throw new SnowyException(); + throw new SnowyException("Invalid index input"); } } @@ -155,7 +155,7 @@ public Task unmarkTask(int index) throws SnowyException { tasks.get(index - 1).markIncomplete(); return tasks.get(index - 1); } catch (IndexOutOfBoundsException e) { - throw new SnowyException(); + throw new SnowyException("Invalid index input"); } } diff --git a/src/test/java/snowy/DeadlineTest.java b/src/test/java/snowy/DeadlineTest.java new file mode 100644 index 0000000000..21a36857ff --- /dev/null +++ b/src/test/java/snowy/DeadlineTest.java @@ -0,0 +1,30 @@ +package snowy; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; + +public class DeadlineTest { + + @Test + public void toString_normalName_success() { + assertEquals("[D][ ] Return Book (by September 11, 2024)", + new Deadline("Return Book", "2024-09-11").toString()); + } + + @Test + public void toFileStorage_normalName_success() { + assertEquals("D|0|Return Book|2024-09-11", + new Deadline("Return Book", "2024-09-11").toFileStorage()); + } + + @Test void constructor_invalidDate_exceptionThrown() { + try { + new Deadline("Return book", "12-123-1234"); + fail(); + } catch (SnowyException e) { + assertEquals("Wrong date format", e.getMessage()); + } + } +} diff --git a/src/test/java/snowy/EventTest.java b/src/test/java/snowy/EventTest.java new file mode 100644 index 0000000000..35940893e0 --- /dev/null +++ b/src/test/java/snowy/EventTest.java @@ -0,0 +1,29 @@ +package snowy; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; + +public class EventTest { + @Test + public void toString_normalName_success() { + assertEquals("[E][ ] Orientation Camp (from September 11, 2024 to: September 12, 2024)", + new Event("Orientation Camp", "2024-09-11", "2024-09-12").toString()); + } + + @Test + public void toFileStorage_normalName_success() { + assertEquals("[E][ ] Orientation Camp (from September 11, 2024 to: September 12, 2024)", + new Event("Orientation Camp", "2024-09-11", "2024-09-12").toString()); + } + + @Test void constructor_invalidDate_exceptionThrown() { + try { + new Event("Return book", "12-123-1234", "12-123-1234"); + fail(); + } catch (SnowyException e) { + assertEquals("Wrong date format", e.getMessage()); + } + } +} diff --git a/src/test/java/snowy/ParserTest.java b/src/test/java/snowy/ParserTest.java new file mode 100644 index 0000000000..41734da3ed --- /dev/null +++ b/src/test/java/snowy/ParserTest.java @@ -0,0 +1,16 @@ +package snowy; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +public class ParserTest { + @Test + public void parse_allCapitalCommand_returnLowerCase() { + assertArrayEquals(new String[] {"todo", "read book"}, Parser.parse("TODO read book")); + } + + public void parse_capitalDescription_retainCapital() { + assertArrayEquals(new String[] {"todo", "Read Book"}, Parser.parse("todo Read Book")); + } +} diff --git a/src/test/java/snowy/TaskListTest.java b/src/test/java/snowy/TaskListTest.java new file mode 100644 index 0000000000..b6023e009d --- /dev/null +++ b/src/test/java/snowy/TaskListTest.java @@ -0,0 +1,101 @@ +package snowy; + +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; + +public class TaskListTest { + + @Test + public void toString_correctInput_success() { + ArrayList lines = new ArrayList<>(); + lines.add("T|1|read book"); + lines.add("D|0|return book|2024-09-05"); + lines.add("E|0|Camp|2024-09-01|2024-09-02"); + TaskList tasks = new TaskList(lines); + + assertEquals("1. [T][X] read book\n" + + "2. [D][ ] return book (by September 5, 2024)\n" + + "3. [E][ ] Camp (from September 1, 2024 to: September 2, 2024)\n" + , tasks.toString()); + } + + @Test + public void toSaveString_correctInput_success() { + ArrayList lines = new ArrayList<>(); + lines.add("T|1|read book"); + lines.add("D|0|return book|2024-09-05"); + lines.add("E|0|Camp|2024-09-01|2024-09-02"); + TaskList tasks = new TaskList(lines); + + assertEquals("T|1|read book\n" + + "D|0|return book|2024-09-05\n" + + "E|0|Camp|2024-09-01|2024-09-02\n" + , tasks.toSaveString()); + } + + @Test + public void addToDo_correctInput_success() { + TaskList tasks = new TaskList(); + tasks.addToDo("Read Book"); + + assertEquals("1. [T][ ] Read Book\n", tasks.toString()); + } + + @Test + public void addToDo_invalidInput_exceptionThrown() { + try { + TaskList tasks = new TaskList(); + tasks.addToDo(""); + fail(); + } catch (SnowyException e) { + assertEquals("Invalid input for Todo", e.getMessage()); + } + } + + @Test + public void addDeadline_correctInput_success() { + TaskList tasks = new TaskList(); + tasks.addDeadline("Return Book /by 2024-09-05"); + + assertEquals("1. [D][ ] Return Book (by September 5, 2024)\n", tasks.toString()); + } + + @Test + public void addDeadline_emptyInput_exceptionThrown() { + try { + TaskList tasks = new TaskList(); + tasks.addDeadline(""); + fail(); + } catch (SnowyException e) { + assertEquals("Invalid input for Deadline", e.getMessage()); + } + } + + @Test + public void addDeadline_missingBy_exceptionThrown() { + try { + TaskList tasks = new TaskList(); + tasks.addDeadline("Return book 2024-09-05"); + fail(); + } catch (SnowyException e) { + assertEquals("Invalid input for Deadline", e.getMessage()); + } + } + + @Test + public void addDeadline_InvalidDate_exceptionThrown() { + try { + TaskList tasks = new TaskList(); + tasks.addDeadline("Return book /by 123-09-05"); + fail(); + } catch (SnowyException e) { + assertEquals("Wrong date format", e.getMessage()); + } + } + + +} diff --git a/src/test/java/snowy/ToDoTest.java b/src/test/java/snowy/ToDoTest.java new file mode 100644 index 0000000000..e82b723075 --- /dev/null +++ b/src/test/java/snowy/ToDoTest.java @@ -0,0 +1,16 @@ +package snowy; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +public class ToDoTest { + @Test + public void toString_normalName_success() { + assertEquals("[T][ ] Read Book",new ToDo("Read Book").toString()); + } + + @Test + public void toFileStorage_normalName_success() { + assertEquals("T|0|Read Book",new ToDo("Read Book").toFileStorage()); + } +}