-
Notifications
You must be signed in to change notification settings - Fork 6
/
test.go
87 lines (68 loc) · 1.8 KB
/
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
package test
import (
"testing"
"github.com/hedhyw/rex/pkg/dialect"
"github.com/hedhyw/rex/pkg/dialect/base"
"github.com/hedhyw/rex/pkg/rex"
)
// RexTestCase is a general test case.
type RexTestCase struct {
Name string
Chain []dialect.Token
Expected string
}
// RexTestCasesSlice helps to process slice of test cases.
type RexTestCasesSlice []RexTestCase
// Run runs in parallel.
func (testCases RexTestCasesSlice) Run(t *testing.T) {
t.Parallel()
for _, tc := range testCases {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
b := rex.New(tc.Chain...)
if b.String() != tc.Expected {
t.Fatalf("Actual: %#q, Expected: %#q", b.String(), tc.Expected)
}
})
}
}
// MatchTestCaseSlice helps to process slice of test cases.
type MatchTestCaseSlice []MatchTestCase
// WithMatched defines matched for all test cases.
func (tcs MatchTestCaseSlice) WithMatched(matched bool) MatchTestCaseSlice {
for i := range tcs {
tcs[i].matched = matched
}
return tcs
}
// MatchTestCaseGroupSlice helps to process groups of test cases.
type MatchTestCaseGroupSlice [][]MatchTestCase
// // Run runs in parallel.
func (tcs MatchTestCaseGroupSlice) Run(t *testing.T, tokens ...dialect.Token) {
t.Parallel()
re := rex.New(base.Group.NonCaptured(
base.Chars.Begin(),
base.Group.NonCaptured(tokens...),
base.Chars.End(),
)).MustCompile()
for _, g := range tcs {
for _, tc := range g {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
t.Logf("\nmatching: %#q\nby %#q", tc.Value, re.String())
actual := re.MatchString(tc.Value)
if actual != tc.matched {
t.Fatalf("Actual: %v, Expected: %v", actual, tc.matched)
}
})
}
}
}
// MatchTestCase used for testing matching.
type MatchTestCase struct {
Name string
Value string
matched bool
}