-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathobj_test.mjs
1006 lines (797 loc) · 26.1 KB
/
obj_test.mjs
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
/* eslint-disable getter-return */
// deno-lint-ignore-file getter-return
import './internal_test_init.mjs'
import * as t from '../test.mjs'
import * as l from '../lang.mjs'
import * as o from '../obj.mjs'
/* Util */
function unreachable() {throw Error(`unreachable`)}
const inherit = Object.create
class Simple {}
class Fat {
constructor() {
this.one = 10
this.two = 20
Object.defineProperty(this, `three`, {
value: 30,
writable: true,
enumerable: false,
configurable: true,
})
}
method() {return 40}
get getter() {return 50}
get getterSetter() {return 60}
set getterSetter(_) {unreachable()}
}
/* Test */
t.test(function test_assign() {
testMut(o.assign)
function mutate(tar, src) {t.is(o.assign(tar, src), tar)}
t.test(function test_shadowing() {
t.test(function test_getters_and_setters() {
t.throws(() => o.assign(new Fat(), {getter: 10}), TypeError, `Cannot set property getter of #<Fat> which has only a getter`)
t.throws(() => o.assign(new Fat(), {getterSetter: 10}), Error, `unreachable`)
})
const tar = new Fat()
const src = {
constructor: 70,
toString: 80,
method: 90,
two: 100,
three: 110,
four: 120,
}
mutate(tar, src)
t.eq(
Object.getOwnPropertyNames(tar),
[...new Set([
...Object.getOwnPropertyNames(new Fat()),
...Object.keys(src),
])],
)
// This doesn't include `.three` which is non-enumerable.
t.eq({...tar}, {
...new Fat(),
constructor: 70,
toString: 80,
method: 90,
two: 100,
four: 120,
})
// Has to be checked separately because non-enumerable.
t.is(tar.three, src.three)
})
})
// Commonalities between `assign` and `patch`.
function testMut(fun) {
t.test(function test_invalid() {
t.throws(() => fun(), TypeError, `expected variant of isStruct`)
t.throws(() => fun(null, {}), TypeError, `expected variant of isStruct`)
t.throws(() => fun(10, {}), TypeError, `expected variant of isStruct`)
t.throws(() => fun(`one`, {}), TypeError, `expected variant of isStruct`)
t.throws(() => fun(`one`, `one`), TypeError, `expected variant of isStruct`)
t.throws(() => fun([], {}), TypeError, `expected variant of isStruct`)
t.throws(() => fun([], []), TypeError, `expected variant of isStruct`)
t.throws(() => fun(l.nop, {}), TypeError, `expected variant of isStruct`)
t.throws(() => fun(l.nop, l.nop), TypeError, `expected variant of isStruct`)
t.throws(() => fun({}, 10), TypeError, `expected variant of isStruct`)
t.throws(() => fun({}, `one`), TypeError, `expected variant of isStruct`)
t.throws(() => fun({}, []), TypeError, `expected variant of isStruct`)
t.throws(() => fun({}, l.nop), TypeError, `expected variant of isStruct`)
t.throws(() => fun({}, []), TypeError, `expected variant of isStruct`)
})
function mutate(tar, src) {t.is(fun(tar, src), tar)}
t.test(function test_returns_target() {
mutate({})
mutate({}, {})
mutate(new class Simple {}(), {})
})
t.test(function test_from_nil() {
function test(val) {
const tar = new Simple()
mutate(tar, val)
t.eq({...tar}, {...val})
}
test(null)
test(undefined)
})
t.test(function test_from_npo() {
const tar = new Simple()
mutate(tar, inherit(null, {one: {value: 10, enumerable: true}}))
t.eq({...tar}, {one: 10})
})
t.test(function test_from_dict() {
const tar = new Simple()
mutate(tar, {one: 10})
t.eq({...tar}, {one: 10})
})
t.test(function test_from_subclass() {
class Sup {}
const tar = new Sup()
class Sub extends Sup {}
const src = Object.assign(new Sub(), {one: 10})
mutate(tar, src)
t.eq({...tar}, {one: 10})
})
}
t.test(function test_patch() {
testMut(o.patch)
function mutate(tar, src) {t.is(o.patch(tar, src), tar)}
t.test(function test_no_shadowing() {
t.test(function test_plain_object() {
const tar = {one: 10, two: 20}
mutate(tar, {constructor: 30, toString: 40, two: 50, three: 60})
t.eq(Object.getOwnPropertyNames(tar), [`one`, `two`, `three`])
t.eq(tar, {one: 10, two: 50, three: 60})
t.is(tar.constructor, Object)
t.is(tar.toString, Object.prototype.toString)
})
t.test(function test_custom_class() {
const tar = new Fat()
mutate(tar, {
constructor: 70,
toString: 80,
method: 90,
getter: 100,
getterSetter: 110,
two: 120,
three: 130,
four: 140,
})
t.eq(Object.getOwnPropertyNames(tar), [`one`, `two`, `three`, `four`])
t.eq({...tar}, {one: 10, two: 120, four: 140})
t.is(tar.constructor, Fat)
t.is(tar.toString, Object.prototype.toString)
t.is(tar.three, 30)
t.is(tar.method(), 40)
t.is(tar.getter, 50)
t.is(tar.getterSetter, 60)
})
})
})
t.test(function test_StructType() {
t.test(function test_invalid() {
function fail(val) {
t.throws(
() => class Tar extends o.Struct {static spec = {one: val}}.getType(),
TypeError,
`invalid definition of "one" in [function Tar]: expected nil or function, got ` + l.show(val),
)
}
fail(10)
fail(`str`)
fail(true)
fail({one: 10})
fail([10])
})
class Type extends o.StructType {
init() {}
reinit() {}
}
class Tar extends o.Struct {
static get Type() {return Type}
static spec = {
id: l.reqIntPos,
name: l.reqStr,
}
}
// Some later tests rely on this.
t.test(function test_statics() {
t.is(o.Struct.Type, o.StructType)
t.inst(o.Struct.getType(), o.StructType)
t.is(o.StructLax.Type, o.StructTypeLax)
t.inst(o.StructLax.getType(), o.StructTypeLax)
t.inst(Tar.getType(), Tar.Type)
t.is(Tar.getType(), Tar.getType())
})
t.test(function test_reset() {
const type = Tar.getType()
const tar = new Tar()
testStructTypeReset(tar, type.reset.bind(type))
type.reset(tar, {id: 30, name: `two`, undeclared0: 40, undeclared1: 50, constructor: 60})
t.own(tar, {id: 30, name: `two`})
})
t.test(function test_patch() {
const type = Tar.getType()
const tar = new Tar()
type.patch(tar)
t.own(tar, {})
type.patch(tar, {})
t.own(tar, {})
type.patch(tar, {id: 30, name: `two`, undeclared0: 40, undeclared1: 50, constructor: 60})
t.own(tar, {undeclared0: 40, undeclared1: 50})
type.patch(tar, {})
t.own(tar, {undeclared0: 40, undeclared1: 50})
})
t.test(function test_mut() {
const type = Tar.getType()
const tar = new Tar()
testStructTypeReset(tar, type.mut.bind(type))
type.mut(tar, {id: 30, name: `two`, undeclared0: 40, undeclared1: 50, constructor: 60})
t.own(tar, {id: 30, name: `two`, undeclared0: 40, undeclared1: 50})
})
t.test(function test_fallback() {
class Tar extends o.Struct {
static get Type() {return Type}
static spec = {id: l.reqIntPos}
static any(val) {return val + 1}
}
const type = Tar.getType()
const tar = new Tar()
type.patch(tar, {id: 10, undeclared0: 20, undeclared1: 30})
t.own(tar, {undeclared0: 21, undeclared1: 31})
type.mut(tar, {id: 10, undeclared0: 40, undeclared1: 50})
t.own(tar, {id: 10, undeclared0: 41, undeclared1: 51})
})
t.test(function test_fields_implementing_isMut() {
class SubDeclared extends o.StructLax {}
class SubUndeclared extends o.StructLax {}
class Tar extends o.Struct {
static get Type() {return Type}
static spec = {
id(val) {return l.reqIntPos(val)},
declared(val) {return l.toInstOpt(val, SubDeclared)},
}
static any(val) {return l.toInstOpt(val, SubUndeclared)}
}
const type = Tar.getType()
const tar = new Tar()
type.mut(tar, {id: 10, declared: {one: 20}, undeclared: {two: 30}})
const {declared, undeclared} = tar
t.own(tar, {id: 10, declared, undeclared})
t.is(tar.id, 10)
t.inst(declared, SubDeclared)
t.own(declared, {one: 20})
t.inst(undeclared, SubUndeclared)
t.own(undeclared, {two: 30})
function unmodified() {
t.own(declared, {one: 20, three: 50})
t.own(undeclared, {two: 30, four: 60})
}
type.mut(tar, {id: 40, declared: {three: 50}, undeclared: {four: 60}})
t.own(tar, {id: 40, declared, undeclared})
t.is(tar.declared, declared)
t.is(tar.undeclared, undeclared)
unmodified()
t.throws(() => type.mut(tar, {id: 40, declared: 50}), TypeError, `expected variant of isStruct, got 50`)
t.throws(() => type.mut(tar, {id: 40, declared: {}, undeclared: 50}), TypeError, `expected variant of isStruct, got 50`)
t.own(tar, {id: 40, declared, undeclared})
t.is(tar.declared, declared)
t.is(tar.undeclared, undeclared)
unmodified()
type.mut(tar, {id: 70})
t.own(tar, {id: 70, declared: undefined, undeclared})
t.is(tar.undeclared, undeclared)
unmodified()
type.mut(tar, {id: 70, undeclared: undefined})
// t.own(tar, {id: 70, declared: undefined, undeclared: undefined})
t.own(tar, {id: 70, declared: undefined, undeclared})
t.is(tar.undeclared, undeclared)
unmodified()
})
})
function testStructTypeReset(tar, fun) {
t.throws(() => fun(tar), TypeError, `invalid property "id"`)
t.own(tar, {})
t.throws(() => fun(tar, {id: undefined}), TypeError, `invalid property "id"`)
t.own(tar, {})
t.throws(() => fun(tar, {id: 10}), TypeError, `invalid property "name"`)
t.own(tar, {id: 10})
t.throws(() => fun(tar, {id: 10, name: undefined}), TypeError, `invalid property "name"`)
t.own(tar, {id: 10})
t.throws(() => fun(tar, {name: `str`}), TypeError, `invalid property "id"`)
t.own(tar, {id: 10})
fun(tar, {id: 20, name: `one`})
t.own(tar, {id: 20, name: `one`})
}
t.test(function test_Struct() {
t.test(function test_undeclared() {
class Tar extends o.Struct {}
const tar = new Tar({one: 10, two: 20})
t.own(tar, {})
tar.mut({three: 30, four: 40})
t.own(tar, {})
})
/*
Subclasses of `Struct` inherit exactly two properties: `.constructor` and
`.mut`. A spec may explicitly override an inherited method such as `.mut`,
and this must not break the built-in behavior of the constructor.
*/
t.test(function test_opt_in_shadowing() {
class Tar extends o.Struct {
static spec = {
mut: l.reqIntPos,
one: l.reqIntPos,
}
two() {}
}
const tar = new Tar({mut: 10, one: 20, two: 30, three: 40})
t.own(tar, {mut: 10, one: 20})
})
t.test(function test_declared() {
class Tar extends o.Struct {
static spec = {
one: l.reqIntPos,
two: l.laxBool,
three: l.optStr,
}
}
testStructDeclaredProperties(Tar)
t.own(new Tar({one: 10, undeclared: 20}), {one: 10, two: false, three: undefined})
t.own(new Tar({one: 10}).mut({one: 20, undeclared: 30}), {one: 20, two: false, three: undefined})
})
t.test(function test_collision_nil() {
class Three extends o.Struct {
static spec = {one: undefined}
get one() {return 10}
}
const tar = new Three({one: 20, undeclared: {two: 30}})
t.is(tar.one, 10)
t.own(tar, {})
})
t.test(function test_collision_method() {
class One extends o.Struct {
static spec = {one: l.reqIntPos}
one() {}
}
testStructFieldCollision(One)
class Two extends o.Struct {static spec = {one: l.reqIntPos}}
class Three extends Two {one() {}}
testStructFieldCollision(Three)
})
t.test(function test_collision_getter() {
class One extends o.Struct {
static spec = {one: l.reqIntPos}
get one() {}
}
testStructFieldCollision(One)
class Two extends o.Struct {static spec = {one: l.reqIntPos}}
class Three extends Two {get one() {}}
testStructFieldCollision(Three)
})
})
t.test(function test_StructLax() {
t.test(function test_undeclared() {
class Tar extends o.StructLax {}
const tar = new Tar({one: 10, two: 20})
t.own(tar, {one: 10, two: 20})
tar.mut({three: 30, four: 40})
t.own(tar, {one: 10, two: 20, three: 30, four: 40})
})
/*
Subclasses of `StructLax` inherit exactly two properties: `.constructor` and
`.mut`. By default, we don't shadow any inherited properties, in accordance
with `patch` semantics. However, we allow to explicitly define fields that
override/shadow inherited properties.
*/
t.test(function test_no_accidental_shadowing() {
class Tar extends o.StructLax {}
t.own(new Tar({constructor: 10, mut: 20, one: 30}), {one: 30})
})
t.test(function test_opt_in_shadowing() {
class Tar extends o.StructLax {
static spec = {
mut: l.reqIntPos,
one: l.reqIntPos,
}
two() {}
}
const tar = new Tar({mut: 10, one: 20, two: 30, three: 40})
t.own(tar, {mut: 10, one: 20, three: 40})
})
t.test(function test_declared() {
class Tar extends o.StructLax {
static spec = {
one: l.reqIntPos,
two: l.laxBool,
three: l.optStr,
}
}
testStructDeclaredProperties(Tar)
t.own(new Tar({one: 10, undeclared: 20}), {one: 10, two: false, three: undefined, undeclared: 20})
t.own(new Tar({one: 10}).mut({one: 20, undeclared: 30}), {one: 20, two: false, three: undefined, undeclared: 30})
})
})
function testStructDeclaredProperties(Tar) {
t.throws(() => new Tar(), TypeError, `invalid property "one"`)
t.throws(() => new Tar({one: undefined}), TypeError, `invalid property "one"`)
t.throws(() => new Tar({one: 10, two: 20}), TypeError, `invalid property "two"`)
t.throws(() => new Tar({one: 10, three: 30}), TypeError, `invalid property "three"`)
const tar = new Tar({one: 10})
t.own(tar, {one: 10, two: false, three: undefined})
tar.mut()
t.own(tar, {one: 10, two: false, three: undefined})
t.throws(() => tar.mut({one: undefined}), TypeError, `invalid property "one"`)
t.own(tar, {one: 10, two: false, three: undefined})
t.throws(() => tar.mut({one: 10, two: 20}), TypeError, `invalid property "two"`)
t.own(tar, {one: 10, two: false, three: undefined})
t.throws(() => tar.mut({one: 10, three: 30}), TypeError, `invalid property "three"`)
t.own(tar, {one: 10, two: false, three: undefined})
tar.mut({one: 20})
t.own(tar, {one: 20, two: false, three: undefined})
tar.mut({one: 20, two: true, three: ``})
t.own(tar, {one: 20, two: true, three: ``})
}
function testStructFieldCollision(cls) {
t.throws(() => cls.getType(), TypeError, `property collision on "one" in ` + l.show(cls))
t.throws(() => new cls({one: 10}), TypeError, `property collision on "one" in ` + l.show(cls))
}
t.test(function test_StructLax_inheritance() {
t.test(function test_define_then_instantiate() {
class Super extends o.StructLax {
static spec = {one: l.reqIntPos}
}
class Sub extends Super {
static spec = {
...super.spec,
two: l.reqStr,
}
}
t.throws(() => new Super({one: `10`}), TypeError, `invalid property "one"`)
t.own(new Super({one: 10, two: 20}), {one: 10, two: 20})
t.throws(() => new Sub({one: 10, two: 20}), TypeError, `invalid property "two"`)
t.own(new Sub({one: 10, two: `20`}), {one: 10, two: `20`})
})
t.test(function test_instantiate_then_define() {
class Super extends o.StructLax {
static spec = {one: l.reqIntPos}
}
t.throws(() => new Super({one: `10`}), TypeError, `invalid property "one"`)
t.own(new Super({one: 10, two: 20}), {one: 10, two: 20})
class Sub extends Super {
static spec = {
...super.spec,
two: l.reqStr,
}
}
t.throws(() => new Sub({one: 10, two: 20}), TypeError, `invalid property "two"`)
t.own(new Sub({one: 10, two: `20`}), {one: 10, two: `20`})
})
})
t.test(function test_Struct_inheritance_remove_declared_property() {
class Super extends o.Struct {
static spec = {
one: l.reqIntPos,
two: l.reqIntPos,
}
}
class Sub extends Super {
static spec = {
...super.spec,
two: undefined,
}
}
t.throws(() => new Super({one: 10}), TypeError, `invalid property "two"`)
t.own(new Super({one: 10, two: 20, three: 30}), {one: 10, two: 20})
t.own(new Sub({one: 10}), {one: 10})
t.own(new Sub({one: 10, two: 20}), {one: 10})
t.own(new Sub({one: 10, two: `20`}), {one: 10})
})
t.test(function test_StructLax_recursive_mut() {
t.test(function test_undeclared() {
class Inner extends o.StructLax {}
class Middle extends o.StructLax {}
class Outer extends o.StructLax {}
const inner = new Inner({one: 10})
const middle = new Middle({two: 20, inner})
const outer = new Outer({three: 30, middle})
t.is(outer.middle, middle)
t.is(middle.inner, inner)
t.eq(inner, new Inner({one: 10}))
t.eq(middle, new Middle({two: 20, inner}))
t.eq(outer, new Outer({three: 30, middle}))
outer.mut({
three: 40,
middle: {two: 50, inner: {one: 60, four: 70}},
five: {six: 80},
})
t.is(outer.middle, middle)
t.is(middle.inner, inner)
t.eq(inner, new Inner({one: 60, four: 70}))
t.eq(middle, new Middle({two: 50, inner: new Inner({one: 60, four: 70})}))
function stable() {
t.eq(outer, new Outer({
three: 40,
middle: new Middle({two: 50, inner: new Inner({one: 60, four: 70})}),
five: {six: 80},
}))
}
stable()
// Note: `outer.middle.inner` implements `.mut`, which must be invoked here,
// passing nil. As a result, the structure is unchanged.
outer.mut({middle: {inner: undefined}})
stable()
// Note: `outer.middle` implements `.mut`, which must be invoked here,
// passing nil. As a result, the structure is unchanged.
outer.mut({middle: undefined})
stable()
})
t.test(function test_declared() {
class Inner extends o.StructLax {
static spec = {
one: l.reqIntPos,
}
}
class Middle extends o.StructLax {
static spec = {
two: l.reqIntPos,
inner(val) {return l.toInstOpt(val, Inner)},
}
}
class Outer extends o.StructLax {
static spec = {
three: l.reqIntPos,
middle(val) {return l.toInstOpt(val, Middle)},
}
}
const outer = new Outer({three: 30, middle: {two: 20, inner: {one: 10}}})
const middle = outer.middle
const inner = middle.inner
t.eq(inner, new Inner({one: 10}))
t.eq(middle, new Middle({two: 20, inner}))
t.eq(outer, new Outer({three: 30, middle}))
outer.mut({
three: 40,
middle: {two: 50, inner: {one: 60, four: 70}},
five: {six: 80},
})
t.is(outer.middle, middle)
t.is(middle.inner, inner)
t.eq(inner, new Inner({one: 60, four: 70}))
t.eq(middle, new Middle({two: 50, inner: new Inner({one: 60, four: 70})}))
t.eq(outer, new Outer({
three: 40,
middle: new Middle({two: 50, inner: new Inner({one: 60, four: 70})}),
five: {six: 80},
}))
})
})
t.test(function test_StructLax_with_mem_getter() {
class Inner extends o.StructLax {}
class Outer extends o.StructLax {
static {o.memGet(this)}
get inner() {return new Inner()}
}
function testWithMemGetter(make) {
const outer = make()
const inner = outer.inner
t.inst(inner, Inner)
t.own(inner, {two: 20})
t.inst(outer, Outer)
t.own(outer, {one: 10, inner})
outer.mut({inner: {two: 30}})
t.is(outer.inner, inner)
t.eq(inner, new Inner({two: 30}))
}
t.test(function test_constructor() {
testWithMemGetter(function make() {
return new Outer({one: 10, inner: {two: 20}})
})
})
t.test(function test_mut() {
testWithMemGetter(function make() {
return new Outer().mut({one: 10, inner: {two: 20}})
})
})
})
// Incomplete: tests only `new` but not `.mut`. TODO test `.mut`.
t.test(function test_StructLax_getters_and_setters() {
function testFields(Tar) {
t.own(new Tar({one: 10}), {one: 10})
t.is(new Tar({one: 10}).inner, `default`)
t.own(new Tar({one: 10, inner: 20}), {one: 10})
}
t.test(function test_undeclared_only_getter() {
class Tar extends o.StructLax {
get inner() {return `default`}
}
testFields(Tar)
})
t.test(function test_undeclared_getter_setter() {
class Tar extends o.StructLax {
get inner() {return `default`}
set inner(_) {throw l.errImpl()}
}
testFields(Tar)
})
t.test(function test_override_getter() {
class Super extends o.StructLax {
get inner() {return `default`}
get one() {return 123}
}
class Sub extends Super {
static spec = {
...super.spec,
one: l.reqIntPos,
}
}
testFields(Sub)
})
t.test(function test_override_setter() {
class Super extends o.StructLax {
get inner() {return `default`}
set one(_) {}
}
class Sub extends Super {
static spec = {
...super.spec,
one: l.reqIntPos,
}
}
testFields(Sub)
})
t.test(function test_override_getter_and_setter() {
class Super extends o.StructLax {
get inner() {return `default`}
get one() {return 123}
set one(_) {}
}
class Sub extends Super {
static spec = {
...super.spec,
one: l.reqIntPos,
}
}
t.throws(() => new Sub({}), TypeError, `invalid property "one"`)
testFields(Sub)
})
/*
We support overriding an inherited field with an explicitly defined field
in a spec, but we don't allow to accidentally override a spec field with
a getter or method on the class prototype, even if the spec is inherited.
*/
t.test(function test_declared_getter_in_subclass() {
class Super extends o.StructLax {
static spec = {one: l.laxFin}
}
t.throws(() => new Super({one: `10`}), TypeError, `invalid property "one"`)
t.own(new Super(), {one: 0})
t.own(new Super({one: 10}), {one: 10})
class Sub extends Super {
get one() {return `default`}
}
testStructFieldCollision(Sub)
})
/*
We support overriding an inherited field with an explicitly defined field
in a spec, but we don't allow to accidentally override a spec field with
a getter or method on the class prototype, even if the spec is inherited.
*/
t.test(function test_declared_getter_setter() {
class Super extends o.StructLax {
static spec = {one: l.laxFin}
}
class Sub extends Super {
get one() {return this.secondary}
set one(val) {this.secondary = val + 1}
}
testStructFieldCollision(Sub)
})
t.test(function test_override_inherited_getter_with_field() {
class Super extends o.StructLax {
get one() {return 10}
get inner() {return `default`}
}
t.own(new Super(), {})
t.own(new Super({one: 20}), {})
class Sub extends Super {
static spec = {
...super.spec,
one: l.laxFin,
}
}
t.throws(() => new Sub({one: `10`}), TypeError, `invalid property "one"`)
t.own(new Sub({one: 20}), {one: 20})
testFields(Sub)
})
})
/*
The current implementation ignores symbolic properties in the spec.
This behavior is accidental. We lock it down by a test for API stability.
Fixing would involve runtime overheads, which seems not worth it.
*/
t.test(function test_StructLax_symbols() {
const one = Symbol.for(`one`)
const two = Symbol.for(`two`)
class Tar extends o.StructLax {
static spec = {[one]: l.reqIntPos}
}
const tar = new Tar({[one]: 10, [two]: 20})
t.own(tar, {})
})
t.test(function test_memGet() {
t.test(function test_only_ancestor() {
const [Anc, Mid, Des] = testInitMem()
o.memGet(Anc)
const ref = new Des()
t.eq(Object.keys(ref), [])
t.is(ref.anc0, ref.anc0)
t.eq(Object.keys(ref), [`anc0`])
t.is(ref.anc1, ref.anc1)
t.eq(Object.keys(ref), [`anc0`, `anc1`])
t.isnt(ref.mid0, ref.mid0)
t.isnt(ref.mid1, ref.mid1)
t.isnt(ref.des0, ref.des0)
t.isnt(ref.des1, ref.des1)
testClearPrototype(Anc, Mid, Des)
})
t.test(function test_only_descendant() {
const [Anc, Mid, Des] = testInitMem()
o.memGet(Des)
const ref = new Des()
t.eq(Object.keys(ref), [])
t.isnt(ref.anc0, ref.anc0)
t.isnt(ref.anc1, ref.anc1)
t.isnt(ref.mid0, ref.mid0)
t.isnt(ref.mid1, ref.mid1)
t.is(ref.des0, ref.des0)
t.eq(Object.keys(ref), [`des0`])
t.is(ref.des1, ref.des1)
t.eq(Object.keys(ref), [`des0`, `des1`])
testClearPrototype(Anc, Mid, Des)
})
t.test(function test_all() {
const [Anc, Mid, Des] = testInitMem()
o.memGet(Anc)
o.memGet(Mid)
o.memGet(Des)
const ref = new Des()
t.eq(Object.keys(ref), [])
t.is(ref.anc0, ref.anc0)
t.eq(Object.keys(ref), [`anc0`])
t.is(ref.anc1, ref.anc1)
t.eq(Object.keys(ref), [`anc0`, `anc1`])
t.is(ref.mid0, ref.mid0)
t.eq(Object.keys(ref), [`anc0`, `anc1`, `mid0`])
t.is(ref.mid1, ref.mid1)
t.eq(Object.keys(ref), [`anc0`, `anc1`, `mid0`, `mid1`])
t.is(ref.des0, ref.des0)
t.eq(Object.keys(ref), [`anc0`, `anc1`, `mid0`, `mid1`, `des0`])
t.is(ref.des1, ref.des1)
t.eq(Object.keys(ref), [`anc0`, `anc1`, `mid0`, `mid1`, `des0`, `des1`])
testClearPrototype(Anc, Mid, Des)
})
t.test(function test_set() {
const [Anc, Mid, Des] = testInitMem()
o.memGet(Anc)
const ref = new Des()
const manual = Symbol(`manual`)
ref.anc0 = manual
t.is(ref.anc0, manual)
t.eq(Object.keys(ref), [`anc0`])
t.is(ref.anc1, ref.anc1)
t.eq(Object.keys(ref), [`anc0`, `anc1`])
testClearPrototype(Anc, Mid, Des)
})
})
function testInitMem() {
class Anc {
get anc0() {return Symbol(`anc0`)}
get anc1() {return Symbol(`anc1`)}
}
class Mid extends Anc {
get mid0() {return Symbol(`mid0`)}
get mid1() {return Symbol(`mid1`)}
}
class Des extends Mid {
get des0() {return Symbol(`des0`)}
get des1() {return Symbol(`des1`)}
}
return [Anc, Mid, Des]
}
function testClearPrototype(...classes) {
for (const Cls of classes) {
t.eq(Object.keys(Cls.prototype), [])
}
}
t.test(function test_MixMain() {
class Super extends o.MixMain(l.Emp) {}
class Sub extends Super {}
t.inst(Super.main, Super)
t.is(Super.main, Super.main)