typeomatica 0.3.3 → 0.3.4

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