| 1 | package common |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/stretchr/testify/require" |
| 7 | ) |
| 8 | |
| 9 | func TestMapMergeDeepReturnsNew(t *testing.T) { |
| 10 | leftMap := make(map[string]any) |
| 11 | leftMap["A"] = "Hello World" |
| 12 | |
| 13 | rightMap := make(map[string]any) |
| 14 | rightMap["A"] = "Foo" |
| 15 | |
| 16 | MapMergeDeep(leftMap, rightMap) |
| 17 | |
| 18 | require.Equal(t, "Hello World", leftMap["A"], "MapMergeDeep should return a new map instance") |
| 19 | } |
| 20 | |
| 21 | func TestMapMergeDeepNewKey(t *testing.T) { |
| 22 | leftMap := make(map[string]any) |
| 23 | leftMap["A"] = "Hello World" |
| 24 | /* |
| 25 | leftMap |
| 26 | { |
| 27 | A: "Hello World" |
| 28 | } |
| 29 | */ |
| 30 | |
| 31 | rightMap := make(map[string]any) |
| 32 | rightMap["B"] = "Bar" |
| 33 | /* |
| 34 | rightMap |
| 35 | { |
| 36 | B: "Bar" |
| 37 | } |
| 38 | */ |
| 39 | |
| 40 | result := MapMergeDeep(leftMap, rightMap) |
| 41 | /* |
| 42 | expected |
| 43 | { |
| 44 | A: "Hello World" |
| 45 | B: "Bar" |
| 46 | } |
| 47 | */ |
| 48 | |
| 49 | require.Equal(t, "Bar", result["B"], "New keys in right map should exist in resulting map") |
| 50 | } |
| 51 | |
| 52 | func TestMapMergeDeepRecursesOnMaps(t *testing.T) { |
| 53 | leftMapA := make(map[string]any) |
| 54 | leftMapA["B"] = "A value!" |
| 55 | leftMapA["C"] = "Another value!" |
| 56 | |
| 57 | leftMap := make(map[string]any) |
| 58 | leftMap["A"] = leftMapA |
| 59 | /* |
| 60 | leftMap |
| 61 | { |
| 62 | A: { |
| 63 | B: "A value!" |
| 64 | C: "Another value!" |
| 65 | } |
| 66 | } |
| 67 | */ |
| 68 | |
| 69 | rightMapA := make(map[string]any) |
| 70 | rightMapA["C"] = "A different value!" |
| 71 | |
| 72 | rightMap := make(map[string]any) |
| 73 | rightMap["A"] = rightMapA |
| 74 | /* |
| 75 | rightMap |
| 76 | { |
| 77 | A: { |
| 78 | C: "A different value!" |
| 79 | } |
| 80 | } |
| 81 | */ |
| 82 | |
| 83 | result := MapMergeDeep(leftMap, rightMap) |
| 84 | /* |
| 85 | expected |
| 86 | { |
| 87 | A: { |
| 88 | B: "A value!" |
| 89 | C: "A different value!" |
| 90 | } |
| 91 | } |
| 92 | */ |
| 93 | |
| 94 | resultA := result["A"].(map[string]any) |
| 95 | require.Equal(t, "A value!", resultA["B"], "Unaltered values should not change") |
| 96 | require.Equal(t, "A different value!", resultA["C"], "Nested values should be altered") |
| 97 | } |
| 98 | |
| 99 | func TestMapMergeDeepRightNotAMap(t *testing.T) { |
| 100 | leftMapA := make(map[string]any) |
| 101 | leftMapA["B"] = "A value!" |
| 102 | |
| 103 | leftMap := make(map[string]any) |
| 104 | leftMap["A"] = leftMapA |
| 105 | /* |
| 106 | origMap |
| 107 | { |
| 108 | A: { |
| 109 | B: "A value!" |
| 110 | } |
| 111 | } |
| 112 | */ |
| 113 | |
| 114 | rightMap := make(map[string]any) |
| 115 | rightMap["A"] = "Not a map!" |
| 116 | /* |
| 117 | newMap |
| 118 | { |
| 119 | A: "Not a map!" |
| 120 | } |
| 121 | */ |
| 122 | |
| 123 | result := MapMergeDeep(leftMap, rightMap) |
| 124 | /* |
| 125 | expected |
| 126 | { |
| 127 | A: "Not a map!" |
| 128 | } |
| 129 | */ |
| 130 | |
| 131 | require.Equal(t, "Not a map!", result["A"], "Right values that are not a map should be set on the result") |
| 132 | } |