Skip to content

Commit

Permalink
Add more automated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
A1WAYSD committed Sep 19, 2023
1 parent b6808a5 commit 43574df
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 29 deletions.
3 changes: 2 additions & 1 deletion src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public String parse(String... inputs) throws DukeException {
boolean isDone = markMatcher.group(1).equals("mark");
return this.tasks.markTask(taskIndex, isDone);
} else if (taskMatcher.matches()) {
return this.tasks.addTask(TaskType.valueOf(taskMatcher.group(1).toUpperCase()), taskMatcher.group(2).trim());
return this.tasks.addTask(TaskType.valueOf(taskMatcher.group(1).toUpperCase()),
taskMatcher.group(2).trim());
} else if (repeat.equals("list") || repeat.equals("List")) {
return this.tasks.getTasks();
} else if (repeat.equals("reminder")) {
Expand Down
62 changes: 34 additions & 28 deletions src/main/java/duke/task/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,35 @@ public String addTask(TaskType taskType, String description) throws DukeExceptio
+ size + taskInTotal + "\n\"Be here now.\"";
}

private Task addTask(String description) {
Pattern pattern = Pattern.compile("(TODO|DEADLINE|EVENT) \\| (0|1) \\| (.+)");
Matcher matcher = pattern.matcher(description);
if (!matcher.find()) {
return null;
}
TaskType taskType = TaskType.valueOf(matcher.group(1));
boolean isDone = matcher.group(2).equals("1");
String taskDescription = matcher.group(3);
switch (taskType) {
case TODO:
return new ToDo(taskDescription, isDone);
case DEADLINE:
Matcher deadlineMatcher = Pattern.compile("(.+) \\| (.+)").matcher(taskDescription);
if (!deadlineMatcher.find()) {
return null;
}
return new Deadline(deadlineMatcher.group(1), deadlineMatcher.group(2), isDone);
case EVENT:
Matcher eventMatcher = Pattern.compile("(.+) \\| (.+) \\| (.+)").matcher(taskDescription);
if (!eventMatcher.find()) {
return null;
}
return new Event(eventMatcher.group(1), eventMatcher.group(2), eventMatcher.group(3), isDone);
default:
return null;
}
}

/**
* Returns the string representation of tasks in a list.
*/
Expand Down Expand Up @@ -187,6 +216,11 @@ public int compare(Deadline d1, Deadline d2) {
return result + "\"One thing at a time.\"";
}

/**
* Adds tasks from the file.
* @param file
* @throws Exception
*/
public void addTasks(File file) throws Exception {
Scanner s = new Scanner(file);
while (s.hasNext()) {
Expand All @@ -198,32 +232,4 @@ public void addTasks(File file) throws Exception {
}
}

private Task addTask(String description) {
Pattern pattern = Pattern.compile("(TODO|DEADLINE|EVENT) \\| (0|1) \\| (.+)");
Matcher matcher = pattern.matcher(description);
if (!matcher.find()) {
return null;
}
TaskType taskType = TaskType.valueOf(matcher.group(1));
boolean isDone = matcher.group(2).equals("1");
String taskDescription = matcher.group(3);
switch (taskType) {
case TODO:
return new ToDo(taskDescription, isDone);
case DEADLINE:
Matcher deadlineMatcher= Pattern.compile("(.+) \\| (.+)").matcher(taskDescription);
if (!deadlineMatcher.find()) {
return null;
}
return new Deadline(deadlineMatcher.group(1), deadlineMatcher.group(2), isDone);
case EVENT:
Matcher eventMatcher = Pattern.compile("(.+) \\| (.+) \\| (.+)").matcher(taskDescription);
if (!eventMatcher.find()) {
return null;
}
return new Event(eventMatcher.group(1), eventMatcher.group(2), eventMatcher.group(3), isDone);
default:
return null;
}
}
}
19 changes: 19 additions & 0 deletions src/test/java/duke/task/DeadlineTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package duke.task;

import org.junit.jupiter.api.Test;

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

public class DeadlineTest {
@Test
public void deadline_invalidDate_exceptionThrown() {
try {
assertEquals("deadline deadline (by: Oct 12 2020)", new Deadline("deadline", "2020-10-12"));
fail();
} catch (Exception e) {
assertEquals("date is before today", e.getMessage());
}
}

}

0 comments on commit 43574df

Please sign in to comment.