openapi_generate_typescript_fetch 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,664 @@
1
+ <%=
2
+ Gen.output.config = Gen.x.cfg['ts_indentation']
3
+ Gen.x.generator_info
4
+ %>
5
+
6
+ import { expect } from 'chai';
7
+ import * as schemas from '../src/schemas.js';
8
+ import type {<%= Gen.x.schemas.import_types.join(', ') %>} from '../src/schemas.d.ts';
9
+ import {<%= Gen.x.makers.functions.sort.join(', ') %>} from './makers.js';
10
+
11
+ describe('Basic Type Utility Functions', () => {
12
+ describe('isNot', () => {
13
+ it('should return false for anything', () => {
14
+ expect(schemas.isNot(null)).to.be.false;
15
+ expect(schemas.isNot(undefined)).to.be.false;
16
+ expect(schemas.isNot(0)).to.be.false;
17
+ expect(schemas.isNot('')).to.be.false;
18
+ expect(schemas.isNot(false)).to.be.false;
19
+ expect(schemas.isNot({})).to.be.false;
20
+ expect(schemas.isNot([])).to.be.false;
21
+ });
22
+ });
23
+ describe('Any Utilities', () => {
24
+ describe('isAny', () => {
25
+ it('should return true for anything', () => {
26
+ expect(schemas.isAny(null)).to.be.true;
27
+ expect(schemas.isAny(undefined)).to.be.true;
28
+ expect(schemas.isAny(0)).to.be.true;
29
+ expect(schemas.isAny('')).to.be.true;
30
+ expect(schemas.isAny(false)).to.be.true;
31
+ expect(schemas.isAny({})).to.be.true;
32
+ expect(schemas.isAny([])).to.be.true;
33
+ });
34
+ });
35
+
36
+ describe('baseAny', () => {
37
+ it('should return the input as is', () => {
38
+ expect(schemas.baseAny(null)).to.equal(null);
39
+ expect(schemas.baseAny(undefined)).to.equal(undefined);
40
+ expect(schemas.baseAny(0)).to.equal(0);
41
+ expect(schemas.baseAny('')).to.equal('');
42
+ expect(schemas.baseAny(false)).to.equal(false);
43
+ const obj = {};
44
+ expect(schemas.baseAny(obj)).to.equal(obj);
45
+ const arr: any[] = [];
46
+ expect(schemas.baseAny(arr)).to.equal(arr);
47
+ });
48
+ });
49
+
50
+ describe('unknown2Any', () => {
51
+ it('should return the input as is', () => {
52
+ expect(schemas.unknown2Any(null)).to.equal(null);
53
+ expect(schemas.unknown2Any(undefined)).to.equal(undefined);
54
+ expect(schemas.unknown2Any(0)).to.equal(0);
55
+ expect(schemas.unknown2Any('')).to.equal('');
56
+ expect(schemas.unknown2Any(false)).to.equal(false);
57
+ const obj = {};
58
+ expect(schemas.unknown2Any(obj)).to.equal(obj);
59
+ const arr: any[] = [];
60
+ expect(schemas.unknown2Any(arr)).to.equal(arr);
61
+ });
62
+ });
63
+ });
64
+ // From claude-3-7-sonnet-20250219 with minor modifications.
65
+ // String utility functions
66
+ describe('String Utilities', () => {
67
+ describe('isString', () => {
68
+ it('should return true for string values', () => {
69
+ expect(schemas.isString('')).to.be.true;
70
+ expect(schemas.isString('hello')).to.be.true;
71
+ expect(schemas.isString(String('test'))).to.be.true;
72
+ });
73
+
74
+ it('should return false for non-string values', () => {
75
+ expect(schemas.isString(123)).to.be.false;
76
+ expect(schemas.isString(true)).to.be.false;
77
+ expect(schemas.isString({})).to.be.false;
78
+ expect(schemas.isString([])).to.be.false;
79
+ expect(schemas.isString(null)).to.be.false;
80
+ expect(schemas.isString(undefined)).to.be.false;
81
+ });
82
+ });
83
+
84
+ describe('unknown2String', () => {
85
+ it('should return the string when given a string', () => {
86
+ expect(schemas.unknown2String('')).to.equal('');
87
+ expect(schemas.unknown2String('hello')).to.equal('hello');
88
+ });
89
+
90
+ it('should throw an error when given a non-string', () => {
91
+ expect(() => schemas.unknown2String(123)).to.throw;
92
+ expect(() => schemas.unknown2String(true)).to.throw;
93
+ expect(() => schemas.unknown2String({})).to.throw;
94
+ expect(() => schemas.unknown2String(null)).to.throw;
95
+ });
96
+ });
97
+
98
+ describe('baseString', () => {
99
+ it('should return the input string', () => {
100
+ expect(schemas.baseString('')).to.equal('');
101
+ expect(schemas.baseString('hello')).to.equal('hello');
102
+ expect(schemas.baseString('123')).to.equal('123');
103
+ });
104
+ });
105
+ });
106
+
107
+ // Number utility functions
108
+ describe('Number Utilities', () => {
109
+ describe('isNumber', () => {
110
+ it('should return true for number values', () => {
111
+ expect(schemas.isNumber(0)).to.be.true;
112
+ expect(schemas.isNumber(123)).to.be.true;
113
+ expect(schemas.isNumber(-456)).to.be.true;
114
+ expect(schemas.isNumber(3.14)).to.be.true;
115
+ expect(schemas.isNumber(Number('42'))).to.be.true;
116
+ });
117
+
118
+ it('should return false for non-number values', () => {
119
+ expect(schemas.isNumber('123')).to.be.false;
120
+ expect(schemas.isNumber(true)).to.be.false;
121
+ expect(schemas.isNumber({})).to.be.false;
122
+ expect(schemas.isNumber([])).to.be.false;
123
+ expect(schemas.isNumber(null)).to.be.false;
124
+ expect(schemas.isNumber(undefined)).to.be.false;
125
+ expect(schemas.isNumber(NaN)).to.be.false; // Note: typeof NaN is 'number', but we might want to consider this case
126
+ });
127
+ });
128
+
129
+ describe('unknown2Number', () => {
130
+ it('should return the number when given a number', () => {
131
+ expect(schemas.unknown2Number(0)).to.equal(0);
132
+ expect(schemas.unknown2Number(123)).to.equal(123);
133
+ expect(schemas.unknown2Number(-456)).to.equal(-456);
134
+ expect(schemas.unknown2Number(3.14)).to.equal(3.14);
135
+ });
136
+
137
+ it('should throw an error when given a non-number', () => {
138
+ expect(() => schemas.unknown2Number('123')).to.throw;
139
+ expect(() => schemas.unknown2Number(true)).to.throw;
140
+ expect(() => schemas.unknown2Number({})).to.throw;
141
+ expect(() => schemas.unknown2Number(null)).to.throw;
142
+ });
143
+ });
144
+
145
+ describe('baseNumber', () => {
146
+ it('should return the input number', () => {
147
+ expect(schemas.baseNumber(0)).to.equal(0);
148
+ expect(schemas.baseNumber(123)).to.equal(123);
149
+ expect(schemas.baseNumber(-456)).to.equal(-456);
150
+ expect(schemas.baseNumber(3.14)).to.equal(3.14);
151
+ });
152
+ });
153
+ });
154
+
155
+ // Boolean utility functions
156
+ describe('Boolean Utilities', () => {
157
+ describe('isBoolean', () => {
158
+ it('should return true for boolean values', () => {
159
+ expect(schemas.isBoolean(true)).to.be.true;
160
+ expect(schemas.isBoolean(false)).to.be.true;
161
+ expect(schemas.isBoolean(Boolean(1))).to.be.true;
162
+ });
163
+
164
+ it('should return false for non-boolean values', () => {
165
+ expect(schemas.isBoolean(0)).to.be.false;
166
+ expect(schemas.isBoolean(1)).to.be.false;
167
+ expect(schemas.isBoolean('true')).to.be.false;
168
+ expect(schemas.isBoolean({})).to.be.false;
169
+ expect(schemas.isBoolean([])).to.be.false;
170
+ expect(schemas.isBoolean(null)).to.be.false;
171
+ expect(schemas.isBoolean(undefined)).to.be.false;
172
+ });
173
+ });
174
+
175
+ describe('unknown2Boolean', () => {
176
+ it('should return the boolean when given a boolean', () => {
177
+ expect(schemas.unknown2Boolean(true)).to.equal(true);
178
+ expect(schemas.unknown2Boolean(false)).to.equal(false);
179
+ });
180
+
181
+ it('should throw an error when given a non-boolean', () => {
182
+ expect(() => schemas.unknown2Boolean(0)).to.throw;
183
+ expect(() => schemas.unknown2Boolean('true')).to.throw;
184
+ expect(() => schemas.unknown2Boolean({})).to.throw;
185
+ expect(() => schemas.unknown2Boolean(null)).to.throw;
186
+ });
187
+ });
188
+
189
+ describe('baseBoolean', () => {
190
+ it('should return the input boolean', () => {
191
+ expect(schemas.baseBoolean(true)).to.equal(true);
192
+ expect(schemas.baseBoolean(false)).to.equal(false);
193
+ });
194
+ });
195
+ });
196
+
197
+ // Claude somehow omitted array function tests.
198
+ // Array utility functions
199
+ describe('Array Utilities', () => {
200
+ describe('isArray', () => {
201
+ it('should return true for array values', () => {
202
+ expect(schemas.isArray([])).to.be.true;
203
+ expect(schemas.isArray([1, 2])).to.be.true;
204
+ })
205
+ it('should return false for non-arrays', () => {
206
+ expect(schemas.isArray({ key: 'value' })).to.be.false;
207
+ expect(schemas.isArray(null)).to.be.false;
208
+ expect(schemas.isArray(1)).to.be.false;
209
+ expect(schemas.isArray('str')).to.be.false;
210
+ expect(schemas.isArray(undefined)).to.be.false;
211
+ expect(schemas.isArray(true)).to.be.false;
212
+ })
213
+ });
214
+ describe('unknown2Array', () => {
215
+ it('returns an array when array', () => {
216
+ const arr = [1, 2];
217
+ expect(schemas.unknown2Array(arr)).to.deep.equal(arr);
218
+ });
219
+ it('throws when not an array', () => {
220
+ expect(() => schemas.unknown2Array(true)).to.throw;
221
+ expect(() => schemas.unknown2Array(null)).to.throw;
222
+ expect(() => schemas.unknown2Array(12)).to.throw;
223
+ expect(() => schemas.unknown2Array('abc')).to.throw;
224
+ expect(() => schemas.unknown2Array({})).to.throw;
225
+ expect(() => schemas.unknown2Array(undefined)).to.throw;
226
+ });
227
+ });
228
+ describe('baseArray', () => {
229
+ it('should return the input array', () => {
230
+ const arr = [1, 2];
231
+ expect(schemas.baseArray(arr)).to.deep.equal(arr);
232
+ const arr2 = [ 1, { key: 'value' }, arr];
233
+ expect(schemas.baseArray(arr2)).to.deep.equal(arr2);
234
+ });
235
+ });
236
+ })
237
+
238
+ // Object utility functions
239
+ describe('Object Utilities', () => {
240
+ describe('isObject', () => {
241
+ it('should return true for object values', () => {
242
+ expect(schemas.isObject({})).to.be.true;
243
+ expect(schemas.isObject({ key: 'value' })).to.be.true;
244
+ expect(schemas.isObject(new Date())).to.be.true;
245
+ });
246
+
247
+ it('should return false for non-object values', () => {
248
+ // Arrays and null are objects in JavaScript but function decides otherwise.
249
+ expect(schemas.isObject([])).to.be.false;
250
+ expect(schemas.isObject(null)).to.be.false;
251
+ expect(schemas.isObject(undefined)).to.be.false;
252
+ expect(schemas.isObject(123)).to.be.false;
253
+ expect(schemas.isObject('string')).to.be.false;
254
+ expect(schemas.isObject(true)).to.be.false;
255
+ });
256
+ });
257
+
258
+ describe('unknown2Object', () => {
259
+ it('should return the object when given an object', () => {
260
+ const obj = { key: 'value' };
261
+ expect(schemas.unknown2Object(obj)).to.equal(obj);
262
+ });
263
+
264
+ it('should throw an error when given a non-object', () => {
265
+ expect(() => schemas.unknown2Object(123)).to.throw;
266
+ expect(() => schemas.unknown2Object('string')).to.throw;
267
+ expect(() => schemas.unknown2Object(true)).to.throw;
268
+ expect(() => schemas.unknown2Object(null)).to.throw;
269
+ expect(() => schemas.unknown2Object([ 1, 2 ])).to.throw;
270
+ });
271
+ });
272
+
273
+ describe('baseObject', () => {
274
+ it('should return the input object', () => {
275
+ const obj = { key: 'value' };
276
+ expect(schemas.baseObject(obj)).to.deep.equal(obj);
277
+ });
278
+ it('handles array and object values', () => {
279
+ const obj = {
280
+ key: 'value',
281
+ arr: [1, 2],
282
+ obj: {
283
+ key2: 'value2'
284
+ }
285
+ };
286
+ expect(schemas.baseObject(obj)).to.deep.equal(obj);
287
+ })
288
+ });
289
+ });
290
+ });
291
+
292
+ describe('Request Encoding Utilities', () => {
293
+ describe('jsonEncode', () => {
294
+ it('should encode an object as JSON UTF-8 and return the correct tuple', () => {
295
+ const body = { key: 'value', num: 42 };
296
+ const [encoded, contentType, length, headers] = schemas.jsonEncode(body);
297
+ expect(encoded).to.be.instanceOf(Uint8Array);
298
+ expect(contentType).to.equal('application/json; charset=utf-8');
299
+ const expectedBytes = new TextEncoder().encode(JSON.stringify(body));
300
+ expect(length).to.equal(expectedBytes.length);
301
+ expect(headers).to.deep.equal({});
302
+ expect(encoded).to.deep.equal(expectedBytes);
303
+ });
304
+
305
+ it('should encode a string body', () => {
306
+ const body = 'hello world';
307
+ const [encoded, contentType, length, headers] = schemas.jsonEncode(body);
308
+ expect(encoded).to.be.instanceOf(Uint8Array);
309
+ expect(contentType).to.equal('application/json; charset=utf-8');
310
+ const expectedBytes = new TextEncoder().encode(JSON.stringify(body));
311
+ expect(length).to.equal(expectedBytes.length);
312
+ expect(headers).to.deep.equal({});
313
+ expect(encoded).to.deep.equal(expectedBytes);
314
+ });
315
+
316
+ it('should encode a number body', () => {
317
+ const body = 123;
318
+ const [encoded, contentType, length, headers] = schemas.jsonEncode(body);
319
+ expect(encoded).to.be.instanceOf(Uint8Array);
320
+ expect(contentType).to.equal('application/json; charset=utf-8');
321
+ expect(length).to.equal((encoded as Uint8Array).length);
322
+ expect(headers).to.deep.equal({});
323
+ });
324
+
325
+ it('should encode an array body', () => {
326
+ const body = [1, 'two', true, null];
327
+ const [encoded, contentType, length, headers] = schemas.jsonEncode(body);
328
+ expect(encoded).to.be.instanceOf(Uint8Array);
329
+ expect(contentType).to.equal('application/json; charset=utf-8');
330
+ const expectedBytes = new TextEncoder().encode(JSON.stringify(body));
331
+ expect(length).to.equal(expectedBytes.length);
332
+ expect(headers).to.deep.equal({});
333
+ });
334
+
335
+ it('should encode null', () => {
336
+ const [encoded, contentType, length, headers] = schemas.jsonEncode(null);
337
+ expect(encoded).to.be.instanceOf(Uint8Array);
338
+ expect(contentType).to.equal('application/json; charset=utf-8');
339
+ expect(length).to.equal((encoded as Uint8Array).length);
340
+ expect(headers).to.deep.equal({});
341
+ });
342
+
343
+ it('should encode a nested object', () => {
344
+ const body = { a: { b: { c: [1, 2, 3] } } };
345
+ const [encoded, contentType, length, headers] = schemas.jsonEncode(body);
346
+ expect(encoded).to.be.instanceOf(Uint8Array);
347
+ expect(contentType).to.equal('application/json; charset=utf-8');
348
+ const expectedBytes = new TextEncoder().encode(JSON.stringify(body));
349
+ expect(length).to.equal(expectedBytes.length);
350
+ expect(headers).to.deep.equal({});
351
+ expect(encoded).to.deep.equal(expectedBytes);
352
+ });
353
+ });
354
+
355
+ describe('isEncodedBodyType', () => {
356
+ it('should return true for a string', () => {
357
+ expect(schemas.isEncodedBodyType('hello')).to.be.true;
358
+ expect(schemas.isEncodedBodyType('')).to.be.true;
359
+ });
360
+
361
+ it('should return true for an ArrayBuffer', () => {
362
+ expect(schemas.isEncodedBodyType(new ArrayBuffer(8))).to.be.true;
363
+ });
364
+
365
+ it('should return true for a Uint8Array (TypedArray / ArrayBufferView)', () => {
366
+ expect(schemas.isEncodedBodyType(new Uint8Array(4))).to.be.true;
367
+ });
368
+
369
+ it('should return true for a DataView (ArrayBufferView)', () => {
370
+ expect(schemas.isEncodedBodyType(new DataView(new ArrayBuffer(8)))).to.be.true;
371
+ });
372
+
373
+ it('should return true for a Blob', () => {
374
+ expect(schemas.isEncodedBodyType(new Blob([]))).to.be.true;
375
+ });
376
+
377
+ it('should return true for a File (subclass of Blob)', () => {
378
+ expect(schemas.isEncodedBodyType(new File([], 'test.txt'))).to.be.true;
379
+ });
380
+
381
+ it('should return true for FormData', () => {
382
+ expect(schemas.isEncodedBodyType(new FormData())).to.be.true;
383
+ });
384
+
385
+ it('should return true for URLSearchParams', () => {
386
+ expect(schemas.isEncodedBodyType(new URLSearchParams())).to.be.true;
387
+ });
388
+
389
+ it('should return true for a ReadableStream', () => {
390
+ expect(schemas.isEncodedBodyType(new ReadableStream())).to.be.true;
391
+ });
392
+
393
+ it('should return false for a number', () => {
394
+ expect(schemas.isEncodedBodyType(42)).to.be.false;
395
+ });
396
+
397
+ it('should return false for a boolean', () => {
398
+ expect(schemas.isEncodedBodyType(true)).to.be.false;
399
+ expect(schemas.isEncodedBodyType(false)).to.be.false;
400
+ });
401
+
402
+ it('should return false for null', () => {
403
+ expect(schemas.isEncodedBodyType(null)).to.be.false;
404
+ });
405
+
406
+ it('should return false for undefined', () => {
407
+ expect(schemas.isEncodedBodyType(undefined)).to.be.false;
408
+ });
409
+
410
+ it('should return false for a plain object', () => {
411
+ expect(schemas.isEncodedBodyType({ key: 'value' })).to.be.false;
412
+ });
413
+
414
+ it('should return false for an array', () => {
415
+ expect(schemas.isEncodedBodyType([1, 2, 3])).to.be.false;
416
+ });
417
+ });
418
+
419
+ describe('baseEncodedBodyType', () => {
420
+ it('should return a string as-is', () => {
421
+ const s = 'hello';
422
+ expect(schemas.baseEncodedBodyType(s)).to.equal(s);
423
+ });
424
+
425
+ it('should return a Uint8Array as-is', () => {
426
+ const buf = new Uint8Array([1, 2, 3]);
427
+ expect(schemas.baseEncodedBodyType(buf)).to.equal(buf);
428
+ });
429
+
430
+ it('should return an ArrayBuffer as-is', () => {
431
+ const ab = new ArrayBuffer(8);
432
+ expect(schemas.baseEncodedBodyType(ab)).to.equal(ab);
433
+ });
434
+
435
+ it('should return a Blob as-is', () => {
436
+ const blob = new Blob(['data']);
437
+ expect(schemas.baseEncodedBodyType(blob)).to.equal(blob);
438
+ });
439
+
440
+ it('should return FormData as-is', () => {
441
+ const fd = new FormData();
442
+ expect(schemas.baseEncodedBodyType(fd)).to.equal(fd);
443
+ });
444
+
445
+ it('should return URLSearchParams as-is', () => {
446
+ const usp = new URLSearchParams('key=value');
447
+ expect(schemas.baseEncodedBodyType(usp)).to.equal(usp);
448
+ });
449
+
450
+ it('should return a ReadableStream as-is', () => {
451
+ const rs = new ReadableStream();
452
+ expect(schemas.baseEncodedBodyType(rs)).to.equal(rs);
453
+ });
454
+ });
455
+ });
456
+
457
+
458
+
459
+ // Types and checker functions.
460
+ <%=
461
+ out = []
462
+ sis = Gen.x.order
463
+ sis.each do |si|
464
+ # This should just loop over pass and fail functions and make expects as appropriate.
465
+ out.push("describe('#{si.name}', function () {")
466
+ out.push(" it('#{si.schema[:is]}', function () {")
467
+ si.schema[:pass].each do |fn|
468
+ out.push(" expect(schemas.#{si.schema[:is]}(#{fn}())).to.be.true;")
469
+ end
470
+ si.schema[:fail].each do |fn|
471
+ out.push(" expect(schemas.#{si.schema[:is]}(#{fn}())).to.be.false;")
472
+ end
473
+ si.schema[:typefail].each do |fn|
474
+ out.push(" expect(schemas.#{si.schema[:is]}(#{fn}())).to.be.false;")
475
+ end
476
+ out.push(" });")
477
+ out.push(" it('#{si.schema[:unknown]}', function () {")
478
+ si.schema[:pass].each do |fn|
479
+ out.push(" expect(schemas.#{si.schema[:unknown]}(#{fn}())).to.deep.equal(#{fn}());")
480
+ end
481
+ si.schema[:fail].each do |fn|
482
+ out.push(" expect(() => schemas.#{si.schema[:unknown]}(#{fn}())).to.throw;")
483
+ end
484
+ out.push(" });")
485
+ out.push(" it('#{si.schema[:base]}', function () {")
486
+ # Does not strip something useful.
487
+ si.schema[:pass].each do |fn|
488
+ out.push(" expect(schemas.#{si.schema[:base]}(#{fn}())).to.deep.equal(#{fn}());")
489
+ end
490
+ if si.schema.key?(:obj)
491
+ unknowns = si.schema[:obj].unknown_names || []
492
+ unknowns.each do |name|
493
+ si.schema[:pass].each do |fn|
494
+ out.push(" expect(schemas.#{si.schema[:base]}({ ...#{fn}(), #{name}: true } as any as #{si.name})).to.deep.equal(#{fn}());")
495
+ end
496
+ end
497
+ end
498
+ out.push(" });")
499
+ out.push("});")
500
+ out.push('')
501
+ end
502
+ Gen.output.join(out)
503
+ %>
504
+
505
+ // Operation parameter type tests.
506
+ <%=
507
+ out = []
508
+ Gen.doc['paths'].each do |path, path_item_object|
509
+ oos = OpenAPISourceTools::ApiObjects.operation_objects(path_item_object)
510
+ oos.each do |method, operation_object|
511
+ out.push("describe('#{operation_object[:args_name]}', function () {")
512
+ out.push(" it('#{operation_object[:args_is]}', function () {")
513
+ operation_object[:pass].each do |fn|
514
+ out.push(" expect(schemas.#{operation_object[:args_is]}(#{fn}())).to.be.true;")
515
+ end
516
+ operation_object[:fail].each do |fn|
517
+ out.push(" expect(schemas.#{operation_object[:args_is]}(#{fn}())).to.be.false;")
518
+ end
519
+ operation_object[:typefail].each do |fn|
520
+ out.push(" expect(schemas.#{operation_object[:args_is]}(#{fn}())).to.be.false;")
521
+ end
522
+ operation_object[:throw].each do |fn|
523
+ out.push(" expect(() => schemas.#{operation_object[:args_is]}(#{fn}())).to.throw;")
524
+ end
525
+ out.push(" });")
526
+ operation_object[:pass].each do |fn|
527
+ out.push("")
528
+ s = <<EOB
529
+ it('#{operation_object[:args_base]} #{fn}', function () {
530
+ const src = #{fn}();
531
+ const keys = Object.keys(src);
532
+ const dst = schemas.#{operation_object[:args_base]}(src);
533
+ if (keys.includes('encoded')) {
534
+ // All body-properties have been dropped.
535
+ const bodies = keys.filter((name: string): boolean => name.startsWith('body'));
536
+ for (const name of bodies) {
537
+ expect(dst).not.to.have.property(name);
538
+ const d: object = dst as object;
539
+ const s: object = src as object;
540
+ d[name as keyof object] = s[name as keyof object]; // Put back for simpler testing.
541
+ }
542
+ }
543
+ expect(dst).to.deep.equal(src);
544
+ });
545
+ EOB
546
+ out.push(s)
547
+ end
548
+ out.push("});")
549
+ out.push('')
550
+ end
551
+ end
552
+ Gen.output.join(out)
553
+ %>
554
+
555
+ // Operation response base types tests.
556
+ <%=
557
+ out = []
558
+ Gen.doc.dig('components', 'responses').each do |name, response|
559
+ name2type = response[:name2type]
560
+ next if name2type.nil?
561
+ out.push("describe('#{response[:name]}', function () {")
562
+ out.push(" it('#{response[:is]}', function () {")
563
+ response[:pass].each do |fn|
564
+ out.push(" expect(schemas.#{response[:is]}(#{fn}())).to.be.true;")
565
+ end
566
+ response[:fail].each do |fn|
567
+ out.push(" expect(schemas.#{response[:is]}(#{fn}())).to.be.false;")
568
+ end
569
+ response[:typefail].each do |fn|
570
+ out.push(" expect(schemas.#{response[:is]}(#{fn}())).to.be.false;")
571
+ end
572
+ response[:null].each do |fn|
573
+ out.push(" expect(schemas.#{response[:is]}(#{fn}())).to.be.true;")
574
+ end
575
+ out.push(" });")
576
+ out.push(" it('#{response[:unknown]}', function () {")
577
+ response[:pass].each do |fn|
578
+ out.push(" expect(schemas.#{response[:unknown]}(#{fn}())).to.deep.equal(#{fn}());")
579
+ end
580
+ response[:fail].each do |fn|
581
+ out.push(" expect(() => schemas.#{response[:unknown]}(#{fn}())).to.throw;")
582
+ end
583
+ response[:typefail].each do |fn|
584
+ out.push(" expect(() => schemas.#{response[:unknown]}(#{fn}())).to.throw;")
585
+ end
586
+ response[:null].each do |fn|
587
+ out.push(" expect(schemas.#{response[:unknown]}(#{fn}())).to.deep.equal(#{fn}());")
588
+ end
589
+ out.push(" });")
590
+ mismatches = []
591
+ max_len = name2type.values.map { |n2t| n2t[:name].size }.max || 0
592
+ mismatches.push('a' * (max_len + 1))
593
+ min_len = name2type.values.map { |n2t| n2t[:name].size }.min || 0
594
+ mismatches.push('a' * (min_len - 1)) if min_len > 1
595
+ unless mismatches.empty?
596
+ response[:mismatches] = mismatches
597
+ out.push(" it('#{response[:base]}', function () {")
598
+ mismatches.each do |name|
599
+ response[:pass].each do |fn|
600
+ out.push(" expect(schemas.#{response[:base]}({ ...#{fn}(), #{name}: true } as any as #{response[:name]})).to.deep.equal(#{fn}());")
601
+ end
602
+ end
603
+ out.push(" });")
604
+ end
605
+ out.push("});")
606
+ out.push('')
607
+ end
608
+ Gen.output.join(out)
609
+ %>
610
+
611
+ // Operation response body types tests.
612
+ <%=
613
+ out = []
614
+ Gen.doc.dig('components', 'responses').each do |name, response|
615
+ name2type = response[:name2type]
616
+ next if name2type.nil?
617
+ content = response['content'] || {}
618
+ content.each do |media_type, mto|
619
+ next unless mto.key?(:name)
620
+ out.push("describe('#{mto[:name]}', function () {")
621
+ out.push(" it('#{mto[:is]}', function () {")
622
+ (mto[:pass] || []).each do |fn|
623
+ out.push(" expect(schemas.#{mto[:is]}(#{fn}())).to.be.true;")
624
+ end
625
+ (mto[:fail] || []).each do |fn|
626
+ out.push(" expect(schemas.#{mto[:is]}(#{fn}())).to.be.false;")
627
+ end
628
+ (mto[:typefail] || []).each do |fn|
629
+ out.push(" expect(schemas.#{mto[:is]}(#{fn}())).to.be.false;")
630
+ end
631
+ out.push(" });")
632
+ mismatches = response[:mismatches]
633
+ if mismatches.nil?
634
+ mismatches = %w[bod bodyy] # We know the added field name is body.
635
+ else
636
+ used = name2type.values.map { |n2t| n2t[:name] }
637
+ %w[bod bodyy].each do |cand|
638
+ mismatches.push(cand) unless used.include?(cand) or mismatches.include?(cand)
639
+ end
640
+ end
641
+ out.push(" it('#{mto[:base]}', function () {")
642
+ mismatches.each do |name|
643
+ (mto[:pass] || []).each do |fn|
644
+ out.push(" expect(schemas.#{mto[:base]}({ ...#{fn}(), #{name}: true } as any as #{mto[:name]})).to.deep.equal(#{fn}());")
645
+ end
646
+ end
647
+ out.push(" });")
648
+ out.push(" it('#{mto[:unknown]}', function () {")
649
+ (mto[:pass] || []).each do |fn|
650
+ out.push(" expect(schemas.#{mto[:unknown]}(#{fn}())).to.deep.equal(#{fn}());")
651
+ end
652
+ (mto[:fail] || []).each do |fn|
653
+ out.push(" expect(() => schemas.#{mto[:unknown]}(#{fn}())).to.throw;")
654
+ end
655
+ (mto[:typefail] || []).each do |fn|
656
+ out.push(" expect(() => schemas.#{mto[:unknown]}(#{fn}())).to.throw;")
657
+ end
658
+ out.push(" });")
659
+ out.push("});")
660
+ out.push('')
661
+ end
662
+ end
663
+ Gen.output.join(out)
664
+ %>