MCPcopy
hub / github.com/hashicorp/hcl / TestExpressionAsTraversal

Function TestExpressionAsTraversal

hclsyntax/expression_test.go:2885–2918  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

2883}
2884
2885func TestExpressionAsTraversal(t *testing.T) {
2886 expr, _ := ParseExpression([]byte("a.b[0][\"c\"]"), "", hcl.Pos{})
2887 traversal, diags := hcl.AbsTraversalForExpr(expr)
2888 if len(diags) != 0 {
2889 t.Fatalf("unexpected diagnostics:\n%s", diags.Error())
2890 }
2891 if len(traversal) != 4 {
2892 t.Fatalf("wrong traversal %#v; want length 3", traversal)
2893 }
2894 if traversal.RootName() != "a" {
2895 t.Errorf("wrong root name %q; want %q", traversal.RootName(), "a")
2896 }
2897 if step, ok := traversal[1].(hcl.TraverseAttr); ok {
2898 if got, want := step.Name, "b"; got != want {
2899 t.Errorf("wrong name %q for step 1; want %q", got, want)
2900 }
2901 } else {
2902 t.Errorf("wrong type %T for step 1; want %T", traversal[1], step)
2903 }
2904 if step, ok := traversal[2].(hcl.TraverseIndex); ok {
2905 if got, want := step.Key, cty.Zero; !want.RawEquals(got) {
2906 t.Errorf("wrong name %#v for step 2; want %#v", got, want)
2907 }
2908 } else {
2909 t.Errorf("wrong type %T for step 2; want %T", traversal[2], step)
2910 }
2911 if step, ok := traversal[3].(hcl.TraverseIndex); ok {
2912 if got, want := step.Key, cty.StringVal("c"); !want.RawEquals(got) {
2913 t.Errorf("wrong name %#v for step 3; want %#v", got, want)
2914 }
2915 } else {
2916 t.Errorf("wrong type %T for step 3; want %T", traversal[3], step)
2917 }
2918}
2919
2920func TestStaticExpressionList(t *testing.T) {
2921 expr, _ := ParseExpression([]byte("[0, a, true]"), "", hcl.Pos{})

Callers

nothing calls this directly

Calls 3

ParseExpressionFunction · 0.70
ErrorMethod · 0.45
RootNameMethod · 0.45

Tested by

no test coverage detected