Skip to content

Commit

Permalink
Add JUnit tests for Parser and TaskList, edit exception throwing
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrylchong committed Aug 25, 2020
1 parent a12f533 commit 64ed090
Show file tree
Hide file tree
Showing 16 changed files with 243 additions and 44 deletions.
8 changes: 4 additions & 4 deletions src/main/java/duke/DukeException.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public static DukeException emptyList() {
}

public static DukeException typeMismatch(String command) {
return new DukeException("Error! Integer should follow '" + command + "' duke.command.");
return new DukeException("Error! Integer should follow '" + command + "' command.");
}

public static DukeException outOfBounds() {
return new DukeException("Error! Enter a valid duke.task number.");
return new DukeException("Error! Enter a valid task number.");
}

public static DukeException emptyDesc(String taskType) {
Expand All @@ -26,7 +26,7 @@ public static DukeException unknownCommand() {
}

public static DukeException missingTask() {
return new DukeException("Error! No duke.task description provided.");
return new DukeException("Error! No task description provided.");
}

public static DukeException missingTime(String byOrAt) {
Expand All @@ -46,7 +46,7 @@ public static DukeException pastDateTime() {
}

public static DukeException wrongDueInFormat() {
return new DukeException("Error! 'due in' duke.command is in the wrong format.");
return new DukeException("Error! 'due in' command is in the wrong format.");
}

public static DukeException loadingError(String path) {
Expand Down
32 changes: 18 additions & 14 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ private static Command parseDone(String input) throws DukeException {
}
} catch (NumberFormatException e) {
throw(DukeException.typeMismatch("done"));
} catch (IndexOutOfBoundsException e) {
throw(DukeException.outOfBounds());
} catch (DukeException e) {
throw(e);
}
}

Expand All @@ -39,8 +39,8 @@ private static Command handleToDo(String input) throws DukeException {
private static Command handleDeadline(String input) throws DukeException {
String basePattern = "(deadline\\s)(.+)";
String almostCompletePattern = "(deadline\\s)(.+)\\s(/by\\s)(.+)";
String datePattern = "(\\d\\d\\d\\d-[01]\\d-[0123]\\d)\\s";
String timePattern = "([012]\\d)([012345]\\d)";
String datePattern = "(\\d\\d\\d\\d-\\d\\d-\\d\\d)\\s";
String timePattern = "(\\d\\d)(\\d\\d)";
String completePattern = "(deadline\\s)(.+)\\s(/by\\s)"+ datePattern + timePattern;
String missingTaskPattern = "(deadline\\s)(/by)((\\s(.*))*)";
try {
Expand All @@ -61,8 +61,8 @@ private static Command handleDeadline(String input) throws DukeException {
} else {
throw(DukeException.emptyDesc("deadline"));
}
} catch (DateTimeParseException e) {
throw(DukeException.invalidDateTime());
} catch (DukeException e) {
throw(e);
}
}

Expand Down Expand Up @@ -91,8 +91,8 @@ private static Command handleEvent(String input) throws DukeException {
} else {
throw(DukeException.emptyDesc("event"));
}
} catch (DateTimeParseException e) {
throw(DukeException.invalidDateTime());
} catch (DukeException e) {
throw(e);
}
}

Expand All @@ -113,12 +113,16 @@ private static Command delete(String input) throws DukeException {
}
}

