Skip to content

Commit

Permalink
Add JUnit tests to test behaviour of ToDo, Deadline, Event, Parser class
Browse files Browse the repository at this point in the history
  • Loading branch information
constancensq committed Aug 27, 2020
1 parent 96b1804 commit f36ad8f
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 3 deletions.
1 change: 0 additions & 1 deletion src/main/java/AddCommand.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import java.io.IOException;
import java.time.format.DateTimeFormatter;

public class AddCommand extends Command {
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import java.io.IOException;

public class Duke {

private Ui ui;
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/DeadlineTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

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

public class DeadlineTest {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime atLocalDate = LocalDateTime.parse("2020-12-12 12:30", formatter);

@Test
public void markAsDone_success() {
Deadline task = new Deadline("proj meeting", atLocalDate);
assertEquals("\u2718", task.getStatusIcon());
task = task.markAsDone();
assertEquals("\u2713", task.getStatusIcon());
}

@Test
public void toString_success() {
Deadline task = new Deadline("proj meeting", atLocalDate);
assertEquals("[D][" + "\u2718" + "] " + "proj meeting (by: Dec 12 2020 12:30 PM)", task.toString());

Deadline doneTask = new Deadline("proj meeting", atLocalDate, true);
assertEquals("[D][" + "\u2713" + "] " + "proj meeting (by: Dec 12 2020 12:30 PM)", doneTask.toString());
}

@Test
public void toTxtFileformat_success() {
Deadline task = new Deadline("proj meeting", atLocalDate);
assertEquals("D | 0 | proj meeting | 2020-12-12 12:30", task.toTxtFileFormat());

Deadline doneTask = new Deadline("proj meeting", atLocalDate, true);
assertEquals("D | 1 | proj meeting | 2020-12-12 12:30", doneTask.toTxtFileFormat());
}
}
40 changes: 40 additions & 0 deletions src/test/java/EventTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

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

public class EventTest {
@Test
public void markAsDone_success() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime byLocalDate = LocalDateTime.parse("2020-12-12 12:30", formatter);
Event task = new Event("proj meeting", byLocalDate);
assertEquals("\u2718", task.getStatusIcon());
task = task.markAsDone();
assertEquals("\u2713", task.getStatusIcon());
}

@Test
public void toString_success() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime byLocalDate = LocalDateTime.parse("2020-12-12 12:30", formatter);
Event task = new Event("proj meeting", byLocalDate);
assertEquals("[E][" + "\u2718" + "] " + "proj meeting (at: Dec 12 2020 12:30 PM)", task.toString());

Event doneTask = new Event("proj meeting", byLocalDate, true);
assertEquals("[E][" + "\u2713" + "] " + "proj meeting (at: Dec 12 2020 12:30 PM)", doneTask.toString());
}

@Test
public void toTxtFileformat_success() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime byLocalDate = LocalDateTime.parse("2020-12-12 12:30", formatter);
Event task = new Event("proj meeting", byLocalDate);
assertEquals("E | 0 | proj meeting | 2020-12-12 12:30", task.toTxtFileFormat());

Event doneTask = new Event("proj meeting", byLocalDate, true);
assertEquals("E | 1 | proj meeting | 2020-12-12 12:30", doneTask.toTxtFileFormat());
}
}
64 changes: 64 additions & 0 deletions src/test/java/ParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import org.junit.jupiter.api.Test;

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

public class ParserTest {
@Test
public void isInteger_stringInteger_success() {
assertEquals(true, Parser.isInteger("2"));
}

@Test
public void isValidFormat_stringInputDate_valid() {
assertEquals(true, Parser.isValidFormat("2020-10-10 12:30"));
}

@Test
public void isValidFormat_stringInputDate_invalid() {
assertEquals(false, Parser.isValidFormat("10th august"));

}

@Test
public void parse_oneWordCommand_success() throws DukeException {
assertEquals(true, Parser.parse("list") instanceof ListCommand);
assertEquals(true, Parser.parse("bye") instanceof ExitCommand);
}

@Test
public void parse_doneCommand_success() throws DukeException {
assertEquals(true, Parser.parse("done 1") instanceof DoneCommand);
assertEquals(true, Parser.parse("done 3") instanceof DoneCommand);
}

@Test
public void parse_InvalidDoneCommand_exceptionThrown() {
try {
assertEquals(true, Parser.parse("done 0") instanceof DoneCommand);
} catch (DukeException e) {
assertEquals("Please enter a valid task number.", e.getMessage());
}
}

@Test
public void parse_deleteCommand_success() throws DukeException {
assertEquals(true, Parser.parse("delete 1") instanceof DeleteCommand);
assertEquals(true, Parser.parse("delete 3") instanceof DeleteCommand);
}

@Test
public void parse_addCommand_success() throws DukeException {
assertEquals(true, Parser.parse("todo study") instanceof AddCommand);
assertEquals(true, Parser.parse("deadline work /by 2020-12-12 12:30") instanceof AddCommand);
assertEquals(true, Parser.parse("event proj /at 2020-12-12 12:30") instanceof AddCommand);
}

@Test
public void parse_invalidCommands_exceptionThrown() {
try {
assertEquals(true, Parser.parse("abcd") instanceof Command);
} catch (DukeException e) {
assertEquals("☹ ERROR: Invalid command provided. Please try again.", e.getMessage());
}
}
}
21 changes: 21 additions & 0 deletions src/test/java/TaskListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

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

public class TaskListTest {

@Test
public void doneTask_intTaskNumber_success() {
List<Task> tmp = new ArrayList<>();
ToDo task = new ToDo("testing");
tmp.add(task);
TaskList tasks = new TaskList(tmp);

assertEquals(false, tasks.getTask(1).isDone);
tasks.doneTask(1);
assertEquals(true, tasks.getTask(1).isDone);
}
}
32 changes: 32 additions & 0 deletions src/test/java/ToDoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import org.junit.jupiter.api.Test;

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

public class ToDoTest {
@Test
public void markAsDone_success() {
ToDo task = new ToDo("testing");
assertEquals("\u2718", task.getStatusIcon());
task = task.markAsDone();
assertEquals("\u2713", task.getStatusIcon());
}

@Test
public void toString_success() {
ToDo task = new ToDo("study at home");
assertEquals("[T][" + "\u2718" + "] " + "study at home", task.toString());

ToDo doneTask = new ToDo("study at home", true);
assertEquals("[T][" + "\u2713" + "] " + "study at home", doneTask.toString());
}

@Test
public void toTxtFileformat_success() {
ToDo task = new ToDo("study at home");
assertEquals("T | 0 | study at home", task.toTxtFileFormat());

ToDo doneTask = new ToDo("study at home", true);
assertEquals("T | 1 | study at home", doneTask.toTxtFileFormat());
}

}

0 comments on commit f36ad8f

Please sign in to comment.