Skip to content

Commit

Permalink
add Junit
Browse files Browse the repository at this point in the history
Written tests that covers 80% of methods
  • Loading branch information
LimZiJia committed Feb 2, 2024
1 parent 84e9160 commit afa7deb
Show file tree
Hide file tree
Showing 10 changed files with 429 additions and 14 deletions.
10 changes: 10 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ repositories {
dependencies {
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.10.0'
testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.10.0'
testImplementation "org.mockito:mockito-inline:3.+"
testImplementation "org.mockito:mockito-junit-jupiter:3.+"
}

test {
Expand Down Expand Up @@ -40,3 +42,11 @@ shadowJar {
run{
standardInput = System.in
}

sourceSets {
test {
java {
srcDirs = ['src/test']
}
}
}
26 changes: 13 additions & 13 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ else if (in.startsWith("delete ")) {

public TaskList addTask(String s, TaskList tasks) {
// Todo_
if (s.startsWith("todo")) {
if (s.startsWith("todo ")) {
// Get name and if it is empty, throw exception
String n = s.substring(5);
if (n.isEmpty()) {
Expand All @@ -73,41 +73,41 @@ public TaskList addTask(String s, TaskList tasks) {
tasks.add(new Todo(n));

// tasks.Deadline
} else if (s.startsWith("deadline")) {
} else if (s.startsWith("deadline ")) {
// Try to get the index of the first '/', if it does not exist, the statement is invalid.
// Also, it should adhere to "/by"
int first = s.indexOf('/');
if (first == -1 || s.length() < first + 14) {
if (first == -1) {
throw new DukeErroneousArgumentException();
} else if (!s.startsWith("/by", first)) {
} else if (!s.startsWith("/by ", first)) {
throw new DukeErroneousArgumentException();
}

// Get name and time. If empty, throw exception
String n = s.substring(9, first);
String t = s.substring(first + 4, first + 14);
String n = s.substring(9, Math.max(first - 1, 9));
String t = s.substring(first + 4);
if (n.isEmpty() || t.isEmpty()) {
throw new DukeEmptyArgumentException();
}

tasks.add(new Deadline(n, t));

// tasks.Event
} else if (s.startsWith("event")) {
} else if (s.startsWith("event ")) {
// Try to get the index of the first and second '/', if it does not exist, the statement is invalid.
// Also, the format should adhere to "/from" and "/to"
int first = s.indexOf('/');
int second = s.indexOf('/', first + 1);
if (first == -1 || second != first + 17 || s.length() < second + 14) {
if (first == -1) {
throw new DukeErroneousArgumentException();
} else if (!s.startsWith("/from", first)
|| !s.startsWith("/to", second)) {
} else if (!s.startsWith("/from ", first)
|| !s.startsWith("/to ", second)) {
throw new DukeErroneousArgumentException();
}

String n = s.substring(6, first - 1);
String f = s.substring(first + 6, second - 1);
String t = s.substring(second + 4, second + 14);
String n = s.substring(6, Math.max(first - 1, 6));
String f = s.substring(first + 6, Math.max(second - 1, first + 6));
String t = s.substring(second + 4);
if (n.isEmpty() || f.isEmpty() || t.isEmpty()) {
throw new DukeEmptyArgumentException();
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/duke/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class Storage {
private static Path PATH_DIR;
private static Path PATH_FILE;

public Storage() {}

public Storage(String dir, String name) {
String userDir = System.getProperty("user.dir");
PATH_DIR = Paths.get(userDir + dir);
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/tasks/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,27 @@ public String toSavedString() {
, this.time.format(DateTimeFormatter.ISO_LOCAL_DATE));
}

@Override
public String toString() {
return String.format("[D][%s] %s (by: %s)\n"
, this.done ? "X" : " "
, this.name
, this.time.format(DateTimeFormatter.ofPattern("MMM dd yyyy")));
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}

if (obj.getClass() != this.getClass()) {
return false;
}

Deadline deadline = (Deadline) obj;
return this.name.equals(deadline.name)
&& this.done == deadline.done
&& this.time.equals(deadline.time);
}
}
18 changes: 18 additions & 0 deletions src/main/java/tasks/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,29 @@ public String toSavedString() {
, this.to.format(DateTimeFormatter.ISO_LOCAL_DATE));
}

@Override
public String toString() {
return String.format("[E][%s] %s (from: %s to: %s)\n"
, this.done ? "X" : " "
, this.name
, this.from.format(DateTimeFormatter.ofPattern("MMM dd yyyy"))
, this.to.format(DateTimeFormatter.ofPattern("MMM dd yyyy")));
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}

if (obj.getClass() != this.getClass()) {
return false;
}

Event event = (Event) obj;
return this.name.equals(event.name)
&& this.done == event.done
&& this.from.equals(event.from)
&& this.to.equals(event.to);
}
}
17 changes: 17 additions & 0 deletions src/main/java/tasks/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,21 @@ public List<String> taskToSavedString() {
.map(Task::toSavedString)
.collect(Collectors.toList());
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}

if (obj.getClass() != this.getClass()) {
return false;
}

TaskList tl = (TaskList) obj;

return this.list.equals(tl.list);
}


}
15 changes: 15 additions & 0 deletions src/main/java/tasks/Todo.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,22 @@ public String toSavedString() {
, this.name);
}

@Override
public String toString() {
return String.format("[T][%s] %s\n", this.done ? "X" : " ", this.name);
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}

if (obj.getClass() != this.getClass()) {
return false;
}

Todo todo = (Todo) obj;
return this.name.equals(todo.name) && this.done == todo.done;
}
}
Loading

0 comments on commit afa7deb

Please sign in to comment.