Skip to content

Commit

Permalink
decoder: Add tests for reference targets (Map)
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Mar 28, 2023
1 parent d58248a commit 4ecb4c4
Showing 1 changed file with 141 additions and 0 deletions.
141 changes: 141 additions & 0 deletions decoder/expr_map_ref_targets_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}

0 comments on commit 4ecb4c4

Please sign in to comment.