refator: In spec tests, use expectTemplate over equals and shouldThrow (#1683)
This commit is contained in:
+387
-399
@@ -6,117 +6,156 @@ beforeEach(function() {
|
|||||||
|
|
||||||
describe('basic context', function() {
|
describe('basic context', function() {
|
||||||
it('most basic', function() {
|
it('most basic', function() {
|
||||||
shouldCompileTo('{{foo}}', { foo: 'foo' }, 'foo');
|
expectTemplate('{{foo}}')
|
||||||
|
.withInput({ foo: 'foo' })
|
||||||
|
.toCompileTo('foo');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('escaping', function() {
|
it('escaping', function() {
|
||||||
shouldCompileTo('\\{{foo}}', { foo: 'food' }, '{{foo}}');
|
expectTemplate('\\{{foo}}')
|
||||||
shouldCompileTo('content \\{{foo}}', { foo: 'food' }, 'content {{foo}}');
|
.withInput({ foo: 'food' })
|
||||||
shouldCompileTo('\\\\{{foo}}', { foo: 'food' }, '\\food');
|
.toCompileTo('{{foo}}');
|
||||||
shouldCompileTo('content \\\\{{foo}}', { foo: 'food' }, 'content \\food');
|
|
||||||
shouldCompileTo('\\\\ {{foo}}', { foo: 'food' }, '\\\\ food');
|
expectTemplate('content \\{{foo}}')
|
||||||
|
.withInput({ foo: 'food' })
|
||||||
|
.toCompileTo('content {{foo}}');
|
||||||
|
|
||||||
|
expectTemplate('\\\\{{foo}}')
|
||||||
|
.withInput({ foo: 'food' })
|
||||||
|
.toCompileTo('\\food');
|
||||||
|
|
||||||
|
expectTemplate('content \\\\{{foo}}')
|
||||||
|
.withInput({ foo: 'food' })
|
||||||
|
.toCompileTo('content \\food');
|
||||||
|
|
||||||
|
expectTemplate('\\\\ {{foo}}')
|
||||||
|
.withInput({ foo: 'food' })
|
||||||
|
.toCompileTo('\\\\ food');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('compiling with a basic context', function() {
|
it('compiling with a basic context', function() {
|
||||||
shouldCompileTo(
|
expectTemplate('Goodbye\n{{cruel}}\n{{world}}!')
|
||||||
'Goodbye\n{{cruel}}\n{{world}}!',
|
.withInput({
|
||||||
{ cruel: 'cruel', world: 'world' },
|
cruel: 'cruel',
|
||||||
'Goodbye\ncruel\nworld!',
|
world: 'world'
|
||||||
'It works if all the required keys are provided'
|
})
|
||||||
);
|
.withMessage('It works if all the required keys are provided')
|
||||||
|
.toCompileTo('Goodbye\ncruel\nworld!');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('compiling with a string context', function() {
|
it('compiling with a string context', function() {
|
||||||
shouldCompileTo('{{.}}{{length}}', 'bye', 'bye3');
|
expectTemplate('{{.}}{{length}}')
|
||||||
|
.withInput('bye')
|
||||||
|
.toCompileTo('bye3');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('compiling with an undefined context', function() {
|
it('compiling with an undefined context', function() {
|
||||||
shouldCompileTo(
|
expectTemplate('Goodbye\n{{cruel}}\n{{world.bar}}!')
|
||||||
'Goodbye\n{{cruel}}\n{{world.bar}}!',
|
.withInput(undefined)
|
||||||
undefined,
|
.toCompileTo('Goodbye\n\n!');
|
||||||
'Goodbye\n\n!'
|
|
||||||
);
|
|
||||||
|
|
||||||
shouldCompileTo(
|
expectTemplate('{{#unless foo}}Goodbye{{../test}}{{test2}}{{/unless}}')
|
||||||
'{{#unless foo}}Goodbye{{../test}}{{test2}}{{/unless}}',
|
.withInput(undefined)
|
||||||
undefined,
|
.toCompileTo('Goodbye');
|
||||||
'Goodbye'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('comments', function() {
|
it('comments', function() {
|
||||||
shouldCompileTo(
|
expectTemplate('{{! Goodbye}}Goodbye\n{{cruel}}\n{{world}}!')
|
||||||
'{{! Goodbye}}Goodbye\n{{cruel}}\n{{world}}!',
|
.withInput({
|
||||||
{ cruel: 'cruel', world: 'world' },
|
cruel: 'cruel',
|
||||||
'Goodbye\ncruel\nworld!',
|
world: 'world'
|
||||||
'comments are ignored'
|
})
|
||||||
|
.withMessage('comments are ignored')
|
||||||
|
.toCompileTo('Goodbye\ncruel\nworld!');
|
||||||
|
|
||||||
|
expectTemplate(' {{~! comment ~}} blah').toCompileTo('blah');
|
||||||
|
|
||||||
|
expectTemplate(' {{~!-- long-comment --~}} blah').toCompileTo(
|
||||||
|
'blah'
|
||||||
);
|
);
|
||||||
|
|
||||||
shouldCompileTo(' {{~! comment ~}} blah', {}, 'blah');
|
expectTemplate(' {{! comment ~}} blah').toCompileTo(' blah');
|
||||||
shouldCompileTo(' {{~!-- long-comment --~}} blah', {}, 'blah');
|
|
||||||
shouldCompileTo(' {{! comment ~}} blah', {}, ' blah');
|
expectTemplate(' {{!-- long-comment --~}} blah').toCompileTo(
|
||||||
shouldCompileTo(' {{!-- long-comment --~}} blah', {}, ' blah');
|
' blah'
|
||||||
shouldCompileTo(' {{~! comment}} blah', {}, ' blah');
|
);
|
||||||
shouldCompileTo(' {{~!-- long-comment --}} blah', {}, ' blah');
|
|
||||||
|
expectTemplate(' {{~! comment}} blah').toCompileTo(' blah');
|
||||||
|
|
||||||
|
expectTemplate(' {{~!-- long-comment --}} blah').toCompileTo(
|
||||||
|
' blah'
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('boolean', function() {
|
it('boolean', function() {
|
||||||
var string = '{{#goodbye}}GOODBYE {{/goodbye}}cruel {{world}}!';
|
var string = '{{#goodbye}}GOODBYE {{/goodbye}}cruel {{world}}!';
|
||||||
shouldCompileTo(
|
expectTemplate(string)
|
||||||
string,
|
.withInput({
|
||||||
{ goodbye: true, world: 'world' },
|
goodbye: true,
|
||||||
'GOODBYE cruel world!',
|
world: 'world'
|
||||||
'booleans show the contents when true'
|
})
|
||||||
);
|
.withMessage('booleans show the contents when true')
|
||||||
|
.toCompileTo('GOODBYE cruel world!');
|
||||||
|
|
||||||
shouldCompileTo(
|
expectTemplate(string)
|
||||||
string,
|
.withInput({
|
||||||
{ goodbye: false, world: 'world' },
|
goodbye: false,
|
||||||
'cruel world!',
|
world: 'world'
|
||||||
'booleans do not show the contents when false'
|
})
|
||||||
);
|
.withMessage('booleans do not show the contents when false')
|
||||||
|
.toCompileTo('cruel world!');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('zeros', function() {
|
it('zeros', function() {
|
||||||
shouldCompileTo(
|
expectTemplate('num1: {{num1}}, num2: {{num2}}')
|
||||||
'num1: {{num1}}, num2: {{num2}}',
|
.withInput({
|
||||||
{ num1: 42, num2: 0 },
|
num1: 42,
|
||||||
'num1: 42, num2: 0'
|
num2: 0
|
||||||
);
|
})
|
||||||
shouldCompileTo('num: {{.}}', 0, 'num: 0');
|
.toCompileTo('num1: 42, num2: 0');
|
||||||
shouldCompileTo('num: {{num1/num2}}', { num1: { num2: 0 } }, 'num: 0');
|
|
||||||
|
expectTemplate('num: {{.}}')
|
||||||
|
.withInput(0)
|
||||||
|
.toCompileTo('num: 0');
|
||||||
|
|
||||||
|
expectTemplate('num: {{num1/num2}}')
|
||||||
|
.withInput({ num1: { num2: 0 } })
|
||||||
|
.toCompileTo('num: 0');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('false', function() {
|
it('false', function() {
|
||||||
/* eslint-disable no-new-wrappers */
|
/* eslint-disable no-new-wrappers */
|
||||||
shouldCompileTo(
|
expectTemplate('val1: {{val1}}, val2: {{val2}}')
|
||||||
'val1: {{val1}}, val2: {{val2}}',
|
.withInput({
|
||||||
{ val1: false, val2: new Boolean(false) },
|
val1: false,
|
||||||
'val1: false, val2: false'
|
val2: new Boolean(false)
|
||||||
);
|
})
|
||||||
shouldCompileTo('val: {{.}}', false, 'val: false');
|
.toCompileTo('val1: false, val2: false');
|
||||||
shouldCompileTo(
|
|
||||||
'val: {{val1/val2}}',
|
|
||||||
{ val1: { val2: false } },
|
|
||||||
'val: false'
|
|
||||||
);
|
|
||||||
|
|
||||||
shouldCompileTo(
|
expectTemplate('val: {{.}}')
|
||||||
'val1: {{{val1}}}, val2: {{{val2}}}',
|
.withInput(false)
|
||||||
{ val1: false, val2: new Boolean(false) },
|
.toCompileTo('val: false');
|
||||||
'val1: false, val2: false'
|
|
||||||
);
|
expectTemplate('val: {{val1/val2}}')
|
||||||
shouldCompileTo(
|
.withInput({ val1: { val2: false } })
|
||||||
'val: {{{val1/val2}}}',
|
.toCompileTo('val: false');
|
||||||
{ val1: { val2: false } },
|
|
||||||
'val: false'
|
expectTemplate('val1: {{{val1}}}, val2: {{{val2}}}')
|
||||||
);
|
.withInput({
|
||||||
|
val1: false,
|
||||||
|
val2: new Boolean(false)
|
||||||
|
})
|
||||||
|
.toCompileTo('val1: false, val2: false');
|
||||||
|
|
||||||
|
expectTemplate('val: {{{val1/val2}}}')
|
||||||
|
.withInput({ val1: { val2: false } })
|
||||||
|
.toCompileTo('val: false');
|
||||||
/* eslint-enable */
|
/* eslint-enable */
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle undefined and null', function() {
|
it('should handle undefined and null', function() {
|
||||||
shouldCompileTo(
|
expectTemplate('{{awesome undefined null}}')
|
||||||
'{{awesome undefined null}}',
|
.withInput({
|
||||||
{
|
|
||||||
awesome: function(_undefined, _null, options) {
|
awesome: function(_undefined, _null, options) {
|
||||||
return (
|
return (
|
||||||
(_undefined === undefined) +
|
(_undefined === undefined) +
|
||||||
@@ -126,373 +165,325 @@ describe('basic context', function() {
|
|||||||
typeof options
|
typeof options
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
},
|
})
|
||||||
'true true object'
|
.toCompileTo('true true object');
|
||||||
);
|
|
||||||
shouldCompileTo(
|
expectTemplate('{{undefined}}')
|
||||||
'{{undefined}}',
|
.withInput({
|
||||||
{
|
|
||||||
undefined: function() {
|
undefined: function() {
|
||||||
return 'undefined!';
|
return 'undefined!';
|
||||||
}
|
}
|
||||||
},
|
})
|
||||||
'undefined!'
|
.toCompileTo('undefined!');
|
||||||
);
|
|
||||||
shouldCompileTo(
|
expectTemplate('{{null}}')
|
||||||
'{{null}}',
|
.withInput({
|
||||||
{
|
|
||||||
null: function() {
|
null: function() {
|
||||||
return 'null!';
|
return 'null!';
|
||||||
}
|
}
|
||||||
},
|
})
|
||||||
'null!'
|
.toCompileTo('null!');
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('newlines', function() {
|
it('newlines', function() {
|
||||||
shouldCompileTo("Alan's\nTest", {}, "Alan's\nTest");
|
expectTemplate("Alan's\nTest").toCompileTo("Alan's\nTest");
|
||||||
shouldCompileTo("Alan's\rTest", {}, "Alan's\rTest");
|
|
||||||
|
expectTemplate("Alan's\rTest").toCompileTo("Alan's\rTest");
|
||||||
});
|
});
|
||||||
|
|
||||||
it('escaping text', function() {
|
it('escaping text', function() {
|
||||||
shouldCompileTo(
|
expectTemplate("Awesome's")
|
||||||
"Awesome's",
|
.withMessage(
|
||||||
{},
|
"text is escaped so that it doesn't get caught on single quotes"
|
||||||
"Awesome's",
|
)
|
||||||
"text is escaped so that it doesn't get caught on single quotes"
|
.toCompileTo("Awesome's");
|
||||||
);
|
|
||||||
shouldCompileTo(
|
expectTemplate('Awesome\\')
|
||||||
'Awesome\\',
|
.withMessage("text is escaped so that the closing quote can't be ignored")
|
||||||
{},
|
.toCompileTo('Awesome\\');
|
||||||
'Awesome\\',
|
|
||||||
"text is escaped so that the closing quote can't be ignored"
|
expectTemplate('Awesome\\\\ foo')
|
||||||
);
|
.withMessage("text is escaped so that it doesn't mess up backslashes")
|
||||||
shouldCompileTo(
|
.toCompileTo('Awesome\\\\ foo');
|
||||||
'Awesome\\\\ foo',
|
|
||||||
{},
|
expectTemplate('Awesome {{foo}}')
|
||||||
'Awesome\\\\ foo',
|
.withInput({ foo: '\\' })
|
||||||
"text is escaped so that it doesn't mess up backslashes"
|
.withMessage("text is escaped so that it doesn't mess up backslashes")
|
||||||
);
|
.toCompileTo('Awesome \\');
|
||||||
shouldCompileTo(
|
|
||||||
'Awesome {{foo}}',
|
expectTemplate(" ' ' ")
|
||||||
{ foo: '\\' },
|
.withMessage('double quotes never produce invalid javascript')
|
||||||
'Awesome \\',
|
.toCompileTo(" ' ' ");
|
||||||
"text is escaped so that it doesn't mess up backslashes"
|
|
||||||
);
|
|
||||||
shouldCompileTo(
|
|
||||||
" ' ' ",
|
|
||||||
{},
|
|
||||||
" ' ' ",
|
|
||||||
'double quotes never produce invalid javascript'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('escaping expressions', function() {
|
it('escaping expressions', function() {
|
||||||
shouldCompileTo(
|
expectTemplate('{{{awesome}}}')
|
||||||
'{{{awesome}}}',
|
.withInput({ awesome: "&'\\<>" })
|
||||||
{ awesome: "&'\\<>" },
|
.withMessage("expressions with 3 handlebars aren't escaped")
|
||||||
"&'\\<>",
|
.toCompileTo("&'\\<>");
|
||||||
"expressions with 3 handlebars aren't escaped"
|
|
||||||
);
|
|
||||||
|
|
||||||
shouldCompileTo(
|
expectTemplate('{{&awesome}}')
|
||||||
'{{&awesome}}',
|
.withInput({ awesome: "&'\\<>" })
|
||||||
{ awesome: "&'\\<>" },
|
.withMessage("expressions with {{& handlebars aren't escaped")
|
||||||
"&'\\<>",
|
.toCompileTo("&'\\<>");
|
||||||
"expressions with {{& handlebars aren't escaped"
|
|
||||||
);
|
|
||||||
|
|
||||||
shouldCompileTo(
|
expectTemplate('{{awesome}}')
|
||||||
'{{awesome}}',
|
.withInput({ awesome: '&"\'`\\<>' })
|
||||||
{ awesome: '&"\'`\\<>' },
|
.withMessage('by default expressions should be escaped')
|
||||||
'&"'`\\<>',
|
.toCompileTo('&"'`\\<>');
|
||||||
'by default expressions should be escaped'
|
|
||||||
);
|
|
||||||
|
|
||||||
shouldCompileTo(
|
expectTemplate('{{awesome}}')
|
||||||
'{{awesome}}',
|
.withInput({ awesome: 'Escaped, <b> looks like: <b>' })
|
||||||
{ awesome: 'Escaped, <b> looks like: <b>' },
|
.withMessage('escaping should properly handle amperstands')
|
||||||
'Escaped, <b> looks like: &lt;b&gt;',
|
.toCompileTo('Escaped, <b> looks like: &lt;b&gt;');
|
||||||
'escaping should properly handle amperstands'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("functions returning safestrings shouldn't be escaped", function() {
|
it("functions returning safestrings shouldn't be escaped", function() {
|
||||||
var hash = {
|
expectTemplate('{{awesome}}')
|
||||||
awesome: function() {
|
.withInput({
|
||||||
return new Handlebars.SafeString("&'\\<>");
|
awesome: function() {
|
||||||
}
|
return new Handlebars.SafeString("&'\\<>");
|
||||||
};
|
}
|
||||||
shouldCompileTo(
|
})
|
||||||
'{{awesome}}',
|
.withMessage("functions returning safestrings aren't escaped")
|
||||||
hash,
|
.toCompileTo("&'\\<>");
|
||||||
"&'\\<>",
|
|
||||||
"functions returning safestrings aren't escaped"
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('functions', function() {
|
it('functions', function() {
|
||||||
shouldCompileTo(
|
expectTemplate('{{awesome}}')
|
||||||
'{{awesome}}',
|
.withInput({
|
||||||
{
|
|
||||||
awesome: function() {
|
awesome: function() {
|
||||||
return 'Awesome';
|
return 'Awesome';
|
||||||
}
|
}
|
||||||
},
|
})
|
||||||
'Awesome',
|
.withMessage('functions are called and render their output')
|
||||||
'functions are called and render their output'
|
.toCompileTo('Awesome');
|
||||||
);
|
|
||||||
shouldCompileTo(
|
expectTemplate('{{awesome}}')
|
||||||
'{{awesome}}',
|
.withInput({
|
||||||
{
|
|
||||||
awesome: function() {
|
awesome: function() {
|
||||||
return this.more;
|
return this.more;
|
||||||
},
|
},
|
||||||
more: 'More awesome'
|
more: 'More awesome'
|
||||||
},
|
})
|
||||||
'More awesome',
|
.withMessage('functions are bound to the context')
|
||||||
'functions are bound to the context'
|
.toCompileTo('More awesome');
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('functions with context argument', function() {
|
it('functions with context argument', function() {
|
||||||
shouldCompileTo(
|
expectTemplate('{{awesome frank}}')
|
||||||
'{{awesome frank}}',
|
.withInput({
|
||||||
{
|
|
||||||
awesome: function(context) {
|
awesome: function(context) {
|
||||||
return context;
|
return context;
|
||||||
},
|
},
|
||||||
frank: 'Frank'
|
frank: 'Frank'
|
||||||
},
|
})
|
||||||
'Frank',
|
.withMessage('functions are called with context arguments')
|
||||||
'functions are called with context arguments'
|
.toCompileTo('Frank');
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('pathed functions with context argument', function() {
|
it('pathed functions with context argument', function() {
|
||||||
shouldCompileTo(
|
expectTemplate('{{bar.awesome frank}}')
|
||||||
'{{bar.awesome frank}}',
|
.withInput({
|
||||||
{
|
|
||||||
bar: {
|
bar: {
|
||||||
awesome: function(context) {
|
awesome: function(context) {
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
frank: 'Frank'
|
frank: 'Frank'
|
||||||
},
|
})
|
||||||
'Frank',
|
.withMessage('functions are called with context arguments')
|
||||||
'functions are called with context arguments'
|
.toCompileTo('Frank');
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('depthed functions with context argument', function() {
|
it('depthed functions with context argument', function() {
|
||||||
shouldCompileTo(
|
expectTemplate('{{#with frank}}{{../awesome .}}{{/with}}')
|
||||||
'{{#with frank}}{{../awesome .}}{{/with}}',
|
.withInput({
|
||||||
{
|
|
||||||
awesome: function(context) {
|
awesome: function(context) {
|
||||||
return context;
|
return context;
|
||||||
},
|
},
|
||||||
frank: 'Frank'
|
frank: 'Frank'
|
||||||
},
|
})
|
||||||
'Frank',
|
.withMessage('functions are called with context arguments')
|
||||||
'functions are called with context arguments'
|
.toCompileTo('Frank');
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('block functions with context argument', function() {
|
it('block functions with context argument', function() {
|
||||||
shouldCompileTo(
|
expectTemplate('{{#awesome 1}}inner {{.}}{{/awesome}}')
|
||||||
'{{#awesome 1}}inner {{.}}{{/awesome}}',
|
.withInput({
|
||||||
{
|
|
||||||
awesome: function(context, options) {
|
awesome: function(context, options) {
|
||||||
return options.fn(context);
|
return options.fn(context);
|
||||||
}
|
}
|
||||||
},
|
})
|
||||||
'inner 1',
|
.withMessage('block functions are called with context and options')
|
||||||
'block functions are called with context and options'
|
.toCompileTo('inner 1');
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('depthed block functions with context argument', function() {
|
it('depthed block functions with context argument', function() {
|
||||||
shouldCompileTo(
|
expectTemplate(
|
||||||
'{{#with value}}{{#../awesome 1}}inner {{.}}{{/../awesome}}{{/with}}',
|
'{{#with value}}{{#../awesome 1}}inner {{.}}{{/../awesome}}{{/with}}'
|
||||||
{
|
)
|
||||||
|
.withInput({
|
||||||
value: true,
|
value: true,
|
||||||
awesome: function(context, options) {
|
awesome: function(context, options) {
|
||||||
return options.fn(context);
|
return options.fn(context);
|
||||||
}
|
}
|
||||||
},
|
})
|
||||||
'inner 1',
|
.withMessage('block functions are called with context and options')
|
||||||
'block functions are called with context and options'
|
.toCompileTo('inner 1');
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('block functions without context argument', function() {
|
it('block functions without context argument', function() {
|
||||||
shouldCompileTo(
|
expectTemplate('{{#awesome}}inner{{/awesome}}')
|
||||||
'{{#awesome}}inner{{/awesome}}',
|
.withInput({
|
||||||
{
|
|
||||||
awesome: function(options) {
|
awesome: function(options) {
|
||||||
return options.fn(this);
|
return options.fn(this);
|
||||||
}
|
}
|
||||||
},
|
})
|
||||||
'inner',
|
.withMessage('block functions are called with options')
|
||||||
'block functions are called with options'
|
.toCompileTo('inner');
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('pathed block functions without context argument', function() {
|
it('pathed block functions without context argument', function() {
|
||||||
shouldCompileTo(
|
expectTemplate('{{#foo.awesome}}inner{{/foo.awesome}}')
|
||||||
'{{#foo.awesome}}inner{{/foo.awesome}}',
|
.withInput({
|
||||||
{
|
|
||||||
foo: {
|
foo: {
|
||||||
awesome: function() {
|
awesome: function() {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
})
|
||||||
'inner',
|
.withMessage('block functions are called with options')
|
||||||
'block functions are called with options'
|
.toCompileTo('inner');
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('depthed block functions without context argument', function() {
|
it('depthed block functions without context argument', function() {
|
||||||
shouldCompileTo(
|
expectTemplate(
|
||||||
'{{#with value}}{{#../awesome}}inner{{/../awesome}}{{/with}}',
|
'{{#with value}}{{#../awesome}}inner{{/../awesome}}{{/with}}'
|
||||||
{
|
)
|
||||||
|
.withInput({
|
||||||
value: true,
|
value: true,
|
||||||
awesome: function() {
|
awesome: function() {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
},
|
})
|
||||||
'inner',
|
.withMessage('block functions are called with options')
|
||||||
'block functions are called with options'
|
.toCompileTo('inner');
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('paths with hyphens', function() {
|
it('paths with hyphens', function() {
|
||||||
shouldCompileTo(
|
expectTemplate('{{foo-bar}}')
|
||||||
'{{foo-bar}}',
|
.withInput({ 'foo-bar': 'baz' })
|
||||||
{ 'foo-bar': 'baz' },
|
.withMessage('Paths can contain hyphens (-)')
|
||||||
'baz',
|
.toCompileTo('baz');
|
||||||
'Paths can contain hyphens (-)'
|
|
||||||
);
|
expectTemplate('{{foo.foo-bar}}')
|
||||||
shouldCompileTo(
|
.withInput({ foo: { 'foo-bar': 'baz' } })
|
||||||
'{{foo.foo-bar}}',
|
.withMessage('Paths can contain hyphens (-)')
|
||||||
{ foo: { 'foo-bar': 'baz' } },
|
.toCompileTo('baz');
|
||||||
'baz',
|
|
||||||
'Paths can contain hyphens (-)'
|
expectTemplate('{{foo/foo-bar}}')
|
||||||
);
|
.withInput({ foo: { 'foo-bar': 'baz' } })
|
||||||
shouldCompileTo(
|
.withMessage('Paths can contain hyphens (-)')
|
||||||
'{{foo/foo-bar}}',
|
.toCompileTo('baz');
|
||||||
{ foo: { 'foo-bar': 'baz' } },
|
|
||||||
'baz',
|
|
||||||
'Paths can contain hyphens (-)'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('nested paths', function() {
|
it('nested paths', function() {
|
||||||
shouldCompileTo(
|
expectTemplate('Goodbye {{alan/expression}} world!')
|
||||||
'Goodbye {{alan/expression}} world!',
|
.withInput({ alan: { expression: 'beautiful' } })
|
||||||
{ alan: { expression: 'beautiful' } },
|
.withMessage('Nested paths access nested objects')
|
||||||
'Goodbye beautiful world!',
|
.toCompileTo('Goodbye beautiful world!');
|
||||||
'Nested paths access nested objects'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('nested paths with empty string value', function() {
|
it('nested paths with empty string value', function() {
|
||||||
shouldCompileTo(
|
expectTemplate('Goodbye {{alan/expression}} world!')
|
||||||
'Goodbye {{alan/expression}} world!',
|
.withInput({ alan: { expression: '' } })
|
||||||
{ alan: { expression: '' } },
|
.withMessage('Nested paths access nested objects with empty string')
|
||||||
'Goodbye world!',
|
.toCompileTo('Goodbye world!');
|
||||||
'Nested paths access nested objects with empty string'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('literal paths', function() {
|
it('literal paths', function() {
|
||||||
shouldCompileTo(
|
expectTemplate('Goodbye {{[@alan]/expression}} world!')
|
||||||
'Goodbye {{[@alan]/expression}} world!',
|
.withInput({ '@alan': { expression: 'beautiful' } })
|
||||||
{ '@alan': { expression: 'beautiful' } },
|
.withMessage('Literal paths can be used')
|
||||||
'Goodbye beautiful world!',
|
.toCompileTo('Goodbye beautiful world!');
|
||||||
'Literal paths can be used'
|
|
||||||
);
|
expectTemplate('Goodbye {{[foo bar]/expression}} world!')
|
||||||
shouldCompileTo(
|
.withInput({ 'foo bar': { expression: 'beautiful' } })
|
||||||
'Goodbye {{[foo bar]/expression}} world!',
|
.withMessage('Literal paths can be used')
|
||||||
{ 'foo bar': { expression: 'beautiful' } },
|
.toCompileTo('Goodbye beautiful world!');
|
||||||
'Goodbye beautiful world!',
|
|
||||||
'Literal paths can be used'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('literal references', function() {
|
it('literal references', function() {
|
||||||
shouldCompileTo(
|
expectTemplate('Goodbye {{[foo bar]}} world!')
|
||||||
'Goodbye {{[foo bar]}} world!',
|
.withInput({ 'foo bar': 'beautiful' })
|
||||||
{ 'foo bar': 'beautiful' },
|
.toCompileTo('Goodbye beautiful world!');
|
||||||
'Goodbye beautiful world!'
|
|
||||||
);
|
expectTemplate('Goodbye {{"foo bar"}} world!')
|
||||||
shouldCompileTo(
|
.withInput({ 'foo bar': 'beautiful' })
|
||||||
'Goodbye {{"foo bar"}} world!',
|
.toCompileTo('Goodbye beautiful world!');
|
||||||
{ 'foo bar': 'beautiful' },
|
|
||||||
'Goodbye beautiful world!'
|
expectTemplate("Goodbye {{'foo bar'}} world!")
|
||||||
);
|
.withInput({ 'foo bar': 'beautiful' })
|
||||||
shouldCompileTo(
|
.toCompileTo('Goodbye beautiful world!');
|
||||||
"Goodbye {{'foo bar'}} world!",
|
|
||||||
{ 'foo bar': 'beautiful' },
|
expectTemplate('Goodbye {{"foo[bar"}} world!')
|
||||||
'Goodbye beautiful world!'
|
.withInput({ 'foo[bar': 'beautiful' })
|
||||||
);
|
.toCompileTo('Goodbye beautiful world!');
|
||||||
shouldCompileTo(
|
|
||||||
'Goodbye {{"foo[bar"}} world!',
|
expectTemplate('Goodbye {{"foo\'bar"}} world!')
|
||||||
{ 'foo[bar': 'beautiful' },
|
.withInput({ "foo'bar": 'beautiful' })
|
||||||
'Goodbye beautiful world!'
|
.toCompileTo('Goodbye beautiful world!');
|
||||||
);
|
|
||||||
shouldCompileTo(
|
expectTemplate("Goodbye {{'foo\"bar'}} world!")
|
||||||
'Goodbye {{"foo\'bar"}} world!',
|
.withInput({ 'foo"bar': 'beautiful' })
|
||||||
{ "foo'bar": 'beautiful' },
|
.toCompileTo('Goodbye beautiful world!');
|
||||||
'Goodbye beautiful world!'
|
|
||||||
);
|
|
||||||
shouldCompileTo(
|
|
||||||
"Goodbye {{'foo\"bar'}} world!",
|
|
||||||
{ 'foo"bar': 'beautiful' },
|
|
||||||
'Goodbye beautiful world!'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("that current context path ({{.}}) doesn't hit helpers", function() {
|
it("that current context path ({{.}}) doesn't hit helpers", function() {
|
||||||
shouldCompileTo('test: {{.}}', [null, { helper: 'awesome' }], 'test: ');
|
expectTemplate('test: {{.}}')
|
||||||
|
.withInput(null)
|
||||||
|
.withHelpers({ helper: 'awesome' })
|
||||||
|
.toCompileTo('test: ');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('complex but empty paths', function() {
|
it('complex but empty paths', function() {
|
||||||
shouldCompileTo('{{person/name}}', { person: { name: null } }, '');
|
expectTemplate('{{person/name}}')
|
||||||
shouldCompileTo('{{person/name}}', { person: {} }, '');
|
.withInput({ person: { name: null } })
|
||||||
|
.toCompileTo('');
|
||||||
|
|
||||||
|
expectTemplate('{{person/name}}')
|
||||||
|
.withInput({ person: {} })
|
||||||
|
.toCompileTo('');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('this keyword in paths', function() {
|
it('this keyword in paths', function() {
|
||||||
var string = '{{#goodbyes}}{{this}}{{/goodbyes}}';
|
expectTemplate('{{#goodbyes}}{{this}}{{/goodbyes}}')
|
||||||
var hash = { goodbyes: ['goodbye', 'Goodbye', 'GOODBYE'] };
|
.withInput({ goodbyes: ['goodbye', 'Goodbye', 'GOODBYE'] })
|
||||||
shouldCompileTo(
|
.withMessage('This keyword in paths evaluates to current context')
|
||||||
string,
|
.toCompileTo('goodbyeGoodbyeGOODBYE');
|
||||||
hash,
|
|
||||||
'goodbyeGoodbyeGOODBYE',
|
|
||||||
'This keyword in paths evaluates to current context'
|
|
||||||
);
|
|
||||||
|
|
||||||
string = '{{#hellos}}{{this/text}}{{/hellos}}';
|
expectTemplate('{{#hellos}}{{this/text}}{{/hellos}}')
|
||||||
hash = {
|
.withInput({
|
||||||
hellos: [{ text: 'hello' }, { text: 'Hello' }, { text: 'HELLO' }]
|
hellos: [{ text: 'hello' }, { text: 'Hello' }, { text: 'HELLO' }]
|
||||||
};
|
})
|
||||||
shouldCompileTo(
|
.withMessage('This keyword evaluates in more complex paths')
|
||||||
string,
|
.toCompileTo('helloHelloHELLO');
|
||||||
hash,
|
|
||||||
'helloHelloHELLO',
|
|
||||||
'This keyword evaluates in more complex paths'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('this keyword nested inside path', function() {
|
it('this keyword nested inside path', function() {
|
||||||
shouldThrow(
|
expectTemplate('{{#hellos}}{{text/this/foo}}{{/hellos}}').toThrow(
|
||||||
function() {
|
|
||||||
CompilerContext.compile('{{#hellos}}{{text/this/foo}}{{/hellos}}');
|
|
||||||
},
|
|
||||||
Error,
|
Error,
|
||||||
'Invalid path: text/this - 1:13'
|
'Invalid path: text/this - 1:13'
|
||||||
);
|
);
|
||||||
|
|
||||||
shouldCompileTo('{{[this]}}', { this: 'bar' }, 'bar');
|
expectTemplate('{{[this]}}')
|
||||||
shouldCompileTo('{{text/[this]}}', { text: { this: 'bar' } }, 'bar');
|
.withInput({ this: 'bar' })
|
||||||
|
.toCompileTo('bar');
|
||||||
|
|
||||||
|
expectTemplate('{{text/[this]}}')
|
||||||
|
.withInput({ text: { this: 'bar' } })
|
||||||
|
.toCompileTo('bar');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('this keyword in helpers', function() {
|
it('this keyword in helpers', function() {
|
||||||
@@ -501,108 +492,105 @@ describe('basic context', function() {
|
|||||||
return 'bar ' + value;
|
return 'bar ' + value;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
var string = '{{#goodbyes}}{{foo this}}{{/goodbyes}}';
|
|
||||||
var hash = { goodbyes: ['goodbye', 'Goodbye', 'GOODBYE'] };
|
|
||||||
shouldCompileTo(
|
|
||||||
string,
|
|
||||||
[hash, helpers],
|
|
||||||
'bar goodbyebar Goodbyebar GOODBYE',
|
|
||||||
'This keyword in paths evaluates to current context'
|
|
||||||
);
|
|
||||||
|
|
||||||
string = '{{#hellos}}{{foo this/text}}{{/hellos}}';
|
expectTemplate('{{#goodbyes}}{{foo this}}{{/goodbyes}}')
|
||||||
hash = {
|
.withInput({ goodbyes: ['goodbye', 'Goodbye', 'GOODBYE'] })
|
||||||
hellos: [{ text: 'hello' }, { text: 'Hello' }, { text: 'HELLO' }]
|
.withHelpers(helpers)
|
||||||
};
|
.withMessage('This keyword in paths evaluates to current context')
|
||||||
shouldCompileTo(
|
.toCompileTo('bar goodbyebar Goodbyebar GOODBYE');
|
||||||
string,
|
|
||||||
[hash, helpers],
|
expectTemplate('{{#hellos}}{{foo this/text}}{{/hellos}}')
|
||||||
'bar hellobar Hellobar HELLO',
|
.withInput({
|
||||||
'This keyword evaluates in more complex paths'
|
hellos: [{ text: 'hello' }, { text: 'Hello' }, { text: 'HELLO' }]
|
||||||
);
|
})
|
||||||
|
.withHelpers(helpers)
|
||||||
|
.withMessage('This keyword evaluates in more complex paths')
|
||||||
|
.toCompileTo('bar hellobar Hellobar HELLO');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('this keyword nested inside helpers param', function() {
|
it('this keyword nested inside helpers param', function() {
|
||||||
var string = '{{#hellos}}{{foo text/this/foo}}{{/hellos}}';
|
expectTemplate('{{#hellos}}{{foo text/this/foo}}{{/hellos}}').toThrow(
|
||||||
shouldThrow(
|
|
||||||
function() {
|
|
||||||
CompilerContext.compile(string);
|
|
||||||
},
|
|
||||||
Error,
|
Error,
|
||||||
'Invalid path: text/this - 1:17'
|
'Invalid path: text/this - 1:17'
|
||||||
);
|
);
|
||||||
|
|
||||||
shouldCompileTo(
|
expectTemplate('{{foo [this]}}')
|
||||||
'{{foo [this]}}',
|
.withInput({
|
||||||
{
|
|
||||||
foo: function(value) {
|
foo: function(value) {
|
||||||
return value;
|
return value;
|
||||||
},
|
},
|
||||||
this: 'bar'
|
this: 'bar'
|
||||||
},
|
})
|
||||||
'bar'
|
.toCompileTo('bar');
|
||||||
);
|
|
||||||
shouldCompileTo(
|
expectTemplate('{{foo text/[this]}}')
|
||||||
'{{foo text/[this]}}',
|
.withInput({
|
||||||
{
|
|
||||||
foo: function(value) {
|
foo: function(value) {
|
||||||
return value;
|
return value;
|
||||||
},
|
},
|
||||||
text: { this: 'bar' }
|
text: { this: 'bar' }
|
||||||
},
|
})
|
||||||
'bar'
|
.toCompileTo('bar');
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('pass string literals', function() {
|
it('pass string literals', function() {
|
||||||
shouldCompileTo('{{"foo"}}', {}, '');
|
expectTemplate('{{"foo"}}').toCompileTo('');
|
||||||
shouldCompileTo('{{"foo"}}', { foo: 'bar' }, 'bar');
|
|
||||||
shouldCompileTo(
|
expectTemplate('{{"foo"}}')
|
||||||
'{{#"foo"}}{{.}}{{/"foo"}}',
|
.withInput({ foo: 'bar' })
|
||||||
{ foo: ['bar', 'baz'] },
|
.toCompileTo('bar');
|
||||||
'barbaz'
|
|
||||||
);
|
expectTemplate('{{#"foo"}}{{.}}{{/"foo"}}')
|
||||||
|
.withInput({
|
||||||
|
foo: ['bar', 'baz']
|
||||||
|
})
|
||||||
|
.toCompileTo('barbaz');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('pass number literals', function() {
|
it('pass number literals', function() {
|
||||||
shouldCompileTo('{{12}}', {}, '');
|
expectTemplate('{{12}}').toCompileTo('');
|
||||||
shouldCompileTo('{{12}}', { '12': 'bar' }, 'bar');
|
|
||||||
shouldCompileTo('{{12.34}}', {}, '');
|
expectTemplate('{{12}}')
|
||||||
shouldCompileTo('{{12.34}}', { '12.34': 'bar' }, 'bar');
|
.withInput({ '12': 'bar' })
|
||||||
shouldCompileTo(
|
.toCompileTo('bar');
|
||||||
'{{12.34 1}}',
|
|
||||||
{
|
expectTemplate('{{12.34}}').toCompileTo('');
|
||||||
|
|
||||||
|
expectTemplate('{{12.34}}')
|
||||||
|
.withInput({ '12.34': 'bar' })
|
||||||
|
.toCompileTo('bar');
|
||||||
|
|
||||||
|
expectTemplate('{{12.34 1}}')
|
||||||
|
.withInput({
|
||||||
'12.34': function(arg) {
|
'12.34': function(arg) {
|
||||||
return 'bar' + arg;
|
return 'bar' + arg;
|
||||||
}
|
}
|
||||||
},
|
})
|
||||||
'bar1'
|
.toCompileTo('bar1');
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('pass boolean literals', function() {
|
it('pass boolean literals', function() {
|
||||||
shouldCompileTo('{{true}}', {}, '');
|
expectTemplate('{{true}}').toCompileTo('');
|
||||||
shouldCompileTo('{{true}}', { '': 'foo' }, '');
|
|
||||||
shouldCompileTo('{{false}}', { false: 'foo' }, 'foo');
|
expectTemplate('{{true}}')
|
||||||
|
.withInput({ '': 'foo' })
|
||||||
|
.toCompileTo('');
|
||||||
|
|
||||||
|
expectTemplate('{{false}}')
|
||||||
|
.withInput({ false: 'foo' })
|
||||||
|
.toCompileTo('foo');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle literals in subexpression', function() {
|
it('should handle literals in subexpression', function() {
|
||||||
var helpers = {
|
expectTemplate('{{foo (false)}}')
|
||||||
foo: function(arg) {
|
.withInput({
|
||||||
|
false: function() {
|
||||||
|
return 'bar';
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.withHelper('foo', function(arg) {
|
||||||
return arg;
|
return arg;
|
||||||
}
|
})
|
||||||
};
|
.toCompileTo('bar');
|
||||||
shouldCompileTo(
|
|
||||||
'{{foo (false)}}',
|
|
||||||
[
|
|
||||||
{
|
|
||||||
false: function() {
|
|
||||||
return 'bar';
|
|
||||||
}
|
|
||||||
},
|
|
||||||
helpers
|
|
||||||
],
|
|
||||||
'bar'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+286
-335
@@ -1,455 +1,405 @@
|
|||||||
describe('blocks', function() {
|
describe('blocks', function() {
|
||||||
it('array', function() {
|
it('array', function() {
|
||||||
var string = '{{#goodbyes}}{{text}}! {{/goodbyes}}cruel {{world}}!';
|
var string = '{{#goodbyes}}{{text}}! {{/goodbyes}}cruel {{world}}!';
|
||||||
var hash = {
|
|
||||||
goodbyes: [{ text: 'goodbye' }, { text: 'Goodbye' }, { text: 'GOODBYE' }],
|
|
||||||
world: 'world'
|
|
||||||
};
|
|
||||||
shouldCompileTo(
|
|
||||||
string,
|
|
||||||
hash,
|
|
||||||
'goodbye! Goodbye! GOODBYE! cruel world!',
|
|
||||||
'Arrays iterate over the contents when not empty'
|
|
||||||
);
|
|
||||||
|
|
||||||
shouldCompileTo(
|
expectTemplate(string)
|
||||||
string,
|
.withInput({
|
||||||
{ goodbyes: [], world: 'world' },
|
goodbyes: [
|
||||||
'cruel world!',
|
{ text: 'goodbye' },
|
||||||
'Arrays ignore the contents when empty'
|
{ text: 'Goodbye' },
|
||||||
);
|
{ text: 'GOODBYE' }
|
||||||
|
],
|
||||||
|
world: 'world'
|
||||||
|
})
|
||||||
|
.withMessage('Arrays iterate over the contents when not empty')
|
||||||
|
.toCompileTo('goodbye! Goodbye! GOODBYE! cruel world!');
|
||||||
|
|
||||||
|
expectTemplate(string)
|
||||||
|
.withInput({
|
||||||
|
goodbyes: [],
|
||||||
|
world: 'world'
|
||||||
|
})
|
||||||
|
.withMessage('Arrays ignore the contents when empty')
|
||||||
|
.toCompileTo('cruel world!');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('array without data', function() {
|
it('array without data', function() {
|
||||||
var string =
|
expectTemplate(
|
||||||
'{{#goodbyes}}{{text}}{{/goodbyes}} {{#goodbyes}}{{text}}{{/goodbyes}}';
|
'{{#goodbyes}}{{text}}{{/goodbyes}} {{#goodbyes}}{{text}}{{/goodbyes}}'
|
||||||
var hash = {
|
)
|
||||||
goodbyes: [{ text: 'goodbye' }, { text: 'Goodbye' }, { text: 'GOODBYE' }],
|
.withInput({
|
||||||
world: 'world'
|
goodbyes: [
|
||||||
};
|
{ text: 'goodbye' },
|
||||||
shouldCompileTo(
|
{ text: 'Goodbye' },
|
||||||
string,
|
{ text: 'GOODBYE' }
|
||||||
[hash, , , false],
|
],
|
||||||
'goodbyeGoodbyeGOODBYE goodbyeGoodbyeGOODBYE'
|
world: 'world'
|
||||||
);
|
})
|
||||||
|
.withCompileOptions({ compat: false })
|
||||||
|
.toCompileTo('goodbyeGoodbyeGOODBYE goodbyeGoodbyeGOODBYE');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('array with @index', function() {
|
it('array with @index', function() {
|
||||||
var string =
|
expectTemplate(
|
||||||
'{{#goodbyes}}{{@index}}. {{text}}! {{/goodbyes}}cruel {{world}}!';
|
'{{#goodbyes}}{{@index}}. {{text}}! {{/goodbyes}}cruel {{world}}!'
|
||||||
var hash = {
|
)
|
||||||
goodbyes: [{ text: 'goodbye' }, { text: 'Goodbye' }, { text: 'GOODBYE' }],
|
.withInput({
|
||||||
world: 'world'
|
goodbyes: [
|
||||||
};
|
{ text: 'goodbye' },
|
||||||
|
{ text: 'Goodbye' },
|
||||||
var template = CompilerContext.compile(string);
|
{ text: 'GOODBYE' }
|
||||||
var result = template(hash);
|
],
|
||||||
|
world: 'world'
|
||||||
equal(
|
})
|
||||||
result,
|
.withMessage('The @index variable is used')
|
||||||
'0. goodbye! 1. Goodbye! 2. GOODBYE! cruel world!',
|
.toCompileTo('0. goodbye! 1. Goodbye! 2. GOODBYE! cruel world!');
|
||||||
'The @index variable is used'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('empty block', function() {
|
it('empty block', function() {
|
||||||
var string = '{{#goodbyes}}{{/goodbyes}}cruel {{world}}!';
|
var string = '{{#goodbyes}}{{/goodbyes}}cruel {{world}}!';
|
||||||
var hash = {
|
|
||||||
goodbyes: [{ text: 'goodbye' }, { text: 'Goodbye' }, { text: 'GOODBYE' }],
|
|
||||||
world: 'world'
|
|
||||||
};
|
|
||||||
shouldCompileTo(
|
|
||||||
string,
|
|
||||||
hash,
|
|
||||||
'cruel world!',
|
|
||||||
'Arrays iterate over the contents when not empty'
|
|
||||||
);
|
|
||||||
|
|
||||||
shouldCompileTo(
|
expectTemplate(string)
|
||||||
string,
|
.withInput({
|
||||||
{ goodbyes: [], world: 'world' },
|
goodbyes: [
|
||||||
'cruel world!',
|
{ text: 'goodbye' },
|
||||||
'Arrays ignore the contents when empty'
|
{ text: 'Goodbye' },
|
||||||
);
|
{ text: 'GOODBYE' }
|
||||||
|
],
|
||||||
|
world: 'world'
|
||||||
|
})
|
||||||
|
.withMessage('Arrays iterate over the contents when not empty')
|
||||||
|
.toCompileTo('cruel world!');
|
||||||
|
|
||||||
|
expectTemplate(string)
|
||||||
|
.withInput({
|
||||||
|
goodbyes: [],
|
||||||
|
world: 'world'
|
||||||
|
})
|
||||||
|
.withMessage('Arrays ignore the contents when empty')
|
||||||
|
.toCompileTo('cruel world!');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('block with complex lookup', function() {
|
it('block with complex lookup', function() {
|
||||||
var string = '{{#goodbyes}}{{text}} cruel {{../name}}! {{/goodbyes}}';
|
expectTemplate('{{#goodbyes}}{{text}} cruel {{../name}}! {{/goodbyes}}')
|
||||||
var hash = {
|
.withInput({
|
||||||
name: 'Alan',
|
name: 'Alan',
|
||||||
goodbyes: [{ text: 'goodbye' }, { text: 'Goodbye' }, { text: 'GOODBYE' }]
|
goodbyes: [
|
||||||
};
|
{ text: 'goodbye' },
|
||||||
|
{ text: 'Goodbye' },
|
||||||
shouldCompileTo(
|
{ text: 'GOODBYE' }
|
||||||
string,
|
]
|
||||||
hash,
|
})
|
||||||
'goodbye cruel Alan! Goodbye cruel Alan! GOODBYE cruel Alan! ',
|
.withMessage(
|
||||||
'Templates can access variables in contexts up the stack with relative path syntax'
|
'Templates can access variables in contexts up the stack with relative path syntax'
|
||||||
);
|
)
|
||||||
|
.toCompileTo(
|
||||||
|
'goodbye cruel Alan! Goodbye cruel Alan! GOODBYE cruel Alan! '
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('multiple blocks with complex lookup', function() {
|
it('multiple blocks with complex lookup', function() {
|
||||||
var string = '{{#goodbyes}}{{../name}}{{../name}}{{/goodbyes}}';
|
expectTemplate('{{#goodbyes}}{{../name}}{{../name}}{{/goodbyes}}')
|
||||||
var hash = {
|
.withInput({
|
||||||
name: 'Alan',
|
name: 'Alan',
|
||||||
goodbyes: [{ text: 'goodbye' }, { text: 'Goodbye' }, { text: 'GOODBYE' }]
|
goodbyes: [
|
||||||
};
|
{ text: 'goodbye' },
|
||||||
|
{ text: 'Goodbye' },
|
||||||
shouldCompileTo(string, hash, 'AlanAlanAlanAlanAlanAlan');
|
{ text: 'GOODBYE' }
|
||||||
|
]
|
||||||
|
})
|
||||||
|
.toCompileTo('AlanAlanAlanAlanAlanAlan');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('block with complex lookup using nested context', function() {
|
it('block with complex lookup using nested context', function() {
|
||||||
var string = '{{#goodbyes}}{{text}} cruel {{foo/../name}}! {{/goodbyes}}';
|
expectTemplate(
|
||||||
|
'{{#goodbyes}}{{text}} cruel {{foo/../name}}! {{/goodbyes}}'
|
||||||
shouldThrow(function() {
|
).toThrow(Error);
|
||||||
CompilerContext.compile(string);
|
|
||||||
}, Error);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('block with deep nested complex lookup', function() {
|
it('block with deep nested complex lookup', function() {
|
||||||
var string =
|
expectTemplate(
|
||||||
'{{#outer}}Goodbye {{#inner}}cruel {{../sibling}} {{../../omg}}{{/inner}}{{/outer}}';
|
'{{#outer}}Goodbye {{#inner}}cruel {{../sibling}} {{../../omg}}{{/inner}}{{/outer}}'
|
||||||
var hash = {
|
)
|
||||||
omg: 'OMG!',
|
.withInput({
|
||||||
outer: [{ sibling: 'sad', inner: [{ text: 'goodbye' }] }]
|
omg: 'OMG!',
|
||||||
};
|
outer: [{ sibling: 'sad', inner: [{ text: 'goodbye' }] }]
|
||||||
|
})
|
||||||
shouldCompileTo(string, hash, 'Goodbye cruel sad OMG!');
|
.toCompileTo('Goodbye cruel sad OMG!');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('works with cached blocks', function() {
|
it('works with cached blocks', function() {
|
||||||
var template = CompilerContext.compile(
|
expectTemplate(
|
||||||
'{{#each person}}{{#with .}}{{first}} {{last}}{{/with}}{{/each}}',
|
'{{#each person}}{{#with .}}{{first}} {{last}}{{/with}}{{/each}}'
|
||||||
{ data: false }
|
)
|
||||||
);
|
.withCompileOptions({ data: false })
|
||||||
|
.withInput({
|
||||||
var result = template({
|
person: [
|
||||||
person: [
|
{ first: 'Alan', last: 'Johnson' },
|
||||||
{ first: 'Alan', last: 'Johnson' },
|
{ first: 'Alan', last: 'Johnson' }
|
||||||
{ first: 'Alan', last: 'Johnson' }
|
]
|
||||||
]
|
})
|
||||||
});
|
.toCompileTo('Alan JohnsonAlan Johnson');
|
||||||
equals(result, 'Alan JohnsonAlan Johnson');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('inverted sections', function() {
|
describe('inverted sections', function() {
|
||||||
it('inverted sections with unset value', function() {
|
it('inverted sections with unset value', function() {
|
||||||
var string =
|
expectTemplate(
|
||||||
'{{#goodbyes}}{{this}}{{/goodbyes}}{{^goodbyes}}Right On!{{/goodbyes}}';
|
'{{#goodbyes}}{{this}}{{/goodbyes}}{{^goodbyes}}Right On!{{/goodbyes}}'
|
||||||
var hash = {};
|
)
|
||||||
shouldCompileTo(
|
.withMessage("Inverted section rendered when value isn't set.")
|
||||||
string,
|
.toCompileTo('Right On!');
|
||||||
hash,
|
|
||||||
'Right On!',
|
|
||||||
"Inverted section rendered when value isn't set."
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('inverted section with false value', function() {
|
it('inverted section with false value', function() {
|
||||||
var string =
|
expectTemplate(
|
||||||
'{{#goodbyes}}{{this}}{{/goodbyes}}{{^goodbyes}}Right On!{{/goodbyes}}';
|
'{{#goodbyes}}{{this}}{{/goodbyes}}{{^goodbyes}}Right On!{{/goodbyes}}'
|
||||||
var hash = { goodbyes: false };
|
)
|
||||||
shouldCompileTo(
|
.withInput({ goodbyes: false })
|
||||||
string,
|
.withMessage('Inverted section rendered when value is false.')
|
||||||
hash,
|
.toCompileTo('Right On!');
|
||||||
'Right On!',
|
|
||||||
'Inverted section rendered when value is false.'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('inverted section with empty set', function() {
|
it('inverted section with empty set', function() {
|
||||||
var string =
|
expectTemplate(
|
||||||
'{{#goodbyes}}{{this}}{{/goodbyes}}{{^goodbyes}}Right On!{{/goodbyes}}';
|
'{{#goodbyes}}{{this}}{{/goodbyes}}{{^goodbyes}}Right On!{{/goodbyes}}'
|
||||||
var hash = { goodbyes: [] };
|
)
|
||||||
shouldCompileTo(
|
.withInput({ goodbyes: [] })
|
||||||
string,
|
.withMessage('Inverted section rendered when value is empty set.')
|
||||||
hash,
|
.toCompileTo('Right On!');
|
||||||
'Right On!',
|
|
||||||
'Inverted section rendered when value is empty set.'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('block inverted sections', function() {
|
it('block inverted sections', function() {
|
||||||
shouldCompileTo(
|
expectTemplate('{{#people}}{{name}}{{^}}{{none}}{{/people}}')
|
||||||
'{{#people}}{{name}}{{^}}{{none}}{{/people}}',
|
.withInput({ none: 'No people' })
|
||||||
{ none: 'No people' },
|
.toCompileTo('No people');
|
||||||
'No people'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('chained inverted sections', function() {
|
it('chained inverted sections', function() {
|
||||||
shouldCompileTo(
|
expectTemplate('{{#people}}{{name}}{{else if none}}{{none}}{{/people}}')
|
||||||
'{{#people}}{{name}}{{else if none}}{{none}}{{/people}}',
|
.withInput({ none: 'No people' })
|
||||||
{ none: 'No people' },
|
.toCompileTo('No people');
|
||||||
'No people'
|
|
||||||
);
|
expectTemplate(
|
||||||
shouldCompileTo(
|
'{{#people}}{{name}}{{else if nothere}}fail{{else unless nothere}}{{none}}{{/people}}'
|
||||||
'{{#people}}{{name}}{{else if nothere}}fail{{else unless nothere}}{{none}}{{/people}}',
|
)
|
||||||
{ none: 'No people' },
|
.withInput({ none: 'No people' })
|
||||||
'No people'
|
.toCompileTo('No people');
|
||||||
);
|
|
||||||
shouldCompileTo(
|
expectTemplate(
|
||||||
'{{#people}}{{name}}{{else if none}}{{none}}{{else}}fail{{/people}}',
|
'{{#people}}{{name}}{{else if none}}{{none}}{{else}}fail{{/people}}'
|
||||||
{ none: 'No people' },
|
)
|
||||||
'No people'
|
.withInput({ none: 'No people' })
|
||||||
);
|
.toCompileTo('No people');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('chained inverted sections with mismatch', function() {
|
it('chained inverted sections with mismatch', function() {
|
||||||
shouldThrow(function() {
|
expectTemplate(
|
||||||
shouldCompileTo(
|
'{{#people}}{{name}}{{else if none}}{{none}}{{/if}}'
|
||||||
'{{#people}}{{name}}{{else if none}}{{none}}{{/if}}',
|
).toThrow(Error);
|
||||||
{ none: 'No people' },
|
|
||||||
'No people'
|
|
||||||
);
|
|
||||||
}, Error);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('block inverted sections with empty arrays', function() {
|
it('block inverted sections with empty arrays', function() {
|
||||||
shouldCompileTo(
|
expectTemplate('{{#people}}{{name}}{{^}}{{none}}{{/people}}')
|
||||||
'{{#people}}{{name}}{{^}}{{none}}{{/people}}',
|
.withInput({
|
||||||
{ none: 'No people', people: [] },
|
none: 'No people',
|
||||||
'No people'
|
people: []
|
||||||
);
|
})
|
||||||
|
.toCompileTo('No people');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('standalone sections', function() {
|
describe('standalone sections', function() {
|
||||||
it('block standalone else sections', function() {
|
it('block standalone else sections', function() {
|
||||||
shouldCompileTo(
|
expectTemplate('{{#people}}\n{{name}}\n{{^}}\n{{none}}\n{{/people}}\n')
|
||||||
'{{#people}}\n{{name}}\n{{^}}\n{{none}}\n{{/people}}\n',
|
.withInput({ none: 'No people' })
|
||||||
{ none: 'No people' },
|
.toCompileTo('No people\n');
|
||||||
'No people\n'
|
|
||||||
);
|
expectTemplate('{{#none}}\n{{.}}\n{{^}}\n{{none}}\n{{/none}}\n')
|
||||||
shouldCompileTo(
|
.withInput({ none: 'No people' })
|
||||||
'{{#none}}\n{{.}}\n{{^}}\n{{none}}\n{{/none}}\n',
|
.toCompileTo('No people\n');
|
||||||
{ none: 'No people' },
|
|
||||||
'No people\n'
|
expectTemplate('{{#people}}\n{{name}}\n{{^}}\n{{none}}\n{{/people}}\n')
|
||||||
);
|
.withInput({ none: 'No people' })
|
||||||
shouldCompileTo(
|
.toCompileTo('No people\n');
|
||||||
'{{#people}}\n{{name}}\n{{^}}\n{{none}}\n{{/people}}\n',
|
|
||||||
{ none: 'No people' },
|
|
||||||
'No people\n'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('block standalone else sections can be disabled', function() {
|
it('block standalone else sections can be disabled', function() {
|
||||||
shouldCompileTo(
|
expectTemplate('{{#people}}\n{{name}}\n{{^}}\n{{none}}\n{{/people}}\n')
|
||||||
'{{#people}}\n{{name}}\n{{^}}\n{{none}}\n{{/people}}\n',
|
.withInput({ none: 'No people' })
|
||||||
[{ none: 'No people' }, {}, {}, { ignoreStandalone: true }],
|
.withCompileOptions({ ignoreStandalone: true })
|
||||||
'\nNo people\n\n'
|
.toCompileTo('\nNo people\n\n');
|
||||||
);
|
|
||||||
shouldCompileTo(
|
expectTemplate('{{#none}}\n{{.}}\n{{^}}\nFail\n{{/none}}\n')
|
||||||
'{{#none}}\n{{.}}\n{{^}}\nFail\n{{/none}}\n',
|
.withInput({ none: 'No people' })
|
||||||
[{ none: 'No people' }, {}, {}, { ignoreStandalone: true }],
|
.withCompileOptions({ ignoreStandalone: true })
|
||||||
'\nNo people\n\n'
|
.toCompileTo('\nNo people\n\n');
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('block standalone chained else sections', function() {
|
it('block standalone chained else sections', function() {
|
||||||
shouldCompileTo(
|
expectTemplate(
|
||||||
'{{#people}}\n{{name}}\n{{else if none}}\n{{none}}\n{{/people}}\n',
|
'{{#people}}\n{{name}}\n{{else if none}}\n{{none}}\n{{/people}}\n'
|
||||||
{ none: 'No people' },
|
)
|
||||||
'No people\n'
|
.withInput({ none: 'No people' })
|
||||||
);
|
.toCompileTo('No people\n');
|
||||||
shouldCompileTo(
|
|
||||||
'{{#people}}\n{{name}}\n{{else if none}}\n{{none}}\n{{^}}\n{{/people}}\n',
|
expectTemplate(
|
||||||
{ none: 'No people' },
|
'{{#people}}\n{{name}}\n{{else if none}}\n{{none}}\n{{^}}\n{{/people}}\n'
|
||||||
'No people\n'
|
)
|
||||||
);
|
.withInput({ none: 'No people' })
|
||||||
|
.toCompileTo('No people\n');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle nesting', function() {
|
it('should handle nesting', function() {
|
||||||
shouldCompileTo(
|
expectTemplate('{{#data}}\n{{#if true}}\n{{.}}\n{{/if}}\n{{/data}}\nOK.')
|
||||||
'{{#data}}\n{{#if true}}\n{{.}}\n{{/if}}\n{{/data}}\nOK.',
|
.withInput({
|
||||||
{ data: [1, 3, 5] },
|
data: [1, 3, 5]
|
||||||
'1\n3\n5\nOK.'
|
})
|
||||||
);
|
.toCompileTo('1\n3\n5\nOK.');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('compat mode', function() {
|
describe('compat mode', function() {
|
||||||
it('block with deep recursive lookup lookup', function() {
|
it('block with deep recursive lookup lookup', function() {
|
||||||
var string =
|
expectTemplate(
|
||||||
'{{#outer}}Goodbye {{#inner}}cruel {{omg}}{{/inner}}{{/outer}}';
|
'{{#outer}}Goodbye {{#inner}}cruel {{omg}}{{/inner}}{{/outer}}'
|
||||||
var hash = { omg: 'OMG!', outer: [{ inner: [{ text: 'goodbye' }] }] };
|
)
|
||||||
|
.withInput({ omg: 'OMG!', outer: [{ inner: [{ text: 'goodbye' }] }] })
|
||||||
shouldCompileTo(
|
.withCompileOptions({ compat: true })
|
||||||
string,
|
.toCompileTo('Goodbye cruel OMG!');
|
||||||
[hash, undefined, undefined, true],
|
|
||||||
'Goodbye cruel OMG!'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('block with deep recursive pathed lookup', function() {
|
it('block with deep recursive pathed lookup', function() {
|
||||||
var string =
|
expectTemplate(
|
||||||
'{{#outer}}Goodbye {{#inner}}cruel {{omg.yes}}{{/inner}}{{/outer}}';
|
'{{#outer}}Goodbye {{#inner}}cruel {{omg.yes}}{{/inner}}{{/outer}}'
|
||||||
var hash = {
|
)
|
||||||
omg: { yes: 'OMG!' },
|
.withInput({
|
||||||
outer: [{ inner: [{ yes: 'no', text: 'goodbye' }] }]
|
omg: { yes: 'OMG!' },
|
||||||
};
|
outer: [{ inner: [{ yes: 'no', text: 'goodbye' }] }]
|
||||||
|
})
|
||||||
shouldCompileTo(
|
.withCompileOptions({ compat: true })
|
||||||
string,
|
.toCompileTo('Goodbye cruel OMG!');
|
||||||
[hash, undefined, undefined, true],
|
|
||||||
'Goodbye cruel OMG!'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
it('block with missed recursive lookup', function() {
|
|
||||||
var string =
|
|
||||||
'{{#outer}}Goodbye {{#inner}}cruel {{omg.yes}}{{/inner}}{{/outer}}';
|
|
||||||
var hash = {
|
|
||||||
omg: { no: 'OMG!' },
|
|
||||||
outer: [{ inner: [{ yes: 'no', text: 'goodbye' }] }]
|
|
||||||
};
|
|
||||||
|
|
||||||
shouldCompileTo(
|
it('block with missed recursive lookup', function() {
|
||||||
string,
|
expectTemplate(
|
||||||
[hash, undefined, undefined, true],
|
'{{#outer}}Goodbye {{#inner}}cruel {{omg.yes}}{{/inner}}{{/outer}}'
|
||||||
'Goodbye cruel '
|
)
|
||||||
);
|
.withInput({
|
||||||
|
omg: { no: 'OMG!' },
|
||||||
|
outer: [{ inner: [{ yes: 'no', text: 'goodbye' }] }]
|
||||||
|
})
|
||||||
|
.withCompileOptions({ compat: true })
|
||||||
|
.toCompileTo('Goodbye cruel ');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('decorators', function() {
|
describe('decorators', function() {
|
||||||
it('should apply mustache decorators', function() {
|
it('should apply mustache decorators', function() {
|
||||||
var helpers = {
|
expectTemplate('{{#helper}}{{*decorator}}{{/helper}}')
|
||||||
helper: function(options) {
|
.withHelper('helper', function(options) {
|
||||||
return options.fn.run;
|
return options.fn.run;
|
||||||
}
|
})
|
||||||
};
|
.withDecorator('decorator', function(fn) {
|
||||||
var decorators = {
|
|
||||||
decorator: function(fn) {
|
|
||||||
fn.run = 'success';
|
fn.run = 'success';
|
||||||
return fn;
|
return fn;
|
||||||
}
|
})
|
||||||
};
|
.toCompileTo('success');
|
||||||
shouldCompileTo(
|
|
||||||
'{{#helper}}{{*decorator}}{{/helper}}',
|
|
||||||
{ hash: {}, helpers: helpers, decorators: decorators },
|
|
||||||
'success'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should apply allow undefined return', function() {
|
it('should apply allow undefined return', function() {
|
||||||
var helpers = {
|
expectTemplate('{{#helper}}{{*decorator}}suc{{/helper}}')
|
||||||
helper: function(options) {
|
.withHelper('helper', function(options) {
|
||||||
return options.fn() + options.fn.run;
|
return options.fn() + options.fn.run;
|
||||||
}
|
})
|
||||||
};
|
.withDecorator('decorator', function(fn) {
|
||||||
var decorators = {
|
|
||||||
decorator: function(fn) {
|
|
||||||
fn.run = 'cess';
|
fn.run = 'cess';
|
||||||
}
|
})
|
||||||
};
|
.toCompileTo('success');
|
||||||
shouldCompileTo(
|
|
||||||
'{{#helper}}{{*decorator}}suc{{/helper}}',
|
|
||||||
{ hash: {}, helpers: helpers, decorators: decorators },
|
|
||||||
'success'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should apply block decorators', function() {
|
it('should apply block decorators', function() {
|
||||||
var helpers = {
|
expectTemplate(
|
||||||
helper: function(options) {
|
'{{#helper}}{{#*decorator}}success{{/decorator}}{{/helper}}'
|
||||||
|
)
|
||||||
|
.withHelper('helper', function(options) {
|
||||||
return options.fn.run;
|
return options.fn.run;
|
||||||
}
|
})
|
||||||
};
|
.withDecorator('decorator', function(fn, props, container, options) {
|
||||||
var decorators = {
|
|
||||||
decorator: function(fn, props, container, options) {
|
|
||||||
fn.run = options.fn();
|
fn.run = options.fn();
|
||||||
return fn;
|
return fn;
|
||||||
}
|
})
|
||||||
};
|
.toCompileTo('success');
|
||||||
shouldCompileTo(
|
|
||||||
'{{#helper}}{{#*decorator}}success{{/decorator}}{{/helper}}',
|
|
||||||
{ hash: {}, helpers: helpers, decorators: decorators },
|
|
||||||
'success'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should support nested decorators', function() {
|
it('should support nested decorators', function() {
|
||||||
var helpers = {
|
expectTemplate(
|
||||||
helper: function(options) {
|
'{{#helper}}{{#*decorator}}{{#*nested}}suc{{/nested}}cess{{/decorator}}{{/helper}}'
|
||||||
|
)
|
||||||
|
.withHelper('helper', function(options) {
|
||||||
return options.fn.run;
|
return options.fn.run;
|
||||||
}
|
})
|
||||||
};
|
.withDecorators({
|
||||||
var decorators = {
|
decorator: function(fn, props, container, options) {
|
||||||
decorator: function(fn, props, container, options) {
|
fn.run = options.fn.nested + options.fn();
|
||||||
fn.run = options.fn.nested + options.fn();
|
return fn;
|
||||||
return fn;
|
},
|
||||||
},
|
nested: function(fn, props, container, options) {
|
||||||
nested: function(fn, props, container, options) {
|
props.nested = options.fn();
|
||||||
props.nested = options.fn();
|
}
|
||||||
}
|
})
|
||||||
};
|
.toCompileTo('success');
|
||||||
shouldCompileTo(
|
|
||||||
'{{#helper}}{{#*decorator}}{{#*nested}}suc{{/nested}}cess{{/decorator}}{{/helper}}',
|
|
||||||
{ hash: {}, helpers: helpers, decorators: decorators },
|
|
||||||
'success'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should apply multiple decorators', function() {
|
it('should apply multiple decorators', function() {
|
||||||
var helpers = {
|
expectTemplate(
|
||||||
helper: function(options) {
|
'{{#helper}}{{#*decorator}}suc{{/decorator}}{{#*decorator}}cess{{/decorator}}{{/helper}}'
|
||||||
|
)
|
||||||
|
.withHelper('helper', function(options) {
|
||||||
return options.fn.run;
|
return options.fn.run;
|
||||||
}
|
})
|
||||||
};
|
.withDecorator('decorator', function(fn, props, container, options) {
|
||||||
var decorators = {
|
|
||||||
decorator: function(fn, props, container, options) {
|
|
||||||
fn.run = (fn.run || '') + options.fn();
|
fn.run = (fn.run || '') + options.fn();
|
||||||
return fn;
|
return fn;
|
||||||
}
|
})
|
||||||
};
|
.toCompileTo('success');
|
||||||
shouldCompileTo(
|
|
||||||
'{{#helper}}{{#*decorator}}suc{{/decorator}}{{#*decorator}}cess{{/decorator}}{{/helper}}',
|
|
||||||
{ hash: {}, helpers: helpers, decorators: decorators },
|
|
||||||
'success'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should access parent variables', function() {
|
it('should access parent variables', function() {
|
||||||
var helpers = {
|
expectTemplate('{{#helper}}{{*decorator foo}}{{/helper}}')
|
||||||
helper: function(options) {
|
.withHelper('helper', function(options) {
|
||||||
return options.fn.run;
|
return options.fn.run;
|
||||||
}
|
})
|
||||||
};
|
.withDecorator('decorator', function(fn, props, container, options) {
|
||||||
var decorators = {
|
|
||||||
decorator: function(fn, props, container, options) {
|
|
||||||
fn.run = options.args;
|
fn.run = options.args;
|
||||||
return fn;
|
return fn;
|
||||||
}
|
})
|
||||||
};
|
.withInput({ foo: 'success' })
|
||||||
shouldCompileTo(
|
.toCompileTo('success');
|
||||||
'{{#helper}}{{*decorator foo}}{{/helper}}',
|
|
||||||
{ hash: { foo: 'success' }, helpers: helpers, decorators: decorators },
|
|
||||||
'success'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should work with root program', function() {
|
it('should work with root program', function() {
|
||||||
var run;
|
var run;
|
||||||
var decorators = {
|
expectTemplate('{{*decorator "success"}}')
|
||||||
decorator: function(fn, props, container, options) {
|
.withDecorator('decorator', function(fn, props, container, options) {
|
||||||
equals(options.args[0], 'success');
|
equals(options.args[0], 'success');
|
||||||
run = true;
|
run = true;
|
||||||
return fn;
|
return fn;
|
||||||
}
|
})
|
||||||
};
|
.withInput({ foo: 'success' })
|
||||||
shouldCompileTo(
|
.toCompileTo('');
|
||||||
'{{*decorator "success"}}',
|
|
||||||
{ hash: { foo: 'success' }, decorators: decorators },
|
|
||||||
''
|
|
||||||
);
|
|
||||||
equals(run, true);
|
equals(run, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should fail when accessing variables from root', function() {
|
it('should fail when accessing variables from root', function() {
|
||||||
var run;
|
var run;
|
||||||
var decorators = {
|
expectTemplate('{{*decorator foo}}')
|
||||||
decorator: function(fn, props, container, options) {
|
.withDecorator('decorator', function(fn, props, container, options) {
|
||||||
equals(options.args[0], undefined);
|
equals(options.args[0], undefined);
|
||||||
run = true;
|
run = true;
|
||||||
return fn;
|
return fn;
|
||||||
}
|
})
|
||||||
};
|
.withInput({ foo: 'fail' })
|
||||||
shouldCompileTo(
|
.toCompileTo('');
|
||||||
'{{*decorator foo}}',
|
|
||||||
{ hash: { foo: 'fail' }, decorators: decorators },
|
|
||||||
''
|
|
||||||
);
|
|
||||||
equals(run, true);
|
equals(run, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -481,6 +431,7 @@ describe('blocks', function() {
|
|||||||
equals(handlebarsEnv.decorators.foo, undefined);
|
equals(handlebarsEnv.decorators.foo, undefined);
|
||||||
equals(handlebarsEnv.decorators.bar, undefined);
|
equals(handlebarsEnv.decorators.bar, undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('fails with multiple and args', function() {
|
it('fails with multiple and args', function() {
|
||||||
shouldThrow(
|
shouldThrow(
|
||||||
function() {
|
function() {
|
||||||
|
|||||||
+454
-450
File diff suppressed because it is too large
Load Diff
+157
-243
@@ -1,31 +1,24 @@
|
|||||||
describe('data', function() {
|
describe('data', function() {
|
||||||
it('passing in data to a compiled function that expects data - works with helpers', function() {
|
it('passing in data to a compiled function that expects data - works with helpers', function() {
|
||||||
var template = CompilerContext.compile('{{hello}}', { data: true });
|
expectTemplate('{{hello}}')
|
||||||
|
.withCompileOptions({ data: true })
|
||||||
var helpers = {
|
.withHelper('hello', function(options) {
|
||||||
hello: function(options) {
|
|
||||||
return options.data.adjective + ' ' + this.noun;
|
return options.data.adjective + ' ' + this.noun;
|
||||||
}
|
})
|
||||||
};
|
.withRuntimeOptions({ data: { adjective: 'happy' } })
|
||||||
|
.withInput({ noun: 'cat' })
|
||||||
var result = template(
|
.withMessage('Data output by helper')
|
||||||
{ noun: 'cat' },
|
.toCompileTo('happy cat');
|
||||||
{ helpers: helpers, data: { adjective: 'happy' } }
|
|
||||||
);
|
|
||||||
equals('happy cat', result, 'Data output by helper');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('data can be looked up via @foo', function() {
|
it('data can be looked up via @foo', function() {
|
||||||
var template = CompilerContext.compile('{{@hello}}');
|
expectTemplate('{{@hello}}')
|
||||||
var result = template({}, { data: { hello: 'hello' } });
|
.withRuntimeOptions({ data: { hello: 'hello' } })
|
||||||
equals('hello', result, '@foo retrieves template data');
|
.withMessage('@foo retrieves template data')
|
||||||
|
.toCompileTo('hello');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('deep @foo triggers automatic top-level data', function() {
|
it('deep @foo triggers automatic top-level data', function() {
|
||||||
var template = CompilerContext.compile(
|
|
||||||
'{{#let world="world"}}{{#if foo}}{{#if foo}}Hello {{@world}}{{/if}}{{/if}}{{/let}}'
|
|
||||||
);
|
|
||||||
|
|
||||||
var helpers = Handlebars.createFrame(handlebarsEnv.helpers);
|
var helpers = Handlebars.createFrame(handlebarsEnv.helpers);
|
||||||
|
|
||||||
helpers.let = function(options) {
|
helpers.let = function(options) {
|
||||||
@@ -39,124 +32,92 @@ describe('data', function() {
|
|||||||
return options.fn(this, { data: frame });
|
return options.fn(this, { data: frame });
|
||||||
};
|
};
|
||||||
|
|
||||||
var result = template({ foo: true }, { helpers: helpers });
|
expectTemplate(
|
||||||
equals('Hello world', result, 'Automatic data was triggered');
|
'{{#let world="world"}}{{#if foo}}{{#if foo}}Hello {{@world}}{{/if}}{{/if}}{{/let}}'
|
||||||
|
)
|
||||||
|
.withInput({ foo: true })
|
||||||
|
.withHelpers(helpers)
|
||||||
|
.withMessage('Automatic data was triggered')
|
||||||
|
.toCompileTo('Hello world');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('parameter data can be looked up via @foo', function() {
|
it('parameter data can be looked up via @foo', function() {
|
||||||
var template = CompilerContext.compile('{{hello @world}}');
|
expectTemplate('{{hello @world}}')
|
||||||
var helpers = {
|
.withRuntimeOptions({ data: { world: 'world' } })
|
||||||
hello: function(noun) {
|
.withHelper('hello', function(noun) {
|
||||||
return 'Hello ' + noun;
|
return 'Hello ' + noun;
|
||||||
}
|
})
|
||||||
};
|
.withMessage('@foo as a parameter retrieves template data')
|
||||||
|
.toCompileTo('Hello world');
|
||||||
var result = template({}, { helpers: helpers, data: { world: 'world' } });
|
|
||||||
equals(
|
|
||||||
'Hello world',
|
|
||||||
result,
|
|
||||||
'@foo as a parameter retrieves template data'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('hash values can be looked up via @foo', function() {
|
it('hash values can be looked up via @foo', function() {
|
||||||
var template = CompilerContext.compile('{{hello noun=@world}}');
|
expectTemplate('{{hello noun=@world}}')
|
||||||
var helpers = {
|
.withRuntimeOptions({ data: { world: 'world' } })
|
||||||
hello: function(options) {
|
.withHelper('hello', function(options) {
|
||||||
return 'Hello ' + options.hash.noun;
|
return 'Hello ' + options.hash.noun;
|
||||||
}
|
})
|
||||||
};
|
.withMessage('@foo as a parameter retrieves template data')
|
||||||
|
.toCompileTo('Hello world');
|
||||||
var result = template({}, { helpers: helpers, data: { world: 'world' } });
|
|
||||||
equals(
|
|
||||||
'Hello world',
|
|
||||||
result,
|
|
||||||
'@foo as a parameter retrieves template data'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('nested parameter data can be looked up via @foo.bar', function() {
|
it('nested parameter data can be looked up via @foo.bar', function() {
|
||||||
var template = CompilerContext.compile('{{hello @world.bar}}');
|
expectTemplate('{{hello @world.bar}}')
|
||||||
var helpers = {
|
.withRuntimeOptions({ data: { world: { bar: 'world' } } })
|
||||||
hello: function(noun) {
|
.withHelper('hello', function(noun) {
|
||||||
return 'Hello ' + noun;
|
return 'Hello ' + noun;
|
||||||
}
|
})
|
||||||
};
|
.withMessage('@foo as a parameter retrieves template data')
|
||||||
|
.toCompileTo('Hello world');
|
||||||
var result = template(
|
|
||||||
{},
|
|
||||||
{ helpers: helpers, data: { world: { bar: 'world' } } }
|
|
||||||
);
|
|
||||||
equals(
|
|
||||||
'Hello world',
|
|
||||||
result,
|
|
||||||
'@foo as a parameter retrieves template data'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('nested parameter data does not fail with @world.bar', function() {
|
it('nested parameter data does not fail with @world.bar', function() {
|
||||||
var template = CompilerContext.compile('{{hello @world.bar}}');
|
expectTemplate('{{hello @world.bar}}')
|
||||||
var helpers = {
|
.withRuntimeOptions({ data: { foo: { bar: 'world' } } })
|
||||||
hello: function(noun) {
|
.withHelper('hello', function(noun) {
|
||||||
return 'Hello ' + noun;
|
return 'Hello ' + noun;
|
||||||
}
|
})
|
||||||
};
|
.withMessage('@foo as a parameter retrieves template data')
|
||||||
|
.toCompileTo('Hello undefined');
|
||||||
var result = template(
|
|
||||||
{},
|
|
||||||
{ helpers: helpers, data: { foo: { bar: 'world' } } }
|
|
||||||
);
|
|
||||||
equals(
|
|
||||||
'Hello undefined',
|
|
||||||
result,
|
|
||||||
'@foo as a parameter retrieves template data'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('parameter data throws when using complex scope references', function() {
|
it('parameter data throws when using complex scope references', function() {
|
||||||
var string = '{{#goodbyes}}{{text}} cruel {{@foo/../name}}! {{/goodbyes}}';
|
expectTemplate(
|
||||||
|
'{{#goodbyes}}{{text}} cruel {{@foo/../name}}! {{/goodbyes}}'
|
||||||
shouldThrow(function() {
|
).toThrow(Error);
|
||||||
CompilerContext.compile(string);
|
|
||||||
}, Error);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('data can be functions', function() {
|
it('data can be functions', function() {
|
||||||
var template = CompilerContext.compile('{{@hello}}');
|
expectTemplate('{{@hello}}')
|
||||||
var result = template(
|
.withRuntimeOptions({
|
||||||
{},
|
|
||||||
{
|
|
||||||
data: {
|
data: {
|
||||||
hello: function() {
|
hello: function() {
|
||||||
return 'hello';
|
return 'hello';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
);
|
.toCompileTo('hello');
|
||||||
equals('hello', result);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('data can be functions with params', function() {
|
it('data can be functions with params', function() {
|
||||||
var template = CompilerContext.compile('{{@hello "hello"}}');
|
expectTemplate('{{@hello "hello"}}')
|
||||||
var result = template(
|
.withRuntimeOptions({
|
||||||
{},
|
|
||||||
{
|
|
||||||
data: {
|
data: {
|
||||||
hello: function(arg) {
|
hello: function(arg) {
|
||||||
return arg;
|
return arg;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
);
|
.toCompileTo('hello');
|
||||||
equals('hello', result);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('data is inherited downstream', function() {
|
it('data is inherited downstream', function() {
|
||||||
var template = CompilerContext.compile(
|
expectTemplate(
|
||||||
'{{#let foo=1 bar=2}}{{#let foo=bar.baz}}{{@bar}}{{@foo}}{{/let}}{{@foo}}{{/let}}',
|
'{{#let foo=1 bar=2}}{{#let foo=bar.baz}}{{@bar}}{{@foo}}{{/let}}{{@foo}}{{/let}}'
|
||||||
{ data: true }
|
)
|
||||||
);
|
.withInput({ bar: { baz: 'hello world' } })
|
||||||
var helpers = {
|
.withCompileOptions({ data: true })
|
||||||
let: function(options) {
|
.withHelper('let', function(options) {
|
||||||
var frame = Handlebars.createFrame(options.data);
|
var frame = Handlebars.createFrame(options.data);
|
||||||
for (var prop in options.hash) {
|
for (var prop in options.hash) {
|
||||||
if (prop in options.hash) {
|
if (prop in options.hash) {
|
||||||
@@ -164,201 +125,154 @@ describe('data', function() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return options.fn(this, { data: frame });
|
return options.fn(this, { data: frame });
|
||||||
}
|
})
|
||||||
};
|
.withRuntimeOptions({ data: {} })
|
||||||
|
.withMessage('data variables are inherited downstream')
|
||||||
var result = template(
|
.toCompileTo('2hello world1');
|
||||||
{ bar: { baz: 'hello world' } },
|
|
||||||
{ helpers: helpers, data: {} }
|
|
||||||
);
|
|
||||||
equals('2hello world1', result, 'data variables are inherited downstream');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('passing in data to a compiled function that expects data - works with helpers in partials', function() {
|
it('passing in data to a compiled function that expects data - works with helpers in partials', function() {
|
||||||
var template = CompilerContext.compile('{{>myPartial}}', { data: true });
|
expectTemplate('{{>myPartial}}')
|
||||||
|
.withCompileOptions({ data: true })
|
||||||
var partials = {
|
.withPartial('myPartial', '{{hello}}')
|
||||||
myPartial: CompilerContext.compile('{{hello}}', { data: true })
|
.withHelper('hello', function(options) {
|
||||||
};
|
|
||||||
|
|
||||||
var helpers = {
|
|
||||||
hello: function(options) {
|
|
||||||
return options.data.adjective + ' ' + this.noun;
|
return options.data.adjective + ' ' + this.noun;
|
||||||
}
|
})
|
||||||
};
|
.withInput({ noun: 'cat' })
|
||||||
|
.withRuntimeOptions({ data: { adjective: 'happy' } })
|
||||||
var result = template(
|
.withMessage('Data output by helper inside partial')
|
||||||
{ noun: 'cat' },
|
.toCompileTo('happy cat');
|
||||||
{ helpers: helpers, partials: partials, data: { adjective: 'happy' } }
|
|
||||||
);
|
|
||||||
equals('happy cat', result, 'Data output by helper inside partial');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('passing in data to a compiled function that expects data - works with helpers and parameters', function() {
|
it('passing in data to a compiled function that expects data - works with helpers and parameters', function() {
|
||||||
var template = CompilerContext.compile('{{hello world}}', { data: true });
|
expectTemplate('{{hello world}}')
|
||||||
|
.withCompileOptions({ data: true })
|
||||||
var helpers = {
|
.withHelper('hello', function(noun, options) {
|
||||||
hello: function(noun, options) {
|
|
||||||
return options.data.adjective + ' ' + noun + (this.exclaim ? '!' : '');
|
return options.data.adjective + ' ' + noun + (this.exclaim ? '!' : '');
|
||||||
}
|
})
|
||||||
};
|
.withInput({ exclaim: true, world: 'world' })
|
||||||
|
.withRuntimeOptions({ data: { adjective: 'happy' } })
|
||||||
var result = template(
|
.withMessage('Data output by helper')
|
||||||
{ exclaim: true, world: 'world' },
|
.toCompileTo('happy world!');
|
||||||
{ helpers: helpers, data: { adjective: 'happy' } }
|
|
||||||
);
|
|
||||||
equals('happy world!', result, 'Data output by helper');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('passing in data to a compiled function that expects data - works with block helpers', function() {
|
it('passing in data to a compiled function that expects data - works with block helpers', function() {
|
||||||
var template = CompilerContext.compile('{{#hello}}{{world}}{{/hello}}', {
|
expectTemplate('{{#hello}}{{world}}{{/hello}}')
|
||||||
data: true
|
.withCompileOptions({
|
||||||
});
|
data: true
|
||||||
|
})
|
||||||
var helpers = {
|
.withHelper('hello', function(options) {
|
||||||
hello: function(options) {
|
|
||||||
return options.fn(this);
|
return options.fn(this);
|
||||||
},
|
})
|
||||||
world: function(options) {
|
.withHelper('world', function(options) {
|
||||||
return options.data.adjective + ' world' + (this.exclaim ? '!' : '');
|
return options.data.adjective + ' world' + (this.exclaim ? '!' : '');
|
||||||
}
|
})
|
||||||
};
|
.withInput({ exclaim: true })
|
||||||
|
.withRuntimeOptions({ data: { adjective: 'happy' } })
|
||||||
var result = template(
|
.withMessage('Data output by helper')
|
||||||
{ exclaim: true },
|
.toCompileTo('happy world!');
|
||||||
{ helpers: helpers, data: { adjective: 'happy' } }
|
|
||||||
);
|
|
||||||
equals('happy world!', result, 'Data output by helper');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('passing in data to a compiled function that expects data - works with block helpers that use ..', function() {
|
it('passing in data to a compiled function that expects data - works with block helpers that use ..', function() {
|
||||||
var template = CompilerContext.compile(
|
expectTemplate('{{#hello}}{{world ../zomg}}{{/hello}}')
|
||||||
'{{#hello}}{{world ../zomg}}{{/hello}}',
|
.withCompileOptions({ data: true })
|
||||||
{ data: true }
|
.withHelper('hello', function(options) {
|
||||||
);
|
|
||||||
|
|
||||||
var helpers = {
|
|
||||||
hello: function(options) {
|
|
||||||
return options.fn({ exclaim: '?' });
|
return options.fn({ exclaim: '?' });
|
||||||
},
|
})
|
||||||
world: function(thing, options) {
|
.withHelper('world', function(thing, options) {
|
||||||
return options.data.adjective + ' ' + thing + (this.exclaim || '');
|
return options.data.adjective + ' ' + thing + (this.exclaim || '');
|
||||||
}
|
})
|
||||||
};
|
.withInput({ exclaim: true, zomg: 'world' })
|
||||||
|
.withRuntimeOptions({ data: { adjective: 'happy' } })
|
||||||
var result = template(
|
.withMessage('Data output by helper')
|
||||||
{ exclaim: true, zomg: 'world' },
|
.toCompileTo('happy world?');
|
||||||
{ helpers: helpers, data: { adjective: 'happy' } }
|
|
||||||
);
|
|
||||||
equals('happy world?', result, 'Data output by helper');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('passing in data to a compiled function that expects data - data is passed to with block helpers where children use ..', function() {
|
it('passing in data to a compiled function that expects data - data is passed to with block helpers where children use ..', function() {
|
||||||
var template = CompilerContext.compile(
|
expectTemplate('{{#hello}}{{world ../zomg}}{{/hello}}')
|
||||||
'{{#hello}}{{world ../zomg}}{{/hello}}',
|
.withCompileOptions({ data: true })
|
||||||
{ data: true }
|
.withHelper('hello', function(options) {
|
||||||
);
|
|
||||||
|
|
||||||
var helpers = {
|
|
||||||
hello: function(options) {
|
|
||||||
return options.data.accessData + ' ' + options.fn({ exclaim: '?' });
|
return options.data.accessData + ' ' + options.fn({ exclaim: '?' });
|
||||||
},
|
})
|
||||||
world: function(thing, options) {
|
.withHelper('world', function(thing, options) {
|
||||||
return options.data.adjective + ' ' + thing + (this.exclaim || '');
|
return options.data.adjective + ' ' + thing + (this.exclaim || '');
|
||||||
}
|
})
|
||||||
};
|
.withInput({ exclaim: true, zomg: 'world' })
|
||||||
|
.withRuntimeOptions({ data: { adjective: 'happy', accessData: '#win' } })
|
||||||
var result = template(
|
.withMessage('Data output by helper')
|
||||||
{ exclaim: true, zomg: 'world' },
|
.toCompileTo('#win happy world?');
|
||||||
{ helpers: helpers, data: { adjective: 'happy', accessData: '#win' } }
|
|
||||||
);
|
|
||||||
equals('#win happy world?', result, 'Data output by helper');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('you can override inherited data when invoking a helper', function() {
|
it('you can override inherited data when invoking a helper', function() {
|
||||||
var template = CompilerContext.compile(
|
expectTemplate('{{#hello}}{{world zomg}}{{/hello}}')
|
||||||
'{{#hello}}{{world zomg}}{{/hello}}',
|
.withCompileOptions({ data: true })
|
||||||
{ data: true }
|
.withHelper('hello', function(options) {
|
||||||
);
|
|
||||||
|
|
||||||
var helpers = {
|
|
||||||
hello: function(options) {
|
|
||||||
return options.fn(
|
return options.fn(
|
||||||
{ exclaim: '?', zomg: 'world' },
|
{ exclaim: '?', zomg: 'world' },
|
||||||
{ data: { adjective: 'sad' } }
|
{ data: { adjective: 'sad' } }
|
||||||
);
|
);
|
||||||
},
|
})
|
||||||
world: function(thing, options) {
|
.withHelper('world', function(thing, options) {
|
||||||
return options.data.adjective + ' ' + thing + (this.exclaim || '');
|
return options.data.adjective + ' ' + thing + (this.exclaim || '');
|
||||||
}
|
})
|
||||||
};
|
.withInput({ exclaim: true, zomg: 'planet' })
|
||||||
|
.withRuntimeOptions({ data: { adjective: 'happy' } })
|
||||||
var result = template(
|
.withMessage('Overriden data output by helper')
|
||||||
{ exclaim: true, zomg: 'planet' },
|
.toCompileTo('sad world?');
|
||||||
{ helpers: helpers, data: { adjective: 'happy' } }
|
|
||||||
);
|
|
||||||
equals('sad world?', result, 'Overriden data output by helper');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('you can override inherited data when invoking a helper with depth', function() {
|
it('you can override inherited data when invoking a helper with depth', function() {
|
||||||
var template = CompilerContext.compile(
|
expectTemplate('{{#hello}}{{world ../zomg}}{{/hello}}')
|
||||||
'{{#hello}}{{world ../zomg}}{{/hello}}',
|
.withCompileOptions({ data: true })
|
||||||
{ data: true }
|
.withHelper('hello', function(options) {
|
||||||
);
|
|
||||||
|
|
||||||
var helpers = {
|
|
||||||
hello: function(options) {
|
|
||||||
return options.fn({ exclaim: '?' }, { data: { adjective: 'sad' } });
|
return options.fn({ exclaim: '?' }, { data: { adjective: 'sad' } });
|
||||||
},
|
})
|
||||||
world: function(thing, options) {
|
.withHelper('world', function(thing, options) {
|
||||||
return options.data.adjective + ' ' + thing + (this.exclaim || '');
|
return options.data.adjective + ' ' + thing + (this.exclaim || '');
|
||||||
}
|
})
|
||||||
};
|
.withInput({ exclaim: true, zomg: 'world' })
|
||||||
|
.withRuntimeOptions({ data: { adjective: 'happy' } })
|
||||||
var result = template(
|
.withMessage('Overriden data output by helper')
|
||||||
{ exclaim: true, zomg: 'world' },
|
.toCompileTo('sad world?');
|
||||||
{ helpers: helpers, data: { adjective: 'happy' } }
|
|
||||||
);
|
|
||||||
equals('sad world?', result, 'Overriden data output by helper');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('@root', function() {
|
describe('@root', function() {
|
||||||
it('the root context can be looked up via @root', function() {
|
it('the root context can be looked up via @root', function() {
|
||||||
var template = CompilerContext.compile('{{@root.foo}}');
|
expectTemplate('{{@root.foo}}')
|
||||||
var result = template({ foo: 'hello' }, { data: {} });
|
.withInput({ foo: 'hello' })
|
||||||
equals('hello', result);
|
.withRuntimeOptions({ data: {} })
|
||||||
|
.toCompileTo('hello');
|
||||||
|
|
||||||
result = template({ foo: 'hello' }, {});
|
expectTemplate('{{@root.foo}}')
|
||||||
equals('hello', result);
|
.withInput({ foo: 'hello' })
|
||||||
|
.toCompileTo('hello');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('passed root values take priority', function() {
|
it('passed root values take priority', function() {
|
||||||
var template = CompilerContext.compile('{{@root.foo}}');
|
expectTemplate('{{@root.foo}}')
|
||||||
var result = template({}, { data: { root: { foo: 'hello' } } });
|
.withInput({ foo: 'should not be used' })
|
||||||
equals('hello', result);
|
.withRuntimeOptions({ data: { root: { foo: 'hello' } } })
|
||||||
|
.toCompileTo('hello');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('nesting', function() {
|
describe('nesting', function() {
|
||||||
it('the root context can be looked up via @root', function() {
|
it('the root context can be looked up via @root', function() {
|
||||||
var template = CompilerContext.compile(
|
expectTemplate(
|
||||||
'{{#helper}}{{#helper}}{{@./depth}} {{@../depth}} {{@../../depth}}{{/helper}}{{/helper}}'
|
'{{#helper}}{{#helper}}{{@./depth}} {{@../depth}} {{@../../depth}}{{/helper}}{{/helper}}'
|
||||||
);
|
)
|
||||||
var result = template(
|
.withInput({ foo: 'hello' })
|
||||||
{ foo: 'hello' },
|
.withHelper('helper', function(options) {
|
||||||
{
|
var frame = Handlebars.createFrame(options.data);
|
||||||
helpers: {
|
frame.depth = options.data.depth + 1;
|
||||||
helper: function(options) {
|
return options.fn(this, { data: frame });
|
||||||
var frame = Handlebars.createFrame(options.data);
|
})
|
||||||
frame.depth = options.data.depth + 1;
|
.withRuntimeOptions({
|
||||||
return options.fn(this, { data: frame });
|
|
||||||
}
|
|
||||||
},
|
|
||||||
data: {
|
data: {
|
||||||
depth: 0
|
depth: 0
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
);
|
.toCompileTo('2 1 0');
|
||||||
equals('2 1 0', result);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Vendored
+40
-7
@@ -129,6 +129,7 @@ function HandlebarsTestBench(templateAsString) {
|
|||||||
this.templateAsString = templateAsString;
|
this.templateAsString = templateAsString;
|
||||||
this.helpers = {};
|
this.helpers = {};
|
||||||
this.partials = {};
|
this.partials = {};
|
||||||
|
this.decorators = {};
|
||||||
this.input = {};
|
this.input = {};
|
||||||
this.message =
|
this.message =
|
||||||
'Template' + templateAsString + ' does not evaluate to expected output';
|
'Template' + templateAsString + ' does not evaluate to expected output';
|
||||||
@@ -146,11 +147,43 @@ HandlebarsTestBench.prototype.withHelper = function(name, helperFunction) {
|
|||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
HandlebarsTestBench.prototype.withHelpers = function(helperFunctions) {
|
||||||
|
var self = this;
|
||||||
|
Object.keys(helperFunctions).forEach(function(name) {
|
||||||
|
self.withHelper(name, helperFunctions[name]);
|
||||||
|
});
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
HandlebarsTestBench.prototype.withPartial = function(name, partialAsString) {
|
HandlebarsTestBench.prototype.withPartial = function(name, partialAsString) {
|
||||||
this.partials[name] = partialAsString;
|
this.partials[name] = partialAsString;
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
HandlebarsTestBench.prototype.withPartials = function(partials) {
|
||||||
|
var self = this;
|
||||||
|
Object.keys(partials).forEach(function(name) {
|
||||||
|
self.withPartial(name, partials[name]);
|
||||||
|
});
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
HandlebarsTestBench.prototype.withDecorator = function(
|
||||||
|
name,
|
||||||
|
decoratorFunction
|
||||||
|
) {
|
||||||
|
this.decorators[name] = decoratorFunction;
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
HandlebarsTestBench.prototype.withDecorators = function(decorators) {
|
||||||
|
var self = this;
|
||||||
|
Object.keys(decorators).forEach(function(name) {
|
||||||
|
self.withDecorator(name, decorators[name]);
|
||||||
|
});
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
HandlebarsTestBench.prototype.withCompileOptions = function(compileOptions) {
|
HandlebarsTestBench.prototype.withCompileOptions = function(compileOptions) {
|
||||||
this.compileOptions = compileOptions;
|
this.compileOptions = compileOptions;
|
||||||
return this;
|
return this;
|
||||||
@@ -167,19 +200,18 @@ HandlebarsTestBench.prototype.withMessage = function(message) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
HandlebarsTestBench.prototype.toCompileTo = function(expectedOutputAsString) {
|
HandlebarsTestBench.prototype.toCompileTo = function(expectedOutputAsString) {
|
||||||
expect(this._compileAndExecute()).to.equal(expectedOutputAsString);
|
expect(this._compileAndExecute()).to.equal(
|
||||||
|
expectedOutputAsString,
|
||||||
|
this.message
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
// see chai "to.throw" (https://www.chaijs.com/api/bdd/#method_throw)
|
// see chai "to.throw" (https://www.chaijs.com/api/bdd/#method_throw)
|
||||||
HandlebarsTestBench.prototype.toThrow = function(
|
HandlebarsTestBench.prototype.toThrow = function(errorLike, errMsgMatcher) {
|
||||||
errorLike,
|
|
||||||
errMsgMatcher,
|
|
||||||
msg
|
|
||||||
) {
|
|
||||||
var self = this;
|
var self = this;
|
||||||
expect(function() {
|
expect(function() {
|
||||||
self._compileAndExecute();
|
self._compileAndExecute();
|
||||||
}).to.throw(errorLike, errMsgMatcher, msg);
|
}).to.throw(errorLike, errMsgMatcher, this.message);
|
||||||
};
|
};
|
||||||
|
|
||||||
HandlebarsTestBench.prototype._compileAndExecute = function() {
|
HandlebarsTestBench.prototype._compileAndExecute = function() {
|
||||||
@@ -202,5 +234,6 @@ HandlebarsTestBench.prototype._combineRuntimeOptions = function() {
|
|||||||
});
|
});
|
||||||
combinedRuntimeOptions.helpers = this.helpers;
|
combinedRuntimeOptions.helpers = this.helpers;
|
||||||
combinedRuntimeOptions.partials = this.partials;
|
combinedRuntimeOptions.partials = this.partials;
|
||||||
|
combinedRuntimeOptions.decorators = this.decorators;
|
||||||
return combinedRuntimeOptions;
|
return combinedRuntimeOptions;
|
||||||
};
|
};
|
||||||
|
|||||||
+527
-822
File diff suppressed because it is too large
Load Diff
@@ -20,14 +20,18 @@ describe('javascript-compiler api', function() {
|
|||||||
return parent + '.bar_' + name;
|
return parent + '.bar_' + name;
|
||||||
};
|
};
|
||||||
/* eslint-disable camelcase */
|
/* eslint-disable camelcase */
|
||||||
shouldCompileTo('{{foo}}', { bar_foo: 'food' }, 'food');
|
expectTemplate('{{foo}}')
|
||||||
|
.withInput({ bar_foo: 'food' })
|
||||||
|
.toCompileTo('food');
|
||||||
/* eslint-enable camelcase */
|
/* eslint-enable camelcase */
|
||||||
});
|
});
|
||||||
|
|
||||||
// Tests nameLookup dot vs. bracket behavior. Bracket is required in certain cases
|
// Tests nameLookup dot vs. bracket behavior. Bracket is required in certain cases
|
||||||
// to avoid errors in older browsers.
|
// to avoid errors in older browsers.
|
||||||
it('should handle reserved words', function() {
|
it('should handle reserved words', function() {
|
||||||
shouldCompileTo('{{foo}} {{~null~}}', { foo: 'food' }, 'food');
|
expectTemplate('{{foo}} {{~null~}}')
|
||||||
|
.withInput({ foo: 'food' })
|
||||||
|
.toCompileTo('food');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('#compilerInfo', function() {
|
describe('#compilerInfo', function() {
|
||||||
@@ -49,7 +53,9 @@ describe('javascript-compiler api', function() {
|
|||||||
throw new Error("It didn't work");
|
throw new Error("It didn't work");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
shouldCompileTo('{{foo}} ', { foo: 'food' }, 'food ');
|
expectTemplate('{{foo}} ')
|
||||||
|
.withInput({ foo: 'food' })
|
||||||
|
.toCompileTo('food ');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('buffer', function() {
|
describe('buffer', function() {
|
||||||
@@ -70,7 +76,9 @@ describe('javascript-compiler api', function() {
|
|||||||
handlebarsEnv.JavaScriptCompiler.prototype.initializeBuffer = function() {
|
handlebarsEnv.JavaScriptCompiler.prototype.initializeBuffer = function() {
|
||||||
return this.quotedString('foo_');
|
return this.quotedString('foo_');
|
||||||
};
|
};
|
||||||
shouldCompileTo('{{foo}} ', { foo: 'food' }, 'foo_food ');
|
expectTemplate('{{foo}} ')
|
||||||
|
.withInput({ foo: 'food' })
|
||||||
|
.toCompileTo('foo_food ');
|
||||||
});
|
});
|
||||||
it('should allow append buffer override', function() {
|
it('should allow append buffer override', function() {
|
||||||
handlebarsEnv.JavaScriptCompiler.prototype.appendToBuffer = function(
|
handlebarsEnv.JavaScriptCompiler.prototype.appendToBuffer = function(
|
||||||
@@ -78,7 +86,9 @@ describe('javascript-compiler api', function() {
|
|||||||
) {
|
) {
|
||||||
return $superAppend.call(this, [string, ' + "_foo"']);
|
return $superAppend.call(this, [string, ' + "_foo"']);
|
||||||
};
|
};
|
||||||
shouldCompileTo('{{foo}}', { foo: 'food' }, 'food_foo');
|
expectTemplate('{{foo}}')
|
||||||
|
.withInput({ foo: 'food' })
|
||||||
|
.toCompileTo('food_foo');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
+442
-606
File diff suppressed because it is too large
Load Diff
+217
-244
@@ -1,61 +1,53 @@
|
|||||||
describe('Regressions', function() {
|
describe('Regressions', function() {
|
||||||
it('GH-94: Cannot read property of undefined', function() {
|
it('GH-94: Cannot read property of undefined', function() {
|
||||||
var data = {
|
expectTemplate('{{#books}}{{title}}{{author.name}}{{/books}}')
|
||||||
books: [
|
.withInput({
|
||||||
{
|
books: [
|
||||||
title: 'The origin of species',
|
{
|
||||||
author: {
|
title: 'The origin of species',
|
||||||
name: 'Charles Darwin'
|
author: {
|
||||||
|
name: 'Charles Darwin'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: 'Lazarillo de Tormes'
|
||||||
}
|
}
|
||||||
},
|
]
|
||||||
{
|
})
|
||||||
title: 'Lazarillo de Tormes'
|
.withMessage('Renders without an undefined property error')
|
||||||
}
|
.toCompileTo('The origin of speciesCharles DarwinLazarillo de Tormes');
|
||||||
]
|
|
||||||
};
|
|
||||||
var string = '{{#books}}{{title}}{{author.name}}{{/books}}';
|
|
||||||
shouldCompileTo(
|
|
||||||
string,
|
|
||||||
data,
|
|
||||||
'The origin of speciesCharles DarwinLazarillo de Tormes',
|
|
||||||
'Renders without an undefined property error'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("GH-150: Inverted sections print when they shouldn't", function() {
|
it("GH-150: Inverted sections print when they shouldn't", function() {
|
||||||
var string = '{{^set}}not set{{/set}} :: {{#set}}set{{/set}}';
|
var string = '{{^set}}not set{{/set}} :: {{#set}}set{{/set}}';
|
||||||
|
|
||||||
shouldCompileTo(
|
expectTemplate(string)
|
||||||
string,
|
.withMessage(
|
||||||
{},
|
"inverted sections run when property isn't present in context"
|
||||||
'not set :: ',
|
)
|
||||||
"inverted sections run when property isn't present in context"
|
.toCompileTo('not set :: ');
|
||||||
);
|
|
||||||
shouldCompileTo(
|
expectTemplate(string)
|
||||||
string,
|
.withInput({ set: undefined })
|
||||||
{ set: undefined },
|
.withMessage('inverted sections run when property is undefined')
|
||||||
'not set :: ',
|
.toCompileTo('not set :: ');
|
||||||
'inverted sections run when property is undefined'
|
|
||||||
);
|
expectTemplate(string)
|
||||||
shouldCompileTo(
|
.withInput({ set: false })
|
||||||
string,
|
.withMessage('inverted sections run when property is false')
|
||||||
{ set: false },
|
.toCompileTo('not set :: ');
|
||||||
'not set :: ',
|
|
||||||
'inverted sections run when property is false'
|
expectTemplate(string)
|
||||||
);
|
.withInput({ set: true })
|
||||||
shouldCompileTo(
|
.withMessage("inverted sections don't run when property is true")
|
||||||
string,
|
.toCompileTo(' :: set');
|
||||||
{ set: true },
|
|
||||||
' :: set',
|
|
||||||
"inverted sections don't run when property is true"
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('GH-158: Using array index twice, breaks the template', function() {
|
it('GH-158: Using array index twice, breaks the template', function() {
|
||||||
var string = '{{arr.[0]}}, {{arr.[1]}}';
|
expectTemplate('{{arr.[0]}}, {{arr.[1]}}')
|
||||||
var data = { arr: [1, 2] };
|
.withInput({ arr: [1, 2] })
|
||||||
|
.withMessage('it works as expected')
|
||||||
shouldCompileTo(string, data, '1, 2', 'it works as expected');
|
.toCompileTo('1, 2');
|
||||||
});
|
});
|
||||||
|
|
||||||
it("bug reported by @fat where lambdas weren't being properly resolved", function() {
|
it("bug reported by @fat where lambdas weren't being properly resolved", function() {
|
||||||
@@ -73,6 +65,7 @@ describe('Regressions', function() {
|
|||||||
'\n' +
|
'\n' +
|
||||||
'<small>Nothing to check out...</small>\n' +
|
'<small>Nothing to check out...</small>\n' +
|
||||||
'{{/hasThings}}';
|
'{{/hasThings}}';
|
||||||
|
|
||||||
var data = {
|
var data = {
|
||||||
thing: function() {
|
thing: function() {
|
||||||
return 'blah';
|
return 'blah';
|
||||||
@@ -95,25 +88,22 @@ describe('Regressions', function() {
|
|||||||
'<li class=two>@dhg</li>\n' +
|
'<li class=two>@dhg</li>\n' +
|
||||||
'<li class=three>@sayrer</li>\n' +
|
'<li class=three>@sayrer</li>\n' +
|
||||||
'</ul>.\n';
|
'</ul>.\n';
|
||||||
shouldCompileTo(string, data, output);
|
|
||||||
|
expectTemplate(string)
|
||||||
|
.withInput(data)
|
||||||
|
.toCompileTo(output);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('GH-408: Multiple loops fail', function() {
|
it('GH-408: Multiple loops fail', function() {
|
||||||
var context = [
|
expectTemplate(
|
||||||
{ name: 'John Doe', location: { city: 'Chicago' } },
|
|
||||||
{ name: 'Jane Doe', location: { city: 'New York' } }
|
|
||||||
];
|
|
||||||
|
|
||||||
var template = CompilerContext.compile(
|
|
||||||
'{{#.}}{{name}}{{/.}}{{#.}}{{name}}{{/.}}{{#.}}{{name}}{{/.}}'
|
'{{#.}}{{name}}{{/.}}{{#.}}{{name}}{{/.}}{{#.}}{{name}}{{/.}}'
|
||||||
);
|
)
|
||||||
|
.withInput([
|
||||||
var result = template(context);
|
{ name: 'John Doe', location: { city: 'Chicago' } },
|
||||||
equals(
|
{ name: 'Jane Doe', location: { city: 'New York' } }
|
||||||
result,
|
])
|
||||||
'John DoeJane DoeJohn DoeJane DoeJohn DoeJane Doe',
|
.withMessage('It should output multiple times')
|
||||||
'It should output multiple times'
|
.toCompileTo('John DoeJane DoeJohn DoeJane DoeJohn DoeJane Doe');
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('GS-428: Nested if else rendering', function() {
|
it('GS-428: Nested if else rendering', function() {
|
||||||
@@ -131,259 +121,242 @@ describe('Regressions', function() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
shouldCompileTo(succeedingTemplate, [{}, helpers], ' Expected ');
|
expectTemplate(succeedingTemplate)
|
||||||
shouldCompileTo(failingTemplate, [{}, helpers], ' Expected ');
|
.withHelpers(helpers)
|
||||||
|
.toCompileTo(' Expected ');
|
||||||
|
|
||||||
|
expectTemplate(failingTemplate)
|
||||||
|
.withHelpers(helpers)
|
||||||
|
.toCompileTo(' Expected ');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('GH-458: Scoped this identifier', function() {
|
it('GH-458: Scoped this identifier', function() {
|
||||||
shouldCompileTo('{{./foo}}', { foo: 'bar' }, 'bar');
|
expectTemplate('{{./foo}}')
|
||||||
|
.withInput({ foo: 'bar' })
|
||||||
|
.toCompileTo('bar');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('GH-375: Unicode line terminators', function() {
|
it('GH-375: Unicode line terminators', function() {
|
||||||
shouldCompileTo('\u2028', {}, '\u2028');
|
expectTemplate('\u2028').toCompileTo('\u2028');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('GH-534: Object prototype aliases', function() {
|
it('GH-534: Object prototype aliases', function() {
|
||||||
/* eslint-disable no-extend-native */
|
/* eslint-disable no-extend-native */
|
||||||
Object.prototype[0xd834] = true;
|
Object.prototype[0xd834] = true;
|
||||||
|
|
||||||
shouldCompileTo('{{foo}}', { foo: 'bar' }, 'bar');
|
expectTemplate('{{foo}}')
|
||||||
|
.withInput({ foo: 'bar' })
|
||||||
|
.toCompileTo('bar');
|
||||||
|
|
||||||
delete Object.prototype[0xd834];
|
delete Object.prototype[0xd834];
|
||||||
/* eslint-enable no-extend-native */
|
/* eslint-enable no-extend-native */
|
||||||
});
|
});
|
||||||
|
|
||||||
it('GH-437: Matching escaping', function() {
|
it('GH-437: Matching escaping', function() {
|
||||||
shouldThrow(function() {
|
expectTemplate('{{{a}}').toThrow(Error, /Parse error on/);
|
||||||
CompilerContext.compile('{{{a}}');
|
expectTemplate('{{a}}}').toThrow(Error, /Parse error on/);
|
||||||
}, Error);
|
|
||||||
shouldThrow(function() {
|
|
||||||
CompilerContext.compile('{{a}}}');
|
|
||||||
}, Error);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('GH-676: Using array in escaping mustache fails', function() {
|
it('GH-676: Using array in escaping mustache fails', function() {
|
||||||
var string = '{{arr}}';
|
|
||||||
var data = { arr: [1, 2] };
|
var data = { arr: [1, 2] };
|
||||||
|
|
||||||
shouldCompileTo(string, data, data.arr.toString(), 'it works as expected');
|
expectTemplate('{{arr}}')
|
||||||
|
.withInput(data)
|
||||||
|
.withMessage('it works as expected')
|
||||||
|
.toCompileTo(data.arr.toString());
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Mustache man page', function() {
|
it('Mustache man page', function() {
|
||||||
var string =
|
expectTemplate(
|
||||||
'Hello {{name}}. You have just won ${{value}}!{{#in_ca}} Well, ${{taxed_value}}, after taxes.{{/in_ca}}';
|
'Hello {{name}}. You have just won ${{value}}!{{#in_ca}} Well, ${{taxed_value}}, after taxes.{{/in_ca}}'
|
||||||
var data = {
|
)
|
||||||
name: 'Chris',
|
.withInput({
|
||||||
value: 10000,
|
name: 'Chris',
|
||||||
taxed_value: 10000 - 10000 * 0.4,
|
value: 10000,
|
||||||
in_ca: true
|
taxed_value: 10000 - 10000 * 0.4,
|
||||||
};
|
in_ca: true
|
||||||
|
})
|
||||||
shouldCompileTo(
|
.withMessage('the hello world mustache example works')
|
||||||
string,
|
.toCompileTo(
|
||||||
data,
|
'Hello Chris. You have just won $10000! Well, $6000, after taxes.'
|
||||||
'Hello Chris. You have just won $10000! Well, $6000, after taxes.',
|
);
|
||||||
'the hello world mustache example works'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('GH-731: zero context rendering', function() {
|
it('GH-731: zero context rendering', function() {
|
||||||
shouldCompileTo(
|
expectTemplate('{{#foo}} This is {{bar}} ~ {{/foo}}')
|
||||||
'{{#foo}} This is {{bar}} ~ {{/foo}}',
|
.withInput({
|
||||||
{ foo: 0, bar: 'OK' },
|
foo: 0,
|
||||||
' This is ~ '
|
bar: 'OK'
|
||||||
);
|
})
|
||||||
|
.toCompileTo(' This is ~ ');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('GH-820: zero pathed rendering', function() {
|
it('GH-820: zero pathed rendering', function() {
|
||||||
shouldCompileTo('{{foo.bar}}', { foo: 0 }, '');
|
expectTemplate('{{foo.bar}}')
|
||||||
|
.withInput({ foo: 0 })
|
||||||
|
.toCompileTo('');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('GH-837: undefined values for helpers', function() {
|
it('GH-837: undefined values for helpers', function() {
|
||||||
var helpers = {
|
expectTemplate('{{str bar.baz}}')
|
||||||
str: function(value) {
|
.withHelpers({
|
||||||
return value + '';
|
str: function(value) {
|
||||||
}
|
return value + '';
|
||||||
};
|
}
|
||||||
|
})
|
||||||
shouldCompileTo('{{str bar.baz}}', [{}, helpers], 'undefined');
|
.toCompileTo('undefined');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('GH-926: Depths and de-dupe', function() {
|
it('GH-926: Depths and de-dupe', function() {
|
||||||
var context = {
|
expectTemplate(
|
||||||
name: 'foo',
|
|
||||||
data: [1],
|
|
||||||
notData: [1]
|
|
||||||
};
|
|
||||||
|
|
||||||
var template = CompilerContext.compile(
|
|
||||||
'{{#if dater}}{{#each data}}{{../name}}{{/each}}{{else}}{{#each notData}}{{../name}}{{/each}}{{/if}}'
|
'{{#if dater}}{{#each data}}{{../name}}{{/each}}{{else}}{{#each notData}}{{../name}}{{/each}}{{/if}}'
|
||||||
);
|
)
|
||||||
|
.withInput({
|
||||||
var result = template(context);
|
name: 'foo',
|
||||||
equals(result, 'foo');
|
data: [1],
|
||||||
|
notData: [1]
|
||||||
|
})
|
||||||
|
.toCompileTo('foo');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('GH-1021: Each empty string key', function() {
|
it('GH-1021: Each empty string key', function() {
|
||||||
var data = {
|
expectTemplate('{{#each data}}Key: {{@key}}\n{{/each}}')
|
||||||
'': 'foo',
|
.withInput({
|
||||||
name: 'Chris',
|
data: {
|
||||||
value: 10000
|
'': 'foo',
|
||||||
};
|
name: 'Chris',
|
||||||
|
value: 10000
|
||||||
shouldCompileTo(
|
}
|
||||||
'{{#each data}}Key: {{@key}}\n{{/each}}',
|
})
|
||||||
{ data: data },
|
.toCompileTo('Key: \nKey: name\nKey: value\n');
|
||||||
'Key: \nKey: name\nKey: value\n'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('GH-1054: Should handle simple safe string responses', function() {
|
it('GH-1054: Should handle simple safe string responses', function() {
|
||||||
var root = '{{#wrap}}{{>partial}}{{/wrap}}';
|
expectTemplate('{{#wrap}}{{>partial}}{{/wrap}}')
|
||||||
var partials = {
|
.withHelpers({
|
||||||
partial: '{{#wrap}}<partial>{{/wrap}}'
|
wrap: function(options) {
|
||||||
};
|
return new Handlebars.SafeString(options.fn());
|
||||||
var helpers = {
|
}
|
||||||
wrap: function(options) {
|
})
|
||||||
return new Handlebars.SafeString(options.fn());
|
.withPartials({
|
||||||
}
|
partial: '{{#wrap}}<partial>{{/wrap}}'
|
||||||
};
|
})
|
||||||
|
.toCompileTo('<partial>');
|
||||||
shouldCompileToWithPartials(
|
|
||||||
root,
|
|
||||||
[{}, helpers, partials],
|
|
||||||
true,
|
|
||||||
'<partial>'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('GH-1065: Sparse arrays', function() {
|
it('GH-1065: Sparse arrays', function() {
|
||||||
var array = [];
|
var array = [];
|
||||||
array[1] = 'foo';
|
array[1] = 'foo';
|
||||||
array[3] = 'bar';
|
array[3] = 'bar';
|
||||||
shouldCompileTo(
|
expectTemplate('{{#each array}}{{@index}}{{.}}{{/each}}')
|
||||||
'{{#each array}}{{@index}}{{.}}{{/each}}',
|
.withInput({ array: array })
|
||||||
{ array: array },
|
.toCompileTo('1foo3bar');
|
||||||
'1foo3bar'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('GH-1093: Undefined helper context', function() {
|
it('GH-1093: Undefined helper context', function() {
|
||||||
var obj = { foo: undefined, bar: 'bat' };
|
expectTemplate('{{#each obj}}{{{helper}}}{{.}}{{/each}}')
|
||||||
var helpers = {
|
.withInput({ obj: { foo: undefined, bar: 'bat' } })
|
||||||
helper: function() {
|
.withHelpers({
|
||||||
// It's valid to execute a block against an undefined context, but
|
helper: function() {
|
||||||
// helpers can not do so, so we expect to have an empty object here;
|
// It's valid to execute a block against an undefined context, but
|
||||||
for (var name in this) {
|
// helpers can not do so, so we expect to have an empty object here;
|
||||||
if (Object.prototype.hasOwnProperty.call(this, name)) {
|
for (var name in this) {
|
||||||
return 'found';
|
if (Object.prototype.hasOwnProperty.call(this, name)) {
|
||||||
|
return 'found';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
// And to make IE happy, check for the known string as length is not enumerated.
|
||||||
|
return this === 'bat' ? 'found' : 'not';
|
||||||
}
|
}
|
||||||
// And to make IE happy, check for the known string as length is not enumerated.
|
})
|
||||||
return this === 'bat' ? 'found' : 'not';
|
.toCompileTo('notfoundbat');
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
shouldCompileTo(
|
|
||||||
'{{#each obj}}{{{helper}}}{{.}}{{/each}}',
|
|
||||||
[{ obj: obj }, helpers],
|
|
||||||
'notfoundbat'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should support multiple levels of inline partials', function() {
|
it('should support multiple levels of inline partials', function() {
|
||||||
var string =
|
expectTemplate(
|
||||||
'{{#> layout}}{{#*inline "subcontent"}}subcontent{{/inline}}{{/layout}}';
|
'{{#> layout}}{{#*inline "subcontent"}}subcontent{{/inline}}{{/layout}}'
|
||||||
var partials = {
|
)
|
||||||
doctype: 'doctype{{> content}}',
|
.withPartials({
|
||||||
layout:
|
doctype: 'doctype{{> content}}',
|
||||||
'{{#> doctype}}{{#*inline "content"}}layout{{> subcontent}}{{/inline}}{{/doctype}}'
|
layout:
|
||||||
};
|
'{{#> doctype}}{{#*inline "content"}}layout{{> subcontent}}{{/inline}}{{/doctype}}'
|
||||||
shouldCompileToWithPartials(
|
})
|
||||||
string,
|
.toCompileTo('doctypelayoutsubcontent');
|
||||||
[{}, {}, partials],
|
|
||||||
true,
|
|
||||||
'doctypelayoutsubcontent'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('GH-1089: should support failover content in multiple levels of inline partials', function() {
|
it('GH-1089: should support failover content in multiple levels of inline partials', function() {
|
||||||
var string = '{{#> layout}}{{/layout}}';
|
expectTemplate('{{#> layout}}{{/layout}}')
|
||||||
var partials = {
|
.withPartials({
|
||||||
doctype: 'doctype{{> content}}',
|
doctype: 'doctype{{> content}}',
|
||||||
layout:
|
layout:
|
||||||
'{{#> doctype}}{{#*inline "content"}}layout{{#> subcontent}}subcontent{{/subcontent}}{{/inline}}{{/doctype}}'
|
'{{#> doctype}}{{#*inline "content"}}layout{{#> subcontent}}subcontent{{/subcontent}}{{/inline}}{{/doctype}}'
|
||||||
};
|
})
|
||||||
shouldCompileToWithPartials(
|
.toCompileTo('doctypelayoutsubcontent');
|
||||||
string,
|
|
||||||
[{}, {}, partials],
|
|
||||||
true,
|
|
||||||
'doctypelayoutsubcontent'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('GH-1099: should support greater than 3 nested levels of inline partials', function() {
|
it('GH-1099: should support greater than 3 nested levels of inline partials', function() {
|
||||||
var string = '{{#> layout}}Outer{{/layout}}';
|
expectTemplate('{{#> layout}}Outer{{/layout}}')
|
||||||
var partials = {
|
.withPartials({
|
||||||
layout: '{{#> inner}}Inner{{/inner}}{{> @partial-block }}',
|
layout: '{{#> inner}}Inner{{/inner}}{{> @partial-block }}',
|
||||||
inner: ''
|
inner: ''
|
||||||
};
|
})
|
||||||
shouldCompileToWithPartials(string, [{}, {}, partials], true, 'Outer');
|
.toCompileTo('Outer');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('GH-1135 : Context handling within each iteration', function() {
|
it('GH-1135 : Context handling within each iteration', function() {
|
||||||
var obj = { array: [1], name: 'John' };
|
expectTemplate(
|
||||||
var helpers = {
|
|
||||||
myif: function(conditional, options) {
|
|
||||||
if (conditional) {
|
|
||||||
return options.fn(this);
|
|
||||||
} else {
|
|
||||||
return options.inverse(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
shouldCompileTo(
|
|
||||||
'{{#each array}}\n' +
|
'{{#each array}}\n' +
|
||||||
' 1. IF: {{#if true}}{{../name}}-{{../../name}}-{{../../../name}}{{/if}}\n' +
|
' 1. IF: {{#if true}}{{../name}}-{{../../name}}-{{../../../name}}{{/if}}\n' +
|
||||||
' 2. MYIF: {{#myif true}}{{../name}}={{../../name}}={{../../../name}}{{/myif}}\n' +
|
' 2. MYIF: {{#myif true}}{{../name}}={{../../name}}={{../../../name}}{{/myif}}\n' +
|
||||||
'{{/each}}',
|
'{{/each}}'
|
||||||
[obj, helpers],
|
)
|
||||||
' 1. IF: John--\n' + ' 2. MYIF: John==\n'
|
.withInput({ array: [1], name: 'John' })
|
||||||
);
|
.withHelpers({
|
||||||
|
myif: function(conditional, options) {
|
||||||
|
if (conditional) {
|
||||||
|
return options.fn(this);
|
||||||
|
} else {
|
||||||
|
return options.inverse(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.toCompileTo(' 1. IF: John--\n' + ' 2. MYIF: John==\n');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('GH-1186: Support block params for existing programs', function() {
|
it('GH-1186: Support block params for existing programs', function() {
|
||||||
var string =
|
expectTemplate(
|
||||||
'{{#*inline "test"}}{{> @partial-block }}{{/inline}}' +
|
'{{#*inline "test"}}{{> @partial-block }}{{/inline}}' +
|
||||||
'{{#>test }}{{#each listOne as |item|}}{{ item }}{{/each}}{{/test}}' +
|
'{{#>test }}{{#each listOne as |item|}}{{ item }}{{/each}}{{/test}}' +
|
||||||
'{{#>test }}{{#each listTwo as |item|}}{{ item }}{{/each}}{{/test}}';
|
'{{#>test }}{{#each listTwo as |item|}}{{ item }}{{/each}}{{/test}}'
|
||||||
|
)
|
||||||
shouldCompileTo(string, { listOne: ['a'], listTwo: ['b'] }, 'ab', '');
|
.withInput({
|
||||||
|
listOne: ['a'],
|
||||||
|
listTwo: ['b']
|
||||||
|
})
|
||||||
|
.withMessage('')
|
||||||
|
.toCompileTo('ab');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('GH-1319: "unless" breaks when "each" value equals "null"', function() {
|
it('GH-1319: "unless" breaks when "each" value equals "null"', function() {
|
||||||
var string =
|
expectTemplate(
|
||||||
'{{#each list}}{{#unless ./prop}}parent={{../value}} {{/unless}}{{/each}}';
|
'{{#each list}}{{#unless ./prop}}parent={{../value}} {{/unless}}{{/each}}'
|
||||||
shouldCompileTo(
|
)
|
||||||
string,
|
.withInput({
|
||||||
{ value: 'parent', list: [null, 'a'] },
|
value: 'parent',
|
||||||
'parent=parent parent=parent ',
|
list: [null, 'a']
|
||||||
''
|
})
|
||||||
);
|
.withMessage('')
|
||||||
|
.toCompileTo('parent=parent parent=parent ');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('GH-1341: 4.0.7 release breaks {{#if @partial-block}} usage', function() {
|
it('GH-1341: 4.0.7 release breaks {{#if @partial-block}} usage', function() {
|
||||||
var string = 'template {{>partial}} template';
|
expectTemplate('template {{>partial}} template')
|
||||||
var partials = {
|
.withPartials({
|
||||||
partialWithBlock:
|
partialWithBlock:
|
||||||
'{{#if @partial-block}} block {{> @partial-block}} block {{/if}}',
|
'{{#if @partial-block}} block {{> @partial-block}} block {{/if}}',
|
||||||
partial: '{{#> partialWithBlock}} partial {{/partialWithBlock}}'
|
partial: '{{#> partialWithBlock}} partial {{/partialWithBlock}}'
|
||||||
};
|
})
|
||||||
shouldCompileToWithPartials(
|
.toCompileTo('template block partial block template');
|
||||||
string,
|
|
||||||
[{}, {}, partials],
|
|
||||||
true,
|
|
||||||
'template block partial block template'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('GH-1561: 4.3.x should still work with precompiled templates from 4.0.0 <= x < 4.3.0', function() {
|
describe('GH-1561: 4.3.x should still work with precompiled templates from 4.0.0 <= x < 4.3.0', function() {
|
||||||
@@ -482,14 +455,14 @@ describe('Regressions', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should allow hash with protected array names', function() {
|
it('should allow hash with protected array names', function() {
|
||||||
var obj = { array: [1], name: 'John' };
|
expectTemplate('{{helpa length="foo"}}')
|
||||||
var helpers = {
|
.withInput({ array: [1], name: 'John' })
|
||||||
helpa: function(options) {
|
.withHelpers({
|
||||||
return options.hash.length;
|
helpa: function(options) {
|
||||||
}
|
return options.hash.length;
|
||||||
};
|
}
|
||||||
|
})
|
||||||
shouldCompileTo('{{helpa length="foo"}}', [obj, helpers], 'foo');
|
.toCompileTo('foo');
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('GH-1598: Performance degradation for partials since v4.3.0', function() {
|
describe('GH-1598: Performance degradation for partials since v4.3.0', function() {
|
||||||
|
|||||||
+22
-44
@@ -20,36 +20,19 @@ describe('security issues', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should allow the "constructor" property to be accessed if it is an "ownProperty"', function() {
|
it('should allow the "constructor" property to be accessed if it is an "ownProperty"', function() {
|
||||||
shouldCompileTo(
|
expectTemplate('{{constructor.name}}')
|
||||||
'{{constructor.name}}',
|
.withInput({ constructor: { name: 'here we go' } })
|
||||||
{
|
.toCompileTo('here we go');
|
||||||
constructor: {
|
|
||||||
name: 'here we go'
|
expectTemplate('{{lookup (lookup this "constructor") "name"}}')
|
||||||
}
|
.withInput({ constructor: { name: 'here we go' } })
|
||||||
},
|
.toCompileTo('here we go');
|
||||||
'here we go'
|
|
||||||
);
|
|
||||||
shouldCompileTo(
|
|
||||||
'{{lookup (lookup this "constructor") "name"}}',
|
|
||||||
{
|
|
||||||
constructor: {
|
|
||||||
name: 'here we go'
|
|
||||||
}
|
|
||||||
},
|
|
||||||
'here we go'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should allow the "constructor" property to be accessed if it is an "own property"', function() {
|
it('should allow the "constructor" property to be accessed if it is an "own property"', function() {
|
||||||
shouldCompileTo(
|
expectTemplate('{{lookup (lookup this "constructor") "name"}}')
|
||||||
'{{lookup (lookup this "constructor") "name"}}',
|
.withInput({ constructor: { name: 'here we go' } })
|
||||||
{
|
.toCompileTo('here we go');
|
||||||
constructor: {
|
|
||||||
name: 'here we go'
|
|
||||||
}
|
|
||||||
},
|
|
||||||
'here we go'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -60,19 +43,13 @@ describe('security issues', function() {
|
|||||||
|
|
||||||
describe('without the option "allowExplicitCallOfHelperMissing"', function() {
|
describe('without the option "allowExplicitCallOfHelperMissing"', function() {
|
||||||
it('should throw an exception when calling "{{helperMissing}}" ', function() {
|
it('should throw an exception when calling "{{helperMissing}}" ', function() {
|
||||||
shouldThrow(function() {
|
expectTemplate('{{helperMissing}}').toThrow(Error);
|
||||||
var template = Handlebars.compile('{{helperMissing}}');
|
|
||||||
template({});
|
|
||||||
}, Error);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw an exception when calling "{{#helperMissing}}{{/helperMissing}}" ', function() {
|
it('should throw an exception when calling "{{#helperMissing}}{{/helperMissing}}" ', function() {
|
||||||
shouldThrow(function() {
|
expectTemplate('{{#helperMissing}}{{/helperMissing}}').toThrow(Error);
|
||||||
var template = Handlebars.compile(
|
|
||||||
'{{#helperMissing}}{{/helperMissing}}'
|
|
||||||
);
|
|
||||||
template({});
|
|
||||||
}, Error);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw an exception when calling "{{blockHelperMissing "abc" .}}" ', function() {
|
it('should throw an exception when calling "{{blockHelperMissing "abc" .}}" ', function() {
|
||||||
var functionCalls = [];
|
var functionCalls = [];
|
||||||
expect(function() {
|
expect(function() {
|
||||||
@@ -85,17 +62,15 @@ describe('security issues', function() {
|
|||||||
}).to.throw(Error);
|
}).to.throw(Error);
|
||||||
expect(functionCalls.length).to.equal(0);
|
expect(functionCalls.length).to.equal(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw an exception when calling "{{#blockHelperMissing .}}{{/blockHelperMissing}}"', function() {
|
it('should throw an exception when calling "{{#blockHelperMissing .}}{{/blockHelperMissing}}"', function() {
|
||||||
shouldThrow(function() {
|
expectTemplate('{{#blockHelperMissing .}}{{/blockHelperMissing}}')
|
||||||
var template = Handlebars.compile(
|
.withInput({
|
||||||
'{{#blockHelperMissing .}}{{/blockHelperMissing}}'
|
|
||||||
);
|
|
||||||
template({
|
|
||||||
fn: function() {
|
fn: function() {
|
||||||
return 'functionInData';
|
return 'functionInData';
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
}, Error);
|
.toThrow(Error);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -104,12 +79,14 @@ describe('security issues', function() {
|
|||||||
var template = Handlebars.compile('{{helperMissing}}');
|
var template = Handlebars.compile('{{helperMissing}}');
|
||||||
template({}, { allowCallsToHelperMissing: true });
|
template({}, { allowCallsToHelperMissing: true });
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not throw an exception when calling "{{#helperMissing}}{{/helperMissing}}" ', function() {
|
it('should not throw an exception when calling "{{#helperMissing}}{{/helperMissing}}" ', function() {
|
||||||
var template = Handlebars.compile(
|
var template = Handlebars.compile(
|
||||||
'{{#helperMissing}}{{/helperMissing}}'
|
'{{#helperMissing}}{{/helperMissing}}'
|
||||||
);
|
);
|
||||||
template({}, { allowCallsToHelperMissing: true });
|
template({}, { allowCallsToHelperMissing: true });
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not throw an exception when calling "{{blockHelperMissing "abc" .}}" ', function() {
|
it('should not throw an exception when calling "{{blockHelperMissing "abc" .}}" ', function() {
|
||||||
var functionCalls = [];
|
var functionCalls = [];
|
||||||
var template = Handlebars.compile('{{blockHelperMissing "abc" .}}');
|
var template = Handlebars.compile('{{blockHelperMissing "abc" .}}');
|
||||||
@@ -123,6 +100,7 @@ describe('security issues', function() {
|
|||||||
);
|
);
|
||||||
equals(functionCalls.length, 1);
|
equals(functionCalls.length, 1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not throw an exception when calling "{{#blockHelperMissing .}}{{/blockHelperMissing}}"', function() {
|
it('should not throw an exception when calling "{{#blockHelperMissing .}}{{/blockHelperMissing}}"', function() {
|
||||||
var template = Handlebars.compile(
|
var template = Handlebars.compile(
|
||||||
'{{#blockHelperMissing true}}sdads{{/blockHelperMissing}}'
|
'{{#blockHelperMissing true}}sdads{{/blockHelperMissing}}'
|
||||||
|
|||||||
+6
-16
@@ -40,22 +40,12 @@ describe('spec', function() {
|
|||||||
/* eslint-enable no-eval */
|
/* eslint-enable no-eval */
|
||||||
}
|
}
|
||||||
it(name + ' - ' + test.name, function() {
|
it(name + ' - ' + test.name, function() {
|
||||||
if (test.partials) {
|
expectTemplate(test.template)
|
||||||
shouldCompileToWithPartials(
|
.withInput(data)
|
||||||
test.template,
|
.withPartials(test.partials || {})
|
||||||
[data, {}, test.partials, true],
|
.withCompileOptions({ compat: true })
|
||||||
true,
|
.withMessage(test.desc + ' "' + test.template + '"')
|
||||||
test.expected,
|
.toCompileTo(test.expected);
|
||||||
test.desc + ' "' + test.template + '"'
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
shouldCompileTo(
|
|
||||||
test.template,
|
|
||||||
[data, {}, {}, true],
|
|
||||||
test.expected,
|
|
||||||
test.desc + ' "' + test.template + '"'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+95
-161
@@ -3,161 +3,107 @@ var Exception = Handlebars.Exception;
|
|||||||
describe('strict', function() {
|
describe('strict', function() {
|
||||||
describe('strict mode', function() {
|
describe('strict mode', function() {
|
||||||
it('should error on missing property lookup', function() {
|
it('should error on missing property lookup', function() {
|
||||||
shouldThrow(
|
expectTemplate('{{hello}}')
|
||||||
function() {
|
.withCompileOptions({ strict: true })
|
||||||
var template = CompilerContext.compile('{{hello}}', { strict: true });
|
.toThrow(Exception, /"hello" not defined in/);
|
||||||
|
|
||||||
template({});
|
|
||||||
},
|
|
||||||
Exception,
|
|
||||||
/"hello" not defined in/
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should error on missing child', function() {
|
it('should error on missing child', function() {
|
||||||
var template = CompilerContext.compile('{{hello.bar}}', { strict: true });
|
expectTemplate('{{hello.bar}}')
|
||||||
equals(template({ hello: { bar: 'foo' } }), 'foo');
|
.withCompileOptions({ strict: true })
|
||||||
|
.withInput({ hello: { bar: 'foo' } })
|
||||||
|
.toCompileTo('foo');
|
||||||
|
|
||||||
shouldThrow(
|
expectTemplate('{{hello.bar}}')
|
||||||
function() {
|
.withCompileOptions({ strict: true })
|
||||||
template({ hello: {} });
|
.withInput({ hello: {} })
|
||||||
},
|
.toThrow(Exception, /"bar" not defined in/);
|
||||||
Exception,
|
|
||||||
/"bar" not defined in/
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle explicit undefined', function() {
|
it('should handle explicit undefined', function() {
|
||||||
var template = CompilerContext.compile('{{hello.bar}}', { strict: true });
|
expectTemplate('{{hello.bar}}')
|
||||||
|
.withCompileOptions({ strict: true })
|
||||||
equals(template({ hello: { bar: undefined } }), '');
|
.withInput({ hello: { bar: undefined } })
|
||||||
|
.toCompileTo('');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should error on missing property lookup in known helpers mode', function() {
|
it('should error on missing property lookup in known helpers mode', function() {
|
||||||
shouldThrow(
|
expectTemplate('{{hello}}')
|
||||||
function() {
|
.withCompileOptions({
|
||||||
var template = CompilerContext.compile('{{hello}}', {
|
strict: true,
|
||||||
strict: true,
|
knownHelpersOnly: true
|
||||||
knownHelpersOnly: true
|
})
|
||||||
});
|
.toThrow(Exception, /"hello" not defined in/);
|
||||||
|
|
||||||
template({});
|
|
||||||
},
|
|
||||||
Exception,
|
|
||||||
/"hello" not defined in/
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
it('should error on missing context', function() {
|
|
||||||
shouldThrow(function() {
|
|
||||||
var template = CompilerContext.compile('{{hello}}', { strict: true });
|
|
||||||
|
|
||||||
template();
|
it('should error on missing context', function() {
|
||||||
}, Error);
|
expectTemplate('{{hello}}')
|
||||||
|
.withCompileOptions({ strict: true })
|
||||||
|
.toThrow(Error);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should error on missing data lookup', function() {
|
it('should error on missing data lookup', function() {
|
||||||
var template = CompilerContext.compile('{{@hello}}', { strict: true });
|
var xt = expectTemplate('{{@hello}}').withCompileOptions({
|
||||||
equals(template(undefined, { data: { hello: 'foo' } }), 'foo');
|
strict: true
|
||||||
|
});
|
||||||
|
|
||||||
shouldThrow(function() {
|
xt.toThrow(Error);
|
||||||
template();
|
|
||||||
}, Error);
|
xt.withRuntimeOptions({ data: { hello: 'foo' } }).toCompileTo('foo');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not run helperMissing for helper calls', function() {
|
it('should not run helperMissing for helper calls', function() {
|
||||||
shouldThrow(
|
expectTemplate('{{hello foo}}')
|
||||||
function() {
|
.withCompileOptions({ strict: true })
|
||||||
var template = CompilerContext.compile('{{hello foo}}', {
|
.withInput({ foo: true })
|
||||||
strict: true
|
.toThrow(Exception, /"hello" not defined in/);
|
||||||
});
|
|
||||||
|
|
||||||
template({ foo: true });
|
expectTemplate('{{#hello foo}}{{/hello}}')
|
||||||
},
|
.withCompileOptions({ strict: true })
|
||||||
Exception,
|
.withInput({ foo: true })
|
||||||
/"hello" not defined in/
|
.toThrow(Exception, /"hello" not defined in/);
|
||||||
);
|
|
||||||
|
|
||||||
shouldThrow(
|
|
||||||
function() {
|
|
||||||
var template = CompilerContext.compile('{{#hello foo}}{{/hello}}', {
|
|
||||||
strict: true
|
|
||||||
});
|
|
||||||
|
|
||||||
template({ foo: true });
|
|
||||||
},
|
|
||||||
Exception,
|
|
||||||
/"hello" not defined in/
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw on ambiguous blocks', function() {
|
it('should throw on ambiguous blocks', function() {
|
||||||
shouldThrow(
|
expectTemplate('{{#hello}}{{/hello}}')
|
||||||
function() {
|
.withCompileOptions({ strict: true })
|
||||||
var template = CompilerContext.compile('{{#hello}}{{/hello}}', {
|
.toThrow(Exception, /"hello" not defined in/);
|
||||||
strict: true
|
|
||||||
});
|
|
||||||
|
|
||||||
template({});
|
expectTemplate('{{^hello}}{{/hello}}')
|
||||||
},
|
.withCompileOptions({ strict: true })
|
||||||
Exception,
|
.toThrow(Exception, /"hello" not defined in/);
|
||||||
/"hello" not defined in/
|
|
||||||
);
|
|
||||||
|
|
||||||
shouldThrow(
|
expectTemplate('{{#hello.bar}}{{/hello.bar}}')
|
||||||
function() {
|
.withCompileOptions({ strict: true })
|
||||||
var template = CompilerContext.compile('{{^hello}}{{/hello}}', {
|
.withInput({ hello: {} })
|
||||||
strict: true
|
.toThrow(Exception, /"bar" not defined in/);
|
||||||
});
|
|
||||||
|
|
||||||
template({});
|
|
||||||
},
|
|
||||||
Exception,
|
|
||||||
/"hello" not defined in/
|
|
||||||
);
|
|
||||||
|
|
||||||
shouldThrow(
|
|
||||||
function() {
|
|
||||||
var template = CompilerContext.compile(
|
|
||||||
'{{#hello.bar}}{{/hello.bar}}',
|
|
||||||
{ strict: true }
|
|
||||||
);
|
|
||||||
|
|
||||||
template({ hello: {} });
|
|
||||||
},
|
|
||||||
Exception,
|
|
||||||
/"bar" not defined in/
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should allow undefined parameters when passed to helpers', function() {
|
it('should allow undefined parameters when passed to helpers', function() {
|
||||||
var template = CompilerContext.compile(
|
expectTemplate('{{#unless foo}}success{{/unless}}')
|
||||||
'{{#unless foo}}success{{/unless}}',
|
.withCompileOptions({ strict: true })
|
||||||
{ strict: true }
|
.toCompileTo('success');
|
||||||
);
|
|
||||||
equals(template({}), 'success');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should allow undefined hash when passed to helpers', function() {
|
it('should allow undefined hash when passed to helpers', function() {
|
||||||
var template = CompilerContext.compile('{{helper value=@foo}}', {
|
expectTemplate('{{helper value=@foo}}')
|
||||||
strict: true
|
.withCompileOptions({
|
||||||
});
|
strict: true
|
||||||
var helpers = {
|
})
|
||||||
helper: function(options) {
|
.withHelpers({
|
||||||
equals('value' in options.hash, true);
|
helper: function(options) {
|
||||||
equals(options.hash.value, undefined);
|
equals('value' in options.hash, true);
|
||||||
return 'success';
|
equals(options.hash.value, undefined);
|
||||||
}
|
return 'success';
|
||||||
};
|
}
|
||||||
equals(template({}, { helpers: helpers }), 'success');
|
})
|
||||||
|
.toCompileTo('success');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should show error location on missing property lookup', function() {
|
it('should show error location on missing property lookup', function() {
|
||||||
shouldThrow(
|
expectTemplate('\n\n\n {{hello}}')
|
||||||
function() {
|
.withCompileOptions({ strict: true })
|
||||||
var template = CompilerContext.compile('\n\n\n {{hello}}', {
|
.toThrow(Exception, '"hello" not defined in [object Object] - 4:5');
|
||||||
strict: true
|
|
||||||
});
|
|
||||||
template({});
|
|
||||||
},
|
|
||||||
Exception,
|
|
||||||
'"hello" not defined in [object Object] - 4:5'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should error contains correct location properties on missing property lookup', function() {
|
it('should error contains correct location properties on missing property lookup', function() {
|
||||||
@@ -177,54 +123,42 @@ describe('strict', function() {
|
|||||||
|
|
||||||
describe('assume objects', function() {
|
describe('assume objects', function() {
|
||||||
it('should ignore missing property', function() {
|
it('should ignore missing property', function() {
|
||||||
var template = CompilerContext.compile('{{hello}}', {
|
expectTemplate('{{hello}}')
|
||||||
assumeObjects: true
|
.withCompileOptions({ assumeObjects: true })
|
||||||
});
|
.toCompileTo('');
|
||||||
|
|
||||||
equal(template({}), '');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should ignore missing child', function() {
|
it('should ignore missing child', function() {
|
||||||
var template = CompilerContext.compile('{{hello.bar}}', {
|
expectTemplate('{{hello.bar}}')
|
||||||
assumeObjects: true
|
.withCompileOptions({ assumeObjects: true })
|
||||||
});
|
.withInput({ hello: {} })
|
||||||
|
.toCompileTo('');
|
||||||
equal(template({ hello: {} }), '');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should error on missing object', function() {
|
it('should error on missing object', function() {
|
||||||
shouldThrow(function() {
|
expectTemplate('{{hello.bar}}')
|
||||||
var template = CompilerContext.compile('{{hello.bar}}', {
|
.withCompileOptions({ assumeObjects: true })
|
||||||
assumeObjects: true
|
.toThrow(Error);
|
||||||
});
|
|
||||||
|
|
||||||
template({});
|
|
||||||
}, Error);
|
|
||||||
});
|
});
|
||||||
it('should error on missing context', function() {
|
|
||||||
shouldThrow(function() {
|
|
||||||
var template = CompilerContext.compile('{{hello}}', {
|
|
||||||
assumeObjects: true
|
|
||||||
});
|
|
||||||
|
|
||||||
template();
|
it('should error on missing context', function() {
|
||||||
}, Error);
|
expectTemplate('{{hello}}')
|
||||||
|
.withCompileOptions({ assumeObjects: true })
|
||||||
|
.withInput(undefined)
|
||||||
|
.toThrow(Error);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should error on missing data lookup', function() {
|
it('should error on missing data lookup', function() {
|
||||||
shouldThrow(function() {
|
expectTemplate('{{@hello.bar}}')
|
||||||
var template = CompilerContext.compile('{{@hello.bar}}', {
|
.withCompileOptions({ assumeObjects: true })
|
||||||
assumeObjects: true
|
.withInput(undefined)
|
||||||
});
|
.toThrow(Error);
|
||||||
|
|
||||||
template();
|
|
||||||
}, Error);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should execute blockHelperMissing', function() {
|
it('should execute blockHelperMissing', function() {
|
||||||
var template = CompilerContext.compile('{{^hello}}foo{{/hello}}', {
|
expectTemplate('{{^hello}}foo{{/hello}}')
|
||||||
assumeObjects: true
|
.withCompileOptions({ assumeObjects: true })
|
||||||
});
|
.toCompileTo('foo');
|
||||||
|
|
||||||
equals(template({}), 'foo');
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+152
-191
@@ -1,151 +1,117 @@
|
|||||||
describe('string params mode', function() {
|
describe('string params mode', function() {
|
||||||
it('arguments to helpers can be retrieved from options hash in string form', function() {
|
it('arguments to helpers can be retrieved from options hash in string form', function() {
|
||||||
var template = CompilerContext.compile('{{wycats is.a slave.driver}}', {
|
expectTemplate('{{wycats is.a slave.driver}}')
|
||||||
stringParams: true
|
.withCompileOptions({
|
||||||
});
|
stringParams: true
|
||||||
|
})
|
||||||
var helpers = {
|
.withHelpers({
|
||||||
wycats: function(passiveVoice, noun) {
|
wycats: function(passiveVoice, noun) {
|
||||||
return 'HELP ME MY BOSS ' + passiveVoice + ' ' + noun;
|
return 'HELP ME MY BOSS ' + passiveVoice + ' ' + noun;
|
||||||
}
|
}
|
||||||
};
|
})
|
||||||
|
.withMessage('String parameters output')
|
||||||
var result = template({}, { helpers: helpers });
|
.toCompileTo('HELP ME MY BOSS is.a slave.driver');
|
||||||
|
|
||||||
equals(
|
|
||||||
result,
|
|
||||||
'HELP ME MY BOSS is.a slave.driver',
|
|
||||||
'String parameters output'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('when using block form, arguments to helpers can be retrieved from options hash in string form', function() {
|
it('when using block form, arguments to helpers can be retrieved from options hash in string form', function() {
|
||||||
var template = CompilerContext.compile(
|
expectTemplate('{{#wycats is.a slave.driver}}help :({{/wycats}}')
|
||||||
'{{#wycats is.a slave.driver}}help :({{/wycats}}',
|
.withCompileOptions({
|
||||||
{ stringParams: true }
|
stringParams: true
|
||||||
);
|
})
|
||||||
|
.withHelpers({
|
||||||
var helpers = {
|
wycats: function(passiveVoice, noun, options) {
|
||||||
wycats: function(passiveVoice, noun, options) {
|
return (
|
||||||
return (
|
'HELP ME MY BOSS ' +
|
||||||
'HELP ME MY BOSS ' +
|
passiveVoice +
|
||||||
passiveVoice +
|
' ' +
|
||||||
' ' +
|
noun +
|
||||||
noun +
|
': ' +
|
||||||
': ' +
|
options.fn(this)
|
||||||
options.fn(this)
|
);
|
||||||
);
|
}
|
||||||
}
|
})
|
||||||
};
|
.withMessage('String parameters output')
|
||||||
|
.toCompileTo('HELP ME MY BOSS is.a slave.driver: help :(');
|
||||||
var result = template({}, { helpers: helpers });
|
|
||||||
|
|
||||||
equals(
|
|
||||||
result,
|
|
||||||
'HELP ME MY BOSS is.a slave.driver: help :(',
|
|
||||||
'String parameters output'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('when inside a block in String mode, .. passes the appropriate context in the options hash', function() {
|
it('when inside a block in String mode, .. passes the appropriate context in the options hash', function() {
|
||||||
var template = CompilerContext.compile(
|
expectTemplate('{{#with dale}}{{tomdale ../need dad.joke}}{{/with}}')
|
||||||
'{{#with dale}}{{tomdale ../need dad.joke}}{{/with}}',
|
.withCompileOptions({
|
||||||
{ stringParams: true }
|
stringParams: true
|
||||||
);
|
})
|
||||||
|
.withHelpers({
|
||||||
var helpers = {
|
tomdale: function(desire, noun, options) {
|
||||||
tomdale: function(desire, noun, options) {
|
return (
|
||||||
return (
|
'STOP ME FROM READING HACKER NEWS I ' +
|
||||||
'STOP ME FROM READING HACKER NEWS I ' +
|
options.contexts[0][desire] +
|
||||||
options.contexts[0][desire] +
|
' ' +
|
||||||
' ' +
|
noun
|
||||||
noun
|
);
|
||||||
);
|
},
|
||||||
},
|
with: function(context, options) {
|
||||||
|
return options.fn(options.contexts[0][context]);
|
||||||
with: function(context, options) {
|
}
|
||||||
return options.fn(options.contexts[0][context]);
|
})
|
||||||
}
|
.withInput({
|
||||||
};
|
|
||||||
|
|
||||||
var result = template(
|
|
||||||
{
|
|
||||||
dale: {},
|
dale: {},
|
||||||
|
|
||||||
need: 'need-a'
|
need: 'need-a'
|
||||||
},
|
})
|
||||||
{ helpers: helpers }
|
.withMessage('Proper context variable output')
|
||||||
);
|
.toCompileTo('STOP ME FROM READING HACKER NEWS I need-a dad.joke');
|
||||||
|
|
||||||
equals(
|
|
||||||
result,
|
|
||||||
'STOP ME FROM READING HACKER NEWS I need-a dad.joke',
|
|
||||||
'Proper context variable output'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('information about the types is passed along', function() {
|
it('information about the types is passed along', function() {
|
||||||
var template = CompilerContext.compile(
|
expectTemplate("{{tomdale 'need' dad.joke true false}}")
|
||||||
"{{tomdale 'need' dad.joke true false}}",
|
.withCompileOptions({
|
||||||
{ stringParams: true }
|
stringParams: true
|
||||||
);
|
})
|
||||||
|
.withHelpers({
|
||||||
var helpers = {
|
tomdale: function(desire, noun, trueBool, falseBool, options) {
|
||||||
tomdale: function(desire, noun, trueBool, falseBool, options) {
|
equal(options.types[0], 'StringLiteral', 'the string type is passed');
|
||||||
equal(options.types[0], 'StringLiteral', 'the string type is passed');
|
equal(
|
||||||
equal(
|
options.types[1],
|
||||||
options.types[1],
|
'PathExpression',
|
||||||
'PathExpression',
|
'the expression type is passed'
|
||||||
'the expression type is passed'
|
);
|
||||||
);
|
equal(
|
||||||
equal(
|
options.types[2],
|
||||||
options.types[2],
|
'BooleanLiteral',
|
||||||
'BooleanLiteral',
|
'the expression type is passed'
|
||||||
'the expression type is passed'
|
);
|
||||||
);
|
equal(desire, 'need', 'the string form is passed for strings');
|
||||||
equal(desire, 'need', 'the string form is passed for strings');
|
equal(noun, 'dad.joke', 'the string form is passed for expressions');
|
||||||
equal(noun, 'dad.joke', 'the string form is passed for expressions');
|
equal(trueBool, true, 'raw booleans are passed through');
|
||||||
equal(trueBool, true, 'raw booleans are passed through');
|
equal(falseBool, false, 'raw booleans are passed through');
|
||||||
equal(falseBool, false, 'raw booleans are passed through');
|
return 'Helper called';
|
||||||
return 'Helper called';
|
}
|
||||||
}
|
})
|
||||||
};
|
.toCompileTo('Helper called');
|
||||||
|
|
||||||
var result = template({}, { helpers: helpers });
|
|
||||||
equal(result, 'Helper called');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('hash parameters get type information', function() {
|
it('hash parameters get type information', function() {
|
||||||
var template = CompilerContext.compile(
|
expectTemplate("{{tomdale he.says desire='need' noun=dad.joke bool=true}}")
|
||||||
"{{tomdale he.says desire='need' noun=dad.joke bool=true}}",
|
.withCompileOptions({
|
||||||
{ stringParams: true }
|
stringParams: true
|
||||||
);
|
})
|
||||||
|
.withHelpers({
|
||||||
|
tomdale: function(exclamation, options) {
|
||||||
|
equal(exclamation, 'he.says');
|
||||||
|
equal(options.types[0], 'PathExpression');
|
||||||
|
|
||||||
var helpers = {
|
equal(options.hashTypes.desire, 'StringLiteral');
|
||||||
tomdale: function(exclamation, options) {
|
equal(options.hashTypes.noun, 'PathExpression');
|
||||||
equal(exclamation, 'he.says');
|
equal(options.hashTypes.bool, 'BooleanLiteral');
|
||||||
equal(options.types[0], 'PathExpression');
|
equal(options.hash.desire, 'need');
|
||||||
|
equal(options.hash.noun, 'dad.joke');
|
||||||
equal(options.hashTypes.desire, 'StringLiteral');
|
equal(options.hash.bool, true);
|
||||||
equal(options.hashTypes.noun, 'PathExpression');
|
return 'Helper called';
|
||||||
equal(options.hashTypes.bool, 'BooleanLiteral');
|
}
|
||||||
equal(options.hash.desire, 'need');
|
})
|
||||||
equal(options.hash.noun, 'dad.joke');
|
.toCompileTo('Helper called');
|
||||||
equal(options.hash.bool, true);
|
|
||||||
return 'Helper called';
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
var result = template({}, { helpers: helpers });
|
|
||||||
equal(result, 'Helper called');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('hash parameters get context information', function() {
|
it('hash parameters get context information', function() {
|
||||||
var template = CompilerContext.compile(
|
|
||||||
"{{#with dale}}{{tomdale he.says desire='need' noun=../dad/joke bool=true}}{{/with}}",
|
|
||||||
{ stringParams: true }
|
|
||||||
);
|
|
||||||
|
|
||||||
var context = { dale: {} };
|
var context = { dale: {} };
|
||||||
|
|
||||||
var helpers = {
|
var helpers = {
|
||||||
@@ -165,82 +131,77 @@ describe('string params mode', function() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var result = template(context, { helpers: helpers });
|
expectTemplate(
|
||||||
equal(result, 'Helper called');
|
"{{#with dale}}{{tomdale he.says desire='need' noun=../dad/joke bool=true}}{{/with}}"
|
||||||
|
)
|
||||||
|
.withCompileOptions({ stringParams: true })
|
||||||
|
.withHelpers(helpers)
|
||||||
|
.withInput(context)
|
||||||
|
.toCompileTo('Helper called');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('when inside a block in String mode, .. passes the appropriate context in the options hash to a block helper', function() {
|
it('when inside a block in String mode, .. passes the appropriate context in the options hash to a block helper', function() {
|
||||||
var template = CompilerContext.compile(
|
expectTemplate(
|
||||||
'{{#with dale}}{{#tomdale ../need dad.joke}}wot{{/tomdale}}{{/with}}',
|
'{{#with dale}}{{#tomdale ../need dad.joke}}wot{{/tomdale}}{{/with}}'
|
||||||
{ stringParams: true }
|
)
|
||||||
);
|
.withCompileOptions({
|
||||||
|
stringParams: true
|
||||||
|
})
|
||||||
|
.withHelpers({
|
||||||
|
tomdale: function(desire, noun, options) {
|
||||||
|
return (
|
||||||
|
'STOP ME FROM READING HACKER NEWS I ' +
|
||||||
|
options.contexts[0][desire] +
|
||||||
|
' ' +
|
||||||
|
noun +
|
||||||
|
' ' +
|
||||||
|
options.fn(this)
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
var helpers = {
|
with: function(context, options) {
|
||||||
tomdale: function(desire, noun, options) {
|
return options.fn(options.contexts[0][context]);
|
||||||
return (
|
}
|
||||||
'STOP ME FROM READING HACKER NEWS I ' +
|
})
|
||||||
options.contexts[0][desire] +
|
.withInput({
|
||||||
' ' +
|
|
||||||
noun +
|
|
||||||
' ' +
|
|
||||||
options.fn(this)
|
|
||||||
);
|
|
||||||
},
|
|
||||||
|
|
||||||
with: function(context, options) {
|
|
||||||
return options.fn(options.contexts[0][context]);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
var result = template(
|
|
||||||
{
|
|
||||||
dale: {},
|
dale: {},
|
||||||
|
|
||||||
need: 'need-a'
|
need: 'need-a'
|
||||||
},
|
})
|
||||||
{ helpers: helpers }
|
.withMessage('Proper context variable output')
|
||||||
);
|
.toCompileTo('STOP ME FROM READING HACKER NEWS I need-a dad.joke wot');
|
||||||
|
|
||||||
equals(
|
|
||||||
result,
|
|
||||||
'STOP ME FROM READING HACKER NEWS I need-a dad.joke wot',
|
|
||||||
'Proper context variable output'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('with nested block ambiguous', function() {
|
it('with nested block ambiguous', function() {
|
||||||
var template = CompilerContext.compile(
|
expectTemplate(
|
||||||
'{{#with content}}{{#view}}{{firstName}} {{lastName}}{{/view}}{{/with}}',
|
'{{#with content}}{{#view}}{{firstName}} {{lastName}}{{/view}}{{/with}}'
|
||||||
{ stringParams: true }
|
)
|
||||||
);
|
.withCompileOptions({
|
||||||
|
stringParams: true
|
||||||
var helpers = {
|
})
|
||||||
with: function() {
|
.withHelpers({
|
||||||
return 'WITH';
|
with: function() {
|
||||||
},
|
return 'WITH';
|
||||||
view: function() {
|
},
|
||||||
return 'VIEW';
|
view: function() {
|
||||||
}
|
return 'VIEW';
|
||||||
};
|
}
|
||||||
|
})
|
||||||
var result = template({}, { helpers: helpers });
|
.toCompileTo('WITH');
|
||||||
equals(result, 'WITH');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle DATA', function() {
|
it('should handle DATA', function() {
|
||||||
var template = CompilerContext.compile('{{foo @bar}}', {
|
expectTemplate('{{foo @bar}}')
|
||||||
stringParams: true
|
.withCompileOptions({
|
||||||
});
|
stringParams: true
|
||||||
|
})
|
||||||
var helpers = {
|
.withHelpers({
|
||||||
foo: function(bar, options) {
|
foo: function(bar, options) {
|
||||||
equal(bar, '@bar');
|
equal(bar, '@bar');
|
||||||
equal(options.types[0], 'PathExpression');
|
equal(options.types[0], 'PathExpression');
|
||||||
return 'Foo!';
|
return 'Foo!';
|
||||||
}
|
}
|
||||||
};
|
})
|
||||||
|
.toCompileTo('Foo!');
|
||||||
var result = template({}, { helpers: helpers });
|
|
||||||
equal(result, 'Foo!');
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+212
-226
@@ -1,61 +1,57 @@
|
|||||||
describe('subexpressions', function() {
|
describe('subexpressions', function() {
|
||||||
it('arg-less helper', function() {
|
it('arg-less helper', function() {
|
||||||
var string = '{{foo (bar)}}!';
|
expectTemplate('{{foo (bar)}}!')
|
||||||
var context = {};
|
.withHelpers({
|
||||||
var helpers = {
|
foo: function(val) {
|
||||||
foo: function(val) {
|
return val + val;
|
||||||
return val + val;
|
},
|
||||||
},
|
bar: function() {
|
||||||
bar: function() {
|
return 'LOL';
|
||||||
return 'LOL';
|
}
|
||||||
}
|
})
|
||||||
};
|
.toCompileTo('LOLLOL!');
|
||||||
shouldCompileTo(string, [context, helpers], 'LOLLOL!');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('helper w args', function() {
|
it('helper w args', function() {
|
||||||
var string = '{{blog (equal a b)}}';
|
expectTemplate('{{blog (equal a b)}}')
|
||||||
|
.withInput({ bar: 'LOL' })
|
||||||
var context = { bar: 'LOL' };
|
.withHelpers({
|
||||||
var helpers = {
|
blog: function(val) {
|
||||||
blog: function(val) {
|
return 'val is ' + val;
|
||||||
return 'val is ' + val;
|
},
|
||||||
},
|
equal: function(x, y) {
|
||||||
equal: function(x, y) {
|
return x === y;
|
||||||
return x === y;
|
}
|
||||||
}
|
})
|
||||||
};
|
.toCompileTo('val is true');
|
||||||
shouldCompileTo(string, [context, helpers], 'val is true');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('mixed paths and helpers', function() {
|
it('mixed paths and helpers', function() {
|
||||||
var string = '{{blog baz.bat (equal a b) baz.bar}}';
|
expectTemplate('{{blog baz.bat (equal a b) baz.bar}}')
|
||||||
|
.withInput({ bar: 'LOL', baz: { bat: 'foo!', bar: 'bar!' } })
|
||||||
var context = { bar: 'LOL', baz: { bat: 'foo!', bar: 'bar!' } };
|
.withHelpers({
|
||||||
var helpers = {
|
blog: function(val, that, theOther) {
|
||||||
blog: function(val, that, theOther) {
|
return 'val is ' + val + ', ' + that + ' and ' + theOther;
|
||||||
return 'val is ' + val + ', ' + that + ' and ' + theOther;
|
},
|
||||||
},
|
equal: function(x, y) {
|
||||||
equal: function(x, y) {
|
return x === y;
|
||||||
return x === y;
|
}
|
||||||
}
|
})
|
||||||
};
|
.toCompileTo('val is foo!, true and bar!');
|
||||||
shouldCompileTo(string, [context, helpers], 'val is foo!, true and bar!');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('supports much nesting', function() {
|
it('supports much nesting', function() {
|
||||||
var string = '{{blog (equal (equal true true) true)}}';
|
expectTemplate('{{blog (equal (equal true true) true)}}')
|
||||||
|
.withInput({ bar: 'LOL' })
|
||||||
var context = { bar: 'LOL' };
|
.withHelpers({
|
||||||
var helpers = {
|
blog: function(val) {
|
||||||
blog: function(val) {
|
return 'val is ' + val;
|
||||||
return 'val is ' + val;
|
},
|
||||||
},
|
equal: function(x, y) {
|
||||||
equal: function(x, y) {
|
return x === y;
|
||||||
return x === y;
|
}
|
||||||
}
|
})
|
||||||
};
|
.toCompileTo('val is true');
|
||||||
shouldCompileTo(string, [context, helpers], 'val is true');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('GH-800 : Complex subexpressions', function() {
|
it('GH-800 : Complex subexpressions', function() {
|
||||||
@@ -69,20 +65,33 @@ describe('subexpressions', function() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
shouldCompileTo(
|
expectTemplate("{{dash 'abc' (concat a b)}}")
|
||||||
"{{dash 'abc' (concat a b)}}",
|
.withInput(context)
|
||||||
[context, helpers],
|
.withHelpers(helpers)
|
||||||
'abc-ab'
|
.toCompileTo('abc-ab');
|
||||||
);
|
|
||||||
shouldCompileTo('{{dash d (concat a b)}}', [context, helpers], 'd-ab');
|
expectTemplate('{{dash d (concat a b)}}')
|
||||||
shouldCompileTo('{{dash c.c (concat a b)}}', [context, helpers], 'c-ab');
|
.withInput(context)
|
||||||
shouldCompileTo('{{dash (concat a b) c.c}}', [context, helpers], 'ab-c');
|
.withHelpers(helpers)
|
||||||
shouldCompileTo('{{dash (concat a e.e) c.c}}', [context, helpers], 'ae-c');
|
.toCompileTo('d-ab');
|
||||||
|
|
||||||
|
expectTemplate('{{dash c.c (concat a b)}}')
|
||||||
|
.withInput(context)
|
||||||
|
.withHelpers(helpers)
|
||||||
|
.toCompileTo('c-ab');
|
||||||
|
|
||||||
|
expectTemplate('{{dash (concat a b) c.c}}')
|
||||||
|
.withInput(context)
|
||||||
|
.withHelpers(helpers)
|
||||||
|
.toCompileTo('ab-c');
|
||||||
|
|
||||||
|
expectTemplate('{{dash (concat a e.e) c.c}}')
|
||||||
|
.withInput(context)
|
||||||
|
.withHelpers(helpers)
|
||||||
|
.toCompileTo('ae-c');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('provides each nested helper invocation its own options hash', function() {
|
it('provides each nested helper invocation its own options hash', function() {
|
||||||
var string = '{{equal (equal true true) true}}';
|
|
||||||
|
|
||||||
var lastOptions = null;
|
var lastOptions = null;
|
||||||
var helpers = {
|
var helpers = {
|
||||||
equal: function(x, y, options) {
|
equal: function(x, y, options) {
|
||||||
@@ -93,200 +102,177 @@ describe('subexpressions', function() {
|
|||||||
return x === y;
|
return x === y;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
shouldCompileTo(string, [{}, helpers], 'true');
|
expectTemplate('{{equal (equal true true) true}}')
|
||||||
|
.withHelpers(helpers)
|
||||||
|
.toCompileTo('true');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('with hashes', function() {
|
it('with hashes', function() {
|
||||||
var string = "{{blog (equal (equal true true) true fun='yes')}}";
|
expectTemplate("{{blog (equal (equal true true) true fun='yes')}}")
|
||||||
|
.withInput({ bar: 'LOL' })
|
||||||
var context = { bar: 'LOL' };
|
.withHelpers({
|
||||||
var helpers = {
|
blog: function(val) {
|
||||||
blog: function(val) {
|
return 'val is ' + val;
|
||||||
return 'val is ' + val;
|
},
|
||||||
},
|
equal: function(x, y) {
|
||||||
equal: function(x, y) {
|
return x === y;
|
||||||
return x === y;
|
}
|
||||||
}
|
})
|
||||||
};
|
.toCompileTo('val is true');
|
||||||
shouldCompileTo(string, [context, helpers], 'val is true');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('as hashes', function() {
|
it('as hashes', function() {
|
||||||
var string = "{{blog fun=(equal (blog fun=1) 'val is 1')}}";
|
expectTemplate("{{blog fun=(equal (blog fun=1) 'val is 1')}}")
|
||||||
|
.withHelpers({
|
||||||
var helpers = {
|
blog: function(options) {
|
||||||
blog: function(options) {
|
return 'val is ' + options.hash.fun;
|
||||||
return 'val is ' + options.hash.fun;
|
},
|
||||||
},
|
equal: function(x, y) {
|
||||||
equal: function(x, y) {
|
return x === y;
|
||||||
return x === y;
|
}
|
||||||
}
|
})
|
||||||
};
|
.toCompileTo('val is true');
|
||||||
shouldCompileTo(string, [{}, helpers], 'val is true');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('multiple subexpressions in a hash', function() {
|
it('multiple subexpressions in a hash', function() {
|
||||||
var string =
|
expectTemplate(
|
||||||
'{{input aria-label=(t "Name") placeholder=(t "Example User")}}';
|
'{{input aria-label=(t "Name") placeholder=(t "Example User")}}'
|
||||||
|
)
|
||||||
var helpers = {
|
.withHelpers({
|
||||||
input: function(options) {
|
input: function(options) {
|
||||||
var hash = options.hash;
|
var hash = options.hash;
|
||||||
var ariaLabel = Handlebars.Utils.escapeExpression(hash['aria-label']);
|
var ariaLabel = Handlebars.Utils.escapeExpression(hash['aria-label']);
|
||||||
var placeholder = Handlebars.Utils.escapeExpression(hash.placeholder);
|
var placeholder = Handlebars.Utils.escapeExpression(hash.placeholder);
|
||||||
return new Handlebars.SafeString(
|
return new Handlebars.SafeString(
|
||||||
'<input aria-label="' +
|
'<input aria-label="' +
|
||||||
ariaLabel +
|
ariaLabel +
|
||||||
'" placeholder="' +
|
'" placeholder="' +
|
||||||
placeholder +
|
placeholder +
|
||||||
'" />'
|
'" />'
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
t: function(defaultString) {
|
t: function(defaultString) {
|
||||||
return new Handlebars.SafeString(defaultString);
|
return new Handlebars.SafeString(defaultString);
|
||||||
}
|
}
|
||||||
};
|
})
|
||||||
shouldCompileTo(
|
.toCompileTo('<input aria-label="Name" placeholder="Example User" />');
|
||||||
string,
|
|
||||||
[{}, helpers],
|
|
||||||
'<input aria-label="Name" placeholder="Example User" />'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('multiple subexpressions in a hash with context', function() {
|
it('multiple subexpressions in a hash with context', function() {
|
||||||
var string =
|
expectTemplate(
|
||||||
'{{input aria-label=(t item.field) placeholder=(t item.placeholder)}}';
|
'{{input aria-label=(t item.field) placeholder=(t item.placeholder)}}'
|
||||||
|
)
|
||||||
var context = {
|
.withInput({
|
||||||
item: {
|
item: {
|
||||||
field: 'Name',
|
field: 'Name',
|
||||||
placeholder: 'Example User'
|
placeholder: 'Example User'
|
||||||
}
|
}
|
||||||
};
|
})
|
||||||
|
.withHelpers({
|
||||||
var helpers = {
|
input: function(options) {
|
||||||
input: function(options) {
|
var hash = options.hash;
|
||||||
var hash = options.hash;
|
var ariaLabel = Handlebars.Utils.escapeExpression(hash['aria-label']);
|
||||||
var ariaLabel = Handlebars.Utils.escapeExpression(hash['aria-label']);
|
var placeholder = Handlebars.Utils.escapeExpression(hash.placeholder);
|
||||||
var placeholder = Handlebars.Utils.escapeExpression(hash.placeholder);
|
return new Handlebars.SafeString(
|
||||||
return new Handlebars.SafeString(
|
'<input aria-label="' +
|
||||||
'<input aria-label="' +
|
ariaLabel +
|
||||||
ariaLabel +
|
'" placeholder="' +
|
||||||
'" placeholder="' +
|
placeholder +
|
||||||
placeholder +
|
'" />'
|
||||||
'" />'
|
);
|
||||||
);
|
},
|
||||||
},
|
t: function(defaultString) {
|
||||||
t: function(defaultString) {
|
return new Handlebars.SafeString(defaultString);
|
||||||
return new Handlebars.SafeString(defaultString);
|
}
|
||||||
}
|
})
|
||||||
};
|
.toCompileTo('<input aria-label="Name" placeholder="Example User" />');
|
||||||
shouldCompileTo(
|
|
||||||
string,
|
|
||||||
[context, helpers],
|
|
||||||
'<input aria-label="Name" placeholder="Example User" />'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('in string params mode,', function() {
|
it('in string params mode,', function() {
|
||||||
var template = CompilerContext.compile(
|
expectTemplate('{{snog (blorg foo x=y) yeah a=b}}')
|
||||||
'{{snog (blorg foo x=y) yeah a=b}}',
|
.withCompileOptions({ stringParams: true })
|
||||||
{ stringParams: true }
|
.withHelpers({
|
||||||
);
|
snog: function(a, b, options) {
|
||||||
|
equals(a, 'foo');
|
||||||
|
equals(
|
||||||
|
options.types.length,
|
||||||
|
2,
|
||||||
|
'string params for outer helper processed correctly'
|
||||||
|
);
|
||||||
|
equals(
|
||||||
|
options.types[0],
|
||||||
|
'SubExpression',
|
||||||
|
'string params for outer helper processed correctly'
|
||||||
|
);
|
||||||
|
equals(
|
||||||
|
options.types[1],
|
||||||
|
'PathExpression',
|
||||||
|
'string params for outer helper processed correctly'
|
||||||
|
);
|
||||||
|
return a + b;
|
||||||
|
},
|
||||||
|
|
||||||
var helpers = {
|
blorg: function(a, options) {
|
||||||
snog: function(a, b, options) {
|
equals(
|
||||||
equals(a, 'foo');
|
options.types.length,
|
||||||
equals(
|
1,
|
||||||
options.types.length,
|
'string params for inner helper processed correctly'
|
||||||
2,
|
);
|
||||||
'string params for outer helper processed correctly'
|
equals(
|
||||||
);
|
options.types[0],
|
||||||
equals(
|
'PathExpression',
|
||||||
options.types[0],
|
'string params for inner helper processed correctly'
|
||||||
'SubExpression',
|
);
|
||||||
'string params for outer helper processed correctly'
|
return a;
|
||||||
);
|
}
|
||||||
equals(
|
})
|
||||||
options.types[1],
|
.withInput({
|
||||||
'PathExpression',
|
|
||||||
'string params for outer helper processed correctly'
|
|
||||||
);
|
|
||||||
return a + b;
|
|
||||||
},
|
|
||||||
|
|
||||||
blorg: function(a, options) {
|
|
||||||
equals(
|
|
||||||
options.types.length,
|
|
||||||
1,
|
|
||||||
'string params for inner helper processed correctly'
|
|
||||||
);
|
|
||||||
equals(
|
|
||||||
options.types[0],
|
|
||||||
'PathExpression',
|
|
||||||
'string params for inner helper processed correctly'
|
|
||||||
);
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
var result = template(
|
|
||||||
{
|
|
||||||
foo: {},
|
foo: {},
|
||||||
yeah: {}
|
yeah: {}
|
||||||
},
|
})
|
||||||
{ helpers: helpers }
|
.toCompileTo('fooyeah');
|
||||||
);
|
|
||||||
|
|
||||||
equals(result, 'fooyeah');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('as hashes in string params mode', function() {
|
it('as hashes in string params mode', function() {
|
||||||
var template = CompilerContext.compile('{{blog fun=(bork)}}', {
|
expectTemplate('{{blog fun=(bork)}}')
|
||||||
stringParams: true
|
.withCompileOptions({ stringParams: true })
|
||||||
});
|
.withHelpers({
|
||||||
|
blog: function(options) {
|
||||||
var helpers = {
|
equals(options.hashTypes.fun, 'SubExpression');
|
||||||
blog: function(options) {
|
return 'val is ' + options.hash.fun;
|
||||||
equals(options.hashTypes.fun, 'SubExpression');
|
},
|
||||||
return 'val is ' + options.hash.fun;
|
bork: function() {
|
||||||
},
|
return 'BORK';
|
||||||
bork: function() {
|
}
|
||||||
return 'BORK';
|
})
|
||||||
}
|
.toCompileTo('val is BORK');
|
||||||
};
|
|
||||||
|
|
||||||
var result = template({}, { helpers: helpers });
|
|
||||||
equals(result, 'val is BORK');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('subexpression functions on the context', function() {
|
it('subexpression functions on the context', function() {
|
||||||
var string = '{{foo (bar)}}!';
|
expectTemplate('{{foo (bar)}}!')
|
||||||
var context = {
|
.withInput({
|
||||||
bar: function() {
|
bar: function() {
|
||||||
return 'LOL';
|
return 'LOL';
|
||||||
}
|
}
|
||||||
};
|
})
|
||||||
var helpers = {
|
.withHelpers({
|
||||||
foo: function(val) {
|
foo: function(val) {
|
||||||
return val + val;
|
return val + val;
|
||||||
}
|
}
|
||||||
};
|
})
|
||||||
shouldCompileTo(string, [context, helpers], 'LOLLOL!');
|
.toCompileTo('LOLLOL!');
|
||||||
});
|
});
|
||||||
|
|
||||||
it("subexpressions can't just be property lookups", function() {
|
it("subexpressions can't just be property lookups", function() {
|
||||||
var string = '{{foo (bar)}}!';
|
expectTemplate('{{foo (bar)}}!')
|
||||||
var context = {
|
.withInput({
|
||||||
bar: 'LOL'
|
bar: 'LOL'
|
||||||
};
|
})
|
||||||
var helpers = {
|
.withHelpers({
|
||||||
foo: function(val) {
|
foo: function(val) {
|
||||||
return val + val;
|
return val + val;
|
||||||
}
|
}
|
||||||
};
|
})
|
||||||
shouldThrow(function() {
|
.toThrow();
|
||||||
shouldCompileTo(string, [context, helpers], 'LOLLOL!');
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+213
-279
@@ -5,216 +5,184 @@ describe('track ids', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should not include anything without the flag', function() {
|
it('should not include anything without the flag', function() {
|
||||||
var template = CompilerContext.compile('{{wycats is.a slave.driver}}');
|
expectTemplate('{{wycats is.a slave.driver}}')
|
||||||
|
.withHelpers({
|
||||||
|
wycats: function(passiveVoice, noun, options) {
|
||||||
|
equal(options.ids, undefined);
|
||||||
|
equal(options.hashIds, undefined);
|
||||||
|
|
||||||
var helpers = {
|
return 'success';
|
||||||
wycats: function(passiveVoice, noun, options) {
|
}
|
||||||
equal(options.ids, undefined);
|
})
|
||||||
equal(options.hashIds, undefined);
|
.toCompileTo('success');
|
||||||
|
|
||||||
return 'success';
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
equals(template({}, { helpers: helpers }), 'success');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should include argument ids', function() {
|
it('should include argument ids', function() {
|
||||||
var template = CompilerContext.compile('{{wycats is.a slave.driver}}', {
|
expectTemplate('{{wycats is.a slave.driver}}')
|
||||||
trackIds: true
|
.withCompileOptions({ trackIds: true })
|
||||||
});
|
.withHelpers({
|
||||||
|
wycats: function(passiveVoice, noun, options) {
|
||||||
|
equal(options.ids[0], 'is.a');
|
||||||
|
equal(options.ids[1], 'slave.driver');
|
||||||
|
|
||||||
var helpers = {
|
return (
|
||||||
wycats: function(passiveVoice, noun, options) {
|
'HELP ME MY BOSS ' +
|
||||||
equal(options.ids[0], 'is.a');
|
options.ids[0] +
|
||||||
equal(options.ids[1], 'slave.driver');
|
':' +
|
||||||
|
passiveVoice +
|
||||||
return (
|
' ' +
|
||||||
'HELP ME MY BOSS ' +
|
options.ids[1] +
|
||||||
options.ids[0] +
|
':' +
|
||||||
':' +
|
noun
|
||||||
passiveVoice +
|
);
|
||||||
' ' +
|
}
|
||||||
options.ids[1] +
|
})
|
||||||
':' +
|
.withInput(context)
|
||||||
noun
|
.toCompileTo('HELP ME MY BOSS is.a:foo slave.driver:bar');
|
||||||
);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
equals(
|
|
||||||
template(context, { helpers: helpers }),
|
|
||||||
'HELP ME MY BOSS is.a:foo slave.driver:bar'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should include hash ids', function() {
|
it('should include hash ids', function() {
|
||||||
var template = CompilerContext.compile(
|
expectTemplate('{{wycats bat=is.a baz=slave.driver}}')
|
||||||
'{{wycats bat=is.a baz=slave.driver}}',
|
.withCompileOptions({ trackIds: true })
|
||||||
{ trackIds: true }
|
.withHelpers({
|
||||||
);
|
wycats: function(options) {
|
||||||
|
equal(options.hashIds.bat, 'is.a');
|
||||||
|
equal(options.hashIds.baz, 'slave.driver');
|
||||||
|
|
||||||
var helpers = {
|
return (
|
||||||
wycats: function(options) {
|
'HELP ME MY BOSS ' +
|
||||||
equal(options.hashIds.bat, 'is.a');
|
options.hashIds.bat +
|
||||||
equal(options.hashIds.baz, 'slave.driver');
|
':' +
|
||||||
|
options.hash.bat +
|
||||||
return (
|
' ' +
|
||||||
'HELP ME MY BOSS ' +
|
options.hashIds.baz +
|
||||||
options.hashIds.bat +
|
':' +
|
||||||
':' +
|
options.hash.baz
|
||||||
options.hash.bat +
|
);
|
||||||
' ' +
|
}
|
||||||
options.hashIds.baz +
|
})
|
||||||
':' +
|
.withInput(context)
|
||||||
options.hash.baz
|
.toCompileTo('HELP ME MY BOSS is.a:foo slave.driver:bar');
|
||||||
);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
equals(
|
|
||||||
template(context, { helpers: helpers }),
|
|
||||||
'HELP ME MY BOSS is.a:foo slave.driver:bar'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should note ../ and ./ references', function() {
|
it('should note ../ and ./ references', function() {
|
||||||
var template = CompilerContext.compile(
|
expectTemplate('{{wycats ./is.a ../slave.driver this.is.a this}}')
|
||||||
'{{wycats ./is.a ../slave.driver this.is.a this}}',
|
.withCompileOptions({ trackIds: true })
|
||||||
{ trackIds: true }
|
.withHelpers({
|
||||||
);
|
wycats: function(passiveVoice, noun, thiz, thiz2, options) {
|
||||||
|
equal(options.ids[0], 'is.a');
|
||||||
|
equal(options.ids[1], '../slave.driver');
|
||||||
|
equal(options.ids[2], 'is.a');
|
||||||
|
equal(options.ids[3], '');
|
||||||
|
|
||||||
var helpers = {
|
return (
|
||||||
wycats: function(passiveVoice, noun, thiz, thiz2, options) {
|
'HELP ME MY BOSS ' +
|
||||||
equal(options.ids[0], 'is.a');
|
options.ids[0] +
|
||||||
equal(options.ids[1], '../slave.driver');
|
':' +
|
||||||
equal(options.ids[2], 'is.a');
|
passiveVoice +
|
||||||
equal(options.ids[3], '');
|
' ' +
|
||||||
|
options.ids[1] +
|
||||||
return (
|
':' +
|
||||||
'HELP ME MY BOSS ' +
|
noun
|
||||||
options.ids[0] +
|
);
|
||||||
':' +
|
}
|
||||||
passiveVoice +
|
})
|
||||||
' ' +
|
.withInput(context)
|
||||||
options.ids[1] +
|
.toCompileTo('HELP ME MY BOSS is.a:foo ../slave.driver:undefined');
|
||||||
':' +
|
|
||||||
noun
|
|
||||||
);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
equals(
|
|
||||||
template(context, { helpers: helpers }),
|
|
||||||
'HELP ME MY BOSS is.a:foo ../slave.driver:undefined'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should note @data references', function() {
|
it('should note @data references', function() {
|
||||||
var template = CompilerContext.compile('{{wycats @is.a @slave.driver}}', {
|
expectTemplate('{{wycats @is.a @slave.driver}}')
|
||||||
trackIds: true
|
.withCompileOptions({ trackIds: true })
|
||||||
});
|
.withHelpers({
|
||||||
|
wycats: function(passiveVoice, noun, options) {
|
||||||
|
equal(options.ids[0], '@is.a');
|
||||||
|
equal(options.ids[1], '@slave.driver');
|
||||||
|
|
||||||
var helpers = {
|
return (
|
||||||
wycats: function(passiveVoice, noun, options) {
|
'HELP ME MY BOSS ' +
|
||||||
equal(options.ids[0], '@is.a');
|
options.ids[0] +
|
||||||
equal(options.ids[1], '@slave.driver');
|
':' +
|
||||||
|
passiveVoice +
|
||||||
return (
|
' ' +
|
||||||
'HELP ME MY BOSS ' +
|
options.ids[1] +
|
||||||
options.ids[0] +
|
':' +
|
||||||
':' +
|
noun
|
||||||
passiveVoice +
|
);
|
||||||
' ' +
|
}
|
||||||
options.ids[1] +
|
})
|
||||||
':' +
|
.withRuntimeOptions({ data: context })
|
||||||
noun
|
.toCompileTo('HELP ME MY BOSS @is.a:foo @slave.driver:bar');
|
||||||
);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
equals(
|
|
||||||
template({}, { helpers: helpers, data: context }),
|
|
||||||
'HELP ME MY BOSS @is.a:foo @slave.driver:bar'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return null for constants', function() {
|
it('should return null for constants', function() {
|
||||||
var template = CompilerContext.compile('{{wycats 1 "foo" key=false}}', {
|
expectTemplate('{{wycats 1 "foo" key=false}}')
|
||||||
trackIds: true
|
.withCompileOptions({ trackIds: true })
|
||||||
});
|
.withHelpers({
|
||||||
|
wycats: function(passiveVoice, noun, options) {
|
||||||
|
equal(options.ids[0], null);
|
||||||
|
equal(options.ids[1], null);
|
||||||
|
equal(options.hashIds.key, null);
|
||||||
|
|
||||||
var helpers = {
|
return (
|
||||||
wycats: function(passiveVoice, noun, options) {
|
'HELP ME MY BOSS ' +
|
||||||
equal(options.ids[0], null);
|
passiveVoice +
|
||||||
equal(options.ids[1], null);
|
' ' +
|
||||||
equal(options.hashIds.key, null);
|
noun +
|
||||||
|
' ' +
|
||||||
return (
|
options.hash.key
|
||||||
'HELP ME MY BOSS ' +
|
);
|
||||||
passiveVoice +
|
}
|
||||||
' ' +
|
})
|
||||||
noun +
|
.withInput(context)
|
||||||
' ' +
|
.toCompileTo('HELP ME MY BOSS 1 foo false');
|
||||||
options.hash.key
|
|
||||||
);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
equals(
|
|
||||||
template(context, { helpers: helpers }),
|
|
||||||
'HELP ME MY BOSS 1 foo false'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return true for subexpressions', function() {
|
it('should return true for subexpressions', function() {
|
||||||
var template = CompilerContext.compile('{{wycats (sub)}}', {
|
expectTemplate('{{wycats (sub)}}')
|
||||||
trackIds: true
|
.withCompileOptions({ trackIds: true })
|
||||||
});
|
.withHelpers({
|
||||||
|
sub: function() {
|
||||||
|
return 1;
|
||||||
|
},
|
||||||
|
wycats: function(passiveVoice, options) {
|
||||||
|
equal(options.ids[0], true);
|
||||||
|
|
||||||
var helpers = {
|
return 'HELP ME MY BOSS ' + passiveVoice;
|
||||||
sub: function() {
|
}
|
||||||
return 1;
|
})
|
||||||
},
|
.withInput(context)
|
||||||
wycats: function(passiveVoice, options) {
|
.toCompileTo('HELP ME MY BOSS 1');
|
||||||
equal(options.ids[0], true);
|
|
||||||
|
|
||||||
return 'HELP ME MY BOSS ' + passiveVoice;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
equals(template(context, { helpers: helpers }), 'HELP ME MY BOSS 1');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should use block param paths', function() {
|
it('should use block param paths', function() {
|
||||||
var template = CompilerContext.compile(
|
expectTemplate('{{#doIt as |is|}}{{wycats is.a slave.driver is}}{{/doIt}}')
|
||||||
'{{#doIt as |is|}}{{wycats is.a slave.driver is}}{{/doIt}}',
|
.withCompileOptions({ trackIds: true })
|
||||||
{ trackIds: true }
|
.withHelpers({
|
||||||
);
|
doIt: function(options) {
|
||||||
|
var blockParams = [this.is];
|
||||||
|
blockParams.path = ['zomg'];
|
||||||
|
return options.fn(this, { blockParams: blockParams });
|
||||||
|
},
|
||||||
|
wycats: function(passiveVoice, noun, blah, options) {
|
||||||
|
equal(options.ids[0], 'zomg.a');
|
||||||
|
equal(options.ids[1], 'slave.driver');
|
||||||
|
equal(options.ids[2], 'zomg');
|
||||||
|
|
||||||
var helpers = {
|
return (
|
||||||
doIt: function(options) {
|
'HELP ME MY BOSS ' +
|
||||||
var blockParams = [this.is];
|
options.ids[0] +
|
||||||
blockParams.path = ['zomg'];
|
':' +
|
||||||
return options.fn(this, { blockParams: blockParams });
|
passiveVoice +
|
||||||
},
|
' ' +
|
||||||
wycats: function(passiveVoice, noun, blah, options) {
|
options.ids[1] +
|
||||||
equal(options.ids[0], 'zomg.a');
|
':' +
|
||||||
equal(options.ids[1], 'slave.driver');
|
noun
|
||||||
equal(options.ids[2], 'zomg');
|
);
|
||||||
|
}
|
||||||
return (
|
})
|
||||||
'HELP ME MY BOSS ' +
|
.withInput(context)
|
||||||
options.ids[0] +
|
.toCompileTo('HELP ME MY BOSS zomg.a:foo slave.driver:bar');
|
||||||
':' +
|
|
||||||
passiveVoice +
|
|
||||||
' ' +
|
|
||||||
options.ids[1] +
|
|
||||||
':' +
|
|
||||||
noun
|
|
||||||
);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
equals(
|
|
||||||
template(context, { helpers: helpers }),
|
|
||||||
'HELP ME MY BOSS zomg.a:foo slave.driver:bar'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('builtin helpers', function() {
|
describe('builtin helpers', function() {
|
||||||
@@ -229,119 +197,85 @@ describe('track ids', function() {
|
|||||||
|
|
||||||
describe('#each', function() {
|
describe('#each', function() {
|
||||||
it('should track contextPath for arrays', function() {
|
it('should track contextPath for arrays', function() {
|
||||||
var template = CompilerContext.compile(
|
expectTemplate('{{#each array}}{{wycats name}}{{/each}}')
|
||||||
'{{#each array}}{{wycats name}}{{/each}}',
|
.withCompileOptions({ trackIds: true })
|
||||||
{ trackIds: true }
|
.withHelpers(helpers)
|
||||||
);
|
.withInput({ array: [{ name: 'foo' }, { name: 'bar' }] })
|
||||||
|
.toCompileTo('foo:array.0\nbar:array.1\n');
|
||||||
equals(
|
|
||||||
template(
|
|
||||||
{ array: [{ name: 'foo' }, { name: 'bar' }] },
|
|
||||||
{ helpers: helpers }
|
|
||||||
),
|
|
||||||
'foo:array.0\nbar:array.1\n'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should track contextPath for keys', function() {
|
it('should track contextPath for keys', function() {
|
||||||
var template = CompilerContext.compile(
|
expectTemplate('{{#each object}}{{wycats name}}{{/each}}')
|
||||||
'{{#each object}}{{wycats name}}{{/each}}',
|
.withCompileOptions({ trackIds: true })
|
||||||
{ trackIds: true }
|
.withHelpers(helpers)
|
||||||
);
|
.withInput({ object: { foo: { name: 'foo' }, bar: { name: 'bar' } } })
|
||||||
|
.toCompileTo('foo:object.foo\nbar:object.bar\n');
|
||||||
equals(
|
|
||||||
template(
|
|
||||||
{ object: { foo: { name: 'foo' }, bar: { name: 'bar' } } },
|
|
||||||
{ helpers: helpers }
|
|
||||||
),
|
|
||||||
'foo:object.foo\nbar:object.bar\n'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle nesting', function() {
|
it('should handle nesting', function() {
|
||||||
var template = CompilerContext.compile(
|
expectTemplate(
|
||||||
'{{#each .}}{{#each .}}{{wycats name}}{{/each}}{{/each}}',
|
'{{#each .}}{{#each .}}{{wycats name}}{{/each}}{{/each}}'
|
||||||
{ trackIds: true }
|
)
|
||||||
);
|
.withCompileOptions({ trackIds: true })
|
||||||
|
.withHelpers(helpers)
|
||||||
equals(
|
.withInput({ array: [{ name: 'foo' }, { name: 'bar' }] })
|
||||||
template(
|
.toCompileTo('foo:.array..0\nbar:.array..1\n');
|
||||||
{ array: [{ name: 'foo' }, { name: 'bar' }] },
|
|
||||||
{ helpers: helpers }
|
|
||||||
),
|
|
||||||
'foo:.array..0\nbar:.array..1\n'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
it('should handle block params', function() {
|
|
||||||
var template = CompilerContext.compile(
|
|
||||||
'{{#each array as |value|}}{{blockParams value.name}}{{/each}}',
|
|
||||||
{ trackIds: true }
|
|
||||||
);
|
|
||||||
|
|
||||||
equals(
|
it('should handle block params', function() {
|
||||||
template(
|
expectTemplate(
|
||||||
{ array: [{ name: 'foo' }, { name: 'bar' }] },
|
'{{#each array as |value|}}{{blockParams value.name}}{{/each}}'
|
||||||
{ helpers: helpers }
|
)
|
||||||
),
|
.withCompileOptions({ trackIds: true })
|
||||||
'foo:array.0.name\nbar:array.1.name\n'
|
.withHelpers(helpers)
|
||||||
);
|
.withInput({ array: [{ name: 'foo' }, { name: 'bar' }] })
|
||||||
|
.toCompileTo('foo:array.0.name\nbar:array.1.name\n');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#with', function() {
|
describe('#with', function() {
|
||||||
it('should track contextPath', function() {
|
it('should track contextPath', function() {
|
||||||
var template = CompilerContext.compile(
|
expectTemplate('{{#with field}}{{wycats name}}{{/with}}')
|
||||||
'{{#with field}}{{wycats name}}{{/with}}',
|
.withCompileOptions({ trackIds: true })
|
||||||
{ trackIds: true }
|
.withHelpers(helpers)
|
||||||
);
|
.withInput({ field: { name: 'foo' } })
|
||||||
|
.toCompileTo('foo:field\n');
|
||||||
equals(
|
|
||||||
template({ field: { name: 'foo' } }, { helpers: helpers }),
|
|
||||||
'foo:field\n'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
it('should handle nesting', function() {
|
|
||||||
var template = CompilerContext.compile(
|
|
||||||
'{{#with bat}}{{#with field}}{{wycats name}}{{/with}}{{/with}}',
|
|
||||||
{ trackIds: true }
|
|
||||||
);
|
|
||||||
|
|
||||||
equals(
|
it('should handle nesting', function() {
|
||||||
template({ bat: { field: { name: 'foo' } } }, { helpers: helpers }),
|
expectTemplate(
|
||||||
'foo:bat.field\n'
|
'{{#with bat}}{{#with field}}{{wycats name}}{{/with}}{{/with}}'
|
||||||
);
|
)
|
||||||
|
.withCompileOptions({ trackIds: true })
|
||||||
|
.withHelpers(helpers)
|
||||||
|
.withInput({ bat: { field: { name: 'foo' } } })
|
||||||
|
.toCompileTo('foo:bat.field\n');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#blockHelperMissing', function() {
|
describe('#blockHelperMissing', function() {
|
||||||
it('should track contextPath for arrays', function() {
|
it('should track contextPath for arrays', function() {
|
||||||
var template = CompilerContext.compile(
|
expectTemplate('{{#field}}{{wycats name}}{{/field}}')
|
||||||
'{{#field}}{{wycats name}}{{/field}}',
|
.withCompileOptions({ trackIds: true })
|
||||||
{ trackIds: true }
|
.withHelpers(helpers)
|
||||||
);
|
.withInput({ field: [{ name: 'foo' }] })
|
||||||
|
.toCompileTo('foo:field.0\n');
|
||||||
equals(
|
|
||||||
template({ field: [{ name: 'foo' }] }, { helpers: helpers }),
|
|
||||||
'foo:field.0\n'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should track contextPath for keys', function() {
|
it('should track contextPath for keys', function() {
|
||||||
var template = CompilerContext.compile(
|
expectTemplate('{{#field}}{{wycats name}}{{/field}}')
|
||||||
'{{#field}}{{wycats name}}{{/field}}',
|
.withCompileOptions({ trackIds: true })
|
||||||
{ trackIds: true }
|
.withHelpers(helpers)
|
||||||
);
|
.withInput({ field: { name: 'foo' } })
|
||||||
|
.toCompileTo('foo:field\n');
|
||||||
equals(
|
|
||||||
template({ field: { name: 'foo' } }, { helpers: helpers }),
|
|
||||||
'foo:field\n'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
it('should handle nesting', function() {
|
|
||||||
var template = CompilerContext.compile(
|
|
||||||
'{{#bat}}{{#field}}{{wycats name}}{{/field}}{{/bat}}',
|
|
||||||
{ trackIds: true }
|
|
||||||
);
|
|
||||||
|
|
||||||
equals(
|
it('should handle nesting', function() {
|
||||||
template({ bat: { field: { name: 'foo' } } }, { helpers: helpers }),
|
expectTemplate('{{#bat}}{{#field}}{{wycats name}}{{/field}}{{/bat}}')
|
||||||
'foo:bat.field\n'
|
.withCompileOptions({ trackIds: true })
|
||||||
);
|
.withHelpers(helpers)
|
||||||
|
.withInput({ bat: { field: { name: 'foo' } } })
|
||||||
|
.toCompileTo('foo:bat.field\n');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+3
-5
@@ -15,11 +15,9 @@ describe('utils', function() {
|
|||||||
it('it should not escape SafeString properties', function() {
|
it('it should not escape SafeString properties', function() {
|
||||||
var name = new Handlebars.SafeString('<em>Sean O'Malley</em>');
|
var name = new Handlebars.SafeString('<em>Sean O'Malley</em>');
|
||||||
|
|
||||||
shouldCompileTo(
|
expectTemplate('{{name}}')
|
||||||
'{{name}}',
|
.withInput({ name: name })
|
||||||
[{ name: name }],
|
.toCompileTo('<em>Sean O'Malley</em>');
|
||||||
'<em>Sean O'Malley</em>'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
+118
-87
@@ -2,125 +2,156 @@ describe('whitespace control', function() {
|
|||||||
it('should strip whitespace around mustache calls', function() {
|
it('should strip whitespace around mustache calls', function() {
|
||||||
var hash = { foo: 'bar<' };
|
var hash = { foo: 'bar<' };
|
||||||
|
|
||||||
shouldCompileTo(' {{~foo~}} ', hash, 'bar<');
|
expectTemplate(' {{~foo~}} ')
|
||||||
shouldCompileTo(' {{~foo}} ', hash, 'bar< ');
|
.withInput(hash)
|
||||||
shouldCompileTo(' {{foo~}} ', hash, ' bar<');
|
.toCompileTo('bar<');
|
||||||
|
|
||||||
shouldCompileTo(' {{~&foo~}} ', hash, 'bar<');
|
expectTemplate(' {{~foo}} ')
|
||||||
shouldCompileTo(' {{~{foo}~}} ', hash, 'bar<');
|
.withInput(hash)
|
||||||
|
.toCompileTo('bar< ');
|
||||||
|
|
||||||
shouldCompileTo('1\n{{foo~}} \n\n 23\n{{bar}}4', {}, '1\n23\n4');
|
expectTemplate(' {{foo~}} ')
|
||||||
|
.withInput(hash)
|
||||||
|
.toCompileTo(' bar<');
|
||||||
|
|
||||||
|
expectTemplate(' {{~&foo~}} ')
|
||||||
|
.withInput(hash)
|
||||||
|
.toCompileTo('bar<');
|
||||||
|
|
||||||
|
expectTemplate(' {{~{foo}~}} ')
|
||||||
|
.withInput(hash)
|
||||||
|
.toCompileTo('bar<');
|
||||||
|
|
||||||
|
expectTemplate('1\n{{foo~}} \n\n 23\n{{bar}}4').toCompileTo('1\n23\n4');
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('blocks', function() {
|
describe('blocks', function() {
|
||||||
it('should strip whitespace around simple block calls', function() {
|
it('should strip whitespace around simple block calls', function() {
|
||||||
var hash = { foo: 'bar<' };
|
var hash = { foo: 'bar<' };
|
||||||
|
|
||||||
shouldCompileTo(' {{~#if foo~}} bar {{~/if~}} ', hash, 'bar');
|
expectTemplate(' {{~#if foo~}} bar {{~/if~}} ')
|
||||||
shouldCompileTo(' {{#if foo~}} bar {{/if~}} ', hash, ' bar ');
|
.withInput(hash)
|
||||||
shouldCompileTo(' {{~#if foo}} bar {{~/if}} ', hash, ' bar ');
|
.toCompileTo('bar');
|
||||||
shouldCompileTo(' {{#if foo}} bar {{/if}} ', hash, ' bar ');
|
|
||||||
|
|
||||||
shouldCompileTo(
|
expectTemplate(' {{#if foo~}} bar {{/if~}} ')
|
||||||
' \n\n{{~#if foo~}} \n\nbar \n\n{{~/if~}}\n\n ',
|
.withInput(hash)
|
||||||
hash,
|
.toCompileTo(' bar ');
|
||||||
'bar'
|
|
||||||
);
|
expectTemplate(' {{~#if foo}} bar {{~/if}} ')
|
||||||
shouldCompileTo(
|
.withInput(hash)
|
||||||
' a\n\n{{~#if foo~}} \n\nbar \n\n{{~/if~}}\n\na ',
|
.toCompileTo(' bar ');
|
||||||
hash,
|
|
||||||
' abara '
|
expectTemplate(' {{#if foo}} bar {{/if}} ')
|
||||||
);
|
.withInput(hash)
|
||||||
|
.toCompileTo(' bar ');
|
||||||
|
|
||||||
|
expectTemplate(' \n\n{{~#if foo~}} \n\nbar \n\n{{~/if~}}\n\n ')
|
||||||
|
.withInput(hash)
|
||||||
|
.toCompileTo('bar');
|
||||||
|
|
||||||
|
expectTemplate(' a\n\n{{~#if foo~}} \n\nbar \n\n{{~/if~}}\n\na ')
|
||||||
|
.withInput(hash)
|
||||||
|
.toCompileTo(' abara ');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should strip whitespace around inverse block calls', function() {
|
it('should strip whitespace around inverse block calls', function() {
|
||||||
var hash = {};
|
expectTemplate(' {{~^if foo~}} bar {{~/if~}} ').toCompileTo('bar');
|
||||||
|
|
||||||
shouldCompileTo(' {{~^if foo~}} bar {{~/if~}} ', hash, 'bar');
|
expectTemplate(' {{^if foo~}} bar {{/if~}} ').toCompileTo(' bar ');
|
||||||
shouldCompileTo(' {{^if foo~}} bar {{/if~}} ', hash, ' bar ');
|
|
||||||
shouldCompileTo(' {{~^if foo}} bar {{~/if}} ', hash, ' bar ');
|
|
||||||
shouldCompileTo(' {{^if foo}} bar {{/if}} ', hash, ' bar ');
|
|
||||||
|
|
||||||
shouldCompileTo(
|
expectTemplate(' {{~^if foo}} bar {{~/if}} ').toCompileTo(' bar ');
|
||||||
' \n\n{{~^if foo~}} \n\nbar \n\n{{~/if~}}\n\n ',
|
|
||||||
hash,
|
expectTemplate(' {{^if foo}} bar {{/if}} ').toCompileTo(' bar ');
|
||||||
'bar'
|
|
||||||
);
|
expectTemplate(
|
||||||
|
' \n\n{{~^if foo~}} \n\nbar \n\n{{~/if~}}\n\n '
|
||||||
|
).toCompileTo('bar');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should strip whitespace around complex block calls', function() {
|
it('should strip whitespace around complex block calls', function() {
|
||||||
var hash = { foo: 'bar<' };
|
var hash = { foo: 'bar<' };
|
||||||
|
|
||||||
shouldCompileTo('{{#if foo~}} bar {{~^~}} baz {{~/if}}', hash, 'bar');
|
expectTemplate('{{#if foo~}} bar {{~^~}} baz {{~/if}}')
|
||||||
shouldCompileTo('{{#if foo~}} bar {{^~}} baz {{/if}}', hash, 'bar ');
|
.withInput(hash)
|
||||||
shouldCompileTo('{{#if foo}} bar {{~^~}} baz {{~/if}}', hash, ' bar');
|
.toCompileTo('bar');
|
||||||
shouldCompileTo('{{#if foo}} bar {{^~}} baz {{/if}}', hash, ' bar ');
|
|
||||||
|
|
||||||
shouldCompileTo('{{#if foo~}} bar {{~else~}} baz {{~/if}}', hash, 'bar');
|
expectTemplate('{{#if foo~}} bar {{^~}} baz {{/if}}')
|
||||||
|
.withInput(hash)
|
||||||
|
.toCompileTo('bar ');
|
||||||
|
|
||||||
shouldCompileTo(
|
expectTemplate('{{#if foo}} bar {{~^~}} baz {{~/if}}')
|
||||||
'\n\n{{~#if foo~}} \n\nbar \n\n{{~^~}} \n\nbaz \n\n{{~/if~}}\n\n',
|
.withInput(hash)
|
||||||
hash,
|
.toCompileTo(' bar');
|
||||||
'bar'
|
|
||||||
);
|
|
||||||
shouldCompileTo(
|
|
||||||
'\n\n{{~#if foo~}} \n\n{{{foo}}} \n\n{{~^~}} \n\nbaz \n\n{{~/if~}}\n\n',
|
|
||||||
hash,
|
|
||||||
'bar<'
|
|
||||||
);
|
|
||||||
|
|
||||||
hash = {};
|
expectTemplate('{{#if foo}} bar {{^~}} baz {{/if}}')
|
||||||
|
.withInput(hash)
|
||||||
|
.toCompileTo(' bar ');
|
||||||
|
|
||||||
shouldCompileTo('{{#if foo~}} bar {{~^~}} baz {{~/if}}', hash, 'baz');
|
expectTemplate('{{#if foo~}} bar {{~else~}} baz {{~/if}}')
|
||||||
shouldCompileTo('{{#if foo}} bar {{~^~}} baz {{/if}}', hash, 'baz ');
|
.withInput(hash)
|
||||||
shouldCompileTo('{{#if foo~}} bar {{~^}} baz {{~/if}}', hash, ' baz');
|
.toCompileTo('bar');
|
||||||
shouldCompileTo('{{#if foo~}} bar {{~^}} baz {{/if}}', hash, ' baz ');
|
|
||||||
|
|
||||||
shouldCompileTo('{{#if foo~}} bar {{~else~}} baz {{~/if}}', hash, 'baz');
|
expectTemplate(
|
||||||
|
'\n\n{{~#if foo~}} \n\nbar \n\n{{~^~}} \n\nbaz \n\n{{~/if~}}\n\n'
|
||||||
|
)
|
||||||
|
.withInput(hash)
|
||||||
|
.toCompileTo('bar');
|
||||||
|
|
||||||
shouldCompileTo(
|
expectTemplate(
|
||||||
'\n\n{{~#if foo~}} \n\nbar \n\n{{~^~}} \n\nbaz \n\n{{~/if~}}\n\n',
|
'\n\n{{~#if foo~}} \n\n{{{foo}}} \n\n{{~^~}} \n\nbaz \n\n{{~/if~}}\n\n'
|
||||||
hash,
|
)
|
||||||
|
.withInput(hash)
|
||||||
|
.toCompileTo('bar<');
|
||||||
|
|
||||||
|
expectTemplate('{{#if foo~}} bar {{~^~}} baz {{~/if}}').toCompileTo(
|
||||||
'baz'
|
'baz'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
expectTemplate('{{#if foo}} bar {{~^~}} baz {{/if}}').toCompileTo('baz ');
|
||||||
|
|
||||||
|
expectTemplate('{{#if foo~}} bar {{~^}} baz {{~/if}}').toCompileTo(
|
||||||
|
' baz'
|
||||||
|
);
|
||||||
|
|
||||||
|
expectTemplate('{{#if foo~}} bar {{~^}} baz {{/if}}').toCompileTo(
|
||||||
|
' baz '
|
||||||
|
);
|
||||||
|
|
||||||
|
expectTemplate('{{#if foo~}} bar {{~else~}} baz {{~/if}}').toCompileTo(
|
||||||
|
'baz'
|
||||||
|
);
|
||||||
|
|
||||||
|
expectTemplate(
|
||||||
|
'\n\n{{~#if foo~}} \n\nbar \n\n{{~^~}} \n\nbaz \n\n{{~/if~}}\n\n'
|
||||||
|
).toCompileTo('baz');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should strip whitespace around partials', function() {
|
it('should strip whitespace around partials', function() {
|
||||||
shouldCompileToWithPartials(
|
expectTemplate('foo {{~> dude~}} ')
|
||||||
'foo {{~> dude~}} ',
|
.withPartials({ dude: 'bar' })
|
||||||
[{}, {}, { dude: 'bar' }],
|
.toCompileTo('foobar');
|
||||||
true,
|
|
||||||
'foobar'
|
|
||||||
);
|
|
||||||
shouldCompileToWithPartials(
|
|
||||||
'foo {{> dude~}} ',
|
|
||||||
[{}, {}, { dude: 'bar' }],
|
|
||||||
true,
|
|
||||||
'foo bar'
|
|
||||||
);
|
|
||||||
shouldCompileToWithPartials(
|
|
||||||
'foo {{> dude}} ',
|
|
||||||
[{}, {}, { dude: 'bar' }],
|
|
||||||
true,
|
|
||||||
'foo bar '
|
|
||||||
);
|
|
||||||
|
|
||||||
shouldCompileToWithPartials(
|
expectTemplate('foo {{> dude~}} ')
|
||||||
'foo\n {{~> dude}} ',
|
.withPartials({ dude: 'bar' })
|
||||||
[{}, {}, { dude: 'bar' }],
|
.toCompileTo('foo bar');
|
||||||
true,
|
|
||||||
'foobar'
|
expectTemplate('foo {{> dude}} ')
|
||||||
);
|
.withPartials({ dude: 'bar' })
|
||||||
shouldCompileToWithPartials(
|
.toCompileTo('foo bar ');
|
||||||
'foo\n {{> dude}} ',
|
|
||||||
[{}, {}, { dude: 'bar' }],
|
expectTemplate('foo\n {{~> dude}} ')
|
||||||
true,
|
.withPartials({ dude: 'bar' })
|
||||||
'foo\n bar'
|
.toCompileTo('foobar');
|
||||||
);
|
|
||||||
|
expectTemplate('foo\n {{> dude}} ')
|
||||||
|
.withPartials({ dude: 'bar' })
|
||||||
|
.toCompileTo('foo\n bar');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should only strip whitespace once', function() {
|
it('should only strip whitespace once', function() {
|
||||||
var hash = { foo: 'bar' };
|
expectTemplate(' {{~foo~}} {{foo}} {{foo}} ')
|
||||||
|
.withInput({ foo: 'bar' })
|
||||||
shouldCompileTo(' {{~foo~}} {{foo}} {{foo}} ', hash, 'barbar bar ');
|
.toCompileTo('barbar bar ');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user