glob/compiler/compiler_test.go

314 lines
7.6 KiB
Go
Raw Normal View History

2016-05-30 19:35:53 +03:00
package compiler
import (
2018-02-16 17:36:02 +03:00
"reflect"
"testing"
2016-05-30 19:35:53 +03:00
"github.com/gobwas/glob/match"
"github.com/gobwas/glob/syntax/ast"
)
var separators = []rune{'.'}
func TestCompiler(t *testing.T) {
for id, test := range []struct {
ast *ast.Node
result match.Matcher
sep []rune
}{
2016-05-31 11:28:02 +03:00
{
ast: ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindText, ast.Text{"abc"}),
),
result: match.NewText("abc"),
},
{
ast: ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindAny, nil),
),
sep: separators,
result: match.NewAny(separators),
},
{
ast: ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindAny, nil),
),
result: match.NewSuper(),
},
{
ast: ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindSuper, nil),
),
result: match.NewSuper(),
},
{
ast: ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindSingle, nil),
),
sep: separators,
result: match.NewSingle(separators),
},
{
ast: ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindRange, ast.Range{
Lo: 'a',
Hi: 'z',
Not: true,
}),
),
result: match.NewRange('a', 'z', true),
},
{
ast: ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindList, ast.List{
Chars: "abc",
Not: true,
}),
),
result: match.NewList([]rune{'a', 'b', 'c'}, true),
},
{
ast: ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindAny, nil),
ast.NewNode(ast.KindSingle, nil),
ast.NewNode(ast.KindSingle, nil),
ast.NewNode(ast.KindSingle, nil),
),
sep: separators,
2018-11-24 21:47:12 +03:00
result: match.NewEveryOf([]match.Matcher{
2016-05-31 11:28:02 +03:00
match.NewMin(3),
2018-11-24 21:47:12 +03:00
match.NewAny(separators),
}),
2016-05-31 11:28:02 +03:00
},
{
ast: ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindAny, nil),
ast.NewNode(ast.KindSingle, nil),
ast.NewNode(ast.KindSingle, nil),
ast.NewNode(ast.KindSingle, nil),
),
result: match.NewMin(3),
},
{
ast: ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindAny, nil),
ast.NewNode(ast.KindText, ast.Text{"abc"}),
ast.NewNode(ast.KindSingle, nil),
),
sep: separators,
2018-11-24 21:47:12 +03:00
result: match.NewTree(
match.NewRow([]match.MatchIndexSizer{
match.NewText("abc"),
match.NewSingle(separators),
}),
2016-05-31 11:28:02 +03:00
match.NewAny(separators),
nil,
),
},
{
ast: ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindText, ast.Text{"/"}),
ast.NewNode(ast.KindAnyOf, nil,
ast.NewNode(ast.KindText, ast.Text{"z"}),
ast.NewNode(ast.KindText, ast.Text{"ab"}),
),
ast.NewNode(ast.KindSuper, nil),
),
sep: separators,
2018-11-24 21:47:12 +03:00
result: match.NewTree(
2016-05-31 11:28:02 +03:00
match.NewText("/"),
nil,
2018-11-24 21:47:12 +03:00
match.NewTree(
match.MustIndexedAnyOf(
match.NewText("z"),
match.NewText("ab"),
),
2016-05-31 11:28:02 +03:00
nil,
match.NewSuper(),
),
),
},
{
ast: ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindSuper, nil),
ast.NewNode(ast.KindSingle, nil),
ast.NewNode(ast.KindText, ast.Text{"abc"}),
ast.NewNode(ast.KindSingle, nil),
),
sep: separators,
2018-11-24 21:47:12 +03:00
result: match.NewTree(
match.NewRow([]match.MatchIndexSizer{
match.NewSingle(separators),
match.NewText("abc"),
match.NewSingle(separators),
}),
2016-05-31 11:28:02 +03:00
match.NewSuper(),
nil,
),
},
{
ast: ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindAny, nil),
ast.NewNode(ast.KindText, ast.Text{"abc"}),
),
result: match.NewSuffix("abc"),
},
{
ast: ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindText, ast.Text{"abc"}),
ast.NewNode(ast.KindAny, nil),
),
result: match.NewPrefix("abc"),
},
{
ast: ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindText, ast.Text{"abc"}),
ast.NewNode(ast.KindAny, nil),
ast.NewNode(ast.KindText, ast.Text{"def"}),
),
result: match.NewPrefixSuffix("abc", "def"),
},
{
ast: ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindAny, nil),
ast.NewNode(ast.KindAny, nil),
ast.NewNode(ast.KindAny, nil),
ast.NewNode(ast.KindText, ast.Text{"abc"}),
ast.NewNode(ast.KindAny, nil),
ast.NewNode(ast.KindAny, nil),
),
2018-11-24 21:47:12 +03:00
result: match.NewContains("abc"),
2016-05-31 11:28:02 +03:00
},
{
ast: ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindAny, nil),
ast.NewNode(ast.KindAny, nil),
ast.NewNode(ast.KindAny, nil),
ast.NewNode(ast.KindText, ast.Text{"abc"}),
ast.NewNode(ast.KindAny, nil),
ast.NewNode(ast.KindAny, nil),
),
sep: separators,
2018-11-24 21:47:12 +03:00
result: match.NewTree(
2016-05-31 11:28:02 +03:00
match.NewText("abc"),
match.NewAny(separators),
match.NewAny(separators),
),
},
{
2018-11-24 21:47:12 +03:00
// TODO: THIS!
2016-05-31 11:28:02 +03:00
ast: ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindSuper, nil),
ast.NewNode(ast.KindSingle, nil),
ast.NewNode(ast.KindText, ast.Text{"abc"}),
ast.NewNode(ast.KindSuper, nil),
ast.NewNode(ast.KindSingle, nil),
),
2018-11-24 21:47:12 +03:00
result: match.NewTree(
2016-05-31 11:28:02 +03:00
match.NewText("abc"),
match.NewMin(1),
match.NewMin(1),
),
},
{
ast: ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindText, ast.Text{"abc"}),
),
result: match.NewText("abc"),
},
{
ast: ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindAnyOf, nil,
ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindAnyOf, nil,
ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindText, ast.Text{"abc"}),
),
),
),
),
),
result: match.NewText("abc"),
},
2016-05-30 19:35:53 +03:00
{
ast: ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindAnyOf, nil,
ast.NewNode(ast.KindPattern, nil,
2016-05-31 11:28:02 +03:00
ast.NewNode(ast.KindText, ast.Text{"abc"}),
2016-05-30 19:35:53 +03:00
ast.NewNode(ast.KindSingle, nil),
),
ast.NewNode(ast.KindPattern, nil,
2016-05-31 11:28:02 +03:00
ast.NewNode(ast.KindText, ast.Text{"abc"}),
ast.NewNode(ast.KindList, ast.List{Chars: "def"}),
2016-05-30 19:35:53 +03:00
),
ast.NewNode(ast.KindPattern, nil,
2016-05-31 11:28:02 +03:00
ast.NewNode(ast.KindText, ast.Text{"abc"}),
2016-05-30 19:35:53 +03:00
),
ast.NewNode(ast.KindPattern, nil,
2016-05-31 11:28:02 +03:00
ast.NewNode(ast.KindText, ast.Text{"abc"}),
2016-05-30 19:35:53 +03:00
),
),
),
2018-11-24 21:47:12 +03:00
result: match.NewTree(
2016-05-30 19:35:53 +03:00
match.NewText("abc"),
nil,
2018-11-24 21:47:12 +03:00
match.NewAnyOf(
2016-05-30 19:35:53 +03:00
match.NewSingle(nil),
match.NewList([]rune{'d', 'e', 'f'}, false),
match.NewNothing(),
2018-11-24 21:47:12 +03:00
),
2016-05-30 19:35:53 +03:00
),
},
2016-05-31 11:28:02 +03:00
{
ast: ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindRange, ast.Range{Lo: 'a', Hi: 'z'}),
ast.NewNode(ast.KindRange, ast.Range{Lo: 'a', Hi: 'x', Not: true}),
ast.NewNode(ast.KindAny, nil),
),
2018-11-24 21:47:12 +03:00
result: match.NewTree(
match.NewRow([]match.MatchIndexSizer{
match.NewRange('a', 'z', false),
match.NewRange('a', 'x', true),
}),
2016-05-31 11:28:02 +03:00
nil,
match.NewSuper(),
),
},
{
ast: ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindAnyOf, nil,
ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindText, ast.Text{"abc"}),
ast.NewNode(ast.KindList, ast.List{Chars: "abc"}),
ast.NewNode(ast.KindText, ast.Text{"ghi"}),
),
ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindText, ast.Text{"abc"}),
ast.NewNode(ast.KindList, ast.List{Chars: "def"}),
ast.NewNode(ast.KindText, ast.Text{"ghi"}),
),
),
),
2018-11-24 21:47:12 +03:00
result: match.NewRow([]match.MatchIndexSizer{
match.NewText("abc"),
match.MustIndexedSizedAnyOf(
match.NewList([]rune{'a', 'b', 'c'}, false),
match.NewList([]rune{'d', 'e', 'f'}, false),
),
match.NewText("ghi"),
}),
2016-05-31 11:28:02 +03:00
},
2016-05-30 19:35:53 +03:00
} {
m, err := Compile(test.ast, test.sep)
if err != nil {
t.Errorf("compilation error: %s", err)
continue
}
if !reflect.DeepEqual(m, test.result) {
2018-11-24 21:47:12 +03:00
t.Errorf("[%d] Compile():\nexp: %#v\nact: %#v\n\ngraphviz:\nexp:\n%s\nact:\n%s\n", id, test.result, m, match.Graphviz("", test.result.(match.Matcher)), match.Graphviz("", m.(match.Matcher)))
2016-05-30 19:35:53 +03:00
continue
}
}
}