-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrop_test.go
102 lines (85 loc) · 3.38 KB
/
grop_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
package grop
import (
"bytes"
"os"
"strings"
"testing"
)
func TestSearch(t *testing.T) {
cases := []struct {
label string
term string
filepath string
results string
}{
{label: "Handle empty term", term: "", filepath: "test_files/A_Mad_Tea_Party.txt", results: ""},
{label: "Return lines matching term", term: "thing", filepath: "test_files/A_Mad_Tea_Party.txt", results: strings.Join([]string{`Alice looked all round the table, but there was nothing on it but tea. "I don't see any wine," she remarked.`,
`"I do," Alice hastily replied; "at least -- at least I mean what I say -- that's the same thing, you know."`,
`"Not the same thing a bit!" said the Hatter. "Why, you might just as well say that "I see what I eat" is the same thing as "I eat what I see!"`,
`"You might just as well say," added the March Hare, "that "I like what I get" is the same thing as "I get what I like"!"`,
`"You might just as well say," added the Dormouse, which seemed to be talking in its sleep, "that "I breathe when I sleep" is the same thing as "I sleep when I breathe"!"`,
`"It is the same thing with you," said the Hatter, and here the conversation dropped, and the party sat silent for a minute, while Alice thought over all she could remember about ravens and writing-desks, which wasn't much.`,
}, "\n") + "\n"},
}
for _, c := range cases {
var buf bytes.Buffer
want := c.results
file, err := os.Open(c.filepath)
if err != nil {
t.Errorf("Unexpected error reading file: %q", err)
}
defer file.Close()
if err = Search(&buf, file, c.term, Options{}); err != nil {
t.Errorf("Unexpected Search() error: %q", err)
}
got := buf.String()
if got != want {
t.Errorf("Search Failed: %q.\nGot: %q\nwant %q", c.label, got, want)
}
}
}
func TestSearchWithOpts(t *testing.T) {
var buf bytes.Buffer
term := "hat"
options := Options{
IgnoreCase: true,
}
inputLines := []string{
"This and that",
"Look at my hat!",
"this one, not THAT!",
"end",
}
searchDoc := strings.Join(inputLines, "\n")
r := strings.NewReader(searchDoc)
err := Search(&buf, r, term, options)
if err != nil {
t.Errorf("Unexpected Error %v", err)
}
want := strings.Join(inputLines[:3], "\n") + "\n"
got := buf.String()
if got != want {
t.Errorf("Case insensitive Search() failed.\nGot: %q\nWant: %q", got, want)
}
}
func TestSearchMulti(t *testing.T) {
var buf bytes.Buffer
term := "hat"
options := Options{
IgnoreCase: true,
WhenHighlight: "always",
}
in := strings.NewReader(`The Hatter was the first to break the silence. "What day of the month is it?" he said, turning to Alice: he had taken his watch out of his pocket, and was looking at it uneasily, shaking it every now and then, and holding it to his ear.`)
err := Search(&buf, in, term, options)
if err != nil {
t.Errorf("Unexpected Error %v", err)
}
// Checking for the following matches with the Red/Reset color characters surrounding matching patterns:
// - "The [Hat]ter ..."
// - "W[hat] day of the ..."
want := "The \x1b[31mHat\x1b[0mter was the first to break the silence. \"W\x1b[31mhat\x1b[0m day of the month is it?\" he said, turning to Alice: he had taken his watch out of his pocket, and was looking at it uneasily, shaking it every now and then, and holding it to his ear.\n"
got := buf.String()
if got != want {
t.Errorf("Case insensitive Search() failed.\nGot: %q\nWant: %q", got, want)
}
}