nexus site path corrected
[portal.git] / ecomp-portal-FE / client / bower_components / lodash / vendor / underscore / test / utility.js
1 (function() {
2   var _ = typeof require == 'function' ? require('..') : window._;
3   var templateSettings;
4
5   QUnit.module('Utility', {
6
7     beforeEach: function() {
8       templateSettings = _.clone(_.templateSettings);
9     },
10
11     afterEach: function() {
12       _.templateSettings = templateSettings;
13     }
14
15   });
16
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');
23         this._ = underscore;
24       } else if (typeof global !== 'undefined') {
25         delete global._;
26       }
27     });
28   }
29
30   if (typeof require == 'function') {
31     QUnit.test('noConflict (node vm)', function(assert) {
32       assert.expect(2);
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();',
40           filename
41         );
42         var context = {_: 'oldvalue'};
43         sandbox.runInNewContext(context);
44         assert.equal(context._, 'oldvalue');
45         assert.equal(context.underscore.VERSION, _.VERSION);
46
47         done();
48       });
49     });
50   }
51
52   QUnit.test('#750 - Return _ instance.', function(assert) {
53     assert.expect(2);
54     var instance = _([]);
55     assert.ok(_(instance) === instance);
56     assert.ok(new _(instance) === instance);
57   });
58
59   QUnit.test('identity', function(assert) {
60     var stooge = {name: 'moe'};
61     assert.equal(_.identity(stooge), stooge, 'stooge is the same as his identity');
62   });
63
64   QUnit.test('constant', function(assert) {
65     var stooge = {name: 'moe'};
66     assert.equal(_.constant(stooge)(), stooge, 'should create a function that returns stooge');
67   });
68
69   QUnit.test('noop', function(assert) {
70     assert.strictEqual(_.noop('curly', 'larry', 'moe'), void 0, 'should always return undefined');
71   });
72
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');
78   });
79
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');
85
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');
90
91     var nullPropertyOf = _.propertyOf(null);
92     assert.equal(nullPropertyOf('curly'), void 0, 'should return undefined when obj is null');
93
94     var undefPropertyOf = _.propertyOf(void 0);
95     assert.equal(undefPropertyOf('curly'), void 0, 'should return undefined when obj is undefined');
96   });
97
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);
102
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');
106
107     assert.ok(_.some(array, function() {
108       return _.random(Number.MAX_VALUE) > 0;
109     }), 'should produce a random number when passed `Number.MAX_VALUE`');
110   });
111
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
115   });
116
117   QUnit.test('uniqueId', function(assert) {
118     var ids = [], i = 0;
119     while (i++ < 100) ids.push(_.uniqueId());
120     assert.equal(_.uniq(ids).length, ids.length, 'can generate a globally-unique stream of ids');
121   });
122
123   QUnit.test('times', function(assert) {
124     var vals = [];
125     _.times(3, function(i) { vals.push(i); });
126     assert.deepEqual(vals, [0, 1, 2], 'is 0 indexed');
127     //
128     vals = [];
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');
133
134     assert.deepEqual(_.times(0, _.identity), []);
135     assert.deepEqual(_.times(-1, _.identity), []);
136     assert.deepEqual(_.times(parseFloat('-Infinity'), _.identity), []);
137   });
138
139   QUnit.test('mixin', function(assert) {
140     _.mixin({
141       myReverse: function(string) {
142         return string.split('').reverse().join('');
143       }
144     });
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');
147   });
148
149   QUnit.test('_.escape', function(assert) {
150     assert.equal(_.escape(null), '');
151   });
152
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');
158   });
159
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 & (&amp;) seperately obviously
163     var escapeCharacters = ['<', '>', '"', '\'', '`'];
164
165     _.each(escapeCharacters, function(escapeChar) {
166       var s = 'a ' + escapeChar + ' string escaped';
167       var e = _.escape(s);
168       assert.notEqual(s, e, escapeChar + ' is escaped');
169       assert.equal(s, _.unescape(e), escapeChar + ' can be unescaped');
170
171       s = 'a ' + escapeChar + escapeChar + escapeChar + 'some more string' + escapeChar;
172       e = _.escape(s);
173
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');
176     });
177
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');
186
187     // test & -> &amp;
188     var str = 'some string & another string & yet another';
189     var escaped = _.escape(str);
190
191     assert.ok(escaped.indexOf('&') !== -1, 'handles & aka &amp;');
192     assert.equal(_.unescape(str), str, 'can unescape &amp;');
193   });
194
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');
199
200     var sansSemicolonTemplate = _.template('A <% this %> B');
201     assert.equal(sansSemicolonTemplate(), 'A  B');
202
203     var backslashTemplate = _.template('<%= thing %> is \\ridanculous');
204     assert.equal(backslashTemplate({thing: 'This'}), 'This is \\ridanculous');
205
206     var escapeTemplate = _.template('<%= a ? "checked=\\"checked\\"" : "" %>');
207     assert.equal(escapeTemplate({a: true}), 'checked="checked"', 'can handle slash escapes in interpolations.');
208
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');
214
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');
218
219     var namespaceCollisionTemplate = _.template('<%= pageCount %> <%= thumbnails[pageCount] %> <% _.each(thumbnails, function(p) { %><div class="thumbnail" rel="<%= p %>"></div><% }); %>');
220     result = namespaceCollisionTemplate({
221       pageCount: 3,
222       thumbnails: {
223         1: 'p1-thumbnail.gif',
224         2: 'p2-thumbnail.gif',
225         3: 'p3-thumbnail.gif'
226       }
227     });
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>');
229
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>');
233
234     var quoteTemplate = _.template("It's its, not it's");
235     assert.equal(quoteTemplate({}), "It's its, not it's");
236
237     var quoteInStatementAndBody = _.template('<% ' +
238     "  if(foo == 'bar'){ " +
239     "%>Statement quotes and 'quotes'.<% } %>");
240     assert.equal(quoteInStatementAndBody({foo: 'bar'}), "Statement quotes and 'quotes'.");
241
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.');
244
245     var template = _.template('<i><%- value %></i>');
246     result = template({value: '<script>'});
247     assert.equal(result, '<i>&lt;script&gt;</i>');
248
249     var stooge = {
250       name: 'Moe',
251       template: _.template("I'm <%= this.name %>")
252     };
253     assert.equal(stooge.template(), "I'm Moe");
254
255     template = _.template('\n ' +
256     '  <%\n ' +
257     '  // a comment\n ' +
258     '  if (data) { data += 12345; }; %>\n ' +
259     '  <li><%= data %></li>\n '
260     );
261     assert.equal(template({data: 12345}).replace(/\s/g, ''), '<li>24690</li>');
262
263     _.templateSettings = {
264       evaluate: /\{\{([\s\S]+?)\}\}/g,
265       interpolate: /\{\{=([\s\S]+?)\}\}/g
266     };
267
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');
271
272     var customQuote = _.template("It's its, not it's");
273     assert.equal(customQuote({}), "It's its, not it's");
274
275     quoteInStatementAndBody = _.template("{{ if(foo == 'bar'){ }}Statement quotes and 'quotes'.{{ } }}");
276     assert.equal(quoteInStatementAndBody({foo: 'bar'}), "Statement quotes and 'quotes'.");
277
278     _.templateSettings = {
279       evaluate: /<\?([\s\S]+?)\?>/g,
280       interpolate: /<\?=([\s\S]+?)\?>/g
281     };
282
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');
286
287     var customWithSpecialCharsQuote = _.template("It's its, not it's");
288     assert.equal(customWithSpecialCharsQuote({}), "It's its, not it's");
289
290     quoteInStatementAndBody = _.template("<? if(foo == 'bar'){ ?>Statement quotes and 'quotes'.<? } ?>");
291     assert.equal(quoteInStatementAndBody({foo: 'bar'}), "Statement quotes and 'quotes'.");
292
293     _.templateSettings = {
294       interpolate: /\{\{(.+?)\}\}/g
295     };
296
297     var mustache = _.template('Hello {{planet}}!');
298     assert.equal(mustache({planet: 'World'}), 'Hello World!', 'can mimic mustache.js');
299
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');
302   });
303
304   QUnit.test('_.template provides the generated function source, when a SyntaxError occurs', function(assert) {
305     var source;
306     try {
307       _.template('<b><%= if x %></b>');
308     } catch (ex) {
309       source = ex.source;
310     }
311     assert.ok(/__p/.test(source));
312   });
313
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>');
317   });
318
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);
326   });
327
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');
332   });
333
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);
337   });
338
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');
342   });
343
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);
348   });
349
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);
353   });
354
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() {
359       return this.a;
360     }), obj.a, 'called with context');
361   });
362
363   QUnit.test('_.templateSettings.variable', function(assert) {
364     var s = '<%=data.x%>';
365     var data = {x: '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');
370   });
371
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);
376   });
377
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}), '');
382
383     var templateEscaped = _.template('<%-x%>');
384     assert.strictEqual(templateEscaped({x: null}), '');
385     assert.strictEqual(templateEscaped({x: void 0}), '');
386
387     var templateWithProperty = _.template('<%=x.foo%>');
388     assert.strictEqual(templateWithProperty({x: {}}), '');
389     assert.strictEqual(templateWithProperty({x: {}}), '');
390
391     var templateWithPropertyEscaped = _.template('<%-x.foo%>');
392     assert.strictEqual(templateWithPropertyEscaped({x: {}}), '');
393     assert.strictEqual(templateWithPropertyEscaped({x: {}}), '');
394   });
395
396   QUnit.test('interpolate evaluates code only once.', function(assert) {
397     assert.expect(2);
398     var count = 0;
399     var template = _.template('<%= f() %>');
400     template({f: function(){ assert.ok(!count++); }});
401
402     var countEscaped = 0;
403     var templateEscaped = _.template('<%- f() %>');
404     templateEscaped({f: function(){ assert.ok(!countEscaped++); }});
405   });
406
407   QUnit.test('#746 - _.template settings are not modified.', function(assert) {
408     assert.expect(1);
409     var settings = {};
410     _.template('', null, settings);
411     assert.deepEqual(settings, {});
412   });
413
414   QUnit.test('#779 - delimeters are applied to unescaped text.', function(assert) {
415     assert.expect(1);
416     var template = _.template('<<\nx\n>>', null, {evaluate: /<<(.*?)>>/g});
417     assert.strictEqual(template(), '<<\nx\n>>');
418   });
419
420 }());