Draw graphviz in tests

This commit is contained in:
gobwas 2016-01-13 01:26:48 +03:00
parent 0be4bc46d1
commit ce3a69147f
18 changed files with 81 additions and 39 deletions

View File

@ -215,6 +215,10 @@ func convertMatchers(matchers []match.Matcher, result []match.Matcher) []match.M
}
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 {
return m, nil
}
@ -233,9 +237,8 @@ func compileMatchers(matchers []match.Matcher) (match.Matcher, error) {
}
}
if val == nil {
return nil, fmt.Errorf("could not convert matchers %s: need at least one matcher", match.Matchers(matchers))
}
// _, ok := val.(match.BTree)
// fmt.Println("a tree", ok)
left := matchers[:idx]
var right []match.Matcher

View File

@ -1,7 +1,10 @@
package glob
import (
"bytes"
"fmt"
"github.com/gobwas/glob/match"
"math/rand"
"reflect"
"testing"
)
@ -35,6 +38,43 @@ func glob(s bool, p, m string, d ...string) test {
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) {
for id, test := range []struct {
pattern string
@ -42,7 +82,11 @@ func TestCompilePattern(t *testing.T) {
exp match.Matcher
}{
{
pattern: "a?*",
pattern: "left*??B*abcd*[!b]??*abc*right",
exp: match.Raw{"t"},
},
{
pattern: "abc*??def",
exp: match.Raw{"t"},
},
} {
@ -53,9 +97,8 @@ func TestCompilePattern(t *testing.T) {
}
matcher := glob.(match.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
}
}

View File

@ -44,5 +44,5 @@ func (self Any) Kind() Kind {
}
func (self Any) String() string {
return fmt.Sprintf("[any:%s]", self.Separators)
return fmt.Sprintf("<any:![%s]>", self.Separators)
}

View File

@ -84,5 +84,5 @@ func (self AnyOf) Kind() Kind {
}
func (self AnyOf) String() string {
return fmt.Sprintf("[any_of:%s]", self.Matchers)
return fmt.Sprintf("<any_of:[%s]>", self.Matchers)
}

View File

@ -112,23 +112,6 @@ func (self BTree) Match(s string) bool {
return false
}
const tpl = `
"%p"[label="%s"]
"%p"[label="%s"]
"%p"[label="%s"]
"%p"->"%p"
"%p"->"%p"
`
func (self BTree) String() string {
// 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)
return fmt.Sprintf("<btree:[%s<-%s->%s]>", self.Left, self.Value, self.Right)
}

View File

@ -61,5 +61,9 @@ func (self Contains) Kind() Kind {
}
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)
}

View File

@ -79,5 +79,5 @@ func (self EveryOf) Kind() Kind {
}
func (self EveryOf) String() string {
return fmt.Sprintf("[every_of:%s]", self.Matchers)
return fmt.Sprintf("<every_of:[%s]>", self.Matchers)
}

View File

@ -40,5 +40,10 @@ func (self List) Index(s string) (int, []int) {
}
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)
}

View File

@ -42,7 +42,7 @@ func (m Matchers) String() string {
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 {

View File

@ -46,5 +46,5 @@ func (self Max) Kind() Kind {
}
func (self Max) String() string {
return fmt.Sprintf("[max:%d]", self.Limit)
return fmt.Sprintf("<max:%d>", self.Limit)
}

View File

@ -45,5 +45,5 @@ func (self Min) Kind() Kind {
}
func (self Min) String() string {
return fmt.Sprintf("[min:%d]", self.Limit)
return fmt.Sprintf("<min:%d>", self.Limit)
}

View File

@ -54,5 +54,5 @@ func (self Prefix) Match(s string) bool {
}
func (self Prefix) String() string {
return fmt.Sprintf("[prefix:%s]", self.Prefix)
return fmt.Sprintf("<prefix:%s>", self.Prefix)
}

View File

@ -60,5 +60,5 @@ func (self PrefixSuffix) Match(s string) bool {
}
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)
}

View File

@ -40,5 +40,9 @@ func (self Range) Index(s string) (int, []int) {
}
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))
}

View File

@ -62,5 +62,5 @@ func (self Row) Kind() Kind {
}
func (self Row) String() string {
return fmt.Sprintf("[row:%s]", self.Matchers)
return fmt.Sprintf("<row:[%s]>", self.Matchers)
}

View File

@ -34,5 +34,5 @@ func (self Single) Kind() Kind {
}
func (self Single) String() string {
return fmt.Sprintf("[single:%s]", self.Separators)
return fmt.Sprintf("<single:![%s]>", self.Separators)
}

View File

@ -39,5 +39,5 @@ func (self Suffix) Match(s string) bool {
}
func (self Suffix) String() string {
return fmt.Sprintf("[suffix:%s]", self.Suffix)
return fmt.Sprintf("<suffix:%s>", self.Suffix)
}

View File

@ -31,5 +31,5 @@ func (self Super) Kind() Kind {
}
func (self Super) String() string {
return fmt.Sprintf("[super]")
return fmt.Sprintf("<super>")
}