-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
116 lines (103 loc) · 2.43 KB
/
main_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 (
"bytes"
"log"
"os"
"strings"
"sync"
"testing"
. "github.com/diontr00/go-dochl/internal"
)
func captureOutput(f func()) string {
var buf = new(bytes.Buffer)
log.SetFlags(0)
log.SetOutput(buf)
f()
log.SetOutput(os.Stdout)
return buf.String()
}
func TestParser(t *testing.T) {
tests := []struct {
name string
path string
keyswords []string
expect []string
}{
{
name: "file with default keywords",
path: "test/test1_test.go",
keyswords: nil,
expect: []string{
`test/test1_test.go:5:TODO(fix): need to change x to 2`,
`test/test1_test.go:8:HACK: modified to hack around`,
},
},
{
name: "directory with default keywords ",
path: "./test",
keyswords: nil,
expect: []string{
`test/test1_test.go:5:TODO(fix): need to change x to 2`,
`test/test1_test.go:8:HACK: modified to hack around`,
`test/nested/test2_test.go:5:FIXME: This will break`,
},
},
{
name: "file with custom keywords ",
path: "test2/test3_test.go",
keyswords: []string{"warning", "hey"},
expect: []string{
`test2/test3_test.go:5:HEY(fix): look how cool`,
`test2/test3_test.go:8:WARNING: have been modified`,
},
},
{
name: "directory with custom keywords ",
path: "test2/",
keyswords: []string{"warning", "hey", "error"},
expect: []string{
`test2/test3_test.go:5:HEY(fix): look how cool`,
`test2/test3_test.go:8:WARNING: have been modified`,
`test2/nested/sub/nested/test4_test.go:5:ERROR: Will be error`,
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
d := &dochl{sync.WaitGroup{}}
if tt.keyswords != nil {
SetKeyWords(tt.keyswords)
}
got := captureOutput(func() {
path, err := os.Stat(tt.path)
if err != nil {
t.Errorf("Cannot process path %s : %v", tt.path, err)
}
if path.IsDir() {
d.wg.Add(1)
d.parseDir(tt.path)
d.wg.Done()
d.wg.Wait()
} else {
d.parseFile(tt.path)
}
})
var b bytes.Buffer
for _, e := range tt.expect {
b.WriteString(e)
b.WriteString("\n")
}
expected := b.String()
gots := strings.Split(got, "\n")
if len(tt.expect) != len(gots)-1 {
t.Fatalf("Expect %d result , but got %d result", len(tt.expect), len(gots)-1)
}
for _, g := range gots {
if !strings.Contains(expected, g) {
t.Fatalf("Missing %s", got)
}
}
})
}
}