(function() { var templateSettings; module('Utility', { setup: function() { templateSettings = _.clone(_.templateSettings); }, teardown: function() { _.templateSettings = templateSettings; } }); test('#750 - Return _ instance.', 2, function() { var instance = _([]); ok(_(instance) === instance); ok(new _(instance) === instance); }); test('identity', function() { var moe = {name : 'moe'}; equal(_.identity(moe), moe, 'moe is the same as his identity'); }); test('constant', function() { var moe = {name : 'moe'}; equal(_.constant(moe)(), moe, 'should create a function that returns moe'); }); test('noop', function() { strictEqual(_.noop('curly', 'larry', 'moe'), undefined, 'should always return undefined'); }); test('property', function() { var moe = {name : 'moe'}; equal(_.property('name')(moe), 'moe', 'should return the property with the given name'); }); test('random', function() { var array = _.range(1000); var min = Math.pow(2, 31); var max = Math.pow(2, 62); ok(_.every(array, function() { return _.random(min, max) >= min; }), 'should produce a random number greater than or equal to the minimum number'); ok(_.some(array, function() { return _.random(Number.MAX_VALUE) > 0; }), 'should produce a random number when passed `Number.MAX_VALUE`'); }); test('now', function() { var diff = _.now() - new Date().getTime(); ok(diff <= 0 && diff > -5, 'Produces the correct time in milliseconds');//within 5ms }); test('uniqueId', function() { var ids = [], i = 0; while (i++ < 100) ids.push(_.uniqueId()); equal(_.uniq(ids).length, ids.length, 'can generate a globally-unique stream of ids'); }); test('times', function() { var vals = []; _.times(3, function (i) { vals.push(i); }); deepEqual(vals, [0, 1, 2], 'is 0 indexed'); // vals = []; _(3).times(function(i) { vals.push(i); }); deepEqual(vals, [0, 1, 2], 'works as a wrapper'); // collects return values deepEqual([0, 1, 2], _.times(3, function(i) { return i; }), 'collects return values'); deepEqual(_.times(0, _.identity), []); deepEqual(_.times(-1, _.identity), []); deepEqual(_.times(parseFloat('-Infinity'), _.identity), []); }); test('mixin', function() { _.mixin({ myReverse: function(string) { return string.split('').reverse().join(''); } }); equal(_.myReverse('panacea'), 'aecanap', 'mixed in a function to _'); equal(_('champ').myReverse(), 'pmahc', 'mixed in a function to the OOP wrapper'); }); test('_.escape', function() { equal(_.escape(null), ''); }); test('_.unescape', function() { var string = 'Curly & Moe'; equal(_.unescape(null), ''); equal(_.unescape(_.escape(string)), string); equal(_.unescape(string), string, 'don\'t unescape unnecessarily'); }); // Don't care what they escape them to just that they're escaped and can be unescaped test('_.escape & unescape', function() { // test & (&) seperately obviously var escapeCharacters = ['<', '>', '"', '\'', '`']; _.each(escapeCharacters, function(escapeChar) { var str = 'a ' + escapeChar + ' string escaped'; var escaped = _.escape(str); notEqual(str, escaped, escapeChar + ' is escaped'); equal(str, _.unescape(escaped), escapeChar + ' can be unescaped'); str = 'a ' + escapeChar + escapeChar + escapeChar + 'some more string' + escapeChar; escaped = _.escape(str); equal(escaped.indexOf(escapeChar), -1, 'can escape multiple occurances of ' + escapeChar); equal(_.unescape(escaped), str, 'multiple occurrences of ' + escapeChar + ' can be unescaped'); }); // handles multiple escape characters at once var joiner = ' other stuff '; var allEscaped = escapeCharacters.join(joiner); allEscaped += allEscaped; ok(_.every(escapeCharacters, function(escapeChar) { return allEscaped.indexOf(escapeChar) !== -1; }), 'handles multiple characters'); ok(allEscaped.indexOf(joiner) >= 0, 'can escape multiple escape characters at the same time'); // test & -> & var str = 'some string & another string & yet another'; var escaped = _.escape(str); ok(escaped.indexOf('&') !== -1, 'handles & aka &'); equal(_.unescape(str), str, 'can unescape &'); }); test('template', function() { var basicTemplate = _.template("<%= thing %> is gettin' on my noives!"); var result = basicTemplate({thing : 'This'}); equal(result, "This is gettin' on my noives!", 'can do basic attribute interpolation'); var sansSemicolonTemplate = _.template('A <% this %> B'); equal(sansSemicolonTemplate(), 'A B'); var backslashTemplate = _.template('<%= thing %> is \\ridanculous'); equal(backslashTemplate({thing: 'This'}), 'This is \\ridanculous'); var escapeTemplate = _.template('<%= a ? "checked=\\"checked\\"" : "" %>'); equal(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.