glob/compiler_test.go

549 lines
12 KiB
Go
Raw Normal View History

2016-01-08 20:14:31 +03:00
package glob
import (
"github.com/gobwas/glob/match"
2016-05-12 10:46:16 +03:00
"github.com/gobwas/glob/match/debug"
2016-01-08 20:14:31 +03:00
"reflect"
"testing"
)
2016-02-02 22:03:37 +03:00
var separators = []rune{'.'}
2016-01-08 20:14:31 +03:00
func TestGlueMatchers(t *testing.T) {
for id, test := range []struct {
in []match.Matcher
exp match.Matcher
}{
{
[]match.Matcher{
match.NewSuper(),
match.NewSingle(nil),
2016-01-08 20:14:31 +03:00
},
match.NewMin(1),
2016-01-08 20:14:31 +03:00
},
{
[]match.Matcher{
match.NewAny(separators),
match.NewSingle(separators),
2016-01-08 20:14:31 +03:00
},
2016-01-12 14:06:59 +03:00
match.EveryOf{match.Matchers{
match.NewMin(1),
match.NewContains(string(separators), true),
2016-01-08 20:14:31 +03:00
}},
},
{
[]match.Matcher{
match.NewSingle(nil),
match.NewSingle(nil),
match.NewSingle(nil),
2016-01-08 20:14:31 +03:00
},
2016-01-12 14:06:59 +03:00
match.EveryOf{match.Matchers{
match.NewMin(3),
match.NewMax(3),
2016-01-08 20:14:31 +03:00
}},
},
{
[]match.Matcher{
match.NewList([]rune{'a'}, true),
match.NewAny([]rune{'a'}),
2016-01-08 20:14:31 +03:00
},
2016-01-12 14:06:59 +03:00
match.EveryOf{match.Matchers{
match.NewMin(1),
match.NewContains("a", true),
2016-01-08 20:14:31 +03:00
}},
},
} {
2016-01-09 02:34:41 +03:00
act, err := compileMatchers(test.in)
2016-01-08 20:14:31 +03:00
if err != nil {
t.Errorf("#%d convert matchers error: %s", id, err)
continue
}
if !reflect.DeepEqual(act, test.exp) {
t.Errorf("#%d unexpected convert matchers result:\nact: %#v;\nexp: %#v", id, act, test.exp)
2016-01-08 20:14:31 +03:00
continue
}
}
}
2016-01-09 02:34:41 +03:00
func TestCompileMatchers(t *testing.T) {
2016-01-08 20:14:31 +03:00
for id, test := range []struct {
in []match.Matcher
exp match.Matcher
}{
{
[]match.Matcher{
match.NewSuper(),
match.NewSingle(separators),
2016-01-14 21:32:02 +03:00
match.NewText("c"),
2016-01-08 20:14:31 +03:00
},
2016-01-14 18:29:13 +03:00
match.NewBTree(
2016-01-14 21:32:02 +03:00
match.NewText("c"),
2016-01-14 18:29:13 +03:00
match.NewBTree(
match.NewSingle(separators),
match.NewSuper(),
2016-01-14 18:29:13 +03:00
nil,
),
nil,
),
2016-01-08 20:14:31 +03:00
},
{
[]match.Matcher{
match.NewAny(nil),
2016-01-14 21:32:02 +03:00
match.NewText("c"),
match.NewAny(nil),
2016-01-08 20:14:31 +03:00
},
2016-01-14 18:29:13 +03:00
match.NewBTree(
2016-01-14 21:32:02 +03:00
match.NewText("c"),
match.NewAny(nil),
match.NewAny(nil),
2016-01-14 18:29:13 +03:00
),
2016-01-08 20:14:31 +03:00
},
2016-01-09 02:34:41 +03:00
{
[]match.Matcher{
match.NewRange('a', 'c', true),
match.NewList([]rune{'z', 't', 'e'}, false),
2016-01-14 21:32:02 +03:00
match.NewText("c"),
match.NewSingle(nil),
2016-01-09 02:34:41 +03:00
},
match.NewRow(
4,
match.Matchers{
match.NewRange('a', 'c', true),
match.NewList([]rune{'z', 't', 'e'}, false),
2016-01-14 21:32:02 +03:00
match.NewText("c"),
match.NewSingle(nil),
}...,
),
2016-01-09 02:34:41 +03:00
},
2016-01-08 20:14:31 +03:00
} {
2016-01-09 02:34:41 +03:00
act, err := compileMatchers(test.in)
2016-01-08 20:14:31 +03:00
if err != nil {
t.Errorf("#%d convert matchers error: %s", id, err)
continue
}
if !reflect.DeepEqual(act, test.exp) {
t.Errorf("#%d unexpected convert matchers result:\nact: %#v\nexp: %#v", id, act, test.exp)
2016-01-08 20:14:31 +03:00
continue
}
}
}
2016-01-11 10:17:19 +03:00
func TestConvertMatchers(t *testing.T) {
2016-01-09 02:34:41 +03:00
for id, test := range []struct {
in, exp []match.Matcher
}{
{
[]match.Matcher{
match.NewRange('a', 'c', true),
match.NewList([]rune{'z', 't', 'e'}, false),
2016-01-14 21:32:02 +03:00
match.NewText("c"),
match.NewSingle(nil),
match.NewAny(nil),
2016-01-09 02:34:41 +03:00
},
[]match.Matcher{
match.NewRow(
4,
[]match.Matcher{
match.NewRange('a', 'c', true),
match.NewList([]rune{'z', 't', 'e'}, false),
2016-01-14 21:32:02 +03:00
match.NewText("c"),
match.NewSingle(nil),
}...,
),
match.NewAny(nil),
2016-01-09 02:34:41 +03:00
},
},
{
[]match.Matcher{
match.NewRange('a', 'c', true),
match.NewList([]rune{'z', 't', 'e'}, false),
2016-01-14 21:32:02 +03:00
match.NewText("c"),
match.NewSingle(nil),
match.NewAny(nil),
match.NewSingle(nil),
match.NewSingle(nil),
match.NewAny(nil),
2016-01-09 02:34:41 +03:00
},
[]match.Matcher{
match.NewRow(
3,
match.Matchers{
match.NewRange('a', 'c', true),
match.NewList([]rune{'z', 't', 'e'}, false),
2016-01-14 21:32:02 +03:00
match.NewText("c"),
}...,
),
match.NewMin(3),
2016-01-09 02:34:41 +03:00
},
},
} {
2016-01-14 18:29:13 +03:00
act := minimizeMatchers(test.in)
2016-01-09 02:34:41 +03:00
if !reflect.DeepEqual(act, test.exp) {
t.Errorf("#%d unexpected convert matchers 2 result:\nact: %#v\nexp: %#v", id, act, test.exp)
2016-01-09 02:34:41 +03:00
continue
}
}
}
2016-01-08 20:14:31 +03:00
func pattern(nodes ...node) *nodePattern {
return &nodePattern{
nodeImpl: nodeImpl{
desc: nodes,
},
}
}
func anyOf(nodes ...node) *nodeAnyOf {
return &nodeAnyOf{
nodeImpl: nodeImpl{
desc: nodes,
},
}
}
func TestCompiler(t *testing.T) {
for id, test := range []struct {
ast *nodePattern
result Glob
2016-02-02 22:03:37 +03:00
sep []rune
2016-01-08 20:14:31 +03:00
}{
2016-05-12 10:46:16 +03:00
//{
// ast: pattern(&nodeText{text: "abc"}),
// result: match.NewText("abc"),
//},
//{
// ast: pattern(&nodeAny{}),
// sep: separators,
// result: match.NewAny(separators),
//},
//{
// ast: pattern(&nodeAny{}),
// result: match.NewSuper(),
//},
//{
// ast: pattern(&nodeSuper{}),
// result: match.NewSuper(),
//},
//{
// ast: pattern(&nodeSingle{}),
// sep: separators,
// result: match.NewSingle(separators),
//},
//{
// ast: pattern(&nodeRange{
// lo: 'a',
// hi: 'z',
// not: true,
// }),
// result: match.NewRange('a', 'z', true),
//},
//{
// ast: pattern(&nodeList{
// chars: "abc",
// not: true,
// }),
// result: match.NewList([]rune{'a', 'b', 'c'}, true),
//},
//{
// ast: pattern(&nodeAny{}, &nodeSingle{}, &nodeSingle{}, &nodeSingle{}),
// sep: separators,
// result: match.EveryOf{Matchers: match.Matchers{
// match.NewMin(3),
// match.NewContains(string(separators), true),
// }},
//},
//{
// ast: pattern(&nodeAny{}, &nodeSingle{}, &nodeSingle{}, &nodeSingle{}),
// result: match.NewMin(3),
//},
//{
// ast: pattern(&nodeAny{}, &nodeText{text: "abc"}, &nodeSingle{}),
// sep: separators,
// result: match.NewBTree(
// match.NewRow(
// 4,
// match.Matchers{
// match.NewText("abc"),
// match.NewSingle(separators),
// }...,
// ),
// match.NewAny(separators),
// nil,
// ),
//},
2016-01-08 20:14:31 +03:00
{
2016-05-12 10:46:16 +03:00
ast: pattern(&nodeText{text: "/"}, anyOf(&nodeText{text: "z"}, &nodeText{text: "ab"}), &nodeSuper{}),
2016-01-14 18:29:13 +03:00
sep: separators,
result: match.NewBTree(
2016-05-12 10:46:16 +03:00
match.NewText("/"),
2016-01-15 19:50:12 +03:00
nil,
2016-05-12 10:46:16 +03:00
match.NewBTree(
match.NewAnyOf(match.NewText("z"), match.NewText("ab")),
nil,
match.NewSuper(),
2016-01-15 19:50:12 +03:00
),
),
2016-01-15 19:50:12 +03:00
},
2016-05-12 10:46:16 +03:00
//{
// ast: pattern(&nodeSuper{}, &nodeSingle{}, &nodeText{text: "abc"}, &nodeSingle{}),
// sep: separators,
// result: match.NewBTree(
// match.NewRow(
// 5,
// match.Matchers{
// match.NewSingle(separators),
// match.NewText("abc"),
// match.NewSingle(separators),
// }...,
// ),
// match.NewSuper(),
// nil,
// ),
//},
//{
// ast: pattern(&nodeAny{}, &nodeText{text: "abc"}),
// result: match.NewSuffix("abc"),
//},
//{
// ast: pattern(&nodeText{text: "abc"}, &nodeAny{}),
// result: match.NewPrefix("abc"),
//},
//{
// ast: pattern(&nodeText{text: "abc"}, &nodeAny{}, &nodeText{text: "def"}),
// result: match.NewPrefixSuffix("abc", "def"),
//},
//{
// ast: pattern(&nodeAny{}, &nodeAny{}, &nodeAny{}, &nodeText{text: "abc"}, &nodeAny{}, &nodeAny{}),
// result: match.NewContains("abc", false),
//},
//{
// ast: pattern(&nodeAny{}, &nodeAny{}, &nodeAny{}, &nodeText{text: "abc"}, &nodeAny{}, &nodeAny{}),
// sep: separators,
// result: match.NewBTree(
// match.NewText("abc"),
// match.NewAny(separators),
// match.NewAny(separators),
// ),
//},
//{
// ast: pattern(&nodeSuper{}, &nodeSingle{}, &nodeText{text: "abc"}, &nodeSuper{}, &nodeSingle{}),
// result: match.NewBTree(
// match.NewText("abc"),
// match.NewMin(1),
// match.NewMin(1),
// ),
//},
//{
// ast: pattern(anyOf(&nodeText{text: "abc"})),
// result: match.NewText("abc"),
//},
//{
// ast: pattern(anyOf(pattern(anyOf(pattern(&nodeText{text: "abc"}))))),
// result: match.NewText("abc"),
//},
//{
// ast: pattern(anyOf(
// pattern(
// &nodeText{text: "abc"},
// &nodeSingle{},
// ),
// pattern(
// &nodeText{text: "abc"},
// &nodeList{chars: "def"},
// ),
// pattern(
// &nodeText{text: "abc"},
// ),
// pattern(
// &nodeText{text: "abc"},
// ),
// )),
// result: match.NewBTree(
// match.NewText("abc"),
// nil,
// match.AnyOf{Matchers: match.Matchers{
// match.NewSingle(nil),
// match.NewList([]rune{'d', 'e', 'f'}, false),
// match.NewNothing(),
// }},
// ),
//},
//{
// ast: pattern(
// &nodeRange{lo: 'a', hi: 'z'},
// &nodeRange{lo: 'a', hi: 'x', not: true},
// &nodeAny{},
// ),
// result: match.NewBTree(
// match.NewRow(
// 2,
// match.Matchers{
// match.NewRange('a', 'z', false),
// match.NewRange('a', 'x', true),
// }...,
// ),
// nil,
// match.NewSuper(),
// ),
//},
//{
// ast: pattern(anyOf(
// pattern(
// &nodeText{text: "abc"},
// &nodeList{chars: "abc"},
// &nodeText{text: "ghi"},
// ),
// pattern(
// &nodeText{text: "abc"},
// &nodeList{chars: "def"},
// &nodeText{text: "ghi"},
// ),
// )),
// result: match.NewRow(
// 7,
// match.Matchers{
// match.NewText("abc"),
// match.AnyOf{Matchers: match.Matchers{
// match.NewList([]rune{'a', 'b', 'c'}, false),
// match.NewList([]rune{'d', 'e', 'f'}, false),
2016-01-11 10:17:19 +03:00
// }},
2016-05-12 10:46:16 +03:00
// match.NewText("ghi"),
// }...,
// ),
//},
2016-01-08 20:14:31 +03:00
} {
m, err := compile(test.ast, test.sep)
2016-01-08 20:14:31 +03:00
if err != nil {
t.Errorf("compilation error: %s", err)
continue
}
if !reflect.DeepEqual(m, test.result) {
2016-05-12 10:46:16 +03:00
t.Errorf("#%d results are not equal:\nexp: %#v\nact: %#v\nexp:\n%s\nact:\n%s\n", id, test.result, m, debug.Graphviz("", test.result.(match.Matcher)), debug.Graphviz("", m.(match.Matcher)))
2016-01-08 20:14:31 +03:00
continue
}
}
}
2016-01-15 19:50:12 +03:00
const complexityString = "abcd"
//func BenchmarkComplexityAny(b *testing.B) {
// m := match.NewAny(nil)
2016-01-15 19:50:12 +03:00
// for i := 0; i < b.N; i++ {
// _ = m.Match(complexityString)
// _, _ = m.Index(complexityString)
// }
//}
//func BenchmarkComplexityContains(b *testing.B) {
// m := match.NewContains()
2016-01-15 19:50:12 +03:00
// for i := 0; i < b.N; i++ {
// _ = m.Match(complexityString)
// _, _ = m.Index(complexityString)
// }
//}
//func BenchmarkComplexityList(b *testing.B) {
// m := match.NewList()
2016-01-15 19:50:12 +03:00
// for i := 0; i < b.N; i++ {
// _ = m.Match(complexityString)
// _, _ = m.Index(complexityString)
// }
//}
//func BenchmarkComplexityMax(b *testing.B) {
// m := match.NewMax()
2016-01-15 19:50:12 +03:00
// for i := 0; i < b.N; i++ {
// _ = m.Match(complexityString)
// _, _ = m.Index(complexityString)
// }
//}
//func BenchmarkComplexityMin(b *testing.B) {
// m := match.NewMin()
2016-01-15 19:50:12 +03:00
// for i := 0; i < b.N; i++ {
// _ = m.Match(complexityString)
// _, _ = m.Index(complexityString)
// }
//}
//func BenchmarkComplexityNothing(b *testing.B) {
// m := match.NewNothing()
2016-01-15 19:50:12 +03:00
// for i := 0; i < b.N; i++ {
// _ = m.Match(complexityString)
// _, _ = m.Index(complexityString)
// }
//}
//func BenchmarkComplexityPrefix(b *testing.B) {
// m := match.NewPrefix()
2016-01-15 19:50:12 +03:00
// for i := 0; i < b.N; i++ {
// _ = m.Match(complexityString)
// _, _ = m.Index(complexityString)
// }
//}
//func BenchmarkComplexityPrefixSuffix(b *testing.B) {
// m := match.NewPrefixSuffix()
2016-01-15 19:50:12 +03:00
// for i := 0; i < b.N; i++ {
// _ = m.Match(complexityString)
// _, _ = m.Index(complexityString)
// }
//}
//func BenchmarkComplexityRange(b *testing.B) {
// m := match.NewRange()
2016-01-15 19:50:12 +03:00
// for i := 0; i < b.N; i++ {
// _ = m.Match(complexityString)
// _, _ = m.Index(complexityString)
// }
//}
//func BenchmarkComplexityRow(b *testing.B) {
// m := match.NewRow()
2016-01-15 19:50:12 +03:00
// for i := 0; i < b.N; i++ {
// _ = m.Match(complexityString)
// _, _ = m.Index(complexityString)
// }
//}
//func BenchmarkComplexitySingle(b *testing.B) {
// m := match.NewSingle(nil)
2016-01-15 19:50:12 +03:00
// for i := 0; i < b.N; i++ {
// _ = m.Match(complexityString)
// _, _ = m.Index(complexityString)
// }
//}
//func BenchmarkComplexitySuffix(b *testing.B) {
// m := match.NewSuffix()
2016-01-15 19:50:12 +03:00
// for i := 0; i < b.N; i++ {
// _ = m.Match(complexityString)
// _, _ = m.Index(complexityString)
// }
//}
//func BenchmarkComplexitySuper(b *testing.B) {
// m := match.NewSuper()
2016-01-15 19:50:12 +03:00
// for i := 0; i < b.N; i++ {
// _ = m.Match(complexityString)
// _, _ = m.Index(complexityString)
// }
//}
//func BenchmarkComplexityText(b *testing.B) {
// m := match.NewText()
2016-01-15 19:50:12 +03:00
// for i := 0; i < b.N; i++ {
// _ = m.Match(complexityString)
// _, _ = m.Index(complexityString)
// }
//}
//func BenchmarkComplexityAnyOf(b *testing.B) {
// m := match.NewAnyOf()
2016-01-15 19:50:12 +03:00
// for i := 0; i < b.N; i++ {
// _ = m.Match(complexityString)
// _, _ = m.Index(complexityString)
// }
//}
//func BenchmarkComplexityBTree(b *testing.B) {
// m := match.NewBTree(match.NewText("abc"), match.NewText("d"), match.NewText("e"))
// for i := 0; i < b.N; i++ {
// _ = m.Match(complexityString)
// _, _ = m.Index(complexityString)
// }
//}
//func BenchmarkComplexityEveryOf(b *testing.B) {
// m := match.NewEveryOf()
2016-01-15 19:50:12 +03:00
// for i := 0; i < b.N; i++ {
// _ = m.Match(complexityString)
// _, _ = m.Index(complexityString)
// }
//}