Skip to content

Commit

Permalink
Merge branch 'A-JUnit'
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmui8 committed Aug 31, 2023
2 parents 860bce1 + e5858ff commit 3e672db
Show file tree
Hide file tree
Showing 6 changed files with 308 additions and 9 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test {
}

application {
mainClass.set("seedu.duke.Duke")
mainClass.set("duke.Duke")
}

shadowJar {
Expand All @@ -37,6 +37,6 @@ shadowJar {
dependsOn("distZip", "distTar")
}

run{
run {
standardInput = System.in
}
3 changes: 2 additions & 1 deletion src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class Parser {
* @return The command to be used by the bot.
*/
public static Command decideCommand(String input) {
// return BYE and LIST only if the input matches exactly
if (input.equals("bye")) {
return Command.BYE;
} else if (input.equals(("list"))) {
Expand All @@ -30,7 +31,7 @@ public static Command decideCommand(String input) {
String command = words[0];

for (Command c : Command.values()) {
if (c.name().toLowerCase().equals(command)) {
if (c.name().toLowerCase().equals(command) && !c.equals(Command.BYE) && !c.equals(Command.LIST)) {
return c;
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/duke/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ public void addTodo(String input) throws DukeException {
addTask(task);
} else {
// Todo description is empty
throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty."
+ "\ntodo ...");
throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty." +
"\ntodo ...");
}
}

Expand All @@ -157,8 +157,8 @@ public void addEvent(String input) throws DukeException {
addTask(task);
} else {
// User did not follow event format
throw new DukeException("Input for event doesn't match the expected format."
+ "\nevent ... /from ... /to ...");
throw new DukeException("Input for event doesn't match the expected format." +
"\nevent ... /from ... /to ...");
}
}

Expand All @@ -183,8 +183,8 @@ public void addDeadline(String input) throws DukeException {
addTask(task);
} else {
// User did not follow deadline format
throw new DukeException("Input for deadline doesn't match the expected format."
+ "\ndeadline ... /by ...");
throw new DukeException("Input for deadline doesn't match the expected format." +
"\ndeadline ... /by ...");
}
}

Expand Down
39 changes: 39 additions & 0 deletions src/test/java/duke/DeadlineTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package duke;

import duke.tasks.Deadline;
import duke.tasks.Task;
import org.junit.jupiter.api.Test;

import java.time.LocalDate;

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

public class DeadlineTest {

@Test
public void testToString() {
Task task = new Deadline(LocalDate.of(2023, 6, 23), "abc");
assertEquals("[D][ ] abc (by: 2023-06-23)", task.toString());
}

@Test
public void testTaskToString() {
Task task = new Deadline(LocalDate.of(2023, 6, 23), "abc");
assertEquals("D | 0 | abc | 2023-06-23", task.tasktoString());
}

@Test
public void testMarkAsDone() {
Task task = new Deadline(LocalDate.of(2023, 6, 23), "abc");
task.markAsDone();
assertEquals("D | 1 | abc | 2023-06-23", task.tasktoString());
}

@Test
public void testUnMark() {
Task task = new Deadline(LocalDate.of(2023, 6, 23), "abc");
task.markAsDone();
task.unMark();
assertEquals("D | 0 | abc | 2023-06-23", task.tasktoString());
}
}
149 changes: 149 additions & 0 deletions src/test/java/duke/ParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package duke;

import duke.tasks.Deadline;
import duke.tasks.Event;
import duke.tasks.Todo;
import org.junit.jupiter.api.Test;

import java.time.LocalDate;

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

public class ParserTest {
@Test
public void decideCommand_invalidInput_invalid() {
assertEquals(Command.INVALID, Parser.decideCommand("abc 123"));
}

@Test
public void decideCommand_wrongByeFormat_invalid() {
assertEquals(Command.INVALID, Parser.decideCommand("bye bye"));
}

@Test
public void decideCommand_wrongListInput_invalid() {
assertEquals(Command.INVALID, Parser.decideCommand("list 123"));
}

@Test
public void decideCommand_eventInput_event() {
assertEquals(Command.EVENT, Parser.decideCommand("event abc"));
}

@Test
public void decideCommand_deadlineInput_deadline() {
assertEquals(Command.DEADLINE, Parser.decideCommand("deadline abc"));
}

@Test
public void decideCommand_todoInput_todo() {
assertEquals(Command.TODO, Parser.decideCommand("todo abc"));
}

@Test
public void decideCommand_markInput_mark() {
assertEquals(Command.MARK, Parser.decideCommand("mark abc"));
}

@Test
public void decideCommand_unmarkInput_unmark() {
assertEquals(Command.UNMARK, Parser.decideCommand("unmark abc"));
}

@Test
public void decideCommand_correctListInput_list() {
assertEquals(Command.LIST, Parser.decideCommand("list"));
}

@Test
public void decideCommand_correctByeInput_bye() {
assertEquals(Command.BYE, Parser.decideCommand("bye"));
}

@Test
public void stringToTask_incorrectTaskType_exceptionThrown() {
try {
assertEquals(new Todo("abc").toString(),
Parser.stringToTask("a | 1 | abc").toString());
fail(); // the test should not reach this line
} catch (Exception e) {
assertEquals("Field 1 (Task type) is invalid", e.getMessage());
}
}

@Test
public void stringToTask_incorrectIsMarked_exceptionThrown() {
try {
assertEquals(new Todo("abc").toString(), Parser.stringToTask("T | 3 | abc").toString());
fail(); // the test should not reach this line
} catch (Exception e) {
assertEquals("Field 2 (isMarked) is invalid", e.getMessage());
}
}

@Test
public void stringToTask_incorrectTodoFormat_exceptionThrown() {
try {
assertEquals(new Todo("abc").toString(), Parser.stringToTask("T | 1").toString());
fail(); // the test should not reach this line
} catch (Exception e) {
assertEquals("Incorrect Format for todo task in file", e.getMessage());
}
}

@Test
public void stringToTask_incorrectDeadlineFormat_exceptionThrown() {
try {
assertEquals(new Deadline(LocalDate.of(2023, 6, 23), "abc").toString(),
Parser.stringToTask("D | 0 | abc").toString());
fail(); // the test should not reach this line
} catch (Exception e) {
assertEquals("Incorrect Format for deadline task in file", e.getMessage());
}
}

@Test
public void stringToTask_incorrectEventFormat_exceptionThrown() {
try {
assertEquals(new Event(LocalDate.of(2023, 6, 23),
LocalDate.of(2023, 6, 24),
"abc").toString(),
Parser.stringToTask("E | 0 | abc | 2023-06-23").toString());
fail(); // the test should not reach this line
} catch (Exception e) {
assertEquals("Incorrect Format for event task in file", e.getMessage());
}
}

@Test
public void stringToTask_correctTodoFormat_todoTask() {
try {
assertEquals(new Todo("abc").toString(),
Parser.stringToTask("T | 0 | abc").toString());
} catch (Exception e) {
fail();
}
}

@Test
public void stringToTask_correctDeadlineFormat_deadlineTask() {
try {
assertEquals(new Deadline(LocalDate.of(2023, 6, 23), "abc").toString(),
Parser.stringToTask("D | 0 | abc | 2023-06-23").toString());
} catch (Exception e) {
fail();
}
}

@Test
public void stringToTask_correctEventFormat_eventTask() {
try {
assertEquals(new Event(LocalDate.of(2023, 6, 23),
LocalDate.of(2023, 6, 24), "abc").toString(),
Parser.stringToTask("E | 0 | abc | 2023-06-23 | 2023-06-24").toString());
} catch (Exception e) {
fail();
}
}
}
110 changes: 110 additions & 0 deletions src/test/java/duke/TaskListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package duke;

import duke.tasks.Deadline;
import duke.tasks.Event;
import duke.tasks.Task;
import duke.tasks.Todo;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

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

public class TaskListTest {
List<Task> list = new ArrayList<>();
Task task1 = new Todo("abc");
Task task2 = new Event(LocalDate.of(2023, 8, 16),
LocalDate.of(2023, 8, 27),
"read book");
Task task3 = new Deadline(LocalDate.of(2023, 5, 23), "read book");
private TaskList taskList = new TaskList(list);

@BeforeEach
public void init() {
taskList.addTask(task1);
taskList.addTask(task2);
taskList.addTask(task3);
}

@Test
public void addDeadline_wrongInputFormat_exceptionThrown() {
try {
taskList.addDeadline("deadline read book 2023-04-16");
Task task4 = new Deadline(LocalDate.of(2023, 4, 16), "read book");
assertEquals(task4.toString(), list.get(3).toString());
fail(); // the test should not reach this line
} catch (Exception e) {
assertEquals("Input for deadline doesn't match the expected format." +
"\ndeadline ... /by ...", e.getMessage());
}
}

@Test
public void addDeadline_wrongDateFormat_exceptionThrown() {
try {
taskList.addDeadline("deadline read book 2023-16-04");
Task task4 = new Deadline(LocalDate.of(2023, 4, 16), "read book");
assertEquals(task4.toString(), list.get(3).toString());
fail(); // the test should not reach this line
} catch (Exception e) {
assertEquals("Input for deadline doesn't match the expected format." +
"\ndeadline ... /by ...", e.getMessage());
}
}

@Test
public void addDeadline_correctInput_taskAdded() {
try {
taskList.addDeadline("deadline read book /by 2023-04-16");
Task task4 = new Deadline(LocalDate.of(2023, 4, 16), "read book");
assertEquals(task4.toString(), list.get(3).toString());
} catch (Exception e) {
fail();
}
}

@Test
public void markTask_invalidNumber_exceptionThrown() {
try {
Task task4 = new Deadline(LocalDate.of(2023, 5, 23), "read book");
task4.markAsDone();
taskList.markTask("4");
assertEquals(task4.toString(), list.get(2).toString());
fail(); // the test should not reach this line
} catch (Exception e) {
System.out.println(e.getMessage());
assertEquals("Invalid number", e.getMessage());
}
}

@Test
public void markTask_noNumber_exceptionThrown() {
try {
Task task4 = new Deadline(LocalDate.of(2023, 5, 23), "read book");
task4.markAsDone();
taskList.markTask("read book");
assertEquals(task4.toString(), list.get(2).toString());
fail(); // the test should not reach this line
} catch (Exception e) {
System.out.println(e.getMessage());
assertEquals("Please key in a number", e.getMessage());
}
}

@Test
public void markTask_validNumber_taskMarked() {
try {
Task task4 = new Deadline(LocalDate.of(2023, 5, 23), "read book");
task4.markAsDone();
taskList.markTask("3");
assertEquals(task4.toString(), list.get(2).toString());
} catch (Exception e) {
fail();
}
}
}

0 comments on commit 3e672db

Please sign in to comment.