Skip to content

Commit 646b638

Browse files
weixue123weixue123
authored andcommitted
Added tests for the Command classes
1 parent c34b347 commit 646b638

15 files changed

Lines changed: 688 additions & 28 deletions

src/main/java/duke/Duke.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,20 @@ public Duke(String filepath) {
2525
}
2626

2727
/**
28-
* Executes the command w.r.t. the users' input.
28+
* Executes the command and computes a response to display to the users w.r.t.
29+
* the users' input.
2930
*
3031
* @param input A line of raw user input.
32+
* @return A <code>String</code> of response.
3133
*/
32-
public void execute(String input) {
34+
public String getResponse(String input) {
3335
Command command = this.getCommand(input);
3436
command.execute(this.tasks);
3537

3638
if (command instanceof ByeCommand) {
3739
this.storage.saveTasks(this.tasks);
3840
}
39-
}
4041

41-
/**
42-
* Computes a response to display to the users w.r.t. the users' input.
43-
*
44-
* @param input A line of raw user input.
45-
* @return A <code>String</code> of response.
46-
*/
47-
public String getResponse(String input) {
48-
Command command = this.getCommand(input);
4942
return command.getResponse(this.tasks);
5043
}
5144

src/main/java/duke/commands/DeleteCommand.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public class DeleteCommand extends Command {
2020
*/
2121
public DeleteCommand(int index) {
2222
this.index = index;
23-
this.deletedTask = null;
2423
}
2524

2625
/**
@@ -38,6 +37,7 @@ public boolean isExit() {
3837
* @param tasks A collection of <code>Task</code> objects representing the application's state.
3938
*/
4039
public void execute(TaskList tasks) {
40+
// Set this.deletedTask to the popped Task (if any), or else null.
4141
this.deletedTask = tasks.popTaskByIndex(this.index);
4242
}
4343

@@ -49,6 +49,9 @@ public void execute(TaskList tasks) {
4949
*/
5050
public String getResponse(TaskList tasks) {
5151
try {
52+
// Note that this getResponse method MUST be called after the execute method. Following
53+
// this condition, if this.deletedTask is equal to null here, then no task was popped
54+
// earlier. That can only mean that the index input into this command does not exist.
5255
if (null == this.deletedTask) {
5356
throw new TaskNumberNotExistException(this.index);
5457
}

src/main/java/duke/commands/DoneCommand.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public class DoneCommand extends Command {
1919
*/
2020
public DoneCommand(int index) {
2121
this.index = index;
22-
this.doneTask = null;
2322
}
2423

2524
/**
@@ -37,6 +36,7 @@ public boolean isExit() {
3736
* @param tasks A collection of <code>Task</code> objects representing the application's state.
3837
*/
3938
public void execute(TaskList tasks) {
39+
// Set this.doneTask to the task to be marked as done (if any), or else null.
4040
this.doneTask = tasks.getTaskByIndex(this.index);
4141
if (null != this.doneTask) {
4242
this.doneTask.markAsDone();
@@ -51,6 +51,9 @@ public void execute(TaskList tasks) {
5151
*/
5252
public String getResponse(TaskList tasks) {
5353
try {
54+
// Note that this getResponse method MUST be called after the execute method. Following
55+
// this condition, iff this.doneTask is equal to null here, then no task was marked as
56+
// done earlier. That can only mean that the index input into this command does not exist.
5457
if (null == this.doneTask) {
5558
throw new TaskNumberNotExistException(this.index);
5659
}

src/main/java/duke/commands/FindCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public String getResponse(TaskList tasks) {
5757
}
5858

5959
if (matchingTasks.getSize() == 0) {
60-
return "There are no tasks matching the '" + this.keywords + "' in your list :O";
60+
return "There are no tasks matching the string '" + this.keywords + "' in your list :O";
6161
} else {
6262
return "Here are the matching tasks in your list:\n" + matchingTasks.getTaskListAsString();
6363
}

src/main/java/duke/exceptions/DateTimeFormatException.java renamed to src/main/java/duke/exceptions/InvalidDateTimeFormatException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package duke.exceptions;
22

3-
public class DateTimeFormatException extends Exception {
3+
public class InvalidDateTimeFormatException extends Exception {
44

5-
public DateTimeFormatException(String dateString) {
5+
public InvalidDateTimeFormatException(String dateString) {
66
super("I can't recognize '" + dateString + "' as a date. Please follow the 'YYYY-MM-DD HH:mm' format :P");
77
}
88
}

src/main/java/duke/ui/MainWindow.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ public void setDuke(Duke duke) {
5555
@FXML
5656
private void handleUserInput() {
5757
String input = this.userInput.getText();
58-
59-
this.duke.execute(input);
6058
String response = this.duke.getResponse(input);
6159

6260
this.dialogContainer.getChildren().addAll(

src/main/java/duke/ui/Parser.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
import duke.commands.FindCommand;
1717
import duke.commands.InvalidInputCommand;
1818
import duke.commands.ListCommand;
19-
import duke.exceptions.DateTimeFormatException;
2019
import duke.exceptions.InvalidActionException;
20+
import duke.exceptions.InvalidDateTimeFormatException;
2121
import duke.exceptions.MissingDeadlineException;
2222
import duke.exceptions.MissingDescriptionException;
2323
import duke.exceptions.MissingEventTimeException;
@@ -243,19 +243,19 @@ private static String getExceptionMessage(String input) {
243243
}
244244

245245
if (action.equals(DEADLINE) && null == convertToDateTime(byDateTimeString)) {
246-
throw new DateTimeFormatException(byDateTimeString);
246+
throw new InvalidDateTimeFormatException(byDateTimeString);
247247
}
248248

249249
if (action.equals(EVENT) && null == convertToDateTime(atDateTimeString)) {
250-
throw new DateTimeFormatException(atDateTimeString);
250+
throw new InvalidDateTimeFormatException(atDateTimeString);
251251
}
252252

253253
} catch (MissingDescriptionException
254254
| InvalidActionException
255255
| TaskNumberNotIntException
256256
| MissingDeadlineException
257257
| MissingEventTimeException
258-
| DateTimeFormatException e) {
258+
| InvalidDateTimeFormatException e) {
259259
return e.getMessage();
260260
}
261261

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package duke.commands;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
import duke.tasks.TaskList;
9+
import duke.tasks.ToDo;
10+
11+
public class TestByeCommand {
12+
private final ToDo toDo;
13+
private final TaskList tasks;
14+
private final ByeCommand command;
15+
16+
/**
17+
* Initialize a <code>ByeCommand</code> instance for testing.
18+
*/
19+
public TestByeCommand() {
20+
this.command = new ByeCommand();
21+
this.tasks = new TaskList();
22+
23+
this.toDo = new ToDo("CS2103 Quiz");
24+
this.tasks.addTask(this.toDo);
25+
}
26+
27+
/**
28+
* Test that <code>ByeCommand</code> correctly determines whether to exit the application.
29+
*/
30+
@Test
31+
public void testIsExit() {
32+
assertTrue(this.command.isExit());
33+
}
34+
35+
/**
36+
* Test that <code>ByeCommand</code> (correctly) does not change the input <code>TaskList</code>.
37+
*/
38+
@Test
39+
public void testExecute() {
40+
assertEquals(1, this.tasks.getSize());
41+
assertEquals(this.toDo, this.tasks.getTaskByIndex(1));
42+
43+
this.command.execute(this.tasks);
44+
45+
assertEquals(1, this.tasks.getSize());
46+
assertEquals(this.toDo, this.tasks.getTaskByIndex(1));
47+
}
48+
49+
/**
50+
* Test that <code>ByeCommand</code> computes the response message correctly.
51+
*/
52+
@Test
53+
public void testResponse() {
54+
String expectedResponse = "Bye. Hope to see you again soon!";
55+
assertEquals(expectedResponse, this.command.getResponse(this.tasks));
56+
}
57+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 TestDeleteCommand {
17+
private final ToDo toDo;
18+
private final Deadline deadline;
19+
private final Event event;
20+
private final TaskList tasks;
21+
private final DeleteCommand command;
22+
23+
/**
24+
* Initialize a <code>DeleteCommand</code> instance and a <code>TaskList</code> instance for testing.
25+
*/
26+
public TestDeleteCommand() {
27+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm");
28+
LocalDateTime dateTime = LocalDateTime.parse("2021-02-06 23:30", formatter);
29+
30+
this.tasks = new TaskList();
31+
this.toDo = new ToDo("CS2103 Quiz 1");
32+
this.deadline = new Deadline("BT4013 Quiz 2", dateTime);
33+
this.event = new Event("CS2103 Quiz 3", dateTime);
34+
35+
this.deadline.markAsDone();
36+
37+
this.tasks.addTask(this.toDo);
38+
this.tasks.addTask(this.deadline);
39+
this.tasks.addTask(this.event);
40+
41+
this.command = new DeleteCommand(1);
42+
}
43+
44+
/**
45+
* Test that <code>DeleteCommand</code> correctly determines whether to exit the application.
46+
*/
47+
@Test
48+
public void testIsExit() {
49+
assertFalse(this.command.isExit());
50+
}
51+
52+
/**
53+
* Test that <code>DeleteCommand</code> alters the input <code>TaskList</code> correctly.
54+
*/
55+
@Test
56+
public void testExecute() {
57+
assertEquals(3, this.tasks.getSize());
58+
assertEquals(this.toDo, this.tasks.getTaskByIndex(1));
59+
assertEquals(this.deadline, this.tasks.getTaskByIndex(2));
60+
assertEquals(this.event, this.tasks.getTaskByIndex(3));
61+
62+
this.command.execute(this.tasks);
63+
64+
assertEquals(2, this.tasks.getSize());
65+
assertEquals(this.deadline, this.tasks.getTaskByIndex(1));
66+
assertEquals(this.event, this.tasks.getTaskByIndex(2));
67+
}
68+
69+
/**
70+
* Test that <code>DeleteCommand</code> computes the response message correctly.
71+
*/
72+
@Test
73+
public void testResponse() {
74+
this.command.execute(this.tasks);
75+
String expectedResponse = "Noted. I've removed this task:\n" + "[T][ ] CS2103 Quiz 1";
76+
assertEquals(expectedResponse, this.command.getResponse(this.tasks));
77+
78+
DeleteCommand invalidCommand = new DeleteCommand(10);
79+
String expectedExceptionMessage = "Task 10 does not exist :O";
80+
assertEquals(expectedExceptionMessage, invalidCommand.getResponse(this.tasks));
81+
}
82+
}

0 commit comments

Comments
 (0)