|
| 1 | +package sliceutil |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | +) |
| 8 | + |
| 9 | +func TestRemoveZeroValues(t *testing.T) { |
| 10 | + boolSlice := []bool{false, false, false} |
| 11 | + require.Equal(t, []bool{}, RemoveZeroValues(boolSlice)) |
| 12 | + |
| 13 | + intSlice := []int{1, 987, 0} |
| 14 | + require.Equal(t, []int{1, 987}, RemoveZeroValues(intSlice)) |
| 15 | + |
| 16 | + strSlice := []string{"hello", "world", ""} |
| 17 | + require.Equal(t, []string{"hello", "world"}, RemoveZeroValues(strSlice)) |
| 18 | + |
| 19 | + str1 := "hello" |
| 20 | + str2 := "world" |
| 21 | + str1Ptr := &str1 |
| 22 | + str2Ptr := &str2 |
| 23 | + var nilStrPtr *string |
| 24 | + |
| 25 | + strPtrSlice := []*string{&str1, &str2, nilStrPtr, nil} |
| 26 | + require.Equal(t, []*string{str1Ptr, str2Ptr}, RemoveZeroValues(strPtrSlice)) |
| 27 | + |
| 28 | + type testStruct struct { |
| 29 | + flag bool |
| 30 | + } |
| 31 | + |
| 32 | + ts := []testStruct{{flag: true}, {flag: false}} |
| 33 | + require.Equal(t, []testStruct{{flag: true}}, RemoveZeroValues(ts)) |
| 34 | +} |
| 35 | + |
| 36 | +func TestToLower(t *testing.T) { |
| 37 | + s := []string{"apples", "ORANGES", "**Pears**", "bAnAnAs-123"} |
| 38 | + require.Equal(t, []string{"apples", "oranges", "**pears**", "bananas-123"}, ToLower(s)) |
| 39 | +} |
| 40 | + |
| 41 | +func TestTrim(t *testing.T) { |
| 42 | + s := []string{"no-space", "middle space", " left-space", "right-space ", " surrounding-space "} |
| 43 | + require.Equal(t, []string{"no-space", "middle space", "left-space", "right-space", "surrounding-space"}, Trim(s)) |
| 44 | +} |
0 commit comments