-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
703 lines (631 loc) · 48.1 KB
/
test.js
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
function addHeadsToListTest(){
describe("addHeadsToListTest.html", function() {
describe("copyNode", function() {
let testNode;
beforeEach(function() {
testNode = {
id: "root",
cat: "phi",
children: [
{id: "kid1", cat: "w",},
{id: "kid2", cat: "w",},
{
id: "intermediate",
cat: "phi",
children: [
{id: "kid3", cat: "w",},
{id: "kid4", cat: "w",},
{id: "kid5", cat: "w",},
],
},
],
};
});
it("notEqual", function () {
assert.notEqual(testNode, copyNode(testNode));
});
it("but deepEqual", function () {
assert.deepEqual(testNode, copyNode(testNode));
});
it("and don't follow eachother", function () {
let nodeCopy = copyNode(testNode);
testNode.head = true;
assert.isUndefined(nodeCopy.head);
nodeCopy.cat = 'i';
assert.equal(testNode.cat, 'phi');
});
it("Recursive copy", function () {
assert.notEqual(testNode.children[0], copyNode(testNode).children[0]);
});
});
describe("isHeaded", function() {
let testNode;
beforeEach(function() {
testNode = {
id: "root",
cat: "phi",
children: [
{id: "kid1", cat: "w",},
{id: "kid2", cat: "w",},
{id: "kid3", cat: "w",},
{id: "kid4", cat: "w",},
{id: "kid5", cat: "w",},
],
};
});
it("Identifies left-headed node", function () {
testNode.children[0].head = true;
assert.isTrue(isHeaded(testNode));
});
it("Identifies right-headed node", function () {
testNode.children[4].head = true;
assert.isTrue(isHeaded(testNode));
});
it("Identifies middle-headed node", function () {
testNode.children[2].head = true;
assert.isTrue(isHeaded(testNode));
});
it("Identifies unheaded node", function () {
assert.isFalse(isHeaded(testNode));
});
});
describe("addLeftHead", function() {
let testNode;
beforeEach(function() {
testNode = addLeftHead({
id: "root",
cat: "phi",
children: [
{id: "kid1", cat: "w",},
{id: "kid2", cat: "w",},
{id: "kid3", cat: "w",},
{id: "kid4", cat: "w",},
{id: "kid5", cat: "w",},
],
});
});
it("Result is Headed", function () {
assert.isTrue(isHeaded(testNode));
});
it("Left node is head", function () {
assert.isTrue(testNode.children[0].head);
});
it("Non-left nodes are not headed", function () {
for(let child of testNode.children.slice(1)) {
assert.isUndefined(child.head);
}
});
it("Does not break if given terminal", function () {
assert.isOk(addLeftHead(testNode.children[4]));
});
it("Does not bread if given an already left-headed node", function () {
assert.isOk(addLeftHead(testNode));
});
});
describe("addRightHead", function() {
let testNode;
beforeEach(function() {
testNode = addRightHead({
id: "root",
cat: "phi",
children: [
{id: "kid1", cat: "w",},
{id: "kid2", cat: "w",},
{id: "kid3", cat: "w",},
{id: "kid4", cat: "w",},
{id: "kid5", cat: "w",},
],
});
});
it("Result is Headed", function () {
assert.isTrue(isHeaded(testNode));
});
it("Right node is head", function () {
assert.isTrue(testNode.children[4].head);
});
it("Non-right nodes are not headed", function () {
for(let child of testNode.children.slice(0, 4)) {
assert.isUndefined(child.head);
}
});
it("Does not break if given terminal", function () {
assert.isOk(addRightHead(testNode.children[0]));
});
it("Does not break if given an already right-headed node", function () {
assert.isOk(addRightHead(testNode));
});
});
describe("getMinimalNodes", function() {
it("{(a) b}", function () {
let testList = getMinimalNodes({id: 'root', cat: 'i', children: [
{
id: 'intermediate',
cat: 'phi',
children: [{id: "a", cat: "w",}],
},
{id: "b", cat: "w",},
]});
assert.lengthOf(testList, 1);
assert.equal(testList[0].children[0].id, 'a');
});
it("(a (b (c)))", function () {
let testList = getMinimalNodes({
id: 'root',
cat: 'phi',
children: [
{id: 'a', cat: 'w'},
{
id: 'intermediate',
cat: 'phi',
children: [
{id: 'b', cat: 'w'},
{
id: 'minimal',
cat: 'phi',
children: [
{id: 'c', cat: 'w'}
]
}
]
}
]
});
assert.lengthOf(testList, 1);
assert.equal(testList[0].children[0].id, 'c')
});
});
describe("genHeadsForTree", function() {
let testTrees = {
'(a)': {id: 'root', cat: 'phi', children: [
{id: "a", cat: "w",},
],
},
'(a b c)': {id: 'root', cat: 'phi', children: [
{id: "a", cat: "w",},
{id: "b", cat: "w",},
{id: "c", cat: "w",},
],
},
'{(a) b}': {id: 'root', cat: 'i', children: [
{
id: 'intermediate',
cat: 'phi',
children: [{id: "a", cat: "w",}],
},
{id: "b", cat: "w",},
],
},
};
it("Correct length for (a): (a*)", function () {
assert.lengthOf(genHeadsForTree(testTrees['(a)']), 1);
});
it("All headed for (a)", function () {
for(let tree of genHeadsForTree(testTrees['(a)'])) {
assert.isTrue(isHeaded(tree));
};
});
it("Original (a) unchanged", function () {
node = testTrees['(a)'];
assert.isUndefined(node.head);
});
it("Correct length of (a b c): (a* b c) and (a b c*)", function () {
assert.lengthOf(genHeadsForTree(testTrees['(a b c)']), 2);
});
it("All headed for (a b c)", function () {
for(let tree of genHeadsForTree(testTrees['(a b c)'])) {
assert.isTrue(isHeaded(tree));
};
});
it("All one-headed for (a b c)", function () {
let tree1, tree2;
for(let tree of genHeadsForTree(testTrees['(a b c)'])) {
let numOfHeads = 0;
for(let terminal of tree.children) {
numOfHeads += terminal.head ? 1 : 0;
}
assert.equal(numOfHeads, 1);
}
});
it("All unique for (a b c)", function () {
let tree1, tree2;
[tree1, tree2] = genHeadsForTree(testTrees['(a b c)']);
assert.notDeepEqual(tree1, tree2);
});
it("Doesn't break when non-exhaustive", function () {
assert.lengthOf(genHeadsForTree(testTrees['{(a) b}']), 1);
});
it("Works for {(a b) (c d) (e f) (g h) (i j)}", function () {
var aj = {
"id": "CP1",
"cat": "cp",
"children": [
{
"cat": "phi",
"id": "XP_11",
"children": [
{
"id": "a",
"cat": "x0"
},
{
"id": "b",
"cat": "x0"
}
]
},
{
"cat": "phi",
"id": "XP_12",
"children": [
{
"id": "c",
"cat": "x0"
},
{
"id": "d",
"cat": "x0"
}
]
},
{
"cat": "phi",
"id": "XP_13",
"children": [
{
"id": "e",
"cat": "x0"
},
{
"id": "f",
"cat": "x0"
}
]
},
{
"cat": "phi",
"id": "XP_14",
"children": [
{
"id": "g",
"cat": "x0"
},
{
"id": "h",
"cat": "x0"
}
]
},
{
"cat": "phi",
"id": "XP_15",
"children": [
{
"id": "i",
"cat": "x0"
},
{
"id": "j",
"cat": "x0"
}
]
}
]
}
var expectedAJoutputString = '[{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":true},{"id":"b","cat":"x0"}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":true},{"id":"d","cat":"x0"}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":true},{"id":"f","cat":"x0"}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":true},{"id":"h","cat":"x0"}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":true},{"id":"j","cat":"x0"}]}]},{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":false},{"id":"b","cat":"x0","head":true}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":true},{"id":"d","cat":"x0"}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":true},{"id":"f","cat":"x0"}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":true},{"id":"h","cat":"x0"}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":true},{"id":"j","cat":"x0"}]}]},{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":true},{"id":"b","cat":"x0"}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":false},{"id":"d","cat":"x0","head":true}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":true},{"id":"f","cat":"x0"}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":true},{"id":"h","cat":"x0"}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":true},{"id":"j","cat":"x0"}]}]},{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":false},{"id":"b","cat":"x0","head":true}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":false},{"id":"d","cat":"x0","head":true}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":true},{"id":"f","cat":"x0"}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":true},{"id":"h","cat":"x0"}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":true},{"id":"j","cat":"x0"}]}]},{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":true},{"id":"b","cat":"x0"}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":true},{"id":"d","cat":"x0"}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":false},{"id":"f","cat":"x0","head":true}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":true},{"id":"h","cat":"x0"}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":true},{"id":"j","cat":"x0"}]}]},{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":false},{"id":"b","cat":"x0","head":true}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":true},{"id":"d","cat":"x0"}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":false},{"id":"f","cat":"x0","head":true}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":true},{"id":"h","cat":"x0"}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":true},{"id":"j","cat":"x0"}]}]},{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":true},{"id":"b","cat":"x0"}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":false},{"id":"d","cat":"x0","head":true}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":false},{"id":"f","cat":"x0","head":true}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":true},{"id":"h","cat":"x0"}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":true},{"id":"j","cat":"x0"}]}]},{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":false},{"id":"b","cat":"x0","head":true}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":false},{"id":"d","cat":"x0","head":true}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":false},{"id":"f","cat":"x0","head":true}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":true},{"id":"h","cat":"x0"}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":true},{"id":"j","cat":"x0"}]}]},{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":true},{"id":"b","cat":"x0"}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":true},{"id":"d","cat":"x0"}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":true},{"id":"f","cat":"x0"}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":false},{"id":"h","cat":"x0","head":true}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":true},{"id":"j","cat":"x0"}]}]},{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":false},{"id":"b","cat":"x0","head":true}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":true},{"id":"d","cat":"x0"}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":true},{"id":"f","cat":"x0"}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":false},{"id":"h","cat":"x0","head":true}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":true},{"id":"j","cat":"x0"}]}]},{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":true},{"id":"b","cat":"x0"}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":false},{"id":"d","cat":"x0","head":true}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":true},{"id":"f","cat":"x0"}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":false},{"id":"h","cat":"x0","head":true}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":true},{"id":"j","cat":"x0"}]}]},{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":false},{"id":"b","cat":"x0","head":true}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":false},{"id":"d","cat":"x0","head":true}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":true},{"id":"f","cat":"x0"}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":false},{"id":"h","cat":"x0","head":true}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":true},{"id":"j","cat":"x0"}]}]},{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":true},{"id":"b","cat":"x0"}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":true},{"id":"d","cat":"x0"}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":false},{"id":"f","cat":"x0","head":true}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":false},{"id":"h","cat":"x0","head":true}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":true},{"id":"j","cat":"x0"}]}]},{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":false},{"id":"b","cat":"x0","head":true}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":true},{"id":"d","cat":"x0"}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":false},{"id":"f","cat":"x0","head":true}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":false},{"id":"h","cat":"x0","head":true}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":true},{"id":"j","cat":"x0"}]}]},{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":true},{"id":"b","cat":"x0"}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":false},{"id":"d","cat":"x0","head":true}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":false},{"id":"f","cat":"x0","head":true}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":false},{"id":"h","cat":"x0","head":true}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":true},{"id":"j","cat":"x0"}]}]},{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":false},{"id":"b","cat":"x0","head":true}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":false},{"id":"d","cat":"x0","head":true}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":false},{"id":"f","cat":"x0","head":true}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":false},{"id":"h","cat":"x0","head":true}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":true},{"id":"j","cat":"x0"}]}]},{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":true},{"id":"b","cat":"x0"}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":true},{"id":"d","cat":"x0"}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":true},{"id":"f","cat":"x0"}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":true},{"id":"h","cat":"x0"}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":false},{"id":"j","cat":"x0","head":true}]}]},{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":false},{"id":"b","cat":"x0","head":true}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":true},{"id":"d","cat":"x0"}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":true},{"id":"f","cat":"x0"}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":true},{"id":"h","cat":"x0"}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":false},{"id":"j","cat":"x0","head":true}]}]},{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":true},{"id":"b","cat":"x0"}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":false},{"id":"d","cat":"x0","head":true}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":true},{"id":"f","cat":"x0"}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":true},{"id":"h","cat":"x0"}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":false},{"id":"j","cat":"x0","head":true}]}]},{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":false},{"id":"b","cat":"x0","head":true}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":false},{"id":"d","cat":"x0","head":true}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":true},{"id":"f","cat":"x0"}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":true},{"id":"h","cat":"x0"}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":false},{"id":"j","cat":"x0","head":true}]}]},{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":true},{"id":"b","cat":"x0"}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":true},{"id":"d","cat":"x0"}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":false},{"id":"f","cat":"x0","head":true}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":true},{"id":"h","cat":"x0"}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":false},{"id":"j","cat":"x0","head":true}]}]},{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":false},{"id":"b","cat":"x0","head":true}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":true},{"id":"d","cat":"x0"}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":false},{"id":"f","cat":"x0","head":true}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":true},{"id":"h","cat":"x0"}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":false},{"id":"j","cat":"x0","head":true}]}]},{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":true},{"id":"b","cat":"x0"}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":false},{"id":"d","cat":"x0","head":true}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":false},{"id":"f","cat":"x0","head":true}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":true},{"id":"h","cat":"x0"}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":false},{"id":"j","cat":"x0","head":true}]}]},{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":false},{"id":"b","cat":"x0","head":true}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":false},{"id":"d","cat":"x0","head":true}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":false},{"id":"f","cat":"x0","head":true}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":true},{"id":"h","cat":"x0"}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":false},{"id":"j","cat":"x0","head":true}]}]},{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":true},{"id":"b","cat":"x0"}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":true},{"id":"d","cat":"x0"}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":true},{"id":"f","cat":"x0"}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":false},{"id":"h","cat":"x0","head":true}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":false},{"id":"j","cat":"x0","head":true}]}]},{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":false},{"id":"b","cat":"x0","head":true}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":true},{"id":"d","cat":"x0"}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":true},{"id":"f","cat":"x0"}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":false},{"id":"h","cat":"x0","head":true}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":false},{"id":"j","cat":"x0","head":true}]}]},{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":true},{"id":"b","cat":"x0"}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":false},{"id":"d","cat":"x0","head":true}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":true},{"id":"f","cat":"x0"}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":false},{"id":"h","cat":"x0","head":true}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":false},{"id":"j","cat":"x0","head":true}]}]},{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":false},{"id":"b","cat":"x0","head":true}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":false},{"id":"d","cat":"x0","head":true}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":true},{"id":"f","cat":"x0"}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":false},{"id":"h","cat":"x0","head":true}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":false},{"id":"j","cat":"x0","head":true}]}]},{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":true},{"id":"b","cat":"x0"}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":true},{"id":"d","cat":"x0"}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":false},{"id":"f","cat":"x0","head":true}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":false},{"id":"h","cat":"x0","head":true}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":false},{"id":"j","cat":"x0","head":true}]}]},{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":false},{"id":"b","cat":"x0","head":true}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":true},{"id":"d","cat":"x0"}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":false},{"id":"f","cat":"x0","head":true}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":false},{"id":"h","cat":"x0","head":true}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":false},{"id":"j","cat":"x0","head":true}]}]},{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":true},{"id":"b","cat":"x0"}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":false},{"id":"d","cat":"x0","head":true}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":false},{"id":"f","cat":"x0","head":true}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":false},{"id":"h","cat":"x0","head":true}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":false},{"id":"j","cat":"x0","head":true}]}]},{"id":"CP1","cat":"cp","children":[{"cat":"phi","id":"XP_11","children":[{"id":"a","cat":"x0","head":false},{"id":"b","cat":"x0","head":true}]},{"cat":"phi","id":"XP_12","children":[{"id":"c","cat":"x0","head":false},{"id":"d","cat":"x0","head":true}]},{"cat":"phi","id":"XP_13","children":[{"id":"e","cat":"x0","head":false},{"id":"f","cat":"x0","head":true}]},{"cat":"phi","id":"XP_14","children":[{"id":"g","cat":"x0","head":false},{"id":"h","cat":"x0","head":true}]},{"cat":"phi","id":"XP_15","children":[{"id":"i","cat":"x0","head":false},{"id":"j","cat":"x0","head":true}]}]}]';
assert.equal(expectedAJoutputString, JSON.stringify(genHeadsForTree(aj)));
});
});
describe("genHeadsForList", function() {
let testTrees, testGENoutput, expectedOutput;
beforeEach(function() {
testTrees = [
{id: 'root', cat: 'phi', children: [
{id: 'supra-a', cat: 'phi', children: [
{cat: 'w', id: 'a'}
]},
{cat: 'w', id: 'b'},
{id: 'supra-c', cat: 'phi', children: [
{cat: 'w', 'id': 'c'}
]}
]},
{id: 'root', cat: 'i', children: [
{id: 'supra-a', cat: 'phi', children: [
{cat: 'w', id: 'a'}
]},
{cat: 'w', id: 'b'},
{id: 'supra-c', cat: 'phi', children: [
{cat: 'w', 'id': 'c'}
]}
]},
]
testGENoutput = [[{}, testTrees[0]], [{}, testTrees[1]]]
expectedOutput = [
{id: 'root', cat: 'phi', children: [
{id: 'supra-a', cat: 'phi', children: [
{cat: 'w', id: 'a', head: true}
]},
{cat: 'w', id: 'b'},
{id: 'supra-c', cat: 'phi', children: [
{cat: 'w', 'id': 'c', head: true}
]}
]},
{id: 'root', cat: 'i', children: [
{id: 'supra-a', cat: 'phi', children: [
{cat: 'w', id: 'a', head: true}
]},
{cat: 'w', id: 'b'},
{id: 'supra-c', cat: 'phi', children: [
{cat: 'w', 'id': 'c', head: true}
]}
]},
]
});
it("works for gen output (pairs of trees)", function () {
var expectedPairsOutput = [];
for(let i in expectedOutput){
expectedPairsOutput.push([{}, expectedOutput[i]]);
}
assert.deepEqual(expectedPairsOutput, genHeadsForList(testGENoutput));
});
it("works for list of trees", function () {
assert.deepEqual(expectedOutput, genHeadsForList(testTrees));
});
});
});
}
addHeadsToListTest();//When a test is failed, message() will return information about the options, stree, and ptree being used in the test.
// Helper for Mocha testing. No tests in this file.
function message(stree, ptree, options) {
options = options || {"no options": 0};
return `with ${Object.keys(options)},\
\n\t${parenthesizeTree(stree)} --> ${parenthesizeTree(ptree)}`;
}
//Same as message(), but also returns the direction of the align constraint (left or right).
function messageAlign(stree, ptree, d, options) {
options = options || {"no options": 0};
return `align with direction ${d} and ${Object.keys(options)},\
\n\t${parenthesizeTree(stree)} --> ${parenthesizeTree(ptree)}`;
}
function messageGEN(treePairs, expectedPairs, options){
options = options || {"no options": 0};
return `with ${Object.keys(options)},\
\n\tExpected ${convertTreePairsListToString(treePairs)} \n\tto equal ${convertTreePairsListToString(expectedPairs)}`;
}
function convertTreePairsListToString(treeList){
var pairStringList = [];
for(let i=0; i<treeList.length; i++){
let sp = [parenthesizeTree(treeList[i][0]), parenthesizeTree(treeList[i][1])].join(', ');
pairStringList = pairStringList.concat(sp);
}
return pairStringList.join('; ');
}
var assert;
function setUpMocha(tag = "save-load-section", index = 0, arg = "tag"){
mocha.setup('bdd');
assert = chai.assert;
const mochaDiv = document.createElement("div");
mochaDiv.setAttribute("id", "mocha");
var notResults;
if (arg === "tag"){
notResults = document.getElementById(tag);
}else{
notResults = document.getElementsByClassName(tag)[index];
}
notResults.insertBefore(mochaDiv, notResults.firstChild);
}// Interface testing with mocha and chai for input validation for string generation.
// Doesn't get auto-tested in the console at present because it requires the interface.
function _rStringInputValidationTest() {
describe("stringInputValidationTest.js", function(){
window.confirm = async function(){ //automatically returning true for confirm prompts
return true;
}
describe("Generate trees", function() {
this.timeout(15000); //timeout at 15000ms
//override timeout - write done inside the parenthesis of function()
//setTimeout(done, #timeout time in ms) for the test you want to change timeout
it("Generate combinations and permutations not added", function() {
document.getElementById("spotForm")["genStringsInput"].value = "";
document.getElementById("spotForm")["genStringsInput"].length = undefined;
document.getElementById("stringGeneration").classList = [""];
genTerminalStrings();
assert.equal(document.getElementById("warning").style.display, "none", "Displayed warning when there is none!");
});
it("Generate combinations and permutations closed", function() {
document.getElementById("spotForm")["genStringsInput"].value = "j";
document.getElementById("spotForm")["genStringsMin"].value = 3;
document.getElementById("spotForm")["genStringsMax"].value = 3;
document.getElementById("spotForm")["genStringsInput"].length = undefined;
document.getElementById("stringGeneration").classList = [""];
genTerminalStrings();
assert.equal(document.getElementById("warning").style.display, "block", "Displayed warning not showing!");
});
for(let i = 0; i < 11; i++){
it("Generate number: " + i, function() {
document.getElementById("error").style.display = "none";
document.getElementById("spotForm")["genStringsInput"].value = "j";
document.getElementById("spotForm")["genStringsMin"].value = i;
document.getElementById("spotForm")["genStringsMax"].value = i;
document.getElementById("spotForm")["genStringsInput"].length = undefined;
document.getElementById("stringGeneration").classList = ["open"];
genTerminalStrings();
if (i == 0 || i == 10){
assert.equal(document.getElementById("error").style.display, "block", "Displayed error not showing!");
}else{
assert.equal(document.getElementById("error").style.display, "none", "Displayed error when there is none!");
}
});
}
it("Generate no min or max present number", function() {
document.getElementById("spotForm")["genStringsInput"].value = "j";
document.getElementById("spotForm")["genStringsInput"].length = undefined;
document.getElementById("stringGeneration").classList = ["open"];
genTerminalStrings();
assert.equal(document.getElementById("error").style.display, "block", "Displayed error not showing!");
});
it("Generate min or max present not number", function() {
document.getElementById("spotForm")["genStringsInput"].value = "j";
document.getElementById("spotForm")["genStringsMin"].value = "j";
document.getElementById("spotForm")["genStringsMax"].value = "j";
document.getElementById("spotForm")["genStringsInput"].length = undefined;
document.getElementById("stringGeneration").classList = ["open"];
genTerminalStrings();
assert.equal(document.getElementById("error").style.display, "block", "Displayed error not showing!");
});
it("Generate min greater than max", function() {
document.getElementById("spotForm")["genStringsInput"].value = "j"
document.getElementById("spotForm")["genStringsMin"].value = 5;
document.getElementById("spotForm")["genStringsMax"].value = 3;
document.getElementById("spotForm")["genStringsInput"].length = undefined;
document.getElementById("stringGeneration").classList = ["open"];
genTerminalStrings();
assert.equal(document.getElementById("error").style.display, "block", "Displayed error not showing!");
});
});
describe("Generate terminal strings", function() {
this.timeout(15000);
it("Input not added", function() {
document.getElementById("spotForm")["genStringsInput"].value = "";
document.getElementById("spotForm")["genStringsInput"].length = undefined;
document.getElementById("stringGeneration").classList = ["open"];
genTerminalStrings();
assert.equal(document.getElementById("error").style.display, "block", "Displayed error not showing!");
});
for(let i = 0; i < 11; i++){
it("Generate number: " + i, function() {
document.getElementById("error").style.display = "none";
document.getElementById("spotForm")["genStringsInput"].value = "j"
document.getElementById("spotForm")["genStringsMin"].value = i;
document.getElementById("spotForm")["genStringsMax"].value = i;
document.getElementById("spotForm")["genStringsInput"].length = undefined;
document.getElementById("stringGeneration").classList = ["open"];
genTerminalStrings();
if (i == 0 || i == 10){
assert.equal(document.getElementById("error").style.display, "block", "Displayed error not showing!");
}else{
assert.equal(document.getElementById("error").style.display, "none", "Displayed error when there is none!");
}
});
}
it("Generate no min or max present number", function() {
document.getElementById("spotForm")["genStringsInput"].value = "j";
document.getElementById("spotForm")["genStringsInput"].length = undefined;
document.getElementById("stringGeneration").classList = ["open"];
genTerminalStrings();
assert.equal(document.getElementById("error").style.display, "block", "Displayed error not showing!");
});
it("Generate min or max present not number", function() {
document.getElementById("spotForm")["genStringsInput"].value = "j";
document.getElementById("spotForm")["genStringsMin"].value = "j";
document.getElementById("spotForm")["genStringsMax"].value = "j";
document.getElementById("spotForm")["genStringsInput"].length = undefined;
document.getElementById("stringGeneration").classList = ["open"];
genTerminalStrings();
assert.equal(document.getElementById("error").style.display, "block", "Displayed error not showing!");
});
it("Generate min greater than max", function() {
document.getElementById("spotForm")["genStringsInput"].value = "j"
document.getElementById("spotForm")["genStringsMin"].value = 5;
document.getElementById("spotForm")["genStringsMax"].value = 3;
document.getElementById("spotForm")["genStringsInput"].length = undefined;
document.getElementById("stringGeneration").classList = ["open"];
genTerminalStrings();
assert.equal(document.getElementById("error").style.display, "block", "Displayed error not showing!");
});
});
});
}
function runStringInputValidationTest() {
setUpMocha();
_rStringInputValidationTest();
mocha.run();
}// Interface testing with mocha and chai. Tests save/load/clear for string generation on the interface.
// Doesn't get auto-tested in the console at present because it requires the interface.
function _rStringTest() {
describe("stringTest.js", function(){
describe("String Generation save/load/clear test", function(){
var testSettings = '';
var numOfInputs = 0;
const arbitraryStrings = ["9000", "9001", "9002", "9003", "4", "3",
"9006", "5", "2", "9009"]; //strings you would not find anywhere else in the saved analysis
//apart from 4, 3, 5 and 2, which were needed to avoid messing up the input validation in terminal string generation. They are used as max and min values in the tests "Load with one / two strings"
var unusedStrings, listDiv, inputs; //assigned beforeEach below
beforeEach(function() {
//runs before each "it" block (hence, beforeEach)
unusedStrings = arbitraryStrings.slice(); //shallow copy, fyi
listDiv = document.getElementById("listOfTerminals");
inputs = listDiv.getElementsByTagName("input"); //reset b/c inputs added/removed
changeInputTabs('goButton', 'inputButton');
});
it("Save with one string", function(){
for(let input of inputs) {
// assign an arbitrary string to each input
if(input.type === 'text') {
numOfInputs ++;
input.value = unusedStrings.pop();
}
}
let savedString = record_analysis();
testSettings = JSON.parse(savedString).myTrees;
//object is more useful than string later on. should not change until "two strings" tests
for(let i = 0; i < numOfInputs.length; i++){
//all we need to know now is that the arbitrary strings all ended up in the saved analysis
let regex = new RegExp(arbitraryStrings[arbitraryStrings.length - i]);
assert(savedString.search(regex) > 0, "Input number " + i + " was not saved");
}
});
it("Clear with one string", function() {
clearAnalysis();
for(let input of inputs) {
assert(input.value == '', input.name + " is not cleared");
}
});
it("Load with one string", function() {
//console.log(testSettings);
//load earlier saved string
my_built_in_analysis({}, false, testSettings, []);
//{id:'root', cat:'cp'}, {id:'root', cat:'xp'}
for(let input of inputs) {
//we know the order arbitraryStrings were assigned, check that the same order is preserved
assert(input.value === unusedStrings.pop(), input.name + " did not load correctly");
}
});
it("Save with two strings", function() {
/*All we have to do now is click the "add list of terminals" button to get
more terminal string inputs and run the exact same three testcases above.
I don't want to factor out the copied code, though, because then clicking
on the test case in mocha would be less useful.*/
document.getElementById("addList").click();
for(let input of inputs) {
if(input.type === 'text') {
numOfInputs ++;
input.value = unusedStrings.pop();
}
}
let savedString = record_analysis();
testSettings = JSON.parse(savedString).myTrees;
for(let i = 0; i < numOfInputs.length; i++){
let regex = new RegExp(arbitraryStrings[arbitraryStrings.length - i]);
assert(savedString.search(regex) > 0, "Input number " + i + " was not saved");
}
});
it("Clear with two strings", function() {
clearAnalysis();
for(let input of inputs) {
assert(input.value == '', input.name + " is not cleared");
}
});
it("Load with two strings", function() {
//console.log(testSettings);
my_built_in_analysis({}, false, testSettings, []);
for(let input of inputs) {
assert(input.value === unusedStrings.pop(), input.name + " did not load correctly");
}
});
});
});
}
function runStringTest() {
setUpMocha();
_rStringTest();
mocha.run();
}