Skip to content

Commit

Permalink
Add Test Cases and Covered Abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
ryamgoh committed Oct 20, 2023
1 parent 7fd1236 commit f2b737e
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 9 deletions.
4 changes: 3 additions & 1 deletion data/duke.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
T | 0 | Go to the gym
T | 1 | Go to the gym
E | 1 | Go to Gym | Dec 12 2023 06:00 PM - Dec 12 2023 08:00 PM
T | 0 | Head to school
T | 1 | Say hello to Ann
T | 0 | asdsad
D | 0 | sdsdsd | Dec 10 2022 07:20 PM
10 changes: 9 additions & 1 deletion src/main/java/duke/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@
* Represents a parser
*/
public class Parser {
private static final String DATE_TIME_FORMAT = "dd/MM/yy HHmm";
/**
* The date time format that user has to type in in order to add a deadline or event
*/
public static final String DATE_TIME_FORMAT = "dd/MM/yy HHmm";
/**
* The date time formatter for toFile storage
*/
public static final String DATE_TIME_FORMAT_FILE_VER = "MMM d yyyy hh:mm a";
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern(DATE_TIME_FORMAT);
private static final String INVALID_COMMAND = "OOPS!!! I'm sorry, but I don't know what that means :-(\n"
+ "If you need help with commands, please type 'help'!";
Expand Down Expand Up @@ -310,4 +317,5 @@ static boolean isNumeric(String str) {
}
}


}
3 changes: 2 additions & 1 deletion src/main/java/duke/storage/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.ArrayList;
import java.util.Scanner;

