-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
151 lines (132 loc) · 2.34 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
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
package main
import (
"bytes"
"io"
"os"
"os/exec"
"strings"
"testing"
)
var content []byte
func init() {
b, err := os.ReadFile("testdata/mock.yaml")
if err != nil {
panic("oops")
}
content = b
}
func BenchmarkYAMLGrep(b *testing.B) {
r := bytes.NewReader(content)
f := &filterer{
writer: io.Discard,
grepArgs: []string{"Male"},
}
b.ResetTimer()
if err := lex(r, f.filter); err != nil {
b.Fatal(err)
}
}
func BenchmarkYAMLGrep_Bash(b *testing.B) {
r := bytes.NewReader(content)
b.ResetTimer()
cmd := exec.Command("./testdata/yamlgrep.sh", "Male")
cmd.Stdin = r
cmd.Stdout = io.Discard
b.ResetTimer()
cmd.Run()
}
func TestYAMLGrep(t *testing.T) {
r := bytes.NewReader(content)
buf := &bytes.Buffer{}
f := &filterer{
writer: buf,
grepArgs: []string{"Male"},
}
if err := lex(r, f.filter); err != nil {
t.Fatal(err)
}
expectedLines := 456 * 7
lines := bytes.Count(buf.Bytes(), []byte("\n"))
if lines != expectedLines {
t.Fatalf("incorrect line count, got: %d, expected %d", lines, expectedLines)
}
}
func TestYAMLGrep_CaseInsensitive(t *testing.T) {
r := bytes.NewReader(content)
buf := &bytes.Buffer{}
f := &filterer{
writer: buf,
grepArgs: []string{"-i", "Male"},
}
if err := lex(r, f.filter); err != nil {
t.Fatal(err)
}
expectedLines := 887 * 7
lines := bytes.Count(buf.Bytes(), []byte("\n"))
if lines != expectedLines {
t.Fatalf("incorrect line count, got: %d, expected %d", lines, expectedLines)
}
}
func TestLex_DocCounts(t *testing.T) {
tests := []string{
// 0 Empty
``,
// 1 Empty with leading delim
`---`,
// 2 Single with leading delim
`---
var: 1
`,
// 3 Single without delim
`var: 1`,
// 4 Single with trailing delim
`var: 1
---
`,
// 5 Multi with leading delim
`---
var: 1
---
var: 2
---
var: 3
`,
// 6 Multi without leading delim
`var: 1
---
var: 2
---
var: 3
`,
// 7 Multi with leading and trailing delim
`---
var: 1
---
var: 2
---
var: 3
---
`,
// 8 Multi without leading delim
`---
var: 1
---
var: 2
---
---
var: 3
`,
// 9 mock.yaml
string(content),
}
expected := []int{1, 1, 1, 1, 2, 3, 3, 4, 4, 1000}
for i, f := range tests {
c := 0
r := strings.NewReader(f)
lex(r, func(b []byte) error { c++; return nil })
e := expected[i]
if c != e {
t.Fatalf("incorrect doc count from lex at index %d, got: %d, expected %d", i, c, e)
}
}
}