diff --git a/cty/object_type.go b/cty/object_type.go index 0c67d4ec..e6bbab4a 100644 --- a/cty/object_type.go +++ b/cty/object_type.go @@ -112,7 +112,7 @@ func (t typeObject) GoString() string { return "cty.EmptyObject" } if len(t.AttrOptional) > 0 { - opt := make([]string, len(t.AttrOptional)) + var opt []string for k := range t.AttrOptional { opt = append(opt, k) } diff --git a/cty/type_test.go b/cty/type_test.go index 22af9a2c..c1568374 100644 --- a/cty/type_test.go +++ b/cty/type_test.go @@ -61,3 +61,91 @@ func TestNilTypeEquals(t *testing.T) { t.Fatal("expected NilTypes to equal") } } + +func TestTypeGoString(t *testing.T) { + tests := []struct { + Type Type + Want string + }{ + { + DynamicPseudoType, + `cty.DynamicPseudoType`, + }, + { + String, + `cty.String`, + }, + { + Tuple([]Type{String, Bool}), + `cty.Tuple([]cty.Type{cty.String, cty.Bool})`, + }, + + { + Number, + `cty.Number`, + }, + { + Bool, + `cty.Bool`, + }, + { + List(String), + `cty.List(cty.String)`, + }, + { + List(List(String)), + `cty.List(cty.List(cty.String))`, + }, + { + List(Bool), + `cty.List(cty.Bool)`, + }, + { + Set(String), + `cty.Set(cty.String)`, + }, + { + Set(Map(String)), + `cty.Set(cty.Map(cty.String))`, + }, + { + Set(Bool), + `cty.Set(cty.Bool)`, + }, + { + Tuple([]Type{Bool}), + `cty.Tuple([]cty.Type{cty.Bool})`, + }, + + { + Map(String), + `cty.Map(cty.String)`, + }, + { + Map(Set(String)), + `cty.Map(cty.Set(cty.String))`, + }, + { + Map(Bool), + `cty.Map(cty.Bool)`, + }, + { + Object(map[string]Type{"foo": Bool}), + `cty.Object(map[string]cty.Type{"foo":cty.Bool})`, + }, + { + ObjectWithOptionalAttrs(map[string]Type{"foo": Bool, "bar": String}, []string{"bar"}), + `cty.ObjectWithOptionalAttrs(map[string]cty.Type{"bar":cty.String, "foo":cty.Bool}, []string{"bar"})`, + }, + } + + for _, test := range tests { + t.Run(test.Type.GoString(), func(t *testing.T) { + got := test.Type.GoString() + want := test.Want + if got != want { + t.Errorf("wrong result\ngot: %s\nwant: %s", got, want) + } + }) + } +}