forked from GSattybay/carbone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.builder.js
1926 lines (1900 loc) · 90.1 KB
/
test.builder.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
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
var assert = require('assert');
var carbone = require('../lib/index');
var input = require('../lib/input');
var builder = require('../lib/builder');
var helper = require('../lib/helper');
describe('builder', function () {
describe('getFormatterString' , function () {
var _testedFormatters = {
int : function () {},
toFixed : function () {},
format : function () {},
formatter : function () {},
formatter1 : function () {},
formatter2 : function () {},
print : function () {},
convCRLF : function () {}
};
let _safeAccessor = null;
let _getSafeValue = null;
beforeEach(function () {
_safeAccessor = builder.generateSafeJSValueAccessor('dictionary');
_getSafeValue = _safeAccessor.get;
});
it('should return an empty string if there is no formatter', function () {
var _actual = builder.getFormatterString(_getSafeValue, '_str', 'context', []);
helper.assert(_actual, '');
});
it('should return a simple call of a function for a formatter without arguments', function () {
var _actual = builder.getFormatterString(_getSafeValue, '_str', 'context', [ 'int' ], _testedFormatters);
helper.assert(_actual, '_str = formatters.int.call(context, _str);\n');
helper.assert(_safeAccessor.getDictionary(), []);
});
it('should return a simple call of a function for a formatter without arguments but called with parenthesis', function () {
var _actual = builder.getFormatterString(_getSafeValue, '_otherString', '_meta', [ 'int()' ], _testedFormatters);
helper.assert(_actual, '_otherString = formatters.int.call(_meta, _otherString);\n');
helper.assert(_safeAccessor.getDictionary(), []);
});
it('should return a call of a function for a formatter with one argument', function () {
var _actual = builder.getFormatterString(_getSafeValue, '_str', '_options', [ 'toFixed(2)' ], _testedFormatters);
helper.assert(_actual, '_str = formatters.toFixed.call(_options, _str, dictionary[0]);\n');
helper.assert(_safeAccessor.getDictionary(), ['2']);
});
it('should return a call of a function for a formatter with one argument which is a string', function () {
builder.getFormatterString(_getSafeValue, '_str', '_options', [ 'format(YYYYMMDD)' ], _testedFormatters);
helper.assert(_safeAccessor.getDictionary(), ['YYYYMMDD']);
});
it('should keep whitespaces if it is a string', function () {
builder.getFormatterString(_getSafeValue, '_str', '_options', [ "format('YYYY MM DD')" ], _testedFormatters);
helper.assert(_safeAccessor.getDictionary(), ['YYYY MM DD']);
});
it('should keep anti-slash quotes', function () {
builder.getFormatterString(_getSafeValue, '_str', '_options', [ "format('YYYY \\' MM DD')" ], _testedFormatters);
helper.assert(_safeAccessor.getDictionary(), ["YYYY \\' MM DD"]);
});
it('should keep parenthesis in the string', function () {
builder.getFormatterString(_getSafeValue, '_str', '_options', [ "format('(YYYY) ' (MM) DD')" ], _testedFormatters);
helper.assert(_safeAccessor.getDictionary(), ["(YYYY) ' (MM) DD"]);
});
it('should return a call of a function for a formatter with two arguments', function () {
builder.getFormatterString(_getSafeValue, '_str', '_options', [ 'formatter(2, 3)' ], _testedFormatters);
helper.assert(_safeAccessor.getDictionary(), ['2', '3']);
});
it('should remove extra whitespaces between arguments', function () {
builder.getFormatterString(_getSafeValue, '_str', '_options', [ 'formatter( 2 , 3 )' ], _testedFormatters);
helper.assert(_safeAccessor.getDictionary(), ['2', '3']);
});
it('should return two calls of functions for two chained formatters', function () {
var _actual = builder.getFormatterString(_getSafeValue, '_str', '_options', [ 'int', 'toFixed(2)' ], _testedFormatters);
// helper.assert(_actual, '_str = formatters.toFixed.call(_options, formatters.int(d.number), \'2\');');
helper.assert(_actual, '_str = formatters.int.call(_options, _str);\n'
+'if(_options.stopPropagation === false){\n'
+ '_str = formatters.toFixed.call(_options, _str, dictionary[0]);\n'
+'}');
helper.assert(_safeAccessor.getDictionary(), ['2']);
});
it('should return two calls of functions for two chained formatters each with arguments', function () {
builder.getFormatterString(_getSafeValue, '_str', '_options', [ 'formatter1(4, 5)', 'formatter2(2, 3)' ], _testedFormatters);
helper.assert(_safeAccessor.getDictionary(), ['4', '5', '2', '3']);
});
it('should return three calls of functions for three chained formatters each with arguments', function () {
var _actual = builder.getFormatterString(_getSafeValue, '_str', '_options', [ 'formatter1(4, 5)', 'formatter2(2, 3)', 'print(\'ok\')' ], _testedFormatters);
assert.equal(_actual, '_str = formatters.formatter1.call(_options, _str, dictionary[0], dictionary[1]);\n'
+'if(_options.stopPropagation === false){\n'
+ '_str = formatters.formatter2.call(_options, _str, dictionary[2], dictionary[3]);\n'
+ 'if(_options.stopPropagation === false){\n'
+ '_str = formatters.print.call(_options, _str, dictionary[4]);\n'
+ '}'
+'}');
helper.assert(_safeAccessor.getDictionary(), ['4', '5', '2', '3', 'ok']);
});
it('should return formatter code according to the filter "canInjectXML"', function () {
_testedFormatters.convCRLF.canInjectXML = true;
var _actual = builder.getFormatterString(_getSafeValue, '_str', '_options', [ 'convCRLF()', 'formatter1(4)' ], _testedFormatters);
helper.assert(_actual, '_str = formatters.formatter1.call(_options, _str, dictionary[0]);\n');
_actual = builder.getFormatterString(_getSafeValue, '_str', '_options', [ 'convCRLF()', 'formatter1(4)' ], _testedFormatters, true);
helper.assert(_actual, '_str = formatters.convCRLF.call(_options, _str);\n');
});
});
describe('generateSafeJSValueAccessor', function () {
it('should update an array, generate JS code to access this value, return its position in the dictionary, and return the dictionary', function () {
var _safeValue = builder.generateSafeJSValueAccessor('dictionary');
helper.assert(_safeValue.get('<xml>' ), 'dictionary[0]');
helper.assert(_safeValue.getIndex('<xml>' ), 0);
// should work even if getIndex is called first
helper.assert(_safeValue.getIndex('<b></b>'), 1);
helper.assert(_safeValue.get('<b></b>'), 'dictionary[1]');
helper.assert(_safeValue.getIndex('<a>' ), 2);
helper.assert(_safeValue.getIndex(';\\\'' ), 3);
// should return the same index if the string was already registred
helper.assert(_safeValue.getIndex('<b></b>'), 1);
helper.assert(_safeValue.get('<b></b>'), 'dictionary[1]');
helper.assert(_safeValue.getDictionary(), ['<xml>', '<b></b>', '<a>', ';\\\'']);
// should reset the dictionary
var _safeValue2 = builder.generateSafeJSValueAccessor('dictionary');
helper.assert(_safeValue2.getDictionary(), []);
});
it('should return separate values (one for each instance) even if arrays are the same', function () {
var _safeValue = builder.generateSafeJSValueAccessor('dictionary');
var _array1 = [1, 2];
var _array2 = [1, 2];
helper.assert(_safeValue.get(_array1), 'dictionary[0]');
helper.assert(_safeValue.get(_array2), 'dictionary[1]');
helper.assert(_safeValue.get(_array1), 'dictionary[0]');
});
});
describe('generateSafeJSVariable', function () {
it('should generate safe JS variable', function () {
var _safeVar = builder.generateSafeJSVariable();
helper.assert(_safeVar('<xml>' ), '_gV0');
helper.assert(_safeVar('<b></b>'), '_gV1');
helper.assert(_safeVar('<a>' ), '_gV2');
helper.assert(_safeVar('<a>' ), '_gV2');
helper.assert(_safeVar('<xml>' ), '_gV0');
});
});
describe('getFilterString', function () {
let _safeAccessor = null;
let _getSafeValue = null;
let _getSafeVar = null;
beforeEach(function () {
_safeAccessor = builder.generateSafeJSValueAccessor('dictionary');
_getSafeVar = builder.generateSafeJSVariable();
_getSafeValue = _safeAccessor.get;
});
it('should return an empty string if code is empty, attr is empty, the conditions array is not defined', function () {
var _actual = builder.getFilterString(_getSafeVar, _getSafeValue, [{left : {parent : 'myObj',attr : 'sort'}, operator : '>', right : '10'}]);
assert.equal(_actual, '');
_actual = builder.getFilterString(_getSafeVar, _getSafeValue, []);
assert.equal(_actual, '');
_actual = builder.getFilterString(_getSafeVar, _getSafeValue);
assert.equal(_actual, '');
_actual = builder.getFilterString(_getSafeVar, _getSafeValue, 'd', 'code');
assert.equal(_actual, '');
});
it('should return a string which contain an "if-condition"', function () {
var _conditions = [
{left : {parent : 'myObj',attr : 'sort'}, operator : '>', right : '10'}
];
var _actual = builder.getFilterString(_getSafeVar, _getSafeValue, _conditions, 'code');
assert.equal(_actual, 'if((_gV0 && _gV0[dictionary[0]]>dictionary[1])){\n code;\n }');
helper.assert(_safeAccessor.getDictionary(), ['sort', '10']);
});
it('should accept multiple conditions', function () {
var _conditions = [
{left : {parent : 'myObj',attr : 'sort'}, operator : '>', right : '10'},
{left : {parent : 'myObj',attr : 'id'}, operator : '<', right : '15'}
];
var _actual = builder.getFilterString(_getSafeVar, _getSafeValue, _conditions, 'code');
assert.equal(_actual, 'if((_gV0 && _gV0[dictionary[0]]>dictionary[1] && _gV0[dictionary[2]]<dictionary[3])){\n code;\n }');
helper.assert(_safeAccessor.getDictionary(), ['sort', '10', 'id', '15']);
});
it('should accept than the left part of the condition is in an object', function () {
var _conditions = [
{left : {parent : 'myObj',attr : 'menu.sort'}, operator : '>', right : '10'},
];
var _actual = builder.getFilterString(_getSafeVar, _getSafeValue, _conditions, 'code');
assert.equal(_actual, 'var _gV0=_gV1[dictionary[0]];\nif((_gV0 && _gV0[dictionary[1]]>dictionary[2])){\n code;\n }');
helper.assert(_safeAccessor.getDictionary(), ['menu', 'sort', '10']);
});
it('should add a prefix on the declared variable', function () {
var _conditions = [
{left : {parent : 'myObj',attr : 'menu.sort'}, operator : '>', right : '10'},
];
var _actual = builder.getFilterString(_getSafeVar, _getSafeValue, _conditions, 'code', 'prefix');
assert.equal(_actual, 'var _gV0=_gV1[dictionary[0]];\nif((_gV0 && _gV0[dictionary[1]]>dictionary[2])){\n code;\n }');
});
it('should handle the reserved index iterator "i"', function () {
var _conditions = [
{left : {parent : 'myObj',attr : 'i'}, operator : '>', right : '10'},
];
var _actual = builder.getFilterString(_getSafeVar, _getSafeValue, _conditions, 'code', 'prefix');
assert.equal(_actual, 'if((_gV0_i >10)){\n code;\n }');
});
it('should handle the reserved index iterator "i" and negative values', function () {
var _conditions = [
{left : {parent : 'myObj',attr : 'i'}, operator : '=', right : '-10'},
];
var _actual = builder.getFilterString(_getSafeVar, _getSafeValue, _conditions, 'code', 'prefix');
assert.equal(_actual, 'if((_gV0_i =_gV0_array_length -10)){\n code;\n }');
});
it('should not inject JS if the index contains weird characters (negative index)', function () {
var _conditions = [
{left : {parent : 'myObj',attr : 'i'}, operator : '=', right : '-10;console.log'},
];
var _actual = builder.getFilterString(_getSafeVar, _getSafeValue, _conditions, 'code', 'prefix');
assert.equal(_actual, 'if((_gV0_i =_gV0_array_length -10)){\n code;\n }');
});
it('should not inject JS if the index contains weird characters (not int)', function () {
var _conditions = [
{left : {parent : 'myObj',attr : 'i'}, operator : '=', right : ';-10;console.log'},
];
var _actual = builder.getFilterString(_getSafeVar, _getSafeValue, _conditions, 'code', 'prefix');
assert.equal(_actual, 'if((_gV0_i =NaN)){\n code;\n }');
});
it('should not inject JS if the index contains weird characters (positive index)', function () {
var _conditions = [
{left : {parent : 'myObj',attr : 'i'}, operator : '=', right : '10;console.log'},
];
var _actual = builder.getFilterString(_getSafeVar, _getSafeValue, _conditions, 'code', 'prefix');
assert.equal(_actual, 'if((_gV0_i =10)){\n code;\n }');
});
it('should not declare the same variable twice if there are two conditions on the same variable', function () {
var _conditions = [
{left : {parent : 'myObj',attr : 'menu.sort'}, operator : '>', right : '10'},
{left : {parent : 'myObj',attr : 'menu.sort'}, operator : '<', right : '20'},
];
var _actual = builder.getFilterString(_getSafeVar, _getSafeValue, _conditions, 'code');
assert.equal(_actual, 'var _gV0=_gV1[dictionary[0]];\nif((_gV0 && _gV0[dictionary[1]]>dictionary[2] && _gV0[dictionary[1]]<dictionary[3])){\n code;\n }');
helper.assert(_safeAccessor.getDictionary(), ['menu', 'sort', '10', '20']);
});
it('should inverse the condition', function () {
var _conditions = [
{left : {parent : 'myObj',attr : 'menu.sort'}, operator : '>', right : '10'},
{left : {parent : 'myObj',attr : 'menu.sort'}, operator : '<', right : '20'},
];
var _actual = builder.getFilterString(_getSafeVar, _getSafeValue, _conditions, 'code', '', true);
assert.equal(_actual, 'var _gV0=_gV1[dictionary[0]];\nif(!(_gV0 && _gV0[dictionary[1]]>dictionary[2] && _gV0[dictionary[1]]<dictionary[3])){\n code;\n }');
});
it('should accept multiple conditions nested in an object', function () {
var _conditions = [
{left : {parent : 'myObj',attr : 'menu.sort'}, operator : '>', right : '10'},
{left : {parent : 'myObj',attr : 'car.sort'}, operator : '<', right : '20'},
];
var _actual = builder.getFilterString(_getSafeVar, _getSafeValue, _conditions, 'code');
assert.equal(_actual,
'var _gV0=_gV1[dictionary[0]];\n'+
'var _gV2=_gV1[dictionary[3]];\n'+
'if((_gV0 && _gV0[dictionary[1]]>dictionary[2] && _gV2 && _gV2[dictionary[1]]<dictionary[4])){\n'+
' code;\n }'
);
helper.assert(_safeAccessor.getDictionary(), ['menu', 'sort', '10', 'car', '20']);
});
});
describe('sortXmlParts', function () {
it('should sort the array with a depth of 1 by default', function () {
var _data = [{pos : [40]}, {pos : [19]}, {pos : [2 ]}, {pos : [20]}];
var _expected = [{pos : [2 ]}, {pos : [19]}, {pos : [20]}, {pos : [40]}];
builder.sortXmlParts(_data);
helper.assert(_data, _expected);
});
it('should sort the array even with strings', function () {
var _data = [{pos : ['zz']}, {pos : ['ab']}, {pos : ['a' ]}, {pos : ['ac']}];
var _expected = [{pos : ['a' ]}, {pos : ['ab']}, {pos : ['ac']}, {pos : ['zz']}];
builder.sortXmlParts(_data);
helper.assert(_data, _expected);
});
it('should sort the array with a depth of 2', function () {
var _data = [{pos : [40, 4]}, {pos : [40, 3]}, {pos : [51, 100]}, {pos : [29, 8 ]}];
var _expected = [{pos : [29, 8]}, {pos : [40, 3]}, {pos : [40, 4 ]}, {pos : [51, 100]}];
builder.sortXmlParts(_data);
helper.assert(_data, _expected);
});
it('should sort the array with a depth of 3', function () {
var _data = [{pos : [4, 4, 2]}, {pos : [4, 4, 1]}, {pos : [4, 3, 2]}, {pos : [1, 9, 1]}, {pos : [2, 5, 6]}, {pos : [1, 8, 9]}];
var _expected = [{pos : [1, 8, 9]}, {pos : [1, 9, 1]}, {pos : [2, 5, 6]}, {pos : [4, 3, 2]}, {pos : [4, 4, 1]}, {pos : [4, 4, 2]}];
builder.sortXmlParts(_data);
helper.assert(_data, _expected);
});
it('should sort the array even if some arrays are incomplete, undefined values appears first', function () {
var _data = [{pos : [4, 4, 2]}, {pos : [4, 4, 1]}, {pos : [2, 4 ]}, {pos : [1, 9, 1]}, {pos : [2, 3 ]}, {pos : [1 ]}];
var _expected = [{pos : [1 ]}, {pos : [1, 9, 1]}, {pos : [2, 3 ]}, {pos : [2, 4 ]}, {pos : [4, 4, 1]}, {pos : [4, 4, 2]}];
builder.sortXmlParts(_data);
helper.assert(_data, _expected);
});
it('should sort a complex array (sort depth of 3) of xml parts', function () {
var _data = [
{ pos : [ 6, 1, 14 ], str : 'Tesla motors' },
{ pos : [ 0 ], str : '<xml> ' },
{ pos : [ 6, 1, 6 ], str : '<tr>' },
{ pos : [ 6, 1, 23 ], str : '</tr>' },
{ pos : [ 6, 0, 14 ], str : 'Lumeneo' },
{ pos : [ 6, 0, 6 ], str : '<tr>' },
{ pos : [ 6, 0, 23 ], str : '</tr>' },
{ pos : [ 6, 2, 14 ], str : 'Toyota' },
{ pos : [ 6, 2, 6 ], str : '<tr>' },
{ pos : [ 6, 2, 23 ], str : '</tr>' },
{ pos : [ 24 ], str : '</xml>' }
];
var _expected = [
{ pos : [ 0 ], str : '<xml> ' },
{ pos : [ 6, 0, 6 ], str : '<tr>' },
{ pos : [ 6, 0, 14 ], str : 'Lumeneo' },
{ pos : [ 6, 0, 23 ], str : '</tr>' },
{ pos : [ 6, 1, 6 ], str : '<tr>' },
{ pos : [ 6, 1, 14 ], str : 'Tesla motors' },
{ pos : [ 6, 1, 23 ], str : '</tr>' },
{ pos : [ 6, 2, 6 ], str : '<tr>' },
{ pos : [ 6, 2, 14 ], str : 'Toyota' },
{ pos : [ 6, 2, 23 ], str : '</tr>' },
{ pos : [ 24 ], str : '</xml>' }
];
builder.sortXmlParts(_data);
helper.assert(_data, _expected);
});
it('should be fast to sort 1 Millons of rows', function () {
var _nbRows = 1000000;
var _data = [];
for (var i = 0; i < _nbRows; i++) {
_data.push({
pos : [i % 100, i % 50, i % 1000, i % 60]
});
}
var _start = process.hrtime();
builder.sortXmlParts(_data, 10);
var _diff = process.hrtime(_start);
var _elapsed = ((_diff[0] * 1e9 + _diff[1]) / 1e6);
console.log('\n sortXmlParts speed : ' + _elapsed + ' ms (usually around 800 ms)\n');
helper.assert(_elapsed < 2000, true);
});
it('should sort by rowShow first if "pos" are the same', function () {
var _data = [{pos : [4], rowShow : false}, {pos : [4], rowShow : false}, {pos : [4], rowShow : true }, {pos : [1], rowShow : false}];
var _expected = [{pos : [1], rowShow : false}, {pos : [4], rowShow : true }, {pos : [4], rowShow : false}, {pos : [4], rowShow : false}];
builder.sortXmlParts(_data);
helper.assert(_data, _expected);
});
/*
complex example, should we tets it?
[
{"pos": [0], "str": "<xml>" },
{"pos": [5, "david", 0, 5], "str": "<t_row>"},
{"pos": [5, "david", 0, 38], "str": "</t_row>"},
{"pos": [5, "david", 0, 12, "B", 0, 16 ], "str": "B"},
{"pos": [5, "david", 0, 12, "B", 0, 12 ], "str": "<td>"},
{"pos": [5, "david", 0, 12, "B", 0, 21 ], "str": "</td>"},
{"pos": [5, "david", 0, 12, "A", 1, 16 ], "str": "A"},
{"pos": [5, "david", 0, 12, "A", 1, 12 ], "str": "<td>"},
{"pos": [5, "david", 0, 12, "A", 1, 21 ], "str": "</td>"},
{"pos": [5, "bob", 1, 5 ], "str": "<t_row>"},
{"pos": [5, "bob", 1, 38 ], "str": "</t_row>"},
{"pos": [5, "bob", 1, 12, "D", 0, 16 ], "str": "D"},
{"pos": [5, "bob", 1, 12, "D", 0, 12 ], "str": "<td>"},
{"pos": [5, "bob", 1, 12, "D", 0, 21 ], "str": "</td>"},
{"pos": [5, "bob", 1, 12, "C", 1, 16 ], "str": "C"},
{"pos": [5, "bob", 1, 12, "C", 1, 12 ], "str": "<td>"},
{"pos": [5, "bob", 1, 12, "C", 1, 21 ], "str": "</td>"},
{"pos": [5, "bob", 1, 12, "E", 2, 16], "str": "E"},
{"pos": [5, "bob", 1, 12, "E", 2, 12], "str": "<td>"},
{"pos": [5, "bob", 1, 12, "E", 2, 21 ], "str": "</td>"},
{"pos": [39], "str": "</xml>"}
]
[
{"pos": [0], "str": "<xml>" },
{"pos": [5, "david", 0, 5], "str": "<t_row>"},
{"pos": [5, "david", 0, 12, "A", 1, 12 ], "str": "<td>"},
{"pos": [5, "david", 0, 12, "A", 1, 16 ], "str": "A"},
{"pos": [5, "david", 0, 12, "A", 1, 21 ], "str": "</td>"},
{"pos": [5, "david", 0, 12, "B", 0, 12 ], "str": "<td>"},
{"pos": [5, "david", 0, 12, "B", 0, 16 ], "str": "B"},
{"pos": [5, "david", 0, 12, "B", 0, 21 ], "str": "</td>"},
{"pos": [5, "david", 0, 38], "str": "</t_row>"},
{"pos": [5, "bob", 1, 5 ], "str": "<t_row>"},
{"pos": [5, "bob", 1, 12, "C", 1, 12 ], "str": "<td>"},
{"pos": [5, "bob", 1, 12, "C", 1, 16 ], "str": "C"},
{"pos": [5, "bob", 1, 12, "C", 1, 21 ], "str": "</td>"},
{"pos": [5, "bob", 1, 12, "D", 0, 12 ], "str": "<td>"},
{"pos": [5, "bob", 1, 12, "D", 0, 16 ], "str": "D"},
{"pos": [5, "bob", 1, 12, "D", 0, 21 ], "str": "</td>"},
{"pos": [5, "bob", 1, 12, "E", 2, 12 ], "str": "<td>"},
{"pos": [5, "bob", 1, 12, "E", 2, 16 ], "str": "E"},
{"pos": [5, "bob", 1, 12, "E", 2, 21 ], "str": "</td>"},
{"pos": [5, "bob", 1, 38 ], "str": "</t_row>"},
{"pos": [39], "str": "</xml>"}
]*/
});
describe('forEachArrayExit call a function for each array we are leaving', function () {
it('should never call the callback if _currentlyVisitedArrays is empty', function () {
var _currentlyVisitedArrays = [];
var _objDependencyDescriptor = {
d : {type : 'array', parent : ''},
cars : {type : 'array', parent : 'd'}
};
var _nextAttrName = '';
var _nbArrayExit = 0;
builder.forEachArrayExit(_currentlyVisitedArrays, _objDependencyDescriptor, _nextAttrName, function () {
_nbArrayExit++;
});
helper.assert(_nbArrayExit, 0);
_nextAttrName = '';
_nbArrayExit = 0;
builder.forEachArrayExit(_currentlyVisitedArrays, {}, _nextAttrName, function () {
_nbArrayExit++;
});
helper.assert(_nbArrayExit, 0);
_nextAttrName = 'cars';
_nbArrayExit = 0;
builder.forEachArrayExit(_currentlyVisitedArrays, _objDependencyDescriptor, _nextAttrName, function () {
_nbArrayExit++;
});
helper.assert(_nbArrayExit, 0);
});
it('should call the callback once if we are leaving the array "cars".\
It should return the name of the array we are leaving.\
It should remove the array name in _currentlyVisitedArrays', function () {
var _currentlyVisitedArrays = ['d', 'cars'];
var _objDependencyDescriptor = {
d : {type : 'array', parent : '' , depth : 1},
cars : {type : 'array', parent : 'd', depth : 2},
test : {type : 'array', parent : 'd', depth : 2}
};
var _nextAttrName = 'test';
var _nbArrayExit = 0;
builder.forEachArrayExit(_currentlyVisitedArrays, _objDependencyDescriptor, _nextAttrName, function (arrayLeft) {
helper.assert(arrayLeft, 'cars');
_nbArrayExit++;
});
helper.assert(_nbArrayExit, 1);
helper.assert(_currentlyVisitedArrays, ['d']);
});
it('should call the callback for each array we are leaving', function () {
var _currentlyVisitedArrays = ['d', 'cars', 'wheels'];
var _objDependencyDescriptor = {
d : {type : 'array', parent : '' , depth : 1},
cars : {type : 'array', parent : 'd' , depth : 2},
wheels : {type : 'array', parent : 'cars' , depth : 3},
site : {type : 'object', parent : 'd' }
};
var _nextAttrName = 'site';
var _nbArrayExit = 0;
builder.forEachArrayExit(_currentlyVisitedArrays, _objDependencyDescriptor, _nextAttrName, function (arrayLeft) {
var _leftArrays = ['wheels', 'cars'];
helper.assert(arrayLeft, _leftArrays[_nbArrayExit]);
_nbArrayExit++;
});
helper.assert(_nbArrayExit, 2);
helper.assert(_currentlyVisitedArrays, ['d']);
});
it('2. should call the callback for each array we are leaving', function () {
var _currentlyVisitedArrays = ['d', 'cars', 'wheels'];
var _objDependencyDescriptor = {
d : {type : 'array', parent : '' , depth : 1},
cars : {type : 'array', parent : 'd' , depth : 2},
wheels : {type : 'array', parent : 'cars' , depth : 3},
site : {type : 'object', parent : 'cars' }
};
var _nextAttrName = 'site';
var _nbArrayExit = 0;
builder.forEachArrayExit(_currentlyVisitedArrays, _objDependencyDescriptor, _nextAttrName, function (arrayLeft) {
var _leftArrays = ['wheels'];
helper.assert(arrayLeft, _leftArrays[_nbArrayExit]);
_nbArrayExit++;
});
helper.assert(_nbArrayExit, 1);
helper.assert(_currentlyVisitedArrays, ['d', 'cars']);
});
it('should works even if the arrays are nested in objects', function () {
var _currentlyVisitedArrays = ['d', 'cars', 'wheels'];
var _objDependencyDescriptor = {
d : {type : 'array' , parent : '' , depth : 1},
obj : {type : 'object', parent : 'd' },
cars : {type : 'array' , parent : 'obj' , depth : 2},
spec : {type : 'object', parent : 'cars' },
wheels : {type : 'array' , parent : 'spec' , depth : 3},
site : {type : 'object', parent : 'wheels' },
players : {type : 'array', parent : 'obj' , depth : 2}
};
var _nextAttrName = 'site';
var _nbArrayExit = 0;
builder.forEachArrayExit(_currentlyVisitedArrays, _objDependencyDescriptor, _nextAttrName, function () {
_nbArrayExit++;
});
helper.assert(_nbArrayExit, 0);
helper.assert(_currentlyVisitedArrays, ['d', 'cars', 'wheels']);
_nextAttrName = 'players';
_nbArrayExit = 0;
builder.forEachArrayExit(_currentlyVisitedArrays, _objDependencyDescriptor, _nextAttrName, function (arrayLeft) {
var _leftArrays = ['wheels', 'cars'];
helper.assert(arrayLeft, _leftArrays[_nbArrayExit]);
_nbArrayExit++;
});
helper.assert(_nbArrayExit, 2);
helper.assert(_currentlyVisitedArrays, ['d']);
});
it('should not leave the array "wheels" if the array "tyres" is nested in "wheels" in XML (depth >)', function () {
var _currentlyVisitedArrays = ['d', 'cars', 'wheels'];
var _objDependencyDescriptor = {
d : {type : 'array' , parent : '' , depth : 1 },
cars : {type : 'array' , parent : 'd' , depth : 2 },
wheels : {type : 'array' , parent : 'cars' , depth : 3 },
tyres : {type : 'array' , parent : 'cars' , depth : 4 },
site : {type : 'object', parent : 'cars' }
};
var _nextAttrName = 'tyres';
var _nbArrayExit = 0;
// eslint-disable-next-line no-unused-vars
builder.forEachArrayExit(_currentlyVisitedArrays, _objDependencyDescriptor, _nextAttrName, function (arrayLeft) {
_nbArrayExit++;
});
helper.assert(_nbArrayExit, 0);
helper.assert(_currentlyVisitedArrays, ['d', 'cars', 'wheels']);
});
it('should not leave the array "countries" if the array "movies" is nested in "countries" in XML (depth >)', function () {
var _currentlyVisitedArrays = ['countries', 'cars'];
var _objDependencyDescriptor = {
d : {type : 'object' , parent : '' },
countries : {type : 'array' , parent : 'd' , depth : 1 },
movies : {type : 'array' , parent : 'd' , depth : 2 },
subObject : {type : 'object' , parent : 'movies' },
cars : {type : 'array' , parent : 'd' , depth : 2 },
subObject2 : {type : 'object' , parent : 'cars' }
};
var _nextAttrName = 'movies';
var _nbArrayExit = 0;
// eslint-disable-next-line no-unused-vars
builder.forEachArrayExit(_currentlyVisitedArrays, _objDependencyDescriptor, _nextAttrName, function (arrayLeft) {
_nbArrayExit++;
});
helper.assert(_nbArrayExit, 1);
helper.assert(_currentlyVisitedArrays, ['countries']);
});
it('should not leave the array "cars" if the object "subObject3" come from cars', function () {
var _currentlyVisitedArrays = ['countries', 'cars'];
var _objDependencyDescriptor = {
d : {type : 'object' , parent : '' },
countries : {type : 'array' , parent : 'd' , depth : 1 },
movies : {type : 'array' , parent : 'd' , depth : 2 },
subObject : {type : 'object' , parent : 'movies' },
cars : {type : 'array' , parent : 'd' , depth : 2 },
subObject2 : {type : 'object' , parent : 'cars' },
subObject3 : {type : 'object' , parent : 'subObject2' }
};
var _nextAttrName = 'subObject3';
var _nbArrayExit = 0;
// eslint-disable-next-line no-unused-vars
builder.forEachArrayExit(_currentlyVisitedArrays, _objDependencyDescriptor, _nextAttrName, function (arrayLeft) {
_nbArrayExit++;
});
helper.assert(_nbArrayExit, 0);
helper.assert(_currentlyVisitedArrays, ['countries', 'cars']);
});
it('should leave the array "cars" if the object "subObject4" come from movies and movies is at the same depth in XML', function () {
var _currentlyVisitedArrays = ['countries', 'cars'];
var _objDependencyDescriptor = {
d : {type : 'object' , parent : '' },
countries : {type : 'array' , parent : 'd' , depth : 1 },
movies : {type : 'array' , parent : 'd' , depth : 2 },
subObject : {type : 'object' , parent : 'movies' },
subObject4 : {type : 'object' , parent : 'subObject' },
cars : {type : 'array' , parent : 'd' , depth : 2 },
subObject2 : {type : 'object' , parent : 'cars' },
subObject3 : {type : 'object' , parent : 'subObject2' }
};
var _nextAttrName = 'subObject4';
var _nbArrayExit = 0;
// eslint-disable-next-line no-unused-vars
builder.forEachArrayExit(_currentlyVisitedArrays, _objDependencyDescriptor, _nextAttrName, function (arrayLeft) {
_nbArrayExit++;
});
helper.assert(_nbArrayExit, 1);
helper.assert(_currentlyVisitedArrays, ['countries']);
});
});
describe('assembleXmlParts', function () {
it('should sort the array of xml parts according to the "pos" attribute and assemble all strings', function () {
var _data = [
{pos : [40], str : '4'},
{pos : [19], str : '2'},
{pos : [2 ], str : '1'},
{pos : [20], str : '3'}
];
helper.assert(builder.assembleXmlParts(_data), '1234');
});
it('should sort a complex array (sort depth of 3) of xml parts and assemble all strings', function () {
var _data = [
{pos : [4, 4, 2], str : '6'},
{pos : [4, 4, 1], str : '5'},
{pos : [2, 4 ], str : '4'},
{pos : [1, 9, 1], str : '2'},
{pos : [2, 3 ], str : '3'},
{pos : [1 ], str : '1'}
];
helper.assert(builder.assembleXmlParts(_data, 3), '123456');
});
it('should sort a complex array and assemble all strings. It should keep all the parts if rowShow,rowStart,rowEnd are undefined', function () {
var _data = [
{ pos : [ 0 ], str : '<xml>' },
{ pos : [ 6, 1, 14 ], str : 'Tesla motors' },
{ pos : [ 6, 1, 6 ], str : '<tr>' },
{ pos : [ 6, 1, 23 ], str : '</tr>' },
{ pos : [ 6, 0, 14 ], str : 'Lumeneo' },
{ pos : [ 6, 0, 6 ], str : '<tr>' },
{ pos : [ 6, 0, 23 ], str : '</tr>' },
{ pos : [ 6, 2, 14 ], str : 'Toyota' },
{ pos : [ 6, 2, 6 ], str : '<tr>' },
{ pos : [ 6, 2, 23 ], str : '</tr>' },
{ pos : [ 24 ], str : '</xml>' }
];
helper.assert(builder.assembleXmlParts(_data, 5), '<xml><tr>Lumeneo</tr><tr>Tesla motors</tr><tr>Toyota</tr></xml>');
});
it('should accept to sort strings.', function () {
var _data = [
{ pos : [ 0 ], str : '<xml>' },
{ pos : [ 6, 'ab', 14 ], str : 'Tesla motors' },
{ pos : [ 6, 'ab', 6 ], str : '<tr>' },
{ pos : [ 6, 'ab', 23 ], str : '</tr>' },
{ pos : [ 6, 'aa', 14 ], str : 'Lumeneo' },
{ pos : [ 6, 'aa', 6 ], str : '<tr>' },
{ pos : [ 6, 'aa', 23 ], str : '</tr>' },
{ pos : [ 6, 'c' , 14 ], str : 'Toyota' },
{ pos : [ 6, 'c' , 6 ], str : '<tr>' },
{ pos : [ 6, 'c' , 23 ], str : '</tr>' },
{ pos : [ 24 ], str : '</xml>' }
];
helper.assert(builder.assembleXmlParts(_data, 5), '<xml><tr>Lumeneo</tr><tr>Tesla motors</tr><tr>Toyota</tr></xml>');
});
it('should remove the entire row of an array if all attributes "rowShow" equals false', function () {
var _data = [
{ pos : [ 0 ], str : '<xml> ' },
{ pos : [ 6, 0, 6 ], str : '<tr><p>' , rowStart : true },
{ pos : [ 6, 0, 13 ], str : 'Thomas' , rowShow : true },
{ pos : [ 6, 0, 29 ], str : '</p></tr>', rowEnd : true },
{ pos : [ 6, 1, 6 ] , str : '<tr><p>' , rowStart : true },
{ pos : [ 6, 1, 13 ], str : 'Trinity' , rowShow : false },
{ pos : [ 6, 1, 29 ], str : '</p></tr>', rowEnd : true },
{ pos : [ 30 ], str : ' </xml>' }
];
helper.assert(builder.assembleXmlParts(_data, 3), '<xml> <tr><p>Thomas</p></tr> </xml>');
});
it('should remove all rows if rowShow is undefined', function () {
var _data = [
{ pos : [ 0 ], str : '<xml> ' },
{ pos : [ 6, 0, 6 ], str : '<tr><p>' , rowStart : true },
{ pos : [ 6, 0, 13 ], str : 'Thomas' },
{ pos : [ 6, 0, 29 ], str : '</p></tr>', rowEnd : true },
{ pos : [ 6, 1, 6 ] , str : '<tr><p>' , rowStart : true },
{ pos : [ 6, 1, 13 ], str : 'Trinity' },
{ pos : [ 6, 1, 29 ], str : '</p></tr>', rowEnd : true },
{ pos : [ 30 ], str : ' </xml>' }
];
helper.assert(builder.assembleXmlParts(_data, 3), '<xml> </xml>');
});
it('should remove (nested array) the entire row of an array if all attributes "rowShow" equals false', function () {
var _data = [
{ pos : [ 0 ], str : '<xml> ' },
{ pos : [ 6, 0, 6 ], str : '<tr><p>' ,rowStart : true},
{ pos : [ 6, 0, 13 ], str : 'Thomas' ,rowShow : false},
{ pos : [ 6, 0, 20 ], str : '</p><p>A. Anderson' ,rowShow : false},
{ pos : [ 6, 0, 20, 0, 20 ], str : '<tr>' ,rowStart : true},
{ pos : [ 6, 0, 20, 0, 24 ], str : 'survive' ,rowShow : false},
{ pos : [ 6, 0, 20, 0, 29 ], str : '</tr>' ,rowEnd : true},
{ pos : [ 6, 0, 20, 1, 20 ], str : '<tr>' ,rowStart : true},
{ pos : [ 6, 0, 20, 1, 24 ], str : 'walk on the walls' ,rowShow : true}, // <-- at least one part to keep
{ pos : [ 6, 0, 20, 1, 29 ], str : '</tr>' ,rowEnd : true},
{ pos : [ 6, 0, 38 ], str : '</p></tr>' ,rowEnd : true},
{ pos : [ 6, 1, 6 ], str : '<tr><p>' ,rowStart : true},
{ pos : [ 6, 1, 13 ], str : 'Trinity' ,rowShow : false},
{ pos : [ 6, 1, 20 ], str : '</p><p>Unknown' ,rowShow : false},
{ pos : [ 6, 1, 20, 0, 20 ], str : '<tr>' ,rowStart : true},
{ pos : [ 6, 1, 20, 0, 24 ], str : 'hack' ,rowShow : false},
{ pos : [ 6, 1, 20, 0, 29 ], str : '</tr>' ,rowEnd : true},
{ pos : [ 6, 1, 38 ], str : '</p></tr>' ,rowEnd : true},
{ pos : [ 39 ], str : ' </xml>' }
];
helper.assert(builder.assembleXmlParts(_data, 5), '<xml> <tr><p>Thomas</p><p>A. Anderson<tr>walk on the walls</tr></p></tr> </xml>');
});
it('should hide or show according to conditional block boolean', function () {
var _data = [
{ pos : [ 0 ] , str : '<a> hey </a><b> <c></c> </b>' },
{ pos : [ 27.8 ] , str : '' },
{ pos : [ 27.9 ] , str : '<d> textD <e>e</e> </d>' , hide : 0 },
{ pos : [ 50.8 ] , str : '' , hide : -1 },
{ pos : [ 50.9 ] , str : '<f> <g></g><h></h><i></i></f><j/><k><l></l></k>' },
{ pos : [ 98.8 ] , str : '' },
{ pos : [ 98.9 ] , str : '<m> <n> textN </n></m>' , hide : 1 },
{ pos : [ 121.8] , str : '' , hide : -1 },
{ pos : [ 121.9] , str : '' },
{ pos : [ 122.9] , str : '<o> <p></p><q></q><r></r></o>' }
];
helper.assert(builder.assembleXmlParts(_data, 5), '<a> hey </a><b> <c></c> </b><d> textD <e>e</e> </d><f> <g></g><h></h><i></i></f><j/><k><l></l></k><o> <p></p><q></q><r></r></o>');
});
it('should hide all XML within a surrounding conditional block', function () {
var _data = [
{ pos : [ 0 ], str : '<xml> ' },
{ pos : [ 6, 0, 6 ], str : '<tr><p>' , hide : 1 },
{ pos : [ 6, 0, 13 ], str : 'Thomas' , },
{ pos : [ 6, 0, 20 ], str : '</p><p>A. Anderson' , },
{ pos : [ 6, 0, 20, 0, 20 ], str : '<tr>' , },
{ pos : [ 6, 0, 20, 0, 24 ], str : 'survive' , hide : 0 },
{ pos : [ 6, 0, 20, 0, 29 ], str : '</tr>' , hide : -1 },
{ pos : [ 6, 0, 20, 1, 20 ], str : '<tr>' , hide : 1 },
{ pos : [ 6, 0, 20, 1, 24 ], str : 'walk on the walls' , },
{ pos : [ 6, 0, 20, 1, 29 ], str : '</tr>' , hide : -1 },
{ pos : [ 6, 0, 38 ], str : '</p></tr>' , },
{ pos : [ 6, 1, 6 ], str : '<tr><p>' , },
{ pos : [ 6, 1, 13 ], str : 'Trinity' , },
{ pos : [ 6, 1, 20 ], str : '</p><p>Unknown' , },
{ pos : [ 6, 1, 20, 0, 20 ], str : '<tr>' , },
{ pos : [ 6, 1, 20, 0, 24 ], str : 'hack' , },
{ pos : [ 6, 1, 20, 0, 29 ], str : '</tr>' , },
{ pos : [ 6, 1, 38 ], str : '</p></tr>' , hide : -1 },
{ pos : [ 39 ], str : ' </xml>' }
];
helper.assert(builder.assembleXmlParts(_data, 5), '<xml> </p></tr> </xml>');
});
});
describe('getBuilderFunction', function () {
/**
* Take XML strings from dictionary (using part.bef and part.aft index) and place them directly in the part.str
*
* This function was created when I modified the methode to inject XML string.
* To avoid updating all unit tests, this function adapt the new result to old unit tests.
*
* @param {Array} dict dictionary generated by the builder function
* @param {Array} arrayParts array of XML part coming from builder function
* @return {Arrau} array of XML part updated for old unit tests
*/
function simplify (dict, arrayParts) {
for (var i = arrayParts.length - 1; i >= 0; i--) {
var _part = arrayParts[i];
if (_part.bef !== undefined) {
_part.str = dict[_part.bef] + _part.str;
}
if (_part.aft !== undefined) {
_part.str += dict[_part.aft];
}
delete _part.bef;
delete _part.aft;
}
return arrayParts;
}
it('should return a function which returns an array of xml parts for static data if dynamicData is empty', function () {
var _desc = {
staticData : {
before : '<xml>',
after : '</xml>'
},
hierarchy : [],
dynamicData : {}
};
var _fn = builder.getBuilderFunction(_desc);
helper.assert(_fn(null, {}, helper, _fn.builderDictionary), [{pos : [0], str : '', bef : 0}, {pos : [1], str : '', aft : 1}]);
helper.assert(_fn.builderDictionary, ['<xml>', '</xml>']);
});
it('should return an array of xml parts according to the descriptor, the data and the formatters', function (done) {
var _desc = {
staticData : {
before : '',
after : '</xml>'
},
hierarchy : ['_root'],
dynamicData : {
_root : {
name : '',
parent : '',
parents : [],
type : 'object',
depth : 0,
xmlParts : [
{obj : '_root', attr : 'number', pos : 5 , depth : 0, before : '<xml>', formatters : [ 'int' ]}
]
}
}
};
var _data = {
number : 24.55
};
var _fn = builder.getBuilderFunction(_desc, input.formatters);
helper.assert(simplify(_fn.builderDictionary, _fn(_data, {formatters : input.formatters}, helper, _fn.builderDictionary)), [
{pos : [5 ],str : '<xml>24', rowShow : true},
{pos : [6 ],str : '</xml>'}
]);
done();
});
it('should manage multiple attributes in the main object "_root"', function () {
var _desc = {
staticData : {
before : '',
after : '</p></xml>'
},
hierarchy : ['_root'],
dynamicData : {
_root : {
name : '',
parent : '',
parents : [],
type : 'object',
depth : 0,
xmlParts : [
{obj : '_root', attr : 'firstname', pos : 8 , depth : 0, before : '<xml><p>', formatters : []},
{obj : '_root', attr : 'lastname' , pos : 15, depth : 0, before : '</p><p>' , formatters : []},
{obj : '_root', attr : 'surname' , pos : 22, depth : 0, before : '</p><p>' , formatters : []}
]
}
}
};
var _data = {
firstname : 'Thomas',
lastname : 'A. Anderson',
surname : 'Neo'
};
var _fn = builder.getBuilderFunction(_desc);
helper.assert(simplify(_fn.builderDictionary, _fn(_data, {}, helper, _fn.builderDictionary)), [
{pos : [8 ],str : '<xml><p>Thomas', rowShow : true},
{pos : [15 ],str : '</p><p>A. Anderson', rowShow : true},
{pos : [22 ],str : '</p><p>Neo', rowShow : true},
{pos : [23 ],str : '</p></xml>'}
]);
});
it('should return only the xml if the data is empty or null or if one of the attribute does not exist. (the object formatters is not provided)', function () {
var _desc = {
staticData : {
before : '',
after : '</p></xml>'
},
hierarchy : ['_root'],
dynamicData : {
_root : {
name : '',
parent : '',
parents : [],
type : 'object',
depth : 0,
xmlParts : [
{obj : '_root', attr : 'firstname', pos : 8 , depth : 0, before : '<xml><p>'},
{obj : '_root', attr : 'lastname' , pos : 15, depth : 0, before : '</p><p>' },
{obj : '_root', attr : 'surname' , pos : 22, depth : 0, before : '</p><p>' }
]
}
}
};
var _fn = builder.getBuilderFunction(_desc);
var _data = {};
helper.assert(simplify(_fn.builderDictionary, _fn(_data, {}, helper, _fn.builderDictionary)), [{pos : [8], str : '<xml><p>', rowShow : true}, {pos : [15], str : '</p><p>', rowShow : true}, {pos : [22], str : '</p><p>', rowShow : true}, {pos : [23], str : '</p></xml>'}]);
helper.assert(simplify(_fn.builderDictionary, _fn(null, {}, helper, _fn.builderDictionary)), [{pos : [8], str : '<xml><p>', rowShow : true}, {pos : [15], str : '</p><p>', rowShow : true}, {pos : [22], str : '</p><p>', rowShow : true}, {pos : [23], str : '</p></xml>'}]);
_data = {
firstname : 'Thomas',
surname : 'Neo'
};
helper.assert(simplify(_fn.builderDictionary, _fn(_data, {}, helper, _fn.builderDictionary)) , [{pos : [8], str : '<xml><p>Thomas', rowShow : true}, {pos : [15], str : '</p><p>', rowShow : true}, {pos : [22], str : '</p><p>Neo', rowShow : true}, {pos : [23], str : '</p></xml>'}]);
});
it('should work even if there is a nested object in the descriptor', function () {
var _desc = {
staticData : {
before : '',
after : '</p></xml>'
},
hierarchy : ['_root', 'info1'],
dynamicData : {
_root : {
name : '',
parent : '',
parents : [],
type : 'object',
depth : 0,
xmlParts : [
{obj : '_root', attr : 'firstname', pos : 8, depth : 0, before : '<xml><p>'},
{obj : '_root', attr : 'lastname' , pos : 15, depth : 0, before : '</p><p>' },
{obj : '_root', attr : 'surname' , pos : 40, depth : 0, before : '</br><p>'}
]
},
info1 : {
name : 'info',
parent : '_root',
parents : ['_root'],
type : 'object',
depth : 0,
xmlParts : [
{obj : 'info1', attr : 'movie', pos : 23, depth : 0, before : '</p><br>' },
{obj : 'info1', attr : 'job' , pos : 32, depth : 0, before : '</br><br>'}
]
}
}
};
var _data = {
firstname : 'Thomas',
lastname : 'A. Anderson',
surname : 'Neo',
info : {
movie : 'matrix',
job : 'developer'
}
};
var _fn = builder.getBuilderFunction(_desc);
helper.assert(simplify(_fn.builderDictionary, _fn(_data, {}, helper, _fn.builderDictionary)), [
{ pos : [ 8 ] , str : '<xml><p>Thomas', rowShow : true},
{ pos : [ 15 ], str : '</p><p>A. Anderson', rowShow : true},
{ pos : [ 40 ], str : '</br><p>Neo', rowShow : true},
{ pos : [ 23 ], str : '</p><br>matrix', rowShow : true},
{ pos : [ 32 ], str : '</br><br>developer', rowShow : true},
{ pos : [ 41 ], str : '</p></xml>'}
]);
});
it('should travel the object in the correct order using the "hierarchy" array even if the dynamicData object is not built in correct order', function () {
var _desc = {
staticData : {
before : '',
after : '</p></xml>'
},
hierarchy : ['_root', 'info1'],
dynamicData : {
info1 : {
name : 'info',
parent : '_root',
parents : ['_root'],
type : 'object',
depth : 0,
xmlParts : [
{obj : 'info1', attr : 'movie', pos : 23, depth : 0, before : '</p><br>' },
{obj : 'info1', attr : 'job' , pos : 32, depth : 0, before : '</br><br>' }
]
},
_root : {
name : '',
parent : '',
parents : [],
type : 'object',
depth : 0,
xmlParts : [
{obj : '_root', attr : 'firstname', pos : 8, depth : 0, before : '<xml><p>'},
{obj : '_root', attr : 'lastname' , pos : 15, depth : 0, before : '</p><p>' },
{obj : '_root', attr : 'surname' , pos : 40, depth : 0, before : '</br><p>'}
]
}
}
};
var _data = {
firstname : 'Thomas',
lastname : 'A. Anderson',
surname : 'Neo',
info : {
movie : 'matrix',
job : 'developer'
}
};
var _fn = builder.getBuilderFunction(_desc);
helper.assert(simplify(_fn.builderDictionary, _fn(_data, {}, helper, _fn.builderDictionary)), [
{ pos : [ 8 ], str : '<xml><p>Thomas', rowShow : true},
{ pos : [ 15 ], str : '</p><p>A. Anderson', rowShow : true},
{ pos : [ 40 ], str : '</br><p>Neo', rowShow : true},
{ pos : [ 23 ], str : '</p><br>matrix', rowShow : true},
{ pos : [ 32 ], str : '</br><br>developer', rowShow : true},
{ pos : [ 41 ], str : '</p></xml>' }
]);
});
it('should work if the main object is an array of object', function () {
var _desc = {
staticData : {
before : '<xml> ',
after : ' </xml>'
},
hierarchy : ['_root'],
dynamicData : {
_root : {
name : '',
parent : '',
parents : [],
type : 'array',
depth : 1,
position : {start : 6, end : 29},
iterators : [{ attr : 'i' }],
xmlParts : [
{obj : '_root', array : 'start' , pos : 6 , depth : 1, after : '<tr><p>' },
{obj : '_root', attr : 'firstname', pos : 13, depth : 1, },
{obj : '_root', attr : 'lastname' , pos : 20, depth : 1, before : '</p><p>' },
{obj : '_root', array : 'end' , pos : 29, depth : 1, before : '</p></tr>' }
]
}
}
};
var _data = [
{firstname : 'Thomas' , lastname : 'A. Anderson'},
{firstname : 'Trinity', lastname : 'Unknown'}
];
var _fn = builder.getBuilderFunction(_desc);
helper.assert(simplify(_fn.builderDictionary, _fn(_data, {}, helper, _fn.builderDictionary)), [
{ pos : [ 0 ], str : '<xml> ' },
{ pos : [ 6, 0, 6 ], str : '<tr><p>' , rowStart : true},
{ pos : [ 6, 0, 13 ], str : 'Thomas' , rowShow : true },