-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_matcher_test.go
61 lines (52 loc) · 1.36 KB
/
string_matcher_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
package hex
import (
"fmt"
"testing"
)
// TODO: Test string matcher
func TestStringMatcher(t *testing.T) {
testCases := []struct {
arg interface{}
input string
want bool
}{
// Exact matching strings
{"foo", "foo", true},
{"foo", "bar", false},
{"", "bar", false},
// Regexp matching
{R("foo"), "barfoobar", true},
{R("^foo"), "foobar", true},
{R("foo$"), "barfoo", true},
{R(`^\d+$`), "123", true},
{R(`^\d+$`), "123.0", false},
// Any/None
{Any, "foo", true},
{Any, "", true},
{Any, "1023", true},
{None, "foo", false},
{None, "", false},
{None, "1023", false},
// Custom matching funcs
{func(s string) bool { return s == "foo" }, "foo", true},
{func(s string) bool { return s == "foo" }, "bar", false},
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("Matching %v against %q", tc.arg, tc.input), func(t *testing.T) {
matcher, err := makeStringMatcher(tc.arg)
if err != nil {
t.Errorf("Unexpected error creating matcher for argument %v", tc.arg)
}
got := matcher.match(tc.input)
if got != tc.want {
t.Errorf("Failed: Got %t, want %t", got, tc.want)
t.FailNow()
}
})
}
}
func TestStringMatcherInvalidArgs(t *testing.T) {
if _, err := makeStringMatcher(struct{}{}); err == nil {
t.Errorf("Expected makeStringMatcher with invalid arguments to return an error, but non was returned")
}
}