Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2021S1#85 from lll-jy/scoping
Browse files Browse the repository at this point in the history
Refine scoping of existing commands.
  • Loading branch information
T-Fang authored Oct 11, 2020
2 parents 3967524 + 89ef446 commit be478f7
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public CommandResult execute(String commandText) throws CommandException, ParseE
logger.info("----------------[USER COMMAND][" + commandText + "]");

CommandResult commandResult;
Command command = mainCatalogueParser.parseCommand(commandText);
Command command = mainCatalogueParser.parseCommand(commandText, model.getStatus());
commandResult = command.execute(model);

try {
Expand Down
77 changes: 54 additions & 23 deletions src/main/java/seedu/address/logic/parser/MainCatalogueParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.StartCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.exceptions.InvalidScopeException;
import seedu.address.model.project.Status;

/**
* Parses user input.
Expand All @@ -33,50 +35,79 @@ public class MainCatalogueParser {
* Parses user input into command for execution.
*
* @param userInput full user input string
* @param status the status of the current scoping
* @return the command based on the user input
* @throws ParseException if the user input does not conform the expected format
*/
public Command parseCommand(String userInput) throws ParseException {
public Command parseCommand(String userInput, Status status) throws ParseException {
final Matcher matcher = BASIC_COMMAND_FORMAT.matcher(userInput.trim());
if (!matcher.matches()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, HelpCommand.MESSAGE_USAGE));
}

final String commandWord = matcher.group("commandWord");
final String arguments = matcher.group("arguments");
switch (commandWord) {
if (status == Status.CATALOGUE) {
switch (commandWord) {

case AddCommand.COMMAND_WORD:
return new AddCommandParser().parse(arguments);
case AddCommand.COMMAND_WORD:
return new AddCommandParser().parse(arguments);

case EditCommand.COMMAND_WORD:
return new EditCommandParser().parse(arguments);
case EditCommand.COMMAND_WORD:
return new EditCommandParser().parse(arguments);

case DeleteCommand.COMMAND_WORD:
return new DeleteCommandParser().parse(arguments);
case DeleteCommand.COMMAND_WORD:
return new DeleteCommandParser().parse(arguments);

case ClearCommand.COMMAND_WORD:
return new ClearCommand();
case ClearCommand.COMMAND_WORD:
return new ClearCommand();

case FindCommand.COMMAND_WORD:
return new FindCommandParser().parse(arguments);
case FindCommand.COMMAND_WORD:
return new FindCommandParser().parse(arguments);

case ListCommand.COMMAND_WORD:
return new ListCommand();
case ListCommand.COMMAND_WORD:
return new ListCommand();

case ExitCommand.COMMAND_WORD:
return new ExitCommand();
case ExitCommand.COMMAND_WORD:
return new ExitCommand();

case HelpCommand.COMMAND_WORD:
return new HelpCommand();
case HelpCommand.COMMAND_WORD:
return new HelpCommand();

case StartCommand.COMMAND_WORD:
return new StartCommandParser().parse(arguments);
case StartCommand.COMMAND_WORD:
return new StartCommandParser().parse(arguments);

case LeaveCommand.COMMAND_WORD:
return new LeaveCommand();
case LeaveCommand.COMMAND_WORD:
throw new InvalidScopeException(Status.PROJECT, Status.CATALOGUE);

default:
default:
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}
} else if (status == Status.PROJECT) {
switch (commandWord) {

case LeaveCommand.COMMAND_WORD:
return new LeaveCommand();

case ExitCommand.COMMAND_WORD:
return new ExitCommand();

case HelpCommand.COMMAND_WORD:
return new HelpCommand();

case AddCommand.COMMAND_WORD:
case EditCommand.COMMAND_WORD:
case DeleteCommand.COMMAND_WORD:
case ClearCommand.COMMAND_WORD:
case FindCommand.COMMAND_WORD:
case ListCommand.COMMAND_WORD:
case StartCommand.COMMAND_WORD:
throw new InvalidScopeException(Status.CATALOGUE, Status.PROJECT);

default:
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}
} else {
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}
}
Expand Down
103 changes: 86 additions & 17 deletions src/test/java/seedu/address/logic/parser/MainCatalogueParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.StartCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.exceptions.InvalidScopeException;
import seedu.address.model.project.NameContainsKeywordsPredicate;
import seedu.address.model.project.Project;
import seedu.address.model.project.Status;
import seedu.address.testutil.EditProjectDescriptorBuilder;
import seedu.address.testutil.ProjectBuilder;
import seedu.address.testutil.ProjectUtil;
Expand All @@ -38,20 +40,20 @@ public class MainCatalogueParserTest {
@Test
public void parseCommand_add() throws Exception {
Project project = new ProjectBuilder().build();
AddCommand command = (AddCommand) parser.parseCommand(ProjectUtil.getAddCommand(project));
AddCommand command = (AddCommand) parser.parseCommand(ProjectUtil.getAddCommand(project), Status.CATALOGUE);
assertEquals(new AddCommand(project), command);
}

@Test
public void parseCommand_clear() throws Exception {
assertTrue(parser.parseCommand(ClearCommand.COMMAND_WORD) instanceof ClearCommand);
assertTrue(parser.parseCommand(ClearCommand.COMMAND_WORD + " 3") instanceof ClearCommand);
assertTrue(parser.parseCommand(ClearCommand.COMMAND_WORD, Status.CATALOGUE) instanceof ClearCommand);
assertTrue(parser.parseCommand(ClearCommand.COMMAND_WORD + " 3", Status.CATALOGUE) instanceof ClearCommand);
}

@Test
public void parseCommand_delete() throws Exception {
DeleteCommand command = (DeleteCommand) parser.parseCommand(
DeleteCommand.COMMAND_WORD + " " + INDEX_FIRST_PROJECT.getOneBased());
DeleteCommand.COMMAND_WORD + " " + INDEX_FIRST_PROJECT.getOneBased(), Status.CATALOGUE);
assertEquals(new DeleteCommand(INDEX_FIRST_PROJECT), command);
}

Expand All @@ -60,57 +62,124 @@ public void parseCommand_edit() throws Exception {
Project project = new ProjectBuilder().build();
EditProjectDescriptor descriptor = new EditProjectDescriptorBuilder(project).build();
EditCommand command = (EditCommand) parser.parseCommand(EditCommand.COMMAND_WORD + " "
+ INDEX_FIRST_PROJECT.getOneBased() + " " + ProjectUtil.getEditProjectDescriptorDetails(descriptor));
+ INDEX_FIRST_PROJECT.getOneBased() + " " + ProjectUtil.getEditProjectDescriptorDetails(descriptor),
Status.CATALOGUE);
assertEquals(new EditCommand(INDEX_FIRST_PROJECT, descriptor), command);
}

