1
16
17 package slice
18
19 import (
20 "reflect"
21 "testing"
22 )
23
24 func TestCopyStrings(t *testing.T) {
25 var src1 []string
26 dest1 := CopyStrings(src1)
27
28 if !reflect.DeepEqual(src1, dest1) {
29 t.Errorf("%v and %v are not equal", src1, dest1)
30 }
31
32 src2 := []string{}
33 dest2 := CopyStrings(src2)
34
35 if !reflect.DeepEqual(src2, dest2) {
36 t.Errorf("%v and %v are not equal", src2, dest2)
37 }
38
39 src3 := []string{"a", "c", "b"}
40 dest3 := CopyStrings(src3)
41
42 if !reflect.DeepEqual(src3, dest3) {
43 t.Errorf("%v and %v are not equal", src3, dest3)
44 }
45
46 src3[0] = "A"
47 if reflect.DeepEqual(src3, dest3) {
48 t.Errorf("CopyStrings didn't make a copy")
49 }
50 }
51
52 func TestSortStrings(t *testing.T) {
53 src := []string{"a", "c", "b"}
54 dest := SortStrings(src)
55 expected := []string{"a", "b", "c"}
56
57 if !reflect.DeepEqual(dest, expected) {
58 t.Errorf("SortString didn't sort the strings")
59 }
60
61 if !reflect.DeepEqual(src, expected) {
62 t.Errorf("SortString didn't sort in place")
63 }
64 }
65
66 func TestContainsString(t *testing.T) {
67 src := []string{"aa", "bb", "cc"}
68 if !ContainsString(src, "bb", nil) {
69 t.Errorf("ContainsString didn't find the string as expected")
70 }
71
72 modifier := func(s string) string {
73 if s == "cc" {
74 return "ee"
75 }
76 return s
77 }
78 if !ContainsString(src, "ee", modifier) {
79 t.Errorf("ContainsString didn't find the string by modifier")
80 }
81
82 src = make([]string, 0)
83 if ContainsString(src, "", nil) {
84 t.Errorf("The result returned is not the expected result")
85 }
86 }
87
88 func TestRemoveString(t *testing.T) {
89 modifier := func(s string) string {
90 if s == "ab" {
91 return "ee"
92 }
93 return s
94 }
95 tests := []struct {
96 testName string
97 input []string
98 remove string
99 modifier func(s string) string
100 want []string
101 }{
102 {
103 testName: "Nil input slice",
104 input: nil,
105 remove: "",
106 modifier: nil,
107 want: nil,
108 },
109 {
110 testName: "Slice doesn't contain the string",
111 input: []string{"a", "ab", "cdef"},
112 remove: "NotPresentInSlice",
113 modifier: nil,
114 want: []string{"a", "ab", "cdef"},
115 },
116 {
117 testName: "All strings removed, result is nil",
118 input: []string{"a"},
119 remove: "a",
120 modifier: nil,
121 want: nil,
122 },
123 {
124 testName: "No modifier func, one string removed",
125 input: []string{"a", "ab", "cdef"},
126 remove: "ab",
127 modifier: nil,
128 want: []string{"a", "cdef"},
129 },
130 {
131 testName: "No modifier func, all(three) strings removed",
132 input: []string{"ab", "a", "ab", "cdef", "ab"},
133 remove: "ab",
134 modifier: nil,
135 want: []string{"a", "cdef"},
136 },
137 {
138 testName: "Removed both the string and the modifier func result",
139 input: []string{"a", "cd", "ab", "ee"},
140 remove: "ee",
141 modifier: modifier,
142 want: []string{"a", "cd"},
143 },
144 }
145 for _, tt := range tests {
146 if got := RemoveString(tt.input, tt.remove, tt.modifier); !reflect.DeepEqual(got, tt.want) {
147 t.Errorf("%v: RemoveString(%v, %q, %T) = %v WANT %v", tt.testName, tt.input, tt.remove, tt.modifier, got, tt.want)
148 }
149 }
150 }
151
View as plain text