Skip to content

Commit 0253ab1

Browse files
denikIsaac
andcommitted
structvar: add the YAML test helpers
Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 7e07b9e commit 0253ab1

1 file changed

Lines changed: 159 additions & 0 deletions

File tree

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package structvar_test
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"os"
7+
"testing"
8+
"time"
9+
10+
"github.com/databricks/cli/libs/diag"
11+
"github.com/databricks/cli/libs/structs/structvar"
12+
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
14+
"go.yaml.in/yaml/v3"
15+
)
16+
17+
// ev is an expected value with the locations it is expected to have.
18+
// val is a scalar, a []ev or a map[string]ev.
19+
type ev struct {
20+
val any
21+
locs []diag.Location
22+
}
23+
24+
func nv(val any, locs []diag.Location) ev {
25+
return ev{val: val, locs: locs}
26+
}
27+
28+
// plain converts the expected value to the plain Go value the decoder produces.
29+
func (e ev) plain() any {
30+
switch v := e.val.(type) {
31+
case []ev:
32+
out := make([]any, len(v))
33+
for i, x := range v {
34+
out[i] = x.plain()
35+
}
36+
return out
37+
case map[string]ev:
38+
out := make(map[string]any, len(v))
39+
for k, x := range v {
40+
out[k] = x.plain()
41+
}
42+
return out
43+
default:
44+
return v
45+
}
46+
}
47+
48+
// assertLocations checks the locations of every value in the expected tree.
49+
func assertLocations(t *testing.T, l *structvar.Locations, e ev, where string) {
50+
assert.Equal(t, e.locs, l.Get(), "locations of %s", where)
51+
switch v := e.val.(type) {
52+
case []ev:
53+
for i, x := range v {
54+
assertLocations(t, l.Index(i), x, fmt.Sprintf("%s[%d]", where, i))
55+
}
56+
case map[string]ev:
57+
for k, x := range v {
58+
assertLocations(t, l.Key(k), x, where+"."+k)
59+
}
60+
}
61+
}
62+
63+
func decodeFile(t *testing.T, file string) (any, *structvar.StructVar, error) {
64+
input, err := os.ReadFile(file)
65+
require.NoError(t, err)
66+
var v any
67+
sv, _, err := structvar.DecodeYAML(file, bytes.NewBuffer(input), &v)
68+
return v, sv, err
69+
}
70+
71+
func loadExample(t *testing.T, file string) (any, *structvar.StructVar) {
72+
v, sv, err := decodeFile(t, file)
73+
require.NoError(t, err)
74+
return v, sv
75+
}
76+
77+
// assertDecoded checks the decoded value and the locations of all its parts.
78+
func assertDecoded(t *testing.T, expected ev, v any, sv *structvar.StructVar) {
79+
assert.Equal(t, expected.plain(), v)
80+
assertLocations(t, sv.Locations, expected, "root")
81+
}
82+
83+
// equalToReference compares the decoded value to the one decoded by yaml.v3.
84+
// Timestamps are kept as the string they were written as, so only their presence is compared.
85+
func equalToReference(t *testing.T, ref, v any) {
86+
switch r := ref.(type) {
87+
case time.Time:
88+
assert.IsType(t, "", v)
89+
case map[string]any:
90+
got, ok := v.(map[string]any)
91+
require.True(t, ok, "expected a map, got %T", v)
92+
require.Len(t, got, len(r))
93+
for k, x := range r {
94+
equalToReference(t, x, got[k])
95+
}
96+
case []any:
97+
got, ok := v.([]any)
98+
require.True(t, ok, "expected a sequence, got %T", v)
99+
require.Len(t, got, len(r))
100+
for i, x := range r {
101+
equalToReference(t, x, got[i])
102+
}
103+
default:
104+
assert.EqualValues(t, ref, v)
105+
}
106+
}
107+
108+
// loadYAML decodes the file and checks that the result matches yaml.v3.
109+
func loadYAML(t *testing.T, path string) (any, *structvar.StructVar) {
110+
input, err := os.ReadFile(path)
111+
require.NoError(t, err)
112+
113+
var ref any
114+
err = yaml.Unmarshal(input, &ref)
115+
require.NoError(t, err)
116+
117+
v, sv := loadExample(t, path)
118+
require.NotNil(t, sv)
119+
120+
equalToReference(t, ref, v)
121+
return v, sv
122+
}
123+
124+
// at returns the value at the path of keys (strings) and indexes (ints).
125+
func at(t *testing.T, v any, path ...any) any {
126+
for _, p := range path {
127+
switch p := p.(type) {
128+
case string:
129+
m, ok := v.(map[string]any)
130+
require.True(t, ok, "expected a map at %q, got %T", p, v)
131+
v = m[p]
132+
case int:
133+
s, ok := v.([]any)
134+
require.True(t, ok, "expected a sequence at %d, got %T", p, v)
135+
v = s[p]
136+
}
137+
}
138+
return v
139+
}
140+
141+
// locsAt returns the locations of the value at the path of keys (strings) and indexes (ints).
142+
func locsAt(sv *structvar.StructVar, path ...any) []diag.Location {
143+
l := sv.Locations
144+
for _, p := range path {
145+
switch p := p.(type) {
146+
case string:
147+
l = l.Key(p)
148+
case int:
149+
l = l.Index(p)
150+
}
151+
}
152+
return l.Get()
153+
}
154+
155+
func TestYAMLEmpty(t *testing.T) {
156+
v, sv := loadYAML(t, "testdata/yaml/empty.yml")
157+
assert.Nil(t, v)
158+
assert.Empty(t, sv.Locations.Get())
159+
}

0 commit comments

Comments
 (0)