Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions glob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,16 @@ func TestGlob(t *testing.T) {

glob(true, pattern_alternatives_combine_lite, fixture_alternatives_combine_lite),

// Two-alternative brace expansion with variable-length matchers (regression for
// AnyOf.Len incorrectly returning a non-(-1) length when one matcher has unknown
// length and another has a known length).
glob(true, "{**/daxing,daxing}/**/*dev*.yaml", "playground/daxing/generated/dev.yaml", '/'),
glob(true, "{**/daxing,daxing}/**/*dev*.yaml", "daxing/generated/dev.yaml", '/'),
glob(false, "{**/daxing,daxing}/**/*dev*.yaml", "playground/other/generated/dev.yaml", '/'),
glob(true, "{**/a,a}", "x/y/a", '/'),
glob(true, "{**/a,a}", "a", '/'),
glob(false, "{**/a,a}", "x/y/b", '/'),

glob(true, pattern_prefix, fixture_prefix_suffix_match),
glob(false, pattern_prefix, fixture_prefix_suffix_mismatch),

Expand Down
15 changes: 7 additions & 8 deletions match/any_of.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,16 @@ func (self AnyOf) Index(s string) (int, []int) {

func (self AnyOf) Len() (l int) {
l = -1
for _, m := range self.Matchers {
for i, m := range self.Matchers {
ml := m.Len()
switch {
case l == -1:
if ml == -1 {
return -1
}
if i == 0 {
l = ml
continue

case ml == -1:
return -1

case l != ml:
}
if l != ml {
return -1
}
}
Expand Down
49 changes: 49 additions & 0 deletions match/any_of_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,55 @@ import (
"testing"
)

func TestAnyOfLen(t *testing.T) {
for id, test := range []struct {
matchers Matchers
want int
}{
{
// all matchers have the same known length
Matchers{NewText("abc"), NewText("xyz")},
3,
},
{
// matchers have different known lengths
Matchers{NewText("ab"), NewText("xyz")},
-1,
},
{
// first matcher has unknown length, second has known length
Matchers{NewSuffix("/daxing"), NewText("daxing")},
-1,
},
{
// first matcher has known length, second has unknown length
Matchers{NewText("daxing"), NewSuffix("/daxing")},
-1,
},
{
// all matchers have unknown length
Matchers{NewSuffix("/a"), NewPrefix("b/")},
-1,
},
{
// single matcher with known length
Matchers{NewText("hello")},
5,
},
{
// single matcher with unknown length
Matchers{NewSuffix("hello")},
-1,
},
} {
anyOf := NewAnyOf(test.matchers...)
got := anyOf.Len()
if got != test.want {
t.Errorf("#%d AnyOf.Len() = %d, want %d", id, got, test.want)
}
}
}

func TestAnyOfIndex(t *testing.T) {
for id, test := range []struct {
matchers Matchers
Expand Down