Skip to content

Commit

Permalink
Add tests for Commands
Browse files Browse the repository at this point in the history
  • Loading branch information
dishenggg committed Sep 19, 2023
1 parent 4e5d2f5 commit d0b6026
Show file tree
Hide file tree
Showing 12 changed files with 311 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/main/java/joe/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ private static Command handleInvalidKeyword() {
sb.append(", ");
}
sb.setLength(sb.length() - 2); //Removes extra ", " at the end
String msg = String.format("Invalid Command Keyword!%nHere is a list of valid commands: %s", sb);
String msg = String.format("Invalid Command Keyword!\nHere is a list of valid commands: %s", sb);
return new InvalidCommand(msg);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/joe/commands/DeadlineCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public String execute(TaskList tasks, Storage storage) {
storage.saveToFile(tasks);
return (
String.format(
"Got it, I've added this task:%n %s%nNow you have %d tasks in the list.",
"Got it, I've added this task:\n %s\nNow you have %d tasks in the list.",
newTask, tasks.size()));
}
}
2 changes: 1 addition & 1 deletion src/main/java/joe/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public String execute(TaskList tasks, Storage storage) throws JoeIndexOutOfBound

return (
String.format(
"Noted. I've removed this task:%n %s%nNow you have %d tasks in the list.",
"Noted. I've removed this task:\n %s\nNow you have %d tasks in the list.",
deletedTask, tasks.size()));
}
}
2 changes: 1 addition & 1 deletion src/main/java/joe/commands/EventCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public String execute(TaskList tasks, Storage storage) throws JoeException {

return (
String.format(
"Got it, I've added this task:%n %s%nNow you have %d tasks in the list.",
"Got it, I've added this task:\n %s\nNow you have %d tasks in the list.",
newTask, tasks.size()));
}
}
2 changes: 1 addition & 1 deletion src/main/java/joe/commands/MarkCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ public String execute(TaskList tasks, Storage storage) throws JoeIndexOutOfBound

storage.saveToFile(tasks);

return (String.format("Nice! I've marked this task as done:%n %s", tasks.get(idx - 1)));
return (String.format("Nice! I've marked this task as done:\n %s", tasks.get(idx - 1)));
}
}
2 changes: 1 addition & 1 deletion src/main/java/joe/commands/TodoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public String execute(TaskList tasks, Storage storage) {

return (
String.format(
"Got it, I've added this task:%n %s%nNow you have %d tasks in the list.",
"Got it, I've added this task:\n %s\nNow you have %d tasks in the list.",
newTask, tasks.size()));
}
}
2 changes: 1 addition & 1 deletion src/main/java/joe/commands/UnmarkCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ public String execute(TaskList tasks, Storage storage) throws JoeIndexOutOfBound

storage.saveToFile(tasks);

return (String.format("OK! I've marked this task as not done:%n %s", tasks.get(idx - 1)));
return (String.format("OK! I've marked this task as not done:\n %s", tasks.get(idx - 1)));
}
}
215 changes: 215 additions & 0 deletions src/test/java/joe/CommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
package joe;

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

import java.time.LocalDateTime;

import org.junit.jupiter.api.Test;

import joe.commands.DeadlineCommand;
import joe.commands.DeleteCommand;
import joe.commands.EventCommand;
import joe.commands.ListCommand;
import joe.commands.MarkCommand;
import joe.commands.TodoCommand;
import joe.commands.UnmarkCommand;
import joe.exceptions.JoeException;
import joe.stubs.StorageStub;
import joe.stubs.TaskListStub;

