github.com/mailgun/holster/v4@v4.20.0/slice/string_test.go (about) 1 package slice_test 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/mailgun/holster/v4/slice" 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestContainsString(t *testing.T) { 12 tests := []struct { 13 name string 14 slice []string 15 str string 16 modifier func(string) string 17 want bool 18 }{ 19 { 20 name: "Slice contains a specific string", 21 slice: []string{"aa", "bb", "CC"}, 22 modifier: strings.ToLower, 23 str: "CC", 24 want: true, 25 }, 26 { 27 name: "Slice contains a string, but it is with upper cases and modifier is nil", 28 slice: []string{"aa", "bb", "CC"}, 29 str: "cc", 30 want: false, 31 }, 32 { 33 name: "Slice contains a string with upper cases and modifier ToLower is provided", 34 slice: []string{"AA", "bb", "cc"}, 35 modifier: strings.ToLower, 36 str: "aa", 37 want: true, 38 }, 39 { 40 name: "Slice does not contains string", 41 slice: []string{"AA", "bb", "cc"}, 42 str: "notExist", 43 want: false, 44 }, 45 { 46 name: "Empty slice", 47 slice: []string{}, 48 str: "notExist", 49 want: false, 50 }, 51 } 52 53 for _, tt := range tests { 54 got := slice.ContainsString(tt.str, tt.slice, tt.modifier) 55 assert.Equal(t, tt.want, got) 56 } 57 } 58 59 func TestContainsStringIgnoreCase(t *testing.T) { 60 tests := []struct { 61 name string 62 slice []string 63 str string 64 want bool 65 }{ 66 { 67 name: "Slice contains a specific string, but with different upper case", 68 slice: []string{"aa", "bb", "cC"}, 69 str: "Cc", 70 want: true, 71 }, 72 { 73 name: "Slice contains a string, but it is with upper case", 74 slice: []string{"aa", "bb", "CC"}, 75 str: "cc", 76 want: true, 77 }, 78 { 79 name: "Slice contains a string, but it is with lower cases", 80 slice: []string{"aa", "bb", "cc"}, 81 str: "AA", 82 want: true, 83 }, 84 { 85 name: "Slice does not contains string", 86 slice: []string{"AA", "bb", "cc"}, 87 str: "notExist", 88 want: false, 89 }, 90 { 91 name: "Empty slice", 92 slice: []string{}, 93 str: "notExist", 94 want: false, 95 }, 96 } 97 98 for _, tt := range tests { 99 got := slice.ContainsStringEqualFold(tt.str, tt.slice) 100 assert.Equal(t, tt.want, got) 101 } 102 }