mirror of https://github.com/k3s-io/k3s
Merge pull request #1648 from deads2k/dev/deads/tidy-compile-regex-api
make api match intent with less specific typepull/6/head
commit
e27dacb1e4
|
@ -165,7 +165,7 @@ func StringDiff(a, b string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Takes a list of strings and compiles them into a list of regular expressions
|
// Takes a list of strings and compiles them into a list of regular expressions
|
||||||
func CompileRegexps(regexpStrings StringList) ([]*regexp.Regexp, error) {
|
func CompileRegexps(regexpStrings []string) ([]*regexp.Regexp, error) {
|
||||||
regexps := []*regexp.Regexp{}
|
regexps := []*regexp.Regexp{}
|
||||||
for _, regexpStr := range regexpStrings {
|
for _, regexpStr := range regexpStrings {
|
||||||
r, err := regexp.Compile(regexpStr)
|
r, err := regexp.Compile(regexpStr)
|
||||||
|
|
|
@ -206,3 +206,28 @@ func TestStringDiff(t *testing.T) {
|
||||||
t.Errorf("diff returned %v", diff)
|
t.Errorf("diff returned %v", diff)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCompileRegex(t *testing.T) {
|
||||||
|
uncompiledRegexes := []string{"endsWithMe$", "^startingWithMe"}
|
||||||
|
regexes, err := CompileRegexps(uncompiledRegexes)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Failed to compile legal regexes: '%v': %v", uncompiledRegexes, err)
|
||||||
|
}
|
||||||
|
if len(regexes) != len(uncompiledRegexes) {
|
||||||
|
t.Errorf("Wrong number of regexes returned: '%v': %v", uncompiledRegexes, regexes)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !regexes[0].MatchString("Something that endsWithMe") {
|
||||||
|
t.Errorf("Wrong regex returned: '%v': %v", uncompiledRegexes[0], regexes[0])
|
||||||
|
}
|
||||||
|
if regexes[0].MatchString("Something that doesn't endsWithMe.") {
|
||||||
|
t.Errorf("Wrong regex returned: '%v': %v", uncompiledRegexes[0], regexes[0])
|
||||||
|
}
|
||||||
|
if !regexes[1].MatchString("startingWithMe is very important") {
|
||||||
|
t.Errorf("Wrong regex returned: '%v': %v", uncompiledRegexes[1], regexes[1])
|
||||||
|
}
|
||||||
|
if regexes[1].MatchString("not startingWithMe should fail") {
|
||||||
|
t.Errorf("Wrong regex returned: '%v': %v", uncompiledRegexes[1], regexes[1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue