typeomatica 0.3.38 → 0.3.55

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