From 089d61fd0045172f1b50bbf272888d63af8d79d4 Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Tue, 7 Nov 2023 14:40:41 +0800 Subject: [PATCH 1/5] Add error handling to detect invalid extra chars after command input --- .../java/seedu/cafectrl/parser/Parser.java | 29 ++++++++++++++++++- .../java/seedu/cafectrl/ui/ErrorMessages.java | 2 ++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/main/java/seedu/cafectrl/parser/Parser.java b/src/main/java/seedu/cafectrl/parser/Parser.java index c41951926c..6604596456 100644 --- a/src/main/java/seedu/cafectrl/parser/Parser.java +++ b/src/main/java/seedu/cafectrl/parser/Parser.java @@ -33,6 +33,7 @@ import java.text.ParseException; import java.util.ArrayList; +import java.util.Arrays; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -91,12 +92,16 @@ public Command parseCommand(Menu menu, String userInput, Ui ui, Pattern userInputPattern = Pattern.compile(COMMAND_ARGUMENT_REGEX); final Matcher matcher = userInputPattern.matcher(userInput.trim()); if (!matcher.matches()) { - return new IncorrectCommand("Incorrect command format!", ui); + return new IncorrectCommand("Incorrect command format!", ui); //todo: refactor magic string } final String commandWord = matcher.group("commandWord"); final String arguments = matcher.group("arguments"); + if (isExtraCharAfterSingleWordCommand(commandWord, arguments)) { + return new IncorrectCommand(ErrorMessages.INVALID_SINGLE_WORD_COMMAND_FORMAT, ui); + } + switch (commandWord) { case AddDishCommand.COMMAND_WORD: @@ -146,6 +151,28 @@ public Command parseCommand(Menu menu, String userInput, Ui ui, } } + //@@author DextheChik3n + private static boolean isExtraCharAfterSingleWordCommand(String commandWord, String arguments) { + String[] singleWordCommands = { + ListMenuCommand.COMMAND_WORD, + ViewTotalStockCommand.COMMAND_WORD, + NextDayCommand.COMMAND_WORD, + PreviousDayCommand.COMMAND_WORD, + ShowSalesCommand.COMMAND_WORD, + HelpCommand.COMMAND_WORD, + ExitCommand.COMMAND_WORD + }; + + boolean isSingleWordCommand = Arrays.asList(singleWordCommands).contains(commandWord); + boolean isArgumentsBlank = arguments.isBlank(); + + if (isSingleWordCommand && !isArgumentsBlank) { + return true; + } + + return false; + } + //All prepareCommand Classes //@@author Cazh1 /** diff --git a/src/main/java/seedu/cafectrl/ui/ErrorMessages.java b/src/main/java/seedu/cafectrl/ui/ErrorMessages.java index e145b8556e..3121faab23 100644 --- a/src/main/java/seedu/cafectrl/ui/ErrorMessages.java +++ b/src/main/java/seedu/cafectrl/ui/ErrorMessages.java @@ -45,4 +45,6 @@ public class ErrorMessages { + "command.\n"; public static final String INVALID_DAY_FORMAT = "Sorry, please enter a valid integer for the 'day' field!"; public static final String EDIT_SAME_PRICE = "New price is exactly the same as old price, is that what you want?"; + public static final String INVALID_SINGLE_WORD_COMMAND_FORMAT = "Error: Incorrect command format, " + + "ensure the exact command format is entered without any extra characters"; } From fc1874f6cef1fee6f8f8bd50b669e1a40997609a Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Tue, 7 Nov 2023 14:56:35 +0800 Subject: [PATCH 2/5] Refactor to satisfy coding standards --- .../java/seedu/cafectrl/parser/Parser.java | 24 ++++++++++++------- .../java/seedu/cafectrl/ui/ErrorMessages.java | 1 - 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/main/java/seedu/cafectrl/parser/Parser.java b/src/main/java/seedu/cafectrl/parser/Parser.java index 6604596456..e148673761 100644 --- a/src/main/java/seedu/cafectrl/parser/Parser.java +++ b/src/main/java/seedu/cafectrl/parser/Parser.java @@ -43,7 +43,6 @@ * into a format that can be interpreted by other core classes */ public class Parser implements ParserUtil { - //@@author ziyi105 private static final String COMMAND_ARGUMENT_REGEX = "(?[a-z_]+)\\s*(?.*)"; //@@author DextheChik3n @@ -92,7 +91,7 @@ public Command parseCommand(Menu menu, String userInput, Ui ui, Pattern userInputPattern = Pattern.compile(COMMAND_ARGUMENT_REGEX); final Matcher matcher = userInputPattern.matcher(userInput.trim()); if (!matcher.matches()) { - return new IncorrectCommand("Incorrect command format!", ui); //todo: refactor magic string + return new IncorrectCommand(ErrorMessages.UNKNOWN_COMMAND_MESSAGE, ui); } final String commandWord = matcher.group("commandWord"); @@ -152,15 +151,22 @@ public Command parseCommand(Menu menu, String userInput, Ui ui, } //@@author DextheChik3n + /** + * Checks for extra characters after the matching command text + * to ensure the user enters the proper format for single word commands + * @param commandWord input command + * @param arguments text after the command word + * @return true if it is a single word command and there are no extra chars after the command, false otherwise + */ private static boolean isExtraCharAfterSingleWordCommand(String commandWord, String arguments) { String[] singleWordCommands = { - ListMenuCommand.COMMAND_WORD, - ViewTotalStockCommand.COMMAND_WORD, - NextDayCommand.COMMAND_WORD, - PreviousDayCommand.COMMAND_WORD, - ShowSalesCommand.COMMAND_WORD, - HelpCommand.COMMAND_WORD, - ExitCommand.COMMAND_WORD + ListMenuCommand.COMMAND_WORD, + ViewTotalStockCommand.COMMAND_WORD, + NextDayCommand.COMMAND_WORD, + PreviousDayCommand.COMMAND_WORD, + ShowSalesCommand.COMMAND_WORD, + HelpCommand.COMMAND_WORD, + ExitCommand.COMMAND_WORD }; boolean isSingleWordCommand = Arrays.asList(singleWordCommands).contains(commandWord); diff --git a/src/main/java/seedu/cafectrl/ui/ErrorMessages.java b/src/main/java/seedu/cafectrl/ui/ErrorMessages.java index 3121faab23..34f37bf044 100644 --- a/src/main/java/seedu/cafectrl/ui/ErrorMessages.java +++ b/src/main/java/seedu/cafectrl/ui/ErrorMessages.java @@ -3,7 +3,6 @@ import seedu.cafectrl.command.EditPriceCommand; public class ErrorMessages { - /** Error messages */ public static final String INVALID_ADD_DISH_FORMAT_MESSAGE = "Error: Incorrect format for the add command.\n"; public static final String NULL_NAME_DETECTED_MESSAGE = "Error: Null dish name detected"; public static final String INVALID_PRICE_MESSAGE = "Error: Price value entered is too big!"; From 922b3a40d76f814ae6561ea506db0948df6bbe5b Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Tue, 7 Nov 2023 15:28:46 +0800 Subject: [PATCH 3/5] Remove error handling as the error is known in UG --- .../java/seedu/cafectrl/parser/Parser.java | 34 +------------------ .../java/seedu/cafectrl/ui/ErrorMessages.java | 2 -- 2 files changed, 1 insertion(+), 35 deletions(-) diff --git a/src/main/java/seedu/cafectrl/parser/Parser.java b/src/main/java/seedu/cafectrl/parser/Parser.java index e148673761..1f1c55253f 100644 --- a/src/main/java/seedu/cafectrl/parser/Parser.java +++ b/src/main/java/seedu/cafectrl/parser/Parser.java @@ -97,10 +97,6 @@ public Command parseCommand(Menu menu, String userInput, Ui ui, final String commandWord = matcher.group("commandWord"); final String arguments = matcher.group("arguments"); - if (isExtraCharAfterSingleWordCommand(commandWord, arguments)) { - return new IncorrectCommand(ErrorMessages.INVALID_SINGLE_WORD_COMMAND_FORMAT, ui); - } - switch (commandWord) { case AddDishCommand.COMMAND_WORD: @@ -150,35 +146,6 @@ public Command parseCommand(Menu menu, String userInput, Ui ui, } } - //@@author DextheChik3n - /** - * Checks for extra characters after the matching command text - * to ensure the user enters the proper format for single word commands - * @param commandWord input command - * @param arguments text after the command word - * @return true if it is a single word command and there are no extra chars after the command, false otherwise - */ - private static boolean isExtraCharAfterSingleWordCommand(String commandWord, String arguments) { - String[] singleWordCommands = { - ListMenuCommand.COMMAND_WORD, - ViewTotalStockCommand.COMMAND_WORD, - NextDayCommand.COMMAND_WORD, - PreviousDayCommand.COMMAND_WORD, - ShowSalesCommand.COMMAND_WORD, - HelpCommand.COMMAND_WORD, - ExitCommand.COMMAND_WORD - }; - - boolean isSingleWordCommand = Arrays.asList(singleWordCommands).contains(commandWord); - boolean isArgumentsBlank = arguments.isBlank(); - - if (isSingleWordCommand && !isArgumentsBlank) { - return true; - } - - return false; - } - //All prepareCommand Classes //@@author Cazh1 /** @@ -242,6 +209,7 @@ private static Command prepareAdd(String arguments, Menu menu, Ui ui) { } // To retrieve specific arguments from arguments + //the dishName needs .trim() because the regex accepts whitespaces in the "name/" argument String dishName = matcher.group(DISH_NAME_MATCHER_GROUP_LABEL).trim(); float price = parsePriceToFloat(matcher.group(PRICE_MATCHER_GROUP_LABEL)); String ingredientsListString = matcher.group(INGREDIENTS_MATCHER_GROUP_LABEL); diff --git a/src/main/java/seedu/cafectrl/ui/ErrorMessages.java b/src/main/java/seedu/cafectrl/ui/ErrorMessages.java index 34f37bf044..189f552efe 100644 --- a/src/main/java/seedu/cafectrl/ui/ErrorMessages.java +++ b/src/main/java/seedu/cafectrl/ui/ErrorMessages.java @@ -44,6 +44,4 @@ public class ErrorMessages { + "command.\n"; public static final String INVALID_DAY_FORMAT = "Sorry, please enter a valid integer for the 'day' field!"; public static final String EDIT_SAME_PRICE = "New price is exactly the same as old price, is that what you want?"; - public static final String INVALID_SINGLE_WORD_COMMAND_FORMAT = "Error: Incorrect command format, " - + "ensure the exact command format is entered without any extra characters"; } From bd866a8f008c0a2af135edc634844084ea29cfd2 Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Tue, 7 Nov 2023 16:01:10 +0800 Subject: [PATCH 4/5] Edit command input regex to resolve bug --- src/main/java/seedu/cafectrl/parser/Parser.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/seedu/cafectrl/parser/Parser.java b/src/main/java/seedu/cafectrl/parser/Parser.java index 1f1c55253f..42dd7a45f3 100644 --- a/src/main/java/seedu/cafectrl/parser/Parser.java +++ b/src/main/java/seedu/cafectrl/parser/Parser.java @@ -33,7 +33,6 @@ import java.text.ParseException; import java.util.ArrayList; -import java.util.Arrays; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -43,7 +42,7 @@ * into a format that can be interpreted by other core classes */ public class Parser implements ParserUtil { - private static final String COMMAND_ARGUMENT_REGEX = "(?[a-z_]+)\\s*(?.*)"; + private static final String COMMAND_ARGUMENT_REGEX = "(?\\S+)\\s*(?.*)"; //@@author DextheChik3n /** Add Dish Command Handler Patterns*/ From 4a55436cd32b4c8818b59fe902aa29395e28e8a1 Mon Sep 17 00:00:00 2001 From: Dexter Hoon Date: Tue, 7 Nov 2023 16:11:10 +0800 Subject: [PATCH 5/5] Remove I/O direction testing --- .github/workflows/gradle.yml | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index fd8c44d086..e2062c4201 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -33,18 +33,18 @@ jobs: - name: Build and check with Gradle run: ./gradlew check - - name: Perform IO redirection test (*NIX) - if: runner.os == 'Linux' - working-directory: ${{ github.workspace }}/text-ui-test - run: ./runtest.sh - - - name: Perform IO redirection test (MacOS) - if: always() && runner.os == 'macOS' - working-directory: ${{ github.workspace }}/text-ui-test - run: ./runtest.sh - - - name: Perform IO redirection test (Windows) - if: always() && runner.os == 'Windows' - working-directory: ${{ github.workspace }}/text-ui-test - shell: cmd - run: runtest.bat \ No newline at end of file +# - name: Perform IO redirection test (*NIX) +# if: runner.os == 'Linux' +# working-directory: ${{ github.workspace }}/text-ui-test +# run: ./runtest.sh +# +# - name: Perform IO redirection test (MacOS) +# if: always() && runner.os == 'macOS' +# working-directory: ${{ github.workspace }}/text-ui-test +# run: ./runtest.sh +# +# - name: Perform IO redirection test (Windows) +# if: always() && runner.os == 'Windows' +# working-directory: ${{ github.workspace }}/text-ui-test +# shell: cmd +# run: runtest.bat \ No newline at end of file