-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathhandlebars_test.js
More file actions
310 lines (273 loc) · 9.94 KB
/
handlebars_test.js
File metadata and controls
310 lines (273 loc) · 9.94 KB
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
'use strict';
var vm = require('vm');
var path = require('path');
var grunt = require('grunt');
var Handlebars = require('handlebars');
// Helper for testing result of template
function testhbs(filename, fn) {
var script = vm.createScript(grunt.file.read(path.join('tmp', filename)));
fn(script.runInNewContext({
Handlebars: Handlebars,
global: {Handlebars: Handlebars},
}, path.basename(filename)));
}
// Helper for getting files without whitespace
function filesAreEqual(actual, expected, fn) {
if (typeof expected === 'function') {
fn = expected;
expected = actual;
}
fn(
String(grunt.file.read(path.join('tmp', actual))).replace(/\s/g, ''),
String(grunt.file.read(path.join('test', 'expected', expected))).replace(/\s/g, '')
);
}
exports.handlebars = {
compile: function(test) {
test.expect(1);
testhbs('handlebars.js', function(tpl) {
var actual = tpl({name: 'Dude'});
var expected = '<p>Hello, my name is Dude. I live in <span>Canada</span></p>';
test.equal(actual, expected,
'should compile partials into Handlebars.partials and handlebars template into JST');
test.done();
});
},
compileNode: function(test) {
test.expect(1);
testhbs('handlebars-node.js', function(tpl) {
var actual = tpl({name: 'Dude'});
var expected = '<p>Hello, my name is Dude. I live in <span>Canada</span></p>';
test.equal(actual, expected,
'should compile as per compile test and also have node directives prepended and appended');
test.done();
});
},
nowrap: function(test) {
test.expect(1);
filesAreEqual('handlebarsnowrap.js', function(actual, expected) {
test.equal(actual, expected,
'should compile partials into Handlebars.partials and handlebars template into JST');
test.done();
});
},
uglyfile: function(test) {
test.expect(1);
testhbs('uglyfile.js', function(tpl) {
var actual = tpl({name: 'Dude'});
var expected = 'Why would you name your file like this?';
test.equal(actual, expected, 'should escape single quotes in filenames');
test.done();
});
},
ns_nested: function(test) {
test.expect(1);
testhbs('ns_nested.js', function(tpl) {
var actual = tpl({name: 'Dude'});
var expected = 'Basic template that does nothing.';
test.equal(actual, expected, 'should define parts of nested namespaces');
test.done();
});
},
ns_nested_this: function(test) {
test.expect(1);
filesAreEqual('ns_nested_this.js', 'ns_nested.js', function(actual, expected) {
test.equal(actual, expected, 'should define parts of nested namespaces, ignoring this.');
test.done();
});
},
no_namespace:function(test) {
test.expect(1);
filesAreEqual('no_namespace.js', function(actual, expected) {
test.equal(actual, expected, 'should skip the creation of a namespace array around the generated template file');
test.done();
});
},
processcontent: function(test) {
test.expect(1);
testhbs('processcontent.js', function(tpl) {
var actual = tpl({name: 'Dude'});
var expected = '<div>\n<span>this template has many spaces</span>\n</div>';
test.equal(actual, expected, 'should remove leading and trailing spaces');
test.done();
});
},
process_ast: function(test) {
test.expect(1);
testhbs('process_ast.js', function(tpl) {
var actual = tpl({name: 'Dude'});
var expected = '<p>Hello, my name is Dude. I live in Fooville</p>';
test.equal(actual, expected, 'should allow the AST to be modified before compilation');
test.done();
});
},
amd_compile: function(test) {
test.expect(1);
filesAreEqual('amd_compile.js', function(actual, expected) {
test.equal(actual, expected, 'should wrap everything with an AMD define block.');
test.done();
});
},
amd_compile_direct: function(test) {
test.expect(1);
filesAreEqual('amd_compile_direct.js', function(actual, expected) {
test.equal(actual, expected, 'should wrap everything with an AMD define block and directly return the template.');
test.done();
});
},
amd_compile_string_path: function(test) {
test.expect(1);
filesAreEqual('amd_compile_string_path.js', function(actual, expected) {
test.equal(actual, expected, 'should wrap everything with an AMD define block and have a custom module name.');
test.done();
});
},
amd_compile_string_deps: function(test) {
test.expect(1);
filesAreEqual('amd_compile_string_deps.js', function(actual, expected) {
test.equal(actual, expected, 'should wrap everything with an AMD define block and have a custom module name.');
test.done();
});
},
amd_compile_array: function(test) {
test.expect(1);
filesAreEqual('amd_compile_array.js', function(actual, expected) {
test.equal(actual, expected, 'should wrap everything with an AMD define block and have a custom module name.');
test.done();
});
},
amd_namespace: function(test) {
test.expect(1);
filesAreEqual('amd_namespace.js', function(actual, expected) {
test.equal(actual, expected, 'should wrap everything with an AMD define block and have a custom module name.');
test.done();
});
},
amd_namespace_with_dots: function(test) {
test.expect(1);
filesAreEqual('amd_namespace_with_dots.js', function(actual, expected) {
test.equal(actual, expected, 'should wrap everything with an AMD define block and have a ' +
'custom module name with dots keeped.');
test.done();
});
},
amd_partials_use_namespace: function(test) {
test.expect(1);
filesAreEqual('amd_partials_use_namespace.js', function(actual, expected) {
test.equal(actual, expected, 'should wrap partial with an AMD define block and with a namespace.');
test.done();
});
},
amd_partials_no_namespace: function(test) {
test.expect(1);
filesAreEqual('amd_partials_no_namespace.js', function(actual, expected) {
test.equal(actual, expected, 'should wrap partial with an AMD define block and no namespace.');
test.done();
});
},
amd_namespace_as_function: function(test) {
test.expect(1);
filesAreEqual('amd_namespace_as_function.js', function(actual, expected) {
test.equal(actual, expected, 'should wrap everything with an AMD define block and have a namespace defined ' +
'with a function .');
test.done();
});
},
commonjs_compile: function(test) {
test.expect(1);
filesAreEqual('commonjs_compile.js', function(actual, expected) {
test.equal(actual, expected, 'should wrap everything with a CommonJS module.');
test.done();
});
},
commonjs_compile_direct: function(test) {
test.expect(1);
filesAreEqual('commonjs_compile_direct.js', function(actual, expected) {
test.equal(actual, expected, 'should wrap everything with a CommonJS and directly return the template.');
test.done();
});
},
custom_separator: function(test) {
test.expect(1);
filesAreEqual('custom_separator.js', function(actual, expected) {
test.equal(actual, expected, 'should use custom file separators as specified.');
test.done();
});
},
processname: function(test) {
test.expect(1);
filesAreEqual('processname.js', function(actual, expected) {
test.equal(actual, expected, 'should convert template name to upper case.');
test.done();
});
},
process_partial_name: function(test) {
test.expect(1);
testhbs('process_partial_name.js', function(tpl) {
var actual = tpl({name: 'Dude'});
var expected = '<p>Hello, my name is Dude. I live in <span>Canada</span></p>';
test.equal(actual, expected, 'should support custom handling of partial naming conventions.');
test.done();
});
},
partial_regex: function(test) {
test.expect(1);
testhbs('partial_regex.js', function(tpl) {
var actual = tpl({name: 'Dude'});
var expected = '<p>Hello, my name is Dude. I live in <span>Canada</span></p>';
test.equal(actual, expected, 'should support custom file name identifiers for partials.');
test.done();
});
},
partials_use_namespace: function(test) {
test.expect(1);
filesAreEqual('partials_use_namespace.js', function(actual, expected) {
test.equal(actual, expected, 'should allow partials to be added to template namespace.');
test.done();
});
},
partials_use_namespace_multiple_templates: function(test) {
test.expect(1);
filesAreEqual('partials_use_namespace_multiple_templates.js', function(actual, expected) {
test.equal(actual, expected, 'should allow partials to be added to template namespace when multiple templates' +
'present.');
test.done();
});
},
partials_path: function(test) {
test.expect(1);
filesAreEqual('partials_path_regex.js', function(actual, expected) {
test.equal(actual, expected, 'should support custom path to partials.');
test.done();
});
},
unknown_helpers: function(test) {
test.expect(1);
filesAreEqual('unknown_helpers.js', function(actual, expected) {
test.equal(actual, expected, 'should wrap unknown helpers by default.');
test.done();
});
},
known_helpers: function(test) {
test.expect(1);
filesAreEqual('known_helpers.js', function(actual, expected) {
test.equal(actual, expected, 'should support specifying known helpers.');
test.done();
});
},
only_known_helpers: function(test) {
test.expect(1);
filesAreEqual('only_known_helpers.js', function(actual, expected) {
test.equal(actual, expected, 'should support `knownHelpersOnly`.');
test.done();
});
},
namespace_as_function: function(test) {
test.expect(1);
filesAreEqual('namespace_as_function.js', function(actual, expected) {
test.equal(actual, expected,
'namespace should allow function to allow nested namespaces based on file system structure.');
test.done();
});
}
};