add pattern matching
This commit is contained in:
118
internal/matcher/radix_test.go
Normal file
118
internal/matcher/radix_test.go
Normal file
@ -0,0 +1,118 @@
|
||||
package matcher_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.loyso.art/frx/eway/internal/matcher"
|
||||
)
|
||||
|
||||
func TestRadixMatcherWithPattern(t *testing.T) {
|
||||
m := matcher.NewRadix()
|
||||
m.Register("aloha")
|
||||
m.Register("hawaii")
|
||||
m.Register("te*")
|
||||
|
||||
var tt = []struct {
|
||||
name string
|
||||
in string
|
||||
pattern string
|
||||
}{{
|
||||
name: "should match exact",
|
||||
in: "aloha",
|
||||
pattern: "aloha",
|
||||
}, {
|
||||
name: "should match exact 2",
|
||||
in: "hawaii",
|
||||
pattern: "hawaii",
|
||||
}, {
|
||||
name: "should match pattern",
|
||||
in: "test",
|
||||
pattern: "te*",
|
||||
}, {
|
||||
name: "should not match",
|
||||
in: "alohe",
|
||||
}, {
|
||||
name: "should not match 2",
|
||||
in: "whoa",
|
||||
}}
|
||||
|
||||
for _, tc := range tt {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
pattern := m.MatchByPattern(tc.in)
|
||||
if pattern != tc.pattern {
|
||||
t.Errorf("expected %s got %s", tc.pattern, pattern)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRadixMatcher(t *testing.T) {
|
||||
m := matcher.NewRadix()
|
||||
m.Register("aloha")
|
||||
m.Register("hawaii")
|
||||
m.Register("te*")
|
||||
|
||||
var tt = []struct {
|
||||
name string
|
||||
in string
|
||||
match bool
|
||||
}{{
|
||||
name: "should match exact",
|
||||
in: "aloha",
|
||||
match: true,
|
||||
}, {
|
||||
name: "should match exact 2",
|
||||
in: "hawaii",
|
||||
match: true,
|
||||
}, {
|
||||
name: "should match pattern",
|
||||
in: "test",
|
||||
match: true,
|
||||
}, {
|
||||
name: "should not match",
|
||||
in: "alohe",
|
||||
}, {
|
||||
name: "should not match 2",
|
||||
in: "whoa",
|
||||
}}
|
||||
|
||||
for _, tc := range tt {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
match := m.Match(tc.in)
|
||||
if match != tc.match {
|
||||
t.Errorf("expected %t got %t", tc.match, match)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRadixMatcherWildcard(t *testing.T) {
|
||||
m := matcher.NewRadix()
|
||||
m.Register("*")
|
||||
|
||||
var tt = []struct {
|
||||
name string
|
||||
in string
|
||||
match bool
|
||||
}{{
|
||||
name: "should match exact",
|
||||
in: "aloha",
|
||||
match: true,
|
||||
}, {
|
||||
name: "should match exact 2",
|
||||
in: "hawaii",
|
||||
match: true,
|
||||
}}
|
||||
|
||||
for _, tc := range tt {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
match := m.Match(tc.in)
|
||||
if match != tc.match {
|
||||
t.Errorf("expected %t got %t", tc.match, match)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user