-
Notifications
You must be signed in to change notification settings - Fork 12
/
schema_test.go
194 lines (163 loc) · 3.88 KB
/
schema_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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package thema
import (
"fmt"
"reflect"
"testing"
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/cue/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var linstr = `name: "single"
schemas: [{
version: [0, 0]
schema: {
astring?: string
anint: int64 | *42
abool: bool
}
examples: {
simple: {
astring: "some string"
anint: 42
abool: true
}
}
}]
`
type TestType struct {
Astring *string `json:"astring"`
Anint int64 `json:"anint"`
Abool bool `json:"abool"`
}
func testLin(linstr string) Lineage {
rt := NewRuntime(cuecontext.New())
val := rt.Context().CompileString(linstr)
lin, err := BindLineage(val, rt)
if err != nil {
panic(err)
}
return lin
}
func ptr[T any](t T) *T {
return &t
}
func TestBindType(t *testing.T) {
lin := testLin(linstr)
tt := &TestType{Astring: ptr("init"), Anint: 10}
ts, err := BindType[*TestType](lin.First(), tt)
if err != nil {
t.Fatal(errors.Details(err, nil))
}
nt1 := ts.NewT()
if nt1.Astring != nil || nt1.Anint == 10 {
t.Fatalf("values set on parameter to BindType showed up on NewT(): %v", nt1)
}
if nt1.Anint != 42 {
t.Fatalf("expected schema-specified default of 42 for nt1.Anint, got %v", nt1.Anint)
}
// Now, ensure values set on returned type don't leak into next return from NewT()
nt1.Astring = ptr("nt1")
nt1.Anint = 10
nt2 := ts.NewT()
if nt2.Astring != nil || nt2.Anint == 10 {
t.Fatalf("values from nt1 leaked into nt2: %v", nt2)
}
if nt2.Anint != 42 {
t.Fatalf("expected schema-specified default of 42 for nt2.Anint, got %v", nt2.Anint)
}
}
func TestSchema_Examples(t *testing.T) {
t.Run("withExamples", func(t *testing.T) {
lin := testLin(linstr)
sch := lin.First()
examples := sch.Examples()
require.NotNil(t, examples)
// There must be a "simple" example based on
// the definition above (beginning of file).
require.NotEmpty(t, examples)
require.NotNil(t, examples["simple"])
ts, err := BindType[*TestType](sch, &TestType{})
require.NoError(t, err)
tinst, err := ts.ValidateTyped(examples["simple"].Underlying())
require.NoError(t, err)
got, err := tinst.Value()
require.NoError(t, err)
expected := &TestType{Astring: ptr("some string"), Anint: 42, Abool: true}
assert.Equal(t, expected, got)
})
t.Run("withoutExamples", func(t *testing.T) {
linStrNoExamples := `name: "single"
schemas: [{
version: [0, 0]
schema: {
astring?: string
anint: int64 | *42
abool: bool
}
}]
`
lin := testLin(linStrNoExamples)
sch := lin.First()
examples := sch.Examples()
require.NotNil(t, examples)
})
t.Run("withEmptyExamples", func(t *testing.T) {
linStrNoExamples := `name: "single"
schemas: [{
version: [0, 0]
schema: {
astring?: string
anint: int64 | *42
abool: bool
}
examples: {}
}]
`
lin := testLin(linStrNoExamples)
sch := lin.First()
examples := sch.Examples()
require.NotNil(t, examples)
})
}
// scratch test, preserved only as a simpler sandbox for future playing with pointers, generics, reflect
func testPointerNewVar(t *testing.T) {
type Foo struct {
V int
}
mk1 := mkNew(Foo{V: 42})
mk1v := mk1()
if mk1v.V == 42 {
t.Fatal("zero value should be zero")
}
mk1v.V = 43
if mk1().V == 43 {
t.Fatal("setting value of return should not influence future values")
}
base2 := &Foo{V: 42}
mk2 := mkNew(base2)
mk2v := mk2()
if mk2v.V == 42 {
t.Fatal("zero value should be zero")
}
mk2v.V = 43
if mk2().V == 43 {
t.Fatal("setting value of return should not influence future values")
}
}
func mkNew[T any](t T) func() T {
fmt.Printf("%T %v || ", t, t)
if reflect.ValueOf(t).Kind() == reflect.Pointer {
zt := reflect.ValueOf(t).Elem().Type()
fmt.Printf("%T %v\n", zt, zt)
return func() T {
return reflect.New(zt).Interface().(T)
}
} else {
zt := reflect.Zero(reflect.TypeOf(t)).Interface().(T)
fmt.Printf("%T %v\n", zt, zt)
return func() T {
return zt
}
}
}