From 4ecb4c40b37ba378289d6b7f9a2bf398c342ae7f Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Tue, 28 Mar 2023 17:41:13 +0100 Subject: [PATCH] decoder: Add tests for reference targets (Map) --- decoder/expr_map_ref_targets_test.go | 141 +++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 decoder/expr_map_ref_targets_test.go diff --git a/decoder/expr_map_ref_targets_test.go b/decoder/expr_map_ref_targets_test.go new file mode 100644 index 00000000..da705e0c --- /dev/null +++ b/decoder/expr_map_ref_targets_test.go @@ -0,0 +1,141 @@ +package decoder + +import ( + "fmt" + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/hashicorp/hcl-lang/reference" + "github.com/hashicorp/hcl-lang/schema" + "github.com/hashicorp/hcl/v2" + "github.com/hashicorp/hcl/v2/hclsyntax" + "github.com/hashicorp/hcl/v2/json" + "github.com/zclconf/go-cty-debug/ctydebug" + "github.com/zclconf/go-cty/cty" +) + +func TestCollectRefTargets_exprMap_hcl(t *testing.T) { + testCases := []struct { + testName string + attrSchema map[string]*schema.AttributeSchema + cfg string + expectedRefTargets reference.Targets + }{ + { + "constraint mismatch", + map[string]*schema.AttributeSchema{ + "attr": { + Constraint: schema.Map{ + Elem: schema.LiteralType{Type: cty.String}, + }, + IsOptional: true, + Address: &schema.AttributeAddrSchema{ + Steps: schema.Address{ + schema.AttrNameStep{}, + }, + AsExprType: true, + }, + }, + }, + `attr = keyword`, + reference.Targets{}, + }, + // TODO: Map(Keyword) - no target collectable Elem + // TODO: Map(Reference) - only 1st class + // TODO: Map(LiteralType) - empty type-aware + // TODO: Map(LiteralType) - type-aware with invalid key type + // TODO: Map(LiteralType) - type-aware with invalid value type + // TODO: Map(LiteralType) - type-unaware + // TODO: Map(Map(LiteralType)) - nested, type-aware + } + for i, tc := range testCases { + t.Run(fmt.Sprintf("%d-%s", i, tc.testName), func(t *testing.T) { + bodySchema := &schema.BodySchema{ + Attributes: tc.attrSchema, + } + + f, diags := hclsyntax.ParseConfig([]byte(tc.cfg), "test.hcl", hcl.InitialPos) + if len(diags) > 0 { + t.Error(diags) + } + d := testPathDecoder(t, &PathContext{ + Schema: bodySchema, + Files: map[string]*hcl.File{ + "test.hcl": f, + }, + }) + + targets, err := d.CollectReferenceTargets() + if err != nil { + t.Fatal(err) + } + + if diff := cmp.Diff(tc.expectedRefTargets, targets, ctydebug.CmpOptions); diff != "" { + t.Fatalf("unexpected targets: %s", diff) + } + }) + } +} + +func TestCollectRefTargets_exprMap_json(t *testing.T) { + testCases := []struct { + testName string + attrSchema map[string]*schema.AttributeSchema + cfg string + expectedRefTargets reference.Targets + }{ + { + "constraint mismatch", + map[string]*schema.AttributeSchema{ + "attr": { + Constraint: schema.Map{ + Elem: schema.LiteralType{Type: cty.String}, + }, + IsOptional: true, + Address: &schema.AttributeAddrSchema{ + Steps: schema.Address{ + schema.AttrNameStep{}, + }, + AsExprType: true, + }, + }, + }, + `{"attr": true}`, + reference.Targets{}, + }, + // TODO: Map(Keyword) - no target collectable Elem + // TODO: Map(Reference) - only 1st class + // TODO: Map(LiteralType) - empty type-aware + // TODO: Map(LiteralType) - type-aware with invalid key type + // TODO: Map(LiteralType) - type-aware with invalid value type + // TODO: Map(LiteralType) - type-unaware + // TODO: Map(Map(LiteralType)) - nested, type-aware + } + for i, tc := range testCases { + t.Run(fmt.Sprintf("%d-%s", i, tc.testName), func(t *testing.T) { + bodySchema := &schema.BodySchema{ + Attributes: tc.attrSchema, + } + + f, diags := json.ParseWithStartPos([]byte(tc.cfg), "test.hcl.json", hcl.InitialPos) + if len(diags) > 0 { + t.Error(diags) + } + d := testPathDecoder(t, &PathContext{ + Schema: bodySchema, + Files: map[string]*hcl.File{ + "test.hcl.json": f, + }, + }) + + targets, err := d.CollectReferenceTargets() + if err != nil { + t.Fatal(err) + } + + if diff := cmp.Diff(tc.expectedRefTargets, targets, ctydebug.CmpOptions); diff != "" { + t.Fatalf("unexpected targets: %s", diff) + } + }) + } +}