Additional test coverage
This commit is contained in:
@@ -219,7 +219,7 @@ export var logger = {
|
||||
}
|
||||
};
|
||||
|
||||
export function log(level, obj) { logger.log(level, obj); }
|
||||
export var log = logger.log;
|
||||
|
||||
export var createFrame = function(object) {
|
||||
var frame = Utils.extend({}, object);
|
||||
|
||||
@@ -8,7 +8,12 @@ describe('blocks', function() {
|
||||
|
||||
shouldCompileTo(string, {goodbyes: [], world: "world"}, "cruel world!",
|
||||
"Arrays ignore the contents when empty");
|
||||
});
|
||||
|
||||
it('array without data', function() {
|
||||
var string = '{{#goodbyes}}{{text}}{{/goodbyes}} {{#goodbyes}}{{text}}{{/goodbyes}}';
|
||||
var hash = {goodbyes: [{text: 'goodbye'}, {text: 'Goodbye'}, {text: 'GOODBYE'}], world: 'world'};
|
||||
shouldCompileTo(string, [hash,,,,false], 'goodbyeGoodbyeGOODBYE goodbyeGoodbyeGOODBYE');
|
||||
});
|
||||
|
||||
it("array with @index", function() {
|
||||
|
||||
+90
-12
@@ -66,9 +66,29 @@ describe('builtin helpers', function() {
|
||||
"each with array argument ignores the contents when empty");
|
||||
});
|
||||
|
||||
it('each without data', function() {
|
||||
var string = '{{#each goodbyes}}{{text}}! {{/each}}cruel {{world}}!';
|
||||
var hash = {goodbyes: [{text: 'goodbye'}, {text: 'Goodbye'}, {text: 'GOODBYE'}], world: 'world'};
|
||||
shouldCompileTo(string, [hash,,,,false], 'goodbye! Goodbye! GOODBYE! cruel world!');
|
||||
|
||||
hash = {goodbyes: 'cruel', world: 'world'};
|
||||
shouldCompileTo('{{#each .}}{{.}}{{/each}}', [hash,,,,false], 'cruelworld');
|
||||
});
|
||||
|
||||
it('each without context', function() {
|
||||
var string = '{{#each goodbyes}}{{text}}! {{/each}}cruel {{world}}!';
|
||||
shouldCompileTo(string, [,,,,], 'cruel !');
|
||||
});
|
||||
|
||||
it("each with an object and @key", function() {
|
||||
var string = "{{#each goodbyes}}{{@key}}. {{text}}! {{/each}}cruel {{world}}!";
|
||||
var hash = {goodbyes: {"<b>#1</b>": {text: "goodbye"}, 2: {text: "GOODBYE"}}, world: "world"};
|
||||
|
||||
function Clazz() {
|
||||
this['<b>#1</b>'] = {text: 'goodbye'};
|
||||
this[2] = {text: 'GOODBYE'};
|
||||
}
|
||||
Clazz.prototype.foo = 'fail';
|
||||
var hash = {goodbyes: new Clazz(), world: 'world'};
|
||||
|
||||
// Object property iteration order is undefined according to ECMA spec,
|
||||
// so we need to check both possible orders
|
||||
@@ -193,19 +213,77 @@ describe('builtin helpers', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it("#log", function() {
|
||||
var string = "{{log blah}}";
|
||||
var hash = { blah: "whee" };
|
||||
describe("#log", function() {
|
||||
var info,
|
||||
error;
|
||||
beforeEach(function() {
|
||||
info = console.info;
|
||||
error = console.error;
|
||||
});
|
||||
afterEach(function() {
|
||||
console.info = info;
|
||||
console.error = error;
|
||||
});
|
||||
|
||||
var levelArg, logArg;
|
||||
handlebarsEnv.log = function(level, arg){
|
||||
levelArg = level;
|
||||
logArg = arg;
|
||||
};
|
||||
it('should call logger at default level', function() {
|
||||
var string = "{{log blah}}";
|
||||
var hash = { blah: "whee" };
|
||||
|
||||
shouldCompileTo(string, hash, "", "log should not display");
|
||||
equals(1, levelArg, "should call log with 1");
|
||||
equals("whee", logArg, "should call log with 'whee'");
|
||||
var levelArg, logArg;
|
||||
handlebarsEnv.log = function(level, arg){
|
||||
levelArg = level;
|
||||
logArg = arg;
|
||||
};
|
||||
|
||||
shouldCompileTo(string, hash, "", "log should not display");
|
||||
equals(1, levelArg, "should call log with 1");
|
||||
equals("whee", logArg, "should call log with 'whee'");
|
||||
});
|
||||
it('should call logger at data level', function() {
|
||||
var string = "{{log blah}}";
|
||||
var hash = { blah: "whee" };
|
||||
|
||||
var levelArg, logArg;
|
||||
handlebarsEnv.log = function(level, arg){
|
||||
levelArg = level;
|
||||
logArg = arg;
|
||||
};
|
||||
|
||||
shouldCompileTo(string, [hash,,,,{level: '03'}], "");
|
||||
equals(3, levelArg);
|
||||
equals("whee", logArg);
|
||||
});
|
||||
it('should not output to console', function() {
|
||||
var string = "{{log blah}}";
|
||||
var hash = { blah: "whee" };
|
||||
|
||||
console.info = function() {
|
||||
throw new Error();
|
||||
};
|
||||
|
||||
shouldCompileTo(string, hash, "", "log should not display");
|
||||
});
|
||||
it('should log at data level', function() {
|
||||
var string = "{{log blah}}";
|
||||
var hash = { blah: "whee" };
|
||||
var called;
|
||||
|
||||
console.error = function(log) {
|
||||
equals("whee", log);
|
||||
called = true;
|
||||
};
|
||||
|
||||
shouldCompileTo(string, [hash,,,,{level: '03'}], "");
|
||||
equals(true, called);
|
||||
});
|
||||
it('should handle missing logger', function() {
|
||||
var string = "{{log blah}}";
|
||||
var hash = { blah: "whee" };
|
||||
|
||||
console.error = undefined;
|
||||
|
||||
shouldCompileTo(string, [hash,,,,{level: '03'}], "");
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
Vendored
+4
@@ -19,6 +19,10 @@ global.compileWithPartials = function(string, hashOrArray, partials) {
|
||||
ary.push(hashOrArray[0]);
|
||||
ary.push({ helpers: hashOrArray[1], partials: hashOrArray[2] });
|
||||
options = {compat: hashOrArray[3]};
|
||||
if (hashOrArray[4] != null) {
|
||||
options.data = !!hashOrArray[4];
|
||||
ary[1].data = hashOrArray[4];
|
||||
}
|
||||
} else {
|
||||
ary = [hashOrArray];
|
||||
}
|
||||
|
||||
+11
-1
@@ -139,7 +139,17 @@ describe('partials', function() {
|
||||
var string = "Dudes: {{#dudes}}{{> dude}}{{/dudes}}";
|
||||
var partial = "";
|
||||
var hash = {dudes: [{name: "Yehuda", url: "http://yehuda"}, {name: "Alan", url: "http://alan"}]};
|
||||
shouldCompileToWithPartials(string, [hash, {}, {dude: partial}], true, "Dudes: "); });
|
||||
shouldCompileToWithPartials(string, [hash, {}, {dude: partial}], true, "Dudes: ");
|
||||
});
|
||||
|
||||
it("throw on missing partial", function() {
|
||||
var compile = handlebarsEnv.compile;
|
||||
handlebarsEnv.compile = undefined;
|
||||
shouldThrow(function() {
|
||||
shouldCompileTo('{{> dude}}', [{}, {}, {dude: 'fail'}], '');
|
||||
}, Error, /The partial dude could not be compiled/);
|
||||
handlebarsEnv.compile = compile;
|
||||
});
|
||||
|
||||
describe('standalone partials', function() {
|
||||
it("indented partials", function() {
|
||||
|
||||
Reference in New Issue
Block a user