import duke.parser.Parser;
import duke.tasks.Deadline;
import duke.tasks.Event;
import duke.tasks.Task;
Expand Down Expand Up @@ -133,7 +134,7 @@ public Task formatStringToTask(String line) {

Task task;

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(Parser.DATE_TIME_FORMAT_FILE_VER);

switch (type) {
case "T":
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/duke/tasks/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import duke.parser.Parser;

/**
* Represents a deadline
*/
Expand Down Expand Up @@ -43,7 +45,7 @@ public Deadline(String description, LocalDateTime deadline, String status) {
@Override
public String toString() {
return "[D]" + super.toString() + " (by: "
+ this.deadline.format(DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a")) + ")";
+ this.deadline.format(DateTimeFormatter.ofPattern(Parser.DATE_TIME_FORMAT_FILE_VER)) + ")";
}

/**
Expand All @@ -53,6 +55,6 @@ public String toString() {
*/
public String toFile() {
return "D" + super.toFile() + " | "
+ this.deadline.format(DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a"));
+ this.deadline.format(DateTimeFormatter.ofPattern(Parser.DATE_TIME_FORMAT_FILE_VER));
}
}
10 changes: 6 additions & 4 deletions src/main/java/duke/tasks/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import duke.parser.Parser;

/**
* Represents an event
*/
Expand Down Expand Up @@ -53,8 +55,8 @@ public Event(String description, LocalDateTime from, LocalDateTime to, String st
@Override
public String toString() {
return "[E]" + super.toString()
+ " (from: " + this.from.format(DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a"))
+ " to: " + this.to.format(DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a")) + ")";
+ " (from: " + this.from.format(DateTimeFormatter.ofPattern(Parser.DATE_TIME_FORMAT_FILE_VER))
+ " to: " + this.to.format(DateTimeFormatter.ofPattern(Parser.DATE_TIME_FORMAT_FILE_VER)) + ")";
}

/**
Expand All @@ -65,7 +67,7 @@ public String toString() {
@Override
public String toFile() {
return "E" + super.toFile() + " | "
+ this.from.format(DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a"))
+ " - " + this.to.format(DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a"));
+ this.from.format(DateTimeFormatter.ofPattern(Parser.DATE_TIME_FORMAT_FILE_VER))
+ " - " + this.to.format(DateTimeFormatter.ofPattern(Parser.DATE_TIME_FORMAT_FILE_VER));
}
}
123 changes: 123 additions & 0 deletions src/test/java/duke/parser/ParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package duke.parser;

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

import org.junit.jupiter.api.Test;

import duke.commands.AddCommand;
import duke.commands.Command;
import duke.commands.ExitCommand;
import duke.commands.IncorrectCommand;
import duke.commands.ListCommand;

public class ParserTest {

@Test
public void list_incorrectCommand() {
Command c = Parser.parse("list xx");
assertTrue(c instanceof IncorrectCommand);
}

@Test
public void list_correctCommand() {
Command c = Parser.parse("list");
assertTrue(c instanceof ListCommand);
}

@Test
public void exit_incorrectCommand() {
Command c = Parser.parse("bye ");
assertTrue(c instanceof IncorrectCommand);
}

@Test
void exit_correctCommand() {
Command c = Parser.parse("bye");
assertTrue(c instanceof ExitCommand);
}

@Test
void todo_invalidTask_incorrectCommand() {
Command c = Parser.parse("todo ");
assertTrue(c instanceof IncorrectCommand);
}

@Test
void todo_validTask_correctCommand() {
Command c = Parser.parse("todo xxx");
assertTrue(c instanceof AddCommand);
}

@Test
void deadline_validTaskAndInvalidDate_correctCommand() {
Command c = Parser.parse("deadline xxx /by ");
assertTrue(c instanceof IncorrectCommand);
}

@Test
void deadline_invalidDeadline_incorrectCommand() {
Command c = Parser.parse("deadline ");
assertTrue(c instanceof IncorrectCommand);
}

@Test
void deadline_missingTaskAndValidDate_incorrectCommand() {
Command c = Parser.parse("deadline /by 21/06/02 1200");
assertTrue(c instanceof IncorrectCommand);
}

@Test
void deadline_validTaskAndInvalidDate_incorrectCommand() {
Command c = Parser.parse("deadline xx /by lorem");
assertTrue(c instanceof IncorrectCommand);
}

@Test
void deadline_validDeadline_correctCommand() {
Command c = Parser.parse("deadline xx /by 21/08/02 1900");
assertTrue(c instanceof AddCommand);
}

@Test
void event_invalidEvent_incorrectCommand() {
Command c = Parser.parse("event");
assertTrue(c instanceof IncorrectCommand);
}

@Test
void event_validTaskAndInvalidDate_incorrectCommand() {
Command c = Parser.parse("event xx");
assertTrue(c instanceof IncorrectCommand);
}

@Test
void event_validTaskAndInvalidTo_incorrectCommand() {
Command c = Parser.parse("event xx /from 21/05/02 1100");
assertTrue(c instanceof IncorrectCommand);
}

@Test
void event_validEvent_correctCommand() {
Command c = Parser.parse("event xx /from 21/05/02 1100 /to 21/05/02 1200");
assertTrue(c instanceof AddCommand);
}

@Test
void event_invalidTaskAndValidDates_incorrectCommand() {
Command c = Parser.parse("event /from 21/05/02 1100 /to 21/05/02 1200");
assertTrue(c instanceof IncorrectCommand);
}

@Test
void event_validTaskAndInvalidFromAndInvalidTo_incorrectCommand() {
Command c = Parser.parse("event go to the cafe /from xx /to yy");
assertTrue(c instanceof IncorrectCommand);
}

@Test
void event_validTaskAndInvalidRange_incorrectCommand() {
Command c = Parser.parse("event go to library /from 21/05/02 1100 /to 21/05/02 1000");
assertTrue(c instanceof IncorrectCommand);
}

}
27 changes: 27 additions & 0 deletions src/test/java/duke/tasks/DeadlineTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package duke.tasks;

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

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.junit.jupiter.api.Test;

public class DeadlineTest {
@Test
public void toString_correctOutput() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yy HHmm");
LocalDateTime by = LocalDateTime.parse("21/08/02 1900", formatter);
Task task = new Deadline("Finish HW", by);
task.completeTask(true);
assertEquals("[D][X] Finish HW (by: Aug 21 2002 07:00 PM)", task.toString());
}

@Test
public void toFile_correctOutput() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yy HHmm");
LocalDateTime by = LocalDateTime.parse("21/08/02 1900", formatter);
Task task = new Deadline("Finish HW", by, "0");
assertEquals("D | 0 | Finish HW | Aug 21 2002 07:00 PM", task.toFile());
}
}
41 changes: 41 additions & 0 deletions src/test/java/duke/tasks/EventTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package duke.tasks;

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

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.junit.jupiter.api.Test;

public class EventTest {
@Test
public void toString_correctOutput() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yy HHmm");
LocalDateTime from = LocalDateTime.parse("21/08/02 1900", formatter);
LocalDateTime to = LocalDateTime.parse("21/08/02 2000", formatter);
Task task = new Event("Group Meeting", from, to, "0");
task.completeTask(true);
assertEquals("[E][X] Group Meeting (from: Aug 21 2002 07:00 PM to: Aug 21 2002 08:00 PM)",
task.toString());
}

@Test
public void toFile_correctOutput() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yy HHmm");
LocalDateTime from = LocalDateTime.parse("21/08/02 1900", formatter);
LocalDateTime to = LocalDateTime.parse("21/08/02 2000", formatter);
Task task = new Event("Group Meeting", from, to, "0");
assertEquals("E | 0 | Group Meeting | Aug 21 2002 07:00 PM - Aug 21 2002 08:00 PM", task.toFile());
}

@Test
public void toString_wrongOutputIfFromDateIsAfterToDate() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yy HHmm");
LocalDateTime from = LocalDateTime.parse("21/08/02 2000", formatter);
LocalDateTime to = LocalDateTime.parse("21/08/02 1900", formatter);
Task task = new Event("Group Meeting", from, to, "0");
task.completeTask(true);
assertEquals("[E][X] Group Meeting (from: Aug 21 2002 08:00 PM to: Aug 21 2002 07:00 PM)",
task.toString());
}
}

0 comments on commit f2b737e

Please sign in to comment.