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) {
|
2019-02-06 23:43:38 +03:00
|
|
|
for _, test := range []struct {
|
|
|
|
name string
|
|
|
|
ast *ast.Node
|
|
|
|
exp match.Matcher
|
|
|
|
sep []rune
|
2016-05-30 19:35:53 +03:00
|
|
|
}{
|
2016-05-31 11:28:02 +03:00
|
|
|
{
|
2019-02-06 23:43:38 +03:00
|
|
|
// #0
|
2016-05-31 11:28:02 +03:00
|
|
|
ast: ast.NewNode(ast.KindPattern, nil,
|
|
|
|
ast.NewNode(ast.KindText, ast.Text{"abc"}),
|
|
|
|
),
|
2019-02-06 23:43:38 +03:00
|
|
|
exp: match.NewText("abc"),
|
2016-05-31 11:28:02 +03:00
|
|
|
},
|
|
|
|
{
|
2019-02-06 23:43:38 +03:00
|
|
|
// #1
|
2016-05-31 11:28:02 +03:00
|
|
|
ast: ast.NewNode(ast.KindPattern, nil,
|
|
|
|
ast.NewNode(ast.KindAny, nil),
|
|
|
|
),
|
2019-02-06 23:43:38 +03:00
|
|
|
sep: separators,
|
|
|
|
exp: match.NewAny(separators),
|
2016-05-31 11:28:02 +03:00
|
|
|
},
|
|
|
|
{
|
2019-02-06 23:43:38 +03:00
|
|
|
// #2
|
2016-05-31 11:28:02 +03:00
|
|
|
ast: ast.NewNode(ast.KindPattern, nil,
|
|
|
|
ast.NewNode(ast.KindAny, nil),
|
|
|
|
),
|
2019-02-06 23:43:38 +03:00
|
|
|
exp: match.NewSuper(),
|
2016-05-31 11:28:02 +03:00
|
|
|
},
|
|
|
|
{
|
2019-02-06 23:43:38 +03:00
|
|
|
// #3
|
2016-05-31 11:28:02 +03:00
|
|
|
ast: ast.NewNode(ast.KindPattern, nil,
|
|
|
|
ast.NewNode(ast.KindSuper, nil),
|
|
|
|
),
|
2019-02-06 23:43:38 +03:00
|
|
|
exp: match.NewSuper(),
|
2016-05-31 11:28:02 +03:00
|
|
|
},
|
|
|
|
{
|
2019-02-06 23:43:38 +03:00
|
|
|
// #4
|
2016-05-31 11:28:02 +03:00
|
|
|
ast: ast.NewNode(ast.KindPattern, nil,
|
|
|
|
ast.NewNode(ast.KindSingle, nil),
|
|
|
|
),
|
2019-02-06 23:43:38 +03:00
|
|
|
sep: separators,
|
|
|
|
exp: match.NewSingle(separators),
|
2016-05-31 11:28:02 +03:00
|
|
|
},
|
|
|
|
{
|
2019-02-06 23:43:38 +03:00
|
|
|
// #5
|
2016-05-31 11:28:02 +03:00
|
|
|
ast: ast.NewNode(ast.KindPattern, nil,
|
|
|
|
ast.NewNode(ast.KindRange, ast.Range{
|
|
|
|
Lo: 'a',
|
|
|
|
Hi: 'z',
|
|
|
|
Not: true,
|
|
|
|
}),
|
|
|
|
),
|
2019-02-06 23:43:38 +03:00
|
|
|
exp: match.NewRange('a', 'z', true),
|
2016-05-31 11:28:02 +03:00
|
|
|
},
|
|
|
|
{
|
2019-02-06 23:43:38 +03:00
|
|
|
// #6
|
2016-05-31 11:28:02 +03:00
|
|
|
ast: ast.NewNode(ast.KindPattern, nil,
|
|
|
|
ast.NewNode(ast.KindList, ast.List{
|
|
|
|
Chars: "abc",
|
|
|
|
Not: true,
|
|
|
|
}),
|
|
|
|
),
|
2019-02-06 23:43:38 +03:00
|
|
|
exp: match.NewList([]rune{'a', 'b', 'c'}, true),
|
2016-05-31 11:28:02 +03:00
|
|
|
},
|
|
|
|
{
|
2019-02-06 23:43:38 +03:00
|
|
|
// #7
|
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),
|
|
|
|
),
|
|
|
|
sep: separators,
|
2019-02-06 23:43:38 +03:00
|
|
|
exp: 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
|
|
|
},
|
|
|
|
{
|
2019-02-06 23:43:38 +03:00
|
|
|
// #8
|
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),
|
|
|
|
),
|
2019-02-06 23:43:38 +03:00
|
|
|
exp: match.NewMin(3),
|
2016-05-31 11:28:02 +03:00
|
|
|
},
|
|
|
|
{
|
2019-02-06 23:43:38 +03:00
|
|
|
// #9
|
2016-05-31 11:28:02 +03:00
|
|
|
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,
|
2019-02-06 23:43:38 +03:00
|
|
|
exp: match.NewTree(
|
2018-11-24 21:47:12 +03:00
|
|
|
match.NewRow([]match.MatchIndexSizer{
|
|
|
|
match.NewText("abc"),
|
|
|
|
match.NewSingle(separators),
|
|
|
|
}),
|
2016-05-31 11:28:02 +03:00
|
|
|
match.NewAny(separators),
|
2019-03-12 22:15:41 +03:00
|
|
|
match.Nothing{},
|
2016-05-31 11:28:02 +03:00
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
2019-02-06 23:43:38 +03:00
|
|
|
// #10
|
2016-05-31 11:28:02 +03:00
|
|
|
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,
|
2019-02-06 23:43:38 +03:00
|
|
|
exp: match.NewTree(
|
2016-05-31 11:28:02 +03:00
|
|
|
match.NewText("/"),
|
2019-03-12 22:15:41 +03:00
|
|
|
match.Nothing{},
|
2018-11-24 21:47:12 +03:00
|
|
|
match.NewTree(
|
|
|
|
match.MustIndexedAnyOf(
|
|
|
|
match.NewText("z"),
|
|
|
|
match.NewText("ab"),
|
|
|
|
),
|
2019-03-12 22:15:41 +03:00
|
|
|
match.Nothing{},
|
2016-05-31 11:28:02 +03:00
|
|
|
match.NewSuper(),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
2019-02-06 23:43:38 +03:00
|
|
|
// #11
|
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.KindSingle, nil),
|
|
|
|
),
|
|
|
|
sep: separators,
|
2019-02-06 23:43:38 +03:00
|
|
|
exp: match.NewTree(
|
2018-11-24 21:47:12 +03:00
|
|
|
match.NewRow([]match.MatchIndexSizer{
|
|
|
|
match.NewSingle(separators),
|
|
|
|
match.NewText("abc"),
|
|
|
|
match.NewSingle(separators),
|
|
|
|
}),
|
2016-05-31 11:28:02 +03:00
|
|
|
match.NewSuper(),
|
2019-03-12 22:15:41 +03:00
|
|
|
match.Nothing{},
|
2016-05-31 11:28:02 +03:00
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
2019-02-06 23:43:38 +03:00
|
|
|
// #12
|
2016-05-31 11:28:02 +03:00
|
|
|
ast: ast.NewNode(ast.KindPattern, nil,
|
|
|
|
ast.NewNode(ast.KindAny, nil),
|
|
|
|
ast.NewNode(ast.KindText, ast.Text{"abc"}),
|
|
|
|
),
|
2019-02-06 23:43:38 +03:00
|
|
|
exp: match.NewSuffix("abc"),
|
2016-05-31 11:28:02 +03:00
|
|
|
},
|
|
|
|
{
|
2019-02-06 23:43:38 +03:00
|
|
|
// #13
|
2016-05-31 11:28:02 +03:00
|
|
|
ast: ast.NewNode(ast.KindPattern, nil,
|
|
|
|
ast.NewNode(ast.KindText, ast.Text{"abc"}),
|
|
|
|
ast.NewNode(ast.KindAny, nil),
|
|
|
|
),
|
2019-02-06 23:43:38 +03:00
|
|
|
exp: match.NewPrefix("abc"),
|
2016-05-31 11:28:02 +03:00
|
|
|
},
|
|
|
|
{
|
2019-02-06 23:43:38 +03:00
|
|
|
// #14
|
2016-05-31 11:28:02 +03:00
|
|
|
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"}),
|
|
|
|
),
|
2019-02-06 23:43:38 +03:00
|
|
|
exp: match.NewPrefixSuffix("abc", "def"),
|
2016-05-31 11:28:02 +03:00
|
|
|
},
|
|
|
|
{
|
2019-02-06 23:43:38 +03:00
|
|
|
// #15
|
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),
|
|
|
|
),
|
2019-02-06 23:43:38 +03:00
|
|
|
exp: match.NewContains("abc"),
|
2016-05-31 11:28:02 +03:00
|
|
|
},
|
|
|
|
{
|
2019-02-06 23:43:38 +03:00
|
|
|
// #16
|
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,
|
2019-02-06 23:43:38 +03:00
|
|
|
exp: match.NewTree(
|
2016-05-31 11:28:02 +03:00
|
|
|
match.NewText("abc"),
|
|
|
|
match.NewAny(separators),
|
|
|
|
match.NewAny(separators),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
2019-02-06 23:43:38 +03:00
|
|
|
// #17
|
|
|
|
// pattern: "**?abc**?"
|
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),
|
|
|
|
),
|
2019-02-06 23:43:38 +03:00
|
|
|
exp: match.NewTree(
|
2016-05-31 11:28:02 +03:00
|
|
|
match.NewText("abc"),
|
|
|
|
match.NewMin(1),
|
|
|
|
match.NewMin(1),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
2019-02-06 23:43:38 +03:00
|
|
|
// #18
|
2016-05-31 11:28:02 +03:00
|
|
|
ast: ast.NewNode(ast.KindPattern, nil,
|
|
|
|
ast.NewNode(ast.KindText, ast.Text{"abc"}),
|
|
|
|
),
|
2019-02-06 23:43:38 +03:00
|
|
|
exp: match.NewText("abc"),
|
2016-05-31 11:28:02 +03:00
|
|
|
},
|
|
|
|
{
|
2019-02-06 23:43:38 +03:00
|
|
|
// #19
|
2016-05-31 11:28:02 +03:00
|
|
|
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"}),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2019-02-06 23:43:38 +03:00
|
|
|
exp: match.NewText("abc"),
|
2016-05-31 11:28:02 +03:00
|
|
|
},
|
2016-05-30 19:35:53 +03:00
|
|
|
{
|
2019-02-06 23:43:38 +03:00
|
|
|
// #20
|
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
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2019-02-06 23:43:38 +03:00
|
|
|
exp: match.NewTree(
|
2016-05-30 19:35:53 +03:00
|
|
|
match.NewText("abc"),
|
2019-03-12 22:15:41 +03:00
|
|
|
match.Nothing{},
|
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
|
|
|
{
|
2019-02-06 23:43:38 +03:00
|
|
|
// #21
|
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),
|
|
|
|
),
|
2019-02-06 23:43:38 +03:00
|
|
|
exp: match.NewTree(
|
2018-11-24 21:47:12 +03:00
|
|
|
match.NewRow([]match.MatchIndexSizer{
|
|
|
|
match.NewRange('a', 'z', false),
|
|
|
|
match.NewRange('a', 'x', true),
|
|
|
|
}),
|
2019-03-12 22:15:41 +03:00
|
|
|
match.Nothing{},
|
2016-05-31 11:28:02 +03:00
|
|
|
match.NewSuper(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
2019-02-06 23:43:38 +03:00
|
|
|
// #22
|
2016-05-31 11:28:02 +03:00
|
|
|
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"}),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2019-02-06 23:43:38 +03:00
|
|
|
exp: match.NewRow([]match.MatchIndexSizer{
|
2018-11-24 21:47:12 +03:00
|
|
|
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
|
|
|
} {
|
2019-02-06 23:43:38 +03:00
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
act, err := Compile(test.ast, test.sep)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("compilation error: %s", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(act, test.exp) {
|
|
|
|
t.Errorf(
|
|
|
|
"Compile():\nact: %#v\nexp: %#v\n\ngraphviz:\n%s\n%s\n",
|
|
|
|
act, test.exp,
|
|
|
|
match.Graphviz("act", act.(match.Matcher)),
|
|
|
|
match.Graphviz("exp", test.exp.(match.Matcher)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
2016-05-30 19:35:53 +03:00
|
|
|
}
|
|
|
|
}
|