forked from GSattybay/carbone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.carbone.js
3183 lines (3145 loc) · 133 KB
/
test.carbone.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 path = require('path');
var fs = require('fs');
var helper = require('../lib/helper');
var params = require('../lib/params');
var input = require('../lib/input');
var converter = require('../lib/converter');
var testPath = path.join(__dirname, 'test_file');
var spawn = require('child_process').spawn;
var execSync = require('child_process').execSync;
describe('Carbone', function () {
describe('set', function () {
var _templatePath = path.join(__dirname,'template1');
var _tempPath = path.join(__dirname,'temp1');
after(function (done) {
helper.rmDirRecursive(_tempPath);
helper.rmDirRecursive(_templatePath);
done();
});
afterEach(function (done) {
carbone.reset();
done();
});
it('should create automatically the template directory if it does not exists', function (done) {
helper.rmDirRecursive(_templatePath);
carbone.set({templatePath : _templatePath});
helper.assert(fs.existsSync(_templatePath), true);
done();
});
it('should create automatically the temp directory if it does not exists', function (done) {
helper.rmDirRecursive(_tempPath);
carbone.set({tempPath : _tempPath});
helper.assert(fs.existsSync(_tempPath), true);
done();
});
it('should not overwrite lang object if provided', function (done) {
carbone.set({
templatePath : _templatePath,
translations : {
fr : {
test : 'trad'
}
}
});
helper.assert(params.translations, {
fr : {
test : 'trad'
}
});
done();
});
});
describe('format date', function () {
afterEach(function (done) {
carbone.reset();
done();
});
it('should return friday for 20140131 even if no timezone is set', function (done) {
carbone.set({lang : 'fr'});
carbone.renderXML('<xml> {d.date:formatD(dddd)} </xml>', { date : '20140131 23:45:00'}, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml> vendredi </xml>');
carbone.renderXML('<xml> {d.date:formatD(dddd)} </xml>', { date : '20140131'}, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml> vendredi </xml>');
carbone.renderXML('<xml> {d.date:formatD(dddd)} </xml>', { date : '20140131 00:10:00'}, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml> vendredi </xml>');
done();
});
});
});
});
it('should change the lang globally of date formatter', function (done) {
carbone.set({lang : 'fr'});
carbone.renderXML('<xml> {d.date:formatD(dddd)} </xml>', { date : '20140131 23:45:00'}, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml> vendredi </xml>');
carbone.set({lang : 'en'});
carbone.renderXML('<xml> {d.date:formatD(dddd)} </xml>', { date : '20140131'}, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml> Friday </xml>');
carbone.set({lang : 'fr'});
carbone.renderXML('<xml> {d.date:formatD(dddd)} </xml>', { date : '20140131'}, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml> vendredi </xml>');
// Should overwrite global lang with options
carbone.renderXML('<xml> {d.date:formatD(dddd)} </xml>', { date : '20140131'}, {lang : 'en'}, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml> Friday </xml>');
done();
});
});
});
});
});
it('should change the timezone globally and localy', function (done) {
carbone.set({timezone : 'Europe/Paris'});
carbone.renderXML('<xml> {d.date:formatD(LTS)} </xml>', { date : '2014-06-01 14:00:00'}, function (err, result) {
helper.assert(err+'', 'null');
// By defaut, Carbone consider the input timezone is Europe/Paris if not specified
helper.assert(result, '<xml> 2:00:00 PM </xml>');
carbone.set({timezone : 'America/New_York'});
carbone.renderXML('<xml> {d.date:formatD(LTS)} </xml>', { date : '2014-06-01 14:00:00'}, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml> 8:00:00 AM </xml>');
// If the input timezone is defined with UTC-X, it takes this into account
carbone.renderXML('<xml> {d.date:formatD(LTS)} </xml>', { date : '2014-06-01 14:00:00-04:00'}, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml> 2:00:00 PM </xml>');
// Should overwrite global timezone with options
carbone.renderXML('<xml> {d.date:formatD(LTS)} </xml>', { date : '2014-06-01 14:00:00-04:00'}, {timezone : 'Europe/Paris'}, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml> 8:00:00 PM </xml>');
done();
});
});
});
});
});
it('should not crash it the wrong timezone or wrong lang is passed during rendering', function (done) {
carbone.renderXML('<xml> {d.date:formatD(LTS)} </xml>', { date : '2014-06-01 14:00:00-04:00'}, {timezone : 'BULLSHIT'}, function (err, result) {
helper.assert(err+'', 'RangeError: Expected Area/Location(/Location)* for time zone, got BULLSHIT');
helper.assert(result, null);
carbone.renderXML('<xml> {d.date:formatD(LTS)} </xml>', { date : '2014-06-01 14:00:00-04:00'}, {lang : 'BULLSHIT'}, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml> 8:00:00 PM </xml>');
done();
});
});
});
it('should accept combination of operations `addD` on dates + formatD', function (done) {
carbone.renderXML('{d.date:addD(1, day):formatD(YYYY-MM-DD)}', { date : '2014-06-01 14:00:00'}, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '2014-06-02');
carbone.renderXML('{d.date:addD(1, day, MM-DD-YYYY):formatD(YYYY-MM-DD)}', { date : '06-01-2014'}, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '2014-06-02');
done();
});
});
});
it('should accept combination of operations `addD` on dates + format', function (done) {
carbone.renderXML('{d.date:subD(1, day):formatD(YYYY-MM-DD)}', { date : '2014-06-01 14:00:00'}, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '2014-05-31');
carbone.renderXML('{d.date:subD(1, day, MM-DD-YYYY):formatD(YYYY-MM-DD)}', { date : '06-01-2014'}, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '2014-05-31');
done();
});
});
});
it('should accept combination of operations `startOfD` on dates + format', function (done) {
carbone.renderXML('{d.date:startOfD(month):formatD(YYYY-MM-DD)}', { date : '2014-06-11 14:00:00'}, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '2014-06-01');
carbone.renderXML('{d.date:startOfD(month, MM-DD-YYYY):formatD(YYYY-MM-DD)}', { date : '06-11-2014'}, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '2014-06-01');
done();
});
});
});
it('should accept combination of operations `endOfD` on dates + format', function (done) {
carbone.renderXML('{d.date:endOfD(month):formatD(YYYY-MM-DD)}', { date : '2014-06-11 14:00:00'}, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '2014-06-30');
carbone.renderXML('{d.date:endOfD(month, MM-DD-YYYY):formatD(YYYY-MM-DD)}', { date : '06-11-2014'}, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '2014-06-30');
done();
});
});
});
});
describe('addTemplate', function () {
var _templatePath = path.join(__dirname,'template');
before(function () {
helper.rmDirRecursive(_templatePath);
fs.mkdirSync(_templatePath, '0755');
carbone.set({templatePath : _templatePath});
});
after(function () {
helper.rmDirRecursive(_templatePath);
carbone.reset();
});
it('should save the template in the folder "templatePath"', function (done) {
var _filePath = path.resolve('./test/datasets/test_word_render_2003_XML.xml');
var _fileContent = fs.readFileSync(_filePath, 'utf8');
var _fileId = '1.xml';
carbone.addTemplate(_fileId, _fileContent, function (err) {
helper.assert(err, null);
var _result = fs.readFileSync(path.join(_templatePath,_fileId), 'utf8');
helper.assert(_result, _fileContent);
done();
});
});
it('should overwrite existing template and should work if data is a buffer', function (done) {
var _fileId = '2.txt';
fs.writeFileSync(path.join(_templatePath, _fileId), 'bla');
var _filePath = path.resolve('./test/datasets/test_word_render_2003_XML.xml');
var _fileContent = fs.readFileSync(_filePath);
carbone.addTemplate(_fileId, _fileContent, function (err) {
helper.assert(err, null);
var _result = fs.readFileSync(path.join(_templatePath,_fileId));
helper.assert(_result, _fileContent);
done();
});
});
});
describe('addFormatters', function () {
it('should add a formatter to the list of custom formatters', function () {
carbone.addFormatters({
yesOrNo : function (d) {
return d === true ? 'yes' : 'no';
}
});
assert.notEqual(typeof input.formatters.yesOrNo, 'undefined');
assert.equal(input.formatters.yesOrNo(true), 'yes');
});
});
describe('removeTemplate', function () {
var _templatePath = path.join(__dirname,'template');
before(function () {
helper.rmDirRecursive(_templatePath);
fs.mkdirSync(_templatePath, '0755');
carbone.set({templatePath : _templatePath});
});
after(function () {
helper.rmDirRecursive(_templatePath);
carbone.reset();
});
it('should remove the template from the Carbone datastore (templatePath)', function (done) {
var _fileId = '2.txt';
var _filePath = path.join(_templatePath, _fileId);
fs.writeFileSync(_filePath, 'bla');
carbone.removeTemplate(_fileId, function (err) {
helper.assert(err, null);
helper.assert(fs.existsSync(_filePath), false);
done();
});
});
it('should not crash if the template does not exist', function (done) {
var _fileId = '5.txt';
carbone.removeTemplate(_fileId, function (err) {
helper.assert(/unlink/.test(err+''), true);
done();
});
});
});
describe('listConversionFormats', function () {
it('should return the list of format for conversion', function (done) {
var _list = carbone.listConversionFormats('document');
helper.assert(_list[0].id, 'bib');
done();
});
});
describe('renderXML with dash', function () {
it('should render an XML string with dash in json keys', function (done) {
var data = {
param : {
'new-param' : {
value : 'test'
}
}
};
carbone.renderXML('<xml>{d.param.new-param.value}</xml>', data, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml>test</xml>');
done();
});
});
it('should render an XML string with multiple dash in json keys', function (done) {
var data = {
param : {
'new-param-s-t-u-popo' : {
value : 'test'
}
}
};
carbone.renderXML('<xml>{d.param.new-param-s-t-u-popo.value}</xml>', data, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml>test</xml>');
done();
});
});
it('should print an array without usng a formatter', function (done) {
var data = {
param : ['test', 'bla']
};
carbone.renderXML('<xml>{d.param}</xml>', data, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml>test,bla</xml>');
done();
});
});
it('should render an XML string with dash in array keys', function (done) {
var data = {
'param-dash' : [{
'new-param-with-dash' : 'val'
}, {
'new-param-with-dash' : 'val1'
}, {
'new-param-with-dash' : 'val2'
}]
};
carbone.renderXML('<xml><t>{d.param-dash[i].new-param-with-dash}</t><t>{d.param-dash[i+1].new-param-with-dash}</t></xml>', data, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml><t>val</t><t>val1</t><t>val2</t></xml>');
done();
});
});
it('should render an XML string with dash in filter keys', function (done) {
var data = {
'param-dash' : [{
'filter-val' : 1,
'new-param-with-dash' : 'val'
}, {
'filter-val' : 1,
'new-param-with-dash' : 'val1'
}, {
'filter-val' : 2,
'new-param-with-dash' : 'val2'
}]
};
carbone.renderXML('<xml><t>{d.param-dash[i, filter-val=1].new-param-with-dash}</t><t>{d.param-dash[i+1, filter-val=1].new-param-with-dash}</t></xml>', data, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml><t>val</t><t>val1</t></xml>');
done();
});
});
it('should render XML with dahs in array kes and filter keys', function (done) {
var _xml = '<xml><t_row> {d.cars-dash[sort-dash-s,i].brand-dash-v:count()} {d.cars-dash[sort-dash-s,i].brand-dash-v} </t_row><t_row> {d.cars-dash[sort-dash-s+1,i+1].brand-dash-v} </t_row></xml>';
var _data = {
'cars-dash' : [
{'brand-dash-v' : 'Lumeneo' , 'sort-dash-s' : 1},
{'brand-dash-v' : 'Tesla motors', 'sort-dash-s' : 2},
{'brand-dash-v' : 'Toyota' , 'sort-dash-s' : 1}
]
};
carbone.renderXML(_xml, _data, function (err, _xmlBuilt) {
assert.equal(_xmlBuilt, '<xml><t_row> 1 Lumeneo </t_row><t_row> 2 Toyota </t_row><t_row> 3 Tesla motors </t_row></xml>');
done();
});
});
it('should render XML with dash and multiple array', function (done) {
var _xml =
'<xml>'
+ '<tr>'
+ '<td>{d[i].cars-dash[i].wheels-dash[i].tire-dash.brand-dash:count()} {d[i].cars-dash[i].wheels-dash[i].tire-dash.brand-dash}</td>'
+ '<td>{d[i].cars-dash[i].wheels-dash[i].tire-dash.brand-dash:count(0)} {d[i].site-dash.label-dash}</td>'
+ '</tr>'
+ '<tr>'
+ '<td>{d[i+1].cars-dash[i+1].wheels-dash[i+1].tire-dash.brand-dash}</td>'
+ '<td>{d[i+1].site-dash.label-dash}</td>'
+ '</tr>'
+'</xml>';
var _data = [
{
'site-dash' : {'label-dash' : 'site_A'},
'cars-dash' : [
{
'wheels-dash' : [
{'tire-dash' : {'brand-dash' : 'mich'}},
{'tire-dash' : {'brand-dash' : 'cont'}}
]
},
{
'wheels-dash' : [
{'tire-dash' : {'brand-dash' : 'mich'}}
]
},
],
},{
'site-dash' : {'label-dash' : 'site_B'},
'cars-dash' : [{
'wheels-dash' : [
{'tire-dash' : {'brand-dash' : 'mich'}},
{'tire-dash' : {'brand-dash' : 'uni' }},
{'tire-dash' : {'brand-dash' : 'cont'}}
]
}
],
}
];
carbone.renderXML(_xml, _data, function (err, _xmlBuilt) {
var _expectedResult =
'<xml>'
+ '<tr>'
+ '<td>1 mich</td>'
+ '<td>0 site_A</td>'
+ '</tr>'
+ '<tr>'
+ '<td>2 cont</td>'
+ '<td>1 site_A</td>'
+ '</tr>'
+ '<tr>'
+ '<td>3 mich</td>'
+ '<td>2 site_A</td>'
+ '</tr>'
+ '<tr>'
+ '<td>4 mich</td>'
+ '<td>3 site_B</td>'
+ '</tr>'
+ '<tr>'
+ '<td>5 uni</td>'
+ '<td>4 site_B</td>'
+ '</tr>'
+ '<tr>'
+ '<td>6 cont</td>'
+ '<td>5 site_B</td>'
+ '</tr>'
+'</xml>';
assert.equal(_xmlBuilt, _expectedResult);
done();
});
});
});
describe('renderXML', function () {
after(function (done) {
carbone.reset();
done();
});
it('should render an XML string', function (done) {
var data = {
param : 'field_1'
};
carbone.renderXML('<xml>{d.param}</xml>', data, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml>field_1</xml>');
done();
});
});
it('should return the current timestamp in UTC with c.now', function (done) {
carbone.renderXML('{c.now}', {}, function (err, result) {
helper.assert(err+'', 'null');
var _parseDate = new Date(result);
helper.assert(((Date.now()/1000) - _parseDate.getTime()) < 1, true) ;
done();
});
});
it('should not overwrite c.now if already defined the current timestamp in UTC with c.now', function (done) {
carbone.renderXML('{c.now}', {}, { complement : {now : 'aa'} }, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, 'aa');
done();
});
});
it('should execute formatter if the data object is empty with the formatter ifEmpty', function (done) {
var data = {};
carbone.renderXML('<xml>{d:ifEmpty(\'yeah\')} {c:ifEmpty(\'oops\')}</xml>', data, {complement : {}}, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml>yeah oops</xml>');
done();
});
});
it('should execute formatter if the data array is empty with the formatter ifEmpty', function (done) {
var data = [];
carbone.renderXML('<xml>{d:ifEmpty(\'yeah\')} {c:ifEmpty(\'oops\')}</xml>', data, {complement : []}, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml>yeah oops</xml>');
done();
});
});
it('should return an error if the formatter does not exist', function (done) {
var data = {
param : 1
};
carbone.renderXML('<xml>{d.param:ifEkual(2, \'two\')}</xml>', data, function (err, result) {
helper.assert(err+'', 'Error: Formatter "ifEkual" does not exist. Do you mean "ifEqual"?');
helper.assert(result, null);
done();
});
});
it('#1 conditional formatters should be executed one after another, the next formatter is called if the condition of the previous one is false', function (done) {
var data = {
param : 1
};
carbone.renderXML('<xml>{d.param:ifEqual(2, \'two\'):ifEqual(3, \'three\'):ifEqual(1, \'one\'):print(\'unknown\')}</xml>', data, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml>one</xml>');
done();
});
});
it('#3 conditional formatters should be executed one after another, the next formatter is called if the condition of the previous one is false', function (done) {
var data = {
param : 3
};
carbone.renderXML('<xml>{d.param:ifEqual(2, \'two\'):ifEqual(3, \'three\'):ifEqual(1, \'one\'):print(\'unknown\')}</xml>', data, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml>three</xml>');
done();
});
});
it('#2 conditional formatters should be executed one after another, the next formatter is called if the condition of the previous one is false', function (done) {
var data = {
param : 2
};
carbone.renderXML('<xml>{d.param:ifEqual(2, \'two\'):ifEqual(3, \'three\'):ifEqual(1, \'one\'):print(\'unknown\')}</xml>', data, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml>two</xml>');
done();
});
});
it('#4 conditional formatters should be executed one after another, the next formatter is called if the condition of the previous one is false', function (done) {
var data = {
param : 6
};
carbone.renderXML('<xml>{d.param:ifEqual(2, \'two\'):ifEqual(3, \'three\'):ifEqual(1, \'one\'):print(\'unknown\')}</xml>', data, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml>unknown</xml>');
done();
});
});
it('conditional formatter should call the next formatter even if the condition is true when continueOnSuccess=true', function (done) {
var data = {
param : 3
};
carbone.renderXML('<xml>{d.param:ifEqual(2, \'two\'):ifEqual(3, \'three\', true):ifEqual(1, \'one\'):print(\'unknown\')}</xml>', data, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml>unknown</xml>');
done();
});
});
it('formatters should be independant. The propagation of one set of cascaded formatters should not alter the propagation of another set of formatters', function (done) {
var data = {
param : 3,
type : 1,
other : 2,
empty : -1
};
carbone.renderXML(
'<xml>{d.param:ifEqual(2, \'two\'):ifEqual(3, \'three\'):ifEqual(1, \'one\'):print(\'unknown\')}</xml>'
+ '<tr>{d.type:ifEqual(2, \'two\'):ifEqual(3, \'three\'):ifEqual(1, \'one\'):print(\'unknown\')}</tr>'
+ '<td>{d.other:ifEqual(2, \'two\'):ifEqual(3, \'three\'):ifEqual(1, \'one\'):print(\'unknown\')}</td>'
+ '<td>{d.empty:ifEqual(2, \'two\'):ifEqual(3, \'three\'):ifEqual(1, \'one\'):print(\'unknown\')}</td>', data, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml>three</xml><tr>one</tr><td>two</td><td>unknown</td>');
done();
});
});
it('ifEqual formatter should render a text with space and comma characters', function (done) {
const _data = {
myvalue : true
};
const _template = '<xml>{d.myvalue:ifEqual(true, \'This is a text, with, commas\')}</xml>';
const _expectedResult = '<xml>This is a text, with, commas</xml>';
carbone.renderXML(_template, _data, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, _expectedResult);
done();
});
});
it('print formatter should render a text with space and comma characters', function (done) {
const _data = {
myvalue : false
};
const _template = '<xml>{d.myvalue:print(\'This is, a, second, text, with, commas and, spaces\')}</xml>';
const _expectedResult = '<xml>This is, a, second, text, with, commas and, spaces</xml>';
carbone.renderXML(_template, _data, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, _expectedResult);
done();
});
});
it('conditional formatter should call the next formatter even if the condition is true when continueOnSuccess=true', function (done) {
var data = {
param : 2
};
var options = {
enum : {
ORDER_STATUS : ['open', 'close', 'sent']
}
};
carbone.renderXML('<xml>{d.param:convEnum(\'ORDER_STATUS\')}</xml>', data, options, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml>sent</xml>');
done();
});
});
it('options.lang = "fr" should force the lang of date formatter', function (done) {
var data = {
param : '20160131'
};
carbone.set({lang : 'en'}); // default lang
var options = {
lang : 'fr' // forced lang
};
carbone.renderXML('<xml>{d.param:convDate(YYYYMMDD, L)}</xml>', data, options, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml>31/01/2016</xml>');
options.lang = 'en';
carbone.set({lang : 'fr'}); // default lang
carbone.renderXML('<xml>{d.param:convDate(YYYYMMDD, L)}</xml>', data, options, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml>01/31/2016</xml>');
done();
});
});
});
it('should set the default lang if not set in options.lang for date formatter', function (done) {
var data = {
param : '20160131'
};
carbone.set({lang : 'fr'});
carbone.renderXML('<xml>{d.param:convDate(YYYYMMDD, L)}</xml>', data, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml>31/01/2016</xml>');
carbone.set({lang : 'en'});
carbone.renderXML('<xml>{d.param:convDate(YYYYMMDD, L)}</xml>', data, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml>01/31/2016</xml>');
done();
});
});
});
it('options.lang should dynamically force the lang of translation markers {t()} and accept provided translations', function (done) {
var data = {
param : '20160131'
};
carbone.set({lang : 'en'}); // default lang
var options = {
lang : 'fr', // forced lang
translations : {
fr : {
kitchen : 'cuisine'
},
es : {
kitchen : 'cocina'
}
}
};
carbone.renderXML('<xml>{t(kitchen)}</xml>', data, options, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml>cuisine</xml>');
options.lang = 'es';
carbone.set({lang : 'fr'}); // default lang
carbone.renderXML('<xml>{t(kitchen)}</xml>', data, options, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml>cocina</xml>');
done();
});
});
});
it('should translate markers which are inside other markers and remove XML inside', function () {
let _xml = '<xml>{<t>d</t>.id:if<tag>Eq</tag>ual(2, \'{<b>t</b>(on <br>Monday) }\'</br>) }</xml>';
let _expectedXML = '<xml>le Lundi<t></t><tag></tag><b></b><br></br></xml>';
let _options = {
lang : 'fr',
translations : {
fr : {
'on Monday' : 'le Lundi'
}
}
};
carbone.renderXML(_xml, {id : 2}, _options, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, _expectedXML);
});
});
it('options.lang should dynamically force the lang of translation markers {t()} and use translations of carbone', function (done) {
var data = {
param : '20160131'
};
carbone.set({lang : 'en'}); // default lang
carbone.set({
translations : {
fr : {
kitchen : 'cuisine'
},
es : {
kitchen : 'cocina'
}
}
});
carbone.renderXML('<xml>{t(kitchen)}</xml>', data, {lang : 'fr'}, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml>cuisine</xml>');
carbone.set({lang : 'fr'}); // default lang
carbone.renderXML('<xml>{t(kitchen)}</xml>', data, {lang : 'es'}, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml>cocina</xml>');
done();
});
});
});
it('should replace a LF (unix) in an odt file', function (done) {
var _xml = '<xml> {d.text:convCRLF()} </xml>';
var _data = {text : 'boo\nbeep'};
var _options = { extension : 'odt' };
carbone.renderXML(_xml, _data, _options, function (err, _xmlBuilt) {
helper.assert(err + '', 'null');
helper.assert(_xmlBuilt, '<xml> boo<text:line-break/>beep </xml>');
done();
});
});
it('should replace a CRLF (windows) in an odt file', function (done) {
var _xml = '<xml> {d.text:convCRLF()} </xml>';
var _data = {text : 'boo\r\nbeep'};
var _options = { extension : 'odt' };
carbone.renderXML(_xml, _data, _options, function (err, _xmlBuilt) {
helper.assert(err + '', 'null');
helper.assert(_xmlBuilt, '<xml> boo<text:line-break/>beep </xml>');
done();
});
});
it('should replace a LF (unix) in a docx file', function (done) {
var _xml = '<xml> <w:t>{d.text:convCRLF()}</w:t> </xml>';
var _data = {text : 'boo\nbeep'};
var _options = { extension : 'docx' };
carbone.renderXML(_xml, _data, _options, function (err, _xmlBuilt) {
helper.assert(err + '', 'null');
helper.assert(_xmlBuilt, '<xml> <w:t>boo</w:t><w:br/><w:t>beep</w:t> </xml>');
done();
});
});
it('should replace a CRLF (windows) in a docx file', function (done) {
var _xml = '<xml> <w:t>{d.text:convCRLF()}</w:t> </xml>';
var _data = {text : 'boo\r\nbeep'};
var _options = { extension : 'docx' };
carbone.renderXML(_xml, _data, _options, function (err, _xmlBuilt) {
helper.assert(err + '', 'null');
helper.assert(_xmlBuilt, '<xml> <w:t>boo</w:t><w:br/><w:t>beep</w:t> </xml>');
done();
});
});
it('should print a counter', function (done) {
var _xml = '<xml><t_row> {d.cars[sort,i].brand:count()} {d.cars[sort,i].brand} </t_row><t_row> {d.cars[sort+1,i+1].brand} </t_row></xml>';
var _data = {
cars : [
{brand : 'Lumeneo' , sort : 1},
{brand : 'Tesla motors', sort : 2},
{brand : 'Toyota' , sort : 1}
]
};
carbone.renderXML(_xml, _data, function (err, _xmlBuilt) {
assert.equal(_xmlBuilt, '<xml><t_row> 1 Lumeneo </t_row><t_row> 2 Toyota </t_row><t_row> 3 Tesla motors </t_row></xml>');
done();
});
});
it('should print a counter which start by 1 and 0', function (done) {
var _xml =
'<xml>'
+ '<tr>'
+ '<td>{d[i].cars[i].wheels[i].tire.brand:count()} {d[i].cars[i].wheels[i].tire.brand}</td>'
+ '<td>{d[i].cars[i].wheels[i].tire.brand:count(0)} {d[i].site.label}</td>'
+ '</tr>'
+ '<tr>'
+ '<td>{d[i+1].cars[i+1].wheels[i+1].tire.brand}</td>'
+ '<td>{d[i+1].site.label}</td>'
+ '</tr>'
+'</xml>';
var _data = [
{
site : {label : 'site_A'},
cars : [
{
wheels : [
{tire : {brand : 'mich'}},
{tire : {brand : 'cont'}}
]
},
{
wheels : [
{tire : {brand : 'mich'}}
]
},
],
},{
site : {label : 'site_B'},
cars : [{
wheels : [
{tire : {brand : 'mich'}},
{tire : {brand : 'uni' }},
{tire : {brand : 'cont'}}
]
}
],
}
];
carbone.renderXML(_xml, _data, function (err, _xmlBuilt) {
var _expectedResult =
'<xml>'
+ '<tr>'
+ '<td>1 mich</td>'
+ '<td>0 site_A</td>'
+ '</tr>'
+ '<tr>'
+ '<td>2 cont</td>'
+ '<td>1 site_A</td>'
+ '</tr>'
+ '<tr>'
+ '<td>3 mich</td>'
+ '<td>2 site_A</td>'
+ '</tr>'
+ '<tr>'
+ '<td>4 mich</td>'
+ '<td>3 site_B</td>'
+ '</tr>'
+ '<tr>'
+ '<td>5 uni</td>'
+ '<td>4 site_B</td>'
+ '</tr>'
+ '<tr>'
+ '<td>6 cont</td>'
+ '<td>5 site_B</td>'
+ '</tr>'
+'</xml>';
assert.equal(_xmlBuilt, _expectedResult);
done();
});
});
describe('Dynamic variables in formatters', function () {
it('should use variable in object if the variable starts with a point', function (done) {
var data = {
param : 3,
textToPrint : 'ddfdf'
};
carbone.renderXML('<xml>{d.param:ifEqual(3, .textToPrint)}</xml>', data, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml>ddfdf</xml>');
done();
});
});
it('should accept all variables are dynamic if they starts with a point', function (done) {
var data = {
param : 3,
valueToCompare : 3,
textToPrint : 'ddfdf'
};
carbone.renderXML('<xml>{d.param:ifEqual(.valueToCompare, .textToPrint)}</xml>', data, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml>ddfdf</xml>');
data.valueToCompare = 4;
carbone.renderXML('<xml>{d.param:ifEqual(.valueToCompare, .textToPrint)}</xml>', data, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml>3</xml>');
done();
});
});
});
it('should accept to access direct parent objects if two points are used', function (done) {
var data = {
param : 3,
subObject : {
id : 2
},
textToPrint : 'ddfdf'
};
carbone.renderXML('<xml>{d.subObject.id:ifEqual(2, ..textToPrint)}</xml>', data, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml>ddfdf</xml>');
done();
});
});
it('should accept to access direct parent objects and allow to access children objects of that parent', function (done) {
var data = {
param : 3,
subObject : {
id : 2
},
otherObj : {
textToPrint : 'ddfdf'
}
};
carbone.renderXML('<xml>{d.subObject.id:ifEqual(2, ..otherObj.textToPrint)}</xml>', data, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml>ddfdf</xml>');
done();
});
});
it('should accept to access direct parent objects and allow to access unlimited children objects of that parent', function (done) {
var data = {
param : 3,
subObject : {
id : 2
},
otherObj : {
otherObj : {
otherObj : {
textToPrint : 'ddfdf'
}
}
}
};
carbone.renderXML('<xml>{d.subObject.id:ifEqual(2, ..otherObj.otherObj.otherObj.textToPrint)}</xml>', data, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml>ddfdf</xml>');
done();
});
});
it('should not crash if object is undefined, it should return an error', function (done) {
var data = {
param : 3,
subObject : {
id : 2
},
otherObj : {
textToPrint : 'ddfdf'
}
};
carbone.renderXML('<xml>{d.subObject.id:ifEqual(2, ..otherObjNoExist.textToPrint)}</xml>', data, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml></xml>');
done();
});
});
it('should return an error if crash if object is undefined', function (done) {
var data = {
param : 3,
subObject : {
id : 2
},
otherObj : {
textToPrint : 'ddfdf'
}
};
carbone.renderXML('<xml>{d.subObject.id:ifEqual(2, ..otherObjNoExist.textToPrint)}</xml>', data, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml></xml>');
done();
});
});
it('should return an empty string if a path does not exist inside a conditional formatter', function (done) {
var data = {
obj : {
a : {
value : true
},
b : {}
},
};
carbone.renderXML('<xml>{d.obj.a.value:ifNEM():and(..b):ifNEM():show(oops):elseShow(success)}</xml>', data, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml>success</xml>');
done();
});
});
it('should return an error if crash if object is undefined', function (done) {
var data = {
param : 3,
subObject : {
id : 2
},
otherObj : [{
textToPrint : 'ddfdf'
}]
};
carbone.renderXML('<xml>{d.subObject.id:ifEqual(2, ..otherObj[0].textToPrint)}</xml>', data, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml></xml>');
done();
});
});
it.skip('should not try to reach the object attribute if the attribute is between double quotes and simple quotes', function (done) {
var data = {
param : 3,
textToPrint : 'ddfdf'
};
carbone.renderXML('<xml>{d.param:ifEqual(3, ".textToPrint")}</xml>', data, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml>.textToPrint</xml>');
carbone.renderXML('<xml>{d.param:ifEqual(3, \'.textToPrint\')}</xml>', data, function (err, result) {
helper.assert(err+'', 'null');
helper.assert(result, '<xml>.textToPrint</xml>');
done();
});
});
});
it('should accept to access direct parent objects if two points are used', function (done) {
var _xml =
'<xml>'
+ '<tr>'
+ '<td>{d[i].cars[i].wheels[i].tire.nb} {d[i].cars[i].wheels[i].tire.nb:add(..nb):add(...nb):add(....site.nb)}</td>'
+ '</tr>'
+ '<tr>'
+ '<td>{d[i+1].cars[i+1].wheels[i+1].tire.nb}</td>'
+ '</tr>'
+'</xml>';