-
Notifications
You must be signed in to change notification settings - Fork 12
/
todotxt_test.go
522 lines (462 loc) · 14.6 KB
/
todotxt_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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package todotxt
import (
"io/ioutil"
"os"
"testing"
"time"
)
var (
testInputTasklist = "testdata/tasklist_todo.txt"
testInputTasklistCreatedDateError = "testdata/tasklist_createdDate_error.txt"
testInputTasklistDueDateError = "testdata/tasklist_dueDate_error.txt"
testInputTasklistCompletedDateError = "testdata/tasklist_completedDate_error.txt"
testInputTasklistScannerError = "testdata/tasklist_scanner_error.txt"
testOutput = "testdata/ouput_todo.txt"
testExpectedOutput = "testdata/expected_todo.txt"
testTasklist TaskList
testExpected interface{}
testGot interface{}
)
func TestLoadFromFile(t *testing.T) {
file, err := os.Open(testInputTasklist)
if err != nil {
t.Fatal(err)
}
defer file.Close()
if testTasklist, err := LoadFromFile(file); err != nil {
t.Fatal(err)
} else {
data, err := ioutil.ReadFile(testExpectedOutput)
if err != nil {
t.Fatal(err)
}
testExpected = string(data)
testGot = testTasklist.String()
if testGot != testExpected {
t.Errorf("Expected TaskList to be [%s], but got [%s]", testExpected, testGot)
}
}
if testTasklist, err := LoadFromFile(nil); testTasklist != nil || err == nil {
t.Errorf("Expected LoadFromFile to fail, but got TaskList back: [%s]", testTasklist)
}
}
func TestLoadFromFilename(t *testing.T) {
if testTasklist, err := LoadFromFilename(testInputTasklist); err != nil {
t.Fatal(err)
} else {
data, err := ioutil.ReadFile(testExpectedOutput)
if err != nil {
t.Fatal(err)
}
testExpected = string(data)
testGot = testTasklist.String()
if testGot != testExpected {
t.Errorf("Expected TaskList to be [%s], but got [%s]", testExpected, testGot)
}
}
if testTasklist, err := LoadFromFilename("some_file_that_does_not_exists.txt"); testTasklist != nil || err == nil {
t.Errorf("Expected LoadFromFilename to fail, but got TaskList back: [%s]", testTasklist)
}
}
func TestWriteFile(t *testing.T) {
os.Remove(testOutput)
os.Create(testOutput)
var err error
fileInput, err := os.Open(testInputTasklist)
if err != nil {
t.Fatal(err)
}
defer fileInput.Close()
fileOutput, err := os.OpenFile(testOutput, os.O_RDWR, 0644)
if err != nil {
t.Fatal(err)
}
defer fileInput.Close()
if testTasklist, err = LoadFromFile(fileInput); err != nil {
t.Fatal(err)
}
if err = WriteToFile(&testTasklist, fileOutput); err != nil {
t.Fatal(err)
}
fileInput.Close()
fileOutput, err = os.Open(testOutput)
if err != nil {
t.Fatal(err)
}
if testTasklist, err = LoadFromFile(fileOutput); err != nil {
t.Fatal(err)
}
data, err := ioutil.ReadFile(testExpectedOutput)
if err != nil {
t.Fatal(err)
}
testExpected = string(data)
testGot = testTasklist.String()
if testGot != testExpected {
t.Errorf("Expected TaskList to be [%s], but got [%s]", testExpected, testGot)
}
}
func TestTaskListWriteFile(t *testing.T) {
os.Remove(testOutput)
os.Create(testOutput)
testTasklist := TaskList{}
fileInput, err := os.Open(testInputTasklist)
if err != nil {
t.Fatal(err)
}
defer fileInput.Close()
fileOutput, err := os.OpenFile(testOutput, os.O_RDWR, 0644)
if err != nil {
t.Fatal(err)
}
defer fileInput.Close()
if err := testTasklist.LoadFromFile(fileInput); err != nil {
t.Fatal(err)
}
if err := testTasklist.WriteToFile(fileOutput); err != nil {
t.Fatal(err)
}
fileInput.Close()
fileOutput, err = os.Open(testOutput)
if err != nil {
t.Fatal(err)
}
if err := testTasklist.LoadFromFile(fileOutput); err != nil {
t.Fatal(err)
}
data, err := ioutil.ReadFile(testExpectedOutput)
if err != nil {
t.Fatal(err)
}
testExpected = string(data)
testGot = testTasklist.String()
if testGot != testExpected {
t.Errorf("Expected TaskList to be [%s], but got [%s]", testExpected, testGot)
}
}
func TestWriteFilename(t *testing.T) {
os.Remove(testOutput)
var err error
if testTasklist, err = LoadFromFilename(testInputTasklist); err != nil {
t.Fatal(err)
}
if err = WriteToFilename(&testTasklist, testOutput); err != nil {
t.Fatal(err)
}
if testTasklist, err = LoadFromFilename(testOutput); err != nil {
t.Fatal(err)
}
data, err := ioutil.ReadFile(testExpectedOutput)
if err != nil {
t.Fatal(err)
}
testExpected = string(data)
testGot = testTasklist.String()
if testGot != testExpected {
t.Errorf("Expected TaskList to be [%s], but got [%s]", testExpected, testGot)
}
}
func TestTaskListWriteFilename(t *testing.T) {
os.Remove(testOutput)
testTasklist := TaskList{}
if err := testTasklist.LoadFromFilename(testInputTasklist); err != nil {
t.Fatal(err)
}
if err := testTasklist.WriteToFilename(testOutput); err != nil {
t.Fatal(err)
}
if err := testTasklist.LoadFromFilename(testOutput); err != nil {
t.Fatal(err)
}
data, err := ioutil.ReadFile(testExpectedOutput)
if err != nil {
t.Fatal(err)
}
testExpected = string(data)
testGot = testTasklist.String()
if testGot != testExpected {
t.Errorf("Expected TaskList to be [%s], but got [%s]", testExpected, testGot)
}
}
func TestNewTaskList(t *testing.T) {
testTasklist := NewTaskList()
testExpected = 0
testGot = len(testTasklist)
if testGot != testExpected {
t.Errorf("Expected TaskList to contain %d tasks, but got %d", testExpected, testGot)
}
}
func TestTaskListCount(t *testing.T) {
if err := testTasklist.LoadFromFilename(testInputTasklist); err != nil {
t.Fatal(err)
}
testExpected = 63
testGot = len(testTasklist)
if testGot != testExpected {
t.Errorf("Expected TaskList to contain %d tasks, but got %d", testExpected, testGot)
}
}
func TestTaskListAddTask(t *testing.T) {
if err := testTasklist.LoadFromFilename(testInputTasklist); err != nil {
t.Fatal(err)
}
// add new empty task
task := NewTask()
testTasklist.AddTask(&task)
testExpected = 64
testGot = len(testTasklist)
if testGot != testExpected {
t.Errorf("Expected TaskList to contain %d tasks, but got %d", testExpected, testGot)
}
taskId := 64
testExpected = time.Now().Format(DateLayout) + " " // tasks created by NewTask() have their created date set
testGot = testTasklist[taskId-1].String()
if testGot != testExpected {
t.Errorf("Expected Task[%d] to be [%s], but got [%s]", taskId, testExpected, testGot)
}
testExpected = 64
testGot = testTasklist[taskId-1].Id
if testGot != testExpected {
t.Errorf("Expected Task[%d] to be [%d], but got [%d]", taskId, testExpected, testGot)
}
taskId++
// add parsed task
parsed, err := ParseTask("x (C) 2014-01-01 Create golang library documentation @Go +go-todotxt due:2014-01-12")
if err != nil {
t.Error(err)
}
testTasklist.AddTask(parsed)
testExpected = "x (C) 2014-01-01 Create golang library documentation @Go +go-todotxt due:2014-01-12"
testGot = testTasklist[taskId-1].String()
if testGot != testExpected {
t.Errorf("Expected Task[%d] to be [%s], but got [%s]", taskId, testExpected, testGot)
}
testExpected = 65
testGot = testTasklist[taskId-1].Id
if testGot != testExpected {
t.Errorf("Expected Task[%d] to be [%d], but got [%d]", taskId, testExpected, testGot)
}
taskId++
// add selfmade task
createdDate := time.Now()
testTasklist.AddTask(&Task{
CreatedDate: createdDate,
Todo: "Go shopping..",
Contexts: []string{"GroceryStore"},
})
testExpected = createdDate.Format(DateLayout) + " Go shopping.. @GroceryStore"
testGot = testTasklist[taskId-1].String()
if testGot != testExpected {
t.Errorf("Expected Task[%d] to be [%s], but got [%s]", taskId, testExpected, testGot)
}
testExpected = 66
testGot = testTasklist[taskId-1].Id
if testGot != testExpected {
t.Errorf("Expected Task[%d] to be [%d], but got [%d]", taskId, testExpected, testGot)
}
taskId++
// add task with explicit Id, AddTask() should ignore this!
testTasklist.AddTask(&Task{
Id: 101,
})
testExpected = 67
testGot = testTasklist[taskId-1].Id
if testGot != testExpected {
t.Errorf("Expected Task[%d] to be [%d], but got [%d]", taskId, testExpected, testGot)
}
taskId++
}
func TestTaskListGetTask(t *testing.T) {
if err := testTasklist.LoadFromFilename(testInputTasklist); err != nil {
t.Fatal(err)
}
taskId := 3
task, err := testTasklist.GetTask(taskId)
if err != nil {
t.Error(err)
}
testExpected = "(B) 2013-12-01 Outline chapter 5 @Computer +Novel Level:5 private:false due:2014-02-17"
testGot = task.String()
if testGot != testExpected {
t.Errorf("Expected Task[%d] to be [%s], but got [%s]", taskId, testExpected, testGot)
}
testExpected = 3
testGot = testTasklist[taskId-1].Id
if testGot != testExpected {
t.Errorf("Expected Task[%d] to be [%d], but got [%d]", taskId, testExpected, testGot)
}
taskId++
}
func TestTaskListUpdateTask(t *testing.T) {
if err := testTasklist.LoadFromFilename(testInputTasklist); err != nil {
t.Fatal(err)
}
taskId := 3
task, err := testTasklist.GetTask(taskId)
if err != nil {
t.Error(err)
}
testExpected = "(B) 2013-12-01 Outline chapter 5 @Computer +Novel Level:5 private:false due:2014-02-17"
testGot = task.String()
if testGot != testExpected {
t.Errorf("Expected Task[%d] to be [%s], but got [%s]", taskId, testExpected, testGot)
}
testExpected = 3
testGot = testTasklist[taskId-1].Id
if testGot != testExpected {
t.Errorf("Expected Task[%d] to be [%d], but got [%d]", taskId, testExpected, testGot)
}
task.Priority = "C"
task.Todo = "Go home!"
date, err := time.Parse(DateLayout, "2011-11-11")
if err != nil {
t.Error(err)
}
task.DueDate = date
testGot := task
os.Remove(testOutput)
if err := testTasklist.WriteToFilename(testOutput); err != nil {
t.Fatal(err)
}
if err := testTasklist.LoadFromFilename(testOutput); err != nil {
t.Fatal(err)
}
testExpected, err := testTasklist.GetTask(taskId)
if err != nil {
t.Error(err)
}
if testGot.Task() != testExpected.Task() {
t.Errorf("Expected Task to be [%v]\n, but got [%v]", testExpected, testGot)
}
}
func TestTaskListRemoveTaskById(t *testing.T) {
if err := testTasklist.LoadFromFilename(testInputTasklist); err != nil {
t.Fatal(err)
}
taskId := 10
if err := testTasklist.RemoveTaskById(taskId); err != nil {
t.Error(err)
}
testExpected = 62
testGot = len(testTasklist)
if testGot != testExpected {
t.Errorf("Expected TaskList to contain %d tasks, but got %d", testExpected, testGot)
}
task, err := testTasklist.GetTask(taskId)
if err == nil || task != nil {
t.Errorf("Expected no Task to be found anymore, but got %v", task)
}
taskId = 27
if err := testTasklist.RemoveTaskById(taskId); err != nil {
t.Error(err)
}
testExpected = 61
testGot = len(testTasklist)
if testGot != testExpected {
t.Errorf("Expected TaskList to contain %d tasks, but got %d", testExpected, testGot)
}
task, err = testTasklist.GetTask(taskId)
if err == nil || task != nil {
t.Errorf("Expected no Task to be found anymore, but got %v", task)
}
taskId = 99
if err := testTasklist.RemoveTaskById(taskId); err == nil {
t.Errorf("Expected no Task to be found for removal")
}
}
func TestTaskListRemoveTask(t *testing.T) {
if err := testTasklist.LoadFromFilename(testInputTasklist); err != nil {
t.Fatal(err)
}
taskId := 52 // Is "unique" in tasklist
task, err := testTasklist.GetTask(taskId)
if err != nil {
t.Error(err)
}
if err := testTasklist.RemoveTask(*task); err != nil {
t.Error(err)
}
testExpected = 62
testGot = len(testTasklist)
if testGot != testExpected {
t.Errorf("Expected TaskList to contain %d tasks, but got %d", testExpected, testGot)
}
task, err = testTasklist.GetTask(taskId)
if err == nil || task != nil {
t.Errorf("Expected no Task to be found anymore, but got %v", task)
}
taskId = 2 // Exists 3 times in tasklist
task, err = testTasklist.GetTask(taskId)
if err != nil {
t.Error(err)
}
if err := testTasklist.RemoveTask(*task); err != nil {
t.Error(err)
}
testExpected = 59
testGot = len(testTasklist)
if testGot != testExpected {
t.Errorf("Expected TaskList to contain %d tasks, but got %d", testExpected, testGot)
}
task, err = testTasklist.GetTask(taskId)
if err == nil || task != nil {
t.Errorf("Expected no Task to be found anymore, but got %v", task)
}
if err := testTasklist.RemoveTask(NewTask()); err == nil {
t.Errorf("Expected no Task to be found for removal")
}
}
func TestTaskListFilter(t *testing.T) {
if err := testTasklist.LoadFromFilename(testInputTasklist); err != nil {
t.Fatal(err)
}
// Filter list to get only completed tasks
completedList := testTasklist.Filter(func(t Task) bool { return t.Completed })
testExpected = 33
testGot = len(*completedList)
if testGot != testExpected {
t.Errorf("Expected TaskList to contain %d tasks, but got %d", testExpected, testGot)
}
// Filter list to get only tasks with a due date
dueDateList := testTasklist.Filter(func(t Task) bool { return t.HasDueDate() })
testExpected = 26
testGot = len(*dueDateList)
if testGot != testExpected {
t.Errorf("Expected TaskList to contain %d tasks, but got %d", testExpected, testGot)
}
// Filter list to get only tasks with "B" priority
prioBList := testTasklist.Filter(func(t Task) bool {
return t.HasPriority() && t.Priority == "B"
})
testExpected = 17
testGot = len(*prioBList)
if testGot != testExpected {
t.Errorf("Expected TaskList to contain %d tasks, but got %d", testExpected, testGot)
}
}
func TestTaskListReadErrors(t *testing.T) {
if testTasklist, err := LoadFromFilename(testInputTasklistCreatedDateError); testTasklist != nil || err == nil {
t.Errorf("Expected LoadFromFilename to fail because of invalid created date, but got TaskList back: [%s]", testTasklist)
} else if err.Error() != `parsing time "2013-13-01": month out of range` {
t.Error(err)
}
if testTasklist, err := LoadFromFilename(testInputTasklistDueDateError); testTasklist != nil || err == nil {
t.Errorf("Expected LoadFromFilename to fail because of invalid due date, but got TaskList back: [%s]", testTasklist)
} else if err.Error() != `parsing time "2014-02-32": day out of range` {
t.Error(err)
}
if testTasklist, err := LoadFromFilename(testInputTasklistCompletedDateError); testTasklist != nil || err == nil {
t.Errorf("Expected LoadFromFilename to fail because of invalid completed date, but got TaskList back: [%s]", testTasklist)
} else if err.Error() != `parsing time "2014-25-04": month out of range` {
t.Error(err)
}
// really silly test
if testTasklist, err := LoadFromFilename(testInputTasklistScannerError); testTasklist != nil || err == nil {
t.Errorf("Expected LoadFromFilename to fail because of invalid file, but got TaskList back: [%s]", testTasklist)
} else if err.Error() != `bufio.Scanner: token too long` {
t.Error(err)
}
}