2 var _ = typeof require == 'function' ? require('..') : window._;
5 QUnit.module('Utility', {
7 beforeEach: function() {
8 templateSettings = _.clone(_.templateSettings);
11 afterEach: function() {
12 _.templateSettings = templateSettings;
17 if (typeof this == 'object') {
18 QUnit.test('noConflict', function(assert) {
19 var underscore = _.noConflict();
20 assert.equal(underscore.identity(1), 1);
21 if (typeof require != 'function') {
22 assert.equal(this._, void 0, 'global underscore is removed');
24 } else if (typeof global !== 'undefined') {
30 if (typeof require == 'function') {
31 QUnit.test('noConflict (node vm)', function(assert) {
33 var done = assert.async();
34 var fs = require('fs');
35 var vm = require('vm');
36 var filename = __dirname + '/../underscore.js';
37 fs.readFile(filename, function(err, content){
38 var sandbox = vm.createScript(
39 content + 'this.underscore = this._.noConflict();',
42 var context = {_: 'oldvalue'};
43 sandbox.runInNewContext(context);
44 assert.equal(context._, 'oldvalue');
45 assert.equal(context.underscore.VERSION, _.VERSION);
52 QUnit.test('#750 - Return _ instance.', function(assert) {
55 assert.ok(_(instance) === instance);
56 assert.ok(new _(instance) === instance);
59 QUnit.test('identity', function(assert) {
60 var stooge = {name: 'moe'};
61 assert.equal(_.identity(stooge), stooge, 'stooge is the same as his identity');
64 QUnit.test('constant', function(assert) {
65 var stooge = {name: 'moe'};
66 assert.equal(_.constant(stooge)(), stooge, 'should create a function that returns stooge');
69 QUnit.test('noop', function(assert) {
70 assert.strictEqual(_.noop('curly', 'larry', 'moe'), void 0, 'should always return undefined');
73 QUnit.test('property', function(assert) {
74 var stooge = {name: 'moe'};
75 assert.equal(_.property('name')(stooge), 'moe', 'should return the property with the given name');
76 assert.equal(_.property('name')(null), void 0, 'should return undefined for null values');
77 assert.equal(_.property('name')(void 0), void 0, 'should return undefined for undefined values');
80 QUnit.test('propertyOf', function(assert) {
81 var stoogeRanks = _.propertyOf({curly: 2, moe: 1, larry: 3});
82 assert.equal(stoogeRanks('curly'), 2, 'should return the property with the given name');
83 assert.equal(stoogeRanks(null), void 0, 'should return undefined for null values');
84 assert.equal(stoogeRanks(void 0), void 0, 'should return undefined for undefined values');
86 function MoreStooges() { this.shemp = 87; }
87 MoreStooges.prototype = {curly: 2, moe: 1, larry: 3};
88 var moreStoogeRanks = _.propertyOf(new MoreStooges());
89 assert.equal(moreStoogeRanks('curly'), 2, 'should return properties from further up the prototype chain');
91 var nullPropertyOf = _.propertyOf(null);
92 assert.equal(nullPropertyOf('curly'), void 0, 'should return undefined when obj is null');
94 var undefPropertyOf = _.propertyOf(void 0);
95 assert.equal(undefPropertyOf('curly'), void 0, 'should return undefined when obj is undefined');
98 QUnit.test('random', function(assert) {
99 var array = _.range(1000);
100 var min = Math.pow(2, 31);
101 var max = Math.pow(2, 62);
103 assert.ok(_.every(array, function() {
104 return _.random(min, max) >= min;
105 }), 'should produce a random number greater than or equal to the minimum number');
107 assert.ok(_.some(array, function() {
108 return _.random(Number.MAX_VALUE) > 0;
109 }), 'should produce a random number when passed `Number.MAX_VALUE`');
112 QUnit.test('now', function(assert) {
113 var diff = _.now() - new Date().getTime();
114 assert.ok(diff <= 0 && diff > -5, 'Produces the correct time in milliseconds');//within 5ms
117 QUnit.test('uniqueId', function(assert) {
119 while (i++ < 100) ids.push(_.uniqueId());
120 assert.equal(_.uniq(ids).length, ids.length, 'can generate a globally-unique stream of ids');
123 QUnit.test('times', function(assert) {
125 _.times(3, function(i) { vals.push(i); });
126 assert.deepEqual(vals, [0, 1, 2], 'is 0 indexed');
129 _(3).times(function(i) { vals.push(i); });
130 assert.deepEqual(vals, [0, 1, 2], 'works as a wrapper');
131 // collects return values
132 assert.deepEqual([0, 1, 2], _.times(3, function(i) { return i; }), 'collects return values');
134 assert.deepEqual(_.times(0, _.identity), []);
135 assert.deepEqual(_.times(-1, _.identity), []);
136 assert.deepEqual(_.times(parseFloat('-Infinity'), _.identity), []);
139 QUnit.test('mixin', function(assert) {
141 myReverse: function(string) {
142 return string.split('').reverse().join('');
145 assert.equal(_.myReverse('panacea'), 'aecanap', 'mixed in a function to _');
146 assert.equal(_('champ').myReverse(), 'pmahc', 'mixed in a function to the OOP wrapper');
149 QUnit.test('_.escape', function(assert) {
150 assert.equal(_.escape(null), '');
153 QUnit.test('_.unescape', function(assert) {
154 var string = 'Curly & Moe';
155 assert.equal(_.unescape(null), '');
156 assert.equal(_.unescape(_.escape(string)), string);
157 assert.equal(_.unescape(string), string, 'don\'t unescape unnecessarily');
160 // Don't care what they escape them to just that they're escaped and can be unescaped
161 QUnit.test('_.escape & unescape', function(assert) {
162 // test & (&) seperately obviously
163 var escapeCharacters = ['<', '>', '"', '\'', '`'];
165 _.each(escapeCharacters, function(escapeChar) {
166 var s = 'a ' + escapeChar + ' string escaped';
168 assert.notEqual(s, e, escapeChar + ' is escaped');
169 assert.equal(s, _.unescape(e), escapeChar + ' can be unescaped');
171 s = 'a ' + escapeChar + escapeChar + escapeChar + 'some more string' + escapeChar;
174 assert.equal(e.indexOf(escapeChar), -1, 'can escape multiple occurances of ' + escapeChar);
175 assert.equal(_.unescape(e), s, 'multiple occurrences of ' + escapeChar + ' can be unescaped');
178 // handles multiple escape characters at once
179 var joiner = ' other stuff ';
180 var allEscaped = escapeCharacters.join(joiner);
181 allEscaped += allEscaped;
182 assert.ok(_.every(escapeCharacters, function(escapeChar) {
183 return allEscaped.indexOf(escapeChar) !== -1;
184 }), 'handles multiple characters');
185 assert.ok(allEscaped.indexOf(joiner) >= 0, 'can escape multiple escape characters at the same time');
188 var str = 'some string & another string & yet another';
189 var escaped = _.escape(str);
191 assert.ok(escaped.indexOf('&') !== -1, 'handles & aka &');
192 assert.equal(_.unescape(str), str, 'can unescape &');
195 QUnit.test('template', function(assert) {
196 var basicTemplate = _.template("<%= thing %> is gettin' on my noives!");
197 var result = basicTemplate({thing: 'This'});
198 assert.equal(result, "This is gettin' on my noives!", 'can do basic attribute interpolation');
200 var sansSemicolonTemplate = _.template('A <% this %> B');
201 assert.equal(sansSemicolonTemplate(), 'A B');
203 var backslashTemplate = _.template('<%= thing %> is \\ridanculous');
204 assert.equal(backslashTemplate({thing: 'This'}), 'This is \\ridanculous');
206 var escapeTemplate = _.template('<%= a ? "checked=\\"checked\\"" : "" %>');
207 assert.equal(escapeTemplate({a: true}), 'checked="checked"', 'can handle slash escapes in interpolations.');
209 var fancyTemplate = _.template('<ul><% ' +
210 ' for (var key in people) { ' +
211 '%><li><%= people[key] %></li><% } %></ul>');
212 result = fancyTemplate({people: {moe: 'Moe', larry: 'Larry', curly: 'Curly'}});
213 assert.equal(result, '<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>', 'can run arbitrary javascript in templates');
215 var escapedCharsInJavascriptTemplate = _.template('<ul><% _.each(numbers.split("\\n"), function(item) { %><li><%= item %></li><% }) %></ul>');
216 result = escapedCharsInJavascriptTemplate({numbers: 'one\ntwo\nthree\nfour'});
217 assert.equal(result, '<ul><li>one</li><li>two</li><li>three</li><li>four</li></ul>', 'Can use escaped characters (e.g. \\n) in JavaScript');
219 var namespaceCollisionTemplate = _.template('<%= pageCount %> <%= thumbnails[pageCount] %> <% _.each(thumbnails, function(p) { %><div class="thumbnail" rel="<%= p %>"></div><% }); %>');
220 result = namespaceCollisionTemplate({
223 1: 'p1-thumbnail.gif',
224 2: 'p2-thumbnail.gif',
225 3: 'p3-thumbnail.gif'
228 assert.equal(result, '3 p3-thumbnail.gif <div class="thumbnail" rel="p1-thumbnail.gif"></div><div class="thumbnail" rel="p2-thumbnail.gif"></div><div class="thumbnail" rel="p3-thumbnail.gif"></div>');
230 var noInterpolateTemplate = _.template('<div><p>Just some text. Hey, I know this is silly but it aids consistency.</p></div>');
231 result = noInterpolateTemplate();
232 assert.equal(result, '<div><p>Just some text. Hey, I know this is silly but it aids consistency.</p></div>');
234 var quoteTemplate = _.template("It's its, not it's");
235 assert.equal(quoteTemplate({}), "It's its, not it's");
237 var quoteInStatementAndBody = _.template('<% ' +
238 " if(foo == 'bar'){ " +
239 "%>Statement quotes and 'quotes'.<% } %>");
240 assert.equal(quoteInStatementAndBody({foo: 'bar'}), "Statement quotes and 'quotes'.");
242 var withNewlinesAndTabs = _.template('This\n\t\tis: <%= x %>.\n\tok.\nend.');
243 assert.equal(withNewlinesAndTabs({x: 'that'}), 'This\n\t\tis: that.\n\tok.\nend.');
245 var template = _.template('<i><%- value %></i>');
246 result = template({value: '<script>'});
247 assert.equal(result, '<i><script></i>');
251 template: _.template("I'm <%= this.name %>")
253 assert.equal(stooge.template(), "I'm Moe");
255 template = _.template('\n ' +
258 ' if (data) { data += 12345; }; %>\n ' +
259 ' <li><%= data %></li>\n '
261 assert.equal(template({data: 12345}).replace(/\s/g, ''), '<li>24690</li>');
263 _.templateSettings = {
264 evaluate: /\{\{([\s\S]+?)\}\}/g,
265 interpolate: /\{\{=([\s\S]+?)\}\}/g
268 var custom = _.template('<ul>{{ for (var key in people) { }}<li>{{= people[key] }}</li>{{ } }}</ul>');
269 result = custom({people: {moe: 'Moe', larry: 'Larry', curly: 'Curly'}});
270 assert.equal(result, '<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>', 'can run arbitrary javascript in templates');
272 var customQuote = _.template("It's its, not it's");
273 assert.equal(customQuote({}), "It's its, not it's");
275 quoteInStatementAndBody = _.template("{{ if(foo == 'bar'){ }}Statement quotes and 'quotes'.{{ } }}");
276 assert.equal(quoteInStatementAndBody({foo: 'bar'}), "Statement quotes and 'quotes'.");
278 _.templateSettings = {
279 evaluate: /<\?([\s\S]+?)\?>/g,
280 interpolate: /<\?=([\s\S]+?)\?>/g
283 var customWithSpecialChars = _.template('<ul><? for (var key in people) { ?><li><?= people[key] ?></li><? } ?></ul>');
284 result = customWithSpecialChars({people: {moe: 'Moe', larry: 'Larry', curly: 'Curly'}});
285 assert.equal(result, '<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>', 'can run arbitrary javascript in templates');
287 var customWithSpecialCharsQuote = _.template("It's its, not it's");
288 assert.equal(customWithSpecialCharsQuote({}), "It's its, not it's");
290 quoteInStatementAndBody = _.template("<? if(foo == 'bar'){ ?>Statement quotes and 'quotes'.<? } ?>");
291 assert.equal(quoteInStatementAndBody({foo: 'bar'}), "Statement quotes and 'quotes'.");
293 _.templateSettings = {
294 interpolate: /\{\{(.+?)\}\}/g
297 var mustache = _.template('Hello {{planet}}!');
298 assert.equal(mustache({planet: 'World'}), 'Hello World!', 'can mimic mustache.js');
300 var templateWithNull = _.template('a null undefined {{planet}}');
301 assert.equal(templateWithNull({planet: 'world'}), 'a null undefined world', 'can handle missing escape and evaluate settings');
304 QUnit.test('_.template provides the generated function source, when a SyntaxError occurs', function(assert) {
307 _.template('<b><%= if x %></b>');
311 assert.ok(/__p/.test(source));
314 QUnit.test('_.template handles \\u2028 & \\u2029', function(assert) {
315 var tmpl = _.template('<p>\u2028<%= "\\u2028\\u2029" %>\u2029</p>');
316 assert.strictEqual(tmpl(), '<p>\u2028\u2028\u2029\u2029</p>');
319 QUnit.test('result calls functions and returns primitives', function(assert) {
320 var obj = {w: '', x: 'x', y: function(){ return this.x; }};
321 assert.strictEqual(_.result(obj, 'w'), '');
322 assert.strictEqual(_.result(obj, 'x'), 'x');
323 assert.strictEqual(_.result(obj, 'y'), 'x');
324 assert.strictEqual(_.result(obj, 'z'), void 0);
325 assert.strictEqual(_.result(null, 'x'), void 0);
328 QUnit.test('result returns a default value if object is null or undefined', function(assert) {
329 assert.strictEqual(_.result(null, 'b', 'default'), 'default');
330 assert.strictEqual(_.result(void 0, 'c', 'default'), 'default');
331 assert.strictEqual(_.result(''.match('missing'), 1, 'default'), 'default');
334 QUnit.test('result returns a default value if property of object is missing', function(assert) {
335 assert.strictEqual(_.result({d: null}, 'd', 'default'), null);
336 assert.strictEqual(_.result({e: false}, 'e', 'default'), false);
339 QUnit.test('result only returns the default value if the object does not have the property or is undefined', function(assert) {
340 assert.strictEqual(_.result({}, 'b', 'default'), 'default');
341 assert.strictEqual(_.result({d: void 0}, 'd', 'default'), 'default');
344 QUnit.test('result does not return the default if the property of an object is found in the prototype', function(assert) {
345 var Foo = function(){};
346 Foo.prototype.bar = 1;
347 assert.strictEqual(_.result(new Foo, 'bar', 2), 1);
350 QUnit.test('result does use the fallback when the result of invoking the property is undefined', function(assert) {
351 var obj = {a: function() {}};
352 assert.strictEqual(_.result(obj, 'a', 'failed'), void 0);
355 QUnit.test('result fallback can use a function', function(assert) {
356 var obj = {a: [1, 2, 3]};
357 assert.strictEqual(_.result(obj, 'b', _.constant(5)), 5);
358 assert.strictEqual(_.result(obj, 'b', function() {
360 }), obj.a, 'called with context');
363 QUnit.test('_.templateSettings.variable', function(assert) {
364 var s = '<%=data.x%>';
366 var tmp = _.template(s, {variable: 'data'});
367 assert.strictEqual(tmp(data), 'x');
368 _.templateSettings.variable = 'data';
369 assert.strictEqual(_.template(s)(data), 'x');
372 QUnit.test('#547 - _.templateSettings is unchanged by custom settings.', function(assert) {
373 assert.ok(!_.templateSettings.variable);
374 _.template('', {}, {variable: 'x'});
375 assert.ok(!_.templateSettings.variable);
378 QUnit.test('#556 - undefined template variables.', function(assert) {
379 var template = _.template('<%=x%>');
380 assert.strictEqual(template({x: null}), '');
381 assert.strictEqual(template({x: void 0}), '');
383 var templateEscaped = _.template('<%-x%>');
384 assert.strictEqual(templateEscaped({x: null}), '');
385 assert.strictEqual(templateEscaped({x: void 0}), '');
387 var templateWithProperty = _.template('<%=x.foo%>');
388 assert.strictEqual(templateWithProperty({x: {}}), '');
389 assert.strictEqual(templateWithProperty({x: {}}), '');
391 var templateWithPropertyEscaped = _.template('<%-x.foo%>');
392 assert.strictEqual(templateWithPropertyEscaped({x: {}}), '');
393 assert.strictEqual(templateWithPropertyEscaped({x: {}}), '');
396 QUnit.test('interpolate evaluates code only once.', function(assert) {
399 var template = _.template('<%= f() %>');
400 template({f: function(){ assert.ok(!count++); }});
402 var countEscaped = 0;
403 var templateEscaped = _.template('<%- f() %>');
404 templateEscaped({f: function(){ assert.ok(!countEscaped++); }});
407 QUnit.test('#746 - _.template settings are not modified.', function(assert) {
410 _.template('', null, settings);
411 assert.deepEqual(settings, {});
414 QUnit.test('#779 - delimeters are applied to unescaped text.', function(assert) {
416 var template = _.template('<<\nx\n>>', null, {evaluate: /<<(.*?)>>/g});
417 assert.strictEqual(template(), '<<\nx\n>>');