forked from hashicorp/hcl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ops_test.go
157 lines (149 loc) · 3.29 KB
/
ops_test.go
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package hcl
import (
"fmt"
"testing"
"github.com/zclconf/go-cty/cty"
)
func TestApplyPath(t *testing.T) {
tests := []struct {
Start cty.Value
Path cty.Path
Want cty.Value
WantErr string
}{
{
cty.StringVal("hello"),
nil,
cty.StringVal("hello"),
``,
},
{
cty.StringVal("hello"),
(cty.Path)(nil).Index(cty.StringVal("boop")),
cty.NilVal,
`Invalid index`,
},
{
cty.StringVal("hello"),
(cty.Path)(nil).Index(cty.NumberIntVal(0)),
cty.NilVal,
`Invalid index`,
},
{
cty.ListVal([]cty.Value{
cty.StringVal("hello"),
}),
(cty.Path)(nil).Index(cty.NumberIntVal(0)),
cty.StringVal("hello"),
``,
},
{
cty.ListVal([]cty.Value{
cty.StringVal("hello"),
}).Mark("x"),
(cty.Path)(nil).Index(cty.NumberIntVal(0)),
cty.StringVal("hello").Mark("x"),
``,
},
{
cty.TupleVal([]cty.Value{
cty.StringVal("hello"),
}),
(cty.Path)(nil).Index(cty.NumberIntVal(0)),
cty.StringVal("hello"),
``,
},
{
cty.MapVal(map[string]cty.Value{
"a": cty.StringVal("foo").Mark("x"),
"b": cty.StringVal("bar").Mark("x"),
}).Mark("x"),
cty.GetAttrPath("a"),
cty.StringVal("foo").Mark("x"),
``,
},
{
cty.ListValEmpty(cty.String),
(cty.Path)(nil).Index(cty.NumberIntVal(0)),
cty.NilVal,
`Invalid index`,
},
{
cty.ListVal([]cty.Value{
cty.StringVal("hello"),
}),
(cty.Path)(nil).Index(cty.NumberIntVal(1)),
cty.NilVal,
`Invalid index`,
},
{
cty.ListVal([]cty.Value{
cty.StringVal("hello"),
}),
(cty.Path)(nil).Index(cty.NumberFloatVal(0.5)),
cty.NilVal,
`Invalid index`,
},
{
cty.ListVal([]cty.Value{
cty.StringVal("hello"),
}),
(cty.Path)(nil).Index(cty.NumberIntVal(0)).GetAttr("foo"),
cty.NilVal,
`Unsupported attribute`,
},
{
cty.ListVal([]cty.Value{
cty.EmptyObjectVal,
}),
(cty.Path)(nil).Index(cty.NumberIntVal(0)).GetAttr("foo"),
cty.NilVal,
`Unsupported attribute`,
},
{
cty.NullVal(cty.List(cty.String)),
(cty.Path)(nil).Index(cty.NumberIntVal(0)),
cty.NilVal,
`Attempt to index null value`,
},
{
cty.NullVal(cty.Map(cty.String)),
(cty.Path)(nil).Index(cty.NumberIntVal(0)),
cty.NilVal,
`Attempt to index null value`,
},
{
cty.NullVal(cty.EmptyObject),
(cty.Path)(nil).GetAttr("foo"),
cty.NilVal,
`Attempt to get attribute from null value`,
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("%#v %#v", test.Start, test.Path), func(t *testing.T) {
got, diags := ApplyPath(test.Start, test.Path, nil)
t.Logf("testing ApplyPath\nstart: %#v\npath: %#v", test.Start, test.Path)
for _, diag := range diags {
t.Logf(diag.Error())
}
if test.WantErr != "" {
if !diags.HasErrors() {
t.Fatalf("succeeded, but want error\nwant error: %s", test.WantErr)
}
if len(diags) != 1 {
t.Fatalf("wrong number of diagnostics %d; want 1", len(diags))
}
if gotErrStr := diags[0].Summary; gotErrStr != test.WantErr {
t.Fatalf("wrong error\ngot error: %s\nwant error: %s", gotErrStr, test.WantErr)
}
return
}
if diags.HasErrors() {
t.Fatalf("failed, but want success\ngot diagnostics:\n%s", diags.Error())
}
if !test.Want.RawEquals(got) {
t.Fatalf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
}
})
}
}