-
Notifications
You must be signed in to change notification settings - Fork 0
/
t_test.go
153 lines (134 loc) · 3.45 KB
/
t_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
package main
import (
"os"
"os/exec"
"testing"
)
func TestCliAddTask(t *testing.T) {
withCliSetup(t, func() {
cmd := exec.Command("go", "run", "t.go", "foo")
err := cmd.Run()
if err != nil {
t.Fatal(err)
}
listCmd := exec.Command("go", "run", "t.go")
out, err := listCmd.Output()
if err != nil {
t.Fatal(err)
}
outString := string(out)
expected := "0 - foo\n"
if outString != expected {
t.Fatalf("Expected output to be '%s', got '%s'", expected, outString)
}
})
}
func TestCliFinishTask(t *testing.T) {
withCliSetup(t, func() {
cmd := exec.Command("go", "run", "t.go", "foo")
err := cmd.Run()
if err != nil {
t.Fatal(err)
}
finishCmd := exec.Command("go", "run", "t.go", "-f", "0")
err = finishCmd.Run()
if err != nil {
t.Fatal(err)
}
listCmd := exec.Command("go", "run", "t.go")
out, err := listCmd.Output()
if err != nil {
t.Fatal(err)
}
outString := string(out)
if outString != "" {
t.Fatalf("Expected output to be '', got '%s'", outString)
}
})
}
func TestCliEditTask(t *testing.T) {
withCliSetup(t, func() {
cmd := exec.Command("go", "run", "t.go", "foo")
err := cmd.Run()
if err != nil {
t.Fatal(err)
}
editCmd := exec.Command("go", "run", "t.go", "-e", "0", "bar")
err = editCmd.Run()
if err != nil {
t.Fatal(err)
}
listCmd := exec.Command("go", "run", "t.go")
out, err := listCmd.Output()
if err != nil {
t.Fatal(err)
}
outString := string(out)
expected := "0 - bar\n"
if outString != expected {
t.Fatalf("Expected output to be '%s', got '%s'", expected, outString)
}
})
}
func withCliSetup(t *testing.T, testFunc func()) {
origTaskFilePath := os.Getenv("T_TASKS_FILE")
err := os.Setenv("T_TASKS_FILE", "/tmp/tasks")
if err != nil {
t.Fatal(err)
}
defer func() {
os.Remove("/tmp/tasks")
os.Setenv("T_TASKS_FILE", origTaskFilePath)
}()
testFunc()
}
func TestAddTask(t *testing.T) {
tasklist := TaskList{}
tasklist.Add("foo")
if len(tasklist.tasks) != 1 {
t.Fatalf("Expected list to have one element, got %d", len(tasklist.tasks))
}
actualTaskDescription := tasklist.tasks[0].description
if actualTaskDescription != "foo" {
t.Fatalf("expected tasklist to contain 'foo', got '%v'", actualTaskDescription)
}
}
func TestListTasks(t *testing.T) {
tasklist := TaskList{}
tasks := tasklist.List()
if len(tasks) != 0 {
t.Fatalf("Expected tasklist to contain no element, got %d", len(tasks))
}
tasklist.Add("Foo")
tasks = tasklist.List()
if len(tasks) != 1 {
t.Fatalf("Expected tasklist to have one element, got %d", len(tasks))
}
}
func TestFinishTask(t *testing.T) {
tasklist := TaskList{}
tasklist.Add("foo")
if len(tasklist.tasks) != 1 {
t.Fatalf("Expected tasklist to contain one element, got %d", len(tasklist.tasks))
}
tasklist.Finish(0)
if len(tasklist.tasks) != 0 {
t.Fatalf("Expected tasklist to contain no element, got %d", len(tasklist.tasks))
}
}
func TestEditTask(t *testing.T) {
tasklist := TaskList{}
tasklist.Add("foo")
if len(tasklist.tasks) != 1 {
t.Fatalf("Expected tasklist to contain one element, got %d", len(tasklist.tasks))
}
actualTaskDescription := tasklist.tasks[0].description
if actualTaskDescription != "foo" {
t.Fatalf("expected tasklist to contain 'foo', got '%v'", actualTaskDescription)
}
tasklist.Edit(0, "bar")
actualTaskDescription = tasklist.tasks[0].description
if actualTaskDescription != "bar" {
t.Fatalf("expected tasklist to contain 'bar', got '%v'", actualTaskDescription)
}
}