-
Notifications
You must be signed in to change notification settings - Fork 142
/
grep_test.go
146 lines (117 loc) · 2.98 KB
/
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
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
package the_platinum_searcher
import (
"bytes"
"os"
"strings"
"testing"
)
func TestFixedGrep(t *testing.T) {
opts := defaultOption()
opts.OutputOption.EnableColor = false
opts.OutputOption.EnableGroup = false
pattern, _ := newPattern("go", opts)
paths := []string{
"ascii.txt",
"ja/utf8.txt",
"ja/euc-jp.txt",
"ja/shift_jis.txt",
"ja/broken_utf8.txt",
"ja/broken_euc-jp.txt",
"ja/broken_shift_jis.txt",
}
asserts := []string{
"ascii.txt:2:go test",
"ja/utf8.txt:2:go テスト",
"ja/euc-jp.txt:2:go テスト",
"ja/shift_jis.txt:2:go テスト",
"ja/broken_utf8.txt:2:go テスト",
"ja/broken_euc-jp.txt:2:go テスト",
"ja/broken_shift_jis.txt:2:go テスト",
}
if !assertGrep(pattern, opts, paths, asserts) {
t.Errorf("Grep result should contain assserts.")
}
}
func TestFixedGrepLargeFile(t *testing.T) {
opts := defaultOption()
opts.OutputOption.EnableColor = false
opts.OutputOption.EnableGroup = false
pattern, _ := newPattern("This is a large file.", opts)
paths := []string{"large/large.txt"}
asserts := []string{
"large/large.txt:10:This is a large file.",
}
if !assertGrep(pattern, opts, paths, asserts) {
t.Errorf("Grep result should contain assserts.")
}
}
func TestExtendedGrep(t *testing.T) {
opts := defaultOption()
opts.OutputOption.EnableColor = false
opts.OutputOption.EnableGroup = false
opts.SearchOption.Regexp = true
pattern, _ := newPattern("g.*", opts)
paths := []string{
"ascii.txt",
"ja/utf8.txt",
"ja/euc-jp.txt",
"ja/shift_jis.txt",
"ja/broken_utf8.txt",
"ja/broken_euc-jp.txt",
"ja/broken_shift_jis.txt",
}
asserts := []string{
"ascii.txt:2:go test",
"ja/utf8.txt:2:go テスト",
"ja/euc-jp.txt:2:go テスト",
"ja/shift_jis.txt:2:go テスト",
"ja/broken_utf8.txt:2:go テスト",
"ja/broken_euc-jp.txt:2:go テスト",
"ja/broken_shift_jis.txt:2:go テスト",
}
if !assertGrep(pattern, opts, paths, asserts) {
t.Errorf("Grep result should contain assserts.")
}
}
func TestStdinGrep(t *testing.T) {
// emulate stdin
stashStdin := os.Stdin
fh, _ := os.Open("files/ascii.txt")
os.Stdin = fh
defer func() { os.Stdin = stashStdin }()
opts := defaultOption()
opts.OutputOption.EnableColor = false
opts.SearchOption.SearchStream = true
pattern, _ := newPattern("go", opts)
paths := []string{""} // from stdin
asserts := []string{
"go test",
}
if !assertGrep(pattern, opts, paths, asserts) {
t.Errorf("Grep result should contain assserts.")
}
}
func assertGrep(pattern pattern, opts Option, paths, asserts []string) bool {
buf := new(bytes.Buffer)
printer := newPrinter(pattern, buf, opts)
in := make(chan string)
done := make(chan struct{})
grep := newGrep(pattern, in, done, opts, printer)
go grep.start()
for _, path := range paths {
if path == "" {
in <- path
} else {
in <- "files/" + path
}
}
close(in)
<-done
result := buf.String()
for _, assert := range asserts {
if !strings.Contains(result, assert) {
return false
}
}
return true
}