-
Notifications
You must be signed in to change notification settings - Fork 5
/
datagraph.tests.coffee
2359 lines (1737 loc) · 92.7 KB
/
datagraph.tests.coffee
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
support = require './support.coffee'
util = require '../Source/utilities.coffee'
assert = require 'assert'
sinon = require 'sinon'
expect = require('sinon-expect').enhance require('expect.js'), sinon, 'was'
__ = sinon.match
describe "Paws' Data types:", ->
Paws = require "../Source/Paws.coffee"
{ Reactor, parse
, Thing, Label, Execution, Native
, ThingSet, Relation, Liability, Combination, Position, Mask, Operation
, ResponsibilityError } = Paws
{ Context, Sequence, Expression } = parse
describe 'Thing', -> # ---- ---- ---- ---- ---- Thing
# FIXME: A lot of these tests are more ... integration-ey than unit-ey; and to boot, a lot of
# *actual* unit tests are absent. I need to extract some of these complicated,
# purpose-related tests into an *integration* suite, and then re-build this suite from
# scratch to achieve ≥80% *unit* coverage.
# ### Thing: Core functionality ###
it 'exists', ->
expect(Thing).to.be.ok()
expect(Thing).to.be.a 'function'
it 'has a UUID', ->
uuid_regex = /[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}/
expect((new Thing).id).to.match uuid_regex
it 'compares by identity', ->
thing1 = new Label 'foo'
thing2 = new Label 'foo'
expect(Thing::compare.call thing1, thing1).to.be true
expect(Thing::compare.call thing1, thing2).to.not.be true
it 'can easily construct a Relation *to* itself', ->
a_parent = new Thing; a_child = new Thing; another_child = new Thing
result = a_child.owned_by a_parent
expect(result).to.be.a Relation
expect(result).to.be.owned()
expect(result.from).to.be a_parent
expect(result.to) .to.be a_child
result = another_child.contained_by a_parent
expect(result).to.be.a Relation
expect(result).not.to.be.owned()
expect(result.from).to.be a_parent
expect(result.to) .to.be another_child
describe '~ Metadata', ->
it 'is an ordered list of metadata', ->
thing = new Thing
expect(thing).to.have.property 'metadata'
expect(thing.metadata).to.be.an 'array'
it 'has an implicit slot for the noughty by default', ->
thing = new Thing
expect(thing.metadata).to.have.length 1
expect(thing.metadata[0]).to.be undefined
it 'can be configured with no noughty-slot', ->
bare_thing = new Thing.with(noughtify: no)()
expect(bare_thing.metadata).to.have.length 0
it 'stores Relations to other Things', ->
child1 = new Thing; child2 = new Thing
thing = new Thing child1, child2
expect(thing).to.have.property 'metadata'
expect(thing.metadata).to.be.an 'array'
expect(thing.metadata[1]).to.be.a Relation
expect(thing.metadata[1].to).to.be child1
expect(thing.metadata[2]).to.be.a Relation
expect(thing.metadata[2].to).to.be child2
it 'tracks ownership', ->
a_parent = new Thing; a_child = new Thing; another_child = new Thing
a_parent = new Thing a_child.owned_by(a_parent), another_child
expect(a_parent.metadata[1]).to.be.owned()
expect(a_parent.metadata[2]).to.not.be.owned()
a_parent.disown_at 1
expect(a_parent.metadata[1]).to.not.be.owned()
expect(a_child.owners).to.be.empty()
a_parent.own_at 2
expect(a_parent.metadata[2]).to.be.owned()
expect(another_child.owners).to.not.be.empty()
expect(another_child.is_owned_by a_parent).to.be yes
describe '~ Backlinks', ->
it 'are created on children added as ‘owned’', ->
a_parent = new Thing; a_child = new Thing; another_child = new Thing; other = new Thing
expect(a_child).to.have.property 'owners'
expect(a_child.owners).to.be.an 'array'
expect(a_child.owners).to.have.length 0
a_parent.push a_child.owned_by(a_parent), other, another_child.owned_by(a_parent)
expect(a_child).to.have.property 'owners'
expect(a_child.owners).to.be.an 'array'
expect(a_child.owners).to.have.length 1
expect(a_child.owners[0]).to.be.a Relation
expect(a_child.owners).to.contain a_parent.metadata[1]
expect(another_child.owners).to.contain a_parent.metadata[3]
expect(other.owners).to.be.empty()
it 'are maintained by other metadata-modifying operations', ->
a_parent = new Thing; a_child = new Thing; another_child = new Thing
a_parent.unshift a_child.owned_by(a_parent)
rel = a_parent.metadata[1]
expect(a_child.owners).to.contain rel
a_parent.set 1, another_child.owned_by(a_parent)
expect(a_parent.metadata[1]).to.not.equal rel
expect(another_child.owners).to.contain a_parent.metadata[1]
it 'are discarded when the child is removed', ->
first = new Thing; second = new Thing; third = new Thing
a_parent = new Thing first.owned_by(a_parent), second.owned_by(a_parent), third.owned_by(a_parent)
expect(first.owners) .to.have.length 1
expect(second.owners).to.have.length 1
expect(third.owners) .to.have.length 1
a_parent.set 2, undefined
expect(second.owners).to.have.length 0
a_parent.shift()
expect(first.owners) .to.have.length 0
a_parent.pop()
expect(third.owners) .to.have.length 0
it 'are removed *and* added when directly setting elements by index', ->
a_parent = new Thing; a_child = new Thing; another_child = new Thing; other = new Thing
a_parent.push a_child.owned_by(a_parent), other
original_relation = a_parent.metadata[1]
expect(a_child.owners).to.contain original_relation
a_parent.set 1, another_child.owned_by(a_parent)
expect(a_parent.metadata[1]).to.not.be original_relation
expect(a_child.owners).to.be.empty()
expect(another_child.owners).to.contain a_parent.metadata[1]
describe '~ Responsibility', ->
it 'is expressed as a set of current ‘custodians’', ->
a Thing
expect(a.thing).to.have.property 'custodians'
# ### Thing: Metadata methods ###
describe '::set', ->
it 'changes a metadata value', ->
child = new Thing
a_thing = new Thing child
expect(a_thing.at(1)).to.be child
a_thing.set 1, new Thing
expect(a_thing.at(1)).to.not.be child
it 'accepts a Thing', ->
replacement = new Thing
a_thing = new Thing(new Thing)
a_thing.set 1, replacement
expect(a_thing.at 1).to.be replacement
it 'accepts a Relation', ->
replacement = new Thing
a_thing = new Thing(new Thing)
a_thing.set 1, (new Relation a_thing, replacement)
expect(a_thing.at 1).to.be replacement
it 'accepts nothingness to signal removal', ->
a_thing = new Thing(new Thing)
a_thing.set 1, undefined
expect(a_thing.at 1).to.be undefined
it 'does not directly use the passed Relation object', ->
replacement = new Thing
a_relation = new Relation a_thing, replacement
a_thing = new Thing(new Thing)
a_thing.set 1, a_relation
expect(a_thing.metadata[1]).to.not.be a_relation
it 'changes the noughty', ->
replacement = new Thing
a_thing = new Thing
a_thing.set 0, replacement
expect(a_thing.at 0).to.be replacement
it 'changes values at any index', ->
child1 = new Thing; child2 = new Thing; child3 = new Thing
replacement = new Thing
a_thing = new Thing child1, child2, child3
expect(a_thing.at 2).to.be child2
a_thing.set 2, replacement
expect(a_thing.at 2).to.be replacement
it 'changes values at an arbitrary index, higher than that of any existing elements', ->
child = new Thing
a_thing = new Thing
expect(a_thing.at 1337).to.be undefined
a_thing.set 1337, child
expect(a_thing.at 1337).to.be child
describe '(ownership)', ->
it 'creates backlinks on newly-owned nodes', ->
a Thing; another Thing
expect(another.thing.is_owned_by a.thing).to.be no
a.thing.set(1, another.thing.owned_by a.thing)
expect(another.thing.is_owned_by a.thing).to.be yes
it 'removes backlinks on replaced nodes', ->
a Thing; another Thing
a.thing.set(1, another.thing.owned_by a.thing)
expect(another.thing.is_owned_by a.thing).to.be yes
a.thing.set(1, (some Thing).owned_by a.thing)
expect(another.thing.is_owned_by a.thing).to.be no
describe '(responsibility)', ->
it 'dedicates newly-added-as-owned nodes to an existing custodian of the parent', ->
a Thing; another Thing
an Execution
a.thing.dedicate(new Liability an.execution, a.thing)
expect(a.thing .belongs_to an.execution, 'read').to.be yes
expect(another.thing.belongs_to an.execution, 'read').to.be no
a.thing.set 1, another.thing.owned_by(a.thing)
expect(another.thing.belongs_to an.execution, 'read').to.be yes
it 'does not dedicate added-as-contained nodes to an existing custodian of the parent', ->
a Thing; another Thing
an Execution
a.thing.dedicate(new Liability an.execution, a.thing)
expect(a.thing .belongs_to an.execution, 'read').to.be yes
expect(another.thing.belongs_to an.execution, 'read').to.be no
a.thing.set 1, another.thing.contained_by(a.thing)
expect(another.thing.belongs_to an.execution, 'read').to.be no
it 'dedicates a newly-added-as-owned nodes to *multiple* custodians of the parent', ->
a Thing; another Thing
an Execution; another Execution
a.thing.dedicate(new Liability an.execution, a.thing)
a.thing.dedicate(new Liability another.execution, a.thing)
expect(another.thing.belongs_to another.execution, 'read').to.be no
a.thing.set 1, another.thing.owned_by(a.thing)
expect(another.thing.belongs_to an.execution, 'read').to.be yes
expect(another.thing.belongs_to another.execution, 'read').to.be yes
it 'emancipates replaced nodes from an existing custodian of the parent', ->
a Thing; another Thing; some Thing
an Execution
a.thing.dedicate(new Liability an.execution, a.thing)
a.thing.set 1, another.thing.owned_by(a.thing)
expect(a.thing .belongs_to an.execution, 'read').to.be yes
expect(another.thing.belongs_to an.execution, 'read').to.be yes
expect(some.thing .belongs_to an.execution, 'read').to.be no
a.thing.set 1, some.thing.owned_by(a.thing)
expect(another.thing.belongs_to an.execution, 'read').to.be no
describe '(errors)', ->
it 'checks availability, and throws an ResponsibilityError if there is a conflict', ->
a Thing; another Thing
an Execution; another Execution
a.thing .dedicate new Liability(an.execution, a.thing, 'write')
another.thing.dedicate new Liability(another.execution, another.thing, 'read')
expect(-> a.thing.set 1, another.thing.owned_by(a.thing)).to
.throwException ResponsibilityError
expect(a.thing.at 1).to.be undefined
expect(a.thing .belongs_to an.execution, 'write').to.be yes
expect(another.thing.belongs_to another.execution, 'read').to.be yes
expect(another.thing.belongs_to an.execution, 'read').to.be no
describe '::push', ->
it 'adds metadata values', ->
a Thing; another Thing
expect(a.thing.metadata).to.have.length 1
a.thing.push another.thing
expect(a.thing.metadata).to.have.length 2
it 'accepts Things', ->
a Thing; another Thing
a.thing.push another.thing
expect(a.thing.at 1).to.be another.thing
it 'accepts Relations', ->
a Thing; another Thing
a.thing.push(new Relation a.thing, another.thing)
expect(a.thing.at 1).to.be another.thing
it 'accepts multiple arguments', ->
a Thing; a Relation, a.thing, new Thing; another Thing
a.thing.push a.thing, a.relation, another.thing
expect(a.thing.at 1).to.be a.thing
expect(a.thing.at 3).to.be another.thing
it 'defaults pushed Things to not being owned', ->
a Thing; another Thing
a.thing.push another.thing
expect(a.thing.owns_at 1).to.be no
it 'respects the declared ownership of passed Relations', ->
a Thing; another Thing
a.thing.push a.thing.owned_by(another.thing)
expect(a.thing.owns_at 1).to.be yes
it 'does not directly use passed Relation objects', ->
a Thing; another Thing
a Relation, a.thing, another.thing
a.thing.push a.relation
expect(a.thing.at 1).to.be another.thing
expect(a.thing.metadata[1]).to.not.be a.relation
it 'can add a noughty', ->
a_thing = new Thing.with(noughtify: no)()
noughty = new Thing
expect(a_thing.metadata).to.have.length 0
a_thing.push noughty
expect(a_thing.metadata).to.have.length 1
describe '(ownership)', ->
it 'creates backlinks on newly-owned nodes', ->
a Thing; another Thing
expect(another.thing.is_owned_by a.thing).to.be no
a.thing.set(1, another.thing.owned_by a.thing)
expect(another.thing.is_owned_by a.thing).to.be yes
it 'removes backlinks on replaced nodes', ->
a Thing; another Thing
a.thing.set(1, another.thing.owned_by a.thing)
expect(another.thing.is_owned_by a.thing).to.be yes
a.thing.set(1, (some Thing).owned_by a.thing)
expect(another.thing.is_owned_by a.thing).to.be no
describe '(ownership)', ->
it 'creates backlinks on any newly-owned nodes', ->
a Thing; some Thing; another Thing
expect(another.thing.is_owned_by a.thing).to.be no
a.thing.push some.thing, another.thing.owned_by(a.thing)
expect(another.thing.is_owned_by a.thing).to.be yes
expect(some.thing .is_owned_by a.thing).to.be no
describe '(responsibility)', ->
it 'dedicates newly-added-as-owned nodes to an existing custodian of the parent', ->
a Thing; another Thing
an Execution
a.thing.dedicate(new Liability an.execution, a.thing)
expect(a.thing .belongs_to an.execution, 'read').to.be yes
expect(another.thing.belongs_to an.execution, 'read').to.be no
a.thing.push another.thing.owned_by(a.thing)
expect(another.thing.belongs_to an.execution, 'read').to.be yes
it 'does not dedicate added-as-contained nodes to an existing custodian of the parent', ->
a Thing; some Thing; another Thing
an Execution
a.thing.dedicate(new Liability an.execution, a.thing)
expect(a.thing .belongs_to an.execution, 'read').to.be yes
expect(another.thing.belongs_to an.execution, 'read').to.be no
a.thing.push some.thing.owned_by(a.thing), another.thing
expect(another.thing.belongs_to an.execution, 'read').to.be no
it 'dedicates a newly-added-as-owned nodes to *multiple* custodians of the parent', ->
a Thing; another Thing
an Execution; another Execution
a.thing.dedicate(new Liability an.execution, a.thing)
a.thing.dedicate(new Liability another.execution, a.thing)
expect(another.thing.belongs_to another.execution, 'read').to.be no
a.thing.push another.thing.owned_by(a.thing)
expect(another.thing.belongs_to an.execution, 'read').to.be yes
expect(another.thing.belongs_to another.execution, 'read').to.be yes
# phew.
it 'dedicates *multiple* owned nodes to *multiple* custodians', ->
a Thing; some Thing; another Thing
an Execution; another Execution
a.thing.dedicate(new Liability an.execution, a.thing)
a.thing.dedicate(new Liability another.execution, a.thing)
expect(some.thing.belongs_to an.execution, 'read').to.be no
expect(some.thing.belongs_to another.execution, 'read').to.be no
expect(another.thing.belongs_to an.execution, 'read').to.be no
expect(another.thing.belongs_to another.execution, 'read').to.be no
a.thing.push some.thing.owned_by(a.thing), another.thing.owned_by(a.thing)
expect(some.thing.belongs_to an.execution, 'read').to.be yes
expect(some.thing.belongs_to another.execution, 'read').to.be yes
expect(another.thing.belongs_to an.execution, 'read').to.be yes
expect(another.thing.belongs_to another.execution, 'read').to.be yes
describe '(errors)', ->
it 'checks availability, and throws an ResponsibilityError if there is a conflict', ->
a Thing; another Thing
an Execution; another Execution
a.thing .dedicate new Liability(an.execution, a.thing, 'write')
another.thing.dedicate new Liability(another.execution, another.thing, 'read')
expect(-> a.thing.push another.thing.owned_by(a.thing)).to
.throwException ResponsibilityError
expect(a.thing.at 1).to.be undefined
expect(a.thing .belongs_to an.execution, 'write').to.be yes
expect(another.thing.belongs_to another.execution, 'read').to.be yes
expect(another.thing.belongs_to an.execution, 'read').to.be no
# XXX: This is not thoroughly tested, due to the amount of duplication with ::push.
describe '::unshift', ->
it 'adds metadata values', ->
a Thing; another Thing
expect(a.thing.metadata).to.have.length 1
a.thing.unshift another.thing
expect(a.thing.metadata).to.have.length 2
it 'places added values after the noughty', ->
a Thing; another Thing
a.thing.unshift another.thing
expect(a.thing.at 0).to.not.be another.thing
expect(a.thing.at 1).to.be another.thing
it 'places added values before existing elements', ->
a Thing; some Thing; another Thing
a.thing.unshift some.thing
expect(a.thing.at 1).to.be some.thing
a.thing.unshift another.thing
expect(a.thing.at 1).to.be another.thing
expect(a.thing.at 2).to.be some.thing
it 'accepts multiple arguments', ->
a Thing; a Relation, a.thing, new Thing; another Thing
a.thing.unshift a.thing, a.relation, another.thing
expect(a.thing.at 1).to.be a.thing
expect(a.thing.at 3).to.be another.thing
it 'defaults prepended Things to not being owned', ->
a Thing; another Thing
a.thing.unshift another.thing
expect(a.thing.owns_at 1).to.be no
it 'respects the declared ownership of passed Relations', ->
a Thing; another Thing
a.thing.unshift a.thing.owned_by(another.thing)
expect(a.thing.owns_at 1).to.be yes
it 'does not directly use passed Relation objects', ->
a Thing; another Thing
a Relation, a.thing, another.thing
a.thing.unshift a.relation
expect(a.thing.at 1).to.be another.thing
expect(a.thing.metadata[1]).to.not.be a.relation
it 'can add a noughty', ->
a_thing = new Thing.with(noughtify: no)()
noughty = new Thing
expect(a_thing.metadata).to.have.length 0
a_thing.unshift noughty
expect(a_thing.metadata).to.have.length 1
describe '::clone', ->
it 'creates a new Thing', ->
thing = new Thing
expect(thing.clone()).to.not.be thing
it 'duplicates the metadata of the receiver', ->
thing = new Thing new Thing, new Thing, new Thing
clone = thing.clone()
expect(clone.metadata).to.have.length 4
clone.metadata.forEach (rel, i) -> if rel
expect(clone.at i).to.be.ok()
expect(rel).not.to.be thing.metadata[i]
expect(rel.to).to.be thing.metadata[i].to
it 'updates the `from`-linkage on the copied metadata', ->
thing = new Thing new Thing, new Thing, new Thing
clone = thing.clone()
clone.metadata.forEach (rel, i) -> if rel
expect(rel.from).to.not.be thing
expect(rel.from).to.be clone
it 'handles empty elements gracefully', ->
thing = new Thing new Thing, undefined, new Thing
expect(-> thing.clone()).to.not.throwError()
it 'can copy metadata to an existing other-Thing instead of creating one', ->
thing1 = new Thing new Thing, new Thing, new Thing
thing2 = new Thing new Thing
old_metadata = thing2.metadata
result = thing1.clone(thing2)
expect(result).to.be thing2
expect(thing2.metadata).to.not.be old_metadata
it 'does not copy active responsibility', ->
thing = new Thing new Thing, new Thing, new Thing
thing.dedicate a Liability, (an Execution), thing
expect(thing.custodians.direct).to.not.be.empty()
clone = thing.clone()
expect(clone.custodians.direct).to.be.empty()
describe '::toArray', ->
it 'reduces the receiver Thing to an Array', ->
it = new Thing
expect(-> it.toArray()).to.not.throwException()
expect( it.toArray()).to.be.an 'array'
expect( it.toArray()).to.be.empty()
another = new Thing new Thing, new Thing, new Thing
expect(another.toArray()).to.be.an 'array'
expect(another.toArray()).to.not.be.empty()
it 'returns Things, not the Relation objects from the receiver', ->
first = new Thing; second = new Thing; third = new Thing
it = new Thing first, second, third
array = it.toArray()
expect(array).to.have.length 3
expect(array[0]).to.not.be.a Relation
expect(array[0]).to.be.a Thing
it 'excludes the noughty by default', ->
first = new Thing; second = new Thing; third = new Thing
it = new Thing first, second, third
array = it.toArray()
expect(array).to.have.length 3
expect(array[0]).to.be first
it 'retains empty slots', ->
first = new Thing; third = new Thing
it = new Thing first, undefined, third
array = it.toArray()
expect(array).to.have.length 3
expect(array[0]).to.be first
expect(array[1]).to.be undefined
expect(array[2]).to.be third
describe 'pair()', ->
it 'creates a new Thing', ->
expect(Thing.pair()).to.be.a Thing
it 'turns the first argument into a Label', ->
a_pair = Thing.pair 'foo'
expect(a_pair.at 1).to.be.a Label
expect(a_pair.at(1).alien).to.be 'foo'
a_pair = Thing.pair Label('bar')
expect(a_pair.at 1).to.be.a Label
expect(a_pair.at(1).alien).to.be 'bar'
it 'creates the pair as owning the key-Label', ->
foo = new Thing
a_pair = Thing.pair 'foo', foo
expect(a_pair.at(1).alien).to.be 'foo'
expect(a_pair.metadata[1]).to.be.owned()
it 'creates the pair as *not* owning the value', ->
foo = new Thing
a_pair = Thing.pair 'foo', foo
expect(a_pair.at(2)).to.be foo
expect(a_pair.metadata[2]).to.not.be.owned()
it 'can be instructed to create the pair as owning the value', ->
foo = new Thing
a_pair = Thing.pair 'foo', foo, yes
expect(a_pair.at(2)).to.be foo
expect(a_pair.metadata[2]).to.be.owned()
it 'takes existing ownership of a passed Relation by default', ->
foo = new Thing
rel = new Relation null, foo, yes
a_pair = Thing.pair 'foo', rel
expect(a_pair.at(2)).to.be foo
expect(a_pair.metadata[2]).to.be.owned()
it 'can be instructed to *override* existing ownership of a passed Relation', ->
foo = new Thing
rel = new Relation null, foo, yes
a_pair = Thing.pair 'foo', rel, no
expect(a_pair.at(2)).to.be foo
expect(a_pair.metadata[2]).to.not.be.owned()
describe '::define', ->
it 'adds a pair to the end of the receiver', ->
a_thing = Thing.construct foo: new Thing
another_thing = new Thing
a_thing.define 'bar', another_thing
expect(a_thing.find('bar')[0].isPair()).to.be yes
expect(a_thing.find('bar')[0].valueish()).to.be another_thing
it 'marks the pushed pair as owned by the receiver', ->
a_thing = Thing.construct foo: new Thing
another_thing = new Thing
a_thing.define 'bar', another_thing
expect(a_thing.owns_at 1).to.be yes
it 'defaults, however, to *not* owning the passed value', ->
a_thing = new Thing
another_thing = new Thing
a_thing.define 'bar', another_thing
pair = a_thing.at 1
expect(pair.owns_at 2).to.be no
it 'accepts a Thing', ->
a_thing = Thing.construct foo: new Thing
another_thing = new Thing
a_thing.define 'bar', another_thing
expect(a_thing.find('bar')[0].isPair()).to.be yes
expect(a_thing.find('bar')[0].valueish()).to.be another_thing
it 'accepts a Relation', ->
a_thing = Thing.construct foo: new Thing
another_thing = new Thing
a_thing.define 'bar', (new Relation a_thing, another_thing)
expect(a_thing.find('bar')[0].isPair()).to.be yes
expect(a_thing.find('bar')[0].valueish()).to.be another_thing
it 'checks availability, and throws an ResponsibilityError if there is a conflict', ->
a_thing = new Thing; another_thing = new Thing
an_execution = new Execution; another_execution = new Execution
a_thing .dedicate new Liability(an_execution, a_thing, 'write')
another_thing.dedicate new Liability(another_execution, another_thing, 'read')
expect(-> a_thing.define 'foo', another_thing.owned_by(a_thing)).to
.throwException ResponsibilityError
expect(a_thing.at 1).to.be undefined
expect(a_thing .belongs_to an_execution, 'write').to.be yes
expect(another_thing.belongs_to another_execution, 'read').to.be yes
expect(another_thing.belongs_to an_execution, 'read').to.be no
describe '::find', ->
first = new Thing; second = new Thing; third = new Thing
foo_bar_foo = new Thing Thing.pair('foo', first),
Thing.pair('bar', second),
Thing.pair('foo', third)
it 'produces an Array of Things', ->
expect(foo_bar_foo.find Label 'nope').to.be.an 'array'
it 'finds Things matching a given key', ->
results = foo_bar_foo.find Label 'foo'
expect(results.length).to.be.greaterThan 0
results.forEach (result) ->
expect(result).to.be.a Thing
it "excludes Things that don't match the key", ->
results = foo_bar_foo.find Label 'foo'
expect(results).to.have.length 2
expect(util.pluck results, 'metadata.2.to').to.not.contain second # FIXME: ugly.
it 'produces the results in reverse order', ->
results = foo_bar_foo.find Label 'foo'
expect(results[0].valueish()).to.be third
expect(results[1].valueish()).to.be first
it 'handles Things with non-pair members gracefully', ->
thing = new Thing Thing.pair('foo', first),
new Thing,
Thing.pair('bar', second),
Thing.pair('foo', third)
expect(thing.find Label 'foo').to.have.length 2
it 'can take a JavaScript primitive as a key instead of a Label', ->
expect(foo_bar_foo.find 'bar').to.have.length 1
describe '~ The root `receiver`', ->
caller = undefined; receiver = undefined
beforeEach ->
caller = new Execution
receiver = Thing::receiver.clone()
it 'preforms a `::find`', ->
a_thing = Thing.construct foo: another_thing = new Thing
params = Execution.create_params caller, a_thing, new Label 'foo'
sinon.spy a_thing, 'find'
bit = receiver.advance params
bit.apply receiver, [params]
expect(a_thing.find).was.calledOnce()
# FIXME: This should really be an *integration* test.
it 'finds a matching pair-ish Thing in the subject', ->
a_thing = Thing.construct foo: another_thing = new Thing
params = Execution.create_params caller, a_thing, new Label 'foo'
sinon.spy caller, 'queue'
bit = receiver.advance params
bit.apply receiver, [params]
expect(caller.queue).was.calledWith __.has 'params', [another_thing]
it 'stages the caller if there is a result', ->
a_thing = Thing.construct foo: another_thing = new Thing
params = Execution.create_params caller, a_thing, new Label 'foo'
sinon.spy caller, 'queue'
bit = receiver.advance params
bit.apply receiver, [params]
expect(caller.queue).was.calledOnce()
it 'does not stage the caller if there is no result', ->
a_thing = Thing.construct foo: another_thing = new Thing
params = Execution.create_params caller, a_thing, new Label 'bar'
sinon.spy caller, 'queue'
bit = receiver.advance params
bit.apply receiver, [params]
expect(caller.queue).was.notCalled()
# NB: A lot of this is duplicating effort from the Giraphe tests; but hey.
describe '::_walk_descendants', ->
it 'exists', ->
a Thing
expect(a.thing._walk_descendants).to.be.a 'function'
it "doesn't throw when given no arguments", ->
expect(-> (a Thing)._walk_descendants()).to.not.throwException()
it 'accepts a callback', ->
a.thing = Thing.construct
foo: foo = new Thing, bar: bar = Thing.construct
widget: widget = new Thing
expect(-> a.thing._walk_descendants(->)).to.not.throwException()
it.skip 'provides a method to cache results during a particular reactor-step'
it 'returns a mapping object', ->
a Thing
rv = a.thing._walk_descendants()
expect(rv).to.be.an 'object'
it 'collects owned descendants into the returned object', ->
a.thing = Thing.construct
foo: foo = new Thing, bar: bar = Thing.construct
widget: widget = new Thing
rv = a.thing._walk_descendants()
expect(util.values(rv)).to.contain foo
expect(rv[foo.id]).to.be foo
it 'calls the callback on each node walked', ->
a.thing = Thing.construct
foo: foo = new Thing, bar: bar = Thing.construct
widget: widget = new Thing
a.thing._walk_descendants cb = sinon.spy()
expect(cb).was.calledOn a.thing
expect(cb).was.calledOn foo
expect(cb).was.calledOn bar
expect(cb).was.calledOn widget
it 'skips objects for which the callback returns false', ->
a.thing = Thing.construct
foo: foo = new Thing, bar: bar = Thing.construct
widget: widget = new Thing
descendants = a.thing._walk_descendants cb = sinon.spy ->
return false if this is bar
expect(cb).was.calledOn bar
expect(descendants[foo.id]).to.be foo
expect(descendants[bar.id]).to.be undefined
it 'can be instructed to cease iteration', ->
a.thing = Thing.construct
foo: foo = new Thing, bar: bar = Thing.construct
widget: widget = new Thing
rv = a.thing._walk_descendants cb = sinon.spy ->
return Thing.abortIteration if this is foo
expect(rv).to.not.be.ok()
it 'skips descendants of objects for which the callback returns false', ->
a.thing = Thing.construct
foo: foo = new Thing, bar: bar = Thing.construct
widget: widget = new Thing
descendants = a.thing._walk_descendants cb = sinon.spy ->
return false if this is bar
# FIXME: Yes, this is awkwardly-worded. It's pending sinon-expect#5:
# <https://github.com/lightsofapollo/sinon-expect/issues/5>
expect(!cb.calledOn widget)
expect(descendants[widget.id]).to.be undefined
it "doesn't touch contained (not-owned) descendants", ->
a.thing = Thing.construct
foo: foo = new Thing, bar: bar = Thing.construct
widget: widget = (new Thing).contained_by(bar)
descendants = a.thing._walk_descendants cb = sinon.spy()
# FIXME: See above.
expect(!cb.calledOn widget)
expect(descendants[widget.id]).to.be undefined
it 'touches each descendant only once, in the presence of cyclic graphcs', ->
a.thing = Thing.construct
child: child = new Thing
child.define('cyclic', a.thing)
a.thing._walk_descendants cb = sinon.spy()
# FIXME: This could probably be better expressed (slash less-tightly-coupled)
expect(cb.callCount).to.be 6
# ### Thing: Responsibility methods ###
describe '::available_to', ->
it 'exists', ->
expect((a Thing).available_to).to.be.a 'function'
it 'accepts a Liability', ->
a_thing = Thing.construct foo: foo = new Thing, bar:
bar = Thing.construct widget: widget = new Thing
a Liability, (an Execution), a_thing
expect(-> a_thing.available_to a.liability).to.not.throwException()
it 'accepts multiple Liabilities', ->
a_thing = Thing.construct foo: foo = new Thing, bar:
bar = Thing.construct widget: widget = new Thing
a Liability, (an Execution), a_thing
another Liability, (another Execution), a_thing
expect(-> a_thing.available_to a.liability, another.liability).to.not.throwException()
it 'accepts an Array of Liabilities', ->
a_thing = Thing.construct foo: foo = new Thing, bar:
bar = Thing.construct widget: widget = new Thing
a Liability, (an Execution), a_thing
another Liability, (another Execution), a_thing
expect(-> a_thing.available_to [a.liability, another.liability]).to.not.throwException()
it 'succeeds if the root has no children and is not adopted', ->
expect((a Thing).available_to a Liability).to.be yes
it "succeeds if none of the receiver's descendants are adopted", ->
a_thing = Thing.construct foo: foo = new Thing, bar:
bar = Thing.construct widget: widget = new Thing
expect(a_thing.available_to a Liability).to.be true
it 'succeeds if the receiver is already adopted by the Execution', ->
a_thing = Thing.construct foo: foo = new Thing, bar:
bar = Thing.construct widget: widget = new Thing
a Liability, (an Execution), a_thing
a_thing.dedicate new Liability an.execution, a_thing
expect(a_thing.available_to a.liability).to.be yes
it 'succeeds if the receiver is already adopted by all of the Executions', ->
a_thing = Thing.construct foo: foo = new Thing, bar:
bar = Thing.construct widget: widget = new Thing
a Liability, (an Execution), a_thing
another Liability, (another Execution), a_thing
a_thing.dedicate new Liability an.execution, a_thing
a_thing.dedicate new Liability another.execution, a_thing
expect(a_thing.available_to a.liability, another.liability).to.be yes
it 'fails if the receiver is adopted by someone else, and conflicts', ->
a_thing = Thing.construct foo: foo = new Thing, bar:
bar = Thing.construct widget: widget = new Thing
a Liability, (an Execution), a_thing
a_thing.dedicate (some Liability, (another Execution), a_thing, 'write')
expect(a_thing.available_to a.liability).to.be no
it 'fails when an owned descendant is adopted by someone else, and conflicts', ->
a_thing = Thing.construct foo: foo = new Thing, bar:
bar = Thing.construct widget: widget = new Thing
a Liability, (an Execution), a_thing
widget.dedicate (some Liability, (another Execution), widget, 'write')
expect(a_thing.available_to a.liability).to.be no
it 'fails when only one argument conflicts with such an adopted owned-descendant', ->
a_thing = Thing.construct foo: foo = new Thing, bar:
bar = Thing.construct widget: widget = new Thing
a Liability, (an Execution), a_thing, 'read'
another Liability, (another Execution), a_thing, 'write'
widget.dedicate (some Liability, (another Execution), widget, 'read')
expect(a_thing.available_to a.liability, another.liability).to.be no
describe '::belongs_to', ->
it 'exists', ->
expect((a Thing).belongs_to).to.be.a 'function'
it 'accepts an Execution', ->
expect(-> (a Thing).belongs_to an Execution).to.not.throwException()
it 'accepts a Liability', ->
expect(-> (a Thing).belongs_to a Liability).to.not.throwException()
it 'accepts a Thing with parents', ->
a_thing = Thing.construct foo: foo = new Thing, bar:
bar = Thing.construct widget: widget = new Thing
a Liability, (an Execution), a_thing
expect(-> widget.belongs_to a.Liability).to.not.throwException()
it 'indicates false if there are no custodians', ->
expect((a Thing).belongs_to a Liability).to.be no
describe '~ Direct responsibility', ->
it 'succeeds if the receiver belongs to the passed Liability', ->
(a Thing).dedicate a Liability, new Execution, a.thing
expect(a.thing.belongs_to a.liability).to.be yes