glob/match/optimize_test.go

166 lines
2.7 KiB
Go
Raw Permalink Normal View History

2018-11-24 21:47:12 +03:00
package match
import (
"reflect"
"testing"
)
2019-02-06 23:43:38 +03:00
var separators = []rune{'.'}
2018-11-24 21:47:12 +03:00
func TestCompile(t *testing.T) {
2019-02-06 23:43:38 +03:00
for _, test := range []struct {
2018-11-24 21:47:12 +03:00
in []Matcher
exp Matcher
}{
{
[]Matcher{
NewSuper(),
NewSingle(nil),
},
NewMin(1),
},
{
[]Matcher{
NewAny(separators),
NewSingle(separators),
},
NewEveryOf([]Matcher{
NewMin(1),
2019-02-06 23:43:38 +03:00
NewAny(separators),
2018-11-24 21:47:12 +03:00
}),
},
{
[]Matcher{
NewSingle(nil),
NewSingle(nil),
NewSingle(nil),
},
NewEveryOf([]Matcher{
NewMin(3),
NewMax(3),
}),
},
{
[]Matcher{
NewList([]rune{'a'}, true),
NewAny([]rune{'a'}),
},
NewEveryOf([]Matcher{
NewMin(1),
2019-02-06 23:43:38 +03:00
NewAny([]rune{'a'}),
2018-11-24 21:47:12 +03:00
}),
},
{
[]Matcher{
NewSuper(),
NewSingle(separators),
NewText("c"),
},
NewTree(
NewText("c"),
2019-02-06 23:43:38 +03:00
NewTree(
2018-11-24 21:47:12 +03:00
NewSingle(separators),
NewSuper(),
2019-03-12 22:04:16 +03:00
Nothing{},
2018-11-24 21:47:12 +03:00
),
2019-03-12 22:04:16 +03:00
Nothing{},
2018-11-24 21:47:12 +03:00
),
},
{
[]Matcher{
NewAny(nil),
NewText("c"),
NewAny(nil),
},
NewTree(
NewText("c"),
NewAny(nil),
NewAny(nil),
),
},
{
[]Matcher{
NewRange('a', 'c', true),
NewList([]rune{'z', 't', 'e'}, false),
NewText("c"),
NewSingle(nil),
},
NewRow([]MatchIndexSizer{
NewRange('a', 'c', true),
NewList([]rune{'z', 't', 'e'}, false),
NewText("c"),
NewSingle(nil),
}),
},
} {
2019-02-06 23:43:38 +03:00
t.Run("", func(t *testing.T) {
act, err := Compile(test.in)
if err != nil {
t.Fatalf("Compile() error: %s", err)
}
if !reflect.DeepEqual(act, test.exp) {
t.Errorf(
"Compile():\nact: %#v;\nexp: %#v;\ngraphviz:\n%s\n%s",
act, test.exp,
Graphviz("act", act), Graphviz("exp", test.exp),
)
}
})
2018-11-24 21:47:12 +03:00
}
}
func TestMinimize(t *testing.T) {
2019-02-06 23:43:38 +03:00
for _, test := range []struct {
in, exp []Matcher
2018-11-24 21:47:12 +03:00
}{
{
2019-02-06 23:43:38 +03:00
in: []Matcher{
NewRange('a', 'c', true),
NewList([]rune{'z', 't', 'e'}, false),
NewText("c"),
NewSingle(nil),
NewAny(nil),
2018-11-24 21:47:12 +03:00
},
2019-02-06 23:43:38 +03:00
exp: []Matcher{
NewRow([]MatchIndexSizer{
NewRange('a', 'c', true),
NewList([]rune{'z', 't', 'e'}, false),
NewText("c"),
}),
NewMin(1),
2018-11-24 21:47:12 +03:00
},
},
{
2019-02-06 23:43:38 +03:00
in: []Matcher{
NewRange('a', 'c', true),
NewList([]rune{'z', 't', 'e'}, false),
NewText("c"),
NewSingle(nil),
NewAny(nil),
NewSingle(nil),
NewSingle(nil),
NewAny(nil),
2018-11-24 21:47:12 +03:00
},
2019-02-06 23:43:38 +03:00
exp: []Matcher{
NewRow([]MatchIndexSizer{
NewRange('a', 'c', true),
NewList([]rune{'z', 't', 'e'}, false),
NewText("c"),
}),
NewMin(3),
2018-11-24 21:47:12 +03:00
},
},
} {
2019-02-06 23:43:38 +03:00
t.Run("", func(t *testing.T) {
act := Minimize(test.in)
if !reflect.DeepEqual(act, test.exp) {
t.Errorf(
"Minimize():\nact: %#v;\nexp: %#v",
act, test.exp,
)
}
})
2018-11-24 21:47:12 +03:00
}
}