Skip to content

Commit

Permalink
Merge branch 'branch-A-JUnit'
Browse files Browse the repository at this point in the history
  • Loading branch information
andrechuakj committed Sep 5, 2023
2 parents 1ff12d1 + bdd2830 commit 845ead3
Show file tree
Hide file tree
Showing 9 changed files with 318 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ public static Command parse(String strCommand) throws DukeException{
commandDetailList.add(taskToDelete);
command = new DeleteComand(commandDetailList);
break;
default:
throw new DukeException("☹ OOPS!!! This command is invalid.");
}
return command;
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/duke/command/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,19 @@ public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException
break;
}
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof AddCommand) {
AddCommand other = (AddCommand) obj;
if (this.commandDetails == null || other.commandDetails == null) {
return false;
}
return this.commandDetails.equals(other.commandDetails) && this.taskType.equals(other.taskType);
}
return false;
}
}
14 changes: 14 additions & 0 deletions src/main/java/duke/command/ByeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,18 @@ public boolean isExit() {
return true;
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof ByeCommand) {
ByeCommand other = (ByeCommand) obj;
if (this.commandDetails == null || other.commandDetails == null) {
return false;
}
return this.commandDetails.equals(other.commandDetails);
}
return false;
}
}
15 changes: 15 additions & 0 deletions src/main/java/duke/command/DeleteComand.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,19 @@ public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException
storage.writeListToFile(tasks);
ui.printTaskDeleted(deletedTask, tasks.size());
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof DeleteComand) {
DeleteComand other = (DeleteComand) obj;
if (this.commandDetails == null || other.commandDetails == null) {
return false;
}
return this.commandDetails.equals(other.commandDetails);
}
return false;
}
}
15 changes: 15 additions & 0 deletions src/main/java/duke/command/ListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,19 @@ public ListCommand(ArrayList<String> commandDetails) {
public void execute(TaskList tasks, Ui ui, Storage storage) {
ui.printList(tasks);
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof ListCommand) {
ListCommand other = (ListCommand) obj;
if (this.commandDetails == null || other.commandDetails == null) {
return false;
}
return this.commandDetails.equals(other.commandDetails);
}
return false;
}
}
15 changes: 15 additions & 0 deletions src/main/java/duke/command/MarkCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,19 @@ public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException
storage.writeListToFile(tasks);
ui.printTaskMarked(markedTask);
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof MarkCommand) {
MarkCommand other = (MarkCommand) obj;
if (this.commandDetails == null || other.commandDetails == null) {
return false;
}
return this.commandDetails.equals(other.commandDetails);
}
return false;
}
}
15 changes: 15 additions & 0 deletions src/main/java/duke/command/UnmarkCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,19 @@ public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException
storage.writeListToFile(tasks);
ui.printTaskMarked(unmarkedTask);
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof UnmarkCommand) {
UnmarkCommand other = (UnmarkCommand) obj;
if (this.commandDetails == null || other.commandDetails == null) {
return false;
}
return this.commandDetails.equals(other.commandDetails);
}
return false;
}
}
183 changes: 183 additions & 0 deletions src/test/java/duke/ParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package duke;

import duke.command.AddCommand;
import duke.command.ByeCommand;
import duke.command.Command;
import duke.command.DeleteComand;
import duke.command.ListCommand;
import duke.command.MarkCommand;
import duke.command.UnmarkCommand;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;

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

