-
Notifications
You must be signed in to change notification settings - Fork 41
/
todo_test.go
116 lines (95 loc) · 1.88 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
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"io/ioutil"
"log"
"testing"
)
func TestTodo_IsBodySeperator(t *testing.T) {
tests := []struct {
in string
out bool
}{
{"Kappa ---", true},
{"--- Kappa", false},
{"", false},
{"Kappa --- ", false},
}
for _, tt := range tests {
t.Run(tt.in, func(t *testing.T) {
todo := Todo{BodySeparator: "---"}
if got := todo.IsBodySeperator(tt.in); got != tt.out {
t.Errorf("got %t, want %t", got, tt.out)
}
})
}
}
func TestTodo_ParseBodyLine(t *testing.T) {
tests := []struct {
in string
out *string
}{
{"TODO: PogChamp", stringPtr(": PogChamp")},
{"PogChamp", nil},
{"", nil},
}
for _, tt := range tests {
t.Run(tt.in, func(t *testing.T) {
todo := Todo{Prefix: "TODO"}
if got := todo.ParseBodyLine(tt.in); !stringPtrEqual(got, tt.out) {
t.Errorf("got %q, want %q", derefString(got), derefString(tt.out))
}
})
}
}
func TestTodo_RemoveShouldWork(t *testing.T) {
tmp, err := ioutil.TempFile("", "")
if err != nil {
log.Fatal(err)
}
fileContent := `package main
import "fmt"
// TODO: Rewrite this in rust
// No really.
func main() {
fmt.Println("Hello world")
}`
if _, err := tmp.WriteString(fileContent); err != nil {
log.Fatal(err)
}
tmp.Close()
wantFileContent := `package main
import "fmt"
func main() {
fmt.Println("Hello world")
}
`
todo := Todo{
Filename: tmp.Name(),
Prefix: "TODO",
Line: 5,
Body: []string{""},
}
err = todo.Remove()
if err != nil {
log.Fatal(err)
}
b, err := ioutil.ReadFile(tmp.Name())
if err != nil {
log.Fatal(err)
}
if got := string(b); got != wantFileContent {
t.Errorf("got:\n%s\nwant:\n%s", got, wantFileContent)
}
}
func stringPtrEqual(s1, s2 *string) bool {
return derefString(s1) == derefString(s2)
}
func stringPtr(s string) *string {
return &s
}
func derefString(s *string) string {
if s == nil {
return "<nil>"
}
return *s
}