Skip to content

Commit

Permalink
Add JUnit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pratham31012002 committed Sep 1, 2022
1 parent a23bd57 commit fc2a6ec
Show file tree
Hide file tree
Showing 14 changed files with 196 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/pluto/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.util.HashSet;

public class Parser {
private enum Type {
enum Type {
TODO,
DEADLINE,
EVENT,
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/pluto/command/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,12 @@ public void execute(TaskList tasks, Ui ui, Storage storage) throws PlutoExceptio
}
}

@Override
public boolean equals(Object o) {
if (o instanceof AddCommand) {
AddCommand other = (AddCommand) o;
return this.t.equals(other.t);
}
return false;
}
}
9 changes: 9 additions & 0 deletions src/main/java/pluto/command/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,13 @@ public void execute(TaskList tasks, Ui ui, Storage storage) throws PlutoExceptio
}

}

@Override
public boolean equals(Object o) {
if (o instanceof DeleteCommand) {
DeleteCommand other = (DeleteCommand) o;
return this.idx == other.idx;
}
return false;
}
}
8 changes: 8 additions & 0 deletions src/main/java/pluto/command/ExitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ public boolean isExit() {
public void execute(TaskList tasks, Ui ui, Storage storage) {
ui.print("\tSee You Later!");
}

@Override
public boolean equals(Object o) {
if (o instanceof ExitCommand) {
return true;
}
return false;
}
}
8 changes: 8 additions & 0 deletions src/main/java/pluto/command/ListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ public void execute(TaskList tasks, Ui ui, Storage storage) {
}
}

@Override
public boolean equals(Object o) {
if (o instanceof ListCommand) {
return true;
}
return false;
}

}
9 changes: 9 additions & 0 deletions src/main/java/pluto/command/ShowCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,13 @@ public void execute(TaskList tasks, Ui ui, Storage storage) throws PlutoExceptio
}
}

@Override
public boolean equals(Object o) {
if (o instanceof ShowCommand) {
ShowCommand other = (ShowCommand) o;
return this.date.equals(other.date);
}
return false;
}

}
9 changes: 9 additions & 0 deletions src/main/java/pluto/command/UpdateStatusCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,13 @@ public void execute(TaskList tasks, Ui ui, Storage storage) throws PlutoExceptio
}
}

@Override
public boolean equals(Object o) {
if (o instanceof UpdateStatusCommand) {
UpdateStatusCommand other = (UpdateStatusCommand) o;
return this.idx == other.idx && this.isDone == other.isDone;
}
return false;
}

}
11 changes: 11 additions & 0 deletions src/main/java/pluto/task/Deadline.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package pluto.task;

import pluto.command.AddCommand;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
Expand Down Expand Up @@ -28,4 +30,13 @@ public String toFile() {
public LocalDate getDateMaybe() {
return by.toLocalDate();
}

@Override
public boolean equals(Object o) {
if (o instanceof Deadline) {
Deadline other = (Deadline) o;
return this.by.equals(other.by) && this.description.equals(other.description) && this.isDone == other.isDone;
}
return false;
}
}
9 changes: 9 additions & 0 deletions src/main/java/pluto/task/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,13 @@ public String toFile() {
public LocalDate getDateMaybe() {
return at.toLocalDate();
}

@Override
public boolean equals(Object o) {
if (o instanceof Event) {
Event other = (Event) o;
return this.at.equals(other.at) && this.description.equals(other.description) && this.isDone == other.isDone;
}
return false;
}
}
1 change: 1 addition & 0 deletions src/main/java/pluto/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ public String getDateTime(LocalDateTime dt) {
}

public abstract LocalDate getDateMaybe();

}
9 changes: 9 additions & 0 deletions src/main/java/pluto/task/Todo.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,13 @@ public String toFile() {
public LocalDate getDateMaybe() {
return null;
}

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

import org.junit.jupiter.api.Test;
import pluto.command.AddCommand;
import pluto.task.Event;

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

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

public class ParserTest {
@Test
public void parseTask_invalidDateFormat_exceptionThrown() {
try {
Parser.parseTask("join meeting /by 04/05/2022", Parser.Type.DEADLINE);
fail(); // the test should not reach this line
} catch (Exception e) {
assertEquals("\tOOPS!!! dd-MM-yyyy HHmm date format required.", e.getMessage());
}
}

@Test
public void parseTask_validFormat_success() throws PlutoException {
LocalDateTime date = LocalDateTime.parse("04-05-2022 1800", DateTimeFormatter.ofPattern("dd-MM-yyyy HHmm"));
assertEquals(new AddCommand(new Event("join meeting", date)),
Parser.parseTask("join meeting /at 04-05-2022 1800", Parser.Type.EVENT));
}

@Test
public void isOnlyCommand_onlyCommand_exceptionThrown() {
try {
Parser.isOnlyCommand("todo");
fail(); // the test should not reach this line
} catch (Exception e) {
assertEquals("\tOOPS!!! The description of todo cannot be empty.", e.getMessage());
}
}
}
48 changes: 48 additions & 0 deletions src/test/java/pluto/TaskListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package pluto;

import org.junit.jupiter.api.Test;
import pluto.task.Task;
import pluto.task.Todo;

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

public class TaskListTest {
@Test
public void deleteTask_validIndex_success() throws PlutoException {
Task t1 = new Todo("test task1");
Task t2 = new Todo("test task2");
Task t3 = new Todo("test task3");
TaskList tasks = new TaskList();
tasks.addTask(t1);
tasks.addTask(t2);
tasks.addTask(t3);
assertEquals(t3, tasks.deleteTask(2));
}
@Test
public void deleteTask_invalidIndex_exceptionThrown() {
try {
Task t = new Todo("test task");
TaskList tasks = new TaskList();
tasks.addTask(t);
tasks.addTask(t);
tasks.addTask(t);
tasks.deleteTask(5);
fail(); // the test should not reach this line
} catch (Exception e) {
assertEquals("\tOOPS!!! Valid index required.", e.getMessage());
}
}
@Test
public void markTask_validIndex_success() throws PlutoException {
Task t1 = new Todo("test task1");
Task t2 = new Todo("test task2");
Task t3 = new Todo("test task3");
TaskList tasks = new TaskList();
tasks.addTask(t1);
tasks.addTask(t2);
tasks.addTask(t3);
tasks.markTask(1, true);
assertEquals(t2.getStatusIcon(), "X");
}
}
26 changes: 26 additions & 0 deletions src/test/java/pluto/task/EventTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package pluto.task;

import org.junit.jupiter.api.Test;

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

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

public class EventTest {
@Test
public void toFile_returnsTasktoFileFormat() {
LocalDateTime date = LocalDateTime.parse("04-05-2022 1800", DateTimeFormatter.ofPattern("dd-MM-yyyy HHmm"));
Event e = new Event("join meeting", date);
e.markAsDone();
assertEquals(e.toFile(), "E | 1 | join meeting | 04-05-2022 1800");
}

@Test
public void getDateMaybe_returnsLocalDate() {
LocalDateTime date = LocalDateTime.parse("04-05-2022 1800", DateTimeFormatter.ofPattern("dd-MM-yyyy HHmm"));
Deadline d = new Deadline("join meeting", date);
assertEquals(d.getDateMaybe(), LocalDate.of(2022, 5,4));
}
}

0 comments on commit fc2a6ec

Please sign in to comment.