diff --git a/docs/UserGuide.md b/docs/UserGuide.md index 3599c6d52dd..1b6a868e47c 100644 --- a/docs/UserGuide.md +++ b/docs/UserGuide.md @@ -31,6 +31,8 @@ Momentum is a **desktop app** that **helps freelancers track time spent on diffe * **`find`**`n/NewMomentum` : Find a project that has `NewMomentum` in its name. * **`delete`**`3` : Deletes the 3rd project shown in the current list. + + * **`sort`**`type/deadline order/asc` : Sorts the list of projects by deadline in ascending order. * **`/exit`** : Exits the app. diff --git a/src/main/java/seedu/momentum/logic/parser/SortCommandParser.java b/src/main/java/seedu/momentum/logic/parser/SortCommandParser.java index 9f49dd7b8c4..e080bc686b4 100644 --- a/src/main/java/seedu/momentum/logic/parser/SortCommandParser.java +++ b/src/main/java/seedu/momentum/logic/parser/SortCommandParser.java @@ -27,6 +27,10 @@ public SortCommand parse(String args) throws ParseException { ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, SORT_TYPE, SORT_ORDER); + if (!argMultimap.getPreamble().isEmpty()) { + throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, SortCommand.MESSAGE_USAGE)); + } + String sortOrder = parseSortOrder(argMultimap); boolean isAscending = sortOrder.equals(INPUT_ASCENDING_ORDER); SortType sortType = parseSortType(argMultimap); @@ -41,6 +45,7 @@ public SortCommand parse(String args) throws ParseException { } private String parseSortOrder(ArgumentMultimap argMultimap) throws ParseException { + if (argMultimap.getValue(SORT_ORDER).isEmpty()) { return INPUT_ASCENDING_ORDER; } @@ -57,6 +62,7 @@ private String parseSortOrder(ArgumentMultimap argMultimap) throws ParseExceptio } private SortType parseSortType(ArgumentMultimap argMultimap) throws ParseException { + if (argMultimap.getValue(SORT_TYPE).isEmpty()) { return SortType.NULL; } @@ -64,10 +70,6 @@ private SortType parseSortType(ArgumentMultimap argMultimap) throws ParseExcepti String sortType = argMultimap.getValue(SORT_TYPE).get(); sortType = sortType.trim(); - if (!argMultimap.getPreamble().isEmpty()) { - throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, SortCommand.MESSAGE_USAGE)); - } - switch (sortType) { case INPUT_ALPHA_TYPE: return SortType.ALPHA; diff --git a/src/test/java/seedu/momentum/logic/parser/ProjectBookParserTest.java b/src/test/java/seedu/momentum/logic/parser/ProjectBookParserTest.java index 0788734f8e6..a9a1321e310 100644 --- a/src/test/java/seedu/momentum/logic/parser/ProjectBookParserTest.java +++ b/src/test/java/seedu/momentum/logic/parser/ProjectBookParserTest.java @@ -86,7 +86,6 @@ public void parseCommand_find() throws Exception { @Test public void parseCommand_sort() throws Exception { assertTrue(parser.parseCommand(SortCommand.COMMAND_WORD) instanceof SortCommand); - assertTrue(parser.parseCommand(SortCommand.COMMAND_WORD + " 3") instanceof SortCommand); SortCommand command = (SortCommand) parser.parseCommand(SortCommand.COMMAND_WORD + " " + SORT_ORDER + SortCommand.INPUT_ASCENDING_ORDER + " " + SORT_TYPE + INPUT_ALPHA_TYPE); assertEquals(new SortCommand(SortType.ALPHA, true, false), command); diff --git a/src/test/java/seedu/momentum/logic/parser/SortCommandParserTest.java b/src/test/java/seedu/momentum/logic/parser/SortCommandParserTest.java index 273ec446ec6..2f01620dc82 100644 --- a/src/test/java/seedu/momentum/logic/parser/SortCommandParserTest.java +++ b/src/test/java/seedu/momentum/logic/parser/SortCommandParserTest.java @@ -80,10 +80,30 @@ public void parse_invalidSortOrder_failure() { assertParseFailure(parser, INVALID_SORT_ORDER, MESSAGE_INVALID_SORT_TYPE_OR_ORDER); } + @Test + public void parse_nonEmptyPreambleNoArguments_success() { + + } + @Test public void parse_nonEmptyPreamble_failure() { - String userInput = PREAMBLE_NON_EMPTY + VALID_ALPHA_SORT_TYPE + VALID_ASCENDING_SORT_ORDER; + + // Non-empty preamble without sort type and sort order + String userInput = PREAMBLE_NON_EMPTY; + assertParseFailure(parser, userInput, MESSAGE_NON_EMPTY_PREAMBLE_FAILURE); + + // Non-empty preamble with valid sort type and sort order + userInput = PREAMBLE_NON_EMPTY + VALID_ALPHA_SORT_TYPE + VALID_ASCENDING_SORT_ORDER; + assertParseFailure(parser, userInput, MESSAGE_NON_EMPTY_PREAMBLE_FAILURE); + + // Non-empty preamble with valid sort type + userInput = PREAMBLE_NON_EMPTY + VALID_ALPHA_SORT_TYPE; assertParseFailure(parser, userInput, MESSAGE_NON_EMPTY_PREAMBLE_FAILURE); + + // Non-empty preamble with valid sort order + userInput = PREAMBLE_NON_EMPTY + VALID_ASCENDING_SORT_ORDER; + assertParseFailure(parser, userInput, MESSAGE_NON_EMPTY_PREAMBLE_FAILURE); + } }