-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow mocked URL matching to be glob based
- Loading branch information
1 parent
1c2d4c6
commit b273595
Showing
4 changed files
with
159 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package stringsx | ||
|
||
import ( | ||
"sort" | ||
"strings" | ||
) | ||
|
||
// GlobMatch does very simple * based glob matching where * can be the entire pattern or start or the end or both. | ||
func GlobMatch(s, pattern string) bool { | ||
if pattern == "" { | ||
return s == "" | ||
} | ||
if pattern == "*" { | ||
return true | ||
} | ||
if strings.HasPrefix(pattern, "*") && strings.HasSuffix(pattern, "*") { | ||
return strings.Contains(s, pattern[1:len(pattern)-1]) | ||
} | ||
if strings.HasPrefix(pattern, "*") { | ||
return strings.HasSuffix(s, pattern[1:]) | ||
} | ||
if strings.HasSuffix(pattern, "*") { | ||
return strings.HasPrefix(s, pattern[0:len(pattern)-1]) | ||
} | ||
|
||
return s == pattern | ||
} | ||
|
||
// GlobSelect returns the most specific matching pattern from the given set. | ||
func GlobSelect(s string, patterns ...string) string { | ||
matching := make([]string, 0, len(patterns)) | ||
for _, p := range patterns { | ||
if GlobMatch(s, p) { | ||
matching = append(matching, p) | ||
} | ||
} | ||
|
||
if len(matching) == 0 { | ||
return "" | ||
} | ||
|
||
// return the longest pattern excluding * chars | ||
sort.SliceStable(matching, func(i, j int) bool { | ||
return len(strings.Trim(matching[i], "*")) > len(strings.Trim(matching[j], "*")) | ||
}) | ||
return matching[0] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package stringsx_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/nyaruka/gocommon/stringsx" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGlobMatch(t *testing.T) { | ||
tcs := []struct { | ||
input string | ||
pattern string | ||
matches bool | ||
}{ | ||
{"", "", true}, | ||
{"hello", "", false}, | ||
{"hello", "hello", true}, | ||
{"HELLO", "hello", false}, | ||
{"hellohello", "hello", false}, | ||
{"hello", "*hello", true}, | ||
{"hello", "*ello", true}, | ||
{"hello", "*llo", true}, | ||
{"hello", "*lo", true}, | ||
{"hello", "*o", true}, | ||
{"hello", "*", true}, | ||
{"hello", "h*", true}, | ||
{"hello", "he*", true}, | ||
{"hello", "hel*", true}, | ||
{"hello", "hello*", true}, | ||
{"hello", "*hello*", true}, | ||
{"hello", "*ell*", true}, | ||
{"hello", "*e*", true}, | ||
{"", "*", true}, | ||
{"hello", "jam*", false}, | ||
{"hello", "*jam", false}, | ||
{"hello", "*j*", false}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
if tc.matches { | ||
assert.True(t, stringsx.GlobMatch(tc.input, tc.pattern), "expected match for %s / %s", tc.input, tc.pattern) | ||
} else { | ||
assert.False(t, stringsx.GlobMatch(tc.input, tc.pattern), "unexpected match for %s / %s", tc.input, tc.pattern) | ||
} | ||
} | ||
} | ||
|
||
func TestGlobSelect(t *testing.T) { | ||
tcs := []struct { | ||
input string | ||
patterns []string | ||
selected string | ||
}{ | ||
{"hello", []string{"*", "*ello", "hello", "hel*"}, "hello"}, | ||
{"hello", []string{"*ello", "hel*"}, "*ello"}, | ||
{"hello", []string{"*", "abc"}, "*"}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
assert.Equal(t, tc.selected, stringsx.GlobSelect(tc.input, tc.patterns...), "select mismatch for %s / %v", tc.input, tc.patterns) | ||
} | ||
} |