forked from node-swig/swig-templates
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtags.test.js
executable file
·60 lines (53 loc) · 1.85 KB
/
tags.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
const swig = require('../lib/swig');
const expect = require('expect.js');
describe('Tags', function () {
it('throws on unknown tags', function () {
expect(function () {
swig.render('\n \n{% foobar %}');
}).to.throwError(/Unexpected tag "foobar" on line 3\./);
});
it('throws on unexpected endtag', function () {
expect(function () {
swig.render('\n{% if foo %}\n asdf\n{% endfoo %}');
}).to.throwError(/Unexpected end of tag "foo" on line 4\./);
});
it('can have any set of tokens in end tags', function () {
expect(
swig.render(
'{% if foo %}hi!{% endif the above will render if foo == true %}',
{ locals: { foo: true } }
)
).to.equal('hi!');
});
describe('can be set', function () {
function parse (str, line, parser, types) {
return true;
}
function compile (compiler, args, content) {
return compiler(content) + '\n' + '_output += " tortilla!"';
}
it('and used in templates', function () {
swig.setTag('tortilla', parse, compile, true);
expect(swig.render('{% tortilla %}flour{% endtortilla %}')).to.equal(
'flour tortilla!'
);
});
it('and use custom extensions', function () {
swig.setExtension('tacos', function () {
return 'Tacos!';
});
swig.setTag('tacotag', parse, function (compiler, args, content) {
return '_output += _ext.tacos();\n';
});
expect(swig.render('{% tacotag %}')).to.equal('Tacos!');
});
it('and throw if are not written correctly', function () {
expect(function () {
swig.setTag('tacos', null, compile);
}).to.throwError(/Tag "tacos" parse method is not a valid function\./);
expect(function () {
swig.setTag('tacos', parse);
}).to.throwError(/Tag "tacos" compile method is not a valid function\./);
});
});
});