File: expression_static_test.go

package info (click to toggle)
golang-github-hashicorp-hcl-v2 2.14.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 3,120 kB
  • sloc: ruby: 205; makefile: 72; python: 43; sh: 11
file content (131 lines) | stat: -rw-r--r-- 3,088 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package hclsyntax

import (
	"testing"

	"github.com/go-test/deep"
	"github.com/hashicorp/hcl/v2"
	"github.com/zclconf/go-cty/cty"
)

func TestTraversalStatic(t *testing.T) {
	expr, diags := ParseExpression([]byte(`a.b.c`), "", hcl.Pos{Line: 1, Column: 1})
	got, moreDiags := hcl.AbsTraversalForExpr(expr)
	diags = append(diags, moreDiags...)

	if len(diags) != 0 {
		t.Errorf("wrong number of diags %d; want 0", len(diags))
		for _, diag := range diags {
			t.Logf("- %s", diag)
		}
		return
	}

	want := hcl.Traversal{
		hcl.TraverseRoot{
			Name: "a",
			SrcRange: hcl.Range{
				Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
				End:   hcl.Pos{Line: 1, Column: 2, Byte: 1},
			},
		},
		hcl.TraverseAttr{
			Name: "b",
			SrcRange: hcl.Range{
				Start: hcl.Pos{Line: 1, Column: 2, Byte: 1},
				End:   hcl.Pos{Line: 1, Column: 4, Byte: 3},
			},
		},
		hcl.TraverseAttr{
			Name: "c",
			SrcRange: hcl.Range{
				Start: hcl.Pos{Line: 1, Column: 4, Byte: 3},
				End:   hcl.Pos{Line: 1, Column: 6, Byte: 5},
			},
		},
	}

	for _, problem := range deep.Equal(got, want) {
		t.Errorf(problem)
	}
}

func TestTupleStatic(t *testing.T) {
	expr, diags := ParseExpression([]byte(`[true, false]`), "", hcl.Pos{Line: 1, Column: 1})
	exprs, moreDiags := hcl.ExprList(expr)
	diags = append(diags, moreDiags...)
	if len(diags) != 0 {
		t.Errorf("wrong number of diags %d; want 0", len(diags))
		for _, diag := range diags {
			t.Logf("- %s", diag)
		}
		return
	}

	if got, want := len(exprs), 2; got != want {
		t.Fatalf("wrong length %d; want %d", got, want)
	}

	got := make([]cty.Value, len(exprs))
	want := []cty.Value{
		cty.True,
		cty.False,
	}
	for i, itemExpr := range exprs {
		val, valDiags := itemExpr.Value(nil)
		if len(valDiags) != 0 {
			t.Errorf("wrong number of diags %d; want 0", len(valDiags))
			for _, diag := range valDiags {
				t.Logf("- %s", diag)
			}
			return
		}
		got[i] = val
	}

	for _, problem := range deep.Equal(got, want) {
		t.Errorf(problem)
	}
}

func TestMapStatic(t *testing.T) {
	expr, diags := ParseExpression([]byte(`{"foo":true,"bar":false}`), "", hcl.Pos{Line: 1, Column: 1})
	items, moreDiags := hcl.ExprMap(expr)
	diags = append(diags, moreDiags...)
	if len(diags) != 0 {
		t.Errorf("wrong number of diags %d; want 0", len(diags))
		for _, diag := range diags {
			t.Logf("- %s", diag)
		}
		return
	}

	if got, want := len(items), 2; got != want {
		t.Fatalf("wrong length %d; want %d", got, want)
	}

	got := make(map[cty.Value]cty.Value)
	want := map[cty.Value]cty.Value{
		cty.StringVal("foo"): cty.True,
		cty.StringVal("bar"): cty.False,
	}
	for _, item := range items {
		var itemDiags hcl.Diagnostics
		key, keyDiags := item.Key.Value(nil)
		itemDiags = append(itemDiags, keyDiags...)
		val, valDiags := item.Value.Value(nil)
		itemDiags = append(itemDiags, valDiags...)
		if len(itemDiags) != 0 {
			t.Errorf("wrong number of diags %d; want 0", len(itemDiags))
			for _, diag := range itemDiags {
				t.Logf("- %s", diag)
			}
			return
		}
		got[key] = val
	}

	for _, problem := range deep.Equal(got, want) {
		t.Errorf(problem)
	}
}