react / wstein / node_modules / jest-cli / node_modules / cover / node_modules / underscore.string / test / test_underscore / utility.js
80684 views$(document).ready(function() {12module("Utility");34test("utility: noConflict", function() {5var underscore = _.noConflict();6ok(underscore.isUndefined(_), "The '_' variable has been returned to its previous state.");7var intersection = underscore.intersect([-1, 0, 1, 2], [1, 2, 3, 4]);8equals(intersection.join(', '), '1, 2', 'but the intersection function still works');9window._ = underscore;10});1112test("utility: identity", function() {13var moe = {name : 'moe'};14equals(_.identity(moe), moe, 'moe is the same as his identity');15});1617test("utility: uniqueId", function() {18var ids = [], i = 0;19while(i++ < 100) ids.push(_.uniqueId());20equals(_.uniq(ids).length, ids.length, 'can generate a globally-unique stream of ids');21});2223test("utility: times", function() {24var vals = [];25_.times(3, function (i) { vals.push(i); });26ok(_.isEqual(vals, [0,1,2]), "is 0 indexed");27//28vals = [];29_(3).times(function (i) { vals.push(i); });30ok(_.isEqual(vals, [0,1,2]), "works as a wrapper");31});3233test("utility: mixin", function() {34_.mixin({35myReverse: function(string) {36return string.split('').reverse().join('');37}38});39equals(_.myReverse('panacea'), 'aecanap', 'mixed in a function to _');40equals(_('champ').myReverse(), 'pmahc', 'mixed in a function to the OOP wrapper');41});4243test("utility: _.escape", function() {44equals(_.escape("Curly & Moe"), "Curly & Moe");45equals(_.escape("Curly & Moe"), "Curly &amp; Moe");46});4748test("utility: template", function() {49var basicTemplate = _.template("<%= thing %> is gettin' on my noives!");50var result = basicTemplate({thing : 'This'});51equals(result, "This is gettin' on my noives!", 'can do basic attribute interpolation');5253var backslashTemplate = _.template("<%= thing %> is \\ridanculous");54equals(backslashTemplate({thing: 'This'}), "This is \\ridanculous");5556var fancyTemplate = _.template("<ul><% \57for (key in people) { \58%><li><%= people[key] %></li><% } %></ul>");59result = fancyTemplate({people : {moe : "Moe", larry : "Larry", curly : "Curly"}});60equals(result, "<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>", 'can run arbitrary javascript in templates');6162var namespaceCollisionTemplate = _.template("<%= pageCount %> <%= thumbnails[pageCount] %> <% _.each(thumbnails, function(p) { %><div class=\"thumbnail\" rel=\"<%= p %>\"></div><% }); %>");63result = namespaceCollisionTemplate({64pageCount: 3,65thumbnails: {661: "p1-thumbnail.gif",672: "p2-thumbnail.gif",683: "p3-thumbnail.gif"69}70});71equals(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>");7273var noInterpolateTemplate = _.template("<div><p>Just some text. Hey, I know this is silly but it aids consistency.</p></div>");74result = noInterpolateTemplate();75equals(result, "<div><p>Just some text. Hey, I know this is silly but it aids consistency.</p></div>");7677var quoteTemplate = _.template("It's its, not it's");78equals(quoteTemplate({}), "It's its, not it's");7980var quoteInStatementAndBody = _.template("<%\81if(foo == 'bar'){ \82%>Statement quotes and 'quotes'.<% } %>");83equals(quoteInStatementAndBody({foo: "bar"}), "Statement quotes and 'quotes'.");8485var withNewlinesAndTabs = _.template('This\n\t\tis: <%= x %>.\n\tok.\nend.');86equals(withNewlinesAndTabs({x: 'that'}), 'This\n\t\tis: that.\n\tok.\nend.');8788var template = _.template("<i><%- value %></i>");89var result = template({value: "<script>"});90equals(result, '<i><script></i>');9192if (!$.browser.msie) {93var fromHTML = _.template($('#template').html());94equals(fromHTML({data : 12345}).replace(/\s/g, ''), '<li>24690</li>');95}9697_.templateSettings = {98evaluate : /\{\{([\s\S]+?)\}\}/g,99interpolate : /\{\{=([\s\S]+?)\}\}/g100};101102var custom = _.template("<ul>{{ for (key in people) { }}<li>{{= people[key] }}</li>{{ } }}</ul>");103result = custom({people : {moe : "Moe", larry : "Larry", curly : "Curly"}});104equals(result, "<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>", 'can run arbitrary javascript in templates');105106var customQuote = _.template("It's its, not it's");107equals(customQuote({}), "It's its, not it's");108109var quoteInStatementAndBody = _.template("{{ if(foo == 'bar'){ }}Statement quotes and 'quotes'.{{ } }}");110equals(quoteInStatementAndBody({foo: "bar"}), "Statement quotes and 'quotes'.");111112_.templateSettings = {113evaluate : /<\?([\s\S]+?)\?>/g,114interpolate : /<\?=([\s\S]+?)\?>/g115};116117var customWithSpecialChars = _.template("<ul><? for (key in people) { ?><li><?= people[key] ?></li><? } ?></ul>");118result = customWithSpecialChars({people : {moe : "Moe", larry : "Larry", curly : "Curly"}});119equals(result, "<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>", 'can run arbitrary javascript in templates');120121var customWithSpecialCharsQuote = _.template("It's its, not it's");122equals(customWithSpecialCharsQuote({}), "It's its, not it's");123124var quoteInStatementAndBody = _.template("<? if(foo == 'bar'){ ?>Statement quotes and 'quotes'.<? } ?>");125equals(quoteInStatementAndBody({foo: "bar"}), "Statement quotes and 'quotes'.");126127_.templateSettings = {128interpolate : /\{\{(.+?)\}\}/g129};130131var mustache = _.template("Hello {{planet}}!");132equals(mustache({planet : "World"}), "Hello World!", "can mimic mustache.js");133});134135});136137138