From b69e2cc1c12003f40792c1f4b649b97f478a7cf2 Mon Sep 17 00:00:00 2001 From: Charlie Voiselle <464492+angrycub@users.noreply.github.com> Date: Tue, 5 Sep 2023 18:14:12 -0400 Subject: [PATCH] Add tests for formatters printType --- sdk/pack/variables/formatters_test.go | 148 ++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 sdk/pack/variables/formatters_test.go diff --git a/sdk/pack/variables/formatters_test.go b/sdk/pack/variables/formatters_test.go new file mode 100644 index 00000000..3dc4a32f --- /dev/null +++ b/sdk/pack/variables/formatters_test.go @@ -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) + }) +}