mirror of
https://github.com/coredns/coredns.git
synced 2025-11-02 10:13:14 -05:00
Refactoring of k8s helpers
This commit is contained in:
11
middleware/pkg/strings/slice.go
Normal file
11
middleware/pkg/strings/slice.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package strings
|
||||
|
||||
// StringInSlice check whether string a is a member of slice.
|
||||
func StringInSlice(a string, slice []string) bool {
|
||||
for _, b := range slice {
|
||||
if b == a {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
33
middleware/pkg/strings/slice_test.go
Normal file
33
middleware/pkg/strings/slice_test.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package strings
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
type InSliceData struct {
|
||||
Slice []string
|
||||
String string
|
||||
InSlice bool
|
||||
}
|
||||
|
||||
// Test data for TestStringInSlice cases.
|
||||
var testdataInSlice = []struct {
|
||||
Slice []string
|
||||
String string
|
||||
ExpectedResult bool
|
||||
}{
|
||||
{[]string{"a", "b", "c"}, "a", true},
|
||||
{[]string{"a", "b", "c"}, "d", false},
|
||||
{[]string{"a", "b", "c"}, "", false},
|
||||
{[]string{}, "a", false},
|
||||
{[]string{}, "", false},
|
||||
}
|
||||
|
||||
func TestStringInSlice(t *testing.T) {
|
||||
for _, example := range testdataInSlice {
|
||||
actualResult := StringInSlice(example.String, example.Slice)
|
||||
if actualResult != example.ExpectedResult {
|
||||
t.Errorf("Expected stringInSlice result '%v' for example string='%v', slice='%v'. Instead got result '%v'.", example.ExpectedResult, example.String, example.Slice, actualResult)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user