Skip to content

Commit

Permalink
Test Parser and Storage using JUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestlim8 committed Aug 24, 2020
1 parent 033440c commit 519c264
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/main/java/command/DeadlineCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import task.Task;

import java.time.LocalDateTime;
import java.util.Objects;

public class DeadlineCommand extends Command {
String taskName;
Expand All @@ -25,4 +26,13 @@ public DeadlineCommand(String taskName, LocalDateTime deadline) {
this.deadline = deadline;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DeadlineCommand that = (DeadlineCommand) o;
return Objects.equals(taskName, that.taskName) &&
Objects.equals(deadline, that.deadline);
}

}
12 changes: 11 additions & 1 deletion src/main/java/command/TodoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import task.Task;
import task.TodoTask;

import java.util.Objects;

public class TodoCommand extends Command {
String todo;

Expand All @@ -22,4 +24,12 @@ public TodoCommand(String todo) {
this.todo = todo;
}

}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TodoCommand that = (TodoCommand) o;
return Objects.equals(todo, that.todo);
}

}
10 changes: 10 additions & 0 deletions src/main/java/task/DeadlineTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

public class DeadlineTask extends Task {
private LocalDateTime deadline;
Expand All @@ -23,4 +24,13 @@ public LocalDateTime getDeadline() {
public String toString() {
return "[D]" + super.toString() + " (by: " + this.deadline.format(DateTimeFormatter.ofPattern("MMM dd yyyy HHmm")) + ")";
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DeadlineTask that = (DeadlineTask) o;
return Objects.equals(deadline, that.deadline);
}

}
14 changes: 14 additions & 0 deletions src/main/java/task/TodoTask.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package task;

import command.DeadlineCommand;

import java.util.Objects;

public class TodoTask extends Task {
public TodoTask(String name) {
super(name);
Expand All @@ -11,4 +15,14 @@ public TodoTask(String name, int hasCompleted) {
public String toString() {
return "[T]" + super.toString();
}


@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TodoTask that = (TodoTask) o;
return Objects.equals(this.getName(), that.getName()) &&
Objects.equals(this.getHasCompleted(), that.getHasCompleted());
}
}
37 changes: 37 additions & 0 deletions src/test/java/ParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import command.DeadlineCommand;
import command.TodoCommand;
import duke.Parser;
import exception.DukeException;
import exception.InvalidDateException;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;

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

public class ParserTest {

@Test
public void parseDateTest1() throws InvalidDateException {
assertEquals(Parser.parseDate("12/12/2019 0910"), LocalDateTime.of(2019, 12, 12, 9, 10));
}

@Test
public void parseDateTest2() throws InvalidDateException {
assertEquals(Parser.parseDate("2/5/2020 1700"), LocalDateTime.of(2020, 5, 2, 17, 0));
}

@Test
public void parseTest1() throws DukeException {
assertEquals(Parser.parse("todo Eat lunch"), new TodoCommand("Eat lunch"));
}

@Test
public void parseTest2() throws DukeException {
assertEquals(Parser.parse("deadline finish assignment /by 12/12/2019 0910"),
new DeadlineCommand("finish assignment", LocalDateTime.of(2019, 12, 12, 9, 10)));
}



}
61 changes: 61 additions & 0 deletions src/test/java/StorageTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import duke.Storage;
import duke.TaskList;
import exception.DukeException;
import org.junit.jupiter.api.Test;
import task.DeadlineTask;
import task.Task;
import task.TodoTask;

import java.io.File;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
public class StorageTest {

@Test
public void TestReadValidFile() throws DukeException {
assertEquals(new Storage("data/duke.txt").readFile(), new File("data/duke.txt"));
}

@Test
public void TestLoad() throws DukeException {
List<Task> arrayList = new Storage("data/duke.txt").load();
assertEquals(arrayList.size(), 1);
assertEquals(arrayList.get(0), new TodoTask("Wash Clothes", 1));
}

// Test data/duke.txt if it doesn't exist in test/data/duke.txt
@Test
public void TestLoadEmptyDirectory() throws DukeException {
assertEquals(new Storage("data/dukeEmpty.txt").load(), new ArrayList<>());
}


@Test
public void TestReadLine() throws DukeException {
assertEquals(new Storage("").readLine("D | 0 | tasking | 2020-02-02T18:00"),
new DeadlineTask("tasking", LocalDateTime.of(2020,2,2,18,0)));
}

@Test
public void TestSaveTask() throws DukeException {
Storage testStorage = new Storage("data/saveTask.txt");
testStorage.load();
TaskList taskList = new TaskList();

taskList.addTask(new TodoTask("Wash Clothes", 1));
taskList.addTask(new TodoTask("Do tutorial", 0));

testStorage.saveTasks(taskList);

List<Task> arrayList = testStorage.load();
assertEquals(arrayList.size(), 2);
assertEquals(arrayList.get(0), new TodoTask("Wash Clothes", 1));
assertEquals(arrayList.get(1), new TodoTask("Do tutorial", 0));


}

}

0 comments on commit 519c264

Please sign in to comment.