Skip to content

Commit

Permalink
cty: fix GoString for object types with optional attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
mildwonkey committed Feb 22, 2021
1 parent 251ac0b commit 73e25aa
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cty/object_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
88 changes: 88 additions & 0 deletions cty/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}

0 comments on commit 73e25aa

Please sign in to comment.