Skip to content

Commit 504d167

Browse files
committed
Schema: Refine Examples() tests
1 parent 7afabfd commit 504d167

File tree

2 files changed

+59
-27
lines changed

2 files changed

+59
-27
lines changed

schema.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (sch *schemaDef) Examples() map[string]*Instance {
4646

4747
examples := make(map[string]*Instance)
4848
for it.Next() {
49-
label := it.Label()
49+
label := it.Selector().String()
5050
examples[label] = &Instance{
5151
raw: it.Value(),
5252
name: label,

schema_test.go

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,7 @@ type TestType struct {
3636
Abool bool `json:"abool"`
3737
}
3838

39-
type TestType2 struct {
40-
Astring string `json:"astring"`
41-
Anint int64 `json:"anint"`
42-
Abool bool `json:"abool"`
43-
}
44-
45-
func testLin() Lineage {
39+
func testLin(linstr string) Lineage {
4640
rt := NewRuntime(cuecontext.New())
4741
val := rt.Context().CompileString(linstr)
4842
lin, err := BindLineage(val, rt)
@@ -57,7 +51,7 @@ func ptr[T any](t T) *T {
5751
}
5852

5953
func TestBindType(t *testing.T) {
60-
lin := testLin()
54+
lin := testLin(linstr)
6155

6256
tt := &TestType{Astring: ptr("init"), Anint: 10}
6357
ts, err := BindType[*TestType](lin.First(), tt)
@@ -87,30 +81,68 @@ func TestBindType(t *testing.T) {
8781
}
8882

8983
func TestSchema_Examples(t *testing.T) {
90-
lin := testLin()
84+
t.Run("withExamples", func(t *testing.T) {
85+
lin := testLin(linstr)
86+
87+
sch := lin.First()
88+
examples := sch.Examples()
89+
require.NotNil(t, examples)
90+
91+
// There must be a "simple" example based on
92+
// the definition above (beginning of file).
93+
require.NotEmpty(t, examples)
94+
require.NotNil(t, examples["simple"])
9195

92-
sch := lin.First()
93-
examples := sch.Examples()
94-
require.NotNil(t, examples)
96+
ts, err := BindType[*TestType](sch, &TestType{})
97+
require.NoError(t, err)
9598

96-
// There must be a "simple" example based on
97-
// the definition above (beginning of file).
98-
require.NotEmpty(t, examples)
99-
require.NotNil(t, examples["simple"])
99+
tinst, err := ts.ValidateTyped(examples["simple"].Underlying())
100+
require.NoError(t, err)
100101

101-
tt := &TestType{}
102-
ts, err := BindType[*TestType](sch, tt)
103-
require.NoError(t, err)
102+
got, err := tinst.Value()
103+
require.NoError(t, err)
104104

105-
tinst, err := ts.ValidateTyped(examples["simple"].Underlying())
106-
require.NoError(t, err)
105+
expected := &TestType{Astring: ptr("some string"), Anint: 42, Abool: true}
106+
assert.Equal(t, expected, got)
107+
})
107108

108-
val, err := tinst.Value()
109-
require.NoError(t, err)
109+
t.Run("withoutExamples", func(t *testing.T) {
110+
linStrNoExamples := `name: "single"
111+
schemas: [{
112+
version: [0, 0]
113+
schema: {
114+
astring?: string
115+
anint: int64 | *42
116+
abool: bool
117+
}
118+
}]
119+
`
120+
121+
lin := testLin(linStrNoExamples)
122+
123+
sch := lin.First()
124+
examples := sch.Examples()
125+
require.NotNil(t, examples)
126+
})
127+
128+
t.Run("withEmptyExamples", func(t *testing.T) {
129+
linStrNoExamples := `name: "single"
130+
schemas: [{
131+
version: [0, 0]
132+
schema: {
133+
astring?: string
134+
anint: int64 | *42
135+
abool: bool
136+
}
137+
examples: {}
138+
}]
139+
`
140+
lin := testLin(linStrNoExamples)
110141

111-
assert.Equal(t, "some string", *val.Astring)
112-
assert.Equal(t, int64(42), val.Anint)
113-
assert.Equal(t, true, val.Abool)
142+
sch := lin.First()
143+
examples := sch.Examples()
144+
require.NotNil(t, examples)
145+
})
114146
}
115147

116148
// scratch test, preserved only as a simpler sandbox for future playing with pointers, generics, reflect

0 commit comments

Comments
 (0)