github.com/arvindram03/terraform@v0.3.7-0.20150212015210-408f838db36d/helper/schema/field_writer_map_test.go (about) 1 package schema 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 func TestMapFieldWriter_impl(t *testing.T) { 9 var _ FieldWriter = new(MapFieldWriter) 10 } 11 12 func TestMapFieldWriter(t *testing.T) { 13 schema := map[string]*Schema{ 14 "bool": &Schema{Type: TypeBool}, 15 "int": &Schema{Type: TypeInt}, 16 "string": &Schema{Type: TypeString}, 17 "list": &Schema{ 18 Type: TypeList, 19 Elem: &Schema{Type: TypeString}, 20 }, 21 "listInt": &Schema{ 22 Type: TypeList, 23 Elem: &Schema{Type: TypeInt}, 24 }, 25 "map": &Schema{Type: TypeMap}, 26 "set": &Schema{ 27 Type: TypeSet, 28 Elem: &Schema{Type: TypeInt}, 29 Set: func(a interface{}) int { 30 return a.(int) 31 }, 32 }, 33 "setDeep": &Schema{ 34 Type: TypeSet, 35 Elem: &Resource{ 36 Schema: map[string]*Schema{ 37 "index": &Schema{Type: TypeInt}, 38 "value": &Schema{Type: TypeString}, 39 }, 40 }, 41 Set: func(a interface{}) int { 42 return a.(map[string]interface{})["index"].(int) 43 }, 44 }, 45 } 46 47 cases := map[string]struct { 48 Addr []string 49 Value interface{} 50 Err bool 51 Out map[string]string 52 }{ 53 "noexist": { 54 []string{"noexist"}, 55 42, 56 true, 57 map[string]string{}, 58 }, 59 60 "bool": { 61 []string{"bool"}, 62 false, 63 false, 64 map[string]string{ 65 "bool": "false", 66 }, 67 }, 68 69 "int": { 70 []string{"int"}, 71 42, 72 false, 73 map[string]string{ 74 "int": "42", 75 }, 76 }, 77 78 "string": { 79 []string{"string"}, 80 "42", 81 false, 82 map[string]string{ 83 "string": "42", 84 }, 85 }, 86 87 "list of strings": { 88 []string{"list"}, 89 []interface{}{"foo", "bar"}, 90 false, 91 map[string]string{ 92 "list.#": "2", 93 "list.0": "foo", 94 "list.1": "bar", 95 }, 96 }, 97 98 "list element": { 99 []string{"list", "0"}, 100 "string", 101 true, 102 map[string]string{}, 103 }, 104 105 "map": { 106 []string{"map"}, 107 map[string]interface{}{"foo": "bar"}, 108 false, 109 map[string]string{ 110 "map.#": "1", 111 "map.foo": "bar", 112 }, 113 }, 114 115 "map delete": { 116 []string{"map"}, 117 nil, 118 false, 119 map[string]string{ 120 "map": "", 121 }, 122 }, 123 124 "map element": { 125 []string{"map", "foo"}, 126 "bar", 127 true, 128 map[string]string{}, 129 }, 130 131 "set": { 132 []string{"set"}, 133 []interface{}{1, 2, 5}, 134 false, 135 map[string]string{ 136 "set.#": "3", 137 "set.1": "1", 138 "set.2": "2", 139 "set.5": "5", 140 }, 141 }, 142 143 "set nil": { 144 []string{"set"}, 145 nil, 146 false, 147 map[string]string{ 148 "set.#": "0", 149 }, 150 }, 151 152 "set resource": { 153 []string{"setDeep"}, 154 []interface{}{ 155 map[string]interface{}{ 156 "index": 10, 157 "value": "foo", 158 }, 159 map[string]interface{}{ 160 "index": 50, 161 "value": "bar", 162 }, 163 }, 164 false, 165 map[string]string{ 166 "setDeep.#": "2", 167 "setDeep.10.index": "10", 168 "setDeep.10.value": "foo", 169 "setDeep.50.index": "50", 170 "setDeep.50.value": "bar", 171 }, 172 }, 173 174 "set element": { 175 []string{"set", "5"}, 176 5, 177 true, 178 map[string]string{}, 179 }, 180 181 "full object": { 182 nil, 183 map[string]interface{}{ 184 "string": "foo", 185 "list": []interface{}{"foo", "bar"}, 186 }, 187 false, 188 map[string]string{ 189 "string": "foo", 190 "list.#": "2", 191 "list.0": "foo", 192 "list.1": "bar", 193 }, 194 }, 195 } 196 197 for name, tc := range cases { 198 w := &MapFieldWriter{Schema: schema} 199 err := w.WriteField(tc.Addr, tc.Value) 200 if (err != nil) != tc.Err { 201 t.Fatalf("%s: err: %s", name, err) 202 } 203 204 actual := w.Map() 205 if !reflect.DeepEqual(actual, tc.Out) { 206 t.Fatalf("%s: bad: %#v", name, actual) 207 } 208 } 209 }