Skip to content

Commit

Permalink
Add JUnit test to Duke,Parser and TaskList
Browse files Browse the repository at this point in the history
  • Loading branch information
rtshkmr committed Aug 26, 2020
1 parent 2ba2f29 commit b2bf031
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 4 deletions.
Binary file modified data/savedData
Binary file not shown.
8 changes: 4 additions & 4 deletions src/main/java/duke/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ public TaskList() {
}

// side effect: create & add task + return response
public String addEntry(String[] parsedOutput, String commandTag) throws DukeException {
public String addEntry(String[] parsedInput, String commandTag) throws DukeException {
switch (commandTag) {
case "T":
ToDo newToDo = new ToDo(parsedOutput[1]);
ToDo newToDo = new ToDo(parsedInput[1]);
this.taskList.add(newToDo);
return newToDo.toString();
case "D":
Deadline newDeadline = Deadline.createDeadline(parsedOutput);
Deadline newDeadline = Deadline.createDeadline(parsedInput);
this.taskList.add(newDeadline);
return newDeadline.toString();
case "E":
Event newEvent = Event.createEvent(parsedOutput);
Event newEvent = Event.createEvent(parsedInput);
this.taskList.add(newEvent);
return newEvent.toString();
default:
Expand Down
Binary file added src/test/data/savedData
Binary file not shown.
19 changes: 19 additions & 0 deletions src/test/java/duke/DukeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package duke;

import org.junit.jupiter.api.Test;

import java.io.IOException;

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

public class DukeTest {
@Test
public void dummyTest(){
assertEquals(2, 2);
}

// @Test
// public void dukeRun() throws IOException {
// new Duke().run();
// }
}
77 changes: 77 additions & 0 deletions src/test/java/duke/ParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package duke;

import duke.enums.Message;
import duke.exception.DukeException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

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

public class ParserTest {

@ParameterizedTest
@ValueSource(strings = {"deadline return book /by 19/02/2020 0900", "event project meeting /at Mon 2-4pm", "todo " +
"join sports club"})
public void parsingCommand_parameterizedTest_validCommandsCreated(String input) throws DukeException {
Parser p = new Parser();
assert (p.parseCommand(input) != null); // successful creation
}

@Test
public void exceptionHandling_insufficientTodoDescription_dukeExceptionReturned() {
String input = "todo";
String expectedMsg = "invalid todo command: do what?";
Parser p = new Parser();
DukeException de = assertThrows(DukeException.class, () -> {
p.parseCommand(input);
});
assertEquals(expectedMsg, de.getMessage());
}

@ParameterizedTest
@ValueSource(strings = {"tod", "blah", })
public void exceptionHanlding_unknownCommand_dukeExceptionReturned(String s) {
String expectedMsg = Message.ERROR_UNKNOWN_CMD.getMsg();
Parser p = new Parser();
DukeException de = assertThrows(DukeException.class, () -> p.parseCommand(s));
assertEquals(de.getMessage(), expectedMsg);
}

@ParameterizedTest
@ValueSource(strings = {"done 4 5 6", "delete 4 5 6"})
public void exceptionHandling_multipleDoneDeleteArguments_dukeExceptionReturned(String s) {
String expectedMsg = Message.ERROR_DONEDELETE_ARGS.getMsg();
Parser p = new Parser();
DukeException de = assertThrows(DukeException.class, () -> p.parseCommand(s));
assertEquals(de.getMessage(), expectedMsg);
}

@ParameterizedTest
@ValueSource(strings = {"deadline", "deadline a"})
public void exceptionHandling_badDeadlineFormat_dukeExceptionReturned(String s) {
String expectedMsg = Message.ERROR_DEADLINE_FORMAT.getMsg();
Parser p = new Parser();
DukeException de = assertThrows(DukeException.class, () -> p.parseCommand(s));
assertEquals(de.getMessage(), expectedMsg);
}

@ParameterizedTest
@ValueSource(strings = {"deadline /by xxx", "deadline /by Sunday"})
public void exceptionHandling_deadlineNoDateDescription_dukeExceptionReturned(String s) {
String expectedMsg = Message.ERROR_DEADLINE_DESC.getMsg();
Parser p = new Parser();
DukeException de = assertThrows(DukeException.class, () -> p.parseCommand(s));
assertEquals(de.getMessage(), expectedMsg);
}

@ParameterizedTest
@ValueSource(strings = {"event /by Sunday"})
public void exceptionHandling_badEventFormat_dukeExceptionReturned(String s) {
String expectedMsg = Message.ERROR_EVENT_FORMAT.getMsg();
Parser p = new Parser();
DukeException de = assertThrows(DukeException.class, () -> p.parseCommand(s));
assertEquals(de.getMessage(), expectedMsg);
}
}
5 changes: 5 additions & 0 deletions src/test/java/duke/StorageTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package duke;

public class StorageTest {

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

import duke.exception.DukeException;
import org.junit.jupiter.api.Test;

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

public class TaskListTest {


public static TaskList testTaskList = new TaskList();

private static void initTest() throws DukeException {
String[] parsedInput1 = {"T", "kill me now"};
String[] parsedInput2 = {"T", "i want ice cream"};
String commandTag = "T";
testTaskList.addEntry(parsedInput1, commandTag);
testTaskList.addEntry(parsedInput2, commandTag);
}

@Test
public void modifyTaskList_addEntryTodo_increaseTaskCount() throws DukeException {
TaskListTest.initTest();
String[] parsedInput1 = {"T", "happy thoughts"};
assertEquals(testTaskList.addEntry(parsedInput1, "T"),
"[T][✘] happy thoughts");
}

@Test
public void queryTaskList_getCurrentStatus_returnsString() throws DukeException {
TaskListTest.initTest();
String expected = "Now you have 2 undone tasks in the list.";
assertEquals(testTaskList.getCurrentStatus(), expected);
}

@Test
public void modifyTaskList_completeTask_returnsString() throws DukeException {
TaskListTest.initTest();
String expected = "[T][✓] i want ice cream";
assertEquals(testTaskList.completeTask(2), expected);
}
}

0 comments on commit b2bf031

Please sign in to comment.