-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo_test.go
171 lines (135 loc) · 3.5 KB
/
todo_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package td_test
import (
"os"
"testing"
"github.com/AliiAhmadi/td"
)
// TestAdd tests add method on list by adding
// some tasks and check them
func TestAdd(t *testing.T) {
l := td.List{}
names := []string{
"task 1",
"task 2",
"task 3",
"task 4",
"task 5",
}
for i := 0; i < len(names); i++ {
l.Add(names[i])
if l[i].Task != names[i] {
t.Errorf("TaskAdd: expected %s - got %s", names[i], l[i].Task)
}
}
}
// TestComplete creates a list and add a task to that
// after that check for Done in that
func TestComplete(t *testing.T) {
l := td.List{}
task := "here is a task for testing"
l.Add(task)
if l[0].Task != task {
t.Errorf("TestComplete: expected %s - got %s", task, l[0].Task)
}
if l[0].Done {
t.Errorf("TaskComplete: new task should not be completed")
}
l.Complete(1)
if !l[0].Done {
t.Errorf("TaskComplete: task should be completed")
}
}
// TestDelete tests delete method on list
func TestDelete(t *testing.T) {
l := td.List{}
tasks := []string{
"first task",
"second task",
"third task",
}
for _, task := range tasks {
l.Add(task)
}
if l[0].Task != tasks[0] {
t.Errorf("TestDelete: expected %s - got %s", tasks[0], l[0].Task)
}
l.Delete(2)
if len(l) != 2 {
t.Errorf("TestDelete: expected %d - got %d", 2, len(l))
}
if l[1].Task != tasks[2] {
t.Errorf("TestDelete: expected %s - got %s", tasks[2], l[1].Task)
}
}
// TestSaveGet tests save and get method
// create a temp file by using os package
// and save from l1 to temp file and read
// from file to l2
func TestSaveGet(t *testing.T) {
l1 := td.List{}
l2 := td.List{}
newTask := "new task"
l1.Add(newTask)
if l1[0].Task != newTask {
t.Errorf("TestSaveGet: expected %s - got %s", newTask, l1[0].Task)
}
temp, err := os.CreateTemp("", "")
if err != nil {
t.Fatalf("TestSaveGet: error creating temp file: %s", err.Error())
}
defer os.Remove(temp.Name())
if err = l1.Save(temp.Name()); err != nil {
t.Fatalf("TestSaveGet: error saving list in temp file: %s", err.Error())
}
if err = l2.Get(temp.Name()); err != nil {
t.Fatalf("TestSaveGet: error getting list from file: %s", err.Error())
}
if len(l2) != 1 {
t.Errorf("TestSaveGet: expected length %d - got %d", 1, len(l2))
}
if l2[0].Task != newTask {
t.Errorf("TestSaveGet: expected %s - got %s", newTask, l2[0].Task)
}
}
func TestUncompletingTasks(t *testing.T) {
l := td.List{}
tasks := []string{
"task 1",
"task 2",
"task 3",
}
for _, task := range tasks {
l.Add(task)
}
// Len of list should be 3
if len(l) != 3 {
t.Fatalf("TestUncompletingTasks: expected %d - got %d", 3, len(l))
}
// Save todos to file
temp, err := os.CreateTemp("", "")
if err != nil {
t.Fatalf("TestUncompletingTasks: error creating temp file: %s", err.Error())
}
defer os.Remove(temp.Name())
if err := l.Save(temp.Name()); err != nil {
t.Fatalf("TestUncompletingTasks: error saving list in temp file: %s", err.Error())
}
// Check all tasks added correctly
for i := 0; i < 3; i++ {
if l[i].Task != tasks[i] {
t.Errorf("TestUncompletingTasks: expected %s - got %s", tasks[i], l[i].Task)
}
}
if err := l.Complete(1); err != nil {
t.Fatalf("TestUncompletingTasks: error in completing task: %s", err.Error())
}
if !l[0].Done {
t.Errorf("TestUncompletingTasks: task should be completed %s", l[0].Task)
}
if err := l.Uncomplete(1); err != nil {
t.Fatalf("TestUncompletingTasks: error in uncompleting task: %s", err.Error())
}
if l[0].Done {
t.Errorf("TestUncompletingTasks: task should be uncompleted %s", l[0].Task)
}
}