forked from mirror/glob
Draw graphviz in tests
This commit is contained in:
parent
0be4bc46d1
commit
ce3a69147f
|
@ -215,6 +215,10 @@ func convertMatchers(matchers []match.Matcher, result []match.Matcher) []match.M
|
||||||
}
|
}
|
||||||
|
|
||||||
func compileMatchers(matchers []match.Matcher) (match.Matcher, error) {
|
func compileMatchers(matchers []match.Matcher) (match.Matcher, error) {
|
||||||
|
if len(matchers) == 0 {
|
||||||
|
return nil, fmt.Errorf("compile error: need at least one matcher")
|
||||||
|
}
|
||||||
|
|
||||||
if m := glueMatchers(matchers); m != nil {
|
if m := glueMatchers(matchers); m != nil {
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
@ -233,9 +237,8 @@ func compileMatchers(matchers []match.Matcher) (match.Matcher, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if val == nil {
|
// _, ok := val.(match.BTree)
|
||||||
return nil, fmt.Errorf("could not convert matchers %s: need at least one matcher", match.Matchers(matchers))
|
// fmt.Println("a tree", ok)
|
||||||
}
|
|
||||||
|
|
||||||
left := matchers[:idx]
|
left := matchers[:idx]
|
||||||
var right []match.Matcher
|
var right []match.Matcher
|
||||||
|
|
49
glob_test.go
49
glob_test.go
|
@ -1,7 +1,10 @@
|
||||||
package glob
|
package glob
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"github.com/gobwas/glob/match"
|
"github.com/gobwas/glob/match"
|
||||||
|
"math/rand"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
@ -35,6 +38,43 @@ func glob(s bool, p, m string, d ...string) test {
|
||||||
return test{p, m, s, d}
|
return test{p, m, s, d}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func draw(pattern string, m match.Matcher) string {
|
||||||
|
if tree, ok := m.(match.BTree); ok {
|
||||||
|
return fmt.Sprintf(`digraph G {graph[label="%s"];%s}`, pattern, graphviz(tree, fmt.Sprintf("%x", rand.Int63())))
|
||||||
|
}
|
||||||
|
|
||||||
|
return m.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func graphviz(tree match.BTree, id string) string {
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
|
||||||
|
fmt.Fprintf(buf, `"%s"[label="%s"];`, id, tree.Value.String())
|
||||||
|
for _, m := range []match.Matcher{tree.Left, tree.Right} {
|
||||||
|
switch n := m.(type) {
|
||||||
|
case nil:
|
||||||
|
rnd := rand.Int63()
|
||||||
|
fmt.Fprintf(buf, `"%x"[label="<nil>"];`, rnd)
|
||||||
|
// fmt.Fprintf(buf, `"%s"->"%x"[label="len = 0"];`, id, rnd)
|
||||||
|
fmt.Fprintf(buf, `"%s"->"%x";`, id, rnd)
|
||||||
|
|
||||||
|
case match.BTree:
|
||||||
|
sub := fmt.Sprintf("%x", rand.Int63())
|
||||||
|
// fmt.Fprintf(buf, `"%s"->"%s"[label="len=%d"];`, id, sub, n.Len())
|
||||||
|
fmt.Fprintf(buf, `"%s"->"%s";`, id, sub)
|
||||||
|
fmt.Fprintf(buf, graphviz(n, sub))
|
||||||
|
|
||||||
|
default:
|
||||||
|
rnd := rand.Int63()
|
||||||
|
fmt.Fprintf(buf, `"%x"[label="%s"];`, rnd, m.String())
|
||||||
|
// fmt.Fprintf(buf, `"%s"->"%x"[label="len = %d"];`, id, rnd, m.Len())
|
||||||
|
fmt.Fprintf(buf, `"%s"->"%x";`, id, rnd)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
func TestCompilePattern(t *testing.T) {
|
func TestCompilePattern(t *testing.T) {
|
||||||
for id, test := range []struct {
|
for id, test := range []struct {
|
||||||
pattern string
|
pattern string
|
||||||
|
@ -42,7 +82,11 @@ func TestCompilePattern(t *testing.T) {
|
||||||
exp match.Matcher
|
exp match.Matcher
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
pattern: "a?*",
|
pattern: "left*??B*abcd*[!b]??*abc*right",
|
||||||
|
exp: match.Raw{"t"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pattern: "abc*??def",
|
||||||
exp: match.Raw{"t"},
|
exp: match.Raw{"t"},
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
|
@ -53,9 +97,8 @@ func TestCompilePattern(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
matcher := glob.(match.Matcher)
|
matcher := glob.(match.Matcher)
|
||||||
|
|
||||||
if !reflect.DeepEqual(test.exp, matcher) {
|
if !reflect.DeepEqual(test.exp, matcher) {
|
||||||
t.Errorf("#%d unexpected compilation:\nexp: %s\nact: %s", id, test.exp, matcher)
|
t.Errorf("#%d unexpected compilation:\nexp: %s\nact: %s", id, test.exp, draw(test.pattern, matcher))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,5 +44,5 @@ func (self Any) Kind() Kind {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self Any) String() string {
|
func (self Any) String() string {
|
||||||
return fmt.Sprintf("[any:%s]", self.Separators)
|
return fmt.Sprintf("<any:![%s]>", self.Separators)
|
||||||
}
|
}
|
||||||
|
|
|
@ -84,5 +84,5 @@ func (self AnyOf) Kind() Kind {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self AnyOf) String() string {
|
func (self AnyOf) String() string {
|
||||||
return fmt.Sprintf("[any_of:%s]", self.Matchers)
|
return fmt.Sprintf("<any_of:[%s]>", self.Matchers)
|
||||||
}
|
}
|
||||||
|
|
|
@ -112,23 +112,6 @@ func (self BTree) Match(s string) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
const tpl = `
|
|
||||||
"%p"[label="%s"]
|
|
||||||
"%p"[label="%s"]
|
|
||||||
"%p"[label="%s"]
|
|
||||||
"%p"->"%p"
|
|
||||||
"%p"->"%p"
|
|
||||||
`
|
|
||||||
|
|
||||||
func (self BTree) String() string {
|
func (self BTree) String() string {
|
||||||
// return fmt.Sprintf("[btree:%s<-%s->%s]", self.Left, self.Value, self.Right)
|
return fmt.Sprintf("<btree:[%s<-%s->%s]>", self.Left, self.Value, self.Right)
|
||||||
|
|
||||||
l, r := "nil", "nil"
|
|
||||||
if self.Left != nil {
|
|
||||||
l = self.Left.String()
|
|
||||||
}
|
|
||||||
if self.Right != nil {
|
|
||||||
r = self.Right.String()
|
|
||||||
}
|
|
||||||
return fmt.Sprintf(tpl, &self, self.Value, &l, l, &r, r, &self, &l, &self, &r)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,5 +61,9 @@ func (self Contains) Kind() Kind {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self Contains) String() string {
|
func (self Contains) String() string {
|
||||||
return fmt.Sprintf("[contains:needle=%s not=%t]", self.Needle, self.Not)
|
var not string
|
||||||
|
if self.Not {
|
||||||
|
not = "!"
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("<contains:%s[%s]>", not, self.Needle)
|
||||||
}
|
}
|
||||||
|
|
|
@ -79,5 +79,5 @@ func (self EveryOf) Kind() Kind {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self EveryOf) String() string {
|
func (self EveryOf) String() string {
|
||||||
return fmt.Sprintf("[every_of:%s]", self.Matchers)
|
return fmt.Sprintf("<every_of:[%s]>", self.Matchers)
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,5 +40,10 @@ func (self List) Index(s string) (int, []int) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self List) String() string {
|
func (self List) String() string {
|
||||||
return fmt.Sprintf("[list:list=%s not=%t]", self.List, self.Not)
|
var not string
|
||||||
|
if self.Not {
|
||||||
|
not = "!"
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("<list:%s[%s]>", not, self.List)
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,7 +42,7 @@ func (m Matchers) String() string {
|
||||||
s = append(s, fmt.Sprint(matcher))
|
s = append(s, fmt.Sprint(matcher))
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Sprintf("matchers[%s]", strings.Join(s, ","))
|
return fmt.Sprintf("%s", strings.Join(s, ","))
|
||||||
}
|
}
|
||||||
|
|
||||||
func appendIfNotAsPrevious(target []int, val int) []int {
|
func appendIfNotAsPrevious(target []int, val int) []int {
|
||||||
|
|
|
@ -46,5 +46,5 @@ func (self Max) Kind() Kind {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self Max) String() string {
|
func (self Max) String() string {
|
||||||
return fmt.Sprintf("[max:%d]", self.Limit)
|
return fmt.Sprintf("<max:%d>", self.Limit)
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,5 +45,5 @@ func (self Min) Kind() Kind {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self Min) String() string {
|
func (self Min) String() string {
|
||||||
return fmt.Sprintf("[min:%d]", self.Limit)
|
return fmt.Sprintf("<min:%d>", self.Limit)
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,5 +54,5 @@ func (self Prefix) Match(s string) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self Prefix) String() string {
|
func (self Prefix) String() string {
|
||||||
return fmt.Sprintf("[prefix:%s]", self.Prefix)
|
return fmt.Sprintf("<prefix:%s>", self.Prefix)
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,5 +60,5 @@ func (self PrefixSuffix) Match(s string) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self PrefixSuffix) String() string {
|
func (self PrefixSuffix) String() string {
|
||||||
return fmt.Sprintf("[prefix_suffix:%s-%s]", self.Prefix, self.Suffix)
|
return fmt.Sprintf("<prefix_suffix:[%s,%s]>", self.Prefix, self.Suffix)
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,5 +40,9 @@ func (self Range) Index(s string) (int, []int) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self Range) String() string {
|
func (self Range) String() string {
|
||||||
return fmt.Sprintf("[range:%s-%s(%t)]", string(self.Lo), string(self.Hi), self.Not)
|
var not string
|
||||||
|
if self.Not {
|
||||||
|
not = "!"
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("<range:%s[%s,%s]>", not, string(self.Lo), string(self.Hi))
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,5 +62,5 @@ func (self Row) Kind() Kind {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self Row) String() string {
|
func (self Row) String() string {
|
||||||
return fmt.Sprintf("[row:%s]", self.Matchers)
|
return fmt.Sprintf("<row:[%s]>", self.Matchers)
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,5 +34,5 @@ func (self Single) Kind() Kind {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self Single) String() string {
|
func (self Single) String() string {
|
||||||
return fmt.Sprintf("[single:%s]", self.Separators)
|
return fmt.Sprintf("<single:![%s]>", self.Separators)
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,5 +39,5 @@ func (self Suffix) Match(s string) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self Suffix) String() string {
|
func (self Suffix) String() string {
|
||||||
return fmt.Sprintf("[suffix:%s]", self.Suffix)
|
return fmt.Sprintf("<suffix:%s>", self.Suffix)
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,5 +31,5 @@ func (self Super) Kind() Kind {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self Super) String() string {
|
func (self Super) String() string {
|
||||||
return fmt.Sprintf("[super]")
|
return fmt.Sprintf("<super>")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue