Skip to content

Commit

Permalink
Merge pull request #62 from kkangs0226/feature-issue-47
Browse files Browse the repository at this point in the history
Update SortCommandParser
  • Loading branch information
kkangs0226 authored Oct 16, 2020
2 parents edb3a2e + cd3e4eb commit a9091f9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
2 changes: 2 additions & 0 deletions docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
10 changes: 6 additions & 4 deletions src/main/java/seedu/momentum/logic/parser/SortCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
Expand All @@ -57,17 +62,14 @@ private String parseSortOrder(ArgumentMultimap argMultimap) throws ParseExceptio
}

private SortType parseSortType(ArgumentMultimap argMultimap) throws ParseException {

if (argMultimap.getValue(SORT_TYPE).isEmpty()) {
return SortType.NULL;
}

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}

}

0 comments on commit a9091f9

Please sign in to comment.