-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtc_test.go
242 lines (217 loc) · 5.03 KB
/
tc_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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"
)
// Usages Tests
//
// Basic idea: We want to test running tc with its different options like
// tc
// tc -nocolor
// tc -nofmt
// tc -nocolor -nofmt
//
// To do this, we mock os.Stdin and os.Stdout by creating an io.Reader from
// fake_test_output.txt and io.Writer from actual_output.txt. Run testcolor()
// with this reader and capture the output to writer. Do a file diff between
// actual_output.txt and expected_output.txt and assert no differences
func TestUsage(t *testing.T) {
var (
fakeOutputFile = "fake.txt"
actualOutputFile = "actual.txt"
expectedOutputFile = "expected.txt"
)
cases := []struct {
name string
dir string
opt *options
}{
{
name: "default",
dir: "testdata/default",
opt: &options{nofmt: false, nocolor: false},
},
{
name: "nocolor",
dir: "testdata/nocolor",
opt: &options{nofmt: false, nocolor: true},
},
{
name: "nofmt",
dir: "testdata/nofmt",
opt: &options{nofmt: true, nocolor: false},
},
{
name: "nofmtnocolor",
dir: "testdata/nofmtnocolor",
opt: &options{nofmt: true, nocolor: true},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
// create our parser
p := newParser(tc.opt)
// mock os.Stdin
mockStdin, err := os.Open(filepath.Join(tc.dir, fakeOutputFile))
if err != nil {
t.Error(err)
}
defer close(t, mockStdin)
// mock os.Stdout
mockStdout, err := os.Create(filepath.Join(tc.dir, actualOutputFile))
if err != nil {
t.Error(err)
}
// Run output will be written to actual_output.txt and immediately
// close file once done
runTestColor(p, mockStdin, mockStdout)
close(t, mockStdout)
// Assert no difference
if filesEqual(t,
filepath.Join(tc.dir, actualOutputFile),
filepath.Join(tc.dir, expectedOutputFile),
) != 0 {
t.Error("Actual output file does not match expected")
}
})
}
// Cleanup function to delete files that get created in subtests, i.e. /testdata/*/actual.txt
t.Cleanup(func() {
for _, tc := range cases {
file := filepath.Join(tc.dir, actualOutputFile)
t.Log("Removing created file", file)
os.Remove(file)
}
})
}
func filesEqual(t *testing.T, pathA, pathB string) int {
a, err := os.Open(pathA)
if err != nil {
t.Error(err)
}
b, err := os.Open(pathB)
if err != nil {
t.Error(err)
}
bytesA, _ := ioutil.ReadAll(a)
bytesB, _ := ioutil.ReadAll(b)
return bytes.Compare(bytesA, bytesB)
}
func close(t *testing.T, c io.Closer) {
err := c.Close()
if err != nil {
t.Error(err)
}
}
func TestSearch(t *testing.T) {
cases := []struct {
data []byte
pattern []byte
n int
expected []int
}{
{
data: []byte(""),
pattern: []byte("bb"),
n: 1,
expected: nil,
},
{
data: []byte("aaaaa"),
pattern: []byte("c"),
n: 1,
expected: nil,
},
{
data: []byte("aaabbcc"),
pattern: []byte("bb"),
n: 1,
expected: []int{3},
},
{
data: []byte("aaabbb"),
pattern: []byte("bb"),
n: 2,
expected: []int{3, 4},
},
{
data: []byte("aaabbb"),
pattern: []byte("bb"),
n: 1,
expected: []int{3},
},
{
data: []byte("AABAACAADAABAABA"),
pattern: []byte("AABA"),
n: -1,
expected: []int{0, 9, 12},
},
{
data: []byte(" TestMockSubTest/two: mock_test.go:47: Here's another occurrence of _test.go"),
pattern: []byte("_test.go"),
n: 1,
expected: []int{29},
},
{
data: []byte("_test.go:47: bunch : of : colons:::"),
pattern: []byte(":"),
n: 2,
expected: []int{8, 11},
},
}
for i, tc := range cases {
t.Run(fmt.Sprintf("Test %v", i), func(t *testing.T) {
t.Logf("Searching text \"%s\" for pattern \"%s\"\n", tc.data, tc.pattern)
res := search(tc.data, tc.pattern, tc.n)
// We must deep equal to compare slices
if !reflect.DeepEqual(res, tc.expected) {
t.Errorf("Got %v Expected %v\n", res, tc.expected)
}
})
}
}
// Test search function with larger input data. Read in testdata/moby.txt and
// check it is the number of bytes we expect
//
// $ stat moby.txt
// size 1276201
// ...
//
// Then run our pattern search function and check it is the number we expect
//
// $ cat moby.txt | grep -o "good" | wc -l
// 201
func TestSearchWithLargerInput(t *testing.T) {
testFilePath := "testdata/moby.txt"
expectedSize := 1276201
expectedMatches := 201
data, err := ioutil.ReadFile(testFilePath)
if err != nil {
t.Fatal(err)
}
if len(data) != expectedSize {
t.Errorf("Got %v Expected %v\n", len(data), expectedSize)
}
res := search(data, []byte("good"), -1)
if len(res) != expectedMatches {
t.Errorf("Got %v Expected %v\n", len(res), expectedMatches)
}
}
func BenchmarkSearch(b *testing.B) {
testFilePath := "testdata/moby.txt"
pattern := []byte("good")
data, err := ioutil.ReadFile(testFilePath)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
search(data, pattern, -1)
}
}