github.com/jpedro/terraform@v0.11.12-beta1/helper/variables/merge_test.go (about) 1 package variables 2 3 import ( 4 "fmt" 5 "reflect" 6 "testing" 7 ) 8 9 func TestMerge(t *testing.T) { 10 cases := []struct { 11 Name string 12 A, B map[string]interface{} 13 Expected map[string]interface{} 14 }{ 15 { 16 "basic key/value", 17 map[string]interface{}{ 18 "foo": "bar", 19 }, 20 map[string]interface{}{ 21 "bar": "baz", 22 }, 23 map[string]interface{}{ 24 "foo": "bar", 25 "bar": "baz", 26 }, 27 }, 28 29 { 30 "map unset", 31 map[string]interface{}{ 32 "foo": "bar", 33 }, 34 map[string]interface{}{ 35 "bar": map[string]interface{}{ 36 "foo": "bar", 37 }, 38 }, 39 map[string]interface{}{ 40 "foo": "bar", 41 "bar": map[string]interface{}{ 42 "foo": "bar", 43 }, 44 }, 45 }, 46 47 { 48 "map merge", 49 map[string]interface{}{ 50 "foo": "bar", 51 "bar": map[string]interface{}{ 52 "bar": "baz", 53 }, 54 }, 55 map[string]interface{}{ 56 "bar": map[string]interface{}{ 57 "foo": "bar", 58 }, 59 }, 60 map[string]interface{}{ 61 "foo": "bar", 62 "bar": map[string]interface{}{ 63 "foo": "bar", 64 "bar": "baz", 65 }, 66 }, 67 }, 68 69 { 70 "basic k/v with lists", 71 map[string]interface{}{ 72 "foo": "bar", 73 "bar": []interface{}{"foo"}, 74 }, 75 map[string]interface{}{ 76 "bar": []interface{}{"bar"}, 77 }, 78 map[string]interface{}{ 79 "foo": "bar", 80 "bar": []interface{}{"bar"}, 81 }, 82 }, 83 } 84 85 for i, tc := range cases { 86 t.Run(fmt.Sprintf("%d-%s", i, tc.Name), func(t *testing.T) { 87 actual := Merge(tc.A, tc.B) 88 if !reflect.DeepEqual(tc.Expected, actual) { 89 t.Fatalf("bad: %#v", actual) 90 } 91 }) 92 } 93 }