From 3b79a446c83d63924c98df43d12be7bb565a57e0 Mon Sep 17 00:00:00 2001 From: Sampy147 Date: Fri, 16 Sep 2022 20:57:45 +0800 Subject: [PATCH] Add assertion statements Assertion statements are able to complement exception handling in handling errors. It is especially useful in indicating mistakes in the code returned Let's, * Write some assertion statements across multiple files to verify the correctness of our code --- data/duke.txt | 1 + src/main/java/duke/command/AddCommand.java | 1 + src/main/java/duke/command/DeleteCommand.java | 3 +++ src/main/java/duke/command/FindCommand.java | 1 + src/main/java/duke/command/MarkCommand.java | 3 +++ src/main/java/duke/command/UnmarkCommand.java | 3 +++ src/main/java/duke/parser/Parser.java | 11 +++++++++-- src/main/java/duke/task/TaskList.java | 6 ++++++ 8 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 data/duke.txt diff --git a/data/duke.txt b/data/duke.txt new file mode 100644 index 0000000000..7dcd6ff40e --- /dev/null +++ b/data/duke.txt @@ -0,0 +1 @@ +T | 0 | ask diff --git a/src/main/java/duke/command/AddCommand.java b/src/main/java/duke/command/AddCommand.java index 2952c219a1..d9c0ce6d83 100644 --- a/src/main/java/duke/command/AddCommand.java +++ b/src/main/java/duke/command/AddCommand.java @@ -19,6 +19,7 @@ public class AddCommand extends Command { * @param task task to be added */ public AddCommand(Task task) { + assert task != null : "task cannot be null"; this.task = task; } diff --git a/src/main/java/duke/command/DeleteCommand.java b/src/main/java/duke/command/DeleteCommand.java index 492c142869..cd2bf5f9e7 100644 --- a/src/main/java/duke/command/DeleteCommand.java +++ b/src/main/java/duke/command/DeleteCommand.java @@ -45,12 +45,15 @@ public boolean isExit() { public String execute(TaskList tasks, Ui ui, Storage storage) throws DukeException { try { Task deletedTask = tasks.deleteTaskAtPos(this.index); + assert this.index <= tasks.getCount() + 1 && this.index > 0 : + "Position argument should be more than 0 and less than or equal to the task list size"; storage.save(tasks); return ui.showDeleted(deletedTask); } catch (IndexOutOfBoundsException e) { if (tasks.getCount() == 0){ throw new DukeException(Message.INVALID_ACCESS_EMPTY_TASKLIST); } else { + assert tasks.getCount() > 0 : "task list should have 1 or more tasks"; throw new DukeException(Message.returnTaskNotFound(tasks)); } } diff --git a/src/main/java/duke/command/FindCommand.java b/src/main/java/duke/command/FindCommand.java index 342e23ff1f..e28f3533a2 100644 --- a/src/main/java/duke/command/FindCommand.java +++ b/src/main/java/duke/command/FindCommand.java @@ -10,6 +10,7 @@ public class FindCommand extends Command { private String keyword; public FindCommand(String keyword) { + assert keyword != null : "keyword cannot be null"; this.keyword = keyword; } diff --git a/src/main/java/duke/command/MarkCommand.java b/src/main/java/duke/command/MarkCommand.java index 86e5753ff6..fa5efd1ef3 100644 --- a/src/main/java/duke/command/MarkCommand.java +++ b/src/main/java/duke/command/MarkCommand.java @@ -46,12 +46,15 @@ public String execute(TaskList tasks, Ui ui, Storage storage) throws DukeExcepti try { tasks.markTaskAtPos(this.index); Task currentTask = tasks.getTask(this.index); + assert this.index <= tasks.getCount() && this.index > 0 : + "Index should be more than 0 and less than or equal to the task list size"; storage.save(tasks); return ui.showMarked(currentTask); } catch (IndexOutOfBoundsException e) { if (tasks.getCount() == 0) { throw new DukeException(Message.INVALID_ACCESS_EMPTY_TASKLIST); } else { + assert tasks.getCount() > 0 : "task list should have 1 or more tasks"; throw new DukeException(Message.returnTaskNotFound(tasks)); } } diff --git a/src/main/java/duke/command/UnmarkCommand.java b/src/main/java/duke/command/UnmarkCommand.java index 8fae09eacc..2d60146705 100644 --- a/src/main/java/duke/command/UnmarkCommand.java +++ b/src/main/java/duke/command/UnmarkCommand.java @@ -46,12 +46,15 @@ public String execute(TaskList tasks, Ui ui, Storage storage) throws DukeExcepti try { tasks.unmarkTaskAtPos(this.index); Task currentTask = tasks.getTask(this.index); + assert this.index <= tasks.getCount() && this.index > 0 : + "Position argument should be more than 0 and less than or equal to the task list size"; storage.save(tasks); return ui.showUnmarked(currentTask); } catch (IndexOutOfBoundsException e) { if (tasks.getCount() == 0) { throw new DukeException(Message.INVALID_ACCESS_EMPTY_TASKLIST); } else { + assert tasks.getCount() > 0 : "task list should have 1 or more tasks"; throw new DukeException(Message.returnTaskNotFound(tasks)); } } diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java index 05ddee007a..c241d2b32e 100644 --- a/src/main/java/duke/parser/Parser.java +++ b/src/main/java/duke/parser/Parser.java @@ -8,6 +8,7 @@ import duke.task.ToDo; import java.time.LocalDate; +import java.util.Arrays; public class Parser { @@ -114,10 +115,16 @@ private static DeleteCommand deleteTask(String command) throws DukeException { } private static FindCommand findTask(String command) throws DukeException { - String[] commandList = command.strip().split(" "); + String[] commandList = command.strip().split(" ", 2); try { - return new FindCommand(commandList[1]); + String keyword = commandList[1].strip(); + if (keyword.equals("")) { + throw new DukeException(Message.INVALID_FIND_TASK_FORMAT); + } + assert !keyword.equals("") : "keyword cannot be empty"; + return new FindCommand(keyword); } catch (IndexOutOfBoundsException e) { + assert commandList.length < 2 : "Command list should contain at most 1 element"; throw new DukeException(Message.INVALID_FIND_TASK_FORMAT); } } diff --git a/src/main/java/duke/task/TaskList.java b/src/main/java/duke/task/TaskList.java index 596ac280a9..783498d0a1 100644 --- a/src/main/java/duke/task/TaskList.java +++ b/src/main/java/duke/task/TaskList.java @@ -66,6 +66,8 @@ public int getCount(){ */ public void markTaskAtPos(int position) throws IndexOutOfBoundsException{ Task currTask = getTask(position); + assert position <= this.count && position > 0 : + "Position argument should be more than 0 and less than or equal to the task list size"; currTask.markAsDone(); } @@ -77,6 +79,8 @@ public void markTaskAtPos(int position) throws IndexOutOfBoundsException{ */ public void unmarkTaskAtPos(int position) throws IndexOutOfBoundsException{ Task currTask = getTask(position); + assert position <= this.count && position > 0 : + "Position argument should be more than 0 and less than or equal to the task list size"; currTask.unmark(); } @@ -90,6 +94,8 @@ public void unmarkTaskAtPos(int position) throws IndexOutOfBoundsException{ */ public Task deleteTaskAtPos(int position) throws IndexOutOfBoundsException { Task deletedTask = getTask(position); + assert position <= this.count && position > 0 : + "Position argument should be more than 0 and less than or equal to the task list size"; this.taskArray.remove(position - 1); this.count -= 1; return deletedTask;