github.com/kcburge/terraform@v0.11.12-beta1/config/configschema/implied_type_test.go (about) 1 package configschema 2 3 import ( 4 "testing" 5 6 "github.com/zclconf/go-cty/cty" 7 ) 8 9 func TestBlockImpliedType(t *testing.T) { 10 tests := map[string]struct { 11 Schema *Block 12 Want cty.Type 13 }{ 14 "nil": { 15 nil, 16 cty.EmptyObject, 17 }, 18 "empty": { 19 &Block{}, 20 cty.EmptyObject, 21 }, 22 "attributes": { 23 &Block{ 24 Attributes: map[string]*Attribute{ 25 "optional": { 26 Type: cty.String, 27 Optional: true, 28 }, 29 "required": { 30 Type: cty.Number, 31 Required: true, 32 }, 33 "computed": { 34 Type: cty.List(cty.Bool), 35 Computed: true, 36 }, 37 "optional_computed": { 38 Type: cty.Map(cty.Bool), 39 Optional: true, 40 }, 41 }, 42 }, 43 cty.Object(map[string]cty.Type{ 44 "optional": cty.String, 45 "required": cty.Number, 46 "computed": cty.List(cty.Bool), 47 "optional_computed": cty.Map(cty.Bool), 48 }), 49 }, 50 "blocks": { 51 &Block{ 52 BlockTypes: map[string]*NestedBlock{ 53 "single": &NestedBlock{ 54 Nesting: NestingSingle, 55 Block: Block{ 56 Attributes: map[string]*Attribute{ 57 "foo": { 58 Type: cty.DynamicPseudoType, 59 Required: true, 60 }, 61 }, 62 }, 63 }, 64 "list": &NestedBlock{ 65 Nesting: NestingList, 66 }, 67 "set": &NestedBlock{ 68 Nesting: NestingSet, 69 }, 70 "map": &NestedBlock{ 71 Nesting: NestingMap, 72 }, 73 }, 74 }, 75 cty.Object(map[string]cty.Type{ 76 "single": cty.Object(map[string]cty.Type{ 77 "foo": cty.DynamicPseudoType, 78 }), 79 "list": cty.List(cty.EmptyObject), 80 "set": cty.Set(cty.EmptyObject), 81 "map": cty.Map(cty.EmptyObject), 82 }), 83 }, 84 "deep block nesting": { 85 &Block{ 86 BlockTypes: map[string]*NestedBlock{ 87 "single": &NestedBlock{ 88 Nesting: NestingSingle, 89 Block: Block{ 90 BlockTypes: map[string]*NestedBlock{ 91 "list": &NestedBlock{ 92 Nesting: NestingList, 93 Block: Block{ 94 BlockTypes: map[string]*NestedBlock{ 95 "set": &NestedBlock{ 96 Nesting: NestingSet, 97 }, 98 }, 99 }, 100 }, 101 }, 102 }, 103 }, 104 }, 105 }, 106 cty.Object(map[string]cty.Type{ 107 "single": cty.Object(map[string]cty.Type{ 108 "list": cty.List(cty.Object(map[string]cty.Type{ 109 "set": cty.Set(cty.EmptyObject), 110 })), 111 }), 112 }), 113 }, 114 } 115 116 for name, test := range tests { 117 t.Run(name, func(t *testing.T) { 118 got := test.Schema.ImpliedType() 119 if !got.Equals(test.Want) { 120 t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want) 121 } 122 }) 123 } 124 }