Skip to content

Commit

Permalink
Merge branch 'branch-A-JUnit'
Browse files Browse the repository at this point in the history
  • Loading branch information
marquestye committed Sep 1, 2023
2 parents d383b69 + c8334f2 commit 1e1ee96
Show file tree
Hide file tree
Showing 9 changed files with 291 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/main/java/duck/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

public class Parser {
private static DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
private static DateTimeFormatter fileDateFormatter = DateTimeFormatter.ofPattern("MMM dd yyyy");
public static DateTimeFormatter fileDateFormatter = DateTimeFormatter.ofPattern("MMM dd yyyy");

public static Command parse(String input) throws DuckException {
String[] splitInput = input.trim().split(" ", 2);
Expand Down Expand Up @@ -80,7 +80,7 @@ private static DeadlineTask parseDeadline(String dataString) throws DuckExceptio
} catch (StringIndexOutOfBoundsException e) {
throw new DuckException("Invalid todo task.");
} catch (DateTimeParseException e) {
throw new DuckException("Please follow the dd/mm/yyyy format for dates.");
throw new DuckException("Please follow the dd/mm/yyyy format.");
} catch (ArrayIndexOutOfBoundsException e) {
throw new DuckException("Please follow the /by format.");
}
Expand Down Expand Up @@ -110,9 +110,9 @@ public static Task parseFromFile(String fileLine) throws DuckException{
case 'T':
return TodoTask.parse(fileLine);
case 'D':
return DeadlineTask.parse(fileLine, fileDateFormatter);
return DeadlineTask.parse(fileLine);
case 'E':
return EventTask.parse(fileLine, fileDateFormatter);
return EventTask.parse(fileLine);
default:
throw new DuckException("Invalid file data.");
}
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/duck/task/DeadlineTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.time.format.DateTimeFormatter;

import duck.DuckException;
import duck.Parser;

public class DeadlineTask extends Task {
LocalDate deadline;
Expand All @@ -29,8 +30,17 @@ public String toString() {
return "[D]" + super.toString() + " (by: " + formatDeadline() + ")";
}

@Override
public boolean equals(Object obj) {
if (obj instanceof DeadlineTask) {
DeadlineTask other = (DeadlineTask) obj;
return super.equals(other) && deadline.equals(other.deadline);
}
return false;
}


public static DeadlineTask parse(String fileLine, DateTimeFormatter dateFormatter) throws DuckException {
public static DeadlineTask parse(String fileLine) throws DuckException {

// Finding isDone
boolean isDone = fileLine.charAt(1) == '1';
Expand All @@ -43,7 +53,7 @@ public static DeadlineTask parse(String fileLine, DateTimeFormatter dateFormatte
// Finding deadline
int secondSlashIndex = fileLine.indexOf("/", slashIndex + 1); // The index of the next slash
String deadlineString = fileLine.substring(secondSlashIndex + 1);
LocalDate deadline = LocalDate.parse(deadlineString, dateFormatter);
LocalDate deadline = LocalDate.parse(deadlineString, Parser.fileDateFormatter);

return new DeadlineTask(name, isDone, deadline);
}
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/duck/task/EventTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.time.format.DateTimeFormatter;

import duck.DuckException;
import duck.Parser;

public class EventTask extends Task {
LocalDate start;
Expand Down Expand Up @@ -37,7 +38,16 @@ public String toString() {
return "[E]" + super.toString() + " (from: " + formatStart() + " to " + formatEnd() + ")";
}

public static EventTask parse(String fileLine, DateTimeFormatter dateFormatter) throws DuckException {
@Override
public boolean equals(Object obj) {
if (obj instanceof EventTask) {
EventTask other = (EventTask) obj;
return super.equals(other) && start.equals(other.start) && end.equals(other.end);
}
return false;
}

public static EventTask parse(String fileLine) throws DuckException {

// Finding isDone
boolean isDone = fileLine.charAt(1) == '1';
Expand All @@ -50,12 +60,12 @@ public static EventTask parse(String fileLine, DateTimeFormatter dateFormatter)
// Finding start
int secondSlashIndex = fileLine.indexOf("/", slashIndex + 1); // The index of the next slash
String startString = fileLine.substring(secondSlashIndex + 1, secondSlashIndex + 12);
LocalDate start = LocalDate.parse(startString, dateFormatter);
LocalDate start = LocalDate.parse(startString, Parser.fileDateFormatter);

// Finding end
int thirdSlashIndex = fileLine.indexOf("/", secondSlashIndex + 1); // The index of the next slash
String endString = fileLine.substring(thirdSlashIndex + 1);
LocalDate end = LocalDate.parse(endString, dateFormatter);
LocalDate end = LocalDate.parse(endString, Parser.fileDateFormatter);

return new EventTask(name, isDone, start, end);
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/duck/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,13 @@ public String toString() {
String str = "[" + done + "] " + name;
return str;
}

@Override
public boolean equals(Object other) {
if (other instanceof Task) {
Task otherTask = (Task) other;
return this.name.equals(otherTask.name) && this.isDone == otherTask.isDone;
}
return false;
}
}
70 changes: 70 additions & 0 deletions src/test/java/duck/ParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package duck;

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_unknownCommand_exceptionThrown() {
try {
Parser.parse("foo bar baz");
fail();
} catch (Exception e) {
assertEquals("Im sorry, I don't know what that means.", e.getMessage());
}
}

@Test
public void parse_markInvalidIndex_exceptionThrown() {
try {
Parser.parse("mark abc");
fail();
} catch (Exception e) {
assertEquals("Please enter a valid task number.", e.getMessage());
}
}

@Test
public void parse_unmarkInvalidIndex_exceptionThrown() {
try {
Parser.parse("unmark abc");
fail();
} catch (Exception e) {
assertEquals("Please enter a valid task number.", e.getMessage());
}
}

@Test
public void parse_deleteInvalidIndex_exceptionThrown() {
try {
Parser.parse("delete abc");
fail();
} catch (Exception e) {
assertEquals("Please enter a valid task number.", e.getMessage());
}
}

@Test
public void parse_deadlineInvalidDateFormat_exceptionThrown() {
// Invalid date format
try {
Parser.parse("deadline abc /by 01-09-2023");
fail();
} catch (Exception e) {
assertEquals("Please follow the dd/mm/yyyy format.", e.getMessage());
}
}

@Test
public void parse_eventInvalidDateFormat_exceptionThrown() {
// Invalid date format
try {
Parser.parse("event abc /from 01-09-2023 /to 01-10-2023");
fail();
} catch (Exception e) {
assertEquals("Please follow the dd/mm/yyyy format.", e.getMessage());
}
}
}
40 changes: 40 additions & 0 deletions src/test/java/duck/task/DeadlineTaskTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package duck.task;

import org.junit.jupiter.api.Test;

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

import java.time.LocalDate;

public class DeadlineTaskTest {
@Test
public void stringify_success() {
DeadlineTask task = new DeadlineTask("return book", false, LocalDate.parse("2023-09-01"));
assertEquals("D011/return book11/Sep 01 2023", task.stringify());

task = new DeadlineTask("submit application", true, LocalDate.parse("2023-10-01"));
assertEquals("D118/submit application11/Oct 01 2023", task.stringify());
}

@Test
public void parse_success() {
try {
DeadlineTask task = new DeadlineTask("return book", false, LocalDate.parse("2023-09-01"));
String taskString = task.stringify();
Task parsedTask = DeadlineTask.parse(taskString);
assertEquals(task, parsedTask);
} catch (Exception e) {
fail();
}
}

@Test
public void toString_success() {
DeadlineTask task = new DeadlineTask("return book", false, LocalDate.parse("2023-09-01"));
assertEquals("[D][ ] return book (by: Sep 01 2023)", task.toString());

task = new DeadlineTask("submit application", true, LocalDate.parse("2023-10-01"));
assertEquals("[D][X] submit application (by: Oct 01 2023)", task.toString());
}
}
40 changes: 40 additions & 0 deletions src/test/java/duck/task/EventTaskTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package duck.task;

import org.junit.jupiter.api.Test;

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

import java.time.LocalDate;

public class EventTaskTest {
@Test
public void stringify_success() {
EventTask task = new EventTask("career fair", false, LocalDate.parse("2023-08-29"), LocalDate.parse("2023-08-30"));
assertEquals("E011/career fair11/Aug 29 202311/Aug 30 2023", task.stringify());

task = new EventTask("TI 23", true, LocalDate.parse("2023-10-12"), LocalDate.parse("2023-10-29"));
assertEquals("E15/TI 2311/Oct 12 202311/Oct 29 2023", task.stringify());
}

@Test
public void parse_success() {
try {
EventTask task = new EventTask("career fair", false, LocalDate.parse("2023-08-29"), LocalDate.parse("2023-08-30"));
String taskString = task.stringify();
Task parsedTask = EventTask.parse(taskString);
assertEquals(task, parsedTask);
} catch (Exception e) {
fail();
}
}

@Test
public void toString_success() {
EventTask task = new EventTask("career fair", false, LocalDate.parse("2023-08-29"), LocalDate.parse("2023-08-30"));
assertEquals("[E][ ] career fair (from: Aug 29 2023 to Aug 30 2023)", task.toString());

task = new EventTask("TI 23", true, LocalDate.parse("2023-10-12"), LocalDate.parse("2023-10-29"));
assertEquals("[E][X] TI 23 (from: Oct 12 2023 to Oct 29 2023)", task.toString());
}
}
65 changes: 65 additions & 0 deletions src/test/java/duck/task/TaskListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package duck.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 TaskListTest {

@Test
public void add_validTask_success() {
TaskList taskList = new TaskList();
assertEquals(0, taskList.getTaskCount());

taskList.add(new TodoTask("test", false));
assertEquals(1, taskList.getTaskCount());

taskList.add(new TodoTask("test", false));
assertEquals(2, taskList.getTaskCount());
}

@Test
public void getTask_invalidIndex_exceptionThrown() {
TaskList taskList = new TaskList();
try {
taskList.getTask(1);
fail();
} catch (Exception e) {
assertEquals("Error - invalid task number.", e.getMessage());
}
}

@Test
public void mark_invalidIndex_exceptionThrown() {
TaskList taskList = new TaskList();
try {
taskList.mark(1);
fail();
} catch (Exception e) {
assertEquals("Error - invalid task number.", e.getMessage());
}
}

@Test
public void unmark_invalidIndex_exceptionThrown() {
TaskList taskList = new TaskList();
try {
taskList.mark(1);
fail();
} catch (Exception e) {
assertEquals("Error - invalid task number.", e.getMessage());
}
}

@Test
public void delete_invalidIndex_exceptionThrown() {
TaskList taskList = new TaskList();
try {
taskList.delete(1);
fail();
} catch (Exception e) {
assertEquals("Error - invalid task number.", e.getMessage());
}
}
}
38 changes: 38 additions & 0 deletions src/test/java/duck/task/TodoTaskTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package duck.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 TodoTaskTest {
@Test
public void stringify_success() {
TodoTask task = new TodoTask("do laundry", false);
assertEquals("T010/do laundry", task.stringify());

task = new TodoTask("finish homework", true);
assertEquals("T115/finish homework", task.stringify());
}

@Test
public void parse_success() {
try {
TodoTask task = new TodoTask("do laundry", false);
String taskString = task.stringify();
Task parsedTask = TodoTask.parse(taskString);
assertEquals(task, parsedTask);
} catch (Exception e) {
fail();
}
}

@Test
public void toString_success() {
TodoTask task = new TodoTask("do laundry", false);
assertEquals("[T][ ] do laundry", task.toString());

task = new TodoTask("finish homework", true);
assertEquals("[T][X] finish homework", task.toString());
}
}

0 comments on commit 1e1ee96

Please sign in to comment.