-
Notifications
You must be signed in to change notification settings - Fork 602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add additional function for parsing traversals with [*] keys #673
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,11 +4,13 @@ | |
package hclsyntax | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/go-test/deep" | ||
"github.com/hashicorp/hcl/v2" | ||
"github.com/zclconf/go-cty/cty" | ||
|
||
"github.com/hashicorp/hcl/v2" | ||
) | ||
|
||
func TestParseTraversalAbs(t *testing.T) { | ||
|
@@ -208,10 +210,61 @@ func TestParseTraversalAbs(t *testing.T) { | |
}, | ||
1, // extra junk after traversal | ||
}, | ||
|
||
{ | ||
"foo[*]", | ||
hcl.Traversal{ | ||
hcl.TraverseRoot{ | ||
Name: "foo", | ||
SrcRange: hcl.Range{ | ||
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, | ||
End: hcl.Pos{Line: 1, Column: 4, Byte: 3}, | ||
}, | ||
}, | ||
hcl.TraverseSplat{ | ||
SrcRange: hcl.Range{ | ||
Start: hcl.Pos{Line: 1, Column: 4, Byte: 3}, | ||
End: hcl.Pos{Line: 1, Column: 7, Byte: 6}, | ||
}, | ||
}, | ||
}, | ||
0, | ||
}, | ||
{ | ||
"foo.*", // Still not supporting this. | ||
hcl.Traversal{ | ||
hcl.TraverseRoot{ | ||
Name: "foo", | ||
SrcRange: hcl.Range{ | ||
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, | ||
End: hcl.Pos{Line: 1, Column: 4, Byte: 3}, | ||
}, | ||
}, | ||
}, | ||
1, | ||
}, | ||
{ | ||
"foo[*].bar", // Run this through the unsupported function. | ||
hcl.Traversal{ | ||
hcl.TraverseRoot{ | ||
Name: "foo", | ||
SrcRange: hcl.Range{ | ||
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, | ||
End: hcl.Pos{Line: 1, Column: 4, Byte: 3}, | ||
}, | ||
}, | ||
}, | ||
1, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.src, func(t *testing.T) { | ||
if test.src == "foo[*]" { | ||
// Skip the test that introduces splat syntax. | ||
t.Skip("skipping test for unsupported splat syntax") | ||
} | ||
|
||
got, diags := ParseTraversalAbs([]byte(test.src), "", hcl.Pos{Line: 1, Column: 1}) | ||
if len(diags) != test.diagCount { | ||
for _, diag := range diags { | ||
|
@@ -226,5 +279,26 @@ func TestParseTraversalAbs(t *testing.T) { | |
} | ||
} | ||
}) | ||
|
||
t.Run(fmt.Sprintf("partial_%s", test.src), func(t *testing.T) { | ||
if test.src == "foo[*].bar" { | ||
// Skip the test that's supposed to fail for splat syntax. | ||
t.Skip("skipping test for unsupported splat syntax") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't we test for this to error? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, this is a copy-paste error which makes the message wrong. This test case is expecting an error, which is not what we want here. So I skip this test case in the test branch that does support splats, otherwise the test will fail expecting an error when there isn't one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll fix this shortly! |
||
} | ||
|
||
got, diags := ParseTraversalPartial([]byte(test.src), "", hcl.Pos{Line: 1, Column: 1}) | ||
if len(diags) != test.diagCount { | ||
for _, diag := range diags { | ||
t.Logf(" - %s", diag.Error()) | ||
} | ||
t.Errorf("wrong number of diagnostics %d; want %d", len(diags), test.diagCount) | ||
} | ||
|
||
if diff := deep.Equal(got, test.want); diff != nil { | ||
for _, problem := range diff { | ||
t.Error(problem) | ||
} | ||
} | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do I understand this right that we skip it here since we use
ParseTraversalAbs
in which case this is unsupported, but include it in the partial_ test cases sinceParseTraversalPartial
supports it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, exactly. Basically, I wanted to make sure that using the splat was still broken in the old implementation but worked in the new one. I figured the easiest way to do that was to just manually skip the relevant tests since there's only one of each branch that needs to be skipped.
I could complicate the whole function by adding in some additional metadata to the test cases, but thought it was overkill just for one case.