-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.hx
1084 lines (1013 loc) · 31.1 KB
/
Graph.hx
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
package zenflo.spec.graph;
// import polygonal.ds.ArrayList;
import haxe.Timer;
import equals.Equal;
import buddy.BuddySuite;
import haxe.DynamicAccess;
import zenflo.graph.GraphJson;
using buddy.Should;
@colorize
class Graph extends buddy.BuddySuite {
public function new() {
BuddySuite.useDefaultTrace = true;
describe("FBP Graph", () -> {
describe('with case sensitivity', {
describe('Unnamed graph instance', {
it('should have an empty name', {
final g = new zenflo.graph.Graph();
g.name.should.be('');
});
});
describe('with new instance', {
final g = new zenflo.graph.Graph('Foo bar', {caseSensitive: true});
it('should get a name from constructor', {
g.name.should.be('Foo bar');
});
it('should have no nodes initially', {
g.nodes.size.should.be(0);
});
it('should have no edges initially', {
g.edges.size.should.be(0);
});
it('should have no initializers initially', {
g.initializers.length.should.be(0);
});
it('should have no inports initially', {
g.inports.keys().length.should.be(0);
});
it('should have no outports initially', {
g.outports.keys().length.should.be(0);
});
beforeEach(() -> {
g.removeAllListeners();
});
describe('New node', {
var n = null;
beforeEach((done) -> {
#if !cpp
haxe.Timer.delay(() -> {
done();
}, 0);
#else
Sys.sleep(0.01);
done();
#end
});
it('should emit an event', (done) -> {
g.once('addNode', (vals) -> {
final node:zenflo.graph.GraphNode = vals[0];
node.id.should.be('Foo');
node.component.should.be('Bar');
n = node;
done();
});
g.addNode('Foo', 'Bar');
});
it('should be in graph\'s list of nodes', {
g.nodes.size.should.be(1);
g.nodes.indexOf(n).should.be(0);
});
it('should be accessible via the getter', {
final node = g.getNode('Foo');
node.id.should.be('Foo');
node.should.be(n);
});
it('should have empty metadata', {
final node = g.getNode('Foo');
node.metadata.should.be(null);
var _node:Dynamic = node;
_node.display.should.be(null);
});
it('should be available in the JSON export', {
final json = g.toJSON();
Reflect.isObject(json.processes["Foo"]).should.be(true);
json.processes["Foo"].component.should.be('Bar');
var foo:Dynamic = json.processes["Foo"];
foo.display.should.be(null);
});
it('removing should emit an event', (done) -> {
g.on('removeNode', (vals) -> {
final node:zenflo.graph.GraphNode = vals[0];
node.id.should.be('Foo');
node.should.be(n);
done();
});
g.removeNode('Foo');
});
it('should not be available after removal', {
final node = g.getNode('Foo');
node.should.be(null);
g.nodes.size.should.be(0);
g.nodes.indexOf(n).should.be(-1);
});
});
describe('New edge', {
beforeEach((done) -> {
#if !cpp
haxe.Timer.delay(() -> {
done();
}, 0);
#else
Sys.sleep(0.01);
done();
#end
});
it('should emit an event', (done) -> {
g.addNode('Foo', 'foo');
g.addNode('Bar', 'bar');
g.once('addEdge', (edges) -> {
final edge:zenflo.graph.GraphEdge = edges[0];
edge.from.node.should.be('Foo');
edge.to.port.should.be('In');
done();
});
#if cpp
Sys.sleep(0.001);
#end
g.addEdge('Foo', 'Out', 'Bar', 'In');
});
it('should add an edge', {
g.addEdge('Foo', 'out', 'Bar', 'in2');
g.edges.size.should.be(2);
});
it('should refuse to add a duplicate edge', {
final edge = g.edges[0];
g.addEdge(edge.from.node, edge.from.port, edge.to.node, edge.to.port);
g.edges.size.should.be(2);
});
});
describe('New edge with index', {
beforeEach((done) -> {
#if !cpp
haxe.Timer.delay(() -> {
done();
}, 0);
#else
Sys.sleep(0.01);
done();
#end
});
it('should emit an event', (done) -> {
g.once('addEdge', (edges) -> {
final edge:zenflo.graph.GraphEdge = edges[0];
edge.from.node.should.be('Foo');
edge.to.port.should.be('in');
edge.to.index.should.be(1);
edge.from.index.should.be(null);
g.edges.size.should.be(3);
done();
});
g.addEdgeIndex('Foo', 'out', null, 'Bar', 'in', 1);
});
it('should add an edge', {
g.addEdgeIndex('Foo', 'out', 2, 'Bar', 'in2');
g.edges.size.should.be(4);
});
});
});
describe('loaded from JSON (with case sensitive port names)', {
final jsonString = '
{
"caseSensitive": true,
"properties": {
"name": "Example",
"foo": "Baz",
"bar": "Foo"
},
"inports": {
"inPut": {
"process": "Foo",
"port": "inPut",
"metadata": {
"x": 5,
"y": 100
}
}
},
"outports": {
"outPut": {
"process": "Bar",
"port": "outPut",
"metadata": {
"x": 500,
"y": 505
}
}
},
"groups": [
{
"name": "first",
"nodes": [
"Foo"
],
"metadata": {
"label": "Main"
}
},
{
"name": "second",
"nodes": [
"Foo2",
"Bar2"
]
}
],
"processes": {
"Foo": {
"component": "Bar",
"metadata": {
"display": {
"x": 100,
"y": 200
},
"routes": [
"one",
"two"
],
"hello": "World"
}
},
"Bar": {
"component": "Baz",
"metadata": {}
},
"Foo2": {
"component": "foo",
"metadata": {}
},
"Bar2": {
"component": "bar",
"metadata": {}
}
},
"connections": [
{
"src": {
"process": "Foo",
"port": "outPut"
},
"tgt": {
"process": "Bar",
"port": "inPut"
},
"metadata": {
"route": "foo",
"hello": "World"
}
},
{
"src": {
"process": "Foo",
"port": "out2"
},
"tgt": {
"process": "Bar",
"port": "in2",
"index": 2
},
"metadata": {
"route": "foo",
"hello": "World"
}
},
{
"data": "Hello, world!",
"tgt": {
"process": "Foo",
"port": "inPut"
}
},
{
"data": "Hello, world, 2!",
"tgt": {
"process": "Foo",
"port": "in2"
}
},
{
"data": "Cheers, world!",
"tgt": {
"process": "Foo",
"port": "arr",
"index": 0
}
},
{
"data": "Cheers, world, 2!",
"tgt": {
"process": "Foo",
"port": "arr",
"index": 1
}
}
]
}
';
final json:GraphJson = haxe.Json.parse(jsonString);
var g:zenflo.graph.Graph = null;
beforeEach((done) -> {
#if !cpp
haxe.Timer.delay(() -> {
done();
}, 0);
#else
Sys.sleep(0.01);
done();
#end
});
it('should produce a Graph when input is string', {
zenflo.graph.Graph.loadJSON(jsonString).handle((c) -> {
switch c {
case Success(instance): {
g = instance;
g.should.not.be(null);
}
case Failure(err): {
fail(err);
}
}
});
});
it('should produce a Graph when input is JSON', {
zenflo.graph.Graph.loadJSON(json).handle((c) -> {
switch c {
case Success(instance): {
g = instance;
g.should.not.be(null);
}
case Failure(err): {
fail(err);
}
}
});
});
it('should not mutate the inputted json object', (done) -> {
if (json != null) {
if (json.processes != null) {
json.processes.keys().length.should.be(4);
} else {
json.processes.should.not.be(null);
}
} else {
json.should.not.be(null);
}
zenflo.graph.Graph.loadJSON(json).handle((c) -> {
var instance:zenflo.graph.Graph = null;
switch c {
case Failure(err): {
fail(err);
return;
}
case Success(_instance): {
instance = _instance;
}
}
if (instance == null) {
fail(new tink.core.Error('No graph loaded'));
return;
} else {
instance.addNode('Split1', 'Split');
instance.addNode('Split1', 'Split');
instance.addNode('Split1', 'Split');
instance.addNode('Split1', 'Split');
instance.addNode('Split1', 'Split');
instance.addNode('Split1', 'Split');
instance.addNode('Split1', 'Split');
instance.addNode('Split1', 'Split');
instance.addNode('Split1', 'Split');
instance.addNode('Split1', 'Split');
instance.addNode('Split1', 'Split');
}
json.processes.keys().length.should.be(4);
done();
});
});
it('should have a name', {g.name.should.be('Example');});
it('should have graph metadata intact', {
var props:Dynamic = g.properties;
props.foo.should.be("Baz");
props.bar.should.be("Foo");
});
it('should produce same JSON when serialized', {
equals.Equal.equals(g.toJSON(), json).should.be(true);
});
it('should allow modifying graph metadata', (done) -> {
g.once('changeProperties', (vals) -> {
final properties:zenflo.graph.PropertyMap = vals[0];
properties.should.be(g.properties);
var want:DynamicAccess<String> = {
foo: 'Baz',
bar: 'Bar',
hello: 'World',
};
for (key in g.properties.keys()) {
g.properties[key].should.be(want[key]);
}
done();
});
g.setProperties({
hello: 'World',
bar: 'Bar',
});
});
it('should contain four nodes', {
g.nodes.size.should.be(4);
});
it('the first Node should have its metadata intact', {
final node = g.getNode('Foo');
node.metadata.should.not.be(null);
Reflect.isObject(node.metadata).should.be(true);
var metadata:Dynamic = node.metadata;
metadata.display.should.not.be(null);
Reflect.isObject(metadata.display).should.be(true);
metadata.display.x.should.be(100);
metadata.display.y.should.be(200);
metadata.routes.should.beType(Array);
var routes:Array<String> = metadata.routes;
routes.should.contain('one');
routes.should.contain('two');
});
beforeEach((done) -> {
#if !cpp
haxe.Timer.delay(() -> {
done();
}, 0);
#else
Sys.sleep(0.01);
done();
#end
});
// beforeEach((done) -> {
// Sys.sleep(0.01);
// g.removeAllListeners();
// done();
// });
it('should allow modifying node metadata', (done) -> {
g.removeAllListeners();
g.once('changeNode', (vals) -> {
final node:zenflo.graph.GraphNode = vals[0];
node.id.should.be('Foo');
var metadata:Dynamic = node.metadata;
metadata.routes.should.beType(Array);
final routes:Array<String> = metadata.routes;
routes.should.contain('one');
routes.should.contain('two');
metadata.hello.should.be('World');
done();
});
g.setNodeMetadata('Foo', {hello: 'World'});
});
it('should contain two connections', {
g.edges.size.should.be(2);
});
it('the first Edge should have its metadata intact', {
final edge = g.edges[0];
Reflect.isObject(edge.metadata).should.be(true);
var metadata:Dynamic = edge.metadata;
metadata.route.should.be('foo');
});
it('should allow modifying edge metadata', (done) -> {
final e = g.edges[0];
g.once('changeEdge', (edges) -> {
final edge:zenflo.graph.GraphEdge = edges[0];
edge.should.be(e);
var metadata:Dynamic = edge.metadata;
metadata.route.should.be('foo');
metadata.hello.should.be('World');
done();
});
g.setEdgeMetadata(e.from.node, e.from.port, e.to.node, e.to.port, {hello: 'World'});
});
it('should contain four IIPs', {
g.initializers.length.should.be(4);
});
it('should contain one published inport', {
g.inports.keys().length.should.be(1);
});
it('should contain one published outport', {
g.inports.keys().length.should.be(1);
});
it('should keep the output export metadata intact', {
final exp = g.outports["outPut"];
var metadata:Dynamic = exp.metadata;
metadata.x.should.be(500);
metadata.y.should.be(505);
});
it('should contain two groups', {
g.groups.size.should.be(2);
});
it('should allow modifying group metadata', (done) -> {
final group = g.groups[0];
g.once('changeGroup', (grps) -> {
final grp:zenflo.graph.GraphGroup = grps[0];
grp.should.be(group);
var metadata:Dynamic = grp.metadata;
metadata.label.should.be('Main');
metadata.foo.should.be('Bar');
equals.Equal.equals(g.groups[1].metadata, {}).should.be(true);
// haxe.Json.stringify(g.groups[1].metadata).should.be("{}");
done();
});
g.setGroupMetadata('first', {foo: 'Bar'});
});
it('should allow renaming groups', (done) -> {
final group = g.groups[0];
g.once('renameGroup', (vals) -> {
var oldName = vals[0];
var newName = vals[1];
oldName.should.be("first");
newName.should.be("renamed");
group.name.should.be(newName);
done();
});
g.renameGroup('first', 'renamed');
});
describe('renaming a node', {
beforeEach((done) -> {
g.removeAllListeners();
#if !cpp
haxe.Timer.delay(() -> {
done();
}, 0);
#else
// Sys.sleep(0.01);
done();
#end
});
it('should emit an event', (done) -> {
g.once('renameNode', (vals) -> {
var oldId = vals[0];
var newId = vals[1];
oldId.should.be('Foo');
newId.should.be('Baz');
done();
});
g.renameNode('Foo', 'Baz');
});
it('should be available with the new name', {
Reflect.isObject(g.getNode('Baz')).should.be(true);
});
it('shouldn\'t be available with the old name', {
g.getNode('Foo').should.be(null);
});
it('should have the edge still going from it', {
var connection = null;
for (edge in g.edges) {
if (edge.from.node == 'Baz') {
connection = edge;
}
}
Reflect.isObject(connection).should.be(true);
});
it('should still be exported', {
g.inports["inPut"].process.should.be("Baz");
});
it('should still be grouped', {
var groups = 0;
for (group in g.groups) {
if (group.nodes.indexOf('Baz') != -1) {
groups += 1;
}
}
groups.should.be(1);
});
it('shouldn\'t be have edges with the old name', {
var connection = null;
for (edge in g.edges) {
if (edge.from.node == 'Foo') {
connection = edge;
}
if (edge.to.node == 'Foo') {
connection = edge;
}
}
connection.should.be(null);
});
it('should have the IIP still going to it', {
var iip = null;
for (initializer in g.initializers) {
if (initializer.to.node == 'Baz') {
iip = initializer;
}
}
Reflect.isObject(iip).should.be(true);
});
it('shouldn\'t have IIPs going to the old name', {
var iip = null;
for (initializer in g.initializers) {
if (initializer.to.node == 'Foo') {
iip = initializer;
}
}
iip.should.be(null);
});
it('shouldn\'t be grouped with the old name', {
var groups = 0;
for (group in g.groups) {
if (group.nodes.indexOf('Foo') != -1) {
groups += 1;
}
}
groups.should.be(0);
});
});
describe('renaming an inport', {
it('should emit an event', (done) -> {
g.once('renameInport', (vals) -> {
var oldName = vals[0];
var newName = vals[1];
oldName.should.be('inPut');
newName.should.be('opt');
g.inports['inPut'].should.be(null);
Reflect.isObject(g.inports['opt']).should.be(true);
var opt:Dynamic = g.inports['opt'];
opt.process.should.be('Baz');
opt.port.should.be('inPut');
done();
});
g.renameInport('inPut', 'opt');
});
});
describe('renaming an outport', {
it('should emit an event', (done) -> {
g.once('renameOutport', (vals) -> {
var oldName = vals[0];
var newName = vals[1];
oldName.should.be('outPut');
newName.should.be('foo');
var outports:Dynamic = g.outports;
outports.outPut.should.be(null);
Reflect.isObject(outports.foo).should.be(true);
outports.foo.process.should.be('Bar');
outports.foo.port.should.be('outPut');
done();
});
g.renameOutport('outPut', 'foo');
});
});
describe('removing a node', {
it('should emit an event', (done) -> {
g.on('removeNode', (nodes) -> {
final node:Dynamic = nodes[0];
node.id.should.be('Baz');
done();
});
g.removeNode('Baz');
});
it('shouldn\'t have edges left behind', (done) -> {
var connections = 0;
for(edge in g.edges){
if(edge.to.node == "Baz") connections += 1;
if(edge.from.node == "Baz") connections +=1;
}
connections.should.be(0);
done();
});
it('shouldn\'t have IIPs left behind', (done) -> {
final connections = Lambda.filter(g.initializers, (iip) -> {
return iip.to.node == "Baz";
});
connections.length.should.be(0);
done();
});
it('shouldn\'t be grouped', {
var groups = 0;
for (group in g.groups) {
if (group.nodes.indexOf('Baz') != -1) {
groups += 1;
}
}
groups.should.be(0);
});
it('shouldn\'t affect other groups', {
final otherGroup = g.groups[0];
otherGroup.nodes.length.should.be(2);
});
});
});
describe('with multiple connected ArrayPorts', {
final g = new zenflo.graph.Graph();
g.addNode('Split1', 'Split');
g.addNode('Split2', 'Split');
g.addNode('Merge1', 'Merge');
g.addNode('Merge2', 'Merge');
g.addEdge('Split1', 'out', 'Merge1', 'in');
g.addEdge('Split1', 'out', 'Merge2', 'in');
g.addEdge('Split2', 'out', 'Merge1', 'in');
g.addEdge('Split2', 'out', 'Merge2', 'in');
it('should contain four nodes', {
g.nodes.size.should.be(4);
});
it('should contain four edges', {
g.edges.size.should.be(4);
});
it('should allow a specific edge to be removed', {
g.removeEdge('Split1', 'out', 'Merge2', 'in');
g.edges.size.should.be(3);
});
// beforeEach((done) -> {
// // g.removeAllListeners();
// #if !cpp
// haxe.Timer.delay(() -> {
// done();
// }, 0);
// #else
// // Sys.sleep(0.01);
// done();
// #end
// });
it('shouldn\'t contain the removed connection from Split1', {
var connection = null;
for (edge in g.edges) {
if ((edge.from.node == 'Split1') && (edge.to.node == 'Merge2')) {
connection = edge;
}
}
connection.should.be(null);
});
it('should still contain the other connection from Split1', {
var connection = null;
for (edge in g.edges) {
if ((edge.from.node == 'Split1') && (edge.to.node == 'Merge1')) {
connection = edge;
}
}
Reflect.isObject(connection).should.be(true);
});
});
describe('with an Initial Information Packet', {
var g = new zenflo.graph.Graph();
g.addNode('Split', 'Split');
g.addInitial('Foo', 'Split', 'in');
it('should contain one node', {
g.nodes.size.should.be(1);
});
it('should contain no edges', {
g.edges.size.should.be(0);
});
it('should contain one IIP', {
g.initializers.length.should.be(1);
});
// beforeAll((done) -> {
// g.removeAllListeners();
// done();
// });
describe('on removing that IIP', () -> {
afterEach((done) -> {
#if !cpp
haxe.Timer.delay(() -> {
done();
}, 2);
#else
// g.removeAllListeners();
Sys.sleep(2);
done();
#end
});
it('should emit a removeInitial event', (done) -> {
g.once('removeInitial', (iips) -> {
final iip:zenflo.graph.GraphIIP = iips[0];
iip.from.data.should.be('Foo');
iip.to.node.should.be('Split');
iip.to.port.should.be('in');
done();
});
g.removeInitial('Split', 'in');
trace("should emit a removeInitial event =>"+g.initializers);
});
it('should contain no IIPs', () -> {
// #if cpp Sys.sleep(2); #end
trace("should contain no IIPs =>"+g.initializers.length);
g.initializers.length.should.be(0);
});
});
});
describe('with an Inport Initial Information Packet', {
final g = new zenflo.graph.Graph();
g.addNode('Split', 'Split');
g.addInport('testinport', 'Split', 'in');
g.addGraphInitial('Foo', 'testinport');
it('should contain one node', () -> g.nodes.size.should.be(1));
it('should contain no edges', () -> g.edges.size.should.be(0));
it('should contain one IIP for the correct node', () -> {
g.initializers.length.should.be(1);
g.initializers[0].from.data.should.be('Foo');
g.initializers[0].to.node.should.be('Split');
g.initializers[0].to.port.should.be('in');
});
describe('on removing that IIP', {
beforeEach((done) -> {
//
#if !cpp
haxe.Timer.delay(() -> {
done();
}, 0);
#else
// g.removeAllListeners();
// Sys.sleep(0.01);
done();
#end
});
it('should emit a removeInitial event', (done) -> {
g.once('removeInitial', (vals) -> {
final iip:zenflo.graph.GraphIIP = vals[0];
iip.from.data.should.be('Foo');
iip.to.node.should.be('Split');
iip.to.port.should.be('in');
done();
});
g.removeGraphInitial('testinport');
});
it('should contain no IIPs', () -> {
// #if cpp Sys.sleep(2); #end
g.initializers.length.should.be(0);
});
});
describe('on adding IIP for a non-existent inport', {
g.addGraphInitial('Bar', 'nonexistent');
it('should not add any IIP', () -> {
g.initializers.length.should.be(0);
});
});
});
describe('with an indexed Inport Initial Information Packet', {
final g = new zenflo.graph.Graph();
g.addNode('Split', 'Split');
g.addInport('testinport', 'Split', 'in');
g.addGraphInitialIndex('Foo', 'testinport', 1);
it('should contain one node', {
g.nodes.size.should.be(1);
});
it('should contain no edges', {
g.edges.size.should.be(0);
});
it('should contain one IIP for the correct node', {
g.initializers.length.should.be(1);
g.initializers[0].from.data.should.be('Foo');
g.initializers[0].to.node.should.be('Split');
g.initializers[0].to.port.should.be('in');
g.initializers[0].to.index.should.be(1);
});
describe('on removing that IIP', {
beforeEach((done) -> {
#if !cpp
haxe.Timer.delay(() -> {
done();
}, 0);
#else
// g.removeAllListeners();
// Sys.sleep(0.01);
done();
#end
});
it('should emit a removeInitial event', (done) -> {
g.once('removeInitial', (iips) -> {
final iip:Dynamic = iips[0];
iip.from.data.should.be('Foo');
iip.to.node.should.be('Split');
iip.to.port.should.be('in');
done();
});
g.removeGraphInitial('testinport');
});
it('should contain no IIPs', () -> {
g.initializers.length.should.be(0);
});
});
describe('on adding IIP for a non-existent inport', {
g.addGraphInitialIndex('Bar', 'nonexistent', 1);
it('should not add any IIP', () -> g.initializers.length.should.be(0));
});
});
describe('with no nodes', {
final g = new zenflo.graph.Graph();
it('should not allow adding edges', {
g.addEdge('Foo', 'out', 'Bar', 'in');
g.edges.size.should.be(0);
});
it('should not allow adding IIPs', {
g.addInitial('Hello', 'Bar', 'in');
g.initializers.length.should.be(0);
});
});
});
#if (sys || hxnodejs)
describe('saving and loading files', {
describe('with .json suffix', {
var originalGraph = null;
var graphPath = null;
beforeEach({
graphPath = haxe.io.Path.join([Sys.getCwd(), "foo.json"]);
});
it('should be possible to save a graph to a file', (done) -> {
final g = new zenflo.graph.Graph();
g.addNode('Foo', 'Bar');
originalGraph = g.toJSON();
g.save(graphPath).handle((s) -> {
switch s {
case Success(i): {
done();
}
case Failure(e): {
fail(e);
}
}
});
});
it('should be possible to load a graph from a file', {
zenflo.graph.Graph.loadFile(graphPath).handle((c) -> {
switch c {
case Success(g): {
g.should.not.be(null);
g.should.beType(zenflo.graph.Graph);
equals.Equal.equals(g.toJSON(), originalGraph).should.be(true);
}
case _:
}
});
});
afterAll({
sys.FileSystem.deleteFile(graphPath);
});
});
describe('without .json suffix', {
var graphPathLegacy = null;
var graphPathLegacySuffix = null;
var originalGraph = null;
beforeEach({
graphPathLegacySuffix = haxe.io.Path.join([Sys.getCwd(), "bar.json"]);
graphPathLegacy = haxe.io.Path.join([Sys.getCwd(), "bar"]);
});