public class CommandTest {
@Test
public void todo_expectedInput_success() {
TaskListStub taskListStub = new TaskListStub(0);
StorageStub storageStub = new StorageStub();
LocalDateTime dateTime = LocalDateTime.of(2023, 1, 1, 12, 0);

TodoCommand command = new TodoCommand("My Task");

String result = command.execute(taskListStub, storageStub);

assertEquals("Got it, I've added this task:\n"
+ " [T][ ] My Task\n"
+ "Now you have 1 tasks in the list.", result);
assertEquals(1, taskListStub.size());
}

@Test
public void deadline_expectedInput_success() {
TaskListStub taskListStub = new TaskListStub(0);
StorageStub storageStub = new StorageStub();
LocalDateTime dateTime = LocalDateTime.of(2023, 1, 1, 12, 0);

DeadlineCommand command = new DeadlineCommand("My Task", dateTime);

String result = command.execute(taskListStub, storageStub);

assertEquals("Got it, I've added this task:\n"
+ " [D][ ] My Task (by: 01 Jan 2023 12:00)\n"
+ "Now you have 1 tasks in the list.", result);
assertEquals(1, taskListStub.size());
}

@Test
public void event_expectedInput_success() {
TaskListStub taskListStub = new TaskListStub(0);
StorageStub storageStub = new StorageStub();
LocalDateTime fromDateTime = LocalDateTime.of(2023, 1, 1, 12, 0);
LocalDateTime toDateTime = LocalDateTime.of(2023, 1, 2, 12, 0);

EventCommand command = new EventCommand("My Task", fromDateTime, toDateTime);

try {
String result = command.execute(taskListStub, storageStub);
assertEquals("Got it, I've added this task:\n"
+ " [E][ ] My Task (from: 01 Jan 2023 12:00 to: 02 Jan 2023 12:00)\n"
+ "Now you have 1 tasks in the list.", result);
assertEquals(1, taskListStub.size());
} catch (JoeException e) {
fail();
}
}

@Test
public void event_badDates_success() {
TaskListStub taskListStub = new TaskListStub(0);
StorageStub storageStub = new StorageStub();
LocalDateTime fromDateTime = LocalDateTime.of(2023, 1, 2, 12, 0);
LocalDateTime toDateTime = LocalDateTime.of(2023, 1, 1, 12, 0);

EventCommand command = new EventCommand("My Task", fromDateTime, toDateTime);
assertThrows(JoeException.class, () -> command.execute(taskListStub, storageStub));
}

@Test
public void event_sameDateTime_success() {
TaskListStub taskListStub = new TaskListStub(0);
StorageStub storageStub = new StorageStub();
LocalDateTime dateTime = LocalDateTime.of(2023, 1, 1, 12, 0);

EventCommand command = new EventCommand("My Task", dateTime, dateTime);

try {
String result = command.execute(taskListStub, storageStub);
assertEquals("Got it, I've added this task:\n"
+ " [E][ ] My Task (from: 01 Jan 2023 12:00 to: 01 Jan 2023 12:00)\n"
+ "Now you have 1 tasks in the list.", result);
assertEquals(1, taskListStub.size());
} catch (JoeException e) {
fail();
}
}

@Test
public void list_expectedInput_success() {
TaskListStub taskListStub = new TaskListStub(0);
StorageStub storageStub = new StorageStub();

ListCommand command = new ListCommand();

String result = command.execute(taskListStub, storageStub);

assertEquals("Here are your tasks:\n"
+ "TaskListStub", result);

}

@Test
public void delete_expectedInput_success() {
TaskListStub taskListStub = new TaskListStub(1);
StorageStub storageStub = new StorageStub();

DeleteCommand command = new DeleteCommand(1);

try {
String result = command.execute(taskListStub, storageStub);
assertEquals("Noted. I've removed this task:\n"
+ " toString\n"
+ "Now you have 0 tasks in the list.", result);
assertEquals(0, taskListStub.size());
} catch (JoeException e) {
fail();
}
}

@Test
public void delete_badIndex_success() {
TaskListStub taskListStub = new TaskListStub(1);
StorageStub storageStub = new StorageStub();

DeleteCommand commandTwo = new DeleteCommand(2);
assertThrows(JoeException.class, () -> commandTwo.execute(taskListStub, storageStub));

DeleteCommand commandZero = new DeleteCommand(0);
assertThrows(JoeException.class, () -> commandZero.execute(taskListStub, storageStub));

DeleteCommand commandNegative = new DeleteCommand(-1);
assertThrows(JoeException.class, () -> commandNegative.execute(taskListStub, storageStub));
}

@Test
public void mark_expectedInput_success() {
TaskListStub taskListStub = new TaskListStub(1);
StorageStub storageStub = new StorageStub();

MarkCommand command = new MarkCommand(1);

try {
String result = command.execute(taskListStub, storageStub);
assertEquals("Nice! I've marked this task as done:\n"
+ " toString", result);
assertEquals(1, taskListStub.size());
} catch (JoeException e) {
fail();
}
}

@Test
public void mark_badIndex_success() {
TaskListStub taskListStub = new TaskListStub(1);
StorageStub storageStub = new StorageStub();

MarkCommand commandTwo = new MarkCommand(2);
assertThrows(JoeException.class, () -> commandTwo.execute(taskListStub, storageStub));

MarkCommand commandZero = new MarkCommand(0);
assertThrows(JoeException.class, () -> commandZero.execute(taskListStub, storageStub));

MarkCommand commandNegative = new MarkCommand(-1);
assertThrows(JoeException.class, () -> commandNegative.execute(taskListStub, storageStub));
}

@Test
public void unmark_expectedInput_success() {
TaskListStub taskListStub = new TaskListStub(1);
StorageStub storageStub = new StorageStub();

UnmarkCommand command = new UnmarkCommand(1);

try {
String result = command.execute(taskListStub, storageStub);
assertEquals("OK! I've marked this task as not done:\n"
+ " toString", result);
assertEquals(1, taskListStub.size());
} catch (JoeException e) {
fail();
}
}

@Test
public void unmark_badIndex_success() {
TaskListStub taskListStub = new TaskListStub(1);
StorageStub storageStub = new StorageStub();

UnmarkCommand commandTwo = new UnmarkCommand(2);
assertThrows(JoeException.class, () -> commandTwo.execute(taskListStub, storageStub));

UnmarkCommand commandZero = new UnmarkCommand(0);
assertThrows(JoeException.class, () -> commandZero.execute(taskListStub, storageStub));

UnmarkCommand commandNegative = new UnmarkCommand(-1);
assertThrows(JoeException.class, () -> commandNegative.execute(taskListStub, storageStub));
}
}
19 changes: 1 addition & 18 deletions src/test/java/joe/TaskListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,10 @@

