-
Notifications
You must be signed in to change notification settings - Fork 3
/
structure_test.go
51 lines (47 loc) · 1.16 KB
/
structure_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
package parse_test
import (
"testing"
"github.com/a-h/parse"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
type ParserTest[T any] struct {
name string
input string
parser parse.Parser[T]
expectedMatch T
expectedOK bool
expectedErr error
}
func RunParserTests[T any](t *testing.T, tests []ParserTest[T]) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
in := parse.NewInput(test.input)
match, ok, err := test.parser.Parse(in)
if err != nil && test.expectedErr == nil {
t.Fatalf("unexpected parser error: %v", err)
}
if test.expectedErr != nil {
if err == nil {
t.Fatalf("expected err=%v, got nil", test.expectedErr)
}
if diff := cmp.Diff(test.expectedErr, err, cmpopts.EquateErrors()); diff != "" {
t.Errorf("error\n:%s", diff)
}
return
}
if ok != test.expectedOK {
t.Errorf("expected ok=%v, got=%v", test.expectedOK, ok)
}
if !test.expectedOK {
if in.Index() != 0 {
t.Error("input not rolled back")
}
return
}
if diff := cmp.Diff(test.expectedMatch, match); diff != "" {
t.Error(diff)
}
})
}
}