@Test
public void parseCommand_exit() throws Exception {
assertTrue(parser.parseCommand(ExitCommand.COMMAND_WORD) instanceof ExitCommand);
assertTrue(parser.parseCommand(ExitCommand.COMMAND_WORD + " 3") instanceof ExitCommand);
assertTrue(parser.parseCommand(ExitCommand.COMMAND_WORD, Status.CATALOGUE) instanceof ExitCommand);
assertTrue(parser.parseCommand(ExitCommand.COMMAND_WORD + " 3", Status.CATALOGUE) instanceof ExitCommand);
assertTrue(parser.parseCommand(ExitCommand.COMMAND_WORD, Status.PROJECT) instanceof ExitCommand);
assertTrue(parser.parseCommand(ExitCommand.COMMAND_WORD + " 3", Status.PROJECT) instanceof ExitCommand);
}

@Test
public void parseCommand_find() throws Exception {
List<String> keywords = Arrays.asList("foo", "bar", "baz");
FindCommand command = (FindCommand) parser.parseCommand(
FindCommand.COMMAND_WORD + " " + keywords.stream().collect(Collectors.joining(" ")));
FindCommand.COMMAND_WORD + " " + keywords.stream().collect(Collectors.joining(" ")), Status.CATALOGUE);
assertEquals(new FindCommand(new NameContainsKeywordsPredicate(keywords)), command);
}

@Test
public void parseCommand_help() throws Exception {
assertTrue(parser.parseCommand(HelpCommand.COMMAND_WORD) instanceof HelpCommand);
assertTrue(parser.parseCommand(HelpCommand.COMMAND_WORD + " 3") instanceof HelpCommand);
assertTrue(parser.parseCommand(HelpCommand.COMMAND_WORD, Status.CATALOGUE) instanceof HelpCommand);
assertTrue(parser.parseCommand(HelpCommand.COMMAND_WORD + " 3", Status.CATALOGUE) instanceof HelpCommand);
assertTrue(parser.parseCommand(HelpCommand.COMMAND_WORD, Status.PROJECT) instanceof HelpCommand);
assertTrue(parser.parseCommand(HelpCommand.COMMAND_WORD + " 3", Status.PROJECT) instanceof HelpCommand);
}

