Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/test/common/jsonSchema.test.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
import assert from 'assert';
6
import { getCompressedContent, IJSONSchema } from '../../common/jsonSchema.js';
7
import { ensureNoDisposablesAreLeakedInTestSuite } from './utils.js';
8
9
suite('JSON Schema', () => {
10
11
ensureNoDisposablesAreLeakedInTestSuite();
12
13
test('getCompressedContent 1', () => {
14
15
const schema: IJSONSchema = {
16
type: 'object',
17
properties: {
18
a: {
19
type: 'object',
20
description: 'a',
21
properties: {
22
b: {
23
type: 'object',
24
properties: {
25
c: {
26
type: 'object',
27
properties: {
28
d: {
29
type: 'string'
30
}
31
}
32
}
33
}
34
}
35
}
36
},
37
e: {
38
type: 'object',
39
description: 'e',
40
properties: {
41
b: {
42
type: 'object',
43
properties: {
44
c: {
45
type: 'object',
46
properties: {
47
d: {
48
type: 'string'
49
}
50
}
51
}
52
}
53
}
54
}
55
}
56
}
57
};
58
59
const expected: IJSONSchema = {
60
type: 'object',
61
properties: {
62
a: {
63
type: 'object',
64
description: 'a',
65
properties: {
66
b: {
67
$ref: '#/$defs/_0'
68
}
69
}
70
},
71
e: {
72
type: 'object',
73
description: 'e',
74
properties: {
75
b: {
76
$ref: '#/$defs/_0'
77
}
78
}
79
}
80
},
81
$defs: {
82
"_0": {
83
type: 'object',
84
properties: {
85
c: {
86
type: 'object',
87
properties: {
88
d: {
89
type: 'string'
90
}
91
}
92
}
93
}
94
}
95
}
96
97
};
98
99
assert.deepEqual(getCompressedContent(schema), JSON.stringify(expected));
100
});
101
102
test('getCompressedContent 2', () => {
103
104
const schema: IJSONSchema = {
105
type: 'object',
106
properties: {
107
a: {
108
type: 'object',
109
properties: {
110
b: {
111
type: 'object',
112
properties: {
113
c: {
114
type: 'object',
115
properties: {
116
d: {
117
type: 'string'
118
}
119
}
120
}
121
}
122
}
123
}
124
},
125
e: {
126
type: 'object',
127
properties: {
128
b: {
129
type: 'object',
130
properties: {
131
c: {
132
type: 'object',
133
properties: {
134
d: {
135
type: 'string'
136
}
137
}
138
}
139
}
140
}
141
}
142
}
143
}
144
};
145
146
const expected: IJSONSchema = {
147
type: 'object',
148
properties: {
149
a: {
150
$ref: '#/$defs/_0'
151
152
},
153
e: {
154
$ref: '#/$defs/_0'
155
}
156
},
157
$defs: {
158
"_0": {
159
type: 'object',
160
properties: {
161
b: {
162
type: 'object',
163
properties: {
164
c: {
165
type: 'object',
166
properties: {
167
d: {
168
type: 'string'
169
}
170
}
171
}
172
}
173
}
174
}
175
}
176
}
177
178
};
179
180
assert.deepEqual(getCompressedContent(schema), JSON.stringify(expected));
181
});
182
183
test('getCompressedContent 3', () => {
184
185
186
const schema: IJSONSchema = {
187
type: 'object',
188
properties: {
189
a: {
190
type: 'object',
191
oneOf: [
192
{
193
allOf: [
194
{
195
properties: {
196
name: {
197
type: 'string'
198
},
199
description: {
200
type: 'string'
201
}
202
}
203
},
204
{
205
properties: {
206
street: {
207
type: 'string'
208
},
209
}
210
}
211
]
212
},
213
{
214
allOf: [
215
{
216
properties: {
217
name: {
218
type: 'string'
219
},
220
description: {
221
type: 'string'
222
}
223
}
224
},
225
{
226
properties: {
227
river: {
228
type: 'string'
229
},
230
}
231
}
232
]
233
},
234
{
235
allOf: [
236
{
237
properties: {
238
name: {
239
type: 'string'
240
},
241
description: {
242
type: 'string'
243
}
244
}
245
},
246
{
247
properties: {
248
mountain: {
249
type: 'string'
250
},
251
}
252
}
253
]
254
}
255
]
256
},
257
b: {
258
type: 'object',
259
properties: {
260
street: {
261
properties: {
262
street: {
263
type: 'string'
264
}
265
}
266
}
267
}
268
}
269
}
270
};
271
272
const expected: IJSONSchema = {
273
"type": "object",
274
"properties": {
275
"a": {
276
"type": "object",
277
"oneOf": [
278
{
279
"allOf": [
280
{
281
"$ref": "#/$defs/_0"
282
},
283
{
284
"$ref": "#/$defs/_1"
285
}
286
]
287
},
288
{
289
"allOf": [
290
{
291
"$ref": "#/$defs/_0"
292
},
293
{
294
"properties": {
295
"river": {
296
"type": "string"
297
}
298
}
299
}
300
]
301
},
302
{
303
"allOf": [
304
{
305
"$ref": "#/$defs/_0"
306
},
307
{
308
"properties": {
309
"mountain": {
310
"type": "string"
311
}
312
}
313
}
314
]
315
}
316
]
317
},
318
"b": {
319
"type": "object",
320
"properties": {
321
"street": {
322
"$ref": "#/$defs/_1"
323
}
324
}
325
}
326
},
327
"$defs": {
328
"_0": {
329
"properties": {
330
"name": {
331
"type": "string"
332
},
333
"description": {
334
"type": "string"
335
}
336
}
337
},
338
"_1": {
339
"properties": {
340
"street": {
341
"type": "string"
342
}
343
}
344
}
345
}
346
};
347
348
const actual = getCompressedContent(schema);
349
assert.deepEqual(actual, JSON.stringify(expected));
350
});
351
352
test('getCompressedContent 4', () => {
353
354
const schema: IJSONSchema = {
355
type: 'object',
356
properties: {
357
a: {
358
type: 'object',
359
properties: {
360
b: {
361
type: 'object',
362
properties: {
363
c: {
364
type: 'object',
365
properties: {
366
d: {
367
type: 'string'
368
}
369
}
370
}
371
}
372
}
373
}
374
},
375
e: {
376
type: 'object',
377
properties: {
378
b: {
379
type: 'object',
380
properties: {
381
c: {
382
type: 'object',
383
properties: {
384
d: {
385
type: 'string'
386
}
387
}
388
}
389
}
390
}
391
}
392
},
393
f: {
394
type: 'object',
395
properties: {
396
d: {
397
type: 'string'
398
}
399
}
400
}
401
}
402
};
403
404
const expected: IJSONSchema = {
405
type: 'object',
406
properties: {
407
a: {
408
$ref: '#/$defs/_0'
409
},
410
e: {
411
$ref: '#/$defs/_0'
412
},
413
f: {
414
$ref: '#/$defs/_1'
415
}
416
},
417
$defs: {
418
"_0": {
419
type: 'object',
420
properties: {
421
b: {
422
type: 'object',
423
properties: {
424
c: {
425
$ref: '#/$defs/_1'
426
}
427
}
428
}
429
}
430
},
431
"_1": {
432
type: 'object',
433
properties: {
434
d: {
435
type: 'string'
436
}
437
}
438
}
439
}
440
441
};
442
443
assert.deepEqual(getCompressedContent(schema), JSON.stringify(expected));
444
});
445
446
test('getCompressedContent 5', () => {
447
448
const schema: IJSONSchema = {
449
type: 'object',
450
properties: {
451
a: {
452
type: 'array',
453
items: {
454
type: 'object',
455
properties: {
456
c: {
457
type: 'object',
458
properties: {
459
d: {
460
type: 'string'
461
}
462
}
463
}
464
}
465
}
466
},
467
e: {
468
type: 'array',
469
items: {
470
type: 'object',
471
properties: {
472
c: {
473
type: 'object',
474
properties: {
475
d: {
476
type: 'string'
477
}
478
}
479
}
480
}
481
}
482
},
483
f: {
484
type: 'object',
485
properties: {
486
b: {
487
type: 'object',
488
properties: {
489
c: {
490
type: 'object',
491
properties: {
492
d: {
493
type: 'string'
494
}
495
}
496
}
497
}
498
}
499
}
500
},
501
g: {
502
type: 'object',
503
properties: {
504
b: {
505
type: 'object',
506
properties: {
507
c: {
508
type: 'object',
509
properties: {
510
d: {
511
type: 'string'
512
}
513
}
514
}
515
}
516
}
517
}
518
}
519
}
520
};
521
522
const expected: IJSONSchema = {
523
type: 'object',
524
properties: {
525
a: {
526
$ref: '#/$defs/_0'
527
},
528
e: {
529
$ref: '#/$defs/_0'
530
},
531
f: {
532
$ref: '#/$defs/_1'
533
},
534
g: {
535
$ref: '#/$defs/_1'
536
}
537
},
538
$defs: {
539
"_0": {
540
type: 'array',
541
items: {
542
$ref: '#/$defs/_2'
543
}
544
},
545
"_1": {
546
type: 'object',
547
properties: {
548
b: {
549
$ref: '#/$defs/_2'
550
}
551
}
552
},
553
"_2": {
554
type: 'object',
555
properties: {
556
c: {
557
type: 'object',
558
properties: {
559
d: {
560
type: 'string'
561
}
562
}
563
}
564
}
565
}
566
}
567
568
};
569
570
assert.deepEqual(getCompressedContent(schema), JSON.stringify(expected));
571
});
572
573
574
});
575
576