From 14d64dc3719a0f3740563a79a4f607ddc02f2451 Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Tue, 19 Sep 2023 00:58:10 +0800 Subject: [PATCH 1/4] Add more test for evey command parsing --- .../java/corgi/parsers/CommandParserTest.java | 179 ++++++++++++++++-- 1 file changed, 164 insertions(+), 15 deletions(-) diff --git a/src/test/java/corgi/parsers/CommandParserTest.java b/src/test/java/corgi/parsers/CommandParserTest.java index 2893a5a5a7..eb5f74be4f 100644 --- a/src/test/java/corgi/parsers/CommandParserTest.java +++ b/src/test/java/corgi/parsers/CommandParserTest.java @@ -5,6 +5,7 @@ import org.junit.jupiter.api.Test; public class CommandParserTest { + @Test public void parse_invalidByeCommand_throwsInvalidCommandFormatException() { CommandParser parser = new CommandParser(); @@ -14,60 +15,208 @@ public void parse_invalidByeCommand_throwsInvalidCommandFormatException() { parser.parse("bye Corgi")); } + @Test + public void parse_invalidListCommand_throwsInvalidCommandFormatException() { + CommandParser parser = new CommandParser(); + + // Test the parsing of the "bye" command with arguments + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("list tasks")); + } + + @Test + public void parse_invalidUndoCommand_throwsInvalidCommandFormatException() { + CommandParser parser = new CommandParser(); + + // Test the parsing of the "bye" command with arguments + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("undo action")); + } + @Test public void parse_invalidMarkCommand_throwsInvalidCommandFormatException() { CommandParser parser = new CommandParser(); - // Test the parsing of the "mark" command with an invalid argument + // No argument provided assertThrows(InvalidCommandFormatException.class, () -> parser.parse("mark")); + // Invalid task number -> Non integer + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("mark /target abc")); + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("mark /target 1.5")); + // Invalid task number -> Multipler tasks + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("mark /target 1 2 3 4")); assertThrows(InvalidCommandFormatException.class, () -> - parser.parse("mark abc")); + parser.parse("mark /target 1 /target 2")); + // Missing argument flag assertThrows(InvalidCommandFormatException.class, () -> - parser.parse("mark 1 2 3 4")); + parser.parse("mark 1")); } @Test public void parse_invalidUnmarkCommand_throwsInvalidCommandFormatException() { CommandParser parser = new CommandParser(); - // Test the parsing of the "mark" command with an invalid argument + // No argument provided assertThrows(InvalidCommandFormatException.class, () -> parser.parse("unmark")); + // Missing value for argument assertThrows(InvalidCommandFormatException.class, () -> - parser.parse("unmark abc")); + parser.parse("unmark /target")); + // Invalid target task number -> non integer assertThrows(InvalidCommandFormatException.class, () -> - parser.parse("unmark 1 2 3 4")); + parser.parse("unmark /target abc")); + // Invalid target task number -> multiple numbers + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("unmark /target 1 2 3 4")); + // Invalid number of argument flag + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("unmark /target 1 /target 2")); + // Missing argument flag + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("unmark 1")); + } + + @Test + public void parse_invalidDeleteCommand_throwsInvalidCommandFormatException() { + CommandParser parser = new CommandParser(); + + // No argument provided + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("delete")); + // Missing value for argument + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("delete /target")); + // Invalid target task number -> non integer + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("delete /target abc")); + // Invalid target task number -> multiple numbers + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("delete /target 1 2 3 4")); + // Invalid number of argument flag + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("delete /target 1 /target 2")); + // Missing argument flag + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("delete 1")); } + @Test + public void parse_invalidFindCommand_throwsInvalidCommandFormatException() { + CommandParser parser = new CommandParser(); + + // No argument provided + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("find")); + // Missing value for argument + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("find /target")); + // Invalid number of argument flag + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("find /target pen /target book")); + // Missing argument flag + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("find pen")); + } + + @Test + public void parse_invalidDateCommand_throwsInvalidCommandFormatException() { + CommandParser parser = new CommandParser(); + + // No argument provided + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("date")); + // Missing value for argument + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("date /target")); + // Invalid number of argument flag + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("date /target 2023-09-22 /target 2023-09-23")); + // Missing argument flag + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("date 2023-09-22")); + // Invalid date format + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("date tmr")); + } + + @Test + public void parse_invalidTodoCommand_throwsInvalidCommandFormatException() { + CommandParser parser = new CommandParser(); + + // No argument provided + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("todo")); + // Missing value for argument + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("todo /desc")); + // Invalid number of same argument + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("todo /desc task1 /desc hard /desc urgent")); + } + + @Test public void parse_invalidDeadlineCommand_throwsInvalidCommandFormatException() { CommandParser parser = new CommandParser(); - // Test the parsing of the "deadline" command with an invalid argument + // No argument provided assertThrows(InvalidCommandFormatException.class, () -> parser.parse("deadline")); + // Invalid date format assertThrows(InvalidCommandFormatException.class, () -> - parser.parse("deadline Invalid deadline /by tomorrow")); - //Todo: Bug found: should count number of /by before start splitting. - // assertThrows(InvalidCommandFormatException.class, - // () -> parser.parse("deadline Invalid deadline /by 2023-09-22 /by 2023-09-23")); + parser.parse("deadline /desc task1 /by tomorrow")); + // Missing argument assertThrows(InvalidCommandFormatException.class, () -> parser.parse("deadline /by 2023-09-22")); + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("deadline /desc task1 2023-09-22")); + // Missing value for argument + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("deadline /desc /by")); + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("deadline /desc task1 /by")); + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("deadline /desc /by 2023-09-22")); + // Invalid number of same argument + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("deadline /desc task1 /by 2023-09-22 /by 2023-09-23")); } @Test public void parse_invalidEventCommand_throwsInvalidCommandFormatException() { CommandParser parser = new CommandParser(); - // Test the parsing of the "event" command with an invalid argument + // No argument provided assertThrows(InvalidCommandFormatException.class, () -> parser.parse("event")); + // Missing argument + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("event /desc event1 /from 2023-09-20")); + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("event /desc event1 /to 2023-09-20")); + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("event /from 2023-09-10 /to 2023-09-20")); + // Invalid number of arguments + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("event /desc event1 /from 2023-09-10 /to 2023-09-20 /to 2023-09-22")); + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("event /desc event1 /desc event1 /from 2023-09-10 /to 2023-09-22")); + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("event /desc event1 /from 2023-09-10 /from 2023-09-10 /to 2023-09-22")); + // Missing value for argument + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("event /desc event1 /from /to 2023-09-22")); + assertThrows(InvalidCommandFormatException.class, () -> + parser.parse("event /desc /from 2023-09-10 /from 2023-09-10 /to 2023-09-22")); assertThrows(InvalidCommandFormatException.class, () -> - parser.parse("event Invalid event /from 2023-09-20")); + parser.parse("event /desc event1 /from 2023-09-10 /from 2023-09-10 /to")); assertThrows(InvalidCommandFormatException.class, () -> - parser.parse("event Invalid event /to 2023-09-20")); + parser.parse("event /desc /from /to")); + // Invalid start date and end date -> start date not before end date assertThrows(InvalidCommandFormatException.class, () -> - parser.parse("event Invalid event /to 2023-09-20 /to 2023-09-22")); + parser.parse("event /desc event1 /from 2023-09-24 /to 2023-09-22")); } } From 3a51309ac245dd2970cf9f789048c8a01d2dbcf6 Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Tue, 19 Sep 2023 00:59:02 +0800 Subject: [PATCH 2/4] Fix exception not thrown for invalid num of args --- src/main/java/corgi/parsers/CommandValidator.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/corgi/parsers/CommandValidator.java b/src/main/java/corgi/parsers/CommandValidator.java index 501f1c206e..c8051a379b 100644 --- a/src/main/java/corgi/parsers/CommandValidator.java +++ b/src/main/java/corgi/parsers/CommandValidator.java @@ -32,8 +32,9 @@ public void validateArguments(String command, Set arguments) continue; } + isValid = false; + if (numOfArg == 0) { - isValid = false; errorMsg.add("Missing argument " + "\"" + argument + "\" !"); } else { errorMsg.add("Invalid number of argument " + "\"" + argument + "\" !"); From 6154c7b408d4e7b4d00ff48cbd2758c1d7f079d4 Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Tue, 19 Sep 2023 01:14:42 +0800 Subject: [PATCH 3/4] Add unit test for validateArguments method --- .../corgi/parsers/CommandValidatorTest.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/test/java/corgi/parsers/CommandValidatorTest.java diff --git a/src/test/java/corgi/parsers/CommandValidatorTest.java b/src/test/java/corgi/parsers/CommandValidatorTest.java new file mode 100644 index 0000000000..67f80e9793 --- /dev/null +++ b/src/test/java/corgi/parsers/CommandValidatorTest.java @@ -0,0 +1,62 @@ +package corgi.parsers; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.HashSet; +import java.util.Set; + +import org.junit.jupiter.api.Test; + +public class CommandValidatorTest { + + @Test + public void validateArguments_invalidNumberOfArguments_throwsInvalidCommandFormatException() { + CommandValidator validator = new CommandValidator(); + + String command1 = "command /arg1 text1 /arg1 text2 /arg2 text3 /arg3 text4"; + String command2 = "command /arg1 text1 /arg2 text2 /arg2 text3 /arg3 text4"; + String command3 = "command /arg1 text1 /arg3 text2 /arg2 text3 /arg3 text4"; + + Set arguments = new HashSet<>(); + arguments.add("/arg1"); + arguments.add("/arg2"); + arguments.add("/arg3"); + + InvalidCommandFormatException exception1 = assertThrows(InvalidCommandFormatException.class, + () -> validator.validateArguments(command1, arguments)); + InvalidCommandFormatException exception2 = assertThrows(InvalidCommandFormatException.class, + () -> validator.validateArguments(command2, arguments)); + InvalidCommandFormatException exception3 = assertThrows(InvalidCommandFormatException.class, + () -> validator.validateArguments(command3, arguments)); + + assertEquals("Invalid number of argument \"/arg1\" !", exception1.getMessage()); + assertEquals("Invalid number of argument \"/arg2\" !", exception2.getMessage()); + assertEquals("Invalid number of argument \"/arg3\" !", exception3.getMessage()); + } + + @Test + public void validateArguments_missingArguments_throwsInvalidCommandFormatException() { + CommandValidator validator = new CommandValidator(); + + String command1 = "command /arg2 text1 /arg3 text2"; + String command2 = "command /arg1 text1 /arg3 text2"; + String command3 = "command /arg1 text1 /arg2 text2"; + + Set arguments = new HashSet<>(); + arguments.add("/arg1"); + arguments.add("/arg2"); + arguments.add("/arg3"); + + InvalidCommandFormatException exception1 = assertThrows(InvalidCommandFormatException.class, + () -> validator.validateArguments(command1, arguments)); + InvalidCommandFormatException exception2 = assertThrows(InvalidCommandFormatException.class, + () -> validator.validateArguments(command2, arguments)); + InvalidCommandFormatException exception3 = assertThrows(InvalidCommandFormatException.class, + () -> validator.validateArguments(command3, arguments)); + + assertEquals("Missing argument \"/arg1\" !", exception1.getMessage()); + assertEquals("Missing argument \"/arg2\" !", exception2.getMessage()); + assertEquals("Missing argument \"/arg3\" !", exception3.getMessage()); + } +} From 3060fc6bc874949b14dbd3e31883b57096a4935d Mon Sep 17 00:00:00 2001 From: Nereus Ng Wei Bin Date: Tue, 19 Sep 2023 01:19:52 +0800 Subject: [PATCH 4/4] Fix checkstyle error --- .../java/corgi/parsers/CommandParserTest.java | 4 ++-- .../corgi/parsers/CommandValidatorTest.java | 24 +++++++++---------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/test/java/corgi/parsers/CommandParserTest.java b/src/test/java/corgi/parsers/CommandParserTest.java index eb5f74be4f..aa8544f9f0 100644 --- a/src/test/java/corgi/parsers/CommandParserTest.java +++ b/src/test/java/corgi/parsers/CommandParserTest.java @@ -153,7 +153,7 @@ public void parse_invalidTodoCommand_throwsInvalidCommandFormatException() { assertThrows(InvalidCommandFormatException.class, () -> parser.parse("todo /desc")); // Invalid number of same argument - assertThrows(InvalidCommandFormatException.class, () -> + assertThrows(InvalidCommandFormatException.class, () -> parser.parse("todo /desc task1 /desc hard /desc urgent")); } @@ -181,7 +181,7 @@ public void parse_invalidDeadlineCommand_throwsInvalidCommandFormatException() { assertThrows(InvalidCommandFormatException.class, () -> parser.parse("deadline /desc /by 2023-09-22")); // Invalid number of same argument - assertThrows(InvalidCommandFormatException.class, () -> + assertThrows(InvalidCommandFormatException.class, () -> parser.parse("deadline /desc task1 /by 2023-09-22 /by 2023-09-23")); } diff --git a/src/test/java/corgi/parsers/CommandValidatorTest.java b/src/test/java/corgi/parsers/CommandValidatorTest.java index 67f80e9793..398bdd923f 100644 --- a/src/test/java/corgi/parsers/CommandValidatorTest.java +++ b/src/test/java/corgi/parsers/CommandValidatorTest.java @@ -23,12 +23,12 @@ public void validateArguments_invalidNumberOfArguments_throwsInvalidCommandForma arguments.add("/arg2"); arguments.add("/arg3"); - InvalidCommandFormatException exception1 = assertThrows(InvalidCommandFormatException.class, - () -> validator.validateArguments(command1, arguments)); - InvalidCommandFormatException exception2 = assertThrows(InvalidCommandFormatException.class, - () -> validator.validateArguments(command2, arguments)); - InvalidCommandFormatException exception3 = assertThrows(InvalidCommandFormatException.class, - () -> validator.validateArguments(command3, arguments)); + InvalidCommandFormatException exception1 = assertThrows(InvalidCommandFormatException.class, () -> + validator.validateArguments(command1, arguments)); + InvalidCommandFormatException exception2 = assertThrows(InvalidCommandFormatException.class, () -> + validator.validateArguments(command2, arguments)); + InvalidCommandFormatException exception3 = assertThrows(InvalidCommandFormatException.class, () -> + validator.validateArguments(command3, arguments)); assertEquals("Invalid number of argument \"/arg1\" !", exception1.getMessage()); assertEquals("Invalid number of argument \"/arg2\" !", exception2.getMessage()); @@ -48,12 +48,12 @@ public void validateArguments_missingArguments_throwsInvalidCommandFormatExcepti arguments.add("/arg2"); arguments.add("/arg3"); - InvalidCommandFormatException exception1 = assertThrows(InvalidCommandFormatException.class, - () -> validator.validateArguments(command1, arguments)); - InvalidCommandFormatException exception2 = assertThrows(InvalidCommandFormatException.class, - () -> validator.validateArguments(command2, arguments)); - InvalidCommandFormatException exception3 = assertThrows(InvalidCommandFormatException.class, - () -> validator.validateArguments(command3, arguments)); + InvalidCommandFormatException exception1 = assertThrows(InvalidCommandFormatException.class, () -> + validator.validateArguments(command1, arguments)); + InvalidCommandFormatException exception2 = assertThrows(InvalidCommandFormatException.class, () -> + validator.validateArguments(command2, arguments)); + InvalidCommandFormatException exception3 = assertThrows(InvalidCommandFormatException.class, () -> + validator.validateArguments(command3, arguments)); assertEquals("Missing argument \"/arg1\" !", exception1.getMessage()); assertEquals("Missing argument \"/arg2\" !", exception2.getMessage());