typeomatica 0.3.39 → 0.3.57

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.
package/test/index.ts DELETED
@@ -1,676 +0,0 @@
1
- 'use strict';
2
- import { describe, expect, test } from '@jest/globals';
3
-
4
- // BasePrototype & BaseClass are the same function
5
- // go as you want for being meaningfull
6
- // or meaningless
7
- const BasePrototype = require('..');
8
- import { BaseClass, FieldConstructor, SymbolInitialValue, Strict } from '..';
9
-
10
- debugger; // eslint-disable-line no-debugger
11
-
12
- interface IBase {
13
- get getterField(): string
14
- // eslint-disable-next-line no-unused-vars
15
- set setterField(value: string)
16
- numberValue: number
17
- stringValue: string
18
- booleanValue: boolean
19
- objectValue: object
20
- }
21
-
22
- let decoratedSomeProp = 0;
23
- // const s = BasePrototype({ someProp: 123 });
24
- // console.log(s);
25
-
26
- // eslint-disable-next-line new-cap
27
- @Strict({ someProp: 123 })
28
- class DecoratedByBase {
29
- someProp!: number;
30
- }
31
-
32
- // @Strict({ someProp: 123 })
33
- class ExtendedDecoratedByBase extends DecoratedByBase {
34
- someProp: number;
35
- constructor() {
36
- super();
37
- this.someProp = 321;
38
- decoratedSomeProp = this.someProp;
39
- }
40
- }
41
-
42
- // eslint-disable-next-line new-cap
43
- class Base extends BasePrototype({
44
- additionalProp: 321,
45
- someMethod() {
46
- return this.numberValue.valueOf();
47
- },
48
- }) implements IBase {
49
- numberValue = 123;
50
- stringValue: string;
51
- booleanValue: boolean;
52
- objectValue: object;
53
-
54
- get getterField() {
55
- const answer = `${this.stringValue}`;
56
- return answer;
57
- }
58
-
59
- set setterField(value: string) {
60
- this.stringValue = value;
61
- }
62
-
63
- constructor() {
64
- super();
65
- this.stringValue = '123';
66
- this.booleanValue = true;
67
- this.objectValue = {};
68
- // ES2022
69
- // Object.defineProperty(this, 'getterField', {
70
- // get() {
71
- // const answer = `${this.stringValue}`;
72
- // return answer;
73
- // }
74
- // });
75
- // Object.defineProperty(this, 'setterField', {
76
- // set(value: string) {
77
- // this.stringValue = value;
78
- // }
79
- // });
80
- }
81
- }
82
- const baseInstance = new Base;
83
-
84
- const upperInstance = Object.create(baseInstance);
85
-
86
- class SimpleBase extends BaseClass {
87
- stringProp = '123';
88
- // ES2022
89
- // stringProp: string;
90
- // constructor() {
91
- // super();
92
- // this.stringProp = '123';
93
- // }
94
- }
95
- const simpleInstance = new SimpleBase;
96
-
97
- interface IFCstr<S> {
98
- (): void
99
- new(): {
100
- [key in keyof S]: S[key]
101
- }
102
- }
103
-
104
- type TmyFunctionalInstance = { stringProp: string }
105
- // eslint-disable-next-line no-unused-vars
106
- const MyFunctionalConstructor = function (this: TmyFunctionalInstance) {
107
- this.stringProp = '123';
108
- } as IFCstr<TmyFunctionalInstance>;
109
-
110
- Reflect.setPrototypeOf(MyFunctionalConstructor.prototype, new BasePrototype);
111
-
112
- const myFunctionalInstance = new MyFunctionalConstructor();
113
-
114
- class SecondaryExtend extends Base { second = 123; }
115
- class TripleExtend extends SecondaryExtend { }
116
- const tiripleExtendInstance = new TripleExtend;
117
-
118
- // eslint-disable-next-line new-cap
119
- class NetworkedExtention extends BasePrototype(tiripleExtendInstance) { }
120
-
121
- const networkedInstance = new NetworkedExtention;
122
-
123
- // eslint-disable-next-line new-cap
124
- class ExtendedArray extends BasePrototype([1, 2, 3]) { }
125
- // eslint-disable-next-line new-cap
126
- class ExtendedSet extends BasePrototype(new Set([1, 2, 3])) { }
127
-
128
- const extendedArrayInstance = new ExtendedArray;
129
- const extendedSetInstance = new ExtendedSet;
130
-
131
- const MUTATION_VALUE = -2;
132
-
133
-
134
- class MyFieldConstructorNoRe extends FieldConstructor {
135
- _value: string;
136
- constructor(value: string) {
137
- super(value);
138
- Reflect.defineProperty(this, 'enumerable', {
139
- value: true
140
- });
141
- this._value = value;
142
- }
143
- }
144
- class MyFieldConstructorReGet extends MyFieldConstructorNoRe {
145
- constructor(value: string) {
146
- super(value);
147
- const self = this;
148
- Reflect.defineProperty(this, 'enumerable', {
149
- value: true
150
- });
151
- Reflect.defineProperty(this, 'get', {
152
- get() {
153
- return function () {
154
- return self._value;
155
- };
156
- },
157
- enumerable: true
158
- });
159
- }
160
- }
161
- class MyFieldConstructorReSet extends MyFieldConstructorNoRe {
162
- constructor(value: string) {
163
- super(value);
164
- const self = this;
165
- Reflect.defineProperty(this, 'enumerable', {
166
- value: true
167
- });
168
- Reflect.defineProperty(this, 'set', {
169
- get() {
170
- return function (_value: string) {
171
- self._value = _value;
172
- };
173
- },
174
- enumerable: true
175
- });
176
- }
177
- }
178
- class MyFieldConstructor extends MyFieldConstructorReGet {
179
- constructor(value: string) {
180
- super(value);
181
- const self = this;
182
- Reflect.defineProperty(this, 'enumerable', {
183
- value: true
184
- });
185
- Reflect.defineProperty(this, 'set', {
186
- get() {
187
- return function (_value: string) {
188
- self._value = _value;
189
- };
190
- },
191
- enumerable: true
192
- });
193
- }
194
- }
195
-
196
- const myField = new MyFieldConstructor('initial value');
197
- const myFieldReGet = new MyFieldConstructorReGet('initial value for get check');
198
- const myFieldReSet = new MyFieldConstructorReSet('initial value for set check');
199
-
200
- class MadeFieldClass extends BaseClass {
201
- myField = myField as unknown | string;
202
- get [SymbolInitialValue]() {
203
- const self = this;
204
- return (fieldName: 'myField') => {
205
- if (fieldName !== 'myField') {
206
- return self[fieldName];
207
- }
208
- //@ts-ignore
209
- const answer = myField[SymbolInitialValue];
210
- return answer;
211
- };
212
- }
213
- }
214
- class SecondMadeFieldClass extends BaseClass { myField = myField as unknown | string; }
215
- const madeFieldInstance = new MadeFieldClass;
216
- const secondMadeFieldInstance = new MadeFieldClass;
217
- const thirdMadeFieldInstance = new SecondMadeFieldClass;
218
-
219
- class MadeReGet extends BaseClass { myField = myFieldReGet as unknown | string; }
220
- class MadeReSet extends BaseClass { myField = myFieldReSet as unknown | string; }
221
- const madeReGet = new MadeReGet;
222
- const madeReSet = new MadeReSet;
223
-
224
- describe('props tests', () => {
225
-
226
- test('decorators works', () => {
227
- const rgp = Reflect.getPrototypeOf;
228
- const decorated = new DecoratedByBase;
229
- const exdecorated = new ExtendedDecoratedByBase;
230
- expect(decoratedSomeProp.valueOf()).toEqual(321);
231
- expect(exdecorated.someProp.valueOf()).toEqual(321);
232
- expect(decorated.someProp.valueOf()).toEqual(123);
233
- const proto = rgp(decorated);
234
- //@ts-expect-error;
235
- expect(proto.someProp).toEqual(123);
236
- });
237
-
238
- test('base instance has props', () => {
239
- let gf: string;
240
- let sv: string;
241
- expect(Object.keys(baseInstance)).toEqual(['numberValue', 'stringValue', 'booleanValue', 'objectValue']);
242
-
243
- gf = baseInstance.getterField;
244
- expect(gf).toEqual('123');
245
- sv = baseInstance.stringValue;
246
- expect(sv.valueOf()).toEqual('123');
247
-
248
-
249
- baseInstance.setterField = '12345';
250
-
251
- gf = baseInstance.getterField;
252
- expect(gf).toEqual('12345');
253
- sv = baseInstance.stringValue;
254
- expect(sv.valueOf()).toEqual('12345');
255
-
256
- baseInstance.setterField = '123';
257
-
258
- gf = baseInstance.getterField;
259
- expect(gf).toEqual('123');
260
- sv = baseInstance.stringValue;
261
- expect(`${sv}`).toEqual('123');
262
- });
263
-
264
- test('simple instance works & strings too', () => {
265
- expect(simpleInstance.stringProp.toString()).toBe('123');
266
- expect(simpleInstance.stringProp.length).toBe(3);
267
-
268
-
269
- expect(/String$/.test(simpleInstance.stringProp.constructor.name)).toBe(true);
270
- expect(() => {
271
- // @ts-expect-error
272
- simpleInstance.stringProp = 123;
273
-
274
- }).toThrow(new TypeError('Type Mismatch'));
275
- });
276
-
277
- test('correct boolean comparison with type coercion', () => {
278
- expect(() => {
279
-
280
- const { booleanValue } = baseInstance;
281
- booleanValue != false;
282
-
283
- }).toThrow(new TypeError('Value Access Denied'));
284
- });
285
-
286
- test('fails boolean arithmetics', () => {
287
- expect(() => {
288
-
289
- // @ts-expect-error
290
- baseInstance.booleanValue + 5;
291
-
292
- }).toThrow(new ReferenceError('Value Access Denied'));
293
- });
294
-
295
- test('correct boolean assignment', () => {
296
-
297
- let { booleanValue } = baseInstance;
298
- expect(booleanValue.valueOf()).toEqual(true);
299
-
300
- // warning!
301
- // booleanValue does not rely on baseInstance anymore!
302
- // @ts-expect-error
303
- booleanValue = new Boolean(false);
304
-
305
- let value = baseInstance.booleanValue.valueOf();
306
- expect(value).toEqual(true);
307
-
308
- // @ts-expect-error
309
- baseInstance.booleanValue = new Boolean(false);
310
- value = baseInstance.booleanValue.valueOf();
311
- expect(value).toEqual(false);
312
-
313
- });
314
-
315
- test('correct JSON.stringify', () => {
316
- const stringifyResult = JSON.stringify(baseInstance);
317
- expect(stringifyResult).toMatchSnapshot();
318
- });
319
-
320
- test('correct boolean constructor', () => {
321
- expect(baseInstance.booleanValue).toBeInstanceOf(Boolean);
322
- });
323
-
324
- test('correct object assignment', () => {
325
- baseInstance.objectValue = { a: 123 };
326
- // @ts-expect-error
327
- expect(baseInstance.objectValue.a).toEqual(123);
328
- });
329
-
330
- test('correct custom field creation', () => {
331
- expect(madeFieldInstance.myField).toEqual('initial value');
332
- });
333
-
334
- test('correct custom field assignment', () => {
335
- madeFieldInstance.myField = 'replaced';
336
- //@ts-ignore
337
- const initialValue = madeFieldInstance[SymbolInitialValue]('myField');
338
- expect(initialValue).toEqual('initial value');
339
- expect(secondMadeFieldInstance.myField).toEqual('replaced');
340
- expect(thirdMadeFieldInstance.myField).toEqual('replaced');
341
-
342
- madeFieldInstance.myField = 'replaced secondary';
343
- expect(secondMadeFieldInstance.myField).toEqual('replaced secondary');
344
- expect(thirdMadeFieldInstance.myField).toEqual('replaced secondary');
345
-
346
- madeFieldInstance.myField = 'replaced';
347
- expect(secondMadeFieldInstance.myField).toEqual('replaced');
348
- expect(thirdMadeFieldInstance.myField).toEqual('replaced');
349
- });
350
-
351
- test('correct custom field no-re-assignment', () => {
352
- expect(madeReGet.myField).toEqual('initial value for get check');
353
- expect(() => {
354
-
355
- madeReGet.myField = 'replaced';
356
-
357
- }).toThrow(new TypeError('Re-Assirnment is Forbidden'));
358
- });
359
-
360
- test('correct custom field setter only', () => {
361
- madeReSet.myField = 'replaced';
362
- expect(madeReSet.myField).toEqual('initial value for set check');
363
- });
364
-
365
- test('takes error on wrong field definition', () => {
366
- expect(() => {
367
- class WrongFieldConstructor extends FieldConstructor {
368
- value: number;
369
- constructor(value: number) {
370
- super(value);
371
- this.value = value;
372
- }
373
- }
374
- const wrongField = new WrongFieldConstructor(123);
375
- class WithWrongField extends BaseClass {
376
- erroredField = wrongField;
377
- }
378
- new WithWrongField;
379
-
380
- }).toThrow();
381
- });
382
-
383
- test('correct custom missing prop search creation', () => {
384
- //@ts-ignore
385
- expect(madeFieldInstance[Symbol.toStringTag]).toEqual(undefined);
386
- //@ts-ignore
387
- expect(madeFieldInstance[Symbol.iterator]).toEqual(undefined);
388
- const util = require('util');
389
- //@ts-ignore
390
- expect(madeFieldInstance[util.inspect.custom]).toEqual(undefined);
391
- const inspected = util.inspect(madeFieldInstance);
392
- const expected = 'MadeFieldClass { myField: [Getter/Setter] }';
393
- expect(inspected).toEqual(expected);
394
- });
395
-
396
- test('wrong assignment to objects', () => {
397
-
398
- expect(() => {
399
- // @ts-expect-error
400
- baseInstance.objectValue = 123;
401
-
402
- }).toThrow(new TypeError('Type Mismatch'));
403
-
404
- expect(() => {
405
-
406
- baseInstance.objectValue = new Set();
407
-
408
- }).toThrow(new TypeError('Type Mismatch'));
409
-
410
- });
411
-
412
- test('fails number arithmetics', () => {
413
- expect(() => {
414
-
415
- baseInstance.numberValue + 5;
416
-
417
- }).toThrow();
418
- });
419
-
420
- test('correct number arithmetics using .valueOf()', () => {
421
-
422
- baseInstance.numberValue = MUTATION_VALUE;
423
-
424
- const result = baseInstance.numberValue.valueOf() + 5;
425
- expect(result).toStrictEqual(3);
426
-
427
- });
428
-
429
- test('correct number arithmetics using hinting for Symbol.toPrimitive (hint)', () => {
430
-
431
- const result = 3 + +baseInstance.numberValue;
432
- expect(result).toStrictEqual(1);
433
-
434
- });
435
-
436
- test('correct number value', () => {
437
- expect(baseInstance.numberValue.toString()).toStrictEqual(MUTATION_VALUE.toString());
438
- expect(/Number$/.test(baseInstance.numberValue.constructor.name)).toBe(true);
439
- });
440
-
441
-
442
- test('wrong assignment', () => {
443
- expect(() => {
444
- // @ts-expect-error
445
- baseInstance.booleanValue = 123;
446
-
447
- }).toThrow(new TypeError('Type Mismatch'));
448
- });
449
-
450
- test('correct null value', () => {
451
- baseInstance.nullValue = null;
452
- expect(baseInstance.nullValue).toEqual(null);
453
- });
454
-
455
- test('any assignment to null value', () => {
456
- expect(() => {
457
-
458
- baseInstance.nullValue = 123;
459
-
460
- }).toThrow(new TypeError('Type Mismatch'));
461
- });
462
-
463
-
464
- test('correct undefined value', () => {
465
- baseInstance.undefinedValue = undefined;
466
- expect(baseInstance.undefinedValue).toEqual(undefined);
467
- });
468
-
469
- test('wrong assignment to undefined value', () => {
470
- expect(() => {
471
-
472
- baseInstance.undefinedValue = 123;
473
-
474
- }).toThrow(new TypeError('Type Mismatch'));
475
- });
476
-
477
- test('correct BigInt value', () => {
478
- baseInstance.BigIntValue = BigInt(1);
479
- expect(baseInstance.BigIntValue).toEqual(BigInt(1));
480
- });
481
-
482
- test('correct assignment to BigInt value', () => {
483
- baseInstance.BigIntValue = BigInt(2);
484
- expect(baseInstance.BigIntValue).toEqual(BigInt(2));
485
- });
486
-
487
- test('wrong assignment to BigInt value', () => {
488
- expect(() => {
489
-
490
- baseInstance.BigIntValue = 123;
491
-
492
- }).toThrow(new TypeError('Type Mismatch'));
493
- });
494
-
495
- test('correct Symbol value', () => {
496
- baseInstance.SymbolValue = Symbol('test');
497
- expect(typeof baseInstance.SymbolValue).toEqual('symbol');
498
- });
499
-
500
- test('wrong assignment to BigInt value', () => {
501
- expect(() => {
502
-
503
- baseInstance.SymbolValue = 123;
504
-
505
- }).toThrow(new TypeError('Type Mismatch'));
506
- });
507
-
508
- test('correct prototype correction', () => {
509
- expect(baseInstance.additionalProp).toEqual(321);
510
- expect(baseInstance.toString).toBeInstanceOf(Function);
511
- });
512
-
513
- test('missing value fails', () => {
514
- expect(() => {
515
-
516
- baseInstance.missingValue > 1;
517
-
518
- }).toThrow(new TypeError('Attempt to Access to Undefined Prop: [ missingValue ] of Base'));
519
- });
520
-
521
- });
522
-
523
- describe('prototype mutations tests', () => {
524
-
525
- test('incorrect prototype invocation number get', () => {
526
- expect(() => {
527
-
528
- upperInstance.numberValue > 1;
529
-
530
- }).toThrow(new ReferenceError('Value Access Denied'));
531
- });
532
-
533
- test('incorrect prototype invocation number set', () => {
534
- expect(() => {
535
-
536
- upperInstance.numberValue = new Number(1);
537
-
538
- }).toThrow(new ReferenceError('Value Access Denied'));
539
- });
540
-
541
-
542
- test('incorrect prototype invocation string get', () => {
543
- expect(() => {
544
-
545
- upperInstance.stringValue > '1';
546
-
547
- }).toThrow(new ReferenceError('Value Access Denied'));
548
- });
549
-
550
- test('incorrect prototype invocation string set', () => {
551
- expect(() => {
552
-
553
- upperInstance.stringValue = new String(1);
554
-
555
- }).toThrow(new ReferenceError('Value Access Denied'));
556
- });
557
-
558
- });
559
-
560
- describe('methods tests', () => {
561
-
562
- test('functions are restricted for data type', () => {
563
-
564
- expect(() => {
565
-
566
- baseInstance.someMethod = function () { };
567
-
568
- }).toThrow(new TypeError('Functions are Restricted'));
569
-
570
- });
571
-
572
- test('proxy proto methods are SOLID', () => {
573
-
574
- const result = baseInstance.someMethod();
575
- expect(result).toBe(MUTATION_VALUE);
576
-
577
- });
578
-
579
- });
580
-
581
- describe('property re-definition works', () => {
582
-
583
- test('exact prototype invocation for correct property extraction', () => {
584
-
585
- Object.defineProperty(upperInstance, 'stringProp', {
586
- get() {
587
- const target = Reflect.getPrototypeOf(upperInstance) as {
588
- stringValue: string
589
- };
590
- return target.stringValue;
591
- }
592
- });
593
-
594
- const value = upperInstance.stringProp;
595
- expect(`${value}`).toEqual('123');
596
-
597
- });
598
- });
599
-
600
-
601
- describe('functional constructors tests', () => {
602
- test('construction made properly', () => {
603
-
604
- const { stringProp,
605
- constructor: {
606
- name
607
- }
608
- } = myFunctionalInstance;
609
- expect(name).toEqual('MyFunctionalConstructor');
610
- expect(stringProp.valueOf()).toEqual('123');
611
-
612
- });
613
- });
614
-
615
- describe('instanceof works', () => {
616
- test('for class construction', () => {
617
-
618
- expect(baseInstance).toBeInstanceOf(Base);
619
-
620
- });
621
- test('for simple construction', () => {
622
-
623
- expect(simpleInstance).toBeInstanceOf(SimpleBase);
624
-
625
- });
626
- test('for functional construction', () => {
627
-
628
- expect(myFunctionalInstance).toBeInstanceOf(MyFunctionalConstructor);
629
-
630
- });
631
- });
632
-
633
- describe('deep extend works', () => {
634
- test('class extended three times construction', () => {
635
-
636
- expect(tiripleExtendInstance).toBeInstanceOf(Base);
637
- expect(tiripleExtendInstance).toBeInstanceOf(SecondaryExtend);
638
- expect(tiripleExtendInstance).toBeInstanceOf(TripleExtend);
639
- expect(`${tiripleExtendInstance.stringValue}`).toEqual('123');
640
- expect(tiripleExtendInstance.second).toBeInstanceOf(Number);
641
-
642
- });
643
-
644
- test('network extention works', () => {
645
- expect(networkedInstance).toBeInstanceOf(NetworkedExtention);
646
-
647
- expect(() => {
648
-
649
- `${networkedInstance.stringValue}`;
650
-
651
- }).toThrow(new ReferenceError('Value Access Denied'));
652
-
653
- });
654
-
655
- test('builtin types works', () => {
656
- expect(extendedArrayInstance).toBeInstanceOf(Array);
657
- expect(extendedArrayInstance).toBeInstanceOf(ExtendedArray);
658
- expect(extendedArrayInstance[0]).toBe(1);
659
- extendedArrayInstance.unshift(0);
660
- extendedArrayInstance.push(5);
661
- expect(+extendedArrayInstance.length).toBe(5);
662
- expect(+extendedArrayInstance[0]).toBe(0);
663
- expect(+extendedArrayInstance[4]).toBe(5);
664
-
665
- expect(extendedSetInstance).toBeInstanceOf(Set);
666
- expect(extendedSetInstance).toBeInstanceOf(ExtendedSet);
667
-
668
- // JS Object.create(new Set([1, 2, 3])) is doing the same error!
669
- expect(() => {
670
-
671
- extendedSetInstance.has(0);
672
-
673
- }).toThrow(new TypeError('Method Set.prototype.has called on incompatible receiver [object Object]'));
674
-
675
- });
676
- });