glob/glob_test.go

195 lines
3.9 KiB
Go
Raw Normal View History

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 (
rGlob "github.com/ryanuber/go-glob"
"regexp"
2015-11-30 19:21:30 +03:00
"testing"
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 17:54:54 +03:00
func TestIndexOfNonEscaped(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-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-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 {
t.Error(err)
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)
}
}
}
const Pattern = "*cat*eyes*"
const ExpPattern = ".*cat.*eyes.*"
const String = "my cat has very bright eyes"
2015-11-30 19:21:30 +03:00
2015-12-01 17:22:17 +03:00
const ProfPattern = "* ?at * eyes"
const ProfString = "my cat has very bright eyes"
2015-11-30 18:24:20 +03:00
//const Pattern = "*.google.com"
//const ExpPattern = ".*google\\.com"
//const String = "mail.google.com"
2015-12-01 17:22:17 +03:00
const PlainPattern = "google.com"
const PlainExpPattern = "google\\.com"
const PlainString = "google.com"
const PSPattern = "https://*.google.com"
const PSExpPattern = `https:\/\/[a-z]+\.google\\.com`
const PSString = "https://account.google.com"
func BenchmarkProf(b *testing.B) {
2015-12-24 17:54:54 +03:00
m, _ := New(Pattern)
2015-12-01 17:22:17 +03:00
for i := 0; i < b.N; i++ {
_ = m.Match(String)
}
}
2015-11-30 17:58:20 +03:00
func BenchmarkGobwas(b *testing.B) {
2015-12-24 17:54:54 +03:00
m, _ := New(Pattern)
2015-11-30 17:58:20 +03:00
2015-11-30 18:24:20 +03:00
for i := 0; i < b.N; i++ {
2015-11-30 17:58:20 +03:00
_ = m.Match(String)
}
}
2015-12-01 17:22:17 +03:00
func BenchmarkGobwasPlain(b *testing.B) {
2015-12-24 17:54:54 +03:00
m, _ := New(PlainPattern)
2015-12-01 17:22:17 +03:00
for i := 0; i < b.N; i++ {
_ = m.Match(PlainString)
}
}
func BenchmarkGobwasPrefix(b *testing.B) {
2015-12-24 17:54:54 +03:00
m, _ := New("abc*")
2015-12-01 17:22:17 +03:00
for i := 0; i < b.N; i++ {
_ = m.Match("abcdef")
}
}
func BenchmarkGobwasSuffix(b *testing.B) {
2015-12-24 17:54:54 +03:00
m, _ := New("*def")
2015-12-01 17:22:17 +03:00
for i := 0; i < b.N; i++ {
_ = m.Match("abcdef")
}
}
func BenchmarkGobwasPrefixSuffix(b *testing.B) {
2015-12-24 17:54:54 +03:00
m, _ := New("ab*ef")
2015-12-01 17:22:17 +03:00
for i := 0; i < b.N; i++ {
_ = m.Match("abcdef")
}
}
2015-11-30 17:58:20 +03:00
func BenchmarkRyanuber(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = rGlob.Glob(Pattern, String)
}
}
2015-12-01 17:22:17 +03:00
func BenchmarkRyanuberPlain(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = rGlob.Glob(PlainPattern, PlainString)
}
}
func BenchmarkRyanuberPrefixSuffix(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = rGlob.Glob(PSPattern, PSString)
}
}
2015-11-30 17:58:20 +03:00
func BenchmarkRegExp(b *testing.B) {
r := regexp.MustCompile(ExpPattern)
for i := 0; i < b.N; i++ {
_ = r.Match([]byte(String))
}
}
2015-12-01 17:22:17 +03:00
func BenchmarkRegExpPrefixSuffix(b *testing.B) {
r := regexp.MustCompile(PSExpPattern)
for i := 0; i < b.N; i++ {
_ = r.Match([]byte(PSString))
}
}