From 8b6a41387f6cc0a8452463a3db956ac6a5dd7c8f Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Mon, 11 Sep 2023 03:27:15 +0800 Subject: [PATCH 1/6] Enable assertions in gradle --- build.gradle | 1 + 1 file changed, 1 insertion(+) diff --git a/build.gradle b/build.gradle index 12625094e2..4c4b476bec 100644 --- a/build.gradle +++ b/build.gradle @@ -59,4 +59,5 @@ shadowJar { run{ standardInput = System.in + enableAssertions = true } From a7e067582a803b4219c3a69f670bf619d19e1ae4 Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Mon, 11 Sep 2023 03:36:55 +0800 Subject: [PATCH 2/6] Add assertions fro Corgi.java The code lacks explicit validation checks for input and command parsing. Without propoer validation, the code may encounter unexpected errors when processing user input. Add assertions for input validation and command parsing. --- src/main/java/corgi/Corgi.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/corgi/Corgi.java b/src/main/java/corgi/Corgi.java index 62b244b9e8..070f34782a 100644 --- a/src/main/java/corgi/Corgi.java +++ b/src/main/java/corgi/Corgi.java @@ -44,10 +44,13 @@ public String getIntro() { * Starts the chatbot - Corgi. */ public String getResponse(String input) { + assert !input.equals("") : "Input cannot be empty"; + Command cmd = null; try { cmd = new CommandParser().parse(input); + assert cmd != null: "Command returned from parser cannot be null"; return cmd.execute(this.tasks, this.renderer, this.storage); } catch (InvalidCommandFormatException e) { return this.renderer.showError(e.getClass().getSimpleName(), e.getMessage()); From a6deeaca2801e2e92c369d3edcff9b3b85e9adf6 Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Mon, 11 Sep 2023 04:09:34 +0800 Subject: [PATCH 3/6] Add assertions for CommandParser.java Lacks explicit validation checks for creating new object. Without propoer validation, the code may encounter unexpected errors. Add assertions for argument validation and new object validation. --- src/main/java/corgi/parsers/CommandParser.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/corgi/parsers/CommandParser.java b/src/main/java/corgi/parsers/CommandParser.java index cd05a443fb..62a242ecf4 100644 --- a/src/main/java/corgi/parsers/CommandParser.java +++ b/src/main/java/corgi/parsers/CommandParser.java @@ -147,6 +147,8 @@ private Command newDateCommand(String[] inputs) throws InvalidCommandFormatExcep throw new InvalidCommandFormatException("Invalid date format!"); } + assert target != null : "Target date cannot be null."; + return new FindTasksOnDateCommand(target); } @@ -172,6 +174,8 @@ private Command newAddCommand(String[] inputs, CommandType type) throws InvalidC if (type == CommandType.TODO) { Task target = new ToDo(taskInfo); + assert target != null : "New Todo task cannot be null."; + return new AddTaskCommand(target, CommandType.TODO); } else if (type == CommandType.DEADLINE) { @@ -179,7 +183,7 @@ private Command newAddCommand(String[] inputs, CommandType type) throws InvalidC String[] deadlineInfos = taskInfo.split(" /by "); if (deadlineInfos.length == 1) { - throw new InvalidCommandFormatException("Missing deadline!" + "\nFormat: " + throw new InvalidCommandFormatException("Invalid format!" + "\nFormat: " + type.getCommandFormat()); } @@ -193,8 +197,12 @@ private Command newAddCommand(String[] inputs, CommandType type) throws InvalidC + type.getCommandFormat()); } + assert by != null : "Date cannot be null."; + Task target = new Deadline(deadlineDesc, by); + assert target != null : "New Deadline task cannot be null."; + return new AddTaskCommand(target, CommandType.DEADLINE); } else { @@ -231,8 +239,13 @@ private Command newAddCommand(String[] inputs, CommandType type) throws InvalidC + type.getCommandFormat()); } + assert from != null : "Date cannot be null."; + assert to != null : "Date cannot be null."; + Task target = new Event(eventDesc, from, to); + assert target != null : "New Event task cannot be null."; + return new AddTaskCommand(target, CommandType.EVENT); } } From 7bd9281665c441b099c3aa15464b2285c102ade4 Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Mon, 11 Sep 2023 04:16:05 +0800 Subject: [PATCH 4/6] Add assertions for TaskParser.java Lacks explicit validation checks for creating new object. Without propoer validation, the code may encounter unexpected errors. Add assertions for arguments validation and new object validation. --- src/main/java/corgi/parsers/TaskParser.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/corgi/parsers/TaskParser.java b/src/main/java/corgi/parsers/TaskParser.java index 806492f0b4..ed66d91bc9 100644 --- a/src/main/java/corgi/parsers/TaskParser.java +++ b/src/main/java/corgi/parsers/TaskParser.java @@ -69,6 +69,8 @@ public Task parse(String s) throws ParsingException { throw new InvalidParsingFormatException("Wrong format for date!"); } + assert by != null : "LocalDate object cannot be null"; + task = new Deadline(status, desc, by); break; @@ -87,12 +89,17 @@ public Task parse(String s) throws ParsingException { throw new InvalidParsingFormatException("Wrong format for date!"); } + assert from != null : "LocalDate object cannot be null"; + assert to != null : "LocalDate object cannot be null"; + task = new Event(status, desc, from, to); break; default: throw new InvalidParsingTypeException("Invalid task type!"); } + assert task != null : "Task object cannot be null."; + return task; } } From 0c0cc244f903dbc10874b20a9b30083df6856218 Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Mon, 11 Sep 2023 04:21:51 +0800 Subject: [PATCH 5/6] Validate input before requesting response --- src/main/java/corgi/ui/MainWindow.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/corgi/ui/MainWindow.java b/src/main/java/corgi/ui/MainWindow.java index 4f0c7d3047..bfd6ac6577 100644 --- a/src/main/java/corgi/ui/MainWindow.java +++ b/src/main/java/corgi/ui/MainWindow.java @@ -43,7 +43,12 @@ public void setCorgi(Corgi c) { */ @FXML private void handleUserInput() { - String input = userInput.getText(); + String input = userInput.getText().trim(); + + if (input.equals("")) { + return; + } + String response = corgi.getResponse(input); dialogContainer.getChildren().addAll( DialogBox.getUserDialog(input, userImage), From d63e83fd19f61015342cae2c75ea86a34b5e3dd1 Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Mon, 11 Sep 2023 04:23:30 +0800 Subject: [PATCH 6/6] Fix checkstyle error --- src/main/java/corgi/Corgi.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/corgi/Corgi.java b/src/main/java/corgi/Corgi.java index 070f34782a..c4562eab84 100644 --- a/src/main/java/corgi/Corgi.java +++ b/src/main/java/corgi/Corgi.java @@ -44,13 +44,13 @@ public String getIntro() { * Starts the chatbot - Corgi. */ public String getResponse(String input) { - assert !input.equals("") : "Input cannot be empty"; + assert !input.equals("") : "Input cannot be empty"; Command cmd = null; try { cmd = new CommandParser().parse(input); - assert cmd != null: "Command returned from parser cannot be null"; + assert cmd != null : "Command returned from parser cannot be null"; return cmd.execute(this.tasks, this.renderer, this.storage); } catch (InvalidCommandFormatException e) { return this.renderer.showError(e.getClass().getSimpleName(), e.getMessage());