forked from paularmstrong/swig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
155 lines (130 loc) · 4.11 KB
/
index.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
var swig = require('./index'),
testCase = require('nodeunit').testCase;
exports.init = testCase({
'custom filtersm': function (test) {
swig.init({ filters: {
foo: function (input) {
return 'bar';
}
}});
var tpl = swig.compile('{{ asdf|foo }}');
test.strictEqual(tpl({ asdf: 'blah' }), 'bar');
test.done();
},
'allowErrors = false': function (test) {
swig.init({
root: __dirname + '/tests/templates',
allowErrors: false
});
var tpl = swig.compileFile('foobar.html');
test.ok((/<pre>Error\: ENOENT, no such file or directory/i).test(tpl.render()), 'pushes a render function with the error');
tpl = swig.compileFile('includes_notfound.html');
test.ok((/<pre>Error\: ENOENT, no such file or directory/i).test(tpl.render()), 'renders the error when includes a file that is not found');
test.done();
},
'allowErrors = true': function (test) {
swig.init({ allowErrors: true });
test.throws(function () {
swig.compileFile('barfoo.html');
}, 'throws when allowErrors is true');
test.done();
},
'custom extensions': function (test) {
test.expect(1);
swig.init({
allowErrors: true,
extensions: { foobar: function () { test.ok(true); } },
tags: {
foo: function (indent) {
return '_ext.foobar();';
}
}
});
var tpl = swig.compile('{% foo %}');
tpl();
test.done();
}
});
exports.compileFile = testCase({
setUp: function (callback) {
callback();
},
basic: function (test) {
swig.init({
root: __dirname + '/tests/templates',
allowErrors: true
});
var tpl = swig.compileFile('included_2.html');
test.strictEqual('2', tpl.render({ array: [1, 1] }), 'from file is a-ok');
test.done();
},
'absolute path': function (test) {
swig.init({
root: __dirname + '/tests/templates',
allowErrors: true
});
var tpl = swig.compileFile('/' + __dirname + '/tests/templates/included_2.html');
test.strictEqual('2', tpl.render({ array: [1, 1] }), 'file from absolute path is a-ok');
test.done();
},
'throws on window': function (test) {
swig.init({});
global.window = true;
test.throws(function () {
swig.compileFile('foobar');
});
delete global.window;
test.done();
},
'render without context': function (test) {
test.strictEqual(swig.compile('{% set foo = "foo" %}{{ foo }}')(), 'foo', 'this should not throw');
test.done();
}
});
exports.Errors = testCase({
'allow - parse error': function (test) {
swig.init({ allowErrors: true });
test.throws(function () {
swig.compile('{% for foo in blah %}{% endif %}');
}, Error);
test.done();
},
'allow - compile error': function (test) {
swig.init({
allowErrors: true,
tags: { foo: function (indent) {
return 'blargh;';
}}
});
var tpl = swig.compile('{% foo %}');
test.throws(function () {
tpl({});
}, Error);
test.done();
},
'disallow - parse error': function (test) {
swig.init({});
test.doesNotThrow(function () {
swig.compile('{% for foo in bar %}{% endif %}');
}, Error);
test.done();
},
'disallow - compile error': function (test) {
swig.init({
tags: { foobar: function (indent) {
return 'blargh;';
}}
});
var tpl = swig.compile('{% foobar %}');
test.doesNotThrow(function () {
tpl({});
}, Error);
test.done();
}
});
exports['double-escape forward-slash'] = function (test) {
swig.init({});
var tpl = swig.compile('foobar\\/');
test.strictEqual(tpl({}), 'foobar\\/');
test.done();
};