Skip to content

Commit

Permalink
Add JUnit tests for DeadlineCommand and EventCommand classes
Browse files Browse the repository at this point in the history
  • Loading branch information
rmj1405 committed Jan 27, 2023
1 parent fefc2a2 commit 07b3b23
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/test/java/command/DeadlineCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.junit.jupiter.api.Test;
import task.TaskManager;
import util.DukeException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
Expand All @@ -11,29 +12,41 @@ public class DeadlineCommandTest {
//creates new task array
TaskManager tm = new TaskManager();
@Test
public void executeCommand_checkAddToList_success(){
DeadlineCommand dc = new DeadlineCommand(tm, "test program /by 25/12/23 11:50PM");
public void executeCommand_checkAddToList_success() throws DukeException {
DeadlineCommand dc = new DeadlineCommand(tm, "test program /by 25/12/23 1150");
dc.executeCommand();
assertEquals(1, tm.getTaskArraySize());

}

@Test
public void executeCommand_checkTaskAddedToList_success(){
DeadlineCommand dc = new DeadlineCommand(tm, "test program /by 25/12/23 11:50PM");
public void executeCommand_checkTaskAddedToList_success() throws DukeException {
DeadlineCommand dc = new DeadlineCommand(tm, "test program /by 25/12/23 1150");
dc.executeCommand();
assertEquals("[D][ ] test program (by: 25/12/23 11:50PM)", tm.printTask(0));
assertEquals("[D][ ] test program (by: 25 Dec 2023 11:50 AM)", tm.printTask(0));
}

@Test
public void executeCommand_invalidUserInput_exceptionThrown(){
public void executeCommand_invalidDateTime_exceptionThrown() {
try {
DeadlineCommand dc = new DeadlineCommand(tm, "test /by 25/12/23 11:50PM");
dc.executeCommand();
assertEquals("[D][ ] test program (by: 25/12/23 11:50PM)", tm.printTask(0));
assertEquals("[D][ ] test program (by: 25/12/23 11:50 AM)", tm.printTask(0));
fail();
} catch (Exception e) {
//assertEquals("no");
assertEquals("Please enter date in dd/mm/yy and time in hhmm 24hr format!", e.getMessage());
}
}

@Test
public void executeCommand_invalidUserInput_exceptionThrown() {
try {
DeadlineCommand dc = new DeadlineCommand(tm, "test /by");
dc.executeCommand();
assertEquals("[D][ ] test program (by: 25/12/23 11:50 AM)", tm.printTask(0));
fail();
} catch (Exception e) {
assertEquals("Please add a description, date and time e.g. homework /by 12/12/12 2359", e.getMessage());
}
}
}
74 changes: 74 additions & 0 deletions src/test/java/command/EventCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package command;

import org.junit.jupiter.api.Test;
import task.TaskManager;
import util.DukeException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
public class EventCommandTest {
TaskManager tm = new TaskManager();
@Test
public void executeCommand_checkAddToList_success() throws DukeException {
EventCommand ec = new EventCommand(tm, "party /from 12/2/23 0600 /to 12/2/23 1000");
ec.executeCommand();
assertEquals(1, tm.getTaskArraySize());

}

@Test
public void executeCommand_checkTaskAddedToList_success() throws DukeException {
EventCommand ec = new EventCommand(tm, "party /from 12/2/23 0600 /to 12/2/23 1000");
ec.executeCommand();
assertEquals("[E][ ] party (Start: 12 Feb 2023 06:00 AM | End: 12 Feb 2023 10:00 AM)", tm.printTask(0));
}

@Test
public void executeCommand_invalidDateTime_exceptionThrown() {
try {
EventCommand ec = new EventCommand(tm, "party /from 12/2/23 0600 /to 12/2/2 1000");
ec.executeCommand();
assertEquals("[E][ ] party (Start: 12 Feb 2023 06:00 AM | End: 12 Feb 2023 10:00 AM)",
tm.printTask(0));
fail();
} catch (Exception e) {
assertEquals("Please enter date in dd/mm/yy and time in hhmm 24hr format!", e.getMessage());
}
}

@Test
public void executeCommand_invalidUserInputV1_exceptionThrown() {
try {
EventCommand ec = new EventCommand(tm, "party /from 12/2/23 0600");
ec.executeCommand();
fail();
} catch (Exception e) {
assertEquals("Please add a description, date and time e.g. party /from 12/2/23 1800 /to 12/2/23 2200",
e.getMessage());
}
}

@Test
public void executeCommand_invalidUserInputV2_exceptionThrown() {
try {
EventCommand ec = new EventCommand(tm, "party /to 12/2/2");
ec.executeCommand();
fail();
} catch (Exception e) {
assertEquals("Please add a description, date and time e.g. party /from 12/2/23 1800 /to 12/2/23 2200",
e.getMessage());
}
}

@Test
public void executeCommand_invalidUserInputV3_exceptionThrown() {
try {
EventCommand ec = new EventCommand(tm, "party");
ec.executeCommand();
fail();
} catch (Exception e) {
assertEquals("Please add a description, date and time e.g. party /from 12/2/23 1800 /to 12/2/23 2200",
e.getMessage());
}
}
}

0 comments on commit 07b3b23

Please sign in to comment.