parent
3fc9515c26
commit
145fcf336b
5 changed files with 72 additions and 17 deletions
@ -0,0 +1,13 @@ |
||||
package run |
||||
|
||||
import "strings" |
||||
|
||||
// hasAtLeastOnePrefix checks whether the key has begins with at least one of prefixes.
|
||||
func hasAtLeastOnePrefix(key string, prefixes []string) bool { |
||||
for _, prefix := range prefixes { |
||||
if strings.HasPrefix(key, prefix) { |
||||
return true |
||||
} |
||||
} |
||||
return false |
||||
} |
@ -0,0 +1,40 @@ |
||||
package run |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/stretchr/testify/assert" |
||||
) |
||||
|
||||
func TestHasAtLeastOnePrefix(t *testing.T) { |
||||
|
||||
cases := []struct { |
||||
key string |
||||
prefixes []string |
||||
expectResult bool |
||||
}{ |
||||
{ |
||||
// no prefix provided
|
||||
"a", |
||||
[]string{}, |
||||
false, |
||||
}, |
||||
{ |
||||
// has prefix
|
||||
"abc", |
||||
[]string{"ab"}, |
||||
true, |
||||
}, |
||||
{ |
||||
// does NOT have prefix
|
||||
"abc", |
||||
[]string{"edf", "wab"}, |
||||
false, |
||||
}, |
||||
} |
||||
|
||||
for _, c := range cases { |
||||
result := hasAtLeastOnePrefix(c.key, c.prefixes) |
||||
assert.Equal(t, c.expectResult, result) |
||||
} |
||||
} |
Loading…
Reference in new issue