Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Object GoString() #86

Merged
merged 1 commit into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
}
})
}
}