diff --git a/test/test.js b/test/test.js index 1c975813d..312faa780 100644 --- a/test/test.js +++ b/test/test.js @@ -935,7 +935,7 @@ deepEqual(Circle.prototype, expected); }); - test('should accept a falsey `object` argument', 1, function() { + test('should accept a falsey `prototype` argument', 1, function() { var actual = [], expected = _.map(falsey, function() { return {}; }); @@ -945,6 +945,18 @@ deepEqual(actual, expected); }); + + test('should ignore primitive `prototype` arguments and use an empty object instead', 1, function() { + var actual = [], + primitives = [1, true, 'a'], + expected = _.map(primitives, function() { return true; }); + + _.forEach(primitives, function(value, index) { + actual.push(_.isPlainObject(index ? _.create(value) : _.create())); + }); + + deepEqual(actual, expected); + }); }()); /*--------------------------------------------------------------------------*/ @@ -4979,6 +4991,125 @@ QUnit.module('lodash.template'); (function() { + test('should use a `with` statement by default', 1, function() { + var compiled = _.template('<%= index %><%= collection[index] %><% _.each(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 correct with `this` references', 2, function() { + var compiled = _.template('a<%= this.b %>c'); + + root.b = 'b'; + equal(compiled(), 'abc'); + delete root.b; + + 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"'); + }); + + test('should evaluate JavaScript in "evaluate" delimiters', 1, function() { + var compiled = _.template( + '
&<>"'\/
', + unescaped = '&<>"\'\/'; + + var compiled = _.template('<%- value %>
'); + 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 %>
' + ); + + equal(compiled({ 'value': 3 }), '6
'); + }); + + test('should work with custom `_.templateSettings` delimiters', 1, function() { + var settings = _.clone(_.templateSettings); + + _.templateSettings = { + 'escape': /\{\{-([\s\S]+?)\}\}/g, + 'evaluate': /\{\{([\s\S]+?)\}\}/g, + 'interpolate': /\{\{=([\s\S]+?)\}\}/g + }; + + var compiled = _.template('" + (value ? "yes" : "no") + "
")\n>>', compiled = _.template(expected, null, { 'evaluate': /<<(.+?)>>/g }), @@ -5122,10 +5269,30 @@ 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 resolve `null` and `undefined` values to empty strings', 4, function() { + var compiled = _.template('<%= a %><%- a %>'); + strictEqual(compiled({ 'a': null }), ''); + strictEqual(compiled({ 'a': undefined }), ''); + + compiled = _.template('<%= a.b %><%- a.b %>'); + strictEqual(compiled({ 'a': {} }), ''); + 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 match delimiters before escaping text', 1, function() { + var compiled = _.template('<<\n a \n>>', null, { 'evaluate': /<<(.*?)>>/g }); + equal(compiled(), '<<\n a \n>>'); + }); }()); /*--------------------------------------------------------------------------*/