Skip to content

Commit b168cbe

Browse files
committed
Add JUnit tests
Manually entered test cases and verified with provided Now can partially automate testing with help of JUnit
1 parent 3cd08f6 commit b168cbe

4 files changed

Lines changed: 117 additions & 2 deletions

File tree

src/main/java/duke/Task.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,27 @@ public boolean getDone() {
4444
public void mark() {
4545
done = true;
4646
}
47-
47+
48+
public boolean equals(Task t) {
49+
if (this.type == t.type) {
50+
if (!this.name.equals(t.name))
51+
return false;
52+
53+
if (this.type == 'D' || this.type == 'E') {
54+
if (this.dateTime.compareTo(t.dateTime) != 0)
55+
return false;
56+
}
57+
58+
if (this.done != t.done)
59+
return false;
60+
61+
return true;
62+
63+
} else {
64+
return false;
65+
}
66+
}
67+
4868
public String toString() {
4969

5070
String str = "["+type+"]";

src/main/java/duke/TaskList.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,17 @@ public int count() {
5454
public Task getTask(int i) {
5555
return taskList.get(i);
5656
}
57-
57+
58+
public boolean equals(TaskList tl) {
59+
if (this.count() == tl.count()) {
60+
for (int i = 0; i < this.count(); i++) {
61+
if (!this.taskList.get(i).equals(tl.getTask(i)))
62+
return false;
63+
}
64+
return true;
65+
} else {
66+
return false;
67+
}
68+
}
69+
5870
}

src/test/java/duke/DukeTest.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import duke.*;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.time.LocalDate;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class DukeTest {
10+
@Test
11+
public void testSave() {
12+
TaskList tl = new TaskList();
13+
tl.add(new Task("list 1"));
14+
tl.add(new Task("list 2", 'D', LocalDate.now()));
15+
tl.add(new Task("list 3", 'E', LocalDate.now()));
16+
17+
Storage st = new Storage("tasklist2");
18+
st.saveTaskList(tl);
19+
20+
TaskList newTl = st.loadTaskList();
21+
22+
assertEquals(true, tl.equals(newTl));
23+
}
24+
25+
@Test
26+
public void testDelete() {
27+
TaskList tl = new TaskList();
28+
tl.add(new Task("list 1"));
29+
tl.add(new Task("list 2", 'D', LocalDate.now()));
30+
tl.add(new Task("list 3", 'E', LocalDate.now()));
31+
32+
TaskList newTl = new TaskList();
33+
newTl.add(new Task("list 1"));
34+
newTl.add(new Task("list 3", 'E', LocalDate.now()));
35+
36+
Parser p = new Parser();
37+
p.process("delete 2", tl);
38+
39+
assertEquals(true, tl.equals(newTl));
40+
41+
}
42+
43+
@Test
44+
public void testAdd() {
45+
LocalDate ld = LocalDate.parse("2020-01-05");
46+
47+
TaskList tl = new TaskList();
48+
tl.add(new Task("list 1"));
49+
tl.add(new Task("list 2", 'D', ld));
50+
tl.add(new Task("list 3", 'E', ld));
51+
52+
TaskList newTl = new TaskList();
53+
54+
Parser p = new Parser();
55+
p.process("todo list 1", newTl);
56+
p.process("deadline list 2 /by 2020-01-05", newTl);
57+
p.process("event list 3 /at 2020-01-05", newTl);
58+
59+
assertEquals(true, tl.equals(newTl));
60+
61+
}
62+
63+
@Test
64+
public void testDone() {
65+
LocalDate ld = LocalDate.parse("2020-01-05");
66+
67+
TaskList tl = new TaskList();
68+
tl.add(new Task("list 1"));
69+
tl.add(new Task("list 2", 'D', ld));
70+
tl.add(new Task("list 3", 'E', ld));
71+
try {
72+
tl.markDone(2);
73+
} catch (DukeException de) {
74+
75+
}
76+
77+
assertEquals(true, tl.getTask(1).getDone());
78+
79+
}
80+
}

src/test/tasklist2

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
T|ND|list 1
2+
D|ND|list 2|2021-01-26
3+
E|ND|list 3|2021-01-26

0 commit comments

Comments
 (0)