@Test
public void parseCommand_list() throws Exception {
assertTrue(parser.parseCommand(ListCommand.COMMAND_WORD) instanceof ListCommand);
assertTrue(parser.parseCommand(ListCommand.COMMAND_WORD + " 3") instanceof ListCommand);
assertTrue(parser.parseCommand(ListCommand.COMMAND_WORD, Status.CATALOGUE) instanceof ListCommand);
assertTrue(parser.parseCommand(ListCommand.COMMAND_WORD + " 3", Status.CATALOGUE) instanceof ListCommand);
}

@Test
public void parseCommand_start() throws Exception {
StartCommand command = (StartCommand) parser.parseCommand(
StartCommand.COMMAND_WORD + " " + INDEX_FIRST_PROJECT.getOneBased());
StartCommand.COMMAND_WORD + " " + INDEX_FIRST_PROJECT.getOneBased(), Status.CATALOGUE);
assertEquals(new StartCommand(INDEX_FIRST_PROJECT), command);
}

@Test
public void parseCommand_leave() throws Exception {
assertTrue(parser.parseCommand(LeaveCommand.COMMAND_WORD) instanceof LeaveCommand);
assertTrue(parser.parseCommand(LeaveCommand.COMMAND_WORD + " 3") instanceof LeaveCommand);
assertTrue(parser.parseCommand(LeaveCommand.COMMAND_WORD, Status.PROJECT) instanceof LeaveCommand);
assertTrue(parser.parseCommand(LeaveCommand.COMMAND_WORD + " 3", Status.PROJECT) instanceof LeaveCommand);
}

@Test
public void parseCommand_unrecognisedInput_throwsParseException() {
assertThrows(ParseException.class, String.format(MESSAGE_INVALID_COMMAND_FORMAT, HelpCommand.MESSAGE_USAGE), ()
-> parser.parseCommand(""));
-> parser.parseCommand("", Status.CATALOGUE));
}

@Test
public void parseCommand_unknownCommand_throwsParseException() {
assertThrows(ParseException.class, MESSAGE_UNKNOWN_COMMAND, () -> parser.parseCommand("unknownCommand"));
assertThrows(ParseException.class, MESSAGE_UNKNOWN_COMMAND, () -> parser.parseCommand("unknownCommand",
Status.CATALOGUE));
}

@Test
public void parseCommand_invalidScope_throwsInvalidScopeException() {
try {
Project project = new ProjectBuilder().build();
parser.parseCommand(ProjectUtil.getAddCommand(project), Status.PROJECT);
} catch (Exception e) {
assertEquals(new InvalidScopeException(Status.CATALOGUE, Status.PROJECT), e);
}

try {
parser.parseCommand(ClearCommand.COMMAND_WORD, Status.PROJECT);
} catch (Exception e) {
assertEquals(new InvalidScopeException(Status.CATALOGUE, Status.PROJECT), e);
}

try {
parser.parseCommand(
DeleteCommand.COMMAND_WORD + " " + INDEX_FIRST_PROJECT.getOneBased(), Status.PROJECT);
} catch (Exception e) {
assertEquals(new InvalidScopeException(Status.CATALOGUE, Status.PROJECT), e);
}

try {
Project project = new ProjectBuilder().build();
EditProjectDescriptor descriptor = new EditProjectDescriptorBuilder(project).build();
parser.parseCommand(EditCommand.COMMAND_WORD + " " + INDEX_FIRST_PROJECT.getOneBased()
+ " " + ProjectUtil.getEditProjectDescriptorDetails(descriptor),
Status.PROJECT);
} catch (Exception e) {
assertEquals(new InvalidScopeException(Status.CATALOGUE, Status.PROJECT), e);
}

try {
List<String> keywords = Arrays.asList("foo", "bar", "baz");
parser.parseCommand(
FindCommand.COMMAND_WORD + " " + keywords.stream().collect(Collectors.joining(" ")),
Status.CATALOGUE);
} catch (Exception e) {
assertEquals(new InvalidScopeException(Status.CATALOGUE, Status.PROJECT), e);
}

try {
parser.parseCommand(ListCommand.COMMAND_WORD, Status.PROJECT);
} catch (Exception e) {
assertEquals(new InvalidScopeException(Status.CATALOGUE, Status.PROJECT), e);
}

try {
parser.parseCommand(
StartCommand.COMMAND_WORD + " " + INDEX_FIRST_PROJECT.getOneBased(), Status.PROJECT);
} catch (Exception e) {
assertEquals(new InvalidScopeException(Status.CATALOGUE, Status.PROJECT), e);
}

try {
parser.parseCommand(LeaveCommand.COMMAND_WORD, Status.CATALOGUE);
} catch (Exception e) {
assertEquals(new InvalidScopeException(Status.PROJECT, Status.CATALOGUE), e);
}
}
}

0 comments on commit be478f7

Please sign in to comment.