|
| 1 | +package duke.commands; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | + |
| 6 | +import java.time.LocalDateTime; |
| 7 | +import java.time.format.DateTimeFormatter; |
| 8 | + |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +import duke.tasks.Deadline; |
| 12 | +import duke.tasks.Event; |
| 13 | +import duke.tasks.TaskList; |
| 14 | +import duke.tasks.ToDo; |
| 15 | + |
| 16 | +public class TestAddTaskCommand { |
| 17 | + private final ToDo toDo; |
| 18 | + private final Deadline deadline; |
| 19 | + private final Event event; |
| 20 | + private final AddTaskCommand addToDoCommand; |
| 21 | + private final AddTaskCommand addDeadlineCommand; |
| 22 | + private final AddTaskCommand addEventCommand; |
| 23 | + |
| 24 | + |
| 25 | + /** |
| 26 | + * Initialize a <code>AddTaskCommand</code> instance for testing. |
| 27 | + */ |
| 28 | + public TestAddTaskCommand() { |
| 29 | + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm"); |
| 30 | + LocalDateTime dateTime = LocalDateTime.parse("2021-02-06 23:30", formatter); |
| 31 | + |
| 32 | + this.toDo = new ToDo("CS2103 Quiz 1"); |
| 33 | + this.deadline = new Deadline("BT4013 Quiz 2", dateTime); |
| 34 | + this.event = new Event("CS2103 Quiz 3", dateTime); |
| 35 | + |
| 36 | + this.addToDoCommand = new AddTaskCommand(this.toDo); |
| 37 | + this.addDeadlineCommand = new AddTaskCommand(this.deadline); |
| 38 | + this.addEventCommand = new AddTaskCommand(this.event); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Test that <code>AddTaskCommand</code> correctly determines whether to exit the application. |
| 43 | + */ |
| 44 | + @Test |
| 45 | + public void testIsExit() { |
| 46 | + assertFalse(this.addToDoCommand.isExit()); |
| 47 | + assertFalse(this.addDeadlineCommand.isExit()); |
| 48 | + assertFalse(this.addEventCommand.isExit()); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Test that <code>AddTaskCommand</code> alters the input <code>TaskList</code> correctly. |
| 53 | + */ |
| 54 | + @Test |
| 55 | + public void testExecute() { |
| 56 | + TaskList tasks = new TaskList(); |
| 57 | + |
| 58 | + this.addToDoCommand.execute(tasks); |
| 59 | + assertEquals(1, tasks.getSize()); |
| 60 | + assertEquals(this.toDo, tasks.getTaskByIndex(1)); |
| 61 | + |
| 62 | + this.addDeadlineCommand.execute(tasks); |
| 63 | + assertEquals(2, tasks.getSize()); |
| 64 | + assertEquals(this.toDo, tasks.getTaskByIndex(1)); |
| 65 | + assertEquals(this.deadline, tasks.getTaskByIndex(2)); |
| 66 | + |
| 67 | + this.addEventCommand.execute(tasks); |
| 68 | + assertEquals(3, tasks.getSize()); |
| 69 | + assertEquals(this.toDo, tasks.getTaskByIndex(1)); |
| 70 | + assertEquals(this.deadline, tasks.getTaskByIndex(2)); |
| 71 | + assertEquals(this.event, tasks.getTaskByIndex(3)); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Test that <code>AddTaskCommand</code> computes the response message correctly. |
| 76 | + */ |
| 77 | + @Test |
| 78 | + public void testResponse() { |
| 79 | + TaskList tasks = new TaskList(); |
| 80 | + |
| 81 | + this.addToDoCommand.execute(tasks); |
| 82 | + String expectedAddToDoResponse = "Got it. I've added this task:\n" |
| 83 | + + "[T][ ] CS2103 Quiz 1\n" |
| 84 | + + "Now you have 1 task(s) in the list."; |
| 85 | + assertEquals(expectedAddToDoResponse, this.addToDoCommand.getResponse(tasks)); |
| 86 | + |
| 87 | + this.addDeadlineCommand.execute(tasks); |
| 88 | + String expectedAddDeadlineResponse = "Got it. I've added this task:\n" |
| 89 | + + "[D][ ] BT4013 Quiz 2 (by: 2021-02-06 23:30)\n" |
| 90 | + + "Now you have 2 task(s) in the list."; |
| 91 | + assertEquals(expectedAddDeadlineResponse, this.addDeadlineCommand.getResponse(tasks)); |
| 92 | + |
| 93 | + this.addEventCommand.execute(tasks); |
| 94 | + String expectedAddEventResponse = "Got it. I've added this task:\n" |
| 95 | + + "[E][ ] CS2103 Quiz 3 (at: 2021-02-06 23:30)\n" |
| 96 | + + "Now you have 3 task(s) in the list."; |
| 97 | + assertEquals(expectedAddEventResponse, this.addEventCommand.getResponse(tasks)); |
| 98 | + } |
| 99 | +} |
0 commit comments