-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_test.go
86 lines (63 loc) · 1.95 KB
/
list_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
package main
import (
"bufio"
"fmt"
"os"
"testing"
"time"
)
func TestLoadLocal(t *testing.T) {
// Prepare data to be loaded
InitTestingPathVariables(t)
taskfile := CreateTaskfile(TaskfilePath)
taskfile.WriteString("a\nb\nc\n")
taskfile.Close()
// Simulate a tasklist
tl := &Tasklist{
filePath: TaskfilePath,
tasks: make(map[int]Task),
}
tl.LoadLocal()
AssertEqual(t, tl.loaded, true, "Simulated tasklist's loaded field is false")
AssertEqual(t, len(tl.tasks), 3, "Simulated tasklist does not have 3 tasks in it")
}
func TestSaveLocal(t *testing.T) {
InitTestingPathVariables(t)
ConfigOptions.DeleteIfEmpty = true
now := time.Now()
unixBirth := time.Unix(0, 0)
// Simulate a tasklist with data in it
tl := &Tasklist{
filePath: TaskfilePath,
tasks: map[int]Task{
1: {text: "a", hash: "idc", creationDate: now, finishedDate: time.Unix(0, 0)},
},
}
// Check serialization
tl.SerializeTasks()
AssertNotEqual(t, len(tl.serialized), 0, "Simulated tasklist's serialized field is empty")
// Save then check file contents
tl.SaveLocal()
var contents []byte
scanner := bufio.NewScanner(OpenTaskfile(TaskfilePath, false))
for scanner.Scan() {
contents = append(contents, scanner.Bytes()...)
}
if err := scanner.Err(); err != nil {
Error(ErrTaskfileRead, TaskfilePath, err)
}
checkContents := fmt.Sprintf("a | id:idc, creation:%s, finished:%s", now.Format(FullDateFormat), unixBirth.Format(FullDateFormat))
AssertEqual(t, string(contents), checkContents, "Taskfile's contents do not match the manually constructed one")
// Test --delete-if-empty
tl.tasks = make(map[int]Task)
tl.SerializeTasks()
// Save and test if the file still exists
tl.SaveLocal()
if _, err := os.Stat(TaskfilePath); err == nil {
t.Fatal("Empty taskfile was not deleted")
}
// Test if backup was created
if _, err := os.Stat(GetMetafilePath(".bak", TaskfilePath)); os.IsNotExist(err) {
t.Fatal("Backup file was not created after SaveLocal()")
}
}