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 + "\" !"); diff --git a/src/test/java/corgi/parsers/CommandParserTest.java b/src/test/java/corgi/parsers/CommandParserTest.java index 2893a5a5a7..aa8544f9f0 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 abc")); + parser.parse("mark /target abc")); assertThrows(InvalidCommandFormatException.class, () -> - parser.parse("mark 1 2 3 4")); + 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 /target 1 /target 2")); + // Missing argument flag + assertThrows(InvalidCommandFormatException.class, () -> + 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 /target")); + // Invalid target task number -> non integer assertThrows(InvalidCommandFormatException.class, () -> - parser.parse("unmark abc")); + parser.parse("unmark /target abc")); + // Invalid target task number -> multiple numbers assertThrows(InvalidCommandFormatException.class, () -> - parser.parse("unmark 1 2 3 4")); + 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")); } } diff --git a/src/test/java/corgi/parsers/CommandValidatorTest.java b/src/test/java/corgi/parsers/CommandValidatorTest.java new file mode 100644 index 0000000000..398bdd923f --- /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()); + } +}