From d0b602638dcf343383a7ef035e6ea8e0a60ea48f Mon Sep 17 00:00:00 2001 From: DS Date: Tue, 19 Sep 2023 16:09:18 +0800 Subject: [PATCH] Add tests for Commands --- src/main/java/joe/Parser.java | 2 +- .../java/joe/commands/DeadlineCommand.java | 2 +- src/main/java/joe/commands/DeleteCommand.java | 2 +- src/main/java/joe/commands/EventCommand.java | 2 +- src/main/java/joe/commands/MarkCommand.java | 2 +- src/main/java/joe/commands/TodoCommand.java | 2 +- src/main/java/joe/commands/UnmarkCommand.java | 2 +- src/test/java/joe/CommandTest.java | 215 ++++++++++++++++++ src/test/java/joe/TaskListTest.java | 19 +- src/test/java/joe/stubs/StorageStub.java | 17 ++ src/test/java/joe/stubs/TaskListStub.java | 39 ++++ src/test/java/joe/stubs/TaskStub.java | 32 +++ 12 files changed, 311 insertions(+), 25 deletions(-) create mode 100644 src/test/java/joe/CommandTest.java create mode 100644 src/test/java/joe/stubs/StorageStub.java create mode 100644 src/test/java/joe/stubs/TaskListStub.java create mode 100644 src/test/java/joe/stubs/TaskStub.java diff --git a/src/main/java/joe/Parser.java b/src/main/java/joe/Parser.java index 5bd176055c..c6829aa7bd 100644 --- a/src/main/java/joe/Parser.java +++ b/src/main/java/joe/Parser.java @@ -225,7 +225,7 @@ private static Command handleInvalidKeyword() { sb.append(", "); } sb.setLength(sb.length() - 2); //Removes extra ", " at the end - String msg = String.format("Invalid Command Keyword!%nHere is a list of valid commands: %s", sb); + String msg = String.format("Invalid Command Keyword!\nHere is a list of valid commands: %s", sb); return new InvalidCommand(msg); } diff --git a/src/main/java/joe/commands/DeadlineCommand.java b/src/main/java/joe/commands/DeadlineCommand.java index 9569452c14..e57147f126 100644 --- a/src/main/java/joe/commands/DeadlineCommand.java +++ b/src/main/java/joe/commands/DeadlineCommand.java @@ -44,7 +44,7 @@ public String execute(TaskList tasks, Storage storage) { storage.saveToFile(tasks); return ( String.format( - "Got it, I've added this task:%n %s%nNow you have %d tasks in the list.", + "Got it, I've added this task:\n %s\nNow you have %d tasks in the list.", newTask, tasks.size())); } } diff --git a/src/main/java/joe/commands/DeleteCommand.java b/src/main/java/joe/commands/DeleteCommand.java index ab35e50ea9..776ea7f8f1 100644 --- a/src/main/java/joe/commands/DeleteCommand.java +++ b/src/main/java/joe/commands/DeleteCommand.java @@ -46,7 +46,7 @@ public String execute(TaskList tasks, Storage storage) throws JoeIndexOutOfBound return ( String.format( - "Noted. I've removed this task:%n %s%nNow you have %d tasks in the list.", + "Noted. I've removed this task:\n %s\nNow you have %d tasks in the list.", deletedTask, tasks.size())); } } diff --git a/src/main/java/joe/commands/EventCommand.java b/src/main/java/joe/commands/EventCommand.java index 3d25111963..9a19e24950 100644 --- a/src/main/java/joe/commands/EventCommand.java +++ b/src/main/java/joe/commands/EventCommand.java @@ -57,7 +57,7 @@ public String execute(TaskList tasks, Storage storage) throws JoeException { return ( String.format( - "Got it, I've added this task:%n %s%nNow you have %d tasks in the list.", + "Got it, I've added this task:\n %s\nNow you have %d tasks in the list.", newTask, tasks.size())); } } diff --git a/src/main/java/joe/commands/MarkCommand.java b/src/main/java/joe/commands/MarkCommand.java index ad11226ed1..879d9af67f 100644 --- a/src/main/java/joe/commands/MarkCommand.java +++ b/src/main/java/joe/commands/MarkCommand.java @@ -36,6 +36,6 @@ public String execute(TaskList tasks, Storage storage) throws JoeIndexOutOfBound storage.saveToFile(tasks); - return (String.format("Nice! I've marked this task as done:%n %s", tasks.get(idx - 1))); + return (String.format("Nice! I've marked this task as done:\n %s", tasks.get(idx - 1))); } } diff --git a/src/main/java/joe/commands/TodoCommand.java b/src/main/java/joe/commands/TodoCommand.java index e3efc6ee8b..dcb29c5594 100644 --- a/src/main/java/joe/commands/TodoCommand.java +++ b/src/main/java/joe/commands/TodoCommand.java @@ -40,7 +40,7 @@ public String execute(TaskList tasks, Storage storage) { return ( String.format( - "Got it, I've added this task:%n %s%nNow you have %d tasks in the list.", + "Got it, I've added this task:\n %s\nNow you have %d tasks in the list.", newTask, tasks.size())); } } diff --git a/src/main/java/joe/commands/UnmarkCommand.java b/src/main/java/joe/commands/UnmarkCommand.java index 9ba8331e95..21642b1646 100644 --- a/src/main/java/joe/commands/UnmarkCommand.java +++ b/src/main/java/joe/commands/UnmarkCommand.java @@ -36,6 +36,6 @@ public String execute(TaskList tasks, Storage storage) throws JoeIndexOutOfBound storage.saveToFile(tasks); - return (String.format("OK! I've marked this task as not done:%n %s", tasks.get(idx - 1))); + return (String.format("OK! I've marked this task as not done:\n %s", tasks.get(idx - 1))); } } diff --git a/src/test/java/joe/CommandTest.java b/src/test/java/joe/CommandTest.java new file mode 100644 index 0000000000..98f534586d --- /dev/null +++ b/src/test/java/joe/CommandTest.java @@ -0,0 +1,215 @@ +package joe; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.fail; + +import java.time.LocalDateTime; + +import org.junit.jupiter.api.Test; + +import joe.commands.DeadlineCommand; +import joe.commands.DeleteCommand; +import joe.commands.EventCommand; +import joe.commands.ListCommand; +import joe.commands.MarkCommand; +import joe.commands.TodoCommand; +import joe.commands.UnmarkCommand; +import joe.exceptions.JoeException; +import joe.stubs.StorageStub; +import joe.stubs.TaskListStub; + +public class CommandTest { + @Test + public void todo_expectedInput_success() { + TaskListStub taskListStub = new TaskListStub(0); + StorageStub storageStub = new StorageStub(); + LocalDateTime dateTime = LocalDateTime.of(2023, 1, 1, 12, 0); + + TodoCommand command = new TodoCommand("My Task"); + + String result = command.execute(taskListStub, storageStub); + + assertEquals("Got it, I've added this task:\n" + + " [T][ ] My Task\n" + + "Now you have 1 tasks in the list.", result); + assertEquals(1, taskListStub.size()); + } + + @Test + public void deadline_expectedInput_success() { + TaskListStub taskListStub = new TaskListStub(0); + StorageStub storageStub = new StorageStub(); + LocalDateTime dateTime = LocalDateTime.of(2023, 1, 1, 12, 0); + + DeadlineCommand command = new DeadlineCommand("My Task", dateTime); + + String result = command.execute(taskListStub, storageStub); + + assertEquals("Got it, I've added this task:\n" + + " [D][ ] My Task (by: 01 Jan 2023 12:00)\n" + + "Now you have 1 tasks in the list.", result); + assertEquals(1, taskListStub.size()); + } + + @Test + public void event_expectedInput_success() { + TaskListStub taskListStub = new TaskListStub(0); + StorageStub storageStub = new StorageStub(); + LocalDateTime fromDateTime = LocalDateTime.of(2023, 1, 1, 12, 0); + LocalDateTime toDateTime = LocalDateTime.of(2023, 1, 2, 12, 0); + + EventCommand command = new EventCommand("My Task", fromDateTime, toDateTime); + + try { + String result = command.execute(taskListStub, storageStub); + assertEquals("Got it, I've added this task:\n" + + " [E][ ] My Task (from: 01 Jan 2023 12:00 to: 02 Jan 2023 12:00)\n" + + "Now you have 1 tasks in the list.", result); + assertEquals(1, taskListStub.size()); + } catch (JoeException e) { + fail(); + } + } + + @Test + public void event_badDates_success() { + TaskListStub taskListStub = new TaskListStub(0); + StorageStub storageStub = new StorageStub(); + LocalDateTime fromDateTime = LocalDateTime.of(2023, 1, 2, 12, 0); + LocalDateTime toDateTime = LocalDateTime.of(2023, 1, 1, 12, 0); + + EventCommand command = new EventCommand("My Task", fromDateTime, toDateTime); + assertThrows(JoeException.class, () -> command.execute(taskListStub, storageStub)); + } + + @Test + public void event_sameDateTime_success() { + TaskListStub taskListStub = new TaskListStub(0); + StorageStub storageStub = new StorageStub(); + LocalDateTime dateTime = LocalDateTime.of(2023, 1, 1, 12, 0); + + EventCommand command = new EventCommand("My Task", dateTime, dateTime); + + try { + String result = command.execute(taskListStub, storageStub); + assertEquals("Got it, I've added this task:\n" + + " [E][ ] My Task (from: 01 Jan 2023 12:00 to: 01 Jan 2023 12:00)\n" + + "Now you have 1 tasks in the list.", result); + assertEquals(1, taskListStub.size()); + } catch (JoeException e) { + fail(); + } + } + + @Test + public void list_expectedInput_success() { + TaskListStub taskListStub = new TaskListStub(0); + StorageStub storageStub = new StorageStub(); + + ListCommand command = new ListCommand(); + + String result = command.execute(taskListStub, storageStub); + + assertEquals("Here are your tasks:\n" + + "TaskListStub", result); + + } + + @Test + public void delete_expectedInput_success() { + TaskListStub taskListStub = new TaskListStub(1); + StorageStub storageStub = new StorageStub(); + + DeleteCommand command = new DeleteCommand(1); + + try { + String result = command.execute(taskListStub, storageStub); + assertEquals("Noted. I've removed this task:\n" + + " toString\n" + + "Now you have 0 tasks in the list.", result); + assertEquals(0, taskListStub.size()); + } catch (JoeException e) { + fail(); + } + } + + @Test + public void delete_badIndex_success() { + TaskListStub taskListStub = new TaskListStub(1); + StorageStub storageStub = new StorageStub(); + + DeleteCommand commandTwo = new DeleteCommand(2); + assertThrows(JoeException.class, () -> commandTwo.execute(taskListStub, storageStub)); + + DeleteCommand commandZero = new DeleteCommand(0); + assertThrows(JoeException.class, () -> commandZero.execute(taskListStub, storageStub)); + + DeleteCommand commandNegative = new DeleteCommand(-1); + assertThrows(JoeException.class, () -> commandNegative.execute(taskListStub, storageStub)); + } + + @Test + public void mark_expectedInput_success() { + TaskListStub taskListStub = new TaskListStub(1); + StorageStub storageStub = new StorageStub(); + + MarkCommand command = new MarkCommand(1); + + try { + String result = command.execute(taskListStub, storageStub); + assertEquals("Nice! I've marked this task as done:\n" + + " toString", result); + assertEquals(1, taskListStub.size()); + } catch (JoeException e) { + fail(); + } + } + + @Test + public void mark_badIndex_success() { + TaskListStub taskListStub = new TaskListStub(1); + StorageStub storageStub = new StorageStub(); + + MarkCommand commandTwo = new MarkCommand(2); + assertThrows(JoeException.class, () -> commandTwo.execute(taskListStub, storageStub)); + + MarkCommand commandZero = new MarkCommand(0); + assertThrows(JoeException.class, () -> commandZero.execute(taskListStub, storageStub)); + + MarkCommand commandNegative = new MarkCommand(-1); + assertThrows(JoeException.class, () -> commandNegative.execute(taskListStub, storageStub)); + } + + @Test + public void unmark_expectedInput_success() { + TaskListStub taskListStub = new TaskListStub(1); + StorageStub storageStub = new StorageStub(); + + UnmarkCommand command = new UnmarkCommand(1); + + try { + String result = command.execute(taskListStub, storageStub); + assertEquals("OK! I've marked this task as not done:\n" + + " toString", result); + assertEquals(1, taskListStub.size()); + } catch (JoeException e) { + fail(); + } + } + + @Test + public void unmark_badIndex_success() { + TaskListStub taskListStub = new TaskListStub(1); + StorageStub storageStub = new StorageStub(); + + UnmarkCommand commandTwo = new UnmarkCommand(2); + assertThrows(JoeException.class, () -> commandTwo.execute(taskListStub, storageStub)); + + UnmarkCommand commandZero = new UnmarkCommand(0); + assertThrows(JoeException.class, () -> commandZero.execute(taskListStub, storageStub)); + + UnmarkCommand commandNegative = new UnmarkCommand(-1); + assertThrows(JoeException.class, () -> commandNegative.execute(taskListStub, storageStub)); + } +} diff --git a/src/test/java/joe/TaskListTest.java b/src/test/java/joe/TaskListTest.java index e4311b29be..86810d75da 100644 --- a/src/test/java/joe/TaskListTest.java +++ b/src/test/java/joe/TaskListTest.java @@ -6,27 +6,10 @@ import org.junit.jupiter.api.Test; +import joe.stubs.TaskStub; import joe.tasks.Task; - public class TaskListTest { - - private static class TaskStub extends Task { - protected TaskStub() { - super(""); - } - - @Override - public String toString() { - return "toString"; - } - - @Override - public String getDescription() { - return "Description"; - } - } - @Test public void add_expectedUsage_success() { TaskList tasks = new TaskList(); diff --git a/src/test/java/joe/stubs/StorageStub.java b/src/test/java/joe/stubs/StorageStub.java new file mode 100644 index 0000000000..7eb1a17c03 --- /dev/null +++ b/src/test/java/joe/stubs/StorageStub.java @@ -0,0 +1,17 @@ +package joe.stubs; + +import joe.Storage; +import joe.TaskList; +/** + * A stub implementation of Storage for testing. + */ +public class StorageStub extends Storage { + public StorageStub() { + super("test.txt"); + } + + @Override + public void saveToFile(TaskList tasks) { + // Do nothing for the stub + } +} diff --git a/src/test/java/joe/stubs/TaskListStub.java b/src/test/java/joe/stubs/TaskListStub.java new file mode 100644 index 0000000000..eb8b566922 --- /dev/null +++ b/src/test/java/joe/stubs/TaskListStub.java @@ -0,0 +1,39 @@ +package joe.stubs; + +import joe.TaskList; +import joe.tasks.Task; +/** + * A stub implementation of TaskList for testing. + */ +public class TaskListStub extends TaskList { + private int size; + + public TaskListStub(int size) { + this.size = size; + } + + @Override + public void add(Task task) { + this.size++; + } + + @Override + public void remove(int idx) { + this.size--; + } + + @Override + public int size() { + return this.size; + } + + @Override + public String toString() { + return "TaskListStub"; + } + + @Override + public Task get(int idx) { + return new TaskStub(); + } +} diff --git a/src/test/java/joe/stubs/TaskStub.java b/src/test/java/joe/stubs/TaskStub.java new file mode 100644 index 0000000000..e56f528ccd --- /dev/null +++ b/src/test/java/joe/stubs/TaskStub.java @@ -0,0 +1,32 @@ +package joe.stubs; + +import joe.tasks.Task; +/** + * A stub implementation of Task for testing. + */ +public class TaskStub extends Task { + public TaskStub() { + super(""); + } + + @Override + public String toString() { + return "toString"; + } + + @Override + public String getDescription() { + return "Description"; + } + + @Override + public void markAsDone() { + //Do Nothing + } + + @Override + public void markAsNotDone() { + //Do Nothing + } + +}