| 123 | } |
| 124 | |
| 125 | func TestPartition(t *testing.T) { |
| 126 | tests := []struct { |
| 127 | name string |
| 128 | slice []any |
| 129 | predicate func(any) bool |
| 130 | wantMatching []any |
| 131 | wantNonMatching []any |
| 132 | }{ |
| 133 | { |
| 134 | name: "When the slice is empty, it returns two empty slices", |
| 135 | slice: []any{}, |
| 136 | predicate: func(any) bool { |
| 137 | return true |
| 138 | }, |
| 139 | wantMatching: []any{}, |
| 140 | wantNonMatching: []any{}, |
| 141 | }, |
| 142 | { |
| 143 | name: "when the slice has one element that satisfies the predicate, it returns a slice with that element and an empty slice", |
| 144 | slice: []any{ |
| 145 | "foo", |
| 146 | }, |
| 147 | predicate: func(any) bool { |
| 148 | return true |
| 149 | }, |
| 150 | wantMatching: []any{"foo"}, |
| 151 | wantNonMatching: []any{}, |
| 152 | }, |
| 153 | { |
| 154 | name: "when the slice has one element that does not satisfy the predicate, it returns an empty slice and a slice with that element", |
| 155 | slice: []any{ |
| 156 | "foo", |
| 157 | }, |
| 158 | predicate: func(any) bool { |
| 159 | return false |
| 160 | }, |
| 161 | wantMatching: []any{}, |
| 162 | wantNonMatching: []any{"foo"}, |
| 163 | }, |
| 164 | { |
| 165 | name: "when the slice has multiple elements, it returns a slice with the elements that satisfy the predicate and a slice with the elements that do not satisfy the predicate", |
| 166 | slice: []any{ |
| 167 | "foo", |
| 168 | "bar", |
| 169 | "baz", |
| 170 | }, |
| 171 | predicate: func(s any) bool { |
| 172 | return s.(string) != "foo" |
| 173 | }, |
| 174 | wantMatching: []any{"bar", "baz"}, |
| 175 | wantNonMatching: []any{"foo"}, |
| 176 | }, |
| 177 | } |
| 178 | |
| 179 | for _, tt := range tests { |
| 180 | t.Run(tt.name, func(t *testing.T) { |
| 181 | gotMatching, gotNonMatching := Partition(tt.slice, tt.predicate) |
| 182 | assert.ElementsMatch(t, tt.wantMatching, gotMatching) |