-
Notifications
You must be signed in to change notification settings - Fork 141
/
line_grep_test.go
70 lines (56 loc) · 1.65 KB
/
line_grep_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
package the_platinum_searcher
import (
"bytes"
"os"
"testing"
)
func TestLineGrepOnlyMatch(t *testing.T) {
opts := defaultOption()
expect := `files/context/context.txt:4:go test
files/context/context.txt:6:go test
`
if !assertLineGrep(opts, "files/context/context.txt", expect) {
t.Errorf("Failed line grep (only match).")
}
}
func TestLineGrepContext(t *testing.T) {
opts := defaultOption()
opts.OutputOption.Before = 2
opts.OutputOption.After = 2
expect := `files/context/context.txt:2-before
files/context/context.txt:3-before
files/context/context.txt:4:go test
files/context/context.txt:5-after
files/context/context.txt:6:go test
files/context/context.txt:7-after
files/context/context.txt:8-after
`
if !assertLineGrep(opts, "files/context/context.txt", expect) {
t.Errorf("Failed line grep (context).")
}
}
// Regression test of https://github.com/monochromegane/the_platinum_searcher/issues/166
func TestLineGrepBefore(t *testing.T) {
opts := defaultOption()
opts.OutputOption.Before = 1
expect := `files/context/context.txt:3-before
files/context/context.txt:4:go test
files/context/context.txt:5-after
files/context/context.txt:6:go test
`
if !assertLineGrep(opts, "files/context/context.txt", expect) {
t.Errorf("Failed line grep (before).")
}
}
func assertLineGrep(opts Option, path string, expect string) bool {
buf := new(bytes.Buffer)
printer := newPrinter(pattern{}, buf, opts)
grep := newLineGrep(printer, opts)
f, _ := os.Open(path)
grep.grepEachLines(f, ASCII, func(b []byte) bool {
return bytes.Contains(b, []byte("go"))
}, func(b []byte) int { return 0 })
close(printer.in)
<-printer.done
return buf.String() == expect
}