2015-11-30 17:58:20 +03:00
|
|
|
package glob
|
2015-11-30 19:21:30 +03:00
|
|
|
|
2015-11-30 17:58:20 +03:00
|
|
|
import (
|
2015-11-30 19:21:30 +03:00
|
|
|
"testing"
|
2015-11-30 17:58:20 +03:00
|
|
|
)
|
|
|
|
|
2015-12-24 19:00:41 +03:00
|
|
|
const (
|
|
|
|
pattern_all = "[a-z][!a-x]*cat*[h][!b]*eyes*"
|
|
|
|
fixture_all = "my cat has very bright eyes"
|
|
|
|
|
|
|
|
pattern_plain = "google.com"
|
|
|
|
fixture_plain = "google.com"
|
|
|
|
|
|
|
|
pattern_multiple = "https://*.google.*"
|
|
|
|
fixture_multiple = "https://account.google.com"
|
|
|
|
|
|
|
|
pattern_prefix = "abc*"
|
|
|
|
pattern_suffix = "*def"
|
|
|
|
pattern_prefix_suffix = "ab*ef"
|
|
|
|
fixture_prefix_suffix = "abcdef"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2015-11-30 17:58:20 +03:00
|
|
|
type test struct {
|
|
|
|
pattern, match string
|
|
|
|
should bool
|
|
|
|
delimiters []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func glob(s bool, p, m string, d ...string) test {
|
|
|
|
return test{p, m, s, d}
|
|
|
|
}
|
|
|
|
|
2015-12-24 19:00:41 +03:00
|
|
|
func TestIndexByteNonEscaped(t *testing.T) {
|
2015-11-30 19:21:30 +03:00
|
|
|
for _, test := range []struct {
|
2015-11-30 17:58:20 +03:00
|
|
|
s string
|
2015-12-24 17:54:54 +03:00
|
|
|
n, e byte
|
2015-11-30 17:58:20 +03:00
|
|
|
i int
|
|
|
|
}{
|
|
|
|
{
|
2015-12-24 17:54:54 +03:00
|
|
|
"\\n_n",
|
|
|
|
'n',
|
|
|
|
'\\',
|
|
|
|
3,
|
2015-11-30 17:58:20 +03:00
|
|
|
},
|
|
|
|
{
|
2015-12-24 17:54:54 +03:00
|
|
|
"ab",
|
|
|
|
'a',
|
|
|
|
'\\',
|
2015-11-30 17:58:20 +03:00
|
|
|
0,
|
2015-12-24 17:54:54 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"ab",
|
|
|
|
'b',
|
|
|
|
'\\',
|
|
|
|
1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"",
|
|
|
|
'b',
|
|
|
|
'\\',
|
|
|
|
-1,
|
2015-11-30 17:58:20 +03:00
|
|
|
},
|
2015-12-24 19:00:41 +03:00
|
|
|
{
|
|
|
|
"\\b",
|
|
|
|
'b',
|
|
|
|
'\\',
|
|
|
|
-1,
|
|
|
|
},
|
2015-11-30 19:21:30 +03:00
|
|
|
} {
|
2015-12-24 17:54:54 +03:00
|
|
|
i := indexByteNonEscaped(test.s, test.n, test.e, 0)
|
|
|
|
if i != test.i {
|
|
|
|
t.Errorf("unexpeted index: expected %v, got %v", test.i, i)
|
2015-11-30 17:58:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGlob(t *testing.T) {
|
2015-11-30 19:21:30 +03:00
|
|
|
for _, test := range []test{
|
2015-11-30 17:58:20 +03:00
|
|
|
glob(true, "abc", "abc"),
|
|
|
|
glob(true, "a*c", "abc"),
|
|
|
|
glob(true, "a*c", "a12345c"),
|
|
|
|
glob(true, "a?c", "a1c"),
|
|
|
|
glob(true, "a.b", "a.b", "."),
|
|
|
|
glob(true, "a.*", "a.b", "."),
|
|
|
|
glob(true, "a.**", "a.b.c", "."),
|
|
|
|
glob(true, "a.?.c", "a.b.c", "."),
|
|
|
|
glob(true, "a.?.?", "a.b.c", "."),
|
|
|
|
glob(true, "?at", "cat"),
|
2015-11-30 18:24:20 +03:00
|
|
|
glob(true, "?at", "fat"),
|
2015-11-30 17:58:20 +03:00
|
|
|
glob(true, "*", "abc"),
|
2015-11-30 19:01:49 +03:00
|
|
|
glob(true, `\*`, "*"),
|
2015-11-30 17:58:20 +03:00
|
|
|
glob(true, "**", "a.b.c", "."),
|
|
|
|
|
2015-12-01 17:22:17 +03:00
|
|
|
glob(true, "* ?at * eyes", "my cat has very bright eyes"),
|
|
|
|
|
2015-11-30 17:58:20 +03:00
|
|
|
glob(false, "?at", "at"),
|
2015-11-30 18:24:20 +03:00
|
|
|
glob(false, "?at", "fat", "f"),
|
2015-11-30 17:58:20 +03:00
|
|
|
glob(false, "a.*", "a.b.c", "."),
|
|
|
|
glob(false, "a.?.c", "a.bb.c", "."),
|
|
|
|
glob(false, "*", "a.b.c", "."),
|
|
|
|
|
|
|
|
glob(true, "*test", "this is a test"),
|
|
|
|
glob(true, "this*", "this is a test"),
|
|
|
|
glob(true, "*is *", "this is a test"),
|
|
|
|
glob(true, "*is*a*", "this is a test"),
|
|
|
|
glob(true, "**test**", "this is a test"),
|
|
|
|
glob(true, "**is**a***test*", "this is a test"),
|
|
|
|
|
|
|
|
glob(false, "*is", "this is a test"),
|
|
|
|
glob(false, "*no*", "this is a test"),
|
2015-12-24 19:00:41 +03:00
|
|
|
glob(true, "[!a]*", "this is a test"),
|
|
|
|
|
|
|
|
glob(true, pattern_all, fixture_all),
|
|
|
|
glob(true, pattern_plain, fixture_plain),
|
|
|
|
glob(true, pattern_multiple, fixture_multiple),
|
|
|
|
glob(true, pattern_prefix, fixture_prefix_suffix),
|
|
|
|
glob(true, pattern_suffix, fixture_prefix_suffix),
|
|
|
|
glob(true, pattern_prefix_suffix, fixture_prefix_suffix),
|
2015-11-30 19:21:30 +03:00
|
|
|
} {
|
2015-12-24 17:54:54 +03:00
|
|
|
g, err := New(test.pattern, test.delimiters...)
|
|
|
|
if err != nil {
|
2015-12-24 19:00:41 +03:00
|
|
|
t.Errorf("parsing pattern %q error: %s", test.pattern, err)
|
2015-12-24 17:54:54 +03:00
|
|
|
continue
|
|
|
|
}
|
2015-11-30 17:58:20 +03:00
|
|
|
|
|
|
|
result := g.Match(test.match)
|
|
|
|
if result != test.should {
|
|
|
|
t.Errorf("pattern %q matching %q should be %v but got %v", test.pattern, test.match, test.should, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-01 17:22:17 +03:00
|
|
|
|
2015-12-24 19:00:41 +03:00
|
|
|
func BenchmarkParse(b *testing.B) {
|
2015-12-01 17:22:17 +03:00
|
|
|
for i := 0; i < b.N; i++ {
|
2015-12-24 19:00:41 +03:00
|
|
|
New(pattern_all)
|
2015-12-01 17:22:17 +03:00
|
|
|
}
|
|
|
|
}
|
2015-11-30 17:58:20 +03:00
|
|
|
|
2015-12-24 19:00:41 +03:00
|
|
|
func BenchmarkAll(b *testing.B) {
|
|
|
|
m, _ := New(pattern_all)
|
2015-11-30 17:58:20 +03:00
|
|
|
|
2015-11-30 18:24:20 +03:00
|
|
|
for i := 0; i < b.N; i++ {
|
2015-12-24 19:00:41 +03:00
|
|
|
_ = m.Match(fixture_all)
|
2015-11-30 17:58:20 +03:00
|
|
|
}
|
|
|
|
}
|
2015-12-01 17:22:17 +03:00
|
|
|
|
2015-12-24 19:00:41 +03:00
|
|
|
func BenchmarkMultiple(b *testing.B) {
|
|
|
|
m, _ := New(pattern_multiple)
|
2015-12-01 17:22:17 +03:00
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2015-12-24 19:00:41 +03:00
|
|
|
_ = m.Match(fixture_multiple)
|
2015-12-01 17:22:17 +03:00
|
|
|
}
|
|
|
|
}
|
2015-12-24 19:00:41 +03:00
|
|
|
func BenchmarkPlain(b *testing.B) {
|
|
|
|
m, _ := New(pattern_plain)
|
2015-12-01 17:22:17 +03:00
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2015-12-24 19:00:41 +03:00
|
|
|
_ = m.Match(fixture_plain)
|
2015-12-01 17:22:17 +03:00
|
|
|
}
|
|
|
|
}
|
2015-12-24 19:00:41 +03:00
|
|
|
func BenchmarkPrefix(b *testing.B) {
|
|
|
|
m, _ := New(pattern_prefix)
|
2015-12-01 17:22:17 +03:00
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2015-12-24 19:00:41 +03:00
|
|
|
_ = m.Match(fixture_prefix_suffix)
|
2015-12-01 17:22:17 +03:00
|
|
|
}
|
|
|
|
}
|
2015-12-24 19:00:41 +03:00
|
|
|
func BenchmarkSuffix(b *testing.B) {
|
|
|
|
m, _ := New(pattern_suffix)
|
2015-11-30 17:58:20 +03:00
|
|
|
|
2015-12-01 17:22:17 +03:00
|
|
|
for i := 0; i < b.N; i++ {
|
2015-12-24 19:00:41 +03:00
|
|
|
_ = m.Match(fixture_prefix_suffix)
|
2015-12-01 17:22:17 +03:00
|
|
|
}
|
|
|
|
}
|
2015-12-24 19:00:41 +03:00
|
|
|
func BenchmarkPrefixSuffix(b *testing.B) {
|
|
|
|
m, _ := New(pattern_prefix_suffix)
|
2015-12-01 17:22:17 +03:00
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2015-12-24 19:00:41 +03:00
|
|
|
_ = m.Match(fixture_prefix_suffix)
|
2015-12-01 17:22:17 +03:00
|
|
|
}
|
|
|
|
}
|