github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/helper/schema/field_reader_map_test.go (about) 1 package schema 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 func TestMapFieldReader_impl(t *testing.T) { 9 var _ FieldReader = new(MapFieldReader) 10 } 11 12 func TestMapFieldReader(t *testing.T) { 13 testFieldReader(t, func(s map[string]*Schema) FieldReader { 14 return &MapFieldReader{ 15 Schema: s, 16 17 Map: BasicMapReader(map[string]string{ 18 "bool": "true", 19 "int": "42", 20 "float": "3.1415", 21 "string": "string", 22 23 "list.#": "2", 24 "list.0": "foo", 25 "list.1": "bar", 26 27 "listInt.#": "2", 28 "listInt.0": "21", 29 "listInt.1": "42", 30 31 "map.%": "2", 32 "map.foo": "bar", 33 "map.bar": "baz", 34 35 "set.#": "2", 36 "set.10": "10", 37 "set.50": "50", 38 39 "setDeep.#": "2", 40 "setDeep.10.index": "10", 41 "setDeep.10.value": "foo", 42 "setDeep.50.index": "50", 43 "setDeep.50.value": "bar", 44 }), 45 } 46 }) 47 } 48 49 func TestMapFieldReader_extra(t *testing.T) { 50 r := &MapFieldReader{ 51 Schema: map[string]*Schema{ 52 "mapDel": &Schema{Type: TypeMap}, 53 "mapEmpty": &Schema{Type: TypeMap}, 54 }, 55 56 Map: BasicMapReader(map[string]string{ 57 "mapDel": "", 58 59 "mapEmpty.%": "0", 60 }), 61 } 62 63 cases := map[string]struct { 64 Addr []string 65 Out interface{} 66 OutOk bool 67 OutComputed bool 68 OutErr bool 69 }{ 70 "mapDel": { 71 []string{"mapDel"}, 72 map[string]interface{}{}, 73 true, 74 false, 75 false, 76 }, 77 78 "mapEmpty": { 79 []string{"mapEmpty"}, 80 map[string]interface{}{}, 81 true, 82 false, 83 false, 84 }, 85 } 86 87 for name, tc := range cases { 88 out, err := r.ReadField(tc.Addr) 89 if err != nil != tc.OutErr { 90 t.Fatalf("%s: err: %s", name, err) 91 } 92 if out.Computed != tc.OutComputed { 93 t.Fatalf("%s: err: %#v", name, out.Computed) 94 } 95 96 if s, ok := out.Value.(*Set); ok { 97 // If it is a set, convert to a list so its more easily checked. 98 out.Value = s.List() 99 } 100 101 if !reflect.DeepEqual(out.Value, tc.Out) { 102 t.Fatalf("%s: out: %#v", name, out.Value) 103 } 104 if out.Exists != tc.OutOk { 105 t.Fatalf("%s: outOk: %#v", name, out.Exists) 106 } 107 } 108 }