-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathkernel.test.ts
More file actions
1336 lines (1066 loc) · 56.8 KB
/
kernel.test.ts
File metadata and controls
1336 lines (1066 loc) · 56.8 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import childProcess = require('child_process');
import fs = require('fs-extra');
import { join } from 'path';
import path = require('path');
import vm = require('vm');
import { api, Kernel } from '../lib';
import { Callback, ObjRef, TOKEN_REF, TOKEN_INTERFACES, TOKEN_MAP, WireStruct, TOKEN_STRUCT } from '../lib/api';
import { closeRecording, recordInteraction } from './recording';
/* eslint-disable require-atomic-updates */
// extract versions of fixtures
const calcBaseVersion = require('@scope/jsii-calc-base/package.json').version.replace(/\+.+$/, '');
const calcLibVersion = require('@scope/jsii-calc-lib/package.json').version.replace(/\+.+$/, '');
const calcVersion = require('jsii-calc/package.json').version.replace(/\+.+$/, '');
// Do this so that regexes stringify nicely in approximate tests
(RegExp.prototype as any).toJSON = function() { return this.source; };
process.setMaxListeners(9999); // since every kernel instance adds an `on('exit')` handler.
process.on('unhandledRejection', e => {
console.error((e as Error).stack);
process.exit(1);
});
jest.setTimeout(60_000);
const recordingOutput = process.env.JSII_RECORD;
if (recordingOutput) {
fs.mkdirpSync(recordingOutput);
console.error(`JSII_RECORD=${recordingOutput}`);
}
function defineTest(name: string, method: (sandbox: Kernel) => Promise<any> | any, testFunc = test) {
const recording = name.replace(/[^A-Za-z]/g, '_');
testFunc(name, async () => {
const kernel = await createCalculatorSandbox(recording);
await method(kernel);
return closeRecording(kernel);
});
}
defineTest.skip = function (name: string, method: (sandbox: Kernel) => Promise<any> | any) {
return defineTest(name, method, test.skip);
}
defineTest('stats() return sandbox statistics', (sandbox) => {
const stats = sandbox.stats({ });
expect(stats.objectCount).toBe(0);
for (let i = 0; i < 100; ++i) {
sandbox.create({ fqn: '@scope/jsii-calc-lib.Number', args: [i] });
}
expect(sandbox.stats({ }).objectCount).toBe(100);
});
defineTest('deleteObject will remove the reference', (sandbox) => {
const objects = new Array<any>();
for (let i = 0; i < 100; ++i) {
objects.push(sandbox.create({ fqn: '@scope/jsii-calc-lib.Number', args: [i] }));
}
expect(sandbox.stats({ }).objectCount).toBe(100);
for (let i = 0; i < 50; ++i) {
sandbox.del({ objref: objects[i] });
}
expect(sandbox.stats({ }).objectCount).toBe(50);
expect(() => sandbox.get({ objref: objects[10], property: 'value' })).toThrow();
});
defineTest('in/out primitive types', (sandbox) => {
const alltypes = sandbox.create({ fqn: 'jsii-calc.AllTypes', args: [] });
sandbox.set({ objref: alltypes, property: 'booleanProperty', value: true });
expect(sandbox.get({ objref: alltypes, property: 'booleanProperty' }).value).toBe(true);
sandbox.set({ objref: alltypes, property: 'booleanProperty', value: false });
expect(sandbox.get({ objref: alltypes, property: 'booleanProperty' }).value).toBe(false);
sandbox.set({ objref: alltypes, property: 'stringProperty', value: 'hello' });
expect(sandbox.get({ objref: alltypes, property: 'stringProperty' }).value).toBe('hello');
sandbox.set({ objref: alltypes, property: 'numberProperty', value: 123 });
expect(sandbox.get({ objref: alltypes, property: 'numberProperty' }).value).toBe(123);
// in -> out for an ANY
const num = sandbox.create({ fqn: '@scope/jsii-calc-lib.Number', args: [444] });
sandbox.set({ objref: alltypes, property: 'anyProperty', value: num });
expect(sandbox.get({ objref: alltypes, property: 'anyProperty' }).value).toEqual(num);
// out -> in for an ANY
const ret = sandbox.invoke({ objref: alltypes, method: 'anyOut' }).result;
sandbox.invoke({ objref: alltypes, method: 'anyIn', args: [ret] });
});
defineTest('in/out objects', (sandbox) => {
const alltypes = sandbox.create({ fqn: 'jsii-calc.AllTypes' });
const num = sandbox.create({ fqn: '@scope/jsii-calc-lib.Number', args: [444] });
sandbox.set({ objref: alltypes, property: 'anyProperty', value: num });
expect(sandbox.get({ objref: alltypes, property: 'anyProperty' }).value).toEqual(num);
});
defineTest('in/out collections', (sandbox) => {
const alltypes = sandbox.create({ fqn: 'jsii-calc.AllTypes', args: [] });
const array = ['1', '2', '3', '4'];
sandbox.set({ objref: alltypes, property: 'arrayProperty', value: array });
expect(sandbox.get({ objref: alltypes, property: 'arrayProperty' }).value).toEqual(array);
const num = create(sandbox, '@scope/jsii-calc-lib.Number');
const map = {
a: num(12),
b: num(33),
c: num(33),
d: num(123),
};
sandbox.set({ objref: alltypes, property: 'mapProperty', value: { [TOKEN_MAP]: map } });
expect(sandbox.get({ objref: alltypes, property: 'mapProperty' }).value[TOKEN_MAP]).toEqual(map);
});
defineTest('in/out date values', (sandbox) => {
const alltypes = sandbox.create({ fqn: 'jsii-calc.AllTypes' });
const date = new Date('2018-01-18T00:00:32.347Z');
sandbox.set({ objref: alltypes, property: 'dateProperty', value: { [api.TOKEN_DATE]: date.toISOString() } });
expect(sandbox.get({ objref: alltypes, property: 'dateProperty' }).value).toEqual({ [api.TOKEN_DATE]: '2018-01-18T00:00:32.347Z' });
});
defineTest('in/out enum values', (sandbox) => {
const alltypes = sandbox.create({ fqn: 'jsii-calc.AllTypes' });
sandbox.set({ objref: alltypes, property: 'enumProperty', value: { [api.TOKEN_ENUM]: 'jsii-calc.AllTypesEnum/MY_ENUM_VALUE' } });
expect(sandbox.get({ objref: alltypes, property: 'enumPropertyValue' }).value).toBe(0);
sandbox.set({ objref: alltypes, property: 'enumProperty', value: { [api.TOKEN_ENUM]: 'jsii-calc.AllTypesEnum/YOUR_ENUM_VALUE' } });
expect(sandbox.get({ objref: alltypes, property: 'enumPropertyValue' }).value).toBe(100);
sandbox.set({ objref: alltypes, property: 'enumProperty', value: { [api.TOKEN_ENUM]: 'jsii-calc.AllTypesEnum/THIS_IS_GREAT' } });
expect(sandbox.get({ objref: alltypes, property: 'enumPropertyValue' }).value).toBe(101);
expect(sandbox.get({ objref: alltypes, property: 'enumProperty' }).value).toEqual({ '$jsii.enum': 'jsii-calc.AllTypesEnum/THIS_IS_GREAT' });
});
defineTest('enum values from @scoped packages awslabs/jsii#138', (sandbox) => {
const objref = sandbox.create({ fqn: 'jsii-calc.ReferenceEnumFromScopedPackage' });
const value = sandbox.get({ objref, property: 'foo' });
expect(value).toEqual({ value: { '$jsii.enum': '@scope/jsii-calc-lib.EnumFromScopedModule/VALUE2' } });
sandbox.set({ objref, property: 'foo', value: { '$jsii.enum': '@scope/jsii-calc-lib.EnumFromScopedModule/VALUE1' } });
const ret = sandbox.invoke({ objref, method: 'loadFoo' });
expect(ret).toEqual({ result: { '$jsii.enum': '@scope/jsii-calc-lib.EnumFromScopedModule/VALUE1' } });
sandbox.invoke({ objref, method: 'saveFoo', args: [{ '$jsii.enum': '@scope/jsii-calc-lib.EnumFromScopedModule/VALUE2' }] });
const value2 = sandbox.get({ objref, property: 'foo' });
expect(value2).toEqual({ value: { '$jsii.enum': '@scope/jsii-calc-lib.EnumFromScopedModule/VALUE2' } });
});
defineTest('fails for invalid enum member name', (sandbox) => {
const objref = sandbox.create({ fqn: 'jsii-calc.ReferenceEnumFromScopedPackage' });
expect(() => {
sandbox.set({ objref, property: 'foo', value: { '$jsii.enum': '@scope/jsii-calc-lib.EnumFromScopedModule/ValueX' } });
}).toThrow(/No enum member named ValueX/);
});
defineTest('set for a non existing property', (sandbox) => {
const obj = sandbox.create({ fqn: 'jsii-calc.SyncVirtualMethods' });
expect(() => sandbox.set({ objref: obj, property: 'idontexist', value: 'Foo' })).toThrow();
});
defineTest('set for a readonly property', (sandbox) => {
const obj = sandbox.create({ fqn: 'jsii-calc.SyncVirtualMethods' });
expect(() => sandbox.set({ objref: obj, property: 'readonlyProperty', value: 'Foo' })).toThrow();
});
defineTest('create object with ctor overloads', (sandbox) => {
sandbox.create({ fqn: 'jsii-calc.Calculator' });
sandbox.create({ fqn: 'jsii-calc.Calculator', args: [{ initialValue: 100 }] });
});
defineTest('objects created inside the sandbox are returned with type info and new objid', (sandbox) => {
const calc = sandbox.create({ fqn: 'jsii-calc.Calculator', args: [{ initialValue: 100 }] });
sandbox.invoke({ objref: calc, method: 'add', args: [50] });
const add = sandbox.get({ objref: calc, property: 'curr' }).value;
expect(sandbox.get({ objref: add, property: 'value' }).value).toBe(150);
});
defineTest('naming allows returns the module name for different languages', (sandbox) => {
expect(sandbox.naming({ assembly: 'jsii-calc' }).naming).toEqual({
dotnet: {
iconUrl: 'https://sdk-for-net.amazonwebservices.com/images/AWSLogo128x128.png',
namespace: 'Amazon.JSII.Tests.CalculatorNamespace',
packageId: 'Amazon.JSII.Tests.CalculatorPackageId',
},
java: {
package: 'software.amazon.jsii.tests.calculator',
maven: { groupId: 'software.amazon.jsii.tests', artifactId: 'calculator' }
},
js: { npm: 'jsii-calc' },
python: { distName: 'jsii-calc', module: 'jsii_calc' },
});
expect(sandbox.naming({ assembly: '@scope/jsii-calc-lib' }).naming).toEqual({
dotnet: {
namespace: 'Amazon.JSII.Tests.CalculatorNamespace.LibNamespace',
packageId: 'Amazon.JSII.Tests.CalculatorPackageId.LibPackageId',
versionSuffix: '-devpreview'
},
java: {
package: 'software.amazon.jsii.tests.calculator.lib',
maven: { groupId: 'software.amazon.jsii.tests', artifactId: 'calculator-lib', versionSuffix: '.DEVPREVIEW' },
},
js: { npm: '@scope/jsii-calc-lib' },
python: { distName: 'scope.jsii-calc-lib', module: 'scope.jsii_calc_lib' },
});
});
defineTest('collection of objects', (sandbox) => {
const sum = sandbox.create({ fqn: 'jsii-calc.Sum' });
const n1 = sandbox.create({ fqn: '@scope/jsii-calc-lib.Number', args: [10] });
const n2 = sandbox.create({ fqn: '@scope/jsii-calc-lib.Number', args: [4] });
const n3 = sandbox.create({ fqn: 'jsii-calc.Power', args: [n1, n2] });
sandbox.set({ objref: sum, property: 'parts', value: [n1, n2, n3] });
const parts = sandbox.get({ objref: sum, property: 'parts' }).value;
expect(sandbox.get({ objref: parts[0], property: 'value' }).value).toBe(10);
expect(sandbox.get({ objref: parts[1], property: 'value' }).value).toBe(4);
expect(sandbox.get({ objref: parts[2], property: 'value' }).value).toBe(Math.pow(10, 4));
const expr = sandbox.get({ objref: sum, property: 'expression' }).value;
expect(sandbox.invoke({ objref: expr, method: 'toString' }).result).toBe('(((0 + 10) + 4) + ((((1 * 10) * 10) * 10) * 10))');
expect(sandbox.get({ objref: expr, property: 'value' }).value).toBe(10014);
});
defineTest('class not found', (sandbox) => {
expect(() => sandbox.create({ fqn: 'NotFound', args: [] })).toThrow();
});
defineTest('type-checking: method and property names are validated against class and base classes', (sandbox) => {
const obj = sandbox.create({ fqn: 'jsii-calc.Calculator' });
sandbox.get({ objref: obj, property: 'stringStyle' }); // property from CompositeOperation
sandbox.get({ objref: obj, property: 'value' }); // property from Value
sandbox.get({ objref: obj, property: 'curr' }); // property from Calculator
sandbox.invoke({ objref: obj, method: 'typeName' }); // method from Value
sandbox.invoke({ objref: obj, method: 'add', args: [1] }); // method from Calculator
expect(() => sandbox.get({ objref: obj, property: 'notFound' })).toThrow();
expect(() => sandbox.invoke({ objref: obj, method: 'boo', args: [] })).toThrow();
});
defineTest('type-checking; module not found', (sandbox) => {
expect(() => sandbox.create({ fqn: 'jsii$boo.Foo' })).toThrow(/Module 'jsii\$boo' not found/);
});
defineTest('type-checking: type not found', (sandbox) => {
expect(() => sandbox.create({ fqn: 'jsii-calc.Unknown' })).toThrow(/Type 'jsii-calc.Unknown' not found/);
});
defineTest('type-checking: try to create an object from a non-class type', (sandbox) => {
expect(() => sandbox.create({ fqn: 'jsii-calc.AllTypesEnum' })).toThrow(/Unexpected FQN kind/);
});
defineTest('type-checking: argument count in methods and initializers', (sandbox) => {
// ctor has one optional argument
sandbox.create({ fqn: 'jsii-calc.Calculator' });
sandbox.create({ fqn: 'jsii-calc.Calculator', args: [{}] });
// but we expect an error if more arguments are passed
expect(() => sandbox.create({ fqn: 'jsii-calc.Calculator', args: [1, 2, 3] })).toThrow(/Too many arguments/);
expect(() => sandbox.create({ fqn: 'jsii-calc.Calculator', args: [1, 2, 3, 4] })).toThrow(/Too many arguments/);
expect(() => sandbox.create({ fqn: 'jsii-calc.Add', args: [] })).toThrow(/Not enough arguments/);
expect(() => sandbox.create({ fqn: 'jsii-calc.Add', args: [1] })).toThrow(/Not enough arguments/);
const obj = sandbox.create({ fqn: 'jsii-calc.RuntimeTypeChecking' });
expect(() => sandbox.invoke({ objref: obj, method: 'methodWithOptionalArguments', args: [] })).toThrow(/Not enough arguments/);
expect(() => sandbox.invoke({ objref: obj, method: 'methodWithOptionalArguments', args: [1] })).toThrow(/Not enough arguments/);
sandbox.invoke({ objref: obj, method: 'methodWithOptionalArguments', args: [1, 'hello'] });
sandbox.invoke({ objref: obj, method: 'methodWithOptionalArguments', args: [1, 'hello', { [api.TOKEN_DATE]: new Date().toISOString() }] });
expect(() => sandbox.invoke({ objref: obj, method: 'methodWithOptionalArguments', args: [1, 'hello', { [api.TOKEN_DATE]: new Date().toISOString() }, 'too much'] })).toThrow(/Too many arguments/);
});
defineTest('verify object literals are converted to real classes', (sandbox) => {
const obj = sandbox.create({ fqn: 'jsii-calc.JSObjectLiteralToNative' });
const obj2 = sandbox.invoke({ objref: obj, method: 'returnLiteral' }).result;
expect(obj2[api.TOKEN_REF]).toBeTruthy(); // verify that we received a ref as a result;
const objid: string = obj2[api.TOKEN_REF];
expect(objid.startsWith('jsii-calc.JSObjectLiteralToNativeClass@'), `${objid} does not have the intended prefix`).toBeTruthy(); // verify the type of the returned object'
});
defineTest('get a property from an type that only has base class properties', (sandbox) => {
const obj = sandbox.create({ fqn: 'jsii-calc.DerivedClassHasNoProperties.Derived' });
sandbox.set({ objref: obj, property: 'prop', value: 'hi' });
expect(sandbox.get({ objref: obj, property: 'prop' }).value).toBe('hi');
});
defineTest('async overrides: ignores overrides for unknown methods (to allow derived class to just pass all local method names)', (sandbox) => {
sandbox.create({ fqn: 'jsii-calc.AsyncVirtualMethods', overrides: [{ method: 'notFound' }] });
});
defineTest('async overrides: override a method', async (sandbox) => {
// first call without an override and expect pendingCallbacks to return
// an empty array.
const obj1 = sandbox.create({ fqn: 'jsii-calc.AsyncVirtualMethods' });
async function callWithOverride(overrideCallback: (x: number) => number) {
const promise1 = sandbox.begin({ objref: obj1, method: 'callMe' });
expect(sandbox.callbacks().callbacks.length).toBe(0);
const result1 = (await sandbox.end(promise1)).result;
expect(result1).toBe(128);
// now add an override and complete it with a some value.
const obj = sandbox.create({ fqn: 'jsii-calc.AsyncVirtualMethods', overrides: [{ method: 'overrideMe', cookie: 'myCookie' }] });
const promise2 = sandbox.begin({ objref: obj, method: 'callMe' });
const callbacks = sandbox.callbacks().callbacks;
expect(callbacks.length).toBe(1);
const cb = callbacks[0];
expect(cb.invoke).toBeTruthy();
expect(cb.invoke!.objref).toEqual(obj);
expect(cb.invoke!.method).toBe('overrideMe');
expect(cb.cookie).toBe('myCookie');
expect(cb.invoke!.args).toEqual([10]);
// calling pendingCallbacks again will return zero results
// since all callbacks are moved to "waiting" state
expect(sandbox.callbacks().callbacks.length).toBe(0);
// call the override
let callbackResult;
let callbackError;
try {
callbackResult = overrideCallback(cb.invoke!.args![0]);
} catch (e) {
callbackError = e.message;
}
// complete the callback with a successful return value of 600
sandbox.complete({ cbid: cb.cbid, err: callbackError, result: callbackResult });
// await for the promise and verify that the completion was successfully
// conveyed from the callback.
return (await sandbox.end(promise2)).result;
}
expect(await callWithOverride(_ => 600)).toBe(608);
expect(await callWithOverride(x => 2 * x)).toBe(28);
// override throws
return expect(callWithOverride(_ => { throw new Error('Bla'); })).rejects.toThrow('Bla');
});
defineTest('async overrides: directly call a method with an override from native code should invoke the "super.method" since it can only be done by the derived class', async (sandbox) => {
const obj = sandbox.create({ fqn: 'jsii-calc.AsyncVirtualMethods', overrides: [{ method: 'overrideMe', cookie: 'myCookie' }] });
const promise = sandbox.begin({ objref: obj, method: 'overrideMe', args: [12] });
// no callbacks should be pending, since this should invoke "super"
expect(sandbox.callbacks().callbacks.length).toBe(0);
expect((await sandbox.end(promise)).result).toBe(144);
});
defineTest('async overrides: two overrides', async (sandbox) => {
const obj = sandbox.create({ fqn: 'jsii-calc.AsyncVirtualMethods', overrides: [
{ method: 'overrideMeToo', cookie: 'cookie1' },
{ method: 'overrideMe', cookie: 'cookie2' }
] });
const promise = sandbox.begin({ objref: obj, method: 'callMe' });
const callbacks1 = sandbox.callbacks();
expect(callbacks1.callbacks.length).toBe(1);
sandbox.complete({ cbid: callbacks1.callbacks[0].cbid, result: 666 });
await processPendingPromises(sandbox); // processing next promise
const callbacks2 = sandbox.callbacks();
expect(callbacks2.callbacks.length).toBe(1);
sandbox.complete({ cbid: callbacks2.callbacks[0].cbid, result: 101 });
await processPendingPromises(sandbox);
const result = await sandbox.end({ promiseid: promise.promiseid });
expect(result.result).toBe(775);
});
/**
* This test simulates a sitation where an async method is called, which invokes
* an async override in return, but the override's promise is not called synchronously
* but rather within the "next tick". This can happen (at the discression of the runtime
* and we should verify that the jsii-runtime (or any runtime for that matter) "processes"
* promises after a "begin" invocation in order to make sure any callbacks are queued.
*
* This is also relevant immediate after a "complete" invocation, which fulfills an async
* promise and may subsequently queue more callbacks before one can "end" the original
* async request.
*
* Yes, this is hairy.
*
* host.ts#93 is where this is implemented in the runtime.
*/
defineTest('async overrides - process promises after "begin"', async (sandbox) => {
const obj = sandbox.create({ fqn: 'jsii-calc.AsyncVirtualMethods', overrides: [
{ method: 'overrideMe', cookie: 'cookie2' },
{ method: 'overrideMeToo' }
] });
const p1 = sandbox.begin({ objref: obj, method: 'callMeDoublePromise' });
// this is needed in order to cycle through another event loop so
// that promises that are lazily called will be processed (nothing ensures
// that the promise callback will be invokes synchronously).
await processPendingPromises(sandbox);
const callbacks1 = sandbox.callbacks();
expect(callbacks1.callbacks.length).toBe(1);
// --------------- start of "override" execution
// we simulate the situation where the override invokes another async override
// call a sync method (just for fun)
const out = sandbox.invoke({ objref: obj, method: 'dontOverrideMe' });
expect(out).toEqual({ result: 8 });
// call another overridden async method
const p2 = sandbox.begin({ objref: obj, method: 'callMe2' });
// we should get a a callback to overrideMe2
const callback2 = sandbox.callbacks();
expect(callback2.callbacks.length).toBe(1);
expect(callback2.callbacks[0].invoke!.method).toBe('overrideMeToo');
// complete the inner callback
sandbox.complete({ cbid: callback2.callbacks[0].cbid, result: 9999 });
// fetch it
expect(await sandbox.end({ promiseid: p2.promiseid })).toEqual({ result: 9999 });
// no more callbacks
expect(sandbox.callbacks()).toEqual({ callbacks: [] });
// now complete the outer callback
sandbox.complete({ cbid: callbacks1.callbacks[0].cbid, result: 8888 });
// required: process pending promises so that we will get the next one in the callbacks list
await processPendingPromises(sandbox);
// ------ end of execution of "overrideMe"
// now we expect 'overrideMeToo' to be called back
const cb3 = sandbox.callbacks();
expect(cb3.callbacks.length).toBe(1);
expect(cb3.callbacks[0].invoke!.method).toBe('overrideMeToo');
// complete it
sandbox.complete({ cbid: cb3.callbacks[0].cbid, result: -20 });
// no more callbacks
const cb4 = sandbox.callbacks();
expect(cb4.callbacks.length).toBe(0);
const result = await sandbox.end({ promiseid: p1.promiseid });
expect(result.result).toBe(8876);
});
function processPendingPromises(sandbox: Kernel) {
return vm.runInContext('new Promise(done => setImmediate(done));', (sandbox as any).sandbox);
}
defineTest('sync overrides', async (sandbox) => {
const pre = sandbox.create({ fqn: 'jsii-calc.SyncVirtualMethods' });
// without override
expect(sandbox.invoke({ objref: pre, method: 'callerIsMethod' }).result).toBe(20);
expect(sandbox.get({ objref: pre, property: 'callerIsProperty' }).value).toBe(20);
const p = sandbox.begin({ objref: pre, method: 'callerIsAsync' });
expect((await sandbox.end({ promiseid: p.promiseid })).result).toBe(20);
sandbox.callbackHandler = makeSyncCallbackHandler(callback => {
expect(callback.cookie).toBe('myCookie');
expect(callback.invoke!.objref[TOKEN_REF]).toMatch(/jsii-calc\.SyncVirtualMethods/);
expect(callback.invoke!.method).toBe('virtualMethod');
expect(callback.invoke!.args![0]).toBe(10);
// to make things a bit more interesting, let's interact with a jsii object
// from within the callback
const lhs = sandbox.create({ fqn: '@scope/jsii-calc-lib.Number', args: [callback.invoke!.args![0]] });
const rhs = sandbox.create({ fqn: '@scope/jsii-calc-lib.Number', args: [12] });
const add = sandbox.create({ fqn: 'jsii-calc.Add', args: [lhs, rhs] });
return sandbox.get({ objref: add, property: 'value' }).value;
});
// now make the same set of calls, and you will notice that the results are affected by the override.
const obj = sandbox.create({ fqn: 'jsii-calc.SyncVirtualMethods', overrides: [{ method: 'virtualMethod', cookie: 'myCookie' }] });
expect(sandbox.invoke({ objref: obj, method: 'callerIsMethod' }).result).toBe(22);
expect(sandbox.get({ objref: obj, property: 'callerIsProperty' }).value).toBe(22);
// verify callbacks can also be called by setters.
let called = false;
sandbox.callbackHandler = makeSyncCallbackHandler(callback => {
expect(callback.invoke!.args![0]).toBe(999);
called = true;
return callback.invoke!.args![0];
});
sandbox.set({ objref: obj, property: 'callerIsProperty', value: 999 });
expect(called).toBeTruthy();
});
defineTest('sync overrides with async caller', async (sandbox) => {
sandbox.callbackHandler = makeSyncCallbackHandler(callback => {
expect(callback.cookie).toBe('myCookie');
expect(callback.invoke!.objref[TOKEN_REF]).toMatch(/jsii-calc\.SyncVirtualMethods/);
expect(callback.invoke!.method).toBe('virtualMethod');
expect(callback.invoke!.args![0]).toBe(10);
// to make things a bit more interesting, let's interact with a jsii object
// from within the callback
const lhs = sandbox.create({ fqn: '@scope/jsii-calc-lib.Number', args: [callback.invoke!.args![0]] });
const rhs = sandbox.create({ fqn: '@scope/jsii-calc-lib.Number', args: [12] });
const add = sandbox.create({ fqn: 'jsii-calc.Add', args: [lhs, rhs] });
return sandbox.get({ objref: add, property: 'value' }).value;
});
const obj = sandbox.create({ fqn: 'jsii-calc.SyncVirtualMethods', overrides: [{ method: 'virtualMethod', cookie: 'myCookie' }] });
const p2 = sandbox.begin({ objref: obj, method: 'callerIsAsync' });
expect((await sandbox.end({ promiseid: p2.promiseid })).result).toBe(22);
});
defineTest('sync overrides: properties - readwrite', (sandbox) => {
const obj = sandbox.create({ fqn: 'jsii-calc.SyncVirtualMethods', overrides: [{ property: 'theProperty', cookie: 'myCookie1234' }] });
let setValue;
sandbox.callbackHandler = makeSyncCallbackHandler(callback => {
expect(callback.cookie).toBe('myCookie1234');
if (callback.get) {
expect(callback.get.property).toBe('theProperty');
return 'override applied';
} else if (callback.set) {
expect(callback.set.property).toBe('theProperty');
setValue = callback.set.value;
return undefined;
}
throw new Error('Invalid callback. Expected get/set');
});
const value = sandbox.invoke({ objref: obj, method: 'retrieveValueOfTheProperty' });
expect(value).toEqual({ result: 'override applied' });
// make sure we can still set the property
sandbox.invoke({ objref: obj, method: 'modifyValueOfTheProperty', args: ['1234'] });
expect(setValue).toBe('1234');
});
defineTest('sync overrides: properties - readwrite (backed by functions)', (sandbox) => {
const obj = sandbox.create({
fqn: 'jsii-calc.SyncVirtualMethods',
overrides: [{ property: 'otherProperty', cookie: 'myCookie1234' }]
});
let setValue;
sandbox.callbackHandler = makeSyncCallbackHandler(callback => {
expect(callback.cookie).toBe('myCookie1234');
if (callback.get) {
expect(callback.get.property).toBe('otherProperty');
return 'override applied for otherProperty';
} else if (callback.set) {
expect(callback.set.property).toBe('otherProperty');
setValue = callback.set.value;
return undefined;
}
throw new Error('Invalid callback. Expected get/set');
});
const value = sandbox.invoke({ objref: obj, method: 'retrieveOtherProperty' });
expect(value).toEqual({ result: 'override applied for otherProperty' });
// make sure we can still set the property
sandbox.invoke({ objref: obj, method: 'modifyOtherProperty', args: ['778877'] });
expect(setValue).toBe('778877');
});
defineTest('sync overrides: duplicate overrides for the same property', (sandbox) => {
expect(() => sandbox.create({
fqn: 'jsii-calc.SyncVirtualMethods',
overrides: [
{ property: 'otherProperty', cookie: 'myCookie1234' },
{ property: 'otherProperty', cookie: 'yourCookie' }
]
})).toThrow();
});
defineTest('sync overrides: properties - readonly', (sandbox) => {
const obj = sandbox.create({ fqn: 'jsii-calc.SyncVirtualMethods', overrides: [{ property: 'readonlyProperty', cookie: 'myCookie1234' }] });
sandbox.callbackHandler = makeSyncCallbackHandler(callback => {
expect(callback.cookie).toBe('myCookie1234');
expect(callback.get).toBeTruthy();
expect(callback.get!.property).toBe('readonlyProperty');
return 'override for readonly property applied';
});
const value = sandbox.invoke({ objref: obj, method: 'retrieveReadOnlyProperty' });
expect(value).toEqual({ result: 'override for readonly property applied' });
// can't set the value of a readonly property, dah!
expect(() => sandbox.set({ objref: obj, property: 'readonlyProperty', value: 1234 })).toThrow();
});
defineTest('sync overrides: properties - get calls super', (sandbox) => {
const obj = sandbox.create({ fqn: 'jsii-calc.SyncVirtualMethods', overrides: [{ property: 'theProperty' }] });
sandbox.callbackHandler = makeSyncCallbackHandler(callback => {
expect(callback.get!.property).toBe('theProperty');
const superValue = sandbox.get({ objref: obj, property: 'theProperty' });
return `override, super=${superValue.value}`;
});
const value = sandbox.invoke({ objref: obj, method: 'retrieveValueOfTheProperty' });
expect(value).toEqual({ result: 'override, super=initial value' });
});
defineTest('sync overrides: properties - set calls super', (sandbox) => {
const obj = sandbox.create({ fqn: 'jsii-calc.SyncVirtualMethods', overrides: [{ property: 'theProperty' }] });
sandbox.callbackHandler = makeSyncCallbackHandler(callback => {
if (callback.get) {
return sandbox.get({ objref: obj, property: 'theProperty' }).value;
}
if (callback.set) {
return sandbox.set({ objref: obj, property: 'theProperty', value: `set by override: ${callback.set.value}` });
}
throw new Error(`Unexpected callback request: ${JSON.stringify(callback)}`);
});
sandbox.invoke({ objref: obj, method: 'modifyValueOfTheProperty', args: ['new_value'] });
const value = sandbox.invoke({ objref: obj, method: 'retrieveValueOfTheProperty' });
expect(value).toEqual({ result: 'set by override: new_value' });
});
defineTest('sync overrides: properties - verify keys are enumerable', (sandbox) => {
const obj = sandbox.create({ fqn: 'Object', overrides: [{ property: 'foo' }, { property: 'readOnlyString' }] });
const reader = sandbox.create({ fqn: 'jsii-calc.UsesInterfaceWithProperties', args: [obj] });
sandbox.callbackHandler = makeSyncCallbackHandler(callback => {
if (callback.get && callback.get.property === 'foo') {
return 999;
}
if (callback.get && callback.get.property === 'readOnlyString') {
return 'STR';
}
throw new Error('Unexpected callback');
});
const result = sandbox.invoke({ objref: reader, method: 'readStringAndNumber', args: [obj] });
expect(result).toEqual({ result: 'base=STR child=999 keys=[foo,readOnlyString]' });
});
defineTest('sync overrides: returns an object', (sandbox) => {
const returnsNumber = sandbox.create({ fqn: 'Object', overrides: [{ method: 'obtainNumber' }, { property: 'numberProp' }] });
const obj = sandbox.create({ fqn: 'jsii-calc.OverrideReturnsObject' });
const number100 = sandbox.create({ fqn: '@scope/jsii-calc-lib.Number', args: [100] });
const number500 = sandbox.create({ fqn: '@scope/jsii-calc-lib.Number', args: [500] });
sandbox.callbackHandler = makeSyncCallbackHandler(callback => {
if (callback.invoke && callback.invoke.method === 'obtainNumber') {
return number100;
}
if (callback.get && callback.get.property === 'numberProp') {
return number500;
}
throw new Error(`Unexpected callback:${JSON.stringify(callback)}`);
});
const ret = sandbox.invoke({ objref: obj, method: 'test', args: [returnsNumber] });
expect(ret).toEqual({ result: 100 * 2 + 500 * 2 });
});
defineTest('fail to begin async from sync - method', (sandbox) => {
const obj = sandbox.create({ fqn: 'jsii-calc.SyncVirtualMethods', overrides: [{ method: 'virtualMethod', cookie: 'myCookie' }] });
let called = 0;
sandbox.callbackHandler = makeSyncCallbackHandler(_ => {
const innerObj = sandbox.create({ fqn: 'jsii-calc.AsyncVirtualMethods' });
expect(() => sandbox.begin({ objref: innerObj, method: 'callMe' })).toThrow();
called++;
return 42; // Need a valid return value
});
sandbox.invoke({ objref: obj, method: 'callerIsMethod' });
expect(called).toBe(1);
sandbox.get({ objref: obj, property: 'callerIsProperty' });
expect(called).toBe(2);
sandbox.set({ objref: obj, property: 'callerIsProperty', value: 33 });
expect(called).toBe(3);
});
defineTest('the "Object" FQN can be used to allow creating empty objects with overrides which comply with an interface', (sandbox) => {
const obj = sandbox.create({ fqn: 'jsii-calc.Polymorphism' });
const friendly = sandbox.create({ fqn: 'Object', overrides: [{ method: 'hello' }] });
sandbox.callbackHandler = makeSyncCallbackHandler(_ => 'oh, hello');
const ret = sandbox.invoke({ objref: obj, method: 'sayHello', args: [friendly] });
expect(ret.result).toBe('oh, oh, hello');
});
defineTest('literal objects can be returned when an interface is expected, and they will be adorned with jsii metadata so they can be interacted with', (sandbox) => {
const obj = sandbox.create({ fqn: 'jsii-calc.JSObjectLiteralForInterface' });
const ret = sandbox.invoke({ objref: obj, method: 'giveMeFriendly' });
expect(sandbox.invoke({ objref: ret.result, method: 'hello' })).toEqual({ result: 'I am literally friendly!' });
const ret2 = sandbox.invoke({ objref: obj, method: 'giveMeFriendlyGenerator' });
expect(sandbox.invoke({ objref: ret2.result, method: 'hello' })).toEqual({ result: 'giveMeFriendlyGenerator' });
expect(sandbox.invoke({ objref: ret2.result, method: 'next' })).toEqual({ result: 42 });
});
defineTest('exceptions include a stack trace into the original source code', (sandbox) => {
const obj = sandbox.create({ fqn: 'jsii-calc.Thrower' });
expect(() => {
try {
sandbox.invoke({ objref: obj, method: 'throwError' });
} catch (error) {
const regexp = /^\s*at Thrower\.doThrowError \(.*jsii[-_]calc.*\/lib\/compliance\.ts:\d+:\d+\)$/m;
expect(regexp.test(error.stack)).toBeTruthy(); // The stack trace includes the path to the original source file
throw error;
}
}).toThrow();
});
defineTest('variadic methods can be called', (sandbox) => {
const obj = sandbox.create({ fqn: 'jsii-calc.VariadicMethod' });
expect(sandbox.invoke({ objref: obj, method: 'asArray', args: [1, 2, 3, 4] }).result)
.toEqual([1, 2, 3, 4]);
});
defineTest('variadic methods can be called without any vararg', (sandbox) => {
const obj = sandbox.create({ fqn: 'jsii-calc.VariadicMethod', args: [1, 2, 3] });
expect(sandbox.invoke({ objref: obj, method: 'asArray', args: [4] }).result)
.toEqual([1, 2, 3, 4]);
});
defineTest('static properties - get', (sandbox) => {
const value = sandbox.sget({ fqn: 'jsii-calc.Statics', property: 'Foo' });
expect(value).toEqual({ value: 'hello' });
});
defineTest('fails: static properties - set readonly', (sandbox) => {
expect(() => sandbox.sset({ fqn: 'jsii-calc.Statics', property: 'Foo', value: 123 })).toThrow();
});
defineTest('static properties - set', (sandbox) => {
const defaultInstance = sandbox.sget({ fqn: 'jsii-calc.Statics', property: 'instance' });
expect(sandbox.get({ objref: defaultInstance.value, property: 'value' })).toEqual({ value: 'default' });
const obj = sandbox.create({ fqn: 'jsii-calc.Statics', args: ['MyInstance'] });
sandbox.sset({ fqn: 'jsii-calc.Statics', property: 'instance', value: obj });
const updatedInstance = sandbox.sget({ fqn: 'jsii-calc.Statics', property: 'instance' });
expect(sandbox.get({ objref: updatedInstance.value, property: 'value' })).toEqual({ value: 'MyInstance' });
});
defineTest('fails: static properties - get/set non-static', (sandbox) => {
expect(() => sandbox.sget({ fqn: 'jsii-calc.Statics', property: 'value' })).toThrow(/is not static/);
expect(() => sandbox.sset({ fqn: 'jsii-calc.Statics', property: 'value', value: 123 })).toThrow(/is not static/);
});
defineTest('fails: static properties - get/set not found', (sandbox) => {
expect(() => sandbox.sget({ fqn: 'jsii-calc.Statics', property: 'zoo' })).toThrow(/doesn't have a property/);
expect(() => sandbox.sset({ fqn: 'jsii-calc.Statics', property: 'bar', value: 123 })).toThrow(/doesn't have a property/);
});
defineTest('static methods', (sandbox) => {
const result = sandbox.sinvoke({ fqn: 'jsii-calc.Statics', method: 'staticMethod', args: ['Jsii'] });
expect(result).toEqual({ result: 'hello ,Jsii!' });
});
defineTest('fails: static methods - not found', (sandbox) => {
expect(() => sandbox.sinvoke({ fqn: 'jsii-calc.Statics', method: 'staticMethodNotFound', args: ['Jsii'] }))
.toThrow(/doesn't have a method/);
});
defineTest('fails: static methods - not static', (sandbox) => {
expect(() => sandbox.sinvoke({ fqn: 'jsii-calc.Statics', method: 'justMethod', args: ['Jsii'] }))
.toThrow(/is not a static method/);
});
defineTest('loading a module twice idepotently succeeds', async (sandbox) => {
sandbox.load({
tarball: await preparePackage('jsii-calc', false),
name: 'jsii-calc',
version: calcVersion
});
});
defineTest('fails if trying to load two different versions of the same module', async (sandbox) => {
const tarball = await preparePackage('jsii-calc', false);
return expect(() => sandbox.load({ tarball, name: 'jsii-calc', version: '99.999.9' }))
.toThrow(/Multiple versions .+ and .+ of the package 'jsii-calc' cannot be loaded together/)
});
defineTest('node.js standard library', async (sandbox) => {
const objref = sandbox.create({ fqn: 'jsii-calc.NodeStandardLibrary' });
const promise = sandbox.begin({ objref, method: 'fsReadFile' });
await processPendingPromises(sandbox);
const output = await sandbox.end({ promiseid: promise.promiseid });
expect(output).toEqual({ result: 'Hello, resource!' });
expect(sandbox.invoke({ objref, method: 'fsReadFileSync' }))
.toEqual({ result: 'Hello, resource! SYNC!' });
const platform = sandbox.get({ objref, property: 'osPlatform' }).value;
expect(platform && platform.length).toBeGreaterThan(0);
expect(sandbox.invoke({ objref, method: 'cryptoSha256' }))
.toEqual({ result: '6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50' });
});
// @see awslabs/jsii#248
defineTest('object literals are returned by reference', (sandbox) => {
const objref = sandbox.create({ fqn: 'jsii-calc.ClassWithMutableObjectLiteralProperty' });
const property = sandbox.get({ objref, property: 'mutableObject' }).value;
const newValue = 'Bazinga!1!';
sandbox.set({ objref: property, property: 'value', value: newValue });
expect(sandbox.get({
objref: sandbox.get({ objref, property: 'mutableObject' }).value,
property: 'value'
}).value).toEqual(newValue);
sandbox.del({ objref: property });
});
defineTest('overrides: method instead of property with the same name', (sandbox) => {
expect(() => {
sandbox.create({ fqn: 'jsii-calc.SyncVirtualMethods', overrides: [
{ method: 'theProperty' }
] });
}).toThrow(/Trying to override property/);
});
defineTest('overrides: property instead of method with the same name', (sandbox) => {
expect(() => {
sandbox.create({ fqn: 'jsii-calc.SyncVirtualMethods', overrides: [
{ property: 'virtualMethod' }
] });
}).toThrow(/Trying to override method/);
});
defineTest('overrides: skip overrides of private methods', (sandbox) => {
const objref = sandbox.create({ fqn: 'jsii-calc.DoNotOverridePrivates', overrides: [
{ method: 'privateMethod' }
] });
sandbox.callbackHandler = makeSyncCallbackHandler(_ => {
throw new Error('override callback should not be called');
});
const result = sandbox.invoke({ objref, method: 'privateMethodValue' });
expect(result.result).toBe('privateMethod');
});
defineTest('overrides: skip overrides of private properties', (sandbox) => {
const objref = sandbox.create({ fqn: 'jsii-calc.DoNotOverridePrivates', overrides: [
{ property: 'privateProperty' }
] });
sandbox.callbackHandler = makeSyncCallbackHandler(_ => {
throw new Error('override callback should not be called');
});
const result = sandbox.invoke({ objref, method: 'privatePropertyValue' });
expect(result.result).toBe('privateProperty');
});
defineTest('nulls are converted to undefined - ctor', (sandbox) => {
sandbox.create({ fqn: 'jsii-calc.NullShouldBeTreatedAsUndefined', args: ['foo', null] });
});
defineTest('nulls are converted to undefined - method arguments', (sandbox) => {
const objref = sandbox.create({ fqn: 'jsii-calc.NullShouldBeTreatedAsUndefined', args: ['foo'] });
sandbox.invoke({ objref, method: 'giveMeUndefined', args: [null] });
});
defineTest('nulls are converted to undefined - inside objects', (sandbox) => {
const objref = sandbox.create({ fqn: 'jsii-calc.NullShouldBeTreatedAsUndefined', args: ['foo'] });
sandbox.invoke({ objref, method: 'giveMeUndefinedInsideAnObject', args: [{
thisShouldBeUndefined: null,
arrayWithThreeElementsAndUndefinedAsSecondArgument: ['one', null, 'two']
}] });
});
defineTest('nulls are converted to undefined - properties', (sandbox) => {
const objref = sandbox.create({ fqn: 'jsii-calc.NullShouldBeTreatedAsUndefined', args: ['foo'] });
sandbox.set({ objref, property: 'changeMeToUndefined', value: null });
sandbox.invoke({ objref, method: 'verifyPropertyIsUndefined' });
});
defineTest('JSII_AGENT is undefined in node.js', (sandbox) => {
expect(sandbox.sget({ fqn: 'jsii-calc.JsiiAgent', property: 'jsiiAgent' }).value).toBe(undefined);
});
defineTest('ObjRefs are labeled with the "most correct" type', (sandbox) => {
typeMatches('makeClass', { [TOKEN_REF]: /^jsii-calc.InbetweenClass@/ });
typeMatches('makeInterface', { [TOKEN_REF]: /^jsii-calc.InbetweenClass@/, [TOKEN_INTERFACES]: ['jsii-calc.IPublicInterface'] });
typeMatches('makeInterface2', { [TOKEN_REF]: /^jsii-calc.InbetweenClass@/ });
typeMatches('makeInterfaces', [{ [TOKEN_REF]: /^jsii-calc.InbetweenClass@/, [TOKEN_INTERFACES]: ['jsii-calc.IPublicInterface'] }]);
typeMatches('hiddenInterface', { [TOKEN_REF]: /^Object@/, [TOKEN_INTERFACES]: ['jsii-calc.IPublicInterface'] });
typeMatches('hiddenInterfaces', [{ [TOKEN_REF]: /^Object@/, [TOKEN_INTERFACES]: ['jsii-calc.IPublicInterface'] }]);
typeMatches('hiddenSubInterfaces', [{ [TOKEN_REF]: /^Object@/, [TOKEN_INTERFACES]: ['jsii-calc.IPublicInterface'] }]);
function typeMatches(staticMethod: string, typeSpec: any) {
const ret = sandbox.sinvoke({ fqn: 'jsii-calc.Constructors', method: staticMethod }).result as api.ObjRef;
expect(deepEqualWithRegex(ret, typeSpec), `Constructors.${staticMethod}() => ${JSON.stringify(ret)}, does not match ${JSON.stringify(typeSpec)}`).toBeTruthy();
}
});
/**
* This is used to validate the ability to use `this` from within a static context.
*
* https://github.com/awslabs/aws-cdk/issues/2304
*/
defineTest('sinvoke allows access to the static context', (sandbox) => {
const response = sandbox.sinvoke({ fqn: 'jsii-calc.StaticContext', method: 'canAccessStaticContext' });
expect(response.result).toBe(true);
});
defineTest('sget allows access to the static context', (sandbox) => {
const response = sandbox.sget({ fqn: 'jsii-calc.StaticContext', property: 'staticVariable' });
expect(response.value).toBe(true);
});
defineTest('sset allows access to the static context', (sandbox) => {
sandbox.sset({ fqn: 'jsii-calc.StaticContext', property: 'staticVariable', value: false });
const response = sandbox.sget({ fqn: 'jsii-calc.StaticContext', property: 'staticVariable' });
expect(response.value).toBe(false);
});
/*
Test currently disabled because we don't have the infrastructure to make it pass.
https://github.com/aws/jsii/issues/399
defineTest('A single instance can be returned under two types', (sandbox) => {
const singleInstanceTwoTypes = create(sandbox, 'jsii-calc.SingleInstanceTwoTypes')();
typeMatches('interface1', { [TOKEN_REF]: /^jsii-calc.InbetweenClass@/ });
typeMatches('interface2', { [TOKEN_REF]: /^jsii-calc.IPublicInterface@/ });
function typeMatches(method: string, typeSpec: any) {
const ret = sandbox.invoke({ objref: singleInstanceTwoTypes, method }).result as api.ObjRef;
test.ok(deepEqualWithRegex(ret, typeSpec), `Constructors.${method}() => ${JSON.stringify(ret)}, does not match ${JSON.stringify(typeSpec)}`);
}
});
*/
defineTest('toSandbox: "null" in hash values send to JS should be treated as non-existing key', (sandbox) => {
const input = { option1: null, option2: 'hello' };
const option1Exists = sandbox.sinvoke({ fqn: 'jsii-calc.EraseUndefinedHashValues', method: 'doesKeyExist', args: [input, 'option1'] });
expect(option1Exists.result).toBe(false);
const option2Exists = sandbox.sinvoke({ fqn: 'jsii-calc.EraseUndefinedHashValues', method: 'doesKeyExist', args: [input, 'option2'] });
expect(option2Exists.result).toBe(true);
});
defineTest('toSandbox: "undefined" in hash values sent to JS should be treated as non-existing key', (sandbox) => {
const input = { option1: undefined, option2: 'hello' };
const option1Exists = sandbox.sinvoke({ fqn: 'jsii-calc.EraseUndefinedHashValues', method: 'doesKeyExist', args: [input, 'option1'] });
expect(option1Exists.result).toBe(false);
const option2Exists = sandbox.sinvoke({ fqn: 'jsii-calc.EraseUndefinedHashValues', method: 'doesKeyExist', args: [input, 'option2'] });