Skip to content

Commit

Permalink
Add test class ParserTest.java, update Parser.java and DukeException.…
Browse files Browse the repository at this point in the history
…java
  • Loading branch information
lebencwb committed Sep 5, 2020
1 parent ef7b817 commit 03cc1c6
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/main/java/duke/DukeException.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ public class DukeException extends Exception {
public static final DukeException INVALID_TASK_DESCRIPTION_EXCEPTION = new DukeException(
"Sorry, the description of a task cannot be empty.");

public static final DukeException INVALID_DEADLINE_FORMAT_EXCEPTION = new DukeException(
"Sorry, the description of a Deadline must be in this format:\n" +
"\tdeadline [task name] /by [deadline]");

public static final DukeException INVALID_EVENT_FORMAT_EXCEPTION = new DukeException(
"Sorry, the description of an Event must be in this format:\n" +
"\tevent [task name] /at [time]");

public static final DukeException FILE_PARSING_EXCEPTION = new DukeException(
"Sorry, an error occurred while trying to parse the file.");

Expand Down
18 changes: 13 additions & 5 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,31 @@ public static Command parse(String string) throws DukeException {
break;
case "todo":
if (parsingScanner.hasNext()) {
command = new AddCommand(CommandType.TODO, parsingScanner.nextLine());
command = new AddCommand(CommandType.TODO, parsingScanner.nextLine().trim());
} else {
throw DukeException.INVALID_TASK_DESCRIPTION_EXCEPTION;
}
break;
case "deadline":
if (parsingScanner.hasNext()) {
command = new AddCommand(CommandType.DEADLINE,
parsingScanner.nextLine().split("( /by )", 2));
try {
String[] details = parsingScanner.nextLine().trim().split("( /by )", 2);
command = new AddCommand(CommandType.DEADLINE, details[0], details[1]);
} catch (IndexOutOfBoundsException e) {
throw DukeException.INVALID_DEADLINE_FORMAT_EXCEPTION;
}
} else {
throw DukeException.INVALID_TASK_DESCRIPTION_EXCEPTION;
}
break;
case "event":
if (parsingScanner.hasNext()) {
command = new AddCommand(CommandType.EVENT,
parsingScanner.nextLine().split("( /at )", 2));
try {
String[] details = parsingScanner.nextLine().trim().split("( /at )", 2);
command = new AddCommand(CommandType.EVENT, details[0], details[1]);
} catch (IndexOutOfBoundsException e) {
throw DukeException.INVALID_EVENT_FORMAT_EXCEPTION;
}
} else {
throw DukeException.INVALID_TASK_DESCRIPTION_EXCEPTION;
}
Expand Down
161 changes: 161 additions & 0 deletions src/test/java/duke/ParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package duke;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

public class ParserTest {
@Test
public void parse_bye_bye() throws Exception {
assertEquals("BYE", Parser.parse("bye").toString());
}

@Test
public void parse_list_list() throws Exception {
assertEquals("LIST", Parser.parse("list").toString());
}

@Test
public void parse_done1_done1() throws Exception {
assertEquals("DONE 1", Parser.parse("done 1").toString());
}

@Test
public void parse_delete2_delete2() throws Exception {
assertEquals("DELETE 2", Parser.parse("delete 2").toString());
}

@Test
public void parse_todo_todo() throws Exception {
assertEquals("TODO [read book]", Parser.parse("todo read book").toString());
}

@Test
public void parse_deadline_deadline() throws Exception {
assertEquals("DEADLINE [return book, 6th June 2020]",
Parser.parse("deadline return book /by 6th June 2020").toString());
}

@Test
public void parse_event_event() throws Exception {
assertEquals("EVENT [meeting, time]",
Parser.parse("event meeting /at time").toString());
}

@Test
public void parse_caseInsensitive_success() throws Exception {
assertEquals("DEADLINE [case, insensitive]",
Parser.parse("dEaDlInE case /by insensitive").toString());
}

@Test
public void parse_doneBadInteger_exceptionThrown() {
try {
assertEquals("DONE 3", Parser.parse("done three").toString());
fail();
} catch (Exception e) {
assertEquals("Sorry, that is not a valid task.", e.getMessage());
}
}

@Test
public void parse_deleteNoNextScannerToken_exceptionThrown() {
try {
assertEquals("DELETE", Parser.parse("delete").toString());
fail();
} catch (Exception e) {
assertEquals("Sorry, that is not a valid task.", e.getMessage());
}
}

@Test
public void parse_todoNoNextScannerToken_exceptionThrown() {
try {
assertEquals("TODO", Parser.parse("todo").toString());
fail();
} catch (Exception e) {
assertEquals("Sorry, the description of a task cannot be empty.", e.getMessage());
}
}

@Test
public void parse_deadlineNoNextScannerToken_exceptionThrown() {
try {
assertEquals("DEADLINE", Parser.parse("deadline").toString());
fail();
} catch (Exception e) {
assertEquals("Sorry, the description of a task cannot be empty.", e.getMessage());
}
}

@Test
public void parse_deadlineNoBySubstring_exceptionThrown() {
try {
assertEquals("DEADLINE [do this, tonight]",
Parser.parse("deadline do this tonight").toString());
fail();
} catch (Exception e) {
assertEquals("Sorry, the description of a Deadline must be in this format:\n" +
"\tdeadline [task name] /by [deadline]", e.getMessage());
}
}

@Test
public void parse_deadlineWrongBySubstring_exceptionThrown() {
try {
assertEquals("DEADLINE [do this, tonight]",
Parser.parse("deadline do this by tonight").toString());
fail();
} catch (Exception e) {
assertEquals("Sorry, the description of a Deadline must be in this format:\n" +
"\tdeadline [task name] /by [deadline]", e.getMessage());
}
}

@Test
public void parse_deadlineFakeBySubstring_success() throws Exception {
assertEquals("DEADLINE [swing by Mom's, tomorrow]",
Parser.parse("deadline swing by Mom's /by tomorrow").toString());
}

@Test
public void parse_eventNoNextScannerToken_exceptionThrown() {
try {
assertEquals("EVENT", Parser.parse("event").toString());
fail();
} catch (Exception e) {
assertEquals("Sorry, the description of a task cannot be empty.", e.getMessage());
}
}

@Test
public void parse_eventNoAtSubstring_exceptionThrown() {
try {
assertEquals("EVENT [do this, 6pm]",
Parser.parse("event do this 6pm").toString());
fail();
} catch (Exception e) {
assertEquals("Sorry, the description of an Event must be in this format:\n" +
"\tevent [task name] /at [time]", e.getMessage());
}
}

@Test
public void parse_eventWrongAtSubstring_exceptionThrown() {
try {
assertEquals("EVENT [do this, 6pm]",
Parser.parse("event do this at 6pm").toString());
fail();
} catch (Exception e) {
assertEquals("Sorry, the description of an Event must be in this format:\n" +
"\tevent [task name] /at [time]", e.getMessage());
}
}

@Test
public void parse_eventFakeAtSubstring_success() throws Exception {
assertEquals("EVENT [stay at home, all times]",
Parser.parse("event stay at home /at all times").toString());
}
}

0 comments on commit 03cc1c6

Please sign in to comment.