import org.junit.jupiter.api.Test;

import joe.stubs.TaskStub;
import joe.tasks.Task;


public class TaskListTest {

private static class TaskStub extends Task {
protected TaskStub() {
super("");
}

@Override
public String toString() {
return "toString";
}

@Override
public String getDescription() {
return "Description";
}
}

@Test
public void add_expectedUsage_success() {
TaskList tasks = new TaskList();
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/joe/stubs/StorageStub.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package joe.stubs;

import joe.Storage;
import joe.TaskList;
/**
* A stub implementation of Storage for testing.
*/
public class StorageStub extends Storage {
public StorageStub() {
super("test.txt");
}

@Override
public void saveToFile(TaskList tasks) {
// Do nothing for the stub
}
}
39 changes: 39 additions & 0 deletions src/test/java/joe/stubs/TaskListStub.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package joe.stubs;

import joe.TaskList;
import joe.tasks.Task;
/**
* A stub implementation of TaskList for testing.
*/
public class TaskListStub extends TaskList {
private int size;

public TaskListStub(int size) {
this.size = size;
}

@Override
public void add(Task task) {
this.size++;
}

@Override
public void remove(int idx) {
this.size--;
}

@Override
public int size() {
return this.size;
}

@Override
public String toString() {
return "TaskListStub";
}

@Override
public Task get(int idx) {
return new TaskStub();
}
}
32 changes: 32 additions & 0 deletions src/test/java/joe/stubs/TaskStub.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package joe.stubs;

import joe.tasks.Task;
/**
* A stub implementation of Task for testing.
*/
public class TaskStub extends Task {
public TaskStub() {
super("");
}

@Override
public String toString() {
return "toString";
}

@Override
public String getDescription() {
return "Description";
}

@Override
public void markAsDone() {
//Do Nothing
}

@Override
public void markAsNotDone() {
//Do Nothing
}

}

0 comments on commit d0b6026

Please sign in to comment.