-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
451 lines (415 loc) · 16.8 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
var sinon = require('sinon');
var test = require('./util').test;
var fs = require('fs');
describe('file-composer tests', function () {
beforeEach(function () {
sinon.stub(fs, 'readFileSync');
sinon.stub(fs, 'writeFile');
sinon.stub(console, 'log');
});
afterEach(function () {
fs.readFileSync.restore();
fs.writeFile.restore();
console.log.restore();
});
describe('include tests', function () {
it('should generate an empty output file when the input is empty', function () {
test('', '');
});
it('should be equals the output file to the input file when there is no includes', function () {
test('Foo', 'Foo');
});
it('should include a one level file', function () {
test('Foo!include(level1)', 'FooBar', {
level1: 'Bar'
});
});
it('should include a one level empty file', function () {
test('Foo!include(level1)', 'Foo', {
level1: ''
});
});
it('should include a one level file (extra spaces are ignored)', function () {
test('Foo!include ( levelx )', 'FooBar', {
levelx: 'Bar'
});
});
it('should include twice a one level file', function () {
test('Foo!include(level1)!include(level1)', 'FooBarBar', {
level1: 'Bar'
});
});
it('should include two files of level 1', function () {
test('Foo!include(level1a)!include(level1b)', 'FooBarBaz', {
level1a: 'Bar',
level1b: 'Baz'
});
});
it('should include files of two levels', function () {
test('Foo!include(level1)', 'FooBarBaz', {
level1: 'Bar!include(level2)',
level2: 'Baz'
});
});
it('should include twice files of two levels', function () {
test('Foo!include(level1)!include(level1)', 'FooBarBazBazBarBazBaz', {
level1: 'Bar!include(level2)!include(level2)',
level2: 'Baz'
});
});
it('should include files of three levels', function () {
test('Foo!include(level1)', 'FooBarBazQux', {
level1: 'Bar!include(level2)',
level2: 'Baz!include(level3)',
level3: 'Qux'
});
});
it('should include a file of third level multiple times', function () {
test('Foo!include(level1)!include(level3)', 'FooBarBazQuxQuxQuxQux', {
level1: 'Bar!include(level2)!include(level3)!include(level3)',
level2: 'Baz!include(level3)',
level3: 'Qux'
});
});
it('should include a file of second level multiple times with three levels', function () {
test('Foo!include(level1)!include(level2)', 'FooBarBazQuxBazQuxBazQux', {
level1: 'Bar!include(level2)!include(level2)',
level2: 'Baz!include(level3)',
level3: 'Qux'
});
});
});
describe('param tests', function () {
it('should work with empty params', function () {
test('Foo!include(level1,{values:{}})', 'FooBar', {
level1: 'Bar'
});
});
it('should replace one param at first level file', function () {
test('Foo!include(level1, { values: { param1:"Baz" }})', 'FooBarBaz', {
level1: 'Bar{{ param1 }}'
});
});
it('should replace one param twice at first level file', function () {
test('Foo!include(level1, { values: { param1:"Baz" }})', 'FooBarBazBaz', {
level1: 'Bar{{ param1 }}{{param1}}'
});
});
it('should replace two params at first level file', function () {
test('Foo!include(level1, { values: { param1:"Baz", param2: "Qux" }})', 'FooBarBazQux', {
level1: 'Bar{{ param1 }}{{ param2 }}'
});
});
it('should use the default value when the param is not passed (extra spaced are ignored)', function () {
test('Foo!include(level1, { values: { param1:"Baz"}})', 'FooBarBazQux', {
level1: 'Bar{{ param1 }}{{ param2 || Qux }}'
});
});
it('should clean the param when it is not passed and there is no a default value', function () {
test('Foo!include(level1)', 'FooBar', {
level1: 'Bar{{ param1 }}'
});
});
it('should replace one param at second level file', function () {
test('Foo!include(level1)', 'FooBarBazQux', {
level1: 'Bar!include(level2, { values: { param1:"Qux" }})',
level2: 'Baz{{ param1 }}'
});
});
it('should replace when the same file is included twice with different param values', function () {
test('Foo!include(level1, { values: { param1:"Baz" }})!include(level1, { values: { param1:"Qux" }})', 'FooBarBazBarQux', {
level1: 'Bar{{ param1 }}'
});
});
it('should replace when the same file is included twice from diferents files with different param values', function () {
test('Foo!include(level1a)!include(level1b)!include(level1c)', 'FooLevel1aLevel2BarLevel1bLevel2BazLevel1cLevel2Qux', {
level1a: 'Level1a!include(level2, { values: { param1: "Bar"} })',
level1b: 'Level1b!include(level2, { values: { param1: "Baz"} })',
level1c: 'Level1c!include(level2)',
level2: 'Level2{{param1 || Qux }}'
});
});
it('should work when the parameter is passed from top level to second level (bypass the first level) ', function () {
test('Foo!include(level1, { values: { param1: "Qux"}})', 'FooBarBazQux', {
level1: 'Bar!include(level2)',
level2: 'Baz{{param1 }}'
});
});
it('should prior parameter from higher levels', function () {
test('Foo!include(level1, { values: { param1: "Baz"}})', 'FooBarBaz', {
level1: 'Bar!include(level2, { values: { param1: "Qux"}})',
level2: '{{ param1 }}'
});
});
it('should prior command line arguments', function () {
test('Foo!include(level1, { values: { param1: "Qux"}})', 'FooBarBaz', {
level1: 'Bar!include(level2)',
level2: '{ { param1 } }'
}, {
v: 'param1:Baz'
});
});
it('should replace one param inside other param at first level file', function () {
test('Foo!include(level1, { values: { param1:"Baz", param2: "{{param1}}" }})', 'FooBarBaz', {
level1: 'Bar{{ param2 }}'
});
});
it('should replace one param inside other compose param at first level file', function () {
test('Foo!include(level1, { values: { paramX:"Baz", paramY: "{{paramX}}{{paramX}}FooBar" }})', 'FooBarBazBazFooBar', {
level1: 'Bar{{ paramY || Foo }}'
});
});
it('should replace one param inside other param at second level file', function () {
test('Foo!include(level1, { values: { param1:"Baz", param2: "{{param1}}" }})', 'FooBarBaz', {
level1: '!include(level2)',
level2: 'Bar{{ param2 }}'
});
});
it('should replace one param inside other param at second level file (the inside param is defined at second level)', function () {
test('Foo!include(level1, { values: { param1:"Baz"}})', 'FooBarBaz', {
level1: '!include(level2, { values: {param2: "{{param1}}" }})',
level2: 'Bar{{ param2 }}'
});
});
it('should replace one param inside other param inside other parem at first level file', function () {
test('Foo!include(level1, { values: { param1:"Baz", param2: "{{param1}}", param3: "{{param2}}" }})', 'FooBarBaz', {
level1: 'Bar{{ param3 }}'
});
});
describe('space and lineBreak tests', function () {
it('should keep spaces of top file', function () {
test('Foo !include(level1)', 'Foo Bar', {
level1: 'Bar'
});
});
it('should keep spaces of top file in all lines', function () {
test('Foo !include(level1)', 'Foo Bar\n Baz', {
level1: 'Bar\nBaz'
});
});
it('should keep spaces of include files', function () {
test('Foo!include(level1)', 'Foo Bar', {
level1: ' Bar'
});
});
it('should keep lineBreaks of top file', function () {
test('Foo\n!include(level1)', 'Foo\nBar', {
level1: 'Bar'
});
});
it('should keep lineBreaks of include files', function () {
test('Foo!include(level1)', 'Foo\nBar', {
level1: '\nBar'
});
});
it('should add spaces acording to spaces parameter', function () {
test('Foo!include(level1, {spaces: 1})', 'Foo Bar', {
level1: 'Bar'
});
});
it('should add spaces to all lines acording to spaces parameter', function () {
test('Foo!include(level1, {spaces: 2})', 'Foo Bar\n Baz', {
level1: 'Bar\nBaz'
});
});
it('should add lineBreak lines acording to lineBreaks parameter', function () {
test('Foo!include(level1, {lineBreaks: 1})', 'Foo\nBar', {
level1: 'Bar'
});
});
it('should add lineBreak lines only at begining of the included file', function () {
test('Foo!include(level1, {lineBreaks: 1})', 'Foo\nBar\nBaz', {
level1: 'Bar\nBaz'
});
});
it('should add lineBreak lines and spaces in one case', function () {
test('Foo!include(level1, {spaces: 1, lineBreaks: 1})', 'Foo\n Bar', {
level1: 'Bar'
});
});
it('should add lineBreak lines and spaces in multilevel case', function () {
test('Foo!include(level1, {spaces: 1, lineBreaks: 1})', 'Foo\n Bar\n Baz', {
level1: 'Bar!include(level2, {spaces: 1, lineBreaks: 1})',
level2: 'Baz'
});
});
it('should add params to existing spaces in all lines', function () {
test('Foo\n !include(level1, {spaces: 2})', 'Foo\n Bar\n Baz', {
level1: 'Bar\nBaz'
});
});
});
describe('times tests', function () {
it('should repeat the include twice', function () {
test('Foo!include(level1, {times: 2})', 'FooBarBar', {
level1: 'Bar'
});
});
it('should repeat the include four times', function () {
test('Foo!include(level1, {times: 4})', 'FooBarBarBarBar', {
level1: 'Bar'
});
});
it('should repeat spaces and lineBreaks twice', function () {
test('Foo!include(level1, {times: 2, spaces: 2, lineBreaks: 1})', 'Foo\n Bar\n Bar', {
level1: 'Bar'
});
});
it('should repeat twice include files of three levels', function () {
test('Foo!include(level1, {times: 2})', 'FooBarBazQuxBarBazQux', {
level1: 'Bar!include(level2)',
level2: 'Baz!include(level3)',
level3: 'Qux'
});
});
it('should repeat twice include files of three levels (with times:2 parameter)', function () {
test('Foo!include(level1, {times: 2})', 'FooBarBazQuxQuxBazQuxQuxBarBazQuxQuxBazQuxQux', {
level1: 'Bar!include(level2, {times: 2})',
level2: 'Baz!include(level3, {times: 2})',
level3: 'Qux'
});
});
it('should replace twice the include param', function () {
test('Foo!include(level1, {times:2, values: { param1:"Baz" }})', 'FooBarBazBarBaz', {
level1: 'Bar{{ param1 }}'
});
});
});
describe('param expression tests', function () {
it('should eval a simple expression', function () {
test('Foo!eval(1 + 4)_eval', 'Foo5');
});
it('should eval a complex arithmetic expression', function () {
test('Foo!eval(2 * (4 + 1) )_eval', 'Foo10');
});
it('should eval a simple function expression call', function () {
test('Foo!eval((function() { return 1 + 4 ; })() )_eval', 'Foo5');
});
it('should eval two simple expressions', function () {
test('Foo!eval(1 + 4)_eval!eval(3 * 5)_eval', 'Foo515');
});
it('should eval core JavaScript functions', function () {
test('Foo!eval([1, 2, 3].join(""))_eval', 'Foo123');
});
it('should eval in one level expressions', function () {
test('Foo !include(level1)', 'Foo 6', {
level1: '!eval(2 + 4)_eval'
});
});
it('should eval in one level expressions with params', function () {
test('Foo !include(level1, { values: {value1: 1, value2: 5} })', 'Foo 6', {
level1: '!eval({{ value1 }} + {{ value2 }})_eval'
});
});
});
describe('param function generator', function () {
it('should include values until the generator is exhausted', function () {
test('Foo!include(level1, { values: {fruit: "{{!fruitGen}}" }})', 'FooAppleOrangePear', {
level1: '{{fruit}}'
});
});
it('can receive parameters', function () {
test('Foo!include(level1, { values: {fruit: "{{!each Apple Orange Pear}}" }})', 'FooAppleOrangePear', {
level1: '{{fruit}}'
});
});
it('can receive parameters at second level', function () {
test('Foo!include(level1, { values: {fruit: "{{!each Apple Orange Pear}}" }})', 'FooAppleOrangePear', {
level1: '!include(level2)',
level2: '{{fruit}}'
});
});
it('can use evaluable expressions', function () {
test('Foo!include(level1, { values: {fruit: "{{!each Apple Orange Pear}}" }})', 'FooApple1Orange1Pear1', {
level1: '!include(level2)',
level2: '!eval("{{fruit}}1")_eval'
});
});
it('can receive parameterized parameters at second level', function () {
test('Foo!include(level1, {values: { anotherFruit: "Melon" } })', 'FooAppleOrangePearMelon', {
level1: '!include(level2, { values: {fruit: "{{!each Apple Orange Pear {{anotherFruit}} }}" }})',
level2: '{{fruit}}'
});
});
it('should use multiple gens in the same level', function () {
test('Foo!include(level1, { values: {fruit: "{{!each Apple Orange Pear}}{{!each 1 2 3}}" }})', 'FooAppleOrangePear123', {
level1: '{{fruit}}'
});
});
it('should make a cross product when multiple generators are used in different levels (version 1)', function () {
test('Foo!include(level1, { values: {fruit: "{{!each Apple Orange Pear}}" }})', 'FooApple123Orange123Pear123', {
level1: '{{fruit}}!include(level2, { values: {number: "{{!each 1 2 3}}" }})',
level2: '{{number}}'
});
});
it('should make a cross product when multiple generators are used in different levels (version 2)', function () {
test('Foo!include(level1, { values: {fruit: "{{!each Apple Orange Pear}}" }})', 'FooApple1Apple2Apple3Orange1Orange2Orange3Pear1Pear2Pear3', {
level1: '!include(level2, { values: {number: "{{!each 1 2 3}}" }})',
level2: '{{fruit}}{{number}}'
});
});
});
});
describe('test conditional include with if', function () {
it('should not include when "if" parameter is false', function () {
test('Foo!include(level1, { if: false })', 'Foo', {
level1: 'Bar'
});
});
it('should not include when "if" parameter is true', function () {
test('Foo!include(level1, { if: true })', 'FooBar', {
level1: 'Bar'
});
});
it('should not include when "if" expression is false', function () {
test('Foo!include(level1, { if: "4 * 2 % 2 !== 0" })', 'Foo', {
level1: 'Bar'
});
});
it('should include when "if" expression is true', function () {
test('Foo!include(level1, { if: "4 * 2 % 2 === 0" })', 'FooBar', {
level1: 'Bar'
});
});
it('should not include when "if" expression is falsy', function () {
test('Foo!include(level1, { if: "NaN" })', 'Foo', {
level1: 'Bar'
});
});
it('should include when "if" expression is truthy', function () {
test('Foo!include(level1, { if: 1 })', 'FooBar', {
level1: 'Bar'
});
});
it('should not include when "if" is a false param value', function () {
test('Foo!include(level1, { if: "{{inc}}", values: { inc: "false"}})', 'Foo', {
level1: 'Bar'
});
});
it('should include when "if" is a true param value', function () {
test('Foo!include(level1, { if: "{{inc}}", values: { inc: "true"}})', 'FooBar', {
level1: 'Bar'
});
});
it('should not include when "if" is a faly param value', function () {
test('Foo!include(level1, { if: "{{inc}}", values: { inc: "NaN"}})', 'Foo', {
level1: 'Bar'
});
});
it('should include when "if" is a truthy param value', function () {
test('Foo!include(level1, { if: "{{inc}}", values: { inc: 1}})', 'FooBar', {
level1: 'Bar'
});
});
it('should work with generator values', function () {
test('Foo!include(level1, { if: "{{number}} % 2 == 0", values: { number: "{{!each 1 2 3 4 5 6 7 8 9 10}}" }})', 'FooBar2Bar4Bar6Bar8Bar10', {
level1: 'Bar{{number}}'
});
});
it('should work with generator values and multiple params', function () {
test('Foo!include(level1, { if: "{{number}} % {{div}} == 0", values: {div: 2, number: "{{!each 1 2 3 4 5 6 7 8 9 10}}" }})', 'FooBar2Bar4Bar6Bar8Bar10', {
level1: 'Bar{{number}}'
});
});
});
});