Skip to content

Commit

Permalink
Add tests for formatters printType
Browse files Browse the repository at this point in the history
  • Loading branch information
angrycub committed Sep 5, 2023
1 parent 4ac9f27 commit b69e2cc
Showing 1 changed file with 148 additions and 0 deletions.
148 changes: 148 additions & 0 deletions sdk/pack/variables/formatters_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package variables

import (
"strings"
"testing"

"github.com/hashicorp/nomad/ci"
"github.com/shoenig/test/must"
"github.com/zclconf/go-cty/cty"
)

func TestFormatters_printType(t *testing.T) {
ci.Parallel(t)
testCases := []struct {
name string
input cty.Type
expect string
err error
}{
{
name: "string",
input: cty.String,
expect: "string",
},
{
name: "bool",
input: cty.Bool,
expect: "bool",
},
{
name: "number",
input: cty.Number,
expect: "number",
},
{
name: "any",
input: cty.DynamicPseudoType,
expect: "dynamic",
},
{
name: "list/string",
input: cty.List(cty.String),
expect: "list(string)",
},
{
name: "list/bool",
input: cty.List(cty.Bool),
expect: "list(bool)",
},
{
name: "list/number",
input: cty.List(cty.Number),
expect: "list(number)",
},
{
name: "list/any",
input: cty.List(cty.DynamicPseudoType),
expect: "list(dynamic)",
},
{
name: "map/string",
input: cty.Map(cty.String),
expect: "map(string)",
},
{
name: "map/bool",
input: cty.Map(cty.Bool),
expect: "map(bool)",
},
{
name: "map/number",
input: cty.Map(cty.Number),
expect: "map(number)",
},
{
name: "map/any",
input: cty.Map(cty.DynamicPseudoType),
expect: "map(dynamic)",
},
{
name: "set/string",
input: cty.Set(cty.String),
expect: "set(string)",
},
{
name: "set/bool",
input: cty.Set(cty.Bool),
expect: "set(bool)",
},
{
name: "set/number",
input: cty.Set(cty.Number),
expect: "set(number)",
},
{
name: "map/any",
input: cty.Set(cty.DynamicPseudoType),
expect: "set(dynamic)",
},
{
name: "tuple",
input: cty.Tuple([]cty.Type{cty.String, cty.Bool, cty.Number, cty.DynamicPseudoType}),
expect: "tuple(string, bool, number, dynamic)",
},
}
for _, tc := range testCases {
tc := tc // capture range variable
t.Run(tc.name, func(t *testing.T) {
ci.Parallel(t)
out := printType(tc.input)
must.Eq(t, tc.expect, out, must.Sprint(tc.input.FriendlyName()))
})
}

t.Run("object", func(t *testing.T) {
// Object is special cased because it has a map that prints in
// non-guaranteed order.
ci.Parallel(t)
var tc = struct {
name string
input cty.Type
expect string
err error
}{
name: "object",
input: cty.Object(map[string]cty.Type{
"str": cty.String,
"b": cty.Bool,
"num": cty.Number,
"any": cty.DynamicPseudoType}),
expect: "object({str = string, b = bool, num = number, any = dynamic})",
}
out := printType(tc.input)
exp := tc.expect
must.True(t, strings.HasPrefix(out, "object({"))
out = strings.TrimPrefix(out, "object({")
exp = strings.TrimPrefix(exp, "object({")

must.True(t, strings.HasSuffix(out, "})"))
out = strings.TrimSuffix(out, "})")
exp = strings.TrimSuffix(exp, "})")

oParts := strings.Split(out, ", ")
eParts := strings.Split(exp, ", ")

must.SliceContainsAll(t, eParts, oParts)
})
}

0 comments on commit b69e2cc

Please sign in to comment.