Skip to content

Commit 9f07a34

Browse files
committed
Additional test coverage
1 parent 6f22ec1 commit 9f07a34

File tree

5 files changed

+111
-14
lines changed

5 files changed

+111
-14
lines changed

lib/handlebars/base.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ export var logger = {
219219
}
220220
};
221221

222-
export function log(level, obj) { logger.log(level, obj); }
222+
export var log = logger.log;
223223

224224
export var createFrame = function(object) {
225225
var frame = Utils.extend({}, object);

spec/blocks.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ describe('blocks', function() {
88

99
shouldCompileTo(string, {goodbyes: [], world: "world"}, "cruel world!",
1010
"Arrays ignore the contents when empty");
11+
});
1112

13+
it('array without data', function() {
14+
var string = '{{#goodbyes}}{{text}}{{/goodbyes}} {{#goodbyes}}{{text}}{{/goodbyes}}';
15+
var hash = {goodbyes: [{text: 'goodbye'}, {text: 'Goodbye'}, {text: 'GOODBYE'}], world: 'world'};
16+
shouldCompileTo(string, [hash,,,,false], 'goodbyeGoodbyeGOODBYE goodbyeGoodbyeGOODBYE');
1217
});
1318

1419
it("array with @index", function() {

spec/builtins.js

Lines changed: 90 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,29 @@ describe('builtin helpers', function() {
6666
"each with array argument ignores the contents when empty");
6767
});
6868

69+
it('each without data', function() {
70+
var string = '{{#each goodbyes}}{{text}}! {{/each}}cruel {{world}}!';
71+
var hash = {goodbyes: [{text: 'goodbye'}, {text: 'Goodbye'}, {text: 'GOODBYE'}], world: 'world'};
72+
shouldCompileTo(string, [hash,,,,false], 'goodbye! Goodbye! GOODBYE! cruel world!');
73+
74+
hash = {goodbyes: 'cruel', world: 'world'};
75+
shouldCompileTo('{{#each .}}{{.}}{{/each}}', [hash,,,,false], 'cruelworld');
76+
});
77+
78+
it('each without context', function() {
79+
var string = '{{#each goodbyes}}{{text}}! {{/each}}cruel {{world}}!';
80+
shouldCompileTo(string, [,,,,], 'cruel !');
81+
});
82+
6983
it("each with an object and @key", function() {
7084
var string = "{{#each goodbyes}}{{@key}}. {{text}}! {{/each}}cruel {{world}}!";
71-
var hash = {goodbyes: {"<b>#1</b>": {text: "goodbye"}, 2: {text: "GOODBYE"}}, world: "world"};
85+
86+
function Clazz() {
87+
this['<b>#1</b>'] = {text: 'goodbye'};
88+
this[2] = {text: 'GOODBYE'};
89+
}
90+
Clazz.prototype.foo = 'fail';
91+
var hash = {goodbyes: new Clazz(), world: 'world'};
7292

7393
// Object property iteration order is undefined according to ECMA spec,
7494
// so we need to check both possible orders
@@ -193,19 +213,77 @@ describe('builtin helpers', function() {
193213
});
194214
});
195215

196-
it("#log", function() {
197-
var string = "{{log blah}}";
198-
var hash = { blah: "whee" };
216+
describe("#log", function() {
217+
var info,
218+
error;
219+
beforeEach(function() {
220+
info = console.info;
221+
error = console.error;
222+
});
223+
afterEach(function() {
224+
console.info = info;
225+
console.error = error;
226+
});
227+
228+
it('should call logger at default level', function() {
229+
var string = "{{log blah}}";
230+
var hash = { blah: "whee" };
199231

200-
var levelArg, logArg;
201-
handlebarsEnv.log = function(level, arg){
202-
levelArg = level;
203-
logArg = arg;
204-
};
232+
var levelArg, logArg;
233+
handlebarsEnv.log = function(level, arg){
234+
levelArg = level;
235+
logArg = arg;
236+
};
205237

206-
shouldCompileTo(string, hash, "", "log should not display");
207-
equals(1, levelArg, "should call log with 1");
208-
equals("whee", logArg, "should call log with 'whee'");
238+
shouldCompileTo(string, hash, "", "log should not display");
239+
equals(1, levelArg, "should call log with 1");
240+
equals("whee", logArg, "should call log with 'whee'");
241+
});
242+
it('should call logger at data level', function() {
243+
var string = "{{log blah}}";
244+
var hash = { blah: "whee" };
245+
246+
var levelArg, logArg;
247+
handlebarsEnv.log = function(level, arg){
248+
levelArg = level;
249+
logArg = arg;
250+
};
251+
252+
shouldCompileTo(string, [hash,,,,{level: '03'}], "");
253+
equals(3, levelArg);
254+
equals("whee", logArg);
255+
});
256+
it('should not output to console', function() {
257+
var string = "{{log blah}}";
258+
var hash = { blah: "whee" };
259+
260+
console.info = function() {
261+
throw new Error();
262+
};
263+
264+
shouldCompileTo(string, hash, "", "log should not display");
265+
});
266+
it('should log at data level', function() {
267+
var string = "{{log blah}}";
268+
var hash = { blah: "whee" };
269+
var called;
270+
271+
console.error = function(log) {
272+
equals("whee", log);
273+
called = true;
274+
};
275+
276+
shouldCompileTo(string, [hash,,,,{level: '03'}], "");
277+
equals(true, called);
278+
});
279+
it('should handle missing logger', function() {
280+
var string = "{{log blah}}";
281+
var hash = { blah: "whee" };
282+
283+
console.error = undefined;
284+
285+
shouldCompileTo(string, [hash,,,,{level: '03'}], "");
286+
});
209287
});
210288

211289

spec/env/common.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ global.compileWithPartials = function(string, hashOrArray, partials) {
1919
ary.push(hashOrArray[0]);
2020
ary.push({ helpers: hashOrArray[1], partials: hashOrArray[2] });
2121
options = {compat: hashOrArray[3]};
22+
if (hashOrArray[4] != null) {
23+
options.data = !!hashOrArray[4];
24+
ary[1].data = hashOrArray[4];
25+
}
2226
} else {
2327
ary = [hashOrArray];
2428
}

spec/partials.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,17 @@ describe('partials', function() {
139139
var string = "Dudes: {{#dudes}}{{> dude}}{{/dudes}}";
140140
var partial = "";
141141
var hash = {dudes: [{name: "Yehuda", url: "http://yehuda"}, {name: "Alan", url: "http://alan"}]};
142-
shouldCompileToWithPartials(string, [hash, {}, {dude: partial}], true, "Dudes: "); });
142+
shouldCompileToWithPartials(string, [hash, {}, {dude: partial}], true, "Dudes: ");
143+
});
144+
145+
it("throw on missing partial", function() {
146+
var compile = handlebarsEnv.compile;
147+
handlebarsEnv.compile = undefined;
148+
shouldThrow(function() {
149+
shouldCompileTo('{{> dude}}', [{}, {}, {dude: 'fail'}], '');
150+
}, Error, /The partial dude could not be compiled/);
151+
handlebarsEnv.compile = compile;
152+
});
143153

144154
describe('standalone partials', function() {
145155
it("indented partials", function() {

0 commit comments

Comments
 (0)