1
+ // +build go1.7
2
+
1
3
package jsonschema_test
2
4
3
5
import (
@@ -10,6 +12,24 @@ import (
10
12
"github.com/stretchr/testify/assert"
11
13
)
12
14
15
+ type dummyValidator struct {}
16
+
17
+ func (v dummyValidator ) Validate (value interface {}) (interface {}, error ) {
18
+ return value , nil
19
+ }
20
+
21
+ func TestErrNotImplemented (t * testing.T ) {
22
+ s := schema.Schema {
23
+ Fields : schema.Fields {
24
+ "i" : {
25
+ Validator : & dummyValidator {},
26
+ },
27
+ },
28
+ }
29
+ enc := jsonschema .NewEncoder (new (bytes.Buffer ))
30
+ assert .Equal (t , jsonschema .ErrNotImplemented , enc .Encode (& s ))
31
+ }
32
+
13
33
// encoderTestCase is used to test the Encoder.Encode() function.
14
34
type encoderTestCase struct {
15
35
name string
@@ -18,23 +38,20 @@ type encoderTestCase struct {
18
38
customValidate encoderValidator
19
39
}
20
40
21
- // Run runs the testCase according to your Go version. For Go >= 1.7, test cases are run in parallel using the Go 1.7
22
- // sub-test feature. For older versions of Go, the testCase name is simply logged before tests are run sequentially.
23
41
func (tc * encoderTestCase ) Run (t * testing.T ) {
24
- tc .run ( t )
25
- }
42
+ t . Run ( tc .name , func ( t * testing. T ) {
43
+ t . Parallel ()
26
44
27
- // test performs the actual test and is used by both implementations of run.
28
- func (tc * encoderTestCase ) test (t * testing.T ) {
29
- b := new (bytes.Buffer )
30
- enc := jsonschema .NewEncoder (b )
31
- assert .NoError (t , enc .Encode (& tc .schema ))
45
+ b := new (bytes.Buffer )
46
+ enc := jsonschema .NewEncoder (b )
47
+ assert .NoError (t , enc .Encode (& tc .schema ))
32
48
33
- if tc .customValidate == nil {
34
- assert .JSONEq (t , tc .expect , b .String ())
35
- } else {
36
- tc .customValidate (t , b .Bytes ())
37
- }
49
+ if tc .customValidate == nil {
50
+ assert .JSONEq (t , tc .expect , b .String ())
51
+ } else {
52
+ tc .customValidate (t , b .Bytes ())
53
+ }
54
+ })
38
55
}
39
56
40
57
// encoderValidator can be used to validate encoded data.
@@ -63,7 +80,7 @@ func TestEncoder(t *testing.T) {
63
80
name : "ReadOnly=true" ,
64
81
schema : schema.Schema {
65
82
Fields : schema.Fields {
66
- "name" : schema. Field {
83
+ "name" : {
67
84
ReadOnly : true ,
68
85
Validator : & schema.String {},
69
86
},
@@ -84,7 +101,7 @@ func TestEncoder(t *testing.T) {
84
101
name : `type(Default)=string` ,
85
102
schema : schema.Schema {
86
103
Fields : schema.Fields {
87
- "name" : schema. Field {
104
+ "name" : {
88
105
Validator : & schema.String {},
89
106
Default : "Luke" ,
90
107
},
@@ -110,7 +127,7 @@ func TestEncoder(t *testing.T) {
110
127
name : `type(Default)=int` ,
111
128
schema : schema.Schema {
112
129
Fields : schema.Fields {
113
- "name" : schema. Field {
130
+ "name" : {
114
131
Validator : & schema.String {},
115
132
Default : 24 ,
116
133
},
@@ -135,10 +152,10 @@ func TestEncoder(t *testing.T) {
135
152
Validator : & schema.Object {
136
153
Schema : & schema.Schema {
137
154
Fields : schema.Fields {
138
- "name" : schema. Field {
155
+ "name" : {
139
156
Validator : & schema.String {},
140
157
},
141
- "age" : schema. Field {
158
+ "age" : {
142
159
Validator : & schema.Integer {},
143
160
},
144
161
},
@@ -178,13 +195,13 @@ func TestEncoder(t *testing.T) {
178
195
name : "MinLen=2" ,
179
196
schema : schema.Schema {
180
197
Fields : schema.Fields {
181
- "foo" : schema. Field {
198
+ "foo" : {
182
199
Validator : & schema.Bool {},
183
200
},
184
- "bar" : schema. Field {
201
+ "bar" : {
185
202
Validator : & schema.Bool {},
186
203
},
187
- "baz" : schema. Field {
204
+ "baz" : {
188
205
Validator : & schema.Bool {},
189
206
},
190
207
},
@@ -205,13 +222,13 @@ func TestEncoder(t *testing.T) {
205
222
name : "MaxLen=2" ,
206
223
schema : schema.Schema {
207
224
Fields : schema.Fields {
208
- "foo" : schema. Field {
225
+ "foo" : {
209
226
Validator : & schema.Bool {},
210
227
},
211
- "bar" : schema. Field {
228
+ "bar" : {
212
229
Validator : & schema.Bool {},
213
230
},
214
- "baz" : schema. Field {
231
+ "baz" : {
215
232
Validator : & schema.Bool {},
216
233
},
217
234
},
@@ -232,11 +249,11 @@ func TestEncoder(t *testing.T) {
232
249
name : "Required=true(1/2)" ,
233
250
schema : schema.Schema {
234
251
Fields : schema.Fields {
235
- "name" : schema. Field {
252
+ "name" : {
236
253
Required : true ,
237
254
Validator : & schema.String {},
238
255
},
239
- "age" : schema. Field {
256
+ "age" : {
240
257
Validator : & schema.Integer {},
241
258
},
242
259
},
@@ -259,15 +276,15 @@ func TestEncoder(t *testing.T) {
259
276
name : "Required=true(2/3)" ,
260
277
schema : schema.Schema {
261
278
Fields : schema.Fields {
262
- "name" : schema. Field {
279
+ "name" : {
263
280
Required : true ,
264
281
Validator : & schema.String {},
265
282
},
266
- "age" : schema. Field {
283
+ "age" : {
267
284
Required : true ,
268
285
Validator : & schema.Integer {},
269
286
},
270
- "class" : schema. Field {
287
+ "class" : {
271
288
Validator : & schema.String {},
272
289
},
273
290
},
@@ -304,7 +321,7 @@ func TestEncoder(t *testing.T) {
304
321
name : "Validator=Array,ValuesValidator=Object{Schema:nil}" ,
305
322
schema : schema.Schema {
306
323
Fields : schema.Fields {
307
- "students" : schema. Field {
324
+ "students" : {
308
325
Validator : & schema.Array {
309
326
ValuesValidator : & schema.Object {},
310
327
},
@@ -327,14 +344,14 @@ func TestEncoder(t *testing.T) {
327
344
schema : schema.Schema {
328
345
Description : "Object with array of students" ,
329
346
Fields : schema.Fields {
330
- "students" : schema. Field {
347
+ "students" : {
331
348
Description : "Array of students" ,
332
349
Validator : & schema.Array {
333
350
ValuesValidator : & schema.Object {
334
351
Schema : & schema.Schema {
335
352
Description : "Student and class" ,
336
353
Fields : schema.Fields {
337
- "student" : schema. Field {
354
+ "student" : {
338
355
Description : "The student name" ,
339
356
Required : true ,
340
357
Default : "Unknown" ,
@@ -343,7 +360,7 @@ func TestEncoder(t *testing.T) {
343
360
MaxLen : 10 ,
344
361
},
345
362
},
346
- "class" : schema. Field {
363
+ "class" : {
347
364
Description : "The class name" ,
348
365
Default : "Unassigned" ,
349
366
Validator : & schema.String {
@@ -395,11 +412,11 @@ func TestEncoder(t *testing.T) {
395
412
name : `Validator=Object,Fields["location"].Validator=Object` ,
396
413
schema : schema.Schema {
397
414
Fields : schema.Fields {
398
- "location" : schema. Field {
415
+ "location" : {
399
416
Validator : & schema.Object {
400
417
Schema : & schema.Schema {
401
418
Fields : schema.Fields {
402
- "country" : schema. Field {
419
+ "country" : {
403
420
Validator : & schema.String {},
404
421
},
405
422
},
@@ -428,7 +445,7 @@ func TestEncoder(t *testing.T) {
428
445
name : `Incorrectly configured field` ,
429
446
schema : schema.Schema {
430
447
Fields : schema.Fields {
431
- "location" : schema. Field {
448
+ "location" : {
432
449
Description : "location of your stuff" ,
433
450
},
434
451
},
0 commit comments