-
Notifications
You must be signed in to change notification settings - Fork 2
/
control.js
67 lines (55 loc) · 2.31 KB
/
control.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
const assert = require('assert');
const { take } = require('./utils');
const { toAST } = require('../src');
describe('toAST', () => {
describe('# control tags', () => {
it('should handle if statements', () => {
const ast = toAST(`
Hello {% if isUser %}User{% endif %}
`);
assert.equal(take(ast, 1, 'name'), 'if');
assert.equal(take(ast, 1, 0, 0, 'name'), 'isUser');
assert.equal(take(ast, 1, 1, 0, 'value'), 'User');
})
it('should handle self closing statements', () => {
const ast = toAST(`
Hello {% set username = "JorgenEvens" %}
`);
assert.equal(take(ast, 1, 'name'), 'set');
assert.strictEqual(take(ast, 1, 'closing'), undefined);
assert.equal(take(ast, 1, 0, 2, 0, 'value'), 'JorgenEvens');
})
it('should handle else / elseif statements', () => {
const ast = toAST(`
{% if isUser %}
Hello User
{% elseif isVisitor %}
Hello Visitor
{% else %}
Hello Anonymous
{% endif %}
`);
assert.equal(take(ast, 1, 'name'), 'if');
assert.equal(take(ast, 1, 0, 0, 'name'), 'isUser');
assert.equal(take(ast, 1, 1, 0, 'value').trim(), 'Hello User');
assert.equal(take(ast, 1, 2, 'name'), 'elseif');
assert.equal(take(ast, 1, 2, 0, 0, 'name'), 'isVisitor');
assert.equal(take(ast, 1, 2, 1, 0, 'value').trim(), 'Hello Visitor');
assert.equal(take(ast, 1, 3, 'name'), 'else');
assert.equal(take(ast, 1, 3, 0, 0, 'value').trim(), 'Hello Anonymous');
})
})
it('should handle for loop iterating both keys and values', () => {
const ast = toAST(`
{% for key, value in array %}
Hello Test
{% endfor %}
`);
assert.equal(take(ast, 1, 'name'), 'for');
assert.equal(take(ast, 1, 0, 0, 'name'), 'key');
assert.equal(take(ast, 1, 1, 0, 'name'), 'value');
assert.equal(take(ast, 1, 1, 1, 'value'), 'in');
assert.equal(take(ast, 1, 1, 2, 0, 'name'), 'array');
assert.equal(take(ast, 1, 2, 'type'), 'BLOCK');
});
});