Debian Codegraph @

annotate_test.go

raw symbols SCIP
1 package annotate
2
3 import (
4 "bytes"
5 "errors"
6 "flag"
7 "fmt"
8 "io/ioutil"
9 "path/filepath"
10 "sort"
11 "strings"
12 "testing"
13 "text/template"
14 "time"
15 "unicode/utf8"
16 )
17
18 var saveExp = flag.Bool("exp", false, "overwrite all expected output files with actual output (returning a failure)")
19 var match = flag.String("m", "", "only run tests whose name contains this string")
20
21 func TestAnnotate(t *testing.T) {
22 tests := map[string]struct {
23 input string
24 anns Annotations
25 want string
26 wantErr error
27 }{
28 "empty and unannotated": {"", nil, "", nil},
29 "unannotated": {"a⌘b", nil, "a⌘b", nil},
30
31 // The docs say "Annotating an empty byte array always returns an empty
32 // byte array.", which is arbitrary but makes implementation easier.
33 "empty annotated": {"", Annotations{{0, 0, []byte("["), []byte("]"), 0}}, "", nil},
34
35 "zero-length annotations": {
36 "aaaa",
37 Annotations{
38 {0, 0, []byte("<b>"), []byte("</b>"), 0},
39 {0, 0, []byte("<i>"), []byte("</i>"), 0},
40 {2, 2, []byte("<i>"), []byte("</i>"), 0},
41 },
42 "<b></b><i></i>aa<i></i>aa",
43 nil,
44 },
45 "1 annotation": {"a", Annotations{{0, 1, []byte("["), []byte("]"), 0}}, "[a]", nil},
46 "nested": {
47 "abc",
48 Annotations{
49 {0, 3, []byte("["), []byte("]"), 0},
50 {1, 2, []byte("<"), []byte(">"), 0},
51 },
52 "[a<b>c]",
53 nil,
54 },
55 "nested 1": {
56 "abcd",
57 Annotations{
58 {0, 4, []byte("<1>"), []byte("</1>"), 0},
59 {1, 3, []byte("<2>"), []byte("</2>"), 0},
60 {2, 2, []byte("<3>"), []byte("</3>"), 0},
61 },
62 "<1>a<2>b<3></3>c</2>d</1>",
63 nil,
64 },
65 "same range": {
66 "ab",
67 Annotations{
68 {0, 2, []byte("["), []byte("]"), 0},
69 {0, 2, []byte("<"), []byte(">"), 0},
70 },
71 "[<ab>]",
72 nil,
73 },
74 "same range (with WantInner)": {
75 "ab",
76 Annotations{
77 {0, 2, []byte("["), []byte("]"), 1},
78 {0, 2, []byte("<"), []byte(">"), 0},
79 },
80 "<[ab]>",
81 nil,
82 },
83 "unicode content": {
84 "abcdef⌘vwxyz",
85 Annotations{
86 {6, 9, []byte("<a>"), []byte("</a>"), 0},
87 {10, 12, []byte("<b>"), []byte("</b>"), 0},
88 {0, 13, []byte("<c>"), []byte("</c>"), 0},
89 },
90 "<c>abcdef<a>⌘</a>v<b>wx</b>y</c>z",
91 nil,
92 },
93 "remainder": {
94 "xyz",
95 Annotations{
96 {0, 2, []byte("<b>"), []byte("</b>"), 0},
97 {0, 1, []byte("<c>"), []byte("</c>"), 0},
98 },
99 "<b><c>x</c>y</b>z",
100 nil,
101 },
102
103 // Overlapping
104 "overlap simple": {
105 "abc",
106 Annotations{
107 {0, 2, []byte("<X>"), []byte("</X>"), 0},
108 {1, 3, []byte("<Y>"), []byte("</Y>"), 0},
109 },
110 // Without re-opening overlapped annotations, we'd get
111 // "<X>a<Y>b</X>c</Y>".
112 "<X>a<Y>b</Y></X><Y>c</Y>",
113 nil,
114 },
115 "overlap simple double": {
116 "abc",
117 Annotations{
118 {0, 2, []byte("<X1>"), []byte("</X1>"), 0},
119 {0, 2, []byte("<X2>"), []byte("</X2>"), 0},
120 {1, 3, []byte("<Y1>"), []byte("</Y1>"), 0},
121 {1, 3, []byte("<Y2>"), []byte("</Y2>"), 0},
122 },
123 "<X1><X2>a<Y1><Y2>b</Y2></Y1></X2></X1><Y1><Y2>c</Y2></Y1>",
124 nil,
125 },
126 "overlap triple complex": {
127 "abcd",
128 Annotations{
129 {0, 2, []byte("<X>"), []byte("</X>"), 0},
130 {1, 3, []byte("<Y>"), []byte("</Y>"), 0},
131 {2, 4, []byte("<Z>"), []byte("</Z>"), 0},
132 },
133 "<X>a<Y>b</Y></X><Y><Z>c</Z></Y><Z>d</Z>",
134 nil,
135 },
136 "overlap same start": {
137 "abcd",
138 Annotations{
139 {0, 2, []byte("<X>"), []byte("</X>"), 0},
140 {0, 3, []byte("<Y>"), []byte("</Y>"), 0},
141 {1, 4, []byte("<Z>"), []byte("</Z>"), 0},
142 },
143 "<Y><X>a<Z>b</Z></X><Z>c</Z></Y><Z>d</Z>",
144 nil,
145 },
146 "overlap (infinite loop regression #1)": {
147 "abcde",
148 Annotations{
149 {0, 3, []byte("<X>"), []byte("</X>"), 0},
150 {1, 5, []byte("<Y>"), []byte("</Y>"), 0},
151 {1, 2, []byte("<Z>"), []byte("</Z>"), 0},
152 },
153 "<X>a<Y><Z>b</Z>c</Y></X><Y>de</Y>",
154 nil,
155 },
156
157 // Errors
158 "start oob": {"a", Annotations{{-1, 1, []byte("<"), []byte(">"), 0}}, "<a>", ErrStartOutOfBounds},
159 "start oob (multiple)": {
160 "a",
161 Annotations{
162 {-3, 1, []byte("1"), []byte(""), 0},
163 {-3, 1, []byte("2"), []byte(""), 0},
164 {-1, 1, []byte("3"), []byte(""), 0},
165 },
166 "123a",
167 ErrStartOutOfBounds,
168 },
169 "end oob": {"a", Annotations{{0, 3, []byte("<"), []byte(">"), 0}}, "<a>", ErrEndOutOfBounds},
170 "end oob (multiple)": {
171 "ab",
172 Annotations{
173 {0, 3, []byte(""), []byte("1"), 0},
174 {1, 3, []byte(""), []byte("2"), 0},
175 {0, 5, []byte(""), []byte("3"), 0},
176 },
177 "ab213",
178 ErrEndOutOfBounds,
179 },
180 }
181 for label, test := range tests {
182 if *match != "" && !strings.Contains(label, *match) {
183 continue
184 }
185
186 sort.Sort(Annotations(test.anns))
187
188 got, err := Annotate([]byte(test.input), test.anns, nil)
189 if err != test.wantErr {
190 if test.wantErr == nil {
191 t.Errorf("%s: Annotate: %s", label, err)
192 } else {
193 t.Errorf("%s: Annotate: got error %v, want %v", label, err, test.wantErr)
194 }
195 }
196 if string(got) != test.want {
197 t.Errorf("%s: Annotate:\ngot %q\nwant %q", label, got, test.want)
198 continue
199 }
200 }
201 }
202
203 func TestAnnotate_Files(t *testing.T) {
204 annsByFile := map[string]Annotations{
205 "hello_world.txt": {
206 {0, 5, []byte("<b>"), []byte("</b>"), 0},
207 {7, 12, []byte("<i>"), []byte("</i>"), 0},
208 },
209 "adjacent.txt": {
210 {0, 3, []byte("<b>"), []byte("</b>"), 0},
211 {3, 6, []byte("<i>"), []byte("</i>"), 0},
212 },
213 "nested_0.txt": {
214 {0, 4, []byte("<1>"), []byte("</1>"), 0},
215 {1, 3, []byte("<2>"), []byte("</2>"), 0},
216 },
217 "nested_2.txt": {
218 {0, 2, []byte("<1>"), []byte("</1>"), 0},
219 {2, 4, []byte("<2>"), []byte("</2>"), 0},
220 {4, 6, []byte("<3>"), []byte("</3>"), 0},
221 {7, 8, []byte("<4>"), []byte("</4>"), 0},
222 },
223 "html.txt": {
224 {193, 203, []byte("<1>"), []byte("</1>"), 0},
225 {336, 339, []byte("<WOOF>"), []byte("</WOOF>"), 0},
226 },
227 }
228
229 dir := "testdata"
230 tests, err := ioutil.ReadDir(dir)
231 if err != nil {
232 t.Fatal(err)
233 }
234
235 for _, test := range tests {
236 name := test.Name()
237 if !strings.Contains(name, *match) {
238 continue
239 }
240 if strings.HasSuffix(name, ".html") {
241 continue
242 }
243 path := filepath.Join(dir, name)
244 input, err := ioutil.ReadFile(path)
245 if err != nil {
246 t.Fatal(err)
247 continue
248 }
249
250 anns := annsByFile[name]
251 sort.Sort(anns)
252
253 got, err := Annotate(input, anns, template.HTMLEscape)
254 if err != nil {
255 t.Errorf("%s: Annotate: %s", name, err)
256 continue
257 }
258
259 expPath := path + ".html"
260 if *saveExp {
261 err = ioutil.WriteFile(expPath, got, 0700)
262 if err != nil {
263 t.Fatal(err)
264 }
265 continue
266 }
267
268 want, err := ioutil.ReadFile(expPath)
269 if err != nil {
270 t.Fatal(err)
271 }
272
273 want = bytes.TrimSpace(want)
274 got = bytes.TrimSpace(got)
275
276 if !bytes.Equal(want, got) {
277 t.Errorf("%s: want %q, got %q", name, want, got)
278 continue
279 }
280 }
281
282 if *saveExp {
283 t.Fatal("overwrote all expected output files with actual output (run tests again without -exp)")
284 }
285 }
286
287 func makeFakeData(size1, size2 int) ([]byte, Annotations) {
288 input := []byte(strings.Repeat(strings.Repeat("a", size1)+"⌘", size2))
289 inputLength := utf8.RuneCount(input)
290 n := len(input)/2 - (size1+1)/2
291 anns := make(Annotations, n)
292 for i := 0; i < n; i++ {
293 if i%2 == 0 {
294 anns[i] = &Annotation{Start: 2 * i, End: 2*i + 1}
295 } else {
296 anns[i] = &Annotation{Start: 2*i - 50, End: 2*i + 50}
297 if anns[i].Start < 0 {
298 anns[i].Start = 0
299 anns[i].End = i
300 }
301 if anns[i].End >= inputLength {
302 anns[i].End = inputLength
303 }
304 }
305 anns[i].Left = []byte("L") //[]byte(strings.Repeat("L", i%20))
306 anns[i].Right = []byte("R") //[]byte(strings.Repeat("R", i%20))
307 anns[i].WantInner = i % 5
308 }
309 sort.Sort(anns)
310 return input, anns
311 }
312
313 func TestAnnotate_GeneratedData(t *testing.T) {
314 input, anns := makeFakeData(1, 15)
315
316 fail := func(err error) {
317 annStrs := make([]string, len(anns))
318 for i, a := range anns {
319 annStrs[i] = fmt.Sprintf("%v", a)
320 }
321 t.Fatalf("Annotate: %s\n\nInput was:\n%q\n\nAnnotations:\n%s", err, input, strings.Join(annStrs, "\n"))
322 }
323
324 tm := time.NewTimer(time.Millisecond * 500)
325 done := make(chan error)
326
327 go func() {
328 _, err := Annotate(input, anns, nil)
329 done <- err
330 }()
331
332 select {
333 case <-tm.C:
334 fail(errors.New("timed out (is there an infinite loop?)"))
335 case err := <-done:
336 if err != nil {
337 fail(err)
338 }
339 }
340 }
341
342 func BenchmarkAnnotate(b *testing.B) {
343 input, anns := makeFakeData(99, 20)
344 b.ResetTimer()
345 for i := 0; i < b.N; i++ {
346 _, err := Annotate(input, anns, nil)
347 if err != nil {
348 b.Fatal(err)
349 }
350 }
351 }