-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema_type_test.go
More file actions
357 lines (319 loc) · 8.79 KB
/
schema_type_test.go
File metadata and controls
357 lines (319 loc) · 8.79 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package openapi
import (
"encoding/json"
"testing"
"gopkg.in/yaml.v3"
)
func TestSchemaType_NewSchemaType(t *testing.T) {
st := NewSchemaType("string")
if st.String() != "string" {
t.Errorf("expected 'string', got %q", st.String())
}
if st.IsArray() {
t.Error("expected IsArray to be false")
}
}
func TestSchemaType_NewSchemaTypes_Single(t *testing.T) {
st := NewSchemaTypes([]string{"string"})
if st.String() != "string" {
t.Errorf("expected 'string', got %q", st.String())
}
if st.IsArray() {
t.Error("single element should not be array")
}
}
func TestSchemaType_NewSchemaTypes_Multiple(t *testing.T) {
st := NewSchemaTypes([]string{"string", "null"})
if !st.IsArray() {
t.Error("expected IsArray to be true")
}
if st.String() != "string" {
t.Errorf("expected first element 'string', got %q", st.String())
}
types := st.Strings()
if len(types) != 2 {
t.Errorf("expected 2 types, got %d", len(types))
}
}
func TestSchemaType_IsEmpty(t *testing.T) {
var empty SchemaType
if !empty.IsEmpty() {
t.Error("zero value should be empty")
}
st := NewSchemaType("")
if !st.IsEmpty() {
t.Error("empty string should be empty")
}
st = NewSchemaType("string")
if st.IsEmpty() {
t.Error("non-empty string should not be empty")
}
}
func TestSchemaType_Contains(t *testing.T) {
st := NewSchemaTypes([]string{"string", "null"})
if !st.Contains("string") {
t.Error("should contain 'string'")
}
if !st.Contains("null") {
t.Error("should contain 'null'")
}
if st.Contains("integer") {
t.Error("should not contain 'integer'")
}
}
func TestSchemaType_IsNullable(t *testing.T) {
st := NewSchemaType("string")
if st.IsNullable() {
t.Error("string type should not be nullable")
}
st = NewSchemaTypes([]string{"string", "null"})
if !st.IsNullable() {
t.Error("type with 'null' should be nullable")
}
}
func TestSchemaType_Strings_Single(t *testing.T) {
st := NewSchemaType("integer")
types := st.Strings()
if len(types) != 1 || types[0] != "integer" {
t.Errorf("expected ['integer'], got %v", types)
}
}
func TestSchemaType_Strings_Empty(t *testing.T) {
var empty SchemaType
types := empty.Strings()
if types != nil {
t.Errorf("expected nil, got %v", types)
}
}
func TestSchemaType_Strings_EmptyString(t *testing.T) {
st := NewSchemaType("")
types := st.Strings()
if types != nil {
t.Errorf("expected nil for empty string, got %v", types)
}
}
func TestSchemaType_String_EmptySlice(t *testing.T) {
st := NewSchemaTypes([]string{})
if st.String() != "" {
t.Errorf("expected empty string, got %q", st.String())
}
}
func TestSchemaType_MarshalJSON_String(t *testing.T) {
st := NewSchemaType("string")
data, err := json.Marshal(st)
if err != nil {
t.Fatalf("marshal error: %v", err)
}
if string(data) != `"string"` {
t.Errorf("expected '\"string\"', got %s", data)
}
}
func TestSchemaType_MarshalJSON_Array(t *testing.T) {
st := NewSchemaTypes([]string{"string", "null"})
data, err := json.Marshal(st)
if err != nil {
t.Fatalf("marshal error: %v", err)
}
if string(data) != `["string","null"]` {
t.Errorf("expected '[\"string\",\"null\"]', got %s", data)
}
}
func TestSchemaType_MarshalJSON_Empty(t *testing.T) {
var empty SchemaType
data, err := json.Marshal(empty)
if err != nil {
t.Fatalf("marshal error: %v", err)
}
if string(data) != "null" {
t.Errorf("expected 'null', got %s", data)
}
}
func TestSchemaType_UnmarshalJSON_String(t *testing.T) {
var st SchemaType
if err := json.Unmarshal([]byte(`"integer"`), &st); err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if st.String() != "integer" {
t.Errorf("expected 'integer', got %q", st.String())
}
if st.IsArray() {
t.Error("should not be array")
}
}
func TestSchemaType_UnmarshalJSON_Array(t *testing.T) {
var st SchemaType
if err := json.Unmarshal([]byte(`["number", "null"]`), &st); err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if !st.IsArray() {
t.Error("should be array")
}
if !st.IsNullable() {
t.Error("should be nullable")
}
types := st.Strings()
if len(types) != 2 || types[0] != "number" || types[1] != "null" {
t.Errorf("unexpected types: %v", types)
}
}
func TestSchemaType_UnmarshalJSON_SingleElementArray(t *testing.T) {
var st SchemaType
if err := json.Unmarshal([]byte(`["string"]`), &st); err != nil {
t.Fatalf("unmarshal error: %v", err)
}
// Single element array should be normalized to string
if st.IsArray() {
t.Error("single element array should be normalized to string")
}
if st.String() != "string" {
t.Errorf("expected 'string', got %q", st.String())
}
}
func TestSchemaType_UnmarshalJSON_Null(t *testing.T) {
var st SchemaType
if err := json.Unmarshal([]byte(`null`), &st); err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if !st.IsEmpty() {
t.Error("null should result in empty")
}
}
func TestSchemaType_MarshalYAML_String(t *testing.T) {
st := NewSchemaType("boolean")
data, err := yaml.Marshal(st)
if err != nil {
t.Fatalf("marshal error: %v", err)
}
if string(data) != "boolean\n" {
t.Errorf("expected 'boolean\\n', got %q", data)
}
}
func TestSchemaType_MarshalYAML_Array(t *testing.T) {
st := NewSchemaTypes([]string{"object", "null"})
data, err := yaml.Marshal(st)
if err != nil {
t.Fatalf("marshal error: %v", err)
}
expected := "- object\n- \"null\"\n"
if string(data) != expected {
t.Errorf("expected %q, got %q", expected, data)
}
}
func TestSchemaType_UnmarshalYAML_String(t *testing.T) {
var st SchemaType
if err := yaml.Unmarshal([]byte("array"), &st); err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if st.String() != "array" {
t.Errorf("expected 'array', got %q", st.String())
}
}
func TestSchemaType_UnmarshalYAML_Array(t *testing.T) {
var st SchemaType
yamlData := `
- string
- "null"
`
if err := yaml.Unmarshal([]byte(yamlData), &st); err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if !st.IsArray() {
t.Error("should be array")
}
if !st.IsNullable() {
t.Error("should be nullable")
}
}
func TestSchemaType_UnmarshalYAML_SingleElementArray(t *testing.T) {
var st SchemaType
if err := yaml.Unmarshal([]byte("- string"), &st); err != nil {
t.Fatalf("unmarshal error: %v", err)
}
// Single element array should be normalized to string
if st.IsArray() {
t.Error("single element array should be normalized to string")
}
if st.String() != "string" {
t.Errorf("expected 'string', got %q", st.String())
}
}
func TestSchemaType_MarshalYAML_Empty(t *testing.T) {
var empty SchemaType
data, err := yaml.Marshal(&empty)
if err != nil {
t.Fatalf("marshal error: %v", err)
}
if string(data) != "null\n" {
t.Errorf("expected 'null\\n', got %q", data)
}
}
func TestSchemaType_UnmarshalYAML_InvalidArray(t *testing.T) {
var st SchemaType
// Create a YAML node that's a sequence with non-string elements
var node yaml.Node
if err := yaml.Unmarshal([]byte("- 123\n- 456"), &node); err != nil {
t.Fatalf("setup error: %v", err)
}
// This should succeed but with string conversion
if err := st.UnmarshalYAML(node.Content[0]); err != nil {
t.Logf("got expected-ish error or nil: %v", err)
}
}
func TestSchemaType_UnmarshalYAML_MappingNode(t *testing.T) {
var st SchemaType
// Create a YAML mapping node
var node yaml.Node
if err := yaml.Unmarshal([]byte("key: value"), &node); err != nil {
t.Fatalf("setup error: %v", err)
}
// Mapping nodes should be silently ignored (returns nil)
if err := st.UnmarshalYAML(node.Content[0]); err != nil {
t.Errorf("unexpected error: %v", err)
}
if !st.IsEmpty() {
t.Error("mapping node should result in empty SchemaType")
}
}
func TestSchemaType_UnmarshalJSON_InvalidArray(t *testing.T) {
var st SchemaType
// Array of non-strings should fail
err := json.Unmarshal([]byte(`[1, 2, 3]`), &st)
if err == nil {
t.Error("expected error for non-string array elements")
}
}
func TestSchemaType_JSON_RoundTrip_String(t *testing.T) {
original := NewSchemaType("string")
data, err := json.Marshal(original)
if err != nil {
t.Fatalf("marshal error: %v", err)
}
var restored SchemaType
if err := json.Unmarshal(data, &restored); err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if original.String() != restored.String() {
t.Errorf("round trip failed: %q != %q", original.String(), restored.String())
}
}
func TestSchemaType_JSON_RoundTrip_Array(t *testing.T) {
original := NewSchemaTypes([]string{"integer", "null"})
data, err := json.Marshal(original)
if err != nil {
t.Fatalf("marshal error: %v", err)
}
var restored SchemaType
if err := json.Unmarshal(data, &restored); err != nil {
t.Fatalf("unmarshal error: %v", err)
}
origTypes := original.Strings()
restoredTypes := restored.Strings()
if len(origTypes) != len(restoredTypes) {
t.Errorf("round trip failed: length mismatch")
}
for i := range origTypes {
if origTypes[i] != restoredTypes[i] {
t.Errorf("round trip failed at index %d: %q != %q", i, origTypes[i], restoredTypes[i])
}
}
}