From 90188bb18d53129c3f3b96e26342f80f80ae9770 Mon Sep 17 00:00:00 2001 From: Paul Meyer Date: Wed, 25 Mar 2020 21:38:05 +0000 Subject: [PATCH] add func for searching string slice --- builder/azure/common/strings_contains.go | 13 +++++++ builder/azure/common/strings_contains_test.go | 39 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 builder/azure/common/strings_contains.go create mode 100644 builder/azure/common/strings_contains_test.go diff --git a/builder/azure/common/strings_contains.go b/builder/azure/common/strings_contains.go new file mode 100644 index 000000000..2a8fae416 --- /dev/null +++ b/builder/azure/common/strings_contains.go @@ -0,0 +1,13 @@ +package common + +import "strings" + +// StringsContains returns true if the `haystack` contains the `needle`. Search is case insensitive. +func StringsContains(haystack []string, needle string) bool { + for _, s := range haystack { + if strings.EqualFold(s, needle) { + return true + } + } + return false +} diff --git a/builder/azure/common/strings_contains_test.go b/builder/azure/common/strings_contains_test.go new file mode 100644 index 000000000..51a24ff70 --- /dev/null +++ b/builder/azure/common/strings_contains_test.go @@ -0,0 +1,39 @@ +package common + +import "testing" + +func TestStringsContains(t *testing.T) { + + tests := []struct { + name string + haystack []string + needle string + want bool + }{ + { + name: "found", + haystack: []string{"a", "b", "c"}, + needle: "b", + want: true, + }, + { + name: "missing", + haystack: []string{"a", "b", "c"}, + needle: "D", + want: false, + }, + { + name: "case insensitive", + haystack: []string{"a", "b", "c"}, + needle: "B", + want: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := StringsContains(tt.haystack, tt.needle); got != tt.want { + t.Errorf("StringsContains() = %v, want %v", got, tt.want) + } + }) + } +}