| 58 | } |
| 59 | |
| 60 | func TestPartition(t *testing.T) { |
| 61 | tests := []struct { |
| 62 | name string |
| 63 | slice []any |
| 64 | predicate func(any) bool |
| 65 | wantMatching []any |
| 66 | wantNonMatching []any |
| 67 | }{ |
| 68 | { |
| 69 | name: "When the slice is empty, it returns two empty slices", |
| 70 | slice: []any{}, |
| 71 | predicate: func(any) bool { |
| 72 | return true |
| 73 | }, |
| 74 | wantMatching: []any{}, |
| 75 | wantNonMatching: []any{}, |
| 76 | }, |
| 77 | { |
| 78 | name: "when the slice has one element that satisfies the predicate, it returns a slice with that element and an empty slice", |
| 79 | slice: []any{ |
| 80 | "foo", |
| 81 | }, |
| 82 | predicate: func(any) bool { |
| 83 | return true |
| 84 | }, |
| 85 | wantMatching: []any{"foo"}, |
| 86 | wantNonMatching: []any{}, |
| 87 | }, |
| 88 | { |
| 89 | name: "when the slice has one element that does not satisfy the predicate, it returns an empty slice and a slice with that element", |
| 90 | slice: []any{ |
| 91 | "foo", |
| 92 | }, |
| 93 | predicate: func(any) bool { |
| 94 | return false |
| 95 | }, |
| 96 | wantMatching: []any{}, |
| 97 | wantNonMatching: []any{"foo"}, |
| 98 | }, |
| 99 | { |
| 100 | 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", |
| 101 | slice: []any{ |
| 102 | "foo", |
| 103 | "bar", |
| 104 | "baz", |
| 105 | }, |
| 106 | predicate: func(s any) bool { |
| 107 | return s.(string) != "foo" |
| 108 | }, |
| 109 | wantMatching: []any{"bar", "baz"}, |
| 110 | wantNonMatching: []any{"foo"}, |
| 111 | }, |
| 112 | } |
| 113 | |
| 114 | for _, tt := range tests { |
| 115 | t.Run(tt.name, func(t *testing.T) { |
| 116 | gotMatching, gotNonMatching := Partition(tt.slice, tt.predicate) |
| 117 | assert.ElementsMatch(t, tt.wantMatching, gotMatching) |