-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.html
More file actions
2363 lines (1914 loc) · 137 KB
/
test.html
File metadata and controls
2363 lines (1914 loc) · 137 KB
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
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1">
<title>gotoв!</title>
</head>
<body>
<noscript>Javascript is deactivated in your browser. Please activate it in order to use this page.</noscript>
<script src="https://cdn.jsdelivr.net/gh/douglascrockford/JSON-js@aef828bfcd7d5efaa41270f831f8d27d5eef3845/json2.min.js"></script>
<script>
var time = Date.now ? function () {return Date.now ()} : function () {return new Date ().getTime ()};
var Time = {zero: time ()}
</script>
<script src="gotoB.min.js"></script>
<script>
Time ['0: load'] = time () - Time.zero;
</script>
<script>
(function () {
// *** SETUP ***
var dale = window.dale, teishi = window.teishi, lith = window.lith, c = window.c, B = window.B;
var type = teishi.type, eq = teishi.eq, last = teishi.last, clog = teishi.clog;
// We override dale.clog to avoid seeing a ton of alerts on old browsers.
try {
dale.clog = console.log.bind (console);
}
catch (error) {
dale.clog = function () {
var output = dale.go (arguments, function (v) {return v === undefined ? 'undefined' : v}).join (' ');
if (window.console) window.console.log (output);
}
}
window.Data = B.store.Data = {};
window.State = B.store.State = {};
// *** TESTING ***
var Test = function (tag, fun) {
var error = fun ();
if (! error) return B.call ('log', 'test OK', tag);
B.call ('log', 'test error', error);
B.eventlog ();
throw new Error (tag + ': ' + error);
}
var Errorlist = [];
window.onerror = function () {
Errorlist.unshift (arguments);
}
// *** BASE RESPONDERS ***
Test ('initial setup', function () {
if (! eq (dale.keys (B.responders).sort (), ['add', 'error', 'madd', 'mrem', 'mset', 'rem', 'set'])) return 'Initial responders mismatch.';
// We use a setTimeout to wait until all the event responders are set before we call the first we want to execute.
setTimeout (function () {B.call ('test', 'log')}, 0);
});
// *** B.LOG ***
B.respond ('test', 'log', {burn: true}, function (x) {
B.call (x, 'verb1', 'a path', 'booyah');
B.call (x, 'verb2', 'a path');
Test ('log', function () {
if (B.log.length !== 5) return 'Invalid amount of log entries.';
var mismatch = dale.stopNot ([
{from: undefined, id: 'E1', verb: 'log', path: ['test OK'], args: ['initial setup']},
{from: undefined, id: 'E2', verb: 'test', path: ['log'], args: undefined},
{from: 'E2', id: 'R8', verb: 'test', path: ['log'], args: undefined},
{from: 'R8/E2', id: 'E3', verb: 'verb1', path: ['a path'], args: ['booyah']},
{from: 'R8/E2', id: 'E4', verb: 'verb2', path: ['a path'], args: undefined}
], undefined, function (v, k) {
var log = teishi.copy (B.log [k]);
delete log.t;
if (! eq (v, log)) return k + ': ' + B.str (log);
});
if (mismatch !== undefined) return 'Invalid log entry #' + (mismatch);
});
B.call (x, 'test', 'error');
});
B.respond ('test', 'error', {burn: true}, function (x) {
Test ('show eventlog in case of error', function () {
B.call (x, 'error', 'test error');
if (! c ('#eventlog')) return 'Eventlog was not shown when error happened.';
if (! c ('#eventlog-snackbar')) return 'Snackbar was not shown when error happened.';
});
// Turn off the error responder until it is tested.
B.responders.error.disabled = true;
B.call (x, 'test', 'data');
});
// *** DATA FUNCTIONS ***
B.respond ('test', 'data', {burn: true}, function (x) {
Test ('data', function () {
var error;
// *** B.GET ***
error = dale.stopNot ([['ok', /notok/], [/notok/], ['ok', null], [null], ['ok', {}], [{}], ['ok', undefined], [undefined], ['ok', NaN], [NaN]], undefined, function (v, k) {
if (B.get.apply (null, v) !== undefined) return 'B.get accepted invalid path #' + (k + 1) + '-1.';
if (B.get (v) !== undefined) return 'B.get accepted invalid path #' + (k + 1) + '-2.';
});
if (error) return error;
Data.items = ['hello'];
Data.item = 'hello';
error = dale.stopNot ([[[], B.store], [['Data'], Data], [['Data', 'items'], Data.items], [['Data', 'items', 0], Data.items [0]], [['Data', 'item'], Data.item], [['foo'], undefined]], undefined, function (v, k) {
if (B.get.apply (null, v [0]) !== v [1]) return 'B.get returned invalid value #' + (k + 1) + '-1.';
if (B.get (v [0]) !== v [1]) return 'B.get returned invalid value #' + (k + 1) + '-2.';
});
if (error) return error;
delete Data.items;
delete Data.item;
// *** B.SET ***
error = dale.stopNot ([[/notok/, 'ok'], [/notok/], [null, 'ok'], [null], [{}, {}, 'ok'], [{}], [undefined, 'ok'], [undefined], [NaN, 'ok'], [NaN]], undefined, function (v, k) {
if (k === 0) v.unshift (x);
if (B.set.apply (null, v) !== false) return 'B.set accepted invalid path #' + (k + 1) + '.';
});
if (error) return error;
if (B.set (x, [], 'a') !== false) return 'B.set accepted simple value with empty path #1.';
if (B.set ([], 'a') !== false) return 'B.set accepted simple value with empty path #2.';
if (B.set (x, [], 333) !== false) return 'B.set accepted simple value with empty path #3.';
if (B.set ([], 333) !== false) return 'B.set accepted simple value with empty path #4.';
error = dale.stopNot ([[['Data', 'items', 0], null], [['State', 'view'], 'main'], ['foo', 'bar'], [[], []], [[], {}], [[0], 1]], undefined, function (v, k) {
if (k === 0) v.unshift (x);
if (B.set.apply (null, v) !== true) return 'B.set rejected valid arguments #' + (k + 1) + '.';
if (B.get (last (v, 2)) !== last (v)) return 'B.set didn\'t set value correctly #' + (k + 1) + '.';
});
if (error) return error;
error = dale.stopNot ([[['Data', 'items'], [0, 1, 2], {Data: {items: [0, 1, 2]}}], [['Data', 'key'], 'val', {Data: {items: [0, 1, 2], key: 'val'}}], [['Data', 0], 1, {Data: [1]}], [[], {Data: {}, State: {}}, {Data: {}, State: {}}]], undefined, function (v, k) {
k === 0 ? B.set (x, v [0], v [1]) : B.set (v [0], v [1]);
if (! eq (B.store, v [2])) return 'B.set overwrite error #' + (k + 1) + '.';
});
if (error) return error;
// *** B.ADD ***
error = dale.stopNot ([[/notok/, 'ok'], [/notok/], [null, 'ok'], [null], [{}, {}, 'ok'], [{}], [undefined, 'ok'], [undefined], [NaN, 'ok'], [NaN]], undefined, function (v, k) {
if (k === 0) v.unshift (x);
if (B.add.apply (null, v) !== false) return 'B.add accepted invalid path #' + (k + 1) + '.';
});
if (error) return error;
error = dale.stopNot ([[['Data', 'items'], null, [null]], [['Data', 'items'], 0, [null, 0]], [['Data', 'items'], [null, 0]], [['Data', 'items'], 1, 2, [null, 0, 1, 2]]], undefined, function (v, k) {
if (k === 0) v.unshift (x);
if (B.add.apply (null, v.slice (0, -1)) !== true) return 'B.add rejected valid arguments #' + (k + 1) + '.';
if (! eq (B.get (v [k === 0 ? 1 : 0]), last (v))) return 'B.add didn\'t add values correctly #' + (k + 1) + '.';
});
if (error) return error;
if (B.add (x, 'Data', 0) !== false) return 'B.add didn\'t reject non-undefined target #1.';
if (B.add ('Data', 0) !== false) return 'B.add didn\'t reject non-undefined target #2.';
B.add (x, 'foo');
if (! eq (B.get ('foo'), [])) return 'B.add didn\'t create array when receiving an undefined target and no elements to push #1.';
B.add ('foo');
if (! eq (B.get ('foo'), [])) return 'B.add didn\'t create array when receiving an undefined target and no elements to push #2.';
delete Data.items;
delete B.store.foo;
// *** B.REM ***
error = dale.stopNot ([[/notok/, 'ok'], [/notok/], [null, 'ok'], [null], [{}, {}, 'ok'], [{}], [undefined, 'ok'], [undefined], [NaN, 'ok'], [NaN]], undefined, function (v, k) {
if (k === 0) v.unshift (x);
if (B.rem.apply (null, v) !== false) return 'B.rem accepted invalid path #' + (k + 1) + '.';
});
if (error) return error;
B.set (['Data', 'items'], ['a', 'b', 'c']);
error = dale.stopNot ([[['Data', 'items'], 'a'], [['Data', 'items'], 0.1], ['Data', 0], [[], /foo/], ['Data', ['items', /foo/]]], undefined, function (v, k) {
if (k === 0) v.unshift (x);
if (B.rem.apply (null, v) !== false) return 'B.rem allowed removing keys of the wrong type #' + (k + 1) + '.';
});
if (error) return error;
error = dale.stopNot ([[['Data', 'foo'], 'a'], [[], 'foo'], ['Data', 'foo'], [['Data', 'items']], ['Data'], ['foo', /bar/], [['Data', 'items'], 5]], undefined, function (v, k) {
if (k === 0) v.unshift (x);
if (B.rem.apply (null, v) !== true) return 'B.rem returned error when performing no-op #' + (k + 1) + '.';
if (! eq (B.store, {State: {}, Data: {items: ['a', 'b', 'c']}})) return 'B.rem modified store when performing no-op #' + (k + 1) + '.';
});
if (error) return error;
var keys = [0, 2];
if (B.rem (['Data', 'items'], keys) !== true) return 'B.rem returned error when performing operation #1.';
if (! eq (B.get ('Data', 'items'), ['b'])) return 'B.rem didn\'t perform operation successfully #1.';
if (! eq (keys, [0, 2])) return 'B.rem modified keys array #1.';
if (B.rem ('Data', ['items']) !== true) return 'B.rem returned error when performing operation #2.';
if (! eq (B.get ('Data'), {})) return 'B.rem didn\'t perform operation successfully #2.';
B.set (['Data', 'items'], ['a', 'b', 'c']);
if (B.rem.apply (null, [['Data', 'items']].concat (keys)) !== true) return 'B.rem returned error when performing operation #3.';
if (! eq (B.get ('Data', 'items'), ['b'])) return 'B.rem didn\'t perform operation successfully #3.';
if (! eq (keys, [0, 2])) return 'B.rem modified keys array #2.';
if (B.rem ('Data', ['items']) !== true) return 'B.rem returned error when performing operation #4.';
if (! eq (B.get ('Data'), {})) return 'B.rem didn\'t perform operation successfully #4.';
if (B.rem ([], 'Data', 'State') !== true) return 'B.rem returned error when performing operation #5.';
if (! eq (B.get (), {})) return 'B.rem didn\'t perform operation successfully #5.';
B.set ([], {Data: {}, Store: {}});
// *** DATA EVENTS ***
var incrementOnChange = 0;
var responder = B.respond ('change', [], {match: function (ev) {return ev.verb === 'change'}}, function () {
incrementOnChange++;
});
dale.go ([[/notok/, 'ok'], [/notok/], [null, 'ok'], [null], [{}, 'ok'], [{}], [undefined, 'ok'], [undefined], [NaN, 'ok'], [NaN]], function (v, k) {
B.call.apply (null, [x, 'set'].concat (v));
});
B.set (['Data', 'items'], ['a', 'b', 'c']);
dale.go ([[/notok/, 'ok'], [/notok/], [null, 'ok'], [null], [{}, 'ok'], [{}], [undefined, 'ok'], [undefined], [NaN, 'ok'], [NaN], [['Data', 'items', 0], 0]], function (v, k) {
B.call.apply (null, [x, 'add'].concat (v));
});
// The last add will trigger an event (because it has a valid path) but it won't trigger a change event.
if (dale.fil (B.log, undefined, function (log) {
if (log.verb === 'change') return log;
}).length) return 'change event logged with a data event receiving invalid arguments #1.';
// The last add should trigger an error
if (incrementOnChange !== 0) return 'change event fired with a data event receiving invalid arguments #1.';
dale.go ([[/notok/, 'ok'], [/notok/], [null, 'ok'], [null], [{}, 'ok'], [{}], [undefined, 'ok'], [undefined], [NaN, 'ok'], [NaN]], function (v, k) {
B.call.apply (null, [x, 'rem'].concat (v));
});
if (dale.fil (B.log, undefined, function (log) {
if (log.verb === 'change') return log;
}).length) return 'change event logged with a data event receiving invalid arguments #2.';
if (incrementOnChange !== 0) return 'change event fired with a data event receiving invalid arguments #2.';
// All these invalid invocations will trigger an event (because they have valid paths) but they won't trigger a change event.
dale.go ([[['Data', 'items'], 'a'], ['Data', 0], [[], /foo/], ['Data', ['items', /foo/]]], function (v, k) {
B.call.apply (null, [x, 'rem'].concat (v));
});
if (dale.fil (B.log, undefined, function (log) {
if (log.verb === 'change') return log;
}).length) return 'change event logged with a data event receiving invalid arguments #3.';
if (incrementOnChange !== 0) return 'change event fired with a data event receiving invalid arguments #3.';
var llog = last (B.log);
if (! eq ({verb: llog.verb, path: llog.path, from: llog.from}, {verb: 'error', path: ['B.rem'], from: last (B.log, 2).id + '/' + last (B.log, 3).id})) return 'error event not fired or fired improperly.';
// no-ops
dale.go ([['set', ['Data', 'items'], teishi.copy (B.get ('Data', 'items'))], ['set', [], teishi.copy (B.store)], ['set', 'foo', undefined], ['set', 'Data', B.get ('Data')], ['set', 'Data', teishi.copy (B.get ('Data'))], ['add', ['Data', 'items']], ['rem', ['Data', 'items'], 3, 4], ['rem', ['Data', 'items'], [3, 4]], ['rem', 'foo', 'bar'], ['rem', ['Data', 'items'], []], ['rem', ['Data', 'items']]], function (v, k) {
B.call.apply (null, k % 2 === 0 ? [x].concat (v) : v);
});
if (dale.fil (B.log, undefined, function (log) {
if (log.verb === 'change') return log;
}).length) return 'change event logged with a data event performing a no-op.';
if (incrementOnChange !== 0) return 'change event fired with a data event performing a no-op.';
var changeCount = 0;
var error = dale.stopNot ([
['set', [], {Data: {}, Store: {}}],
['add', 'Array', 'whatever'],
['rem', [], 'Array'],
['add', ['Data', 'items'], 'one'],
['add', ['Data', 'items'], 'two', 'three'],
['add', ['Data', 'items'], 'four', 'five'],
['rem', ['Data', 'items'], 3, 4],
['rem', ['Data', 'items'], [0, 1, 2]],
['add', ['Data', 'items'], undefined, null, {three: 3}, false],
['set', 'Other', true],
['rem', [], 'Other'],
['set', ['Data', 'obj', 'last'], 'playboy'],
['set', ['Data', 'items', 0, 0], 'beep'],
['set', ['Data', 'null'], null],
['rem', ['Data'], 'obj', 'null']
], undefined, function (test, k) {
var evid = B.call.apply (null, [x].concat (test)), eprefix = 'Test #' + (k + 1) + ': ', path = type (test [1]) === 'array' ? test [1] : [test [1]];
changeCount += {6: 2, 7: 3, 14: 2} [k] || 1;
if (type (evid) !== 'string') return eprefix + 'B.call didn\'t return event id.';
if (incrementOnChange !== changeCount) return eprefix + 'incorrect amount of change events triggered, expecting ' + changeCount + ', got ' + incrementOnChange;
var args = type (test [2]) === 'array' ? test [2] : test.slice (2);
if (test [0] === 'rem') {
var error;
dale.go ({2: 'Array', 6: [4, 3], 7: [2, 1, 0], 10: 'Other', 14: ['null', 'obj']} [k], function (v, k) {
if (! teishi.eq (test [1].concat (v), last (B.log, (k + 1) * 2).path)) error = 'Invalid path on change event triggered by rem, expecting ' + B.str (test [1].concat (v)) + ', got ' + B.str (last (B.log, (k + 1) * 2).path);
});
if (error) return error;
if (dale.stop (args, true, function (key) {
return dale.keys (B.get (test [1])).indexOf (key) > -1;
})) return eprefix + 'rem didn\'t remove key.';
return;
}
if (dale.stop (path, true, function (pathitem, k2) {
var target = B.get (path.slice (0, k2));
if (type (target) !== (type (pathitem) === 'string' ? 'object' : 'array')) return true;
})) return eprefix + 'add/set didn\'t set parent target with the right type.';
if (test [0] === 'add' && type (B.get (path)) !== 'array') return eprefix + 'add didn\'t set the target type correctly.';
if (test [0] === 'set' && path.length) {
if (type (B.get (path.slice (0, -1))) !== (type (last (path)) === 'string' ? 'object' : 'array')) return eprefix + 'set didn\'t set the target type correctly.';
}
if (test [0] === 'add' && ! eq (B.get (path).slice (B.get (path).length - args.length), args)) return eprefix + 'add didn\'t add the value correctly.';
if (test [0] === 'set' && ! eq (B.get (test [1]), test [2])) return eprefix + 'set didn\'t set the value correctly.';
});
if (error) return error;
// *** MUTE DATA EVENTS ***
var lastIncrementOnChange = incrementOnChange;
var error = dale.stopNot ([
['mset', [], {Data: {}, Store: {}}],
['madd', 'Array', 'whatever'],
['mrem', [], 'Array'],
['madd', ['Data', 'items'], 'one'],
['madd', ['Data', 'items'], 'two', 'three'],
['madd', ['Data', 'items'], 'four', 'five'],
['mrem', ['Data', 'items'], 3, 4],
['mrem', ['Data', 'items'], [0, 1, 2]],
['madd', ['Data', 'items'], undefined, null, {three: 3}, false],
['mset', 'Other', true],
['mrem', [], 'Other'],
['mset', ['Data', 'obj', 'last'], 'playboy'],
['mset', ['Data', 'items', 0, 0], 'beep'],
['mset', ['Data', 'null'], null],
['mrem', 'Data', 'obj', 'null']
], undefined, function (test, k) {
var evid = B.call.apply (null, [x].concat (test)), eprefix = 'Test #' + (k + 1) + ': ', path = type (test [1]) === 'array' ? test [1] : [test [1]];
if (type (evid) !== 'string') return eprefix + 'B.call didn\'t return event id.';
if (incrementOnChange !== lastIncrementOnChange) return eprefix + ' change event triggered on mute event.';
var args = type (test [2]) === 'array' ? test [2] : test.slice (2);
if (test [0] === 'rem') {
if (dale.stop (args, true, function (key) {
return dale.keys (B.get (test [1])).indexOf (key) > -1;
})) return eprefix + 'rem didn\'t remove key.';
return;
}
if (dale.stop (path, true, function (pathitem, k2) {
var target = B.get (path.slice (0, k2));
if (type (target) !== (type (pathitem) === 'string' ? 'object' : 'array')) return true;
})) return eprefix + 'add/set didn\'t set parent target with the right type.';
if (test [0] === 'add' && type (B.get (path)) !== 'array') return eprefix + 'add didn\'t set the target type correctly.';
if (test [0] === 'set' && path.length) {
if (type (B.get (path.slice (0, -1))) !== (type (last (path)) === 'string' ? 'object' : 'array')) return eprefix + 'set didn\'t set the target type correctly.';
}
if (test [0] === 'add' && ! eq (B.get (path).slice (B.get (path).length - args.length), args)) return eprefix + 'add didn\'t add the value correctly.';
if (test [0] === 'set' && ! eq (B.get (test [1]), test [2])) return eprefix + 'set didn\'t set the value correctly.';
});
B.forget (responder);
// *** B.CHANGERESPONDER ***
var matches = [];
var responder2 = B.respond ('change', ['foo', 'bar'], {match: B.changeResponder}, function (x) {
matches.push (x.path);
});
B.call (x, 'set', [], teishi.copy (B.store));
if (matches.length !== 0) return 'B.changeResponder error #1';
B.call (x, 'set', ['foo', 'bar'], undefined);
if (matches.length !== 0) return 'B.changeResponder error #2';
B.call (x, 'set', ['foo', 'bar'], 2);
if (! eq (matches, [['foo', 'bar']])) return 'B.changeResponder error #3';
B.call (x, 'set', ['foo'], {bar: 2}, 2);
if (! eq (matches, [['foo', 'bar']])) return 'B.changeResponder error #4';
B.call (x, 'set', ['foo'], {bar: 3});
if (! eq (matches, [['foo', 'bar'], ['foo']])) return 'B.changeResponder error #5';
B.call (x, 'set', [], teishi.copy (B.store));
if (! eq (matches, [['foo', 'bar'], ['foo']])) return 'B.changeResponder error #6';
B.call (x, 'set', [], {foo: {bar: 2}});
if (! eq (matches, [['foo', 'bar'], ['foo'], []])) return 'B.changeResponder error #7';
B.call (x, 'set', ['foo', 'bar', 'yip'], 3);
if (! eq (matches, [['foo', 'bar'], ['foo'], [], ['foo', 'bar', 'yip']])) return 'B.changeResponder error #8';
B.call (x, 'set', ['foo', 'bar', 'yip'], 3);
if (! eq (matches, [['foo', 'bar'], ['foo'], [], ['foo', 'bar', 'yip']])) return 'B.changeResponder error #9';
B.call (x, 'set', ['foo', 'other'], 3);
if (! eq (matches, [['foo', 'bar'], ['foo'], [], ['foo', 'bar', 'yip']])) return 'B.changeResponder error #10';
B.call (x, 'change', ['foo', 'bar']);
if (! eq (matches, [['foo', 'bar'], ['foo'], [], ['foo', 'bar', 'yip'], ['foo', 'bar']])) return 'B.changeResponder error #11';
B.call (x, 'change', ['foo']);
if (! eq (matches, [['foo', 'bar'], ['foo'], [], ['foo', 'bar', 'yip'], ['foo', 'bar'], ['foo']])) return 'B.changeResponder error #12';
B.call (x, 'change', ['foo', 'yip']);
if (! eq (matches, [['foo', 'bar'], ['foo'], [], ['foo', 'bar', 'yip'], ['foo', 'bar'], ['foo']])) return 'B.changeResponder error #13';
B.call (x, 'change', ['yip']);
if (! eq (matches, [['foo', 'bar'], ['foo'], [], ['foo', 'bar', 'yip'], ['foo', 'bar'], ['foo']])) return 'B.changeResponder error #14';
B.call (x, 'change', []);
if (! eq (matches, [['foo', 'bar'], ['foo'], [], ['foo', 'bar', 'yip'], ['foo', 'bar'], ['foo'], []])) return 'B.changeResponder error #15';
var currentValues = [];
var previousValues = [];
var responder3 = B.respond ('change', ['esa', 'abuela'], function (x, currentValue, previousValue) {
currentValues.push (currentValue);
previousValues.push (previousValue);
});
B.call (x, 'set', ['esa', 'abuela'], 1);
B.call (x, 'set', ['esa', 'abuela'], 2);
if (! eq (currentValues, [1, 2])) return 'change event not fired with currentValue.';
if (! eq (previousValues, [undefined, 1])) return 'change event not fired with previousValue.';
B.forget (responder2);
B.forget (responder3);
});
// We update the references since we've re-assigned Data and State during the above tests.
B.store = {};
window.Data = B.store.Data = {};
window.State = B.store.State = {};
B.call (x, 'test', 'B.ev');
});
// *** B.EV ***
B.respond ('test', 'B.ev', {burn: true}, function (x) {
Test ('B.str', function () {
if ([
'[undefined, null, function () {return true}, false, /a/, "a", 3, {"a": "\'b", "c": ["d\\"", NaN, Infinity], "e-f": null}]',
// Compatibility: Safari <= 8
'[undefined, null, function () {return true;}, false, /a/, "a", 3, {"a": "\'b", "c": ["d\\"", NaN, Infinity], "e-f": null}]',
// Compatibility: FF <= 16
'[undefined, null, function () {\n return true;\n}, false, /a/, "a", 3, {"a": "\'b", "c": ["d\\"", NaN, Infinity], "e-f": null}]'
].indexOf (B.str ([undefined, null, function () {return true}, false, /a/, 'a', 3, {a: '\'b', c: ['d"', NaN, Infinity], 'e-f': null}])) === -1) return 'Warning: B.str setting list of values in a different way: ' + B.str ([undefined, null, function () {return true}, false, /a/, 'a', 3, {a: '\'b', c: ['d"', NaN, Infinity], 'e-f': null}]);
var test = eval (B.str ([undefined, null, function () {return true}, false, /a/, 'a', 3, {a: '\'b', c: ['d"', NaN, Infinity], 'e-f': null}]));
if (test [0] !== undefined) return 'B.str didn\'t set undefined.';
if (test [1] !== null) return 'B.str didn\'t set null.';
if (test [2] () !== true) return 'B.str didn\'t set function.';
if (test [3] !== false) return 'B.str didn\'t set function.';
if (type (test [4]) !== 'regex') return 'B.str didn\'t set regex.';
if (test [5] !== 'a') return 'B.str didn\'t set string.';
if (test [6] !== 3) return 'B.str didn\'t set number.';
if (! test [7] || test [7].a !== "'b" || test [7].c [0] !== 'd"' || type (test [7].c [1]) !== 'nan' || test [7].c [2] !== Infinity) return 'B.str didn\'t set object properly.';
});
Test ('B.ev validation', function () {
var error = dale.stopNot ([
[[/boo/], 'invalid verb'],
[[[/boo/]], 'invalid verb'],
[[null], 'invalid verb'],
[[[null]], 'invalid verb'],
[['boo'], 'no path'],
[[['boo']], 'no path'],
[['boo', 0.5], 'invalid path'],
[[['boo'], [null]], 'invalid path']
], undefined, function (v, k) {
if (B.ev.apply (null, v [0]) !== false) return 'B.ev accepted invalid arguments: ' + v [1] + ' (#' + (k + 1) + ').';
});
if (error) return error;
});
Test ('B.ev valid input', function () {
var defaultValue = ! B.internal.oldFF ? 'this.value' : 'this.value || (this.attributes.value ? this.attributes.value.nodeValue : "")';
var error = dale.stopNot ([
[[], ''],
[[[]], ''],
[[['a', 'b'], []], ' B.call ({"from": id}, "a", "b", ' + defaultValue + ');'],
[[[], ['a', 'b']], ' B.call ({"from": id}, "a", "b", ' + defaultValue + ');'],
[['a', 'b'], ' B.call ({"from": id}, "a", "b", ' + defaultValue + ');'],
[['a', ['b']], ' B.call ({"from": id}, "a", ["b"], ' + defaultValue + ');'],
[['a', 'b', 'c'], ' B.call ({"from": id}, "a", "b", "c");'],
[['a', 'b', undefined], ' B.call ({"from": id}, "a", "b", undefined);'],
[['a', 'b', null], ' B.call ({"from": id}, "a", "b", null);'],
[['a', 'b', null, true, NaN], ' B.call ({"from": id}, "a", "b", null, true, NaN);'],
[['a', 'b', /null/], ' B.call ({"from": id}, "a", "b", /null/);'],
[['a', 'b', {c: 'd'}], ' B.call ({"from": id}, "a", "b", {"c": "d"});'],
[['a', 'b', [{c: 'd'}]], ' B.call ({"from": id}, "a", "b", [{"c": "d"}]);'],
[['a', 'b', {raw: 'event'}], ' B.call ({"from": id}, "a", "b", event);'],
[['a', 'b', 'c', {raw: 'event'}], ' B.call ({"from": id}, "a", "b", "c", event);'],
[['a', 'b', 'c', {raw: 0}], ' B.call ({"from": id}, "a", "b", "c", {"raw": 0});'],
[['a', 'b', 'c', {raw: ['not', 'a', 'string']}], ' B.call ({"from": id}, "a", "b", "c", {"raw": ["not", "a", "string"]});'],
[['a', 'b', 'c', {raw: 'event', ignored: 'field'}], ' B.call ({"from": id}, "a", "b", "c", event);'],
[['a', 'b', '"two', {raw: 'event'}, {raw: 'this'}], ' B.call ({"from": id}, "a", "b", "\\"two", event, this);'],
[[['a', 'b'], ['c', 'd']], ' B.call ({"from": id}, "a", "b", ' + defaultValue + '); B.call ({"from": id}, "c", "d", ' + defaultValue + ');', true],
[[['a', 'b'], [], ['c', 'd']], ' B.call ({"from": id}, "a", "b", ' + defaultValue + '); B.call ({"from": id}, "c", "d", ' + defaultValue + ');', true],
[[['a', 'b'], ['c', 'd', {raw: 'event.preventDefault'}]], ' B.call ({"from": id}, "a", "b", ' + defaultValue + '); B.call ({"from": id}, "c", "d", event.preventDefault);', true]
], undefined, function (v, k) {
var result = 'var id = B.call ("ev", event ? event.type : "undefined event", B.evh (this));' + v [1];
if (B.ev.apply (null, v [0]) !== result) return 'B.ev error with valid arguments #' + (k + 1) + ': ' + B.ev.apply (null, v [0]) + ' Expected: ' + result;
});
if (error) return error;
});
Test ('B.ev + logs', function () {
c.fill ('body', lith.g ([
['input', {'class': 'noclass', onchange: B.ev ('verb', 'path')}],
]));
c ('.noclass') [0].value = 'hello';
c.fire ('.noclass', 'change');
var logs = [last (B.log, 2), last (B.log)];
// Compatibility: IE<=8 adds other attributes to args, so we filter them out.
logs [0].args = dale.go (logs [0].args, function (v) {
return dale.obj (v, function (v2, k2) {
if (k2 === 'class') return [k2, v2];
});
});
if (! eq (logs [0], {t: logs [0].t, id: logs [0].id, from: undefined, verb: 'ev', path: ['change'], 'args': [{'class': 'noclass'}]})) return 'B.ev error: invalid logs #1.';
if (! eq (logs [1], {t: logs [1].t, id: logs [1].id, from: logs [0].id, verb: 'verb', path: ['path'], args: ['hello']})) return 'B.ev error: invalid logs #2.';
});
B.call (x, 'test', 'B.mount|unmount');
});
// *** B.MOUNT|UNMOUNT ***
B.respond ('test', 'B.mount|unmount', {burn: true}, function (x) {
Test ('B.validateLith', function () {
var error;
error = dale.stopNot ([
/a/,
['p', 222, 222],
['p', {}, 2, 3],
['p', {'class': NaN}],
['p', NaN],
[NaN],
['style', ['not', 'a', 'litc']],
['style', ['ok', {}, ['not', 'a', 'litc']]],
['style', [
['not', 'a', 'litc']
]],
['style', [
['ok', {}],
['not', 'a', 'litc']
]],
['style', [
['ok', {}, ['not', 'a', 'litc']]
]],
[1, 2, /bam/],
[1, 2, [/bam/], 4],
['div', [
['style', [2]],
['p', 'something']
]],
['div', [
['p', 'something'],
['style', [
['ok', {}],
['not', 'a', 'litc']
]]
]],
['div', [
['p', 'something'],
['style', [
['ok', {}],
['ok', {}, ['not', 'a', 'litc']]
]]
]],
['div', [
['LITERAL', ['nope']]
]],
['div', [
['style', [
['ok', {}],
['LITERAL', ['nope']]
]]
]]
], undefined, function (v) {
var validation = B.validateLith (v);
if (validation === 'Lith' || validation === 'Lithbag') return 'B.validateLith error with invalid input: ' + JSON.stringify (v) + ' ' + validation;
});
if (error) return error;
error = dale.stopNot ([
[true, ['br']],
[true, ['p', {id: 'p3', 'class': 'remark'}, 'This is a remark']],
[true, ['p', ['something']]],
[true, ['div', {id: 'container'}, ['p', {'class': 'remark'}, 'This is a remark']]],
[true, ['table', dale.go ([['A1', 'B1'], ['A2', 'B2']], function (v, k) {
return ['tr', {id: 'row' + (k + 1)}, dale.go (v, function (v2) {
return ['td', v2];
})];
})]],
[true, ['div', [
['p'], ['p']
]]],
[true, ['p', [
'Hola!', ['br']
]]],
[false, [
['p'], ['div']
]],
[false, [
['p'], [
['div']
]
]],
[false, ['c', 'c']],
[false, [1, 2, ['bam'], 4]],
[true, (function () {
var dataset = [{id: 1, name: 'a'}, {id: 2, name: 'b'}];
function createRows (data) {
var output = [];
dale.go (data, function (v) {
output.push (['tr', [
['td', v.id],
['td', v.name],
]]);
});
return output;
}
var table = ['table', [
['tr', [
['th', 'Id'],
['th', 'Name']
]],
createRows (dataset)
]];
return table;
}) ()],
[true, ['div', [
['p', 'Hi'],
['LITERAL', '<p>Hello!</p>']
]]],
[true, ['script', {src: 'scripts.js', charset: 'utf-8'}]],
[true, ['style', ['span.action', {color: 'blue'}]]],
[true, ['style', lith.css.g (['span.action', {color: 'blue'}])]],
[true, ['style', ['a', ['a', {}, ['a']]]]]
], undefined, function (v) {
var validation = B.validateLith (v [1]);
if (v [0] && validation !== 'Lith') return 'B.validateLith error with valid input: ' + JSON.stringify (v [1]) + ' ' + validation;
if (! v [0] && validation !== 'Lithbag') return 'B.validateLith error with valid input: ' + JSON.stringify (v [1]) + ' ' + validation;
});
if (error) return error;
});
var f = function (a) {
return function () {return a}
}
Test ('B.mount & B.unmount invalid input', function () {
if (B.mount ('p') !== false) return 'B.mount accepted invalid target #1.';
if (B.mount ('#a p') !== false) return 'B.mount accepted invalid target #2.';
if (B.mount (22) !== false) return 'B.mount accepted invalid target #3.';
if (B.mount ({}) !== false) return 'B.mount accepted invalid target #4.';
if (B.mount ('body>p', f ([])) !== false) return 'B.mount accepted invalid target #5.';
if (B.mount ('body', f (/a/)) !== false) return 'B.mount accepted invalid elem #1.';
if (B.mount ('body', f ({})) !== false) return 'B.mount accepted invalid elem #2.';
if (B.mount ('body', f (null)) !== false) return 'B.mount accepted invalid elem #3.';
if (B.mount ('body', f (['a', 'c', 'd'])) !== false) return 'B.mount accepted invalid elem #4.';
if (B.mount ('body', ['div']) !== false) return 'B.mount accepted invalid elem #5.';
if (B.mount ('#nosuch', []) !== false) return 'B.mount accepted non-existing target.';
if (B.unmount ('p') !== false) return 'B.unmount accepted invalid target #1.';
if (B.unmount ('#a p') !== false) return 'B.unmount accepted invalid target #2.';
if (B.unmount (22) !== false) return 'B.unmount accepted invalid target #3.';
if (B.unmount ({}) !== false) return 'B.unmount accepted invalid target #4.';
if (B.unmount ('body>p') !== false) return 'B.unmount accepted invalid target #5.';
if (B.unmount ('#nosuch', []) !== false) return 'B.unmount accepted non-existing target.';
});
Test ('B.mount & B.unmount valid', function () {
c.empty ('body');
B.mount ('body', function () {
return ['div', {id: 'first'}, 'a'];
});
if (! c ('body div#second')) return 'B.mount didn\'t place element.';
B.mount ('body', function () {
return ['div', {id: 'second'}, 'b'];
});
if (! eq (c.get ('div', 'id'), [{id: 'first'}, {id: 'second'}])) return 'B.mount didn\'t put element at the bottom of the target.';
B.mount ('#first', function () {
return ['div', {id: 'third'}, 'c'];
});
if ([
'<div id="first">a<div id="third">c</div></div><div id="second">b</div>',
// Compatibility: IE<=8 removes the quotes of id, uppercases the tags and adds \r\n
'<DIV id=first>a\r\n<DIV id=third>c</DIV></DIV>\r\n<DIV id=second>b</DIV>'
].indexOf (document.body.innerHTML) === -1) return 'B.mount didn\'t mount element inside element.';
c.empty ('#first');
B.mount ('#first', f (['lith', 'bag']));
if (c ('#first').innerHTML !== 'lithbag') return 'B.mount didn\'t mount lithbag.';
B.unmount ('body');
if (document.body.innerHTML !== '') return 'B.unmount didn\'t clear target.';
B.respond ('a', 'b', {id: B.B + 1}, function () {});
B.respond ('a', 'b', {id: B.B + 2}, function () {});
B.mount ('body', function () {
return ['div', {id: B.B + 1}, 'id="' + B.B + 2 + '"'];
});
B.unmount ('body');
if (B.responders [B.B + 1]) return 'B.unmount didn\'t remove event responder that corresponded to an element id.';
if (! B.responders [B.B + 2]) return 'B.unmount removed responder that didn\'t correspond to an element id.';
B.forget (B.B + 2);
if (B.mount ('body', function () {}) !== undefined) return 'B.mount didn\'t accept empty element.';
});
B.call (x, 'ok', 'ok');
B.call (x, 'test', 'B.prediff');
});
// *** B.PREDIFF ***
B.respond ('test', 'B.prediff', {burn: true}, function (x) {
Test ('B.prediff', function () {
var error = dale.stopNot ([
[['a'], ['O a', 'C a']],
[['a', {}], ['O a {}', 'C a']],
[['table'], B.internal.olderIE ? ['O table', 'O tbody', 'C tbody', 'C table'] : ['O table', 'C table']],
[['table', ['tr']], ['O table', 'O tbody', 'O tr', 'C tr', 'C tbody', 'C table']],
[['table', ['tr', ['td', ['table', ['tr']]]]], ['O table', 'O tbody', 'O tr', 'O td', 'O table', 'O tbody', 'O tr', 'C tr', 'C tbody', 'C table', 'C td', 'C tr', 'C tbody', 'C table']],
[['table', ['thead', ['tr']]], B.internal.olderIE ? ['O table', 'O thead', 'O tr', 'C tr', 'C thead', 'O tbody', 'C tbody', 'C table'] : ['O table', 'O thead', 'O tr', 'C tr', 'C thead', 'C table']],
[['table', [['thead', ['tr']], ['tr']]], ['O table', 'O thead', 'O tr', 'C tr', 'C thead', 'O tbody', 'O tr', 'C tr', 'C tbody', 'C table']],
[['a', undefined], ['O a', 'C a']],
[['a', ''], ['O a', 'C a']],
[['a', ['']], ['O a', 'C a']],
[['a', 'a'], ['O a', 'L a', 'C a']],
[['a', ['z', '']], ['O a', 'L z', 'C a']],
[['a', ['z', 'b']], ['O a', 'L zb', 'C a']],
[['a', ['z', ' b']], ['O a', 'L z b', 'C a']],
[['a', [1, 'z', ' b']], ['O a', 'L 1z b', 'C a']],
[['a', [[[['z']]], 'b']], ['O a', 'L zb', 'C a']],
[['a', {b: 'c'}], ['O a {"b":"c"}', 'C a']],
[['a', {b: null}], ['O a {}', 'C a']],
[['a', {b: false}], ['O a {}', 'C a']],
[['a', {b: ''}], ['O a {}', 'C a']],
[['a', {a: '', b: 'c', d: null, e: false, f: 'g'}], ['O a {"b":"c","f":"g"}', 'C a']],
[['a', {b: ''}], ['O a {}', 'C a']],
[['a', {b: undefined}], ['O a {}', 'C a']],
[['a', {b: 0}], ['O a {"b":0}', 'C a']],
[['a', {b: 'c'}, 'd'], ['O a {"b":"c"}', 'L d', 'C a']],
[['a', {b: 'c'}, ['d', 'e']], ['O a {"b":"c"}', 'L de', 'C a']],
[['a', ['LITERAL', '<svg></svg>']], ['O a', 'L <svg></svg>', 'C a']],
[['a', {opaque: true}, ['LITERAL', '<svg></svg>']], ['P 19 a {"opaque":true} <svg></svg>']],
[['a', {opaque: true}, '<svg></svg>'], ['P 19 a {"opaque":true} <svg></svg>']],
[['a', '<svg></svg>'], ['O a', 'L <svg></svg>', 'C a']],
[['div', [['a', {href: 'c'}], ['p', 2]]], ['O div', 'O a {"href":"c"}', 'C a', 'O p', 'L 2', 'C p', 'C div']],
[['style', ['div', {margin: 0}]], ['O style', 'L div{margin:0;}', 'C style']]
], undefined, function (v, k) {
if (! eq (B.prediff (v [0]), v [1])) return 'B.prediff mismatch: ' + (k + 1) + ' ' + JSON.stringify (v) + ' ' + B.prediff (v [0]);
});
if (error) return error;
B.responders [B.B + 1000] = {elem: ['a', {id: B.B + 1000}, 'z']};
if (
! eq (B.prediff (['a', {id: B.B + 1000}, 'u']), ['O a {"id":"' + B.B + 1000 + '"}', 'L u', 'C a']) &&
// Compatibility: We use `\u0432` instead of `B.B` because of IE8 (but not IE6-7!)
! eq (B.prediff (['a', {id: B.B + 1000}, 'u']), ['O a {"id":"\\u0432' + 1000 + '"}', 'L u', 'C a'])
) return 'Mismatch when prediffing up top-level reactive view: ' + B.prediff (['a', {id: B.B + 1000}, 'u']);
if (
! eq (['O div', 'O a {"id":"' + B.B + 1000 + '"}', 'L z', 'C a', 'C div'], B.prediff (['div', ['a', {id: B.B + 1000}, 'u']])) &&
// Compatibility: We use `\u0432` instead of `B.B` because of IE8 (but not IE6-7!)
! eq (['O div', 'O a {"id":"\\u0432' + 1000 + '"}', 'L z', 'C a', 'C div'], B.prediff (['div', ['a', {id: B.B + 1000}, 'u']]))
) return 'Mismatch when looking up nested reactive view.';
B.forget (B.B + 1000);
});
B.call (x, 'test', 'B.view');
});
// *** B.VIEW ***
B.respond ('test', 'B.view', {burn: true}, function (x) {
Test ('B.redraw', function () {
var noElement = B.redraw ({}, 'nosuch');
var lastLog = last (B.log);
if (noElement !== false || ! eq ({verb: 'error', path: ['B.redraw'], error: 'Attempt to redraw dangling element.'}, {verb: lastLog.verb, path: lastLog.path, error: lastLog.args [0]})) return 'No such element error wasn\'t reported properly.';
if (B.internal.redrawing !== true) return 'B.redraw didn\'t set redrawing flag #1.';
B.internal.redrawing = false;
var el = document.createElement ('div');
el.id = 'abc';
var danglingElement = B.redraw ({}, 'abc');
lastLog = last (B.log);
if (noElement !== false || ! eq ({verb: 'error', path: ['B.redraw'], error: 'Attempt to redraw dangling element.'}, {verb: lastLog.verb, path: lastLog.path, error: lastLog.args [0]})) return 'Dangling element error wasn\'t reported properly.';
if (B.internal.redrawing !== true) return 'B.redraw didn\'t set redrawing flag #2.';
B.internal.redrawing = false;
// Error reporting of B.applyDiff errors is not tested here.
});
var logLength = B.log.length;
Test ('B.view invalid inputs', function () {
var error = dale.stopNot ([
{path: undefined, fun: function () {}},
{path: / /, fun: function () {}},
{path: function () {}, fun: function () {}},
{path: [null], fun: function () {}},
{path: 'a', fun: undefined},
{path: 'a', fun: / /},
{path: 'a', fun: null},
{path: 'a', fun: ''},
{path: 'a', fun: []}
], undefined, function (args, k) {
if (B.view (args.path, args.fun) !== false) return 'B.view - invalid arguments accepted as valid: ' + B.str (args);
if (B.log.length !== logLength + k + 1) return 'Error not reported: ' + B.str (args);
var lastLog = last (B.log);
if (lastLog.verb !== 'error' || lastLog.path [0] !== 'B.view' || ! lastLog.args.length || type (lastLog.args [0]) !== 'string') return 'Error reported incorrectly: ' + B.str (args);
if (! lastLog.args [0].match (k <= 3 ? 'Invalid path' : 'Validation error:')) return 'Invalid error message: ' + B.str (args);
if (k > 3 && ! lastLog.args [1].match ('fun passed to B.view should')) return 'Invalid error message: ' + B.str (args);
});
if (error) return error;
if (B.internal.count !== 1) return 'Invalid invocation to B.view incremented B.internal.count.';
});
logLength = B.log.length;
Test ('B.view invalid vfun outputs', function () {
var error = dale.stopNot ([
{path: [1], fun: function () {}},
{path: [2], fun: function () {return 'a'}},
{path: [3], fun: function () {return [377, 'a']}},
{path: [4], fun: function () {return /boo/}},
{path: [5], fun: function () {return null}},
{path: [6], fun: function () {return ['div.green', {color: 'green'}]}},
{path: [7], fun: function () {return ['div', {id: 'a'}]}}
], undefined, function (args, k) {
if (B.view (args.path, args.fun) !== false) return 'B.view - invalid arguments accepted as valid: ' + B.str (args);
if (B.log.length !== logLength + k + 1) return 'Error not reported: ' + B.str (args);
var lastLog = last (B.log);
if (lastLog.verb !== 'error' || lastLog.path [0] !== 'B.view' || ! lastLog.args.length || type (lastLog.args [0]) !== 'string') return 'Error reported incorrectly: ' + B.str (args);
if (! lastLog.args [0].match (/View function at path/)) return 'Invalid error message: ' + B.str (args);
if (! lastLog.args [2].match (/must return a lith element/)) return 'Invalid error message: ' + B.str (args);
if (! B.responders [B.B + (k + 1)]) return 'Responder not set: ' + B.str (args);
if (B.responders [B.B + (k + 1)].path.length !== 0) return 'Responder not set with empty path.';
B.forget (B.B + (k + 1));
});
if (error) return error;
B.internal.count = 1;
});
// Turn on again error handler
B.responders.error.disabled = false;
Test ('B.view valid inputs, non-nested', function () {
// INITIAL SETUP
B.call (x, 'set', ['Data', 'view'], ['div']);
B.mount ('body', function () {
return B.view (['Data', 'view'], function (view) {
return view;
});
});
if ([
'<div id="в1" path="Data:view"></div>',
// Compatibility: IE<=8 removes the quotes around id and uppercases the tag name
'<DIV id=в1 path="Data:view"></DIV>'
].indexOf (document.body.innerHTML) === -1) return 'Invalid HTML mounted #1: ' + document.body.innerHTML;
var responder = B.responders [B.B + 1];
if (responder.verb !== 'change') return 'Invalid change #1.';
if (responder.path.length !== 0) return 'Invalid path #1.';
if (responder.priority !== -1) return 'Invalid priority #1.';
if (eq (responder.elem, B.get ('Data', 'view'))) return 'Store was modified by vfun #1.';
if (! eq (responder.elem, ['div', {id: B.B + 1, path: 'Data:view'}])) return 'Invalid elem #1.';
if (responder.children.length !== 0) return 'Invalid children #1.';
var DOMElement = c ('#' + B.B + 1);
if (c.get ('#' + B.B + 1, 'path').path !== 'Data:view') return 'Invalid path in DOM #1.';
// ADD CLASS
B.call (x, 'set', ['Data', 'view'], ['div', {'class': 'hello'}]);
if (c ('#' + B.B + 1) !== DOMElement) return 'DOM element not recycled #2.';
if (c.get ('#' + B.B + 1, 'path').path !== 'Data:view') return 'Invalid path in DOM #2.';
if (c.get ('.hello').length !== 1) return 'Class not applied #2.';
if ([
'<div id="в1" path="Data:view" class="hello"></div>',
// Compatibility: IE10-11, FF<=48 put the class first
'<div class="hello" id="в1" path="Data:view"></div>',
// Compatibility: IE9 puts the class in the middle
'<div id="в1" class="hello" path="Data:view"></div>',
// Compatibility: IE<=8 puts the class in the middle, removes the quotes around id & class and uppercases the tag name
'<DIV id=в1 class=hello path="Data:view"></DIV>',
// Compatibility: IE<=7 puts the class at the beginning
'<DIV class=hello id=в1 path="Data:view"></DIV>'
].indexOf (document.body.innerHTML) === -1) return 'Invalid HTML mounted #2: ' + document.body.innerHTML;
var llog = teishi.last (B.log);
if (! eq ({verb: llog.verb, path: llog.path, rid: llog.args [0].responder}, {verb: 'redraw', path: ['Data', 'view'], rid: B.B + 1})) return 'Invalid redraw log #1.';
// REMOVE CLASS
B.call (x, 'set', ['Data', 'view'], ['div']);
if (c ('#' + B.B + 1) !== DOMElement) return 'DOM element not recycled #3.';
if (c.get ('.hello').length !== 0) return 'Class not removed #3.';
if ([
'<div id="в1" path="Data:view"></div>',
// Compatibility: IE<=8 removes the quotes around id and uppercases the tag name
'<DIV id=в1 path="Data:view"></DIV>'
].indexOf (document.body.innerHTML) === -1) return 'Invalid HTML mounted #3: ' + document.body.innerHTML;
// ADD CLASS AGAIN
B.call (x, 'set', ['Data', 'view'], ['div', {'class': 'вla'}]);
if (c.get ('.вla').length !== 1) return 'Class not added #4.';
if ([
'<div id="в1" path="Data:view" class="вla"></div>',
// Compatibility: IE10-11, FF<=48 put the class first
'<div class="вla" id="в1" path="Data:view"></div>',
// Compatibility: IE9 puts the class in the middle
'<div id="в1" class="вla" path="Data:view"></div>',
// Compatibility: IE<=8 removes the quotes around id & class and uppercases the tag name
'<DIV id=в1 class=вla path="Data:view"></DIV>',
// Compatibility: IE<=7 puts the class at the beginning
'<DIV class=вla id=в1 path="Data:view"></DIV>'
].indexOf (document.body.innerHTML) === -1) return 'Invalid HTML mounted #4: ' + document.body.innerHTML;
// REMOVE CLASS WITH null
B.call (x, 'set', ['Data', 'view'], ['div', {'class': null}]);
if (c.get ('.вla').length !== 0) return 'Class not removed #5.';
if ([
'<div id="в1" path="Data:view"></div>',
// Compatibility: IE<=8 removes the quotes around id and uppercases the tag name
'<DIV id=в1 path="Data:view"></DIV>'
].indexOf (document.body.innerHTML) === -1) return 'Invalid HTML mounted #5: ' + document.body.innerHTML;
// ADD CLASS AGAIN AND REMOVE WITH ''
B.call (x, 'set', ['Data', 'view'], ['div', {'class': 'foo'}]);