Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2122S1#74 from rohit0718/update/add-p…
Browse files Browse the repository at this point in the history
…arseutil

Add new parseutil method
  • Loading branch information
SHEZADHASSAN22 committed Oct 12, 2021
2 parents a24d04e + 777630a commit 509688b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -340,4 +340,25 @@ public static Module parseModule(String code, Optional<String> name) throws Pars
: Optional.empty();
return new Module(moduleCode, moduleName);
}

/**
* Parses a {@code String args} and returns first word if it is one of mod, lesson, or exam.
* If args is invalid, returns a ParseException with the provided errorMessage.
* Note that we only allow exact lowercase matches for now.
*
* @throws ParseException if the args string is invalid.
*/
public static Type parseFirstArg(String args, String errorMessage) throws ParseException {
String firstArg = args.split(" ", 2)[0];
switch (firstArg) {
case "mod":
return Type.MOD;
case "lesson":
return Type.LESSON;
case "exam":
return Type.EXAM;
default:
throw new ParseException(errorMessage);
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/seedu/address/logic/parser/Type.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package seedu.address.logic.parser;

public enum Type {
MOD, LESSON, EXAM
}
21 changes: 21 additions & 0 deletions src/test/java/seedu/address/logic/parser/ParserUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class ParserUtilTest {
private static final String INVALID_LESSON_NAME = " ";
private static final String INVALID_MODULE_CODE = "C2030S";
private static final String INVALID_MODULE_NAME = " ";
private static final String INVALID_FIRST_ARG = "shez";

private static final String VALID_NAME = "Rachel Walker";
private static final String VALID_PHONE = "123456";
Expand All @@ -67,6 +68,9 @@ public class ParserUtilTest {
private static final String VALID_LESSON_NAME = "Lecture";
private static final String VALID_MODULE_CODE = "CS2103T";
private static final String VALID_MODULE_NAME = "Software Engineering";
private static final String VALID_FIRST_ARG_MOD = "mod";
private static final String VALID_FIRST_ARG_EXAM = "exam";
private static final String VALID_FIRST_ARG_LESSON = "lesson";

private static final String WHITESPACE = " \t\r\n";

Expand Down Expand Up @@ -485,4 +489,21 @@ public void parseModule_validValues_returnsModule() throws Exception {
Module expectedModule = new Module(moduleCode, Optional.of(moduleName));
assertEquals(expectedModule, ParserUtil.parseModule(VALID_MODULE_CODE, Optional.of(VALID_MODULE_NAME)));
}

@Test
public void parseFirstArg_null_throwsNullPointerException() throws Exception {
assertThrows(NullPointerException.class, () -> ParserUtil.parseFirstArg(null, null));
}

@Test
public void parseFirstArg_validValues_returnsValidResults() throws Exception {
assertEquals(Type.MOD, ParserUtil.parseFirstArg(VALID_FIRST_ARG_MOD, null));
assertEquals(Type.LESSON, ParserUtil.parseFirstArg(VALID_FIRST_ARG_LESSON, null));
assertEquals(Type.EXAM, ParserUtil.parseFirstArg(VALID_FIRST_ARG_EXAM, null));
}

@Test
public void parseFirstArg_invalidValue_throwsParseException() throws Exception {
assertThrows(ParseException.class, () -> ParserUtil.parseFirstArg(INVALID_FIRST_ARG, null));
}
}

0 comments on commit 509688b

Please sign in to comment.