$(document).ready(function() { module("Utility"); test("utility: noConflict", function() { var underscore = _.noConflict(); ok(underscore.isUndefined(_), "The '_' variable has been returned to its previous state."); var intersection = underscore.intersect([-1, 0, 1, 2], [1, 2, 3, 4]); equals(intersection.join(', '), '1, 2', 'but the intersection function still works'); window._ = underscore; }); test("utility: identity", function() { var moe = {name : 'moe'}; equals(_.identity(moe), moe, 'moe is the same as his identity'); }); test("utility: uniqueId", function() { var ids = [], i = 0; while(i++ < 100) ids.push(_.uniqueId()); equals(_.uniq(ids).length, ids.length, 'can generate a globally-unique stream of ids'); }); test("utility: times", function() { var vals = []; _.times(3, function (i) { vals.push(i); }); ok(_.isEqual(vals, [0,1,2]), "is 0 indexed"); // vals = []; _(3).times(function (i) { vals.push(i); }); ok(_.isEqual(vals, [0,1,2]), "works as a wrapper"); }); test("utility: mixin", function() { _.mixin({ myReverse: function(string) { return string.split('').reverse().join(''); } }); equals(_.myReverse('panacea'), 'aecanap', 'mixed in a function to _'); equals(_('champ').myReverse(), 'pmahc', 'mixed in a function to the OOP wrapper'); }); test("utility: _.escape", function() { equals(_.escape("Curly & Moe"), "Curly & Moe"); equals(_.escape("Curly & Moe"), "Curly & Moe"); }); test("utility: template", function() { var basicTemplate = _.template("<%= thing %> is gettin' on my noives!"); var result = basicTemplate({thing : 'This'}); equals(result, "This is gettin' on my noives!", 'can do basic attribute interpolation'); var sansSemicolonTemplate = _.template("A <% this %> B"); equals(sansSemicolonTemplate(), "A B"); var backslashTemplate = _.template("<%= thing %> is \\ridanculous"); equals(backslashTemplate({thing: 'This'}), "This is \\ridanculous"); var escapeTemplate = _.template('<%= a ? "checked=\\"checked\\"" : "" %>'); equals(escapeTemplate({a: true}), 'checked="checked"', 'can handle slash escapes in interpolations.'); var fancyTemplate = _.template("
Just some text. Hey, I know this is silly but it aids consistency.
Just some text. Hey, I know this is silly but it aids consistency.