-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo_test.go
103 lines (78 loc) · 1.92 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
package todo_test
import (
"io/ioutil"
"os"
"testing"
todo "github.com/max-frisch/goCliToDoApp"
)
// TestAdd tests the Add method of the List type
func TestAdd(t *testing.T) {
l := todo.List{}
taskName := "New Task"
l.Add(taskName)
if l[0].Task != taskName {
t.Errorf("expected %q, got %q instead.", taskName, l[0].Task)
}
}
// TestComplete tests the Complete method of the List type
func TestComplete(t *testing.T) {
l := todo.List{}
taskName := "New Task"
l.Add(taskName)
if l[0].Task != taskName {
t.Errorf("expected %q, got %q instead.", taskName, l[0].Task)
}
if l[0].Done {
t.Errorf("new task should not be completed.")
}
l.Complete(1)
if !l[0].Done {
t.Errorf("new task should be completed")
}
}
// TestDelete tests the Delete method of the List type
func TestDelete(t *testing.T) {
l := todo.List{}
tasks := []string{
"New Task 1",
"New Task 2",
"New Task 3",
}
for _, v := range tasks {
l.Add(v)
}
if l[0].Task != tasks[0] {
t.Errorf("expected %q, got %q instead.", tasks, l[0].Task)
}
l.Delete(2)
if len(l) != 2 {
t.Errorf("expected list length %q, got %q instead.", 2, len(l))
}
if l[1].Task != tasks[2] {
t.Errorf("expected %q, got %q instead.", tasks[2], l[1].Task)
}
}
// TestSaveGet tests the Save and Get methods of the List type
func TestSaveGet(t *testing.T) {
l1 := todo.List{}
l2 := todo.List{}
taskName := "New Task"
l1.Add(taskName)
if l1[0].Task != taskName {
t.Errorf("expected %q, got %q instead.", taskName, l1[0].Task)
}
tf, err := ioutil.TempFile("", "")
if err != nil {
t.Fatalf("error creating temp file: %s", err)
}
defer os.Remove(tf.Name())
if err := l1.Save(tf.Name()); err != nil {
t.Errorf("error saving list to file: %s", err)
}
if err := l2.Get(tf.Name()); err != nil {
t.Fatalf("error getting list from file: %s", err)
}
if l1[0].Task != l2[0].Task {
t.Errorf("task %q should match %q task.", l1[0].Task, l2[0].Task)
}
}