Skip to content

Commit fda62c7

Browse files
committed
add unit test
1 parent e4c395f commit fda62c7

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

finder/index.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ func (idx *IndexFinder) validatePlainQuery(query string, wildcardMinDistance int
160160
return errs.NewErrorWithCode("query has unmatched brackets", http.StatusBadRequest)
161161
}
162162

163-
if where.HasWildcard(query) && where.MaxWildcardDistance(query) < wildcardMinDistance {
163+
var maxDist = where.MaxWildcardDistance(query)
164+
165+
if maxDist != -1 && maxDist < wildcardMinDistance {
164166
return errs.NewErrorWithCode("query has wildcards way too early at the start and at the end of it", http.StatusBadRequest)
165167
}
166168

pkg/where/where_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,23 @@ func TestNonRegexpPrefix(t *testing.T) {
7272
assert.Equal(t, test.prefix, prefix, testName)
7373
}
7474
}
75+
76+
func TestMaxWildcardDistance(t *testing.T) {
77+
table := []struct {
78+
glob string
79+
dist int
80+
}{
81+
{`a.b.c.d.e`, -1},
82+
{`test.*.foo.bar`, 2},
83+
{`test.foo.*.*.bar.count`, 2},
84+
{`test.foo.bar.*.bar.foo.test`, 3},
85+
{`test.foo.bar.foobar.*.middle.*.foobar.bar.foo.test`, 4},
86+
{`*.test.foo.bar.*`, 0},
87+
}
88+
89+
for _, test := range table {
90+
testName := fmt.Sprintf("glob: %#v", test.glob)
91+
dist := MaxWildcardDistance(test.glob)
92+
assert.Equal(t, test.dist, dist, testName)
93+
}
94+
}

0 commit comments

Comments
 (0)