1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 // A recorder implements the RuleProcessor interface, whereby its methods
14 // simply record the invocations.
15 type recorder struct {
19 func (r *recorder) Reset(anchor string, before int) error {
21 return fmt.Errorf("before %d > 5", before)
23 r.calls = append(r.calls, fmt.Sprintf("R:%s-%d", anchor, before))
27 func (r *recorder) Insert(level int, str, context, extend string) error {
28 s := fmt.Sprintf("O:%d:%s", level, str)
35 r.calls = append(r.calls, s)
39 func (r *recorder) Index(id string) {
40 r.calls = append(r.calls, fmt.Sprintf("I:%s", id))
43 func (r *recorder) Error(err error) {
44 r.calls = append(r.calls, fmt.Sprintf("E:%v", err))
47 func TestRuleProcessor(t *testing.T) {
48 for _, tt := range []struct {
54 {desc: "whitespace and comments only",
69 & [first tertiary ignorable]
72 &'\u0069' # LATIN SMALL LETTER I
80 R:<first tertiary ignorable/>-0
139 in: "&B<t<<<T<s<<<S<e<<<E",
151 desc: "err operator",
153 out: "E:1: illegal operator 'a'",
156 desc: "err line number",
163 E:3: illegal operator 'a'`,
166 desc: "err empty anchor",
168 out: "E:1: missing string",
171 desc: "err anchor invalid special 1",
173 out: "E:1: unmatched bracket",
176 desc: "err anchor invalid special 2",
178 out: "E:1: unmatched bracket",
181 desc: "err anchor invalid before 1",
183 out: `E:1: strconv.ParseUint: parsing "a": invalid syntax`,
186 desc: "err anchor invalid before 2",
188 out: `E:1: strconv.ParseUint: parsing "12": value out of range`,
191 desc: "err anchor invalid before 3",
193 out: "E:1: missing string",
196 desc: "err anchor invalid before 4",
198 out: "E:1: before 6 > 5",
201 desc: "err empty order",
203 out: "E:1: missing string",
206 desc: "err empty identity",
208 out: "E:1: missing string",
211 desc: "err empty context",
213 out: "E:1: missing string after context",
216 desc: "err empty extend",
218 out: "E:1: missing string after extension",
221 desc: "err empty sequence",
223 out: "E:1: empty sequence",
226 desc: "err sequence 1",
228 out: "E:1: range without starter value",
231 desc: "err sequence 3",
234 E:1: range without starter value
238 desc: "err sequence 3",
241 E:1: invalid range 'b'-'a'
245 desc: "err unmatched quote",
247 out: ` E:1: unmatched single quote
254 {hidden: hidden{CharData: tt.in}},
261 want := strings.Split(strings.TrimSpace(tt.out), "\n")
265 if len(got) != len(want) {
266 t.Errorf("%s: nResults: got %d; want %d", tt.desc, len(got), len(want))
269 for i, g := range got {
270 if want := strings.TrimSpace(want[i]); g != want {
271 t.Errorf("%s:%d: got %q; want %q", tt.desc, i, g, want)