|
| 1 | +package collections_test |
| 2 | + |
| 3 | +import ( |
| 4 | + . "github.com/onsi/ginkgo/v2" |
| 5 | + . "github.com/onsi/gomega" |
| 6 | + |
| 7 | + "github.com/openmcp-project/controller-utils/pkg/collections" |
| 8 | +) |
| 9 | + |
| 10 | +var _ = Describe("Utils Tests", func() { |
| 11 | + |
| 12 | + Context("ProjectSlice", func() { |
| 13 | + |
| 14 | + projectFunc := func(i int) int { |
| 15 | + return i * 2 |
| 16 | + } |
| 17 | + |
| 18 | + It("should use the projection function on each element of the slice", func() { |
| 19 | + src := []int{1, 2, 3, 4} |
| 20 | + projected := collections.ProjectSlice(src, projectFunc) |
| 21 | + Expect(projected).To(Equal([]int{2, 4, 6, 8})) |
| 22 | + Expect(src).To(Equal([]int{1, 2, 3, 4}), "original slice should not be modified") |
| 23 | + }) |
| 24 | + |
| 25 | + It("should return an empty slice for an empty or nil input slice", func() { |
| 26 | + Expect(collections.ProjectSlice(nil, projectFunc)).To(BeEmpty()) |
| 27 | + Expect(collections.ProjectSlice([]int{}, projectFunc)).To(BeEmpty()) |
| 28 | + }) |
| 29 | + |
| 30 | + It("should return nil for a nil projection function", func() { |
| 31 | + src := []int{1, 2, 3, 4} |
| 32 | + projected := collections.ProjectSlice[int, int](src, nil) |
| 33 | + Expect(projected).To(BeNil()) |
| 34 | + Expect(src).To(Equal([]int{1, 2, 3, 4}), "original slice should not be modified") |
| 35 | + }) |
| 36 | + |
| 37 | + }) |
| 38 | + |
| 39 | + Context("ProjectMapToSlice", func() { |
| 40 | + |
| 41 | + projectFunc := func(k string, v string) string { |
| 42 | + return k + ":" + v |
| 43 | + } |
| 44 | + |
| 45 | + It("should use the projection function on each key-value pair of the map", func() { |
| 46 | + src := map[string]string{"a": "1", "b": "2", "c": "3"} |
| 47 | + projected := collections.ProjectMapToSlice(src, projectFunc) |
| 48 | + Expect(projected).To(ConsistOf("a:1", "b:2", "c:3")) |
| 49 | + Expect(src).To(Equal(map[string]string{"a": "1", "b": "2", "c": "3"}), "original map should not be modified") |
| 50 | + }) |
| 51 | + |
| 52 | + It("should return an empty slice for an empty or nil input map", func() { |
| 53 | + Expect(collections.ProjectMapToSlice(nil, projectFunc)).To(BeEmpty()) |
| 54 | + Expect(collections.ProjectMapToSlice(map[string]string{}, projectFunc)).To(BeEmpty()) |
| 55 | + }) |
| 56 | + |
| 57 | + It("should return nil for a nil projection function", func() { |
| 58 | + src := map[string]string{"a": "1", "b": "2", "c": "3"} |
| 59 | + projected := collections.ProjectMapToSlice[string, string, string](src, nil) |
| 60 | + Expect(projected).To(BeNil()) |
| 61 | + Expect(src).To(Equal(map[string]string{"a": "1", "b": "2", "c": "3"}), "original map should not be modified") |
| 62 | + }) |
| 63 | + |
| 64 | + }) |
| 65 | + |
| 66 | + Context("ProjectMapToMap", func() { |
| 67 | + |
| 68 | + projectFunc := func(k string, v string) (string, int) { |
| 69 | + return k, len(v) |
| 70 | + } |
| 71 | + |
| 72 | + It("should use the projection function on each key-value pair of the map", func() { |
| 73 | + src := map[string]string{"a": "1", "b": "22", "c": "333"} |
| 74 | + projected := collections.ProjectMapToMap(src, projectFunc) |
| 75 | + Expect(projected).To(Equal(map[string]int{"a": 1, "b": 2, "c": 3})) |
| 76 | + Expect(src).To(Equal(map[string]string{"a": "1", "b": "22", "c": "333"}), "original map should not be modified") |
| 77 | + }) |
| 78 | + |
| 79 | + It("should return an empty map for an empty or nil input map", func() { |
| 80 | + Expect(collections.ProjectMapToMap(nil, projectFunc)).To(BeEmpty()) |
| 81 | + Expect(collections.ProjectMapToMap(map[string]string{}, projectFunc)).To(BeEmpty()) |
| 82 | + }) |
| 83 | + |
| 84 | + It("should return nil for a nil projection function", func() { |
| 85 | + src := map[string]string{"a": "1", "b": "22", "c": "333"} |
| 86 | + projected := collections.ProjectMapToMap[string, string, string, int](src, nil) |
| 87 | + Expect(projected).To(BeNil()) |
| 88 | + Expect(src).To(Equal(map[string]string{"a": "1", "b": "22", "c": "333"}), "original map should not be modified") |
| 89 | + }) |
| 90 | + |
| 91 | + }) |
| 92 | + |
| 93 | +}) |
0 commit comments