-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from EmilGeorgiev/implment_function_slice_cont…
…ains_string_ignore_cases implement a function "slice.ContainsStringIgnoreCase"
- Loading branch information
Showing
2 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package slice_test | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/mailgun/holster/slice" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestContainsString(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
slice []string | ||
str string | ||
modifier func(string) string | ||
want bool | ||
}{ | ||
{ | ||
name: "Slice contains a specific string", | ||
slice: []string{"aa", "bb", "CC"}, | ||
modifier: strings.ToLower, | ||
str: "CC", | ||
want: true, | ||
}, | ||
{ | ||
name: "Slice contains a string, but it is with upper cases and modifier is nil", | ||
slice: []string{"aa", "bb", "CC"}, | ||
str: "cc", | ||
want: false, | ||
}, | ||
{ | ||
name: "Slice contains a string with upper cases and modifier ToLower is provided", | ||
slice: []string{"AA", "bb", "cc"}, | ||
modifier: strings.ToLower, | ||
str: "aa", | ||
want: true, | ||
}, | ||
{ | ||
name: "Slice does not contains string", | ||
slice: []string{"AA", "bb", "cc"}, | ||
str: "notExist", | ||
want: false, | ||
}, | ||
{ | ||
name: "Empty slice", | ||
slice: []string{}, | ||
str: "notExist", | ||
want: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
got := slice.ContainsString(tt.str, tt.slice, tt.modifier) | ||
assert.Equal(t, tt.want, got) | ||
} | ||
} | ||
|
||
func TestContainsStringIgnoreCase(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
slice []string | ||
str string | ||
want bool | ||
}{ | ||
{ | ||
name: "Slice contains a specific string, but with different upper case", | ||
slice: []string{"aa", "bb", "cC"}, | ||
str: "Cc", | ||
want: true, | ||
}, | ||
{ | ||
name: "Slice contains a string, but it is with upper case", | ||
slice: []string{"aa", "bb", "CC"}, | ||
str: "cc", | ||
want: true, | ||
}, | ||
{ | ||
name: "Slice contains a string, but it is with lower cases", | ||
slice: []string{"aa", "bb", "cc"}, | ||
str: "AA", | ||
want: true, | ||
}, | ||
{ | ||
name: "Slice does not contains string", | ||
slice: []string{"AA", "bb", "cc"}, | ||
str: "notExist", | ||
want: false, | ||
}, | ||
{ | ||
name: "Empty slice", | ||
slice: []string{}, | ||
str: "notExist", | ||
want: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
got := slice.ContainsStringEqualFold(tt.str, tt.slice) | ||
assert.Equal(t, tt.want, got) | ||
} | ||
} |