diff --git a/test/test.js b/test/test.js index 24ec3c722..68141e7aa 100644 --- a/test/test.js +++ b/test/test.js @@ -7139,7 +7139,6 @@ }); }(1, 2, 3)); - /*--------------------------------------------------------------------------*/ QUnit.module('lodash.slice'); @@ -7483,32 +7482,11 @@ QUnit.module('lodash.template'); (function() { - test('should use a `with` statement by default', 1, function() { - var compiled = _.template('<%= index %><%= collection[index] %><% _.forEach(collection, function(value, index) { %><%= index %><% }); %>'), - actual = compiled({ 'index': 1, 'collection': ['a', 'b', 'c'] }); - - equal(actual, '1b012'); - }); - test('should interpolate data object properties', 1, function() { var compiled = _.template('<%= a %>BC'); equal(compiled({ 'a': 'A' }), 'ABC'); }); - test('should work correctly with `this` references', 2, function() { - var compiled = _.template('a<%= this.String("b") %>c'); - equal(compiled(), 'abc'); - - var object = { 'b': 'B' }; - object.compiled = _.template('A<%= this.b %>C', null, { 'variable': 'obj' }); - equal(object.compiled(), 'ABC'); - }); - - test('should work with backslashes', 1, function() { - var compiled = _.template('<%= a %> \\b'); - equal(compiled({ 'a': 'A' }), 'A \\b'); - }); - test('should support escaped values in "interpolation" delimiters', 1, function() { var compiled = _.template('<%= a ? "a=\\"A\\"" : "" %>'); equal(compiled({ 'a': true }), 'a="A"'); @@ -7526,29 +7504,6 @@ equal(actual, '
&<>"'\/
', unescaped = '&<>"\'\/'; @@ -7557,77 +7512,21 @@ equal(compiled({ 'value': unescaped }), escaped); }); - test('should work with templates containing newlines and comments', 1, function() { - var compiled = _.template('<%\n\ - // comment\n\ - if (value) { value += 3; }\n\ - %><%= value %>
' - ); + test('should work with "interpolate" delimiters containing ternary operators', 1, function() { + var compiled = _.template('<%= value ? value : "b" %>'), + data = { 'value': 'a' }; - equal(compiled({ 'value': 3 }), '6
'); + equal(compiled(data), 'a'); }); - test('should work with custom `_.templateSettings` delimiters', 1, function() { - var settings = _.clone(_.templateSettings); + test('should work with "interpolate" delimiters containing global values', 1, function() { + var compiled = _.template('<%= typeof Math.abs %>'); - _.assign(_.templateSettings, { - 'escape': /\{\{-([\s\S]+?)\}\}/g, - 'evaluate': /\{\{([\s\S]+?)\}\}/g, - 'interpolate': /\{\{=([\s\S]+?)\}\}/g - }); - - var compiled = _.template('<%= value %>
' + ); + + equal(compiled({ 'value': 3 }), '6
'); + }); + test('should not error with IE conditional comments enabled (test with development build)', 1, function() { var compiled = _.template(''), pass = true; @@ -7705,23 +7713,6 @@ equal(compiled(data), ''); }); - test('should work with "interpolate" delimiters containing ternary operators', 1, function() { - var compiled = _.template('<%= value ? value : "b" %>'), - data = { 'value': 'a' }; - - equal(compiled(data), 'a'); - }); - - test('should work with "interpolate" delimiters containing global values', 1, function() { - var compiled = _.template('<%= typeof Math.abs %>'); - - try { - var actual = compiled(); - } catch(e) { } - - equal(actual, 'function'); - }); - test('should evaluate delimiters once', 1, function() { var actual = [], compiled = _.template('<%= func("a") %><%- func("b") %><% func("c") %>'); @@ -7730,37 +7721,9 @@ deepEqual(actual, ['a', 'b', 'c']); }); - test('should parse delimiters with newlines', 1, function() { - var expected = '<<\nprint("" + (value ? "yes" : "no") + "
")\n>>', - compiled = _.template(expected, null, { 'evaluate': /<<(.+?)>>/g }), - data = { 'value': true }; - - equal(compiled(data), expected); - }); - - test('should parse ES6 template delimiters', 2, function() { - var data = { 'value': 2 }; - strictEqual(_.template('1${value}3', data), '123'); - equal(_.template('${"{" + value + "\\}"}', data), '{2}'); - }); - - test('supports the "imports" option', 1, function() { - var options = { 'imports': { 'a': 1 } }, - compiled = _.template('<%= a %>', null, options); - - strictEqual(compiled({}), '1'); - }); - - test('should coerce `text` argument to a string', 1, function() { - var data = { 'a': 1 }, - object = { 'toString': function() { return '<%= a %>'; } }; - - strictEqual(_.template(object, data), '1'); - }); - - test('should handle \\u2028 & \\u2029 characters', 1, function() { - var compiled = _.template('\u2028<%= "\\u2028\\u2029" %>\u2029'); - strictEqual(compiled(), '\u2028\u2028\u2029\u2029'); + test('should match delimiters before escaping text', 1, function() { + var compiled = _.template('<<\n a \n>>', null, { 'evaluate': /<<(.*?)>>/g }); + equal(compiled(), '<<\n a \n>>'); }); test('should resolve `null` and `undefined` values to empty strings', 4, function() { @@ -7773,14 +7736,41 @@ strictEqual(compiled({ 'a': {} }), ''); }); - test('should support single line comments in "evaluate" delimiters (test production builds)', 1, function() { - var compiled = _.template('<% // comment %><% if (value) { %>yap<% } else { %>nope<% } %>'); - equal(compiled({ 'value': true }), 'yap'); + test('should parse delimiters with newlines', 1, function() { + var expected = '<<\nprint("" + (value ? "yes" : "no") + "
")\n>>', + compiled = _.template(expected, null, { 'evaluate': /<<(.+?)>>/g }), + data = { 'value': true }; + + equal(compiled(data), expected); }); - test('should match delimiters before escaping text', 1, function() { - var compiled = _.template('<<\n a \n>>', null, { 'evaluate': /<<(.*?)>>/g }); - equal(compiled(), '<<\n a \n>>'); + test('should support recursive calls', 1, function() { + var compiled = _.template('<%= a %><% a = _.template(c, obj) %><%= a %>'), + data = { 'a': 'A', 'b': 'B', 'c': '<%= b %>' }; + + equal(compiled(data), 'AB'); + }); + + test('should coerce `text` argument to a string', 1, function() { + var data = { 'a': 1 }, + object = { 'toString': function() { return '<%= a %>'; } }; + + strictEqual(_.template(object, data), '1'); + }); + + test('should not augment the `options` object', 1, function() { + var options = {}; + _.template('', {}, options); + deepEqual(options, {}); + }); + + test('should not modify `_.templateSettings` when `options` are provided', 2, function() { + equal('a' in _.templateSettings, false); + + _.template('', {}, { 'a': 1 }); + equal('a' in _.templateSettings, false); + + delete _.templateSettings.a; }); test('should not error for non-object `data` and `options` values', 2, function() { @@ -7802,6 +7792,15 @@ } ok(pass); }); + + test('should provide the template source when a SyntaxError occurs', 1, function() { + try { + _.template('<% if x %>'); + } catch(e) { + var source = e.source; + } + ok(/__p/.test(source)); + }); }()); /*--------------------------------------------------------------------------*/ @@ -8155,7 +8154,6 @@ }); }()); - /*--------------------------------------------------------------------------*/ QUnit.module('lodash.slice and lodash.toArray');