github.com/sleungcy-sap/cli@v7.1.0+incompatible/util/generic/map_test.go (about) 1 package generic_test 2 3 import ( 4 . "code.cloudfoundry.org/cli/util/generic" 5 . "github.com/onsi/ginkgo" 6 . "github.com/onsi/gomega" 7 ) 8 9 var _ = Describe("generic maps", func() { 10 Describe("DeepMerge", func() { 11 It("deep merges, with the last map taking precedence in conflicts", func() { 12 map1 := NewMap(map[interface{}]interface{}{ 13 "key1": "val1", 14 "key2": "val2", 15 "nest1": map[interface{}]interface{}{ 16 "nestKey1": "nest1Val1", 17 "nestKey2": "nest1Val2", 18 }, 19 "nest2": []interface{}{ 20 "nest2Val1", 21 }, 22 }) 23 24 map2 := NewMap(map[interface{}]interface{}{ 25 "key1": "newVal1", 26 "nest1": map[interface{}]interface{}{ 27 "nestKey1": "newNest1Val1", 28 }, 29 "nest2": []interface{}{ 30 "something", 31 }, 32 }) 33 34 expectedMap := NewMap(map[interface{}]interface{}{ 35 "key1": "newVal1", 36 "key2": "val2", 37 "nest1": NewMap(map[interface{}]interface{}{ 38 "nestKey1": "newNest1Val1", 39 "nestKey2": "nest1Val2", 40 }), 41 "nest2": []interface{}{ 42 "nest2Val1", 43 "something", 44 }, 45 }) 46 47 mergedMap := DeepMerge(map1, map2) 48 Expect(mergedMap).To(Equal(expectedMap)) 49 }) 50 51 When("the second map overwrites a value in the first map with nil", func() { 52 It("sets the value to nil", func() { 53 map1 := NewMap(map[interface{}]interface{}{ 54 "nest1": map[interface{}]interface{}{}, 55 }) 56 57 map2 := NewMap(map[interface{}]interface{}{ 58 "nest1": nil, 59 }) 60 61 mergedMap := DeepMerge(map1, map2) 62 Expect(mergedMap.Get("nest1")).To(BeNil()) 63 }) 64 }) 65 66 When("the second map overwrites a nil value in the first map", func() { 67 It("overwrites the nil value", func() { 68 expected := map[interface{}]interface{}{} 69 70 map1 := NewMap(map[interface{}]interface{}{ 71 "nest1": nil, 72 }) 73 74 map2 := NewMap(map[interface{}]interface{}{ 75 "nest1": expected, 76 }) 77 78 mergedMap := DeepMerge(map1, map2) 79 Expect(mergedMap.Get("nest1")).To(BeEquivalentTo(&expected)) 80 }) 81 }) 82 }) 83 84 Describe("IsMappable", func() { 85 It("returns true for generic.Map", func() { 86 m := NewMap() 87 Expect(IsMappable(m)).To(BeTrue()) 88 }) 89 90 It("returns true for maps", func() { 91 var m map[string]interface{} 92 Expect(IsMappable(m)).To(BeTrue()) 93 94 var n map[interface{}]interface{} 95 Expect(IsMappable(n)).To(BeTrue()) 96 97 var o map[int]interface{} 98 Expect(IsMappable(o)).To(BeTrue()) 99 }) 100 101 It("returns false for other things", func() { 102 Expect(IsMappable(2)).To(BeFalse()) 103 Expect(IsMappable("2")).To(BeFalse()) 104 Expect(IsMappable(true)).To(BeFalse()) 105 Expect(IsMappable([]string{"hello"})).To(BeFalse()) 106 Expect(IsMappable(nil)).To(BeFalse()) 107 }) 108 }) 109 })