github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/configs/configschema/empty_value_test.go (about) 1 package configschema 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/apparentlymart/go-dump/dump" 8 "github.com/davecgh/go-spew/spew" 9 "github.com/zclconf/go-cty/cty" 10 ) 11 12 func TestBlockEmptyValue(t *testing.T) { 13 tests := []struct { 14 Schema *Block 15 Want cty.Value 16 }{ 17 { 18 &Block{}, 19 cty.EmptyObjectVal, 20 }, 21 { 22 &Block{ 23 Attributes: map[string]*Attribute{ 24 "str": {Type: cty.String, Required: true}, 25 }, 26 }, 27 cty.ObjectVal(map[string]cty.Value{ 28 "str": cty.NullVal(cty.String), 29 }), 30 }, 31 { 32 &Block{ 33 BlockTypes: map[string]*NestedBlock{ 34 "single": { 35 Nesting: NestingSingle, 36 Block: Block{ 37 Attributes: map[string]*Attribute{ 38 "str": {Type: cty.String, Required: true}, 39 }, 40 }, 41 }, 42 }, 43 }, 44 cty.ObjectVal(map[string]cty.Value{ 45 "single": cty.NullVal(cty.Object(map[string]cty.Type{ 46 "str": cty.String, 47 })), 48 }), 49 }, 50 { 51 &Block{ 52 BlockTypes: map[string]*NestedBlock{ 53 "group": { 54 Nesting: NestingGroup, 55 Block: Block{ 56 Attributes: map[string]*Attribute{ 57 "str": {Type: cty.String, Required: true}, 58 }, 59 }, 60 }, 61 }, 62 }, 63 cty.ObjectVal(map[string]cty.Value{ 64 "group": cty.ObjectVal(map[string]cty.Value{ 65 "str": cty.NullVal(cty.String), 66 }), 67 }), 68 }, 69 { 70 &Block{ 71 BlockTypes: map[string]*NestedBlock{ 72 "list": { 73 Nesting: NestingList, 74 Block: Block{ 75 Attributes: map[string]*Attribute{ 76 "str": {Type: cty.String, Required: true}, 77 }, 78 }, 79 }, 80 }, 81 }, 82 cty.ObjectVal(map[string]cty.Value{ 83 "list": cty.ListValEmpty(cty.Object(map[string]cty.Type{ 84 "str": cty.String, 85 })), 86 }), 87 }, 88 { 89 &Block{ 90 BlockTypes: map[string]*NestedBlock{ 91 "list_dynamic": { 92 Nesting: NestingList, 93 Block: Block{ 94 Attributes: map[string]*Attribute{ 95 "str": {Type: cty.DynamicPseudoType, Required: true}, 96 }, 97 }, 98 }, 99 }, 100 }, 101 cty.ObjectVal(map[string]cty.Value{ 102 "list_dynamic": cty.EmptyTupleVal, 103 }), 104 }, 105 { 106 &Block{ 107 BlockTypes: map[string]*NestedBlock{ 108 "map": { 109 Nesting: NestingMap, 110 Block: Block{ 111 Attributes: map[string]*Attribute{ 112 "str": {Type: cty.String, Required: true}, 113 }, 114 }, 115 }, 116 }, 117 }, 118 cty.ObjectVal(map[string]cty.Value{ 119 "map": cty.MapValEmpty(cty.Object(map[string]cty.Type{ 120 "str": cty.String, 121 })), 122 }), 123 }, 124 { 125 &Block{ 126 BlockTypes: map[string]*NestedBlock{ 127 "map_dynamic": { 128 Nesting: NestingMap, 129 Block: Block{ 130 Attributes: map[string]*Attribute{ 131 "str": {Type: cty.DynamicPseudoType, Required: true}, 132 }, 133 }, 134 }, 135 }, 136 }, 137 cty.ObjectVal(map[string]cty.Value{ 138 "map_dynamic": cty.EmptyObjectVal, 139 }), 140 }, 141 { 142 &Block{ 143 BlockTypes: map[string]*NestedBlock{ 144 "set": { 145 Nesting: NestingSet, 146 Block: Block{ 147 Attributes: map[string]*Attribute{ 148 "str": {Type: cty.String, Required: true}, 149 }, 150 }, 151 }, 152 }, 153 }, 154 cty.ObjectVal(map[string]cty.Value{ 155 "set": cty.SetValEmpty(cty.Object(map[string]cty.Type{ 156 "str": cty.String, 157 })), 158 }), 159 }, 160 } 161 162 for _, test := range tests { 163 t.Run(fmt.Sprintf("%#v", test.Schema), func(t *testing.T) { 164 got := test.Schema.EmptyValue() 165 if !test.Want.RawEquals(got) { 166 t.Errorf("wrong result\nschema: %s\ngot: %s\nwant: %s", spew.Sdump(test.Schema), dump.Value(got), dump.Value(test.Want)) 167 } 168 }) 169 } 170 }