github.com/skyscape-cloud-services/terraform@v0.9.2-0.20170609144644-7ece028a1747/flatmap/expand_test.go (about) 1 package flatmap 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/hashicorp/hil" 8 ) 9 10 func TestExpand(t *testing.T) { 11 cases := []struct { 12 Map map[string]string 13 Key string 14 Output interface{} 15 }{ 16 { 17 Map: map[string]string{ 18 "foo": "bar", 19 "bar": "baz", 20 }, 21 Key: "foo", 22 Output: "bar", 23 }, 24 25 { 26 Map: map[string]string{ 27 "foo.#": "2", 28 "foo.0": "one", 29 "foo.1": "two", 30 }, 31 Key: "foo", 32 Output: []interface{}{ 33 "one", 34 "two", 35 }, 36 }, 37 38 { 39 Map: map[string]string{ 40 "foo.#": "1", 41 "foo.0.name": "bar", 42 "foo.0.port": "3000", 43 "foo.0.enabled": "true", 44 }, 45 Key: "foo", 46 Output: []interface{}{ 47 map[string]interface{}{ 48 "name": "bar", 49 "port": "3000", 50 "enabled": true, 51 }, 52 }, 53 }, 54 55 { 56 Map: map[string]string{ 57 "foo.#": "1", 58 "foo.0.name": "bar", 59 "foo.0.ports.#": "2", 60 "foo.0.ports.0": "1", 61 "foo.0.ports.1": "2", 62 }, 63 Key: "foo", 64 Output: []interface{}{ 65 map[string]interface{}{ 66 "name": "bar", 67 "ports": []interface{}{ 68 "1", 69 "2", 70 }, 71 }, 72 }, 73 }, 74 75 { 76 Map: map[string]string{ 77 "list_of_map.#": "2", 78 "list_of_map.0.%": "1", 79 "list_of_map.0.a": "1", 80 "list_of_map.1.%": "2", 81 "list_of_map.1.b": "2", 82 "list_of_map.1.c": "3", 83 }, 84 Key: "list_of_map", 85 Output: []interface{}{ 86 map[string]interface{}{ 87 "a": "1", 88 }, 89 map[string]interface{}{ 90 "b": "2", 91 "c": "3", 92 }, 93 }, 94 }, 95 96 { 97 Map: map[string]string{ 98 "map_of_list.%": "2", 99 "map_of_list.list2.#": "1", 100 "map_of_list.list2.0": "c", 101 "map_of_list.list1.#": "2", 102 "map_of_list.list1.0": "a", 103 "map_of_list.list1.1": "b", 104 }, 105 Key: "map_of_list", 106 Output: map[string]interface{}{ 107 "list1": []interface{}{"a", "b"}, 108 "list2": []interface{}{"c"}, 109 }, 110 }, 111 112 { 113 Map: map[string]string{ 114 "set.#": "3", 115 "set.1234": "a", 116 "set.1235": "b", 117 "set.1236": "c", 118 }, 119 Key: "set", 120 Output: []interface{}{"a", "b", "c"}, 121 }, 122 123 { 124 Map: map[string]string{ 125 "computed_set.#": "1", 126 "computed_set.~1234.a": "a", 127 "computed_set.~1234.b": "b", 128 "computed_set.~1234.c": "c", 129 }, 130 Key: "computed_set", 131 Output: []interface{}{ 132 map[string]interface{}{"a": "a", "b": "b", "c": "c"}, 133 }, 134 }, 135 136 { 137 Map: map[string]string{ 138 "struct.#": "1", 139 "struct.0.name": "hello", 140 "struct.0.rules.#": hil.UnknownValue, 141 }, 142 Key: "struct", 143 Output: []interface{}{ 144 map[string]interface{}{ 145 "name": "hello", 146 "rules": hil.UnknownValue, 147 }, 148 }, 149 }, 150 151 { 152 Map: map[string]string{ 153 "struct.#": "1", 154 "struct.0.name": "hello", 155 "struct.0.set.#": "0", 156 "struct.0.set.0.key": "value", 157 }, 158 Key: "struct", 159 Output: []interface{}{ 160 map[string]interface{}{ 161 "name": "hello", 162 "set": []interface{}{}, 163 }, 164 }, 165 }, 166 167 { 168 Map: map[string]string{ 169 "empty_map_of_sets.%": "0", 170 "empty_map_of_sets.set1.#": "0", 171 "empty_map_of_sets.set1.1234": "x", 172 }, 173 Key: "empty_map_of_sets", 174 Output: map[string]interface{}{}, 175 }, 176 } 177 178 for _, tc := range cases { 179 t.Run(tc.Key, func(t *testing.T) { 180 actual := Expand(tc.Map, tc.Key) 181 if !reflect.DeepEqual(actual, tc.Output) { 182 t.Errorf( 183 "Key: %v\nMap:\n\n%#v\n\nOutput:\n\n%#v\n\nExpected:\n\n%#v\n", 184 tc.Key, 185 tc.Map, 186 actual, 187 tc.Output) 188 } 189 }) 190 } 191 }