|
| 1 | +package collections_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + . "github.com/onsi/ginkgo/v2" |
| 7 | + . "github.com/onsi/gomega" |
| 8 | + |
| 9 | + "github.com/openmcp-project/controller-utils/pkg/collections" |
| 10 | + "github.com/openmcp-project/controller-utils/pkg/pairs" |
| 11 | +) |
| 12 | + |
| 13 | +var _ = Describe("Utils Tests", func() { |
| 14 | + |
| 15 | + Context("ProjectSlice", func() { |
| 16 | + |
| 17 | + projectFunc := func(i int) int { |
| 18 | + return i * 2 |
| 19 | + } |
| 20 | + |
| 21 | + It("should use the projection function on each element of the slice", func() { |
| 22 | + src := []int{1, 2, 3, 4} |
| 23 | + projected := collections.ProjectSlice(src, projectFunc) |
| 24 | + Expect(projected).To(Equal([]int{2, 4, 6, 8})) |
| 25 | + Expect(src).To(Equal([]int{1, 2, 3, 4}), "original slice should not be modified") |
| 26 | + }) |
| 27 | + |
| 28 | + It("should return an empty slice for an empty or nil input slice", func() { |
| 29 | + Expect(collections.ProjectSlice(nil, projectFunc)).To(BeEmpty()) |
| 30 | + Expect(collections.ProjectSlice([]int{}, projectFunc)).To(BeEmpty()) |
| 31 | + }) |
| 32 | + |
| 33 | + It("should return nil for a nil projection function", func() { |
| 34 | + src := []int{1, 2, 3, 4} |
| 35 | + projected := collections.ProjectSlice[int, int](src, nil) |
| 36 | + Expect(projected).To(BeNil()) |
| 37 | + Expect(src).To(Equal([]int{1, 2, 3, 4}), "original slice should not be modified") |
| 38 | + }) |
| 39 | + |
| 40 | + }) |
| 41 | + |
| 42 | + Context("ProjectMapToSlice", func() { |
| 43 | + |
| 44 | + projectFunc := func(k string, v string) string { |
| 45 | + return k + ":" + v |
| 46 | + } |
| 47 | + |
| 48 | + It("should use the projection function on each key-value pair of the map", func() { |
| 49 | + src := map[string]string{"a": "1", "b": "2", "c": "3"} |
| 50 | + projected := collections.ProjectMapToSlice(src, projectFunc) |
| 51 | + Expect(projected).To(ConsistOf("a:1", "b:2", "c:3")) |
| 52 | + Expect(src).To(Equal(map[string]string{"a": "1", "b": "2", "c": "3"}), "original map should not be modified") |
| 53 | + }) |
| 54 | + |
| 55 | + It("should return an empty slice for an empty or nil input map", func() { |
| 56 | + Expect(collections.ProjectMapToSlice(nil, projectFunc)).To(BeEmpty()) |
| 57 | + Expect(collections.ProjectMapToSlice(map[string]string{}, projectFunc)).To(BeEmpty()) |
| 58 | + }) |
| 59 | + |
| 60 | + It("should return nil for a nil projection function", func() { |
| 61 | + src := map[string]string{"a": "1", "b": "2", "c": "3"} |
| 62 | + projected := collections.ProjectMapToSlice[string, string, string](src, nil) |
| 63 | + Expect(projected).To(BeNil()) |
| 64 | + Expect(src).To(Equal(map[string]string{"a": "1", "b": "2", "c": "3"}), "original map should not be modified") |
| 65 | + }) |
| 66 | + |
| 67 | + }) |
| 68 | + |
| 69 | + Context("ProjectMapToMap", func() { |
| 70 | + |
| 71 | + projectFunc := func(k string, v string) (string, int) { |
| 72 | + return k, len(v) |
| 73 | + } |
| 74 | + |
| 75 | + It("should use the projection function on each key-value pair of the map", func() { |
| 76 | + src := map[string]string{"a": "1", "b": "22", "c": "333"} |
| 77 | + projected := collections.ProjectMapToMap(src, projectFunc) |
| 78 | + Expect(projected).To(Equal(map[string]int{"a": 1, "b": 2, "c": 3})) |
| 79 | + Expect(src).To(Equal(map[string]string{"a": "1", "b": "22", "c": "333"}), "original map should not be modified") |
| 80 | + }) |
| 81 | + |
| 82 | + It("should return an empty map for an empty or nil input map", func() { |
| 83 | + Expect(collections.ProjectMapToMap(nil, projectFunc)).To(BeEmpty()) |
| 84 | + Expect(collections.ProjectMapToMap(map[string]string{}, projectFunc)).To(BeEmpty()) |
| 85 | + }) |
| 86 | + |
| 87 | + It("should return nil for a nil projection function", func() { |
| 88 | + src := map[string]string{"a": "1", "b": "22", "c": "333"} |
| 89 | + projected := collections.ProjectMapToMap[string, string, string, int](src, nil) |
| 90 | + Expect(projected).To(BeNil()) |
| 91 | + Expect(src).To(Equal(map[string]string{"a": "1", "b": "22", "c": "333"}), "original map should not be modified") |
| 92 | + }) |
| 93 | + |
| 94 | + }) |
| 95 | + |
| 96 | + Context("AggregateSlice", func() { |
| 97 | + |
| 98 | + sum := func(val, s int) int { |
| 99 | + return val + s |
| 100 | + } |
| 101 | + stradd := func(val int, s string) string { |
| 102 | + return fmt.Sprintf("%s%d", s, val) |
| 103 | + } |
| 104 | + |
| 105 | + It("should return the initial value if the aggregation function is nil", func() { |
| 106 | + src := []int{1, 2, 3, 4} |
| 107 | + result := collections.AggregateSlice(src, nil, 0) |
| 108 | + Expect(result).To(Equal(0)) |
| 109 | + Expect(src).To(Equal([]int{1, 2, 3, 4})) |
| 110 | + }) |
| 111 | + |
| 112 | + It("should correctly aggregate the slice using the provided function", func() { |
| 113 | + src := []int{1, 2, 3, 4} |
| 114 | + result := collections.AggregateSlice(src, sum, 0) |
| 115 | + Expect(result).To(Equal(10)) |
| 116 | + Expect(src).To(Equal([]int{1, 2, 3, 4})) |
| 117 | + |
| 118 | + result2 := collections.AggregateSlice(src, stradd, "test") |
| 119 | + Expect(result2).To(Equal("test1234")) |
| 120 | + Expect(src).To(Equal([]int{1, 2, 3, 4})) |
| 121 | + }) |
| 122 | + |
| 123 | + It("should handle a nil input slice", func() { |
| 124 | + result := collections.AggregateSlice[int, int](nil, sum, 100) |
| 125 | + Expect(result).To(Equal(100)) |
| 126 | + }) |
| 127 | + |
| 128 | + }) |
| 129 | + |
| 130 | + Context("AggregateMap", func() { |
| 131 | + |
| 132 | + aggregate := func(k string, v int, agg pairs.Pair[string, int]) pairs.Pair[string, int] { |
| 133 | + return pairs.New(agg.Key+k, agg.Value+v) |
| 134 | + } |
| 135 | + |
| 136 | + It("should return the initial value if the aggregation function is nil", func() { |
| 137 | + src := map[string]int{"a": 1, "b": 2, "c": 3} |
| 138 | + result := collections.AggregateMap(src, nil, 0) |
| 139 | + Expect(result).To(Equal(0)) |
| 140 | + Expect(src).To(Equal(map[string]int{"a": 1, "b": 2, "c": 3})) |
| 141 | + }) |
| 142 | + |
| 143 | + It("should correctly aggregate the map using the provided function", func() { |
| 144 | + src := map[string]int{"a": 1, "b": 2, "c": 3} |
| 145 | + result := collections.AggregateMap(src, aggregate, pairs.New("", 0)) |
| 146 | + Expect(result.Key).To(HaveLen(3)) |
| 147 | + Expect(result.Key).To(ContainSubstring("a")) |
| 148 | + Expect(result.Key).To(ContainSubstring("b")) |
| 149 | + Expect(result.Key).To(ContainSubstring("c")) |
| 150 | + Expect(result.Value).To(Equal(6)) |
| 151 | + Expect(src).To(Equal(map[string]int{"a": 1, "b": 2, "c": 3})) |
| 152 | + }) |
| 153 | + |
| 154 | + It("should handle a nil input map", func() { |
| 155 | + result := collections.AggregateMap(nil, aggregate, pairs.New("", 0)) |
| 156 | + Expect(result.Key).To(BeEmpty()) |
| 157 | + Expect(result.Value).To(Equal(0)) |
| 158 | + }) |
| 159 | + |
| 160 | + }) |
| 161 | + |
| 162 | +}) |
0 commit comments