summaryrefslogtreecommitdiff
path: root/traversal_for_expr_test.go
diff options
context:
space:
mode:
authorMartin Atkins <mart@degeneration.co.uk>2019-09-09 16:08:19 -0700
committerMartin Atkins <mart@degeneration.co.uk>2019-09-09 16:08:19 -0700
commit6c4344623b6ac528a57f9b80e4622acfab2fde40 (patch)
tree83031084d3ab54abbe40e3fd749e439c8b28e9d1 /traversal_for_expr_test.go
parent0f5ab3bd563c111077917020c0cbc8c211e1bff3 (diff)
Unfold the "hcl" directory up into the root
The main HCL package is more visible this way, and so it's easier than having to pick it out from dozens of other package directories.
Diffstat (limited to 'traversal_for_expr_test.go')
-rw-r--r--traversal_for_expr_test.go218
1 files changed, 218 insertions, 0 deletions
diff --git a/traversal_for_expr_test.go b/traversal_for_expr_test.go
new file mode 100644
index 0000000..9a9539f
--- /dev/null
+++ b/traversal_for_expr_test.go
@@ -0,0 +1,218 @@
+package hcl
+
+import (
+ "testing"
+)
+
+type asTraversalSupported struct {
+ staticExpr
+ RootName string
+}
+
+type asTraversalSupportedAttr struct {
+ staticExpr
+ RootName string
+ AttrName string
+}
+
+type asTraversalNotSupported struct {
+ staticExpr
+}
+
+type asTraversalDeclined struct {
+ staticExpr
+}
+
+type asTraversalWrappedDelegated struct {
+ original Expression
+ staticExpr
+}
+
+func (e asTraversalSupported) AsTraversal() Traversal {
+ return Traversal{
+ TraverseRoot{
+ Name: e.RootName,
+ },
+ }
+}
+
+func (e asTraversalSupportedAttr) AsTraversal() Traversal {
+ return Traversal{
+ TraverseRoot{
+ Name: e.RootName,
+ },
+ TraverseAttr{
+ Name: e.AttrName,
+ },
+ }
+}
+
+func (e asTraversalDeclined) AsTraversal() Traversal {
+ return nil
+}
+
+func (e asTraversalWrappedDelegated) UnwrapExpression() Expression {
+ return e.original
+}
+
+func TestAbsTraversalForExpr(t *testing.T) {
+ tests := []struct {
+ Expr Expression
+ WantRootName string
+ }{
+ {
+ asTraversalSupported{RootName: "foo"},
+ "foo",
+ },
+ {
+ asTraversalNotSupported{},
+ "",
+ },
+ {
+ asTraversalDeclined{},
+ "",
+ },
+ {
+ asTraversalWrappedDelegated{
+ original: asTraversalSupported{RootName: "foo"},
+ },
+ "foo",
+ },
+ {
+ asTraversalWrappedDelegated{
+ original: asTraversalWrappedDelegated{
+ original: asTraversalSupported{RootName: "foo"},
+ },
+ },
+ "foo",
+ },
+ }
+
+ for _, test := range tests {
+ t.Run("", func(t *testing.T) {
+ got, diags := AbsTraversalForExpr(test.Expr)
+ switch {
+ case got != nil:
+ if test.WantRootName == "" {
+ t.Fatalf("traversal was returned; want error")
+ }
+ if len(got) != 1 {
+ t.Fatalf("wrong traversal length %d; want 1", len(got))
+ }
+ gotRoot, ok := got[0].(TraverseRoot)
+ if !ok {
+ t.Fatalf("first traversal step is %T; want hcl.TraverseRoot", got[0])
+ }
+ if gotRoot.Name != test.WantRootName {
+ t.Errorf("wrong root name %q; want %q", gotRoot.Name, test.WantRootName)
+ }
+ default:
+ if !diags.HasErrors() {
+ t.Errorf("returned nil traversal without error diagnostics")
+ }
+ if test.WantRootName != "" {
+ t.Errorf("traversal was not returned; want TraverseRoot(%q)", test.WantRootName)
+ }
+ }
+ })
+ }
+}
+
+func TestRelTraversalForExpr(t *testing.T) {
+ tests := []struct {
+ Expr Expression
+ WantFirstName string
+ }{
+ {
+ asTraversalSupported{RootName: "foo"},
+ "foo",
+ },
+ {
+ asTraversalNotSupported{},
+ "",
+ },
+ {
+ asTraversalDeclined{},
+ "",
+ },
+ }
+
+ for _, test := range tests {
+ t.Run("", func(t *testing.T) {
+ got, diags := RelTraversalForExpr(test.Expr)
+ switch {
+ case got != nil:
+ if test.WantFirstName == "" {
+ t.Fatalf("traversal was returned; want error")
+ }
+ if len(got) != 1 {
+ t.Fatalf("wrong traversal length %d; want 1", len(got))
+ }
+ gotRoot, ok := got[0].(TraverseAttr)
+ if !ok {
+ t.Fatalf("first traversal step is %T; want hcl.TraverseAttr", got[0])
+ }
+ if gotRoot.Name != test.WantFirstName {
+ t.Errorf("wrong root name %q; want %q", gotRoot.Name, test.WantFirstName)
+ }
+ default:
+ if !diags.HasErrors() {
+ t.Errorf("returned nil traversal without error diagnostics")
+ }
+ if test.WantFirstName != "" {
+ t.Errorf("traversal was not returned; want TraverseAttr(%q)", test.WantFirstName)
+ }
+ }
+ })
+ }
+}
+
+func TestExprAsKeyword(t *testing.T) {
+ tests := []struct {
+ Expr Expression
+ Want string
+ }{
+ {
+ asTraversalSupported{RootName: "foo"},
+ "foo",
+ },
+ {
+ asTraversalSupportedAttr{
+ RootName: "foo",
+ AttrName: "bar",
+ },
+ "",
+ },
+ {
+ asTraversalNotSupported{},
+ "",
+ },
+ {
+ asTraversalDeclined{},
+ "",
+ },
+ {
+ asTraversalWrappedDelegated{
+ original: asTraversalSupported{RootName: "foo"},
+ },
+ "foo",
+ },
+ {
+ asTraversalWrappedDelegated{
+ original: asTraversalWrappedDelegated{
+ original: asTraversalSupported{RootName: "foo"},
+ },
+ },
+ "foo",
+ },
+ }
+
+ for _, test := range tests {
+ t.Run("", func(t *testing.T) {
+ got := ExprAsKeyword(test.Expr)
+ if got != test.Want {
+ t.Errorf("wrong result %q; want %q\ninput: %T", got, test.Want, test.Expr)
+ }
+ })
+ }
+}