github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/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.foo": "bar", 111 }, 112 }, 113 114 "map delete": { 115 []string{"map"}, 116 nil, 117 false, 118 map[string]string{ 119 "map": "", 120 }, 121 }, 122 123 "map element": { 124 []string{"map", "foo"}, 125 "bar", 126 true, 127 map[string]string{}, 128 }, 129 130 "set": { 131 []string{"set"}, 132 []interface{}{1, 2, 5}, 133 false, 134 map[string]string{ 135 "set.#": "3", 136 "set.1": "1", 137 "set.2": "2", 138 "set.5": "5", 139 }, 140 }, 141 142 "set resource": { 143 []string{"setDeep"}, 144 []interface{}{ 145 map[string]interface{}{ 146 "index": 10, 147 "value": "foo", 148 }, 149 map[string]interface{}{ 150 "index": 50, 151 "value": "bar", 152 }, 153 }, 154 false, 155 map[string]string{ 156 "setDeep.#": "2", 157 "setDeep.10.index": "10", 158 "setDeep.10.value": "foo", 159 "setDeep.50.index": "50", 160 "setDeep.50.value": "bar", 161 }, 162 }, 163 164 "set element": { 165 []string{"set", "5"}, 166 5, 167 true, 168 map[string]string{}, 169 }, 170 171 "full object": { 172 nil, 173 map[string]interface{}{ 174 "string": "foo", 175 "list": []interface{}{"foo", "bar"}, 176 }, 177 false, 178 map[string]string{ 179 "string": "foo", 180 "list.#": "2", 181 "list.0": "foo", 182 "list.1": "bar", 183 }, 184 }, 185 } 186 187 for name, tc := range cases { 188 w := &MapFieldWriter{Schema: schema} 189 err := w.WriteField(tc.Addr, tc.Value) 190 if (err != nil) != tc.Err { 191 t.Fatalf("%s: err: %s", name, err) 192 } 193 194 actual := w.Map() 195 if !reflect.DeepEqual(actual, tc.Out) { 196 t.Fatalf("%s: bad: %#v", name, actual) 197 } 198 } 199 }