-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_test.go
More file actions
215 lines (159 loc) · 3.9 KB
/
Copy pathtask_test.go
File metadata and controls
215 lines (159 loc) · 3.9 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"path/filepath"
"testing"
)
func setup(t *testing.T) {
t.Helper()
tasks = []Task{}
dir := t.TempDir()
storageFile = filepath.Join(dir, "temp_tasks.json")
}
func TestCmdAdd(t *testing.T) {
setup(t)
cmdAdd([]string{"buy groceries"})
if len(tasks) != 1 {
t.Fatalf("expected 1 task, got %d", len(tasks))
}
task := tasks[0]
if task.Id != 1 {
t.Errorf("Id = %d, want 1", task.Id)
}
if task.Description != "buy groceries" {
t.Errorf("Description = %q, want %q", task.Description, "Buy groceries")
}
if task.Status != Todo {
t.Errorf("Status = %q, want %q", task.Status, Todo)
}
if task.CreatedAt == "" || task.UpdatedAt == "" {
t.Error("timestamps must not be empty")
}
}
func TestCmdAddMultiWordArg(t *testing.T) {
setup(t)
cmdAdd([]string{"buy", "some", "pants"})
if tasks[0].Description != "buy some pants" {
t.Errorf("Description = %q, want %q", tasks[0].Description, "Buy more groceries")
}
}
func TestCmdAddNoArgs(t *testing.T) {
setup(t)
cmdAdd(nil)
if len(tasks) != 0 {
t.Errorf("expected 0 tasks after invalid add, got %d", len(tasks))
}
}
func TestCmdAddIncrementsID(t *testing.T) {
setup(t)
cmdAdd([]string{"first"})
cmdAdd([]string{"second"})
cmdAdd([]string{"third"})
for i, task := range tasks {
want := uint(i + 1)
if task.Id != want {
t.Errorf("tasks[%d].Id = %d, want %d", i, task.Id, want)
}
}
}
// update tests
func TestCmdUpdate(t *testing.T) {
setup(t)
cmdAdd([]string{"original"})
cmdUpdate([]string{"1", "update description"})
if tasks[0].Description != "update description" {
t.Errorf("Description = %q, want %q", tasks[0].Description, "update description")
}
}
func TestCmdUpdateNotFound(t *testing.T) {
setup(t)
cmdAdd([]string{"task"})
before := tasks[0].Description
cmdUpdate([]string{"99", "new"})
if tasks[0].Description != before {
t.Error("description should be unchanged when ID not found")
}
}
func TestCmdUpdateMissingArgs(t *testing.T) {
setup(t)
cmdAdd([]string{"task"})
before := tasks[0].Description
cmdUpdate([]string{"1"})
if tasks[0].Description != before {
t.Error("description should be unchanged with missing args")
}
}
// status
func TestCmdStatus(t *testing.T) {
setup(t)
cmdAdd([]string{"task"})
transitions := []Status{InProgress, Done, Todo}
for _, s := range transitions {
cmdStatus([]string{"1", string(s)})
if tasks[0].Status != s {
t.Errorf("after setting %q: Status = %q", s, tasks[0].Status)
}
}
}
func TestCmdStatusInvalid(t *testing.T) {
setup(t)
cmdAdd([]string{"task"})
cmdStatus([]string{"1", "invalid"})
if tasks[0].Status != Todo {
t.Errorf("status should stay %q, got %q", Todo, tasks[0].Status)
}
}
func TestCmdStatusNotFound(t *testing.T) {
setup(t)
cmdAdd([]string{"task"})
cmdStatus([]string{"99", "done"})
if tasks[0].Status != Todo {
t.Errorf("status should be unchanged, got %q", tasks[0].Status)
}
}
// Delete
func TestCmdDelete(t *testing.T) {
setup(t)
cmdAdd([]string{"a"})
cmdAdd([]string{"b"})
cmdAdd([]string{"c"})
cmdDelete([]string{"2"})
if len(tasks) != 2 {
t.Fatalf("expected 2 tasks after delete, got %d", len(tasks))
}
for _, t2 := range tasks {
if t2.Id == 2 {
t.Error("deleted task should not exist")
}
}
}
func TestCmdDeleteNotFound(t *testing.T) {
setup(t)
cmdAdd([]string{"task"})
cmdDelete([]string{"99"})
if len(tasks) != 1 {
t.Errorf("expected 1 task, got %d", len(tasks))
}
}
func TestCmdDeleteNoArgs(t *testing.T) {
setup(t)
cmdAdd([]string{"task"})
cmdDelete(nil)
if len(tasks) != 1 {
t.Errorf("expected 1 task, got %d", len(tasks))
}
}
// cmdlist
func TestCmdListFilterByStatus(t *testing.T){
setup(t)
cmdAdd([]string{"todo task"})
cmdAdd([]string{"done task"})
cmdStatus([]string{"2", "done"})
cmdList([]string{"done"})
cmdList([]string{"todo"})
cmdList([]string{"in-progress"})
cmdList(nil)
}
func TestCmdListInvalidFilter(t *testing.T) {
setup(t)
cmdList([]string{"bogus"})
}