1
- import { TypeSystem } from '../../../system ' ;
1
+ import { ModuleType } from '../../../type/classes/ModuleType ' ;
2
2
import type { Type } from '../../../type' ;
3
3
4
- export const testBinaryCodegen = ( transcode : ( system : TypeSystem , type : Type , value : unknown ) => void ) => {
4
+ export const testBinaryCodegen = ( transcode : ( system : ModuleType , type : Type , value : unknown ) => void ) => {
5
5
describe ( '"any" type' , ( ) => {
6
6
test ( 'can encode any value - 1' , ( ) => {
7
- const system = new TypeSystem ( ) ;
7
+ const system = new ModuleType ( ) ;
8
8
const any = system . t . any ;
9
9
const value = { foo : 'bar' } ;
10
10
const decoded = transcode ( system , any , value ) ;
11
11
expect ( decoded ) . toStrictEqual ( value ) ;
12
12
} ) ;
13
13
14
14
test ( 'can encode any value - 2' , ( ) => {
15
- const system = new TypeSystem ( ) ;
15
+ const system = new ModuleType ( ) ;
16
16
const any = system . t . any ;
17
17
const value = 123 ;
18
18
const decoded = transcode ( system , any , value ) ;
@@ -22,15 +22,15 @@ export const testBinaryCodegen = (transcode: (system: TypeSystem, type: Type, va
22
22
23
23
describe ( '"con" type' , ( ) => {
24
24
test ( 'can encode number const' , ( ) => {
25
- const system = new TypeSystem ( ) ;
25
+ const system = new ModuleType ( ) ;
26
26
const any = system . t . Const < 123 > ( 123 ) ;
27
27
const value = { foo : 'bar' } ;
28
28
const decoded = transcode ( system , any , value ) ;
29
29
expect ( decoded ) . toStrictEqual ( 123 ) ;
30
30
} ) ;
31
31
32
32
test ( 'can encode array const' , ( ) => {
33
- const system = new TypeSystem ( ) ;
33
+ const system = new ModuleType ( ) ;
34
34
const any = system . t . Const ( < const > [ 1 , 2 , 3 ] ) ;
35
35
const decoded = transcode ( system , any , [ false , true , null ] ) ;
36
36
expect ( decoded ) . toStrictEqual ( [ 1 , 2 , 3 ] ) ;
@@ -39,7 +39,7 @@ export const testBinaryCodegen = (transcode: (system: TypeSystem, type: Type, va
39
39
40
40
describe ( '"bool" type' , ( ) => {
41
41
test ( 'can encode booleans' , ( ) => {
42
- const system = new TypeSystem ( ) ;
42
+ const system = new ModuleType ( ) ;
43
43
const any = system . t . bool ;
44
44
expect ( transcode ( system , any , true ) ) . toStrictEqual ( true ) ;
45
45
expect ( transcode ( system , any , false ) ) . toStrictEqual ( false ) ;
@@ -50,7 +50,7 @@ export const testBinaryCodegen = (transcode: (system: TypeSystem, type: Type, va
50
50
51
51
describe ( '"num" type' , ( ) => {
52
52
test ( 'can encode any number' , ( ) => {
53
- const system = new TypeSystem ( ) ;
53
+ const system = new ModuleType ( ) ;
54
54
const any = system . t . num ;
55
55
expect ( transcode ( system , any , 0 ) ) . toBe ( 0 ) ;
56
56
expect ( transcode ( system , any , 1 ) ) . toBe ( 1 ) ;
@@ -61,7 +61,7 @@ export const testBinaryCodegen = (transcode: (system: TypeSystem, type: Type, va
61
61
} ) ;
62
62
63
63
test ( 'can encode an integer' , ( ) => {
64
- const system = new TypeSystem ( ) ;
64
+ const system = new ModuleType ( ) ;
65
65
const any = system . t . num . options ( { format : 'i' } ) ;
66
66
expect ( transcode ( system , any , 0 ) ) . toBe ( 0 ) ;
67
67
expect ( transcode ( system , any , 1 ) ) . toBe ( 1 ) ;
@@ -71,7 +71,7 @@ export const testBinaryCodegen = (transcode: (system: TypeSystem, type: Type, va
71
71
} ) ;
72
72
73
73
test ( 'can encode an unsigned ints' , ( ) => {
74
- const system = new TypeSystem ( ) ;
74
+ const system = new ModuleType ( ) ;
75
75
const any = system . t . num . options ( { format : 'u8' } ) ;
76
76
expect ( transcode ( system , any , 0 ) ) . toBe ( 0 ) ;
77
77
expect ( transcode ( system , any , 1 ) ) . toBe ( 1 ) ;
@@ -80,7 +80,7 @@ export const testBinaryCodegen = (transcode: (system: TypeSystem, type: Type, va
80
80
} ) ;
81
81
82
82
test ( 'can encode an floats' , ( ) => {
83
- const system = new TypeSystem ( ) ;
83
+ const system = new ModuleType ( ) ;
84
84
const any = system . t . num . options ( { format : 'f' } ) ;
85
85
expect ( transcode ( system , any , 0 ) ) . toBe ( 0 ) ;
86
86
expect ( transcode ( system , any , 1 ) ) . toBe ( 1 ) ;
@@ -92,7 +92,7 @@ export const testBinaryCodegen = (transcode: (system: TypeSystem, type: Type, va
92
92
93
93
describe ( '"str" type' , ( ) => {
94
94
test ( 'can encode regular strings' , ( ) => {
95
- const system = new TypeSystem ( ) ;
95
+ const system = new ModuleType ( ) ;
96
96
const type = system . t . str ;
97
97
let value = '' ;
98
98
expect ( transcode ( system , type , value ) ) . toBe ( value ) ;
@@ -108,7 +108,7 @@ export const testBinaryCodegen = (transcode: (system: TypeSystem, type: Type, va
108
108
} ) ;
109
109
110
110
test ( 'can encode ascii strings' , ( ) => {
111
- const system = new TypeSystem ( ) ;
111
+ const system = new ModuleType ( ) ;
112
112
const type = system . t . str . options ( { ascii : true } ) ;
113
113
let value = '' ;
114
114
expect ( transcode ( system , type , value ) ) . toBe ( value ) ;
@@ -126,7 +126,7 @@ export const testBinaryCodegen = (transcode: (system: TypeSystem, type: Type, va
126
126
127
127
describe ( '"bin" type' , ( ) => {
128
128
test ( 'can encode binary data' , ( ) => {
129
- const system = new TypeSystem ( ) ;
129
+ const system = new ModuleType ( ) ;
130
130
const type = system . t . bin ;
131
131
let value = new Uint8Array ( ) ;
132
132
expect ( transcode ( system , type , value ) ) . toStrictEqual ( value ) ;
@@ -137,7 +137,7 @@ export const testBinaryCodegen = (transcode: (system: TypeSystem, type: Type, va
137
137
138
138
describe ( '"arr" type' , ( ) => {
139
139
test ( 'can encode simple arrays' , ( ) => {
140
- const system = new TypeSystem ( ) ;
140
+ const system = new ModuleType ( ) ;
141
141
const type = system . t . arr ;
142
142
let value : any [ ] = [ ] ;
143
143
expect ( transcode ( system , type , value ) ) . toStrictEqual ( value ) ;
@@ -146,7 +146,7 @@ export const testBinaryCodegen = (transcode: (system: TypeSystem, type: Type, va
146
146
} ) ;
147
147
148
148
test ( 'can encode array inside array' , ( ) => {
149
- const system = new TypeSystem ( ) ;
149
+ const system = new ModuleType ( ) ;
150
150
const type = system . t . Array ( system . t . arr ) ;
151
151
const value : any [ ] = [
152
152
[ 1 , 2 , 3 ] ,
@@ -157,46 +157,46 @@ export const testBinaryCodegen = (transcode: (system: TypeSystem, type: Type, va
157
157
} ) ;
158
158
159
159
test ( 'can encode array of strings' , ( ) => {
160
- const system = new TypeSystem ( ) ;
160
+ const system = new ModuleType ( ) ;
161
161
const type = system . t . Array ( system . t . str ) ;
162
162
const value : any [ ] = [ '1' , '2' , '3' ] ;
163
163
expect ( transcode ( system , type , value ) ) . toStrictEqual ( value ) ;
164
164
} ) ;
165
165
166
166
test ( 'can encode a simple tuple' , ( ) => {
167
- const system = new TypeSystem ( ) ;
167
+ const system = new ModuleType ( ) ;
168
168
const t = system . t ;
169
169
const type = system . t . Tuple ( [ t . str , t . num , t . bool ] ) ;
170
170
const value : any [ ] = [ 'abc' , 123 , true ] ;
171
171
expect ( transcode ( system , type , value ) ) . toStrictEqual ( value ) ;
172
172
} ) ;
173
173
174
174
test ( 'can encode an empty tuple' , ( ) => {
175
- const system = new TypeSystem ( ) ;
175
+ const system = new ModuleType ( ) ;
176
176
const t = system . t ;
177
177
const type = system . t . Tuple ( [ ] ) ;
178
178
const value : any [ ] = [ ] ;
179
179
expect ( transcode ( system , type , value ) ) . toStrictEqual ( value ) ;
180
180
} ) ;
181
181
182
182
test ( 'can encode a tuple of arrays' , ( ) => {
183
- const system = new TypeSystem ( ) ;
183
+ const system = new ModuleType ( ) ;
184
184
const t = system . t ;
185
185
const type = system . t . Tuple ( [ t . arr , t . arr ] ) ;
186
186
const value : any [ ] = [ [ ] , [ 1 , 'b' , false ] ] ;
187
187
expect ( transcode ( system , type , value ) ) . toStrictEqual ( value ) ;
188
188
} ) ;
189
189
190
190
test ( 'can encode a tuple tail' , ( ) => {
191
- const system = new TypeSystem ( ) ;
191
+ const system = new ModuleType ( ) ;
192
192
const t = system . t ;
193
193
const type = system . t . Tuple ( [ t . arr , t . arr ] , t . bool , [ t . str , t . num ] ) ;
194
194
const value : any [ ] = [ [ ] , [ 1 , 'b' , false ] , true , false , 'abc' , 123 ] ;
195
195
expect ( transcode ( system , type , value ) ) . toStrictEqual ( value ) ;
196
196
} ) ;
197
197
198
198
test ( 'elements and 2-tail' , ( ) => {
199
- const system = new TypeSystem ( ) ;
199
+ const system = new ModuleType ( ) ;
200
200
const t = system . t ;
201
201
const type = system . t . Tuple ( [ ] , t . bool , [ t . str , t . num ] ) ;
202
202
const value1 : any [ ] = [ true , false , 'abc' , 123 ] ;
@@ -210,7 +210,7 @@ export const testBinaryCodegen = (transcode: (system: TypeSystem, type: Type, va
210
210
} ) ;
211
211
212
212
test ( 'elements and 1-tail' , ( ) => {
213
- const system = new TypeSystem ( ) ;
213
+ const system = new ModuleType ( ) ;
214
214
const t = system . t ;
215
215
const type = system . t . Tuple ( [ ] , t . bool , [ t . num ] ) ;
216
216
const value1 : any [ ] = [ true , false , 123 ] ;
@@ -226,15 +226,15 @@ export const testBinaryCodegen = (transcode: (system: TypeSystem, type: Type, va
226
226
227
227
describe ( '"obj" type' , ( ) => {
228
228
test ( 'can encode empty object' , ( ) => {
229
- const system = new TypeSystem ( ) ;
229
+ const system = new ModuleType ( ) ;
230
230
const t = system . t ;
231
231
const type = t . obj ;
232
232
const value : any = { } ;
233
233
expect ( transcode ( system , type , value ) ) . toStrictEqual ( value ) ;
234
234
} ) ;
235
235
236
236
test ( 'can encode empty object, which has optional fields' , ( ) => {
237
- const system = new TypeSystem ( ) ;
237
+ const system = new ModuleType ( ) ;
238
238
const t = system . t ;
239
239
const type = t . Object ( t . propOpt ( 'field1' , t . str ) ) ;
240
240
const value1 : any = { } ;
@@ -244,7 +244,7 @@ export const testBinaryCodegen = (transcode: (system: TypeSystem, type: Type, va
244
244
} ) ;
245
245
246
246
test ( 'can encode fixed size object' , ( ) => {
247
- const system = new TypeSystem ( ) ;
247
+ const system = new ModuleType ( ) ;
248
248
const t = system . t ;
249
249
const type = t . Object ( t . prop ( 'field1' , t . str ) , t . prop ( 'field2' , t . num ) , t . prop ( 'bool' , t . bool ) ) ;
250
250
const value : any = {
@@ -256,7 +256,7 @@ export const testBinaryCodegen = (transcode: (system: TypeSystem, type: Type, va
256
256
} ) ;
257
257
258
258
test ( 'can encode object with an optional field' , ( ) => {
259
- const system = new TypeSystem ( ) ;
259
+ const system = new ModuleType ( ) ;
260
260
const t = system . t ;
261
261
const type = t . Object ( t . prop ( 'id' , t . str ) , t . propOpt ( 'name' , t . str ) ) ;
262
262
const value : any = {
@@ -267,7 +267,7 @@ export const testBinaryCodegen = (transcode: (system: TypeSystem, type: Type, va
267
267
} ) ;
268
268
269
269
test ( 'can encode object with a couple of optional fields' , ( ) => {
270
- const system = new TypeSystem ( ) ;
270
+ const system = new ModuleType ( ) ;
271
271
const t = system . t ;
272
272
const type = t . Object (
273
273
t . prop ( 'id' , t . str ) ,
@@ -285,7 +285,7 @@ export const testBinaryCodegen = (transcode: (system: TypeSystem, type: Type, va
285
285
} ) ;
286
286
287
287
test ( 'can encode object with unknown fields' , ( ) => {
288
- const system = new TypeSystem ( ) ;
288
+ const system = new ModuleType ( ) ;
289
289
const t = system . t ;
290
290
const type = t
291
291
. Object ( t . prop ( 'id' , t . str ) , t . propOpt ( 'name' , t . str ) , t . prop ( 'age' , t . num ) , t . propOpt ( 'address' , t . str ) )
@@ -301,7 +301,7 @@ export const testBinaryCodegen = (transcode: (system: TypeSystem, type: Type, va
301
301
} ) ;
302
302
303
303
test ( 'can encode nested objects' , ( ) => {
304
- const system = new TypeSystem ( ) ;
304
+ const system = new ModuleType ( ) ;
305
305
const t = system . t ;
306
306
const type = t
307
307
. Object (
@@ -349,7 +349,7 @@ export const testBinaryCodegen = (transcode: (system: TypeSystem, type: Type, va
349
349
} ) ;
350
350
351
351
test ( 'can encode object with only optional fields (encodeUnknownFields = true)' , ( ) => {
352
- const system = new TypeSystem ( ) ;
352
+ const system = new ModuleType ( ) ;
353
353
const t = system . t ;
354
354
const type = t
355
355
. Object ( t . propOpt ( 'id' , t . str ) , t . propOpt ( 'name' , t . str ) , t . propOpt ( 'address' , t . str ) )
@@ -376,7 +376,7 @@ export const testBinaryCodegen = (transcode: (system: TypeSystem, type: Type, va
376
376
} ) ;
377
377
378
378
test ( 'can encode object with only optional fields (encodeUnknownFields = false)' , ( ) => {
379
- const system = new TypeSystem ( ) ;
379
+ const system = new ModuleType ( ) ;
380
380
const t = system . t ;
381
381
const type = t
382
382
. Object ( t . propOpt ( 'id' , t . str ) , t . propOpt ( 'name' , t . str ) , t . propOpt ( 'address' , t . str ) )
@@ -405,31 +405,31 @@ export const testBinaryCodegen = (transcode: (system: TypeSystem, type: Type, va
405
405
406
406
describe ( '"map" type' , ( ) => {
407
407
test ( 'can encode empty map' , ( ) => {
408
- const system = new TypeSystem ( ) ;
408
+ const system = new ModuleType ( ) ;
409
409
const t = system . t ;
410
410
const type = t . map ;
411
411
const value : any = { } ;
412
412
expect ( transcode ( system , type , value ) ) . toStrictEqual ( value ) ;
413
413
} ) ;
414
414
415
415
test ( 'can encode empty map with one key' , ( ) => {
416
- const system = new TypeSystem ( ) ;
416
+ const system = new ModuleType ( ) ;
417
417
const t = system . t ;
418
418
const type = t . map ;
419
419
const value : any = { a : 'asdf' } ;
420
420
expect ( transcode ( system , type , value ) ) . toStrictEqual ( value ) ;
421
421
} ) ;
422
422
423
423
test ( 'can encode typed map with two keys' , ( ) => {
424
- const system = new TypeSystem ( ) ;
424
+ const system = new ModuleType ( ) ;
425
425
const t = system . t ;
426
426
const type = t . Map ( t . bool ) ;
427
427
const value : any = { x : true , y : false } ;
428
428
expect ( transcode ( system , type , value ) ) . toStrictEqual ( value ) ;
429
429
} ) ;
430
430
431
431
test ( 'can encode nested maps' , ( ) => {
432
- const system = new TypeSystem ( ) ;
432
+ const system = new ModuleType ( ) ;
433
433
const t = system . t ;
434
434
const type = t . Map ( t . Map ( t . bool ) ) ;
435
435
const value : any = { a : { x : true , y : false } } ;
@@ -439,7 +439,7 @@ export const testBinaryCodegen = (transcode: (system: TypeSystem, type: Type, va
439
439
440
440
describe ( '"ref" type' , ( ) => {
441
441
test ( 'can encode a simple reference' , ( ) => {
442
- const system = new TypeSystem ( ) ;
442
+ const system = new ModuleType ( ) ;
443
443
const t = system . t ;
444
444
system . alias ( 'Obj' , t . Object ( t . prop ( 'foo' , t . str ) ) ) ;
445
445
const type = t . Ref ( 'Obj' ) ;
@@ -451,7 +451,7 @@ export const testBinaryCodegen = (transcode: (system: TypeSystem, type: Type, va
451
451
452
452
describe ( '"or" type' , ( ) => {
453
453
test ( 'can encode a simple union type' , ( ) => {
454
- const system = new TypeSystem ( ) ;
454
+ const system = new ModuleType ( ) ;
455
455
const t = system . t ;
456
456
const type = system . t . Or ( t . str , t . num ) . options ( {
457
457
discriminator : [ 'if' , [ '==' , 'string' , [ 'type' , [ 'get' , '' ] ] ] , 0 , 1 ] ,
@@ -463,7 +463,7 @@ export const testBinaryCodegen = (transcode: (system: TypeSystem, type: Type, va
463
463
464
464
describe ( 'various' , ( ) => {
465
465
test ( 'encodes benchmark example' , ( ) => {
466
- const system = new TypeSystem ( ) ;
466
+ const system = new ModuleType ( ) ;
467
467
const t = system . t ;
468
468
const response = system . alias (
469
469
'Response' ,
@@ -522,7 +522,7 @@ export const testBinaryCodegen = (transcode: (system: TypeSystem, type: Type, va
522
522
} ) ;
523
523
524
524
test ( 'serializes according to schema a POJO object' , ( ) => {
525
- const system = new TypeSystem ( ) ;
525
+ const system = new ModuleType ( ) ;
526
526
const t = system . t ;
527
527
const type = t . Object (
528
528
t . prop ( 'a' , t . num ) ,
@@ -548,7 +548,7 @@ export const testBinaryCodegen = (transcode: (system: TypeSystem, type: Type, va
548
548
} ) ;
549
549
550
550
test ( 'supports "encodeUnknownFields" property' , ( ) => {
551
- const system = new TypeSystem ( ) ;
551
+ const system = new ModuleType ( ) ;
552
552
const t = system . t ;
553
553
const type = t . Object ( t . prop ( 'a' , t . Object ( ) . options ( { encodeUnknownKeys : true } ) ) ) ;
554
554
const value = {
@@ -561,7 +561,7 @@ export const testBinaryCodegen = (transcode: (system: TypeSystem, type: Type, va
561
561
} ) ;
562
562
563
563
test ( 'supports "encodeUnknownFields" property' , ( ) => {
564
- const system = new TypeSystem ( ) ;
564
+ const system = new ModuleType ( ) ;
565
565
const t = system . t ;
566
566
const type = t . Object ( t . prop ( 'a' , t . num ) , t . propOpt ( 'b' , t . num ) , t . prop ( 'c' , t . bool ) , t . propOpt ( 'd' , t . nil ) ) ;
567
567
const json1 = {
@@ -588,7 +588,7 @@ export const testBinaryCodegen = (transcode: (system: TypeSystem, type: Type, va
588
588
} ) ;
589
589
590
590
test ( 'supports "encodeUnknownFields" property' , ( ) => {
591
- const system = new TypeSystem ( ) ;
591
+ const system = new ModuleType ( ) ;
592
592
const t = system . t ;
593
593
const type = t . Object (
594
594
t . prop (
0 commit comments