public class ParserTest {

// Success test cases
@Test
void parse_byeCommand_success() throws DukeException {
ArrayList<String> resString = new ArrayList<>();
resString.add("bye");
Command resCommand = new ByeCommand(resString);
assertEquals(resCommand, Parser.parse("bye"));
}

@Test
void parse_listCommand_success() throws DukeException {
ArrayList<String> resString = new ArrayList<>();
resString.add("list");
Command resCommand = new ListCommand(resString);
assertEquals(resCommand, Parser.parse("list"));
}

@Test
void parse_markCommand_success() throws DukeException {
ArrayList<String> resString = new ArrayList<>();
resString.add("3");
Command resCommand = new MarkCommand(resString);
assertEquals(resCommand, Parser.parse("mark 3"));
}

@Test
void parse_unmarkCommand_success() throws DukeException {
ArrayList<String> resString = new ArrayList<>();
resString.add("4");
Command resCommand = new UnmarkCommand(resString);
assertEquals(resCommand, Parser.parse("unmark 4"));
}

@Test
void parse_deleteCommand_success() throws DukeException {
ArrayList<String> resString = new ArrayList<>();
resString.add("1");
Command resCommand = new DeleteComand(resString);
assertEquals(resCommand, Parser.parse("delete 1"));
}

@Test
public void parse_validTodoCommand_success() throws DukeException {
ArrayList<String> resString = new ArrayList<>();
resString.add("coding");
Command resCommand = new AddCommand(resString, "T");
assertEquals(resCommand, Parser.parse("todo coding"));
}

@Test
public void parse_validDeadlineCommand_success() throws DukeException {
ArrayList<String> resString = new ArrayList<>();
resString.add("typing");
resString.add("2023-05-09");
Command resCommand = new AddCommand(resString, "D");
assertEquals(resCommand, Parser.parse("deadline typing /by 2023-05-09"));
}

@Test
public void parse_validEventCommand_success() throws DukeException {
ArrayList<String> resString = new ArrayList<>();
resString.add("hacking");
resString.add("2023-05-09");
resString.add("2023-05-10");
Command resCommand = new AddCommand(resString, "E");
assertEquals(resCommand, Parser.parse("event hacking /from 2023-05-09 /to 2023-05-10"));
}

// Exception thrown test cases

@Test
void parse_byeCommandSpellingError_exceptionThrown() throws DukeException {
try {
ArrayList<String> resString = new ArrayList<>();
Command resCommand = new ByeCommand(resString);
assertEquals(resCommand, Parser.parse("byebye"));
} catch (Exception e) {
assertEquals("☹ OOPS!!! This command is invalid.", e.getMessage());
}
}

@Test
void parse_listCommandSpellingError_exceptionThrown() throws DukeException {
try {
ArrayList<String> resString = new ArrayList<>();
Command resCommand = new ListCommand(resString);
assertEquals(resCommand, Parser.parse("listall"));
} catch (Exception e) {
assertEquals("☹ OOPS!!! This command is invalid.", e.getMessage());
}
}

@Test
void parse_markCommandNoTaskNumber_exceptionThrown() throws DukeException {
try {
ArrayList<String> resString = new ArrayList<>();
Command resCommand = new MarkCommand(resString);
assertEquals(resCommand, Parser.parse("mark"));
} catch (Exception e) {
assertEquals("☹ OOPS!!! The task number to mark cannot be empty.", e.getMessage());
}
}

@Test
void parse_unmarkCommandNoTaskNumber_exceptionThrown() throws DukeException {
try {
ArrayList<String> resString = new ArrayList<>();
Command resCommand = new UnmarkCommand(resString);
assertEquals(resCommand, Parser.parse("unmark"));
} catch (Exception e) {
assertEquals("☹ OOPS!!! The task number to unmark cannot be empty.", e.getMessage());
}
}

@Test
void parse_deleteCommandNoTaskNumber_exceptionThrown() throws DukeException {
try {
ArrayList<String> resString = new ArrayList<>();
Command resCommand = new DeleteComand(resString);
assertEquals(resCommand, Parser.parse("delete"));
} catch (Exception e) {
assertEquals("☹ OOPS!!! The task number to delete cannot be empty.", e.getMessage());
}
}

@Test
public void parse_todoCommandSpellingMistake_exceptionThrown() {
try {
ArrayList<String> string = new ArrayList<>();
Command command = new AddCommand(string, "T");
assertEquals(command, Parser.parse("todoj"));
fail(); // the test should not reach this line
} catch (Exception e) {
assertEquals("☹ OOPS!!! This command is invalid.", e.getMessage());
}
}

@Test
public void parse_deadLineCommandNoDesc_exceptionThrown() {
try {
ArrayList<String> string = new ArrayList<>();
Command command = new AddCommand(string, "D");
assertEquals(command, Parser.parse("deadline"));
fail(); // the test should not reach this line
} catch (Exception e) {
assertEquals("☹ OOPS!!! The format of the deadline command is invalid.\n"
+ "Here is an example of a valid format:"
+ " deadline coding /by 2023-09-04", e.getMessage());
}
}

@Test
public void parse_eventCommandWrongOrder_exceptionThrown() {
try {
ArrayList<String> string = new ArrayList<>();
Command command = new AddCommand(string, "E");
assertEquals(command, Parser.parse("event /to 2023-10-10 /from 2023-01-01"));
fail(); // the test should not reach this line
} catch (Exception e) {
assertEquals("☹ OOPS!!! The format of the event command is invalid.\n"
+ "Here is an example of a valid format:"
+ " event coding /from 2023-01-01 /to 2023-12-31", e.getMessage());
}
}
}
44 changes: 44 additions & 0 deletions src/test/java/duke/task/DeadlineTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package duke.task;

import duke.Duke;
import duke.task.Task;
import duke.task.Deadline;
import duke.DukeException;
import org.junit.jupiter.api.Test;

import java.time.LocalDate;

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

public class DeadlineTest {
@Test
public void constructor_validDescAndDate_success() throws DukeException {
Deadline deadline = new Deadline("Complete task", "2023-09-30");
assertEquals("Complete task", deadline.description);
assertEquals(LocalDate.of(2023, 9, 30), deadline.by);
assertEquals(false, deadline.isDone);
}

@Test
public void constructor_wrongDate_exceptionThrown() {
try {
new Deadline("Complete task", "30-09-2023");
} catch (DukeException e) {
assertEquals("Incorrect date format. Please enter date in yyyy-mm-dd format", e.getMessage());
}
}

@Test
public void toStringTest() throws DukeException {
Deadline deadline = new Deadline("Complete task", "2023-09-30");
String expected = "[D][ ] Complete task (by: Sep 30 2023)";
assertEquals(expected, deadline.toString());
}

@Test
public void toDataTest() throws DukeException {
Deadline deadline = new Deadline("Complete task", "2023-09-30");
String expected = "D | 0 | Complete task | 2023-09-30";
assertEquals(expected, deadline.toData());
}
}

0 comments on commit 845ead3

Please sign in to comment.