"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "vendor/underscore/test/utility.js" between
lodash-3.0.0.tar.gz and lodash-4.0.0.tar.gz

About: lodash is a modern JavaScript utility library delivering modularity, performance, & extras.

utility.js  (lodash-3.0.0):utility.js  (lodash-4.0.0)
(function() { (function() {
var _ = typeof require == 'function' ? require('..') : window._;
var templateSettings; var templateSettings;
module('Utility', { QUnit.module('Utility', {
setup: function() { beforeEach: function() {
templateSettings = _.clone(_.templateSettings); templateSettings = _.clone(_.templateSettings);
}, },
teardown: function() { afterEach: function() {
_.templateSettings = templateSettings; _.templateSettings = templateSettings;
} }
}); });
test('#750 - Return _ instance.', 2, function() { if (typeof this == 'object') {
QUnit.test('noConflict', function(assert) {
var underscore = _.noConflict();
assert.equal(underscore.identity(1), 1);
if (typeof require != 'function') {
assert.equal(this._, void 0, 'global underscore is removed');
this._ = underscore;
} else if (typeof global !== 'undefined') {
delete global._;
}
});
}
if (typeof require == 'function') {
QUnit.test('noConflict (node vm)', function(assert) {
assert.expect(2);
var done = assert.async();
var fs = require('fs');
var vm = require('vm');
var filename = __dirname + '/../underscore.js';
fs.readFile(filename, function(err, content){
var sandbox = vm.createScript(
content + 'this.underscore = this._.noConflict();',
filename
);
var context = {_: 'oldvalue'};
sandbox.runInNewContext(context);
assert.equal(context._, 'oldvalue');
assert.equal(context.underscore.VERSION, _.VERSION);
done();
});
});
}
QUnit.test('#750 - Return _ instance.', function(assert) {
assert.expect(2);
var instance = _([]); var instance = _([]);
ok(_(instance) === instance); assert.ok(_(instance) === instance);
ok(new _(instance) === instance); assert.ok(new _(instance) === instance);
});
QUnit.test('identity', function(assert) {
var stooge = {name: 'moe'};
assert.equal(_.identity(stooge), stooge, 'stooge is the same as his identity
');
}); });
test('identity', function() { QUnit.test('constant', function(assert) {
var moe = {name : 'moe'}; var stooge = {name: 'moe'};
equal(_.identity(moe), moe, 'moe is the same as his identity'); assert.equal(_.constant(stooge)(), stooge, 'should create a function that re
turns stooge');
}); });
test('constant', function() { QUnit.test('noop', function(assert) {
var moe = {name : 'moe'}; assert.strictEqual(_.noop('curly', 'larry', 'moe'), void 0, 'should always r
equal(_.constant(moe)(), moe, 'should create a function that returns moe'); eturn undefined');
}); });
test('noop', function() { QUnit.test('property', function(assert) {
strictEqual(_.noop('curly', 'larry', 'moe'), undefined, 'should always retur var stooge = {name: 'moe'};
n undefined'); assert.equal(_.property('name')(stooge), 'moe', 'should return the property
with the given name');
assert.equal(_.property('name')(null), void 0, 'should return undefined for
null values');
assert.equal(_.property('name')(void 0), void 0, 'should return undefined fo
r undefined values');
}); });
test('property', function() { QUnit.test('propertyOf', function(assert) {
var moe = {name : 'moe'}; var stoogeRanks = _.propertyOf({curly: 2, moe: 1, larry: 3});
equal(_.property('name')(moe), 'moe', 'should return the property with the g assert.equal(stoogeRanks('curly'), 2, 'should return the property with the g
iven name'); iven name');
assert.equal(stoogeRanks(null), void 0, 'should return undefined for null va
lues');
assert.equal(stoogeRanks(void 0), void 0, 'should return undefined for undef
ined values');
function MoreStooges() { this.shemp = 87; }
MoreStooges.prototype = {curly: 2, moe: 1, larry: 3};
var moreStoogeRanks = _.propertyOf(new MoreStooges());
assert.equal(moreStoogeRanks('curly'), 2, 'should return properties from fur
ther up the prototype chain');
var nullPropertyOf = _.propertyOf(null);
assert.equal(nullPropertyOf('curly'), void 0, 'should return undefined when
obj is null');
var undefPropertyOf = _.propertyOf(void 0);
assert.equal(undefPropertyOf('curly'), void 0, 'should return undefined when
obj is undefined');
}); });
test('random', function() { QUnit.test('random', function(assert) {
var array = _.range(1000); var array = _.range(1000);
var min = Math.pow(2, 31); var min = Math.pow(2, 31);
var max = Math.pow(2, 62); var max = Math.pow(2, 62);
ok(_.every(array, function() { assert.ok(_.every(array, function() {
return _.random(min, max) >= min; return _.random(min, max) >= min;
}), 'should produce a random number greater than or equal to the minimum num ber'); }), 'should produce a random number greater than or equal to the minimum num ber');
ok(_.some(array, function() { assert.ok(_.some(array, function() {
return _.random(Number.MAX_VALUE) > 0; return _.random(Number.MAX_VALUE) > 0;
}), 'should produce a random number when passed `Number.MAX_VALUE`'); }), 'should produce a random number when passed `Number.MAX_VALUE`');
}); });
test('now', function() { QUnit.test('now', function(assert) {
var diff = _.now() - new Date().getTime(); var diff = _.now() - new Date().getTime();
ok(diff <= 0 && diff > -5, 'Produces the correct time in milliseconds');//wi thin 5ms assert.ok(diff <= 0 && diff > -5, 'Produces the correct time in milliseconds ');//within 5ms
}); });
test('uniqueId', function() { QUnit.test('uniqueId', function(assert) {
var ids = [], i = 0; var ids = [], i = 0;
while (i++ < 100) ids.push(_.uniqueId()); while (i++ < 100) ids.push(_.uniqueId());
equal(_.uniq(ids).length, ids.length, 'can generate a globally-unique stream of ids'); assert.equal(_.uniq(ids).length, ids.length, 'can generate a globally-unique stream of ids');
}); });
test('times', function() { QUnit.test('times', function(assert) {
var vals = []; var vals = [];
_.times(3, function (i) { vals.push(i); }); _.times(3, function(i) { vals.push(i); });
deepEqual(vals, [0, 1, 2], 'is 0 indexed'); assert.deepEqual(vals, [0, 1, 2], 'is 0 indexed');
// //
vals = []; vals = [];
_(3).times(function(i) { vals.push(i); }); _(3).times(function(i) { vals.push(i); });
deepEqual(vals, [0, 1, 2], 'works as a wrapper'); assert.deepEqual(vals, [0, 1, 2], 'works as a wrapper');
// collects return values // collects return values
deepEqual([0, 1, 2], _.times(3, function(i) { return i; }), 'collects return values'); assert.deepEqual([0, 1, 2], _.times(3, function(i) { return i; }), 'collects return values');
deepEqual(_.times(0, _.identity), []); assert.deepEqual(_.times(0, _.identity), []);
deepEqual(_.times(-1, _.identity), []); assert.deepEqual(_.times(-1, _.identity), []);
deepEqual(_.times(parseFloat('-Infinity'), _.identity), []); assert.deepEqual(_.times(parseFloat('-Infinity'), _.identity), []);
}); });
test('mixin', function() { QUnit.test('mixin', function(assert) {
_.mixin({ _.mixin({
myReverse: function(string) { myReverse: function(string) {
return string.split('').reverse().join(''); return string.split('').reverse().join('');
} }
}); });
equal(_.myReverse('panacea'), 'aecanap', 'mixed in a function to _'); assert.equal(_.myReverse('panacea'), 'aecanap', 'mixed in a function to _');
equal(_('champ').myReverse(), 'pmahc', 'mixed in a function to the OOP wrapp assert.equal(_('champ').myReverse(), 'pmahc', 'mixed in a function to the OO
er'); P wrapper');
}); });
test('_.escape', function() { QUnit.test('_.escape', function(assert) {
equal(_.escape(null), ''); assert.equal(_.escape(null), '');
}); });
test('_.unescape', function() { QUnit.test('_.unescape', function(assert) {
var string = 'Curly & Moe'; var string = 'Curly & Moe';
equal(_.unescape(null), ''); assert.equal(_.unescape(null), '');
equal(_.unescape(_.escape(string)), string); assert.equal(_.unescape(_.escape(string)), string);
equal(_.unescape(string), string, 'don\'t unescape unnecessarily'); assert.equal(_.unescape(string), string, 'don\'t unescape unnecessarily');
}); });
// Don't care what they escape them to just that they're escaped and can be un escaped // Don't care what they escape them to just that they're escaped and can be un escaped
test('_.escape & unescape', function() { QUnit.test('_.escape & unescape', function(assert) {
// test & (&amp;) seperately obviously // test & (&amp;) seperately obviously
var escapeCharacters = ['<', '>', '"', '\'', '`']; var escapeCharacters = ['<', '>', '"', '\'', '`'];
_.each(escapeCharacters, function(escapeChar) { _.each(escapeCharacters, function(escapeChar) {
var str = 'a ' + escapeChar + ' string escaped'; var s = 'a ' + escapeChar + ' string escaped';
var escaped = _.escape(str); var e = _.escape(s);
notEqual(str, escaped, escapeChar + ' is escaped'); assert.notEqual(s, e, escapeChar + ' is escaped');
equal(str, _.unescape(escaped), escapeChar + ' can be unescaped'); assert.equal(s, _.unescape(e), escapeChar + ' can be unescaped');
str = 'a ' + escapeChar + escapeChar + escapeChar + 'some more string' + e s = 'a ' + escapeChar + escapeChar + escapeChar + 'some more string' + esc
scapeChar; apeChar;
escaped = _.escape(str); e = _.escape(s);
equal(escaped.indexOf(escapeChar), -1, 'can escape multiple occurances of assert.equal(e.indexOf(escapeChar), -1, 'can escape multiple occurances of
' + escapeChar); ' + escapeChar);
equal(_.unescape(escaped), str, 'multiple occurrences of ' + escapeChar + assert.equal(_.unescape(e), s, 'multiple occurrences of ' + escapeChar + '
' can be unescaped'); can be unescaped');
}); });
// handles multiple escape characters at once // handles multiple escape characters at once
var joiner = ' other stuff '; var joiner = ' other stuff ';
var allEscaped = escapeCharacters.join(joiner); var allEscaped = escapeCharacters.join(joiner);
allEscaped += allEscaped; allEscaped += allEscaped;
ok(_.every(escapeCharacters, function(escapeChar) { assert.ok(_.every(escapeCharacters, function(escapeChar) {
return allEscaped.indexOf(escapeChar) !== -1; return allEscaped.indexOf(escapeChar) !== -1;
}), 'handles multiple characters'); }), 'handles multiple characters');
ok(allEscaped.indexOf(joiner) >= 0, 'can escape multiple escape characters a t the same time'); assert.ok(allEscaped.indexOf(joiner) >= 0, 'can escape multiple escape chara cters at the same time');
// test & -> &amp; // test & -> &amp;
var str = 'some string & another string & yet another'; var str = 'some string & another string & yet another';
var escaped = _.escape(str); var escaped = _.escape(str);
ok(escaped.indexOf('&') !== -1, 'handles & aka &amp;'); assert.ok(escaped.indexOf('&') !== -1, 'handles & aka &amp;');
equal(_.unescape(str), str, 'can unescape &amp;'); assert.equal(_.unescape(str), str, 'can unescape &amp;');
}); });
test('template', function() { QUnit.test('template', function(assert) {
var basicTemplate = _.template("<%= thing %> is gettin' on my noives!"); var basicTemplate = _.template("<%= thing %> is gettin' on my noives!");
var result = basicTemplate({thing : 'This'}); var result = basicTemplate({thing: 'This'});
equal(result, "This is gettin' on my noives!", 'can do basic attribute inter assert.equal(result, "This is gettin' on my noives!", 'can do basic attribut
polation'); e interpolation');
var sansSemicolonTemplate = _.template('A <% this %> B'); var sansSemicolonTemplate = _.template('A <% this %> B');
equal(sansSemicolonTemplate(), 'A B'); assert.equal(sansSemicolonTemplate(), 'A B');
var backslashTemplate = _.template('<%= thing %> is \\ridanculous'); var backslashTemplate = _.template('<%= thing %> is \\ridanculous');
equal(backslashTemplate({thing: 'This'}), 'This is \\ridanculous'); assert.equal(backslashTemplate({thing: 'This'}), 'This is \\ridanculous');
var escapeTemplate = _.template('<%= a ? "checked=\\"checked\\"" : "" %>'); var escapeTemplate = _.template('<%= a ? "checked=\\"checked\\"" : "" %>');
equal(escapeTemplate({a: true}), 'checked="checked"', 'can handle slash esca pes in interpolations.'); assert.equal(escapeTemplate({a: true}), 'checked="checked"', 'can handle sla sh escapes in interpolations.');
var fancyTemplate = _.template('<ul><% ' + var fancyTemplate = _.template('<ul><% ' +
' for (var key in people) { ' + ' for (var key in people) { ' +
'%><li><%= people[key] %></li><% } %></ul>'); '%><li><%= people[key] %></li><% } %></ul>');
result = fancyTemplate({people : {moe : 'Moe', larry : 'Larry', curly : 'Cur result = fancyTemplate({people: {moe: 'Moe', larry: 'Larry', curly: 'Curly'}
ly'}}); });
equal(result, '<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>', 'can run assert.equal(result, '<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>', 'c
arbitrary javascript in templates'); an run arbitrary javascript in templates');
var escapedCharsInJavascriptTemplate = _.template('<ul><% _.each(numbers.spl it("\\n"), function(item) { %><li><%= item %></li><% }) %></ul>'); var escapedCharsInJavascriptTemplate = _.template('<ul><% _.each(numbers.spl it("\\n"), function(item) { %><li><%= item %></li><% }) %></ul>');
result = escapedCharsInJavascriptTemplate({numbers: 'one\ntwo\nthree\nfour'} ); result = escapedCharsInJavascriptTemplate({numbers: 'one\ntwo\nthree\nfour'} );
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'); 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');
var namespaceCollisionTemplate = _.template('<%= pageCount %> <%= thumbnails [pageCount] %> <% _.each(thumbnails, function(p) { %><div class="thumbnail" rel= "<%= p %>"></div><% }); %>'); var namespaceCollisionTemplate = _.template('<%= pageCount %> <%= thumbnails [pageCount] %> <% _.each(thumbnails, function(p) { %><div class="thumbnail" rel= "<%= p %>"></div><% }); %>');
result = namespaceCollisionTemplate({ result = namespaceCollisionTemplate({
pageCount: 3, pageCount: 3,
thumbnails: { thumbnails: {
1: 'p1-thumbnail.gif', 1: 'p1-thumbnail.gif',
2: 'p2-thumbnail.gif', 2: 'p2-thumbnail.gif',
3: 'p3-thumbnail.gif' 3: 'p3-thumbnail.gif'
} }
}); });
equal(result, '3 p3-thumbnail.gif <div class="thumbnail" rel="p1-thumbnail.g if"></div><div class="thumbnail" rel="p2-thumbnail.gif"></div><div class="thumbn ail" rel="p3-thumbnail.gif"></div>'); assert.equal(result, '3 p3-thumbnail.gif <div class="thumbnail" rel="p1-thum bnail.gif"></div><div class="thumbnail" rel="p2-thumbnail.gif"></div><div class= "thumbnail" rel="p3-thumbnail.gif"></div>');
var noInterpolateTemplate = _.template('<div><p>Just some text. Hey, I know this is silly but it aids consistency.</p></div>'); var noInterpolateTemplate = _.template('<div><p>Just some text. Hey, I know this is silly but it aids consistency.</p></div>');
result = noInterpolateTemplate(); result = noInterpolateTemplate();
equal(result, '<div><p>Just some text. Hey, I know this is silly but it aids consistency.</p></div>'); assert.equal(result, '<div><p>Just some text. Hey, I know this is silly but it aids consistency.</p></div>');
var quoteTemplate = _.template("It's its, not it's"); var quoteTemplate = _.template("It's its, not it's");
equal(quoteTemplate({}), "It's its, not it's"); assert.equal(quoteTemplate({}), "It's its, not it's");
var quoteInStatementAndBody = _.template('<% ' + var quoteInStatementAndBody = _.template('<% ' +
" if(foo == 'bar'){ " + " if(foo == 'bar'){ " +
"%>Statement quotes and 'quotes'.<% } %>"); "%>Statement quotes and 'quotes'.<% } %>");
equal(quoteInStatementAndBody({foo: 'bar'}), "Statement quotes and 'quotes'. "); assert.equal(quoteInStatementAndBody({foo: 'bar'}), "Statement quotes and 'q uotes'.");
var withNewlinesAndTabs = _.template('This\n\t\tis: <%= x %>.\n\tok.\nend.') ; var withNewlinesAndTabs = _.template('This\n\t\tis: <%= x %>.\n\tok.\nend.') ;
equal(withNewlinesAndTabs({x: 'that'}), 'This\n\t\tis: that.\n\tok.\nend.'); assert.equal(withNewlinesAndTabs({x: 'that'}), 'This\n\t\tis: that.\n\tok.\n end.');
var template = _.template('<i><%- value %></i>'); var template = _.template('<i><%- value %></i>');
result = template({value: '<script>'}); result = template({value: '<script>'});
equal(result, '<i>&lt;script&gt;</i>'); assert.equal(result, '<i>&lt;script&gt;</i>');
var stooge = { var stooge = {
name: 'Moe', name: 'Moe',
template: _.template("I'm <%= this.name %>") template: _.template("I'm <%= this.name %>")
}; };
equal(stooge.template(), "I'm Moe"); assert.equal(stooge.template(), "I'm Moe");
template = _.template('\n ' + template = _.template('\n ' +
' <%\n ' + ' <%\n ' +
' // a comment\n ' + ' // a comment\n ' +
' if (data) { data += 12345; }; %>\n ' + ' if (data) { data += 12345; }; %>\n ' +
' <li><%= data %></li>\n ' ' <li><%= data %></li>\n '
); );
equal(template({data : 12345}).replace(/\s/g, ''), '<li>24690</li>'); assert.equal(template({data: 12345}).replace(/\s/g, ''), '<li>24690</li>');
_.templateSettings = { _.templateSettings = {
evaluate : /\{\{([\s\S]+?)\}\}/g, evaluate: /\{\{([\s\S]+?)\}\}/g,
interpolate : /\{\{=([\s\S]+?)\}\}/g interpolate: /\{\{=([\s\S]+?)\}\}/g
}; };
var custom = _.template('<ul>{{ for (var key in people) { }}<li>{{= people[k ey] }}</li>{{ } }}</ul>'); var custom = _.template('<ul>{{ for (var key in people) { }}<li>{{= people[k ey] }}</li>{{ } }}</ul>');
result = custom({people : {moe : 'Moe', larry : 'Larry', curly : 'Curly'}}); result = custom({people: {moe: 'Moe', larry: 'Larry', curly: 'Curly'}});
equal(result, '<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>', 'can run assert.equal(result, '<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>', 'c
arbitrary javascript in templates'); an run arbitrary javascript in templates');
var customQuote = _.template("It's its, not it's"); var customQuote = _.template("It's its, not it's");
equal(customQuote({}), "It's its, not it's"); assert.equal(customQuote({}), "It's its, not it's");
quoteInStatementAndBody = _.template("{{ if(foo == 'bar'){ }}Statement quote s and 'quotes'.{{ } }}"); quoteInStatementAndBody = _.template("{{ if(foo == 'bar'){ }}Statement quote s and 'quotes'.{{ } }}");
equal(quoteInStatementAndBody({foo: 'bar'}), "Statement quotes and 'quotes'. "); assert.equal(quoteInStatementAndBody({foo: 'bar'}), "Statement quotes and 'q uotes'.");
_.templateSettings = { _.templateSettings = {
evaluate : /<\?([\s\S]+?)\?>/g, evaluate: /<\?([\s\S]+?)\?>/g,
interpolate : /<\?=([\s\S]+?)\?>/g interpolate: /<\?=([\s\S]+?)\?>/g
}; };
var customWithSpecialChars = _.template('<ul><? for (var key in people) { ?> <li><?= people[key] ?></li><? } ?></ul>'); var customWithSpecialChars = _.template('<ul><? for (var key in people) { ?> <li><?= people[key] ?></li><? } ?></ul>');
result = customWithSpecialChars({people : {moe : 'Moe', larry : 'Larry', cur result = customWithSpecialChars({people: {moe: 'Moe', larry: 'Larry', curly:
ly : 'Curly'}}); 'Curly'}});
equal(result, '<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>', 'can run assert.equal(result, '<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>', 'c
arbitrary javascript in templates'); an run arbitrary javascript in templates');
var customWithSpecialCharsQuote = _.template("It's its, not it's"); var customWithSpecialCharsQuote = _.template("It's its, not it's");
equal(customWithSpecialCharsQuote({}), "It's its, not it's"); assert.equal(customWithSpecialCharsQuote({}), "It's its, not it's");
quoteInStatementAndBody = _.template("<? if(foo == 'bar'){ ?>Statement quote s and 'quotes'.<? } ?>"); quoteInStatementAndBody = _.template("<? if(foo == 'bar'){ ?>Statement quote s and 'quotes'.<? } ?>");
equal(quoteInStatementAndBody({foo: 'bar'}), "Statement quotes and 'quotes'. "); assert.equal(quoteInStatementAndBody({foo: 'bar'}), "Statement quotes and 'q uotes'.");
_.templateSettings = { _.templateSettings = {
interpolate : /\{\{(.+?)\}\}/g interpolate: /\{\{(.+?)\}\}/g
}; };
var mustache = _.template('Hello {{planet}}!'); var mustache = _.template('Hello {{planet}}!');
equal(mustache({planet : 'World'}), 'Hello World!', 'can mimic mustache.js') ; assert.equal(mustache({planet: 'World'}), 'Hello World!', 'can mimic mustach e.js');
var templateWithNull = _.template('a null undefined {{planet}}'); var templateWithNull = _.template('a null undefined {{planet}}');
equal(templateWithNull({planet : 'world'}), 'a null undefined world', 'can h andle missing escape and evaluate settings'); assert.equal(templateWithNull({planet: 'world'}), 'a null undefined world', 'can handle missing escape and evaluate settings');
}); });
test('_.template provides the generated function source, when a SyntaxError oc QUnit.test('_.template provides the generated function source, when a SyntaxEr
curs', function() { ror occurs', function(assert) {
var source;
try { try {
_.template('<b><%= if x %></b>'); _.template('<b><%= if x %></b>');
} catch (ex) { } catch (ex) {
var source = ex.source; source = ex.source;
} }
ok(/__p/.test(source)); assert.ok(/__p/.test(source));
}); });
test('_.template handles \\u2028 & \\u2029', function() { QUnit.test('_.template handles \\u2028 & \\u2029', function(assert) {
var tmpl = _.template('<p>\u2028<%= "\\u2028\\u2029" %>\u2029</p>'); var tmpl = _.template('<p>\u2028<%= "\\u2028\\u2029" %>\u2029</p>');
strictEqual(tmpl(), '<p>\u2028\u2028\u2029\u2029</p>'); assert.strictEqual(tmpl(), '<p>\u2028\u2028\u2029\u2029</p>');
}); });
test('result calls functions and returns primitives', function() { QUnit.test('result calls functions and returns primitives', function(assert) {
var obj = {w: '', x: 'x', y: function(){ return this.x; }}; var obj = {w: '', x: 'x', y: function(){ return this.x; }};
strictEqual(_.result(obj, 'w'), ''); assert.strictEqual(_.result(obj, 'w'), '');
strictEqual(_.result(obj, 'x'), 'x'); assert.strictEqual(_.result(obj, 'x'), 'x');
strictEqual(_.result(obj, 'y'), 'x'); assert.strictEqual(_.result(obj, 'y'), 'x');
strictEqual(_.result(obj, 'z'), undefined); assert.strictEqual(_.result(obj, 'z'), void 0);
strictEqual(_.result(null, 'x'), undefined); assert.strictEqual(_.result(null, 'x'), void 0);
});
QUnit.test('result returns a default value if object is null or undefined', fu
nction(assert) {
assert.strictEqual(_.result(null, 'b', 'default'), 'default');
assert.strictEqual(_.result(void 0, 'c', 'default'), 'default');
assert.strictEqual(_.result(''.match('missing'), 1, 'default'), 'default');
});
QUnit.test('result returns a default value if property of object is missing',
function(assert) {
assert.strictEqual(_.result({d: null}, 'd', 'default'), null);
assert.strictEqual(_.result({e: false}, 'e', 'default'), false);
});
QUnit.test('result only returns the default value if the object does not have
the property or is undefined', function(assert) {
assert.strictEqual(_.result({}, 'b', 'default'), 'default');
assert.strictEqual(_.result({d: void 0}, 'd', 'default'), 'default');
});
QUnit.test('result does not return the default if the property of an object is
found in the prototype', function(assert) {
var Foo = function(){};
Foo.prototype.bar = 1;
assert.strictEqual(_.result(new Foo, 'bar', 2), 1);
});
QUnit.test('result does use the fallback when the result of invoking the prope
rty is undefined', function(assert) {
var obj = {a: function() {}};
assert.strictEqual(_.result(obj, 'a', 'failed'), void 0);
});
QUnit.test('result fallback can use a function', function(assert) {
var obj = {a: [1, 2, 3]};
assert.strictEqual(_.result(obj, 'b', _.constant(5)), 5);
assert.strictEqual(_.result(obj, 'b', function() {
return this.a;
}), obj.a, 'called with context');
}); });
test('_.templateSettings.variable', function() { QUnit.test('_.templateSettings.variable', function(assert) {
var s = '<%=data.x%>'; var s = '<%=data.x%>';
var data = {x: 'x'}; var data = {x: 'x'};
var tmp = _.template(s, {variable: 'data'}); var tmp = _.template(s, {variable: 'data'});
strictEqual(tmp(data), 'x'); assert.strictEqual(tmp(data), 'x');
_.templateSettings.variable = 'data'; _.templateSettings.variable = 'data';
strictEqual(_.template(s)(data), 'x'); assert.strictEqual(_.template(s)(data), 'x');
}); });
test('#547 - _.templateSettings is unchanged by custom settings.', function() QUnit.test('#547 - _.templateSettings is unchanged by custom settings.', funct
{ ion(assert) {
ok(!_.templateSettings.variable); assert.ok(!_.templateSettings.variable);
_.template('', {}, {variable: 'x'}); _.template('', {}, {variable: 'x'});
ok(!_.templateSettings.variable); assert.ok(!_.templateSettings.variable);
}); });
test('#556 - undefined template variables.', function() { QUnit.test('#556 - undefined template variables.', function(assert) {
var template = _.template('<%=x%>'); var template = _.template('<%=x%>');
strictEqual(template({x: null}), ''); assert.strictEqual(template({x: null}), '');
strictEqual(template({x: undefined}), ''); assert.strictEqual(template({x: void 0}), '');
var templateEscaped = _.template('<%-x%>'); var templateEscaped = _.template('<%-x%>');
strictEqual(templateEscaped({x: null}), ''); assert.strictEqual(templateEscaped({x: null}), '');
strictEqual(templateEscaped({x: undefined}), ''); assert.strictEqual(templateEscaped({x: void 0}), '');
var templateWithProperty = _.template('<%=x.foo%>'); var templateWithProperty = _.template('<%=x.foo%>');
strictEqual(templateWithProperty({x: {}}), ''); assert.strictEqual(templateWithProperty({x: {}}), '');
strictEqual(templateWithProperty({x: {}}), ''); assert.strictEqual(templateWithProperty({x: {}}), '');
var templateWithPropertyEscaped = _.template('<%-x.foo%>'); var templateWithPropertyEscaped = _.template('<%-x.foo%>');
strictEqual(templateWithPropertyEscaped({x: {}}), ''); assert.strictEqual(templateWithPropertyEscaped({x: {}}), '');
strictEqual(templateWithPropertyEscaped({x: {}}), ''); assert.strictEqual(templateWithPropertyEscaped({x: {}}), '');
}); });
test('interpolate evaluates code only once.', 2, function() { QUnit.test('interpolate evaluates code only once.', function(assert) {
assert.expect(2);
var count = 0; var count = 0;
var template = _.template('<%= f() %>'); var template = _.template('<%= f() %>');
template({f: function(){ ok(!count++); }}); template({f: function(){ assert.ok(!count++); }});
var countEscaped = 0; var countEscaped = 0;
var templateEscaped = _.template('<%- f() %>'); var templateEscaped = _.template('<%- f() %>');
templateEscaped({f: function(){ ok(!countEscaped++); }}); templateEscaped({f: function(){ assert.ok(!countEscaped++); }});
}); });
test('#746 - _.template settings are not modified.', 1, function() { QUnit.test('#746 - _.template settings are not modified.', function(assert) {
assert.expect(1);
var settings = {}; var settings = {};
_.template('', null, settings); _.template('', null, settings);
deepEqual(settings, {}); assert.deepEqual(settings, {});
}); });
test('#779 - delimeters are applied to unescaped text.', 1, function() { QUnit.test('#779 - delimeters are applied to unescaped text.', function(assert
) {
assert.expect(1);
var template = _.template('<<\nx\n>>', null, {evaluate: /<<(.*?)>>/g}); var template = _.template('<<\nx\n>>', null, {evaluate: /<<(.*?)>>/g});
strictEqual(template(), '<<\nx\n>>'); assert.strictEqual(template(), '<<\nx\n>>');
}); });
}()); }());
 End of changes. 84 change blocks. 
134 lines changed or deleted 245 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)