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