From 356cee7b89c99ea1f77ea002bb2249b2950ad331 Mon Sep 17 00:00:00 2001 From: Alisdair McDiarmid Date: Wed, 27 Apr 2022 14:25:42 -0400 Subject: [PATCH] typeexpr: Add test coverage for optional modifier --- internal/typeexpr/get_type_test.go | 35 +++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/internal/typeexpr/get_type_test.go b/internal/typeexpr/get_type_test.go index 391bf4f938..0cfb5b39b7 100644 --- a/internal/typeexpr/get_type_test.go +++ b/internal/typeexpr/get_type_test.go @@ -1,6 +1,7 @@ package typeexpr import ( + "fmt" "testing" "github.com/hashicorp/hcl/v2/gohcl" @@ -245,10 +246,42 @@ func TestGetType(t *testing.T) { cty.List(cty.Map(cty.EmptyTuple)), ``, }, + + // Optional modifier + { + `object({name=string,age=optional(number)})`, + true, + cty.ObjectWithOptionalAttrs(map[string]cty.Type{ + "name": cty.String, + "age": cty.Number, + }, []string{"age"}), + ``, + }, + { + `object({name=string,age=optional(number)})`, + false, + cty.Object(map[string]cty.Type{ + "name": cty.String, + "age": cty.Number, + }), + `Optional attribute modifier is only for type constraints, not for exact types.`, + }, + { + `optional(string)`, + false, + cty.DynamicPseudoType, + `Keyword "optional" is valid only as a modifier for object type attributes.`, + }, + { + `optional`, + false, + cty.DynamicPseudoType, + `The keyword "optional" is not a valid type specification.`, + }, } for _, test := range tests { - t.Run(test.Source, func(t *testing.T) { + t.Run(fmt.Sprintf("%s (constraint=%v)", test.Source, test.Constraint), func(t *testing.T) { expr, diags := hclsyntax.ParseExpression([]byte(test.Source), "", hcl.Pos{Line: 1, Column: 1}) if diags.HasErrors() { t.Fatalf("failed to parse: %s", diags)