vault: utility string set methods

This commit is contained in:
Armon Dadgar 2015-03-24 13:56:07 -07:00
parent 493fbc12fc
commit b41d2e6368
2 changed files with 42 additions and 0 deletions

View File

@ -47,3 +47,14 @@ func strListContains(haystack []string, needle string) bool {
} }
return false return false
} }
// strListSubset checks if a given list is a subset
// of another set
func strListSubset(super, sub []string) bool {
for _, item := range sub {
if !strListContains(super, item) {
return false
}
}
return true
}

View File

@ -50,3 +50,34 @@ func TestStrListContains(t *testing.T) {
t.Fatalf("Bad") t.Fatalf("Bad")
} }
} }
func TestStrListSubset(t *testing.T) {
parent := []string{
"dev",
"ops",
"prod",
"root",
}
child := []string{
"prod",
"ops",
}
if !strListSubset(parent, child) {
t.Fatalf("Bad")
}
if !strListSubset(parent, parent) {
t.Fatalf("Bad")
}
if !strListSubset(child, child) {
t.Fatalf("Bad")
}
if !strListSubset(child, nil) {
t.Fatalf("Bad")
}
if strListSubset(child, parent) {
t.Fatalf("Bad")
}
if strListSubset(nil, child) {
t.Fatalf("Bad")
}
}