Skip to content

Commit

Permalink
Create JUnit Tests for Parser and TaskList classes, tested with Gradle
Browse files Browse the repository at this point in the history
  • Loading branch information
cheahTJ committed Jan 31, 2024
1 parent bcbccfd commit ea0f4ca
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 10 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ shadowJar {

run{
standardInput = System.in
enableAssertions = true
}
3 changes: 2 additions & 1 deletion src/main/java/pingmebot/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public class Parser {

public Parser (String userInput) {
this.userInput = userInput;
this.words = new ArrayList<>(Arrays.asList(userInput.split(" ")));}
this.words = new ArrayList<>(Arrays.asList(userInput.split(" ")));
}

/*
* Returns a todo object with the task description
Expand Down
1 change: 0 additions & 1 deletion src/main/java/pingmebot/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public void updateTaskToStorage(fileStorage fs) throws myBotException {
} catch (myBotException e) {
throw new myBotException(e.getMessage());
}

}
public void addTask(Task task) {
this.tasks.add(task);
Expand Down
8 changes: 0 additions & 8 deletions src/main/java/pingmebot/fileStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,11 @@ public fileStorage(String filePath) throws myBotException {
this.filePath = filePath;
if (!myFile.exists()) {
try {
if (this.myFile.getParentFile().mkdirs()) {
System.out.println("Directory has been successfully created");
} else {
System.out.println("Error creating directory...");
}

if (this.myFile.createNewFile()) {
System.out.println("File has been successfully created");
} else {
System.out.println("Error creating file...");
}


} catch (IOException e) {
throw new myBotException(e.getMessage());
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/pingmebot/task/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,16 @@ public String updateDeadlineText(int isCompleted) {
return text;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Deadline otherDeadline = (Deadline) obj;
return this.description.equals(otherDeadline.description) && this.by.equals(otherDeadline.by);
}

}
12 changes: 12 additions & 0 deletions src/main/java/pingmebot/task/Events.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ public String updateEventText(int isCompleted) {
text += "event | " + isCompleted + " | " + this.description + " | " + this.start + " | " + this.end;
return text;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Events otherEvent = (Events) obj;
return this.description.equals(otherEvent.description) && this.start.equals(otherEvent.start) && this.end.equals(otherEvent.end);
}
}


14 changes: 14 additions & 0 deletions src/main/java/pingmebot/task/ToDos.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,18 @@ public String updateToDoText(int isCompleted) {
text += "todo | " + isCompleted + " | " + this.description;
return text;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
ToDos otherToDo = (ToDos) obj;
return this.description.equals(otherToDo.description);
}


}
60 changes: 60 additions & 0 deletions src/test/java/pingmebot/ParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package pingmebot;

import org.junit.jupiter.api.Test;
import pingmebot.task.Deadline;
import pingmebot.task.Events;
import pingmebot.task.ToDos;

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

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

public class ParserTest {
@Test
public void todoParserTest() throws myBotException {
String command = "todo project";
Parser parser = new Parser(command);
assertEquals(new ToDos("project"),parser.todoParser());
}

@Test
public void deadlineParserTest() throws myBotException {
String command = "deadline project /by 05/05/2000 1800";
Parser parser = new Parser(command);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/M/yyyy HHmm");
String time = "05/05/2000 1800";
assertEquals(new Deadline("project", LocalDateTime.parse(time, formatter)),parser.deadlineParser());
}

@Test
public void eventParserTest() throws myBotException {
String command = "event project /from 9am /to 8pm";
Parser parser = new Parser(command);
assertEquals(new Events("project", " 9am"," 8pm"),parser.eventsParser());
}

@Test
public void markParserTest() throws myBotException {
String command = "mark 2";
int arbituaryNumOfTask = 3;
Parser parser = new Parser(command);
assertEquals(1, parser.markParser(arbituaryNumOfTask));
}

@Test
public void unmarkParserTest() throws myBotException {
String command = "unmark 2";
int arbituaryNumOfTask = 3;
Parser parser = new Parser(command);
assertEquals(1, parser.markParser(arbituaryNumOfTask));
}

@Test
public void deleteParserTest() throws myBotException {
String command = "delete 2";
int arbituaryNumOfTask = 3;
Parser parser = new Parser(command);
assertEquals(1, parser.markParser(arbituaryNumOfTask));
}
}
124 changes: 124 additions & 0 deletions src/test/java/pingmebot/TaskListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package pingmebot;

import org.junit.jupiter.api.Test;
import pingmebot.task.Task;
import pingmebot.task.ToDos;

import java.io.*;
import java.nio.file.Paths;
import java.util.ArrayList;

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

public class TaskListTest {
@Test
public void getTaskSizeTest() {
ArrayList<Task> tasks = new ArrayList<>();
TaskList tl = new TaskList(tasks);
tasks.add(new ToDos("project 1"));
tasks.add(new ToDos("project 2"));
tasks.add(new ToDos("project 3"));
assertEquals(3, tl.getTaskSize());
}

@Test
public void updateTaskToStorageTest() {
try {
ArrayList<Task> tasks = new ArrayList<>();
tasks.add(new ToDos("project 1"));
tasks.add(new ToDos("project 2"));
TaskList tl = new TaskList(tasks);
String filePath = "./data/dukeData.txt";
// File will always exist since it will be created in the file storage constructor if not found
tl.updateTaskToStorage(new fileStorage(filePath));

String line;
String totalLine = "";
try (BufferedReader reader = new BufferedReader(new FileReader(Paths.get(filePath).toFile()))) {
while ((line = reader.readLine()) != null) {
totalLine += line + "\n";
}
} catch (IOException e) {
fail("Test Failed.");
}

assertEquals("todo | 0 | project 1" + "\n" + "todo | 0 | project 2" + "\n",totalLine);
} catch (myBotException e) {
fail("Test Failed.");
}

}

@Test
public void addTaskTest() {
ArrayList<Task> tasks = new ArrayList<>();
TaskList tl = new TaskList(tasks);
tl.addTask(new ToDos("project"));
assertEquals(1,tl.tasks.size());
}

@Test
public void removeTaskTest() {
ArrayList<Task> tasks = new ArrayList<>();
tasks.add(new ToDos("project"));
TaskList tl = new TaskList(tasks);
// Task number is 0-based
tl.removeTask(0);
assertEquals(0, tl.tasks.size());
}

@Test
public void taskToStringTest() {
ArrayList<Task> tasks = new ArrayList<>();
tasks.add(new ToDos("project"));
TaskList tl = new TaskList(tasks);
assertEquals("[T][ ] project", tl.taskToString(0));
}

@Test
public void taskStatusIconTest() {
ArrayList<Task> tasks = new ArrayList<>();
tasks.add(new ToDos("project"));
TaskList tl = new TaskList(tasks);
assertEquals(" ", tl.taskStatusIcon(0));
}

@Test
public void taskMarkAsDoneTest() {
ArrayList<Task> tasks = new ArrayList<>();
tasks.add(new ToDos("project"));
TaskList tl = new TaskList(tasks);
tl.taskMarkAsDone(0);
assertEquals("[T][X] project", tl.tasks.get(0).toString());
}

@Test
public void taskUncheckTaskTest() {
ArrayList<Task> tasks = new ArrayList<>();
tasks.add(new ToDos("project"));
TaskList tl = new TaskList(tasks);
tl.taskMarkAsDone(0);
tl.taskUncheckTask(0);
assertEquals("[T][ ] project", tl.tasks.get(0).toString());
}

@Test
public void listTaskTest() {
ArrayList<Task> tasks = new ArrayList<>();
tasks.add(new ToDos("project"));
TaskList tl = new TaskList(tasks);

PrintStream originalOut = System.out;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintStream newOut = new PrintStream(outputStream);
System.setOut(newOut);
tl.listTask();
System.setOut(originalOut);
String capturedOutput = outputStream.toString();
assertEquals("1.[T][ ] project" + "\n", capturedOutput);
}



}

0 comments on commit ea0f4ca

Please sign in to comment.