public static LocalDateTime extractDateTime(String input, String completePattern) {
String date = input.replaceAll(completePattern, "$4");
String hours = input.replaceAll(completePattern, "$5");
String minutes = input.replaceAll(completePattern, "$6");
String time = hours + ":" + minutes;
return LocalDateTime.parse(date + "T" + time);
public static LocalDateTime extractDateTime(String input, String completePattern) throws DukeException {
try {
String date = input.replaceAll(completePattern, "$4");
String hours = input.replaceAll(completePattern, "$5");
String minutes = input.replaceAll(completePattern, "$6");
String time = hours + ":" + minutes;
return LocalDateTime.parse(date + "T" + time);
} catch (DateTimeParseException e) {
throw(DukeException.invalidDateTime());
}
}

private static Command handleDueIn(String input) throws DukeException {
Expand Down
44 changes: 25 additions & 19 deletions src/main/java/duke/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,30 @@ public Storage(String filePath) {
this.list = new ArrayList<>();
}

private void createTask(String s) {
Task next;
if (s.startsWith("todo")) {
next = new ToDo(s.substring(5));
} else if (s.startsWith("deadline")) {
String datePattern = "(\\d\\d\\d\\d-\\d\\d-\\d\\d)\\s";
String timePattern = "(\\d\\d)(\\d\\d)";
String pattern = "(deadline\\s)(.+)\\s(/by\\s)"+ datePattern + timePattern;
String task = s.replaceAll(pattern, "$2");
LocalDateTime dateTime = Parser.extractDateTime(s, pattern);
next = new Deadline(task, dateTime);
} else {
String datePattern = "(\\d\\d\\d\\d-\\d\\d-\\d\\d)\\s";
String timePattern = "(\\d\\d)(\\d\\d)";
String pattern = "(event\\s)(.+)\\s(/at\\s)(.+)"+ datePattern + timePattern;
String task = s.replaceAll(pattern, "$2");
LocalDateTime dateTime = Parser.extractDateTime(s, pattern);
next = new Event(task, dateTime);
private void createTask(String s) throws DukeException {
try {
Task next;
if (s.startsWith("todo")) {
next = new ToDo(s.substring(5));
} else if (s.startsWith("deadline")) {
String datePattern = "(\\d\\d\\d\\d-\\d\\d-\\d\\d)\\s";
String timePattern = "(\\d\\d)(\\d\\d)";
String pattern = "(deadline\\s)(.+)\\s(/by\\s)"+ datePattern + timePattern;
String task = s.replaceAll(pattern, "$2");
LocalDateTime dateTime = Parser.extractDateTime(s, pattern);
next = new Deadline(task, dateTime);
} else {
String datePattern = "(\\d\\d\\d\\d-\\d\\d-\\d\\d)\\s";
String timePattern = "(\\d\\d)(\\d\\d)";
String pattern = "(event\\s)(.+)\\s(/at\\s)(.+)"+ datePattern + timePattern;
String task = s.replaceAll(pattern, "$2");
LocalDateTime dateTime = Parser.extractDateTime(s, pattern);
next = new Event(task, dateTime);
}
list.add(next);
} catch (DukeException e) {
throw(e);
}
list.add(next);
}

public ArrayList<Task> load() throws DukeException {
Expand All @@ -62,6 +66,8 @@ public ArrayList<Task> load() throws DukeException {
return list;
} catch (FileNotFoundException e) {
throw DukeException.loadingError(filePath);
} catch (DukeException e) {
throw(e);
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/main/java/duke/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ public TaskList(ArrayList<Task> list) {
this.list = list;
}

public ArrayList<Task> getList() {
return list;
}

public String listContents() throws DukeException {
if (list.size() > 0) {
String text = "Here is your list:";
Expand Down Expand Up @@ -58,9 +54,13 @@ public String delete(int index) {
return task.toString();
}

public String done(int index) {
list.get(index).setDone();
return list.get(index).toString();
public String done(int index) throws DukeException {
try {
list.get(index).setDone();
return list.get(index).toString();
} catch (IndexOutOfBoundsException e) {
throw(DukeException.outOfBounds());
}
}

public String extractListData() {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/duke/command/ByeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException
public boolean isExit() {
return true;
}

@Override
public boolean equals(Object obj) {
return obj instanceof ByeCommand;
}
}
4 changes: 4 additions & 0 deletions src/main/java/duke/command/DeadlineCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException
throw(DukeException.pastDateTime());
}
}
@Override
public boolean equals(Object obj) {
return obj instanceof DeadlineCommand;
}
}
5 changes: 5 additions & 0 deletions src/main/java/duke/command/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException
String task = tasks.delete(index);
ui.say("Deleted this task:\n" + task);
}

@Override
public boolean equals(Object obj) {
return obj instanceof DeleteCommand;
}
}
5 changes: 5 additions & 0 deletions src/main/java/duke/command/DoneCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException
String task = tasks.done(index);
ui.say("Marked this task as done:\n" + task);
}

@Override
public boolean equals(Object obj) {
return obj instanceof DoneCommand;
}
}
5 changes: 5 additions & 0 deletions src/main/java/duke/command/DueInCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException
ui.say(tasks.extractDueTasksDays(time));
}
}

@Override
public boolean equals(Object obj) {
return obj instanceof DueInCommand;
}
}
5 changes: 5 additions & 0 deletions src/main/java/duke/command/EventCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException
throw(DukeException.pastDateTime());
}
}

@Override
public boolean equals(Object obj) {
return obj instanceof EventCommand;
}
}
5 changes: 5 additions & 0 deletions src/main/java/duke/command/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException
"todo <description> - adds a todo duke.task with the given description to the duke.task list";
ui.say(help);
}

@Override
public boolean equals(Object obj) {
return obj instanceof HelpCommand;
}
}
5 changes: 5 additions & 0 deletions src/main/java/duke/command/ListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException
throw(e);
}
}

@Override
public boolean equals(Object obj) {
return obj instanceof ListCommand;
}
}
5 changes: 5 additions & 0 deletions src/main/java/duke/command/ToDoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException
tasks.add(task, LocalDateTime.now(), TaskType.TODO);
ui.say("Added ToDo '" + task + "' to your list!");
}

@Override
public boolean equals(Object obj) {
return obj instanceof ToDoCommand;
}
}
84 changes: 84 additions & 0 deletions src/test/java/duke/ParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package duke;

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

public class ParserTest {

@Test
public void parse_unknownCommand_exceptionThrown() {
try {
Parser.parse("unknown");
} catch (DukeException e) {
assertEquals("Sorry, I don't understand what you just said.", e.getMessage());
}
}

@Test
public void parse_doneWithNoTaskIndex_exceptionThrown() {
try {
Parser.parse("done ");
} catch (DukeException e) {
assertEquals("Error! 'done' description cannot be empty.", e.getMessage());
}
}

@Test
public void parse_doneWithNonIntegerValue_exceptionThrown() {

}

@Test
public void parse_todoWithNoDescription_exceptionThrown() {
try {
Parser.parse("todo ");
} catch (DukeException e) {
assertEquals("Error! 'todo' description cannot be empty.", e.getMessage());
}
}

@Test
public void parse_deadlineWithNoDescription_exceptionThrown() {
try {
Parser.parse("deadline ");
} catch (DukeException e) {
assertEquals("Error! 'deadline' description cannot be empty.", e.getMessage());
}
}

@Test
public void parse_deadlineWithNoTaskDescription_exceptionThrown() {
try {
Parser.parse("deadline /by 2020-12-12 1000");
} catch (DukeException e) {
assertEquals("Error! No task description provided.", e.getMessage());
}
}

@Test
public void parse_deadlineWithNoDateTime_exceptionThrown() {
try {
Parser.parse("deadline this");
} catch (DukeException e) {
assertEquals("Error! '/by' date not found.", e.getMessage());
}
}

@Test
public void parse_deadlineWithWrongDateTimeFormat_exceptionThrown() {
try {
Parser.parse("deadline this /by 2020/12/12 10:00");
} catch (DukeException e) {
assertEquals("Error! Date/time is in the wrong format.", e.getMessage());
}
}

@Test
public void parse_deadlineWithInvalidDateTime_exceptionThrown() {
try {
Parser.parse("deadline this /by 2020-13-12 3300");
} catch (DukeException e) {
assertEquals("Error! Date/time is invalid.", e.getMessage());
}
}
}
35 changes: 35 additions & 0 deletions src/test/java/duke/TaskListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package duke;

import duke.task.Task;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;

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

public class TaskListTest {

@Test
public void listContents_emptyList_exceptionThrown() {
try {
TaskList tasks = new TaskList();
tasks.listContents();
} catch (DukeException e) {
assertEquals("Your list is empty.", e.getMessage());
}
}

@Test
public void listContents_nonEmptyList_success() {
try {
ArrayList<Task> list = new ArrayList<>();
list.add(new ToDoStub("test"));
TaskList tasks = new TaskList(list);
String expected = "Here is your list:\n1.test";
assertEquals(tasks.listContents(), expected);
} catch (DukeException e) {
fail();
}
}
}
Loading

0 comments on commit 64ed090

Please sign in to comment.