-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
decoder: Add tests for reference targets (Map)
- Loading branch information
1 parent
d58248a
commit 4ecb4c4
Showing
1 changed file
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |