mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 15:27:50 +00:00
120
test/arrays.js
120
test/arrays.js
@@ -3,69 +3,69 @@ $(document).ready(function() {
|
||||
module("Arrays");
|
||||
|
||||
test("arrays: first", function() {
|
||||
equals(_.first([1,2,3]), 1, 'can pull out the first element of an array');
|
||||
equals(_([1, 2, 3]).first(), 1, 'can perform OO-style "first()"');
|
||||
equals(_.first([1,2,3], 0).join(', '), "", 'can pass an index to first');
|
||||
equals(_.first([1,2,3], 2).join(', '), '1, 2', 'can pass an index to first');
|
||||
equals(_.first([1,2,3], 5).join(', '), '1, 2, 3', 'can pass an index to first');
|
||||
equal(_.first([1,2,3]), 1, 'can pull out the first element of an array');
|
||||
equal(_([1, 2, 3]).first(), 1, 'can perform OO-style "first()"');
|
||||
equal(_.first([1,2,3], 0).join(', '), "", 'can pass an index to first');
|
||||
equal(_.first([1,2,3], 2).join(', '), '1, 2', 'can pass an index to first');
|
||||
equal(_.first([1,2,3], 5).join(', '), '1, 2, 3', 'can pass an index to first');
|
||||
var result = (function(){ return _.first(arguments); })(4, 3, 2, 1);
|
||||
equals(result, 4, 'works on an arguments object.');
|
||||
equal(result, 4, 'works on an arguments object.');
|
||||
result = _.map([[1,2,3],[1,2,3]], _.first);
|
||||
equals(result.join(','), '1,1', 'works well with _.map');
|
||||
equal(result.join(','), '1,1', 'works well with _.map');
|
||||
});
|
||||
|
||||
test("arrays: rest", function() {
|
||||
var numbers = [1, 2, 3, 4];
|
||||
equals(_.rest(numbers).join(", "), "2, 3, 4", 'working rest()');
|
||||
equals(_.rest(numbers, 0).join(", "), "1, 2, 3, 4", 'working rest(0)');
|
||||
equals(_.rest(numbers, 2).join(', '), '3, 4', 'rest can take an index');
|
||||
equal(_.rest(numbers).join(", "), "2, 3, 4", 'working rest()');
|
||||
equal(_.rest(numbers, 0).join(", "), "1, 2, 3, 4", 'working rest(0)');
|
||||
equal(_.rest(numbers, 2).join(', '), '3, 4', 'rest can take an index');
|
||||
var result = (function(){ return _(arguments).tail(); })(1, 2, 3, 4);
|
||||
equals(result.join(', '), '2, 3, 4', 'aliased as tail and works on arguments object');
|
||||
equal(result.join(', '), '2, 3, 4', 'aliased as tail and works on arguments object');
|
||||
result = _.map([[1,2,3],[1,2,3]], _.rest);
|
||||
equals(_.flatten(result).join(','), '2,3,2,3', 'works well with _.map');
|
||||
equal(_.flatten(result).join(','), '2,3,2,3', 'works well with _.map');
|
||||
});
|
||||
|
||||
test("arrays: initial", function() {
|
||||
equals(_.initial([1,2,3,4,5]).join(", "), "1, 2, 3, 4", 'working initial()');
|
||||
equals(_.initial([1,2,3,4],2).join(", "), "1, 2", 'initial can take an index');
|
||||
equal(_.initial([1,2,3,4,5]).join(", "), "1, 2, 3, 4", 'working initial()');
|
||||
equal(_.initial([1,2,3,4],2).join(", "), "1, 2", 'initial can take an index');
|
||||
var result = (function(){ return _(arguments).initial(); })(1, 2, 3, 4);
|
||||
equals(result.join(", "), "1, 2, 3", 'initial works on arguments object');
|
||||
equal(result.join(", "), "1, 2, 3", 'initial works on arguments object');
|
||||
result = _.map([[1,2,3],[1,2,3]], _.initial);
|
||||
equals(_.flatten(result).join(','), '1,2,1,2', 'initial works with _.map');
|
||||
equal(_.flatten(result).join(','), '1,2,1,2', 'initial works with _.map');
|
||||
});
|
||||
|
||||
test("arrays: last", function() {
|
||||
equals(_.last([1,2,3]), 3, 'can pull out the last element of an array');
|
||||
equals(_.last([1,2,3], 0).join(', '), "", 'can pass an index to last');
|
||||
equals(_.last([1,2,3], 2).join(', '), '2, 3', 'can pass an index to last');
|
||||
equals(_.last([1,2,3], 5).join(', '), '1, 2, 3', 'can pass an index to last');
|
||||
equal(_.last([1,2,3]), 3, 'can pull out the last element of an array');
|
||||
equal(_.last([1,2,3], 0).join(', '), "", 'can pass an index to last');
|
||||
equal(_.last([1,2,3], 2).join(', '), '2, 3', 'can pass an index to last');
|
||||
equal(_.last([1,2,3], 5).join(', '), '1, 2, 3', 'can pass an index to last');
|
||||
var result = (function(){ return _(arguments).last(); })(1, 2, 3, 4);
|
||||
equals(result, 4, 'works on an arguments object');
|
||||
equal(result, 4, 'works on an arguments object');
|
||||
result = _.map([[1,2,3],[1,2,3]], _.last);
|
||||
equals(result.join(','), '3,3', 'works well with _.map');
|
||||
equal(result.join(','), '3,3', 'works well with _.map');
|
||||
});
|
||||
|
||||
test("arrays: compact", function() {
|
||||
equals(_.compact([0, 1, false, 2, false, 3]).length, 3, 'can trim out all falsy values');
|
||||
equal(_.compact([0, 1, false, 2, false, 3]).length, 3, 'can trim out all falsy values');
|
||||
var result = (function(){ return _(arguments).compact().length; })(0, 1, false, 2, false, 3);
|
||||
equals(result, 3, 'works on an arguments object');
|
||||
equal(result, 3, 'works on an arguments object');
|
||||
});
|
||||
|
||||
test("arrays: flatten", function() {
|
||||
if (window.JSON) {
|
||||
var list = [1, [2], [3, [[[4]]]]];
|
||||
equals(JSON.stringify(_.flatten(list)), '[1,2,3,4]', 'can flatten nested arrays');
|
||||
equals(JSON.stringify(_.flatten(list, true)), '[1,2,3,[[[4]]]]', 'can shallowly flatten nested arrays');
|
||||
equal(JSON.stringify(_.flatten(list)), '[1,2,3,4]', 'can flatten nested arrays');
|
||||
equal(JSON.stringify(_.flatten(list, true)), '[1,2,3,[[[4]]]]', 'can shallowly flatten nested arrays');
|
||||
var result = (function(){ return _.flatten(arguments); })(1, [2], [3, [[[4]]]]);
|
||||
equals(JSON.stringify(result), '[1,2,3,4]', 'works on an arguments object');
|
||||
equal(JSON.stringify(result), '[1,2,3,4]', 'works on an arguments object');
|
||||
}
|
||||
});
|
||||
|
||||
test("arrays: without", function() {
|
||||
var list = [1, 2, 1, 0, 3, 1, 4];
|
||||
equals(_.without(list, 0, 1).join(', '), '2, 3, 4', 'can remove all instances of an object');
|
||||
equal(_.without(list, 0, 1).join(', '), '2, 3, 4', 'can remove all instances of an object');
|
||||
var result = (function(){ return _.without(arguments, 0, 1); })(1, 2, 1, 0, 3, 1, 4);
|
||||
equals(result.join(', '), '2, 3, 4', 'works on an arguments object');
|
||||
equal(result.join(', '), '2, 3, 4', 'works on an arguments object');
|
||||
|
||||
var list = [{one : 1}, {two : 2}];
|
||||
ok(_.without(list, {one : 1}).length == 2, 'uses real object identity for comparisons.');
|
||||
@@ -74,93 +74,93 @@ $(document).ready(function() {
|
||||
|
||||
test("arrays: uniq", function() {
|
||||
var list = [1, 2, 1, 3, 1, 4];
|
||||
equals(_.uniq(list).join(', '), '1, 2, 3, 4', 'can find the unique values of an unsorted array');
|
||||
equal(_.uniq(list).join(', '), '1, 2, 3, 4', 'can find the unique values of an unsorted array');
|
||||
|
||||
var list = [1, 1, 1, 2, 2, 3];
|
||||
equals(_.uniq(list, true).join(', '), '1, 2, 3', 'can find the unique values of a sorted array faster');
|
||||
equal(_.uniq(list, true).join(', '), '1, 2, 3', 'can find the unique values of a sorted array faster');
|
||||
|
||||
var list = [{name:'moe'}, {name:'curly'}, {name:'larry'}, {name:'curly'}];
|
||||
var iterator = function(value) { return value.name; };
|
||||
equals(_.map(_.uniq(list, false, iterator), iterator).join(', '), 'moe, curly, larry', 'can find the unique values of an array using a custom iterator');
|
||||
equal(_.map(_.uniq(list, false, iterator), iterator).join(', '), 'moe, curly, larry', 'can find the unique values of an array using a custom iterator');
|
||||
|
||||
var iterator = function(value) { return value +1; };
|
||||
var list = [1, 2, 2, 3, 4, 4];
|
||||
equals(_.uniq(list, true, iterator).join(', '), '1, 2, 3, 4', 'iterator works with sorted array');
|
||||
equal(_.uniq(list, true, iterator).join(', '), '1, 2, 3, 4', 'iterator works with sorted array');
|
||||
|
||||
var result = (function(){ return _.uniq(arguments); })(1, 2, 1, 3, 1, 4);
|
||||
equals(result.join(', '), '1, 2, 3, 4', 'works on an arguments object');
|
||||
equal(result.join(', '), '1, 2, 3, 4', 'works on an arguments object');
|
||||
});
|
||||
|
||||
test("arrays: intersection", function() {
|
||||
var stooges = ['moe', 'curly', 'larry'], leaders = ['moe', 'groucho'];
|
||||
equals(_.intersection(stooges, leaders).join(''), 'moe', 'can take the set intersection of two arrays');
|
||||
equals(_(stooges).intersection(leaders).join(''), 'moe', 'can perform an OO-style intersection');
|
||||
equal(_.intersection(stooges, leaders).join(''), 'moe', 'can take the set intersection of two arrays');
|
||||
equal(_(stooges).intersection(leaders).join(''), 'moe', 'can perform an OO-style intersection');
|
||||
var result = (function(){ return _.intersection(arguments, leaders); })('moe', 'curly', 'larry');
|
||||
equals(result.join(''), 'moe', 'works on an arguments object');
|
||||
equal(result.join(''), 'moe', 'works on an arguments object');
|
||||
});
|
||||
|
||||
test("arrays: union", function() {
|
||||
var result = _.union([1, 2, 3], [2, 30, 1], [1, 40]);
|
||||
equals(result.join(' '), '1 2 3 30 40', 'takes the union of a list of arrays');
|
||||
equal(result.join(' '), '1 2 3 30 40', 'takes the union of a list of arrays');
|
||||
|
||||
var result = _.union([1, 2, 3], [2, 30, 1], [1, 40, [1]]);
|
||||
equals(result.join(' '), '1 2 3 30 40 1', 'takes the union of a list of nested arrays');
|
||||
equal(result.join(' '), '1 2 3 30 40 1', 'takes the union of a list of nested arrays');
|
||||
});
|
||||
|
||||
test("arrays: difference", function() {
|
||||
var result = _.difference([1, 2, 3], [2, 30, 40]);
|
||||
equals(result.join(' '), '1 3', 'takes the difference of two arrays');
|
||||
equal(result.join(' '), '1 3', 'takes the difference of two arrays');
|
||||
|
||||
var result = _.difference([1, 2, 3, 4], [2, 30, 40], [1, 11, 111]);
|
||||
equals(result.join(' '), '3 4', 'takes the difference of three arrays');
|
||||
equal(result.join(' '), '3 4', 'takes the difference of three arrays');
|
||||
});
|
||||
|
||||
test('arrays: zip', function() {
|
||||
var names = ['moe', 'larry', 'curly'], ages = [30, 40, 50], leaders = [true];
|
||||
var stooges = _.zip(names, ages, leaders);
|
||||
equals(String(stooges), 'moe,30,true,larry,40,,curly,50,', 'zipped together arrays of different lengths');
|
||||
equal(String(stooges), 'moe,30,true,larry,40,,curly,50,', 'zipped together arrays of different lengths');
|
||||
});
|
||||
|
||||
test("arrays: indexOf", function() {
|
||||
var numbers = [1, 2, 3];
|
||||
numbers.indexOf = null;
|
||||
equals(_.indexOf(numbers, 2), 1, 'can compute indexOf, even without the native function');
|
||||
equal(_.indexOf(numbers, 2), 1, 'can compute indexOf, even without the native function');
|
||||
var result = (function(){ return _.indexOf(arguments, 2); })(1, 2, 3);
|
||||
equals(result, 1, 'works on an arguments object');
|
||||
equals(_.indexOf(null, 2), -1, 'handles nulls properly');
|
||||
equal(result, 1, 'works on an arguments object');
|
||||
equal(_.indexOf(null, 2), -1, 'handles nulls properly');
|
||||
|
||||
var numbers = [10, 20, 30, 40, 50], num = 35;
|
||||
var index = _.indexOf(numbers, num, true);
|
||||
equals(index, -1, '35 is not in the list');
|
||||
equal(index, -1, '35 is not in the list');
|
||||
|
||||
numbers = [10, 20, 30, 40, 50]; num = 40;
|
||||
index = _.indexOf(numbers, num, true);
|
||||
equals(index, 3, '40 is in the list');
|
||||
equal(index, 3, '40 is in the list');
|
||||
|
||||
numbers = [1, 40, 40, 40, 40, 40, 40, 40, 50, 60, 70]; num = 40;
|
||||
index = _.indexOf(numbers, num, true);
|
||||
equals(index, 1, '40 is in the list');
|
||||
equal(index, 1, '40 is in the list');
|
||||
});
|
||||
|
||||
test("arrays: lastIndexOf", function() {
|
||||
var numbers = [1, 0, 1, 0, 0, 1, 0, 0, 0];
|
||||
numbers.lastIndexOf = null;
|
||||
equals(_.lastIndexOf(numbers, 1), 5, 'can compute lastIndexOf, even without the native function');
|
||||
equals(_.lastIndexOf(numbers, 0), 8, 'lastIndexOf the other element');
|
||||
equal(_.lastIndexOf(numbers, 1), 5, 'can compute lastIndexOf, even without the native function');
|
||||
equal(_.lastIndexOf(numbers, 0), 8, 'lastIndexOf the other element');
|
||||
var result = (function(){ return _.lastIndexOf(arguments, 1); })(1, 0, 1, 0, 0, 1, 0, 0, 0);
|
||||
equals(result, 5, 'works on an arguments object');
|
||||
equals(_.indexOf(null, 2), -1, 'handles nulls properly');
|
||||
equal(result, 5, 'works on an arguments object');
|
||||
equal(_.indexOf(null, 2), -1, 'handles nulls properly');
|
||||
});
|
||||
|
||||
test("arrays: range", function() {
|
||||
equals(_.range(0).join(''), '', 'range with 0 as a first argument generates an empty array');
|
||||
equals(_.range(4).join(' '), '0 1 2 3', 'range with a single positive argument generates an array of elements 0,1,2,...,n-1');
|
||||
equals(_.range(5, 8).join(' '), '5 6 7', 'range with two arguments a & b, a<b generates an array of elements a,a+1,a+2,...,b-2,b-1');
|
||||
equals(_.range(8, 5).join(''), '', 'range with two arguments a & b, b<a generates an empty array');
|
||||
equals(_.range(3, 10, 3).join(' '), '3 6 9', 'range with three arguments a & b & c, c < b-a, a < b generates an array of elements a,a+c,a+2c,...,b - (multiplier of a) < c');
|
||||
equals(_.range(3, 10, 15).join(''), '3', 'range with three arguments a & b & c, c > b-a, a < b generates an array with a single element, equal to a');
|
||||
equals(_.range(12, 7, -2).join(' '), '12 10 8', 'range with three arguments a & b & c, a > b, c < 0 generates an array of elements a,a-c,a-2c and ends with the number not less than b');
|
||||
equals(_.range(0, -10, -1).join(' '), '0 -1 -2 -3 -4 -5 -6 -7 -8 -9', 'final example in the Python docs');
|
||||
equal(_.range(0).join(''), '', 'range with 0 as a first argument generates an empty array');
|
||||
equal(_.range(4).join(' '), '0 1 2 3', 'range with a single positive argument generates an array of elements 0,1,2,...,n-1');
|
||||
equal(_.range(5, 8).join(' '), '5 6 7', 'range with two arguments a & b, a<b generates an array of elements a,a+1,a+2,...,b-2,b-1');
|
||||
equal(_.range(8, 5).join(''), '', 'range with two arguments a & b, b<a generates an empty array');
|
||||
equal(_.range(3, 10, 3).join(' '), '3 6 9', 'range with three arguments a & b & c, c < b-a, a < b generates an array of elements a,a+c,a+2c,...,b - (multiplier of a) < c');
|
||||
equal(_.range(3, 10, 15).join(''), '3', 'range with three arguments a & b & c, c > b-a, a < b generates an array with a single element, equal to a');
|
||||
equal(_.range(12, 7, -2).join(' '), '12 10 8', 'range with three arguments a & b & c, a > b, c < 0 generates an array of elements a,a-c,a-2c and ends with the number not less than b');
|
||||
equal(_.range(0, -10, -1).join(' '), '0 -1 -2 -3 -4 -5 -6 -7 -8 -9', 'final example in the Python docs');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -29,7 +29,7 @@ $(document).ready(function() {
|
||||
}).sortBy(function(n) {
|
||||
return -n;
|
||||
}).value();
|
||||
equals(numbers.join(', '), "10, 6, 2", "filtered and reversed the numbers");
|
||||
equal(numbers.join(', '), "10, 6, 2", "filtered and reversed the numbers");
|
||||
});
|
||||
|
||||
test("chaining: select/reject/sortBy in functional style", function() {
|
||||
@@ -41,7 +41,7 @@ $(document).ready(function() {
|
||||
}).sortBy(function(n) {
|
||||
return -n;
|
||||
}).value();
|
||||
equals(numbers.join(', '), "10, 6, 2", "filtered and reversed the numbers");
|
||||
equal(numbers.join(', '), "10, 6, 2", "filtered and reversed the numbers");
|
||||
});
|
||||
|
||||
test("chaining: reverse/concat/unshift/pop/map", function() {
|
||||
@@ -53,7 +53,7 @@ $(document).ready(function() {
|
||||
.pop()
|
||||
.map(function(n){ return n * 2; })
|
||||
.value();
|
||||
equals(numbers.join(', '), "34, 10, 8, 6, 4, 2, 10, 10", 'can chain together array functions.');
|
||||
equal(numbers.join(', '), "34, 10, 8, 6, 4, 2, 10, 10", 'can chain together array functions.');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -4,22 +4,22 @@ $(document).ready(function() {
|
||||
|
||||
test("collections: each", function() {
|
||||
_.each([1, 2, 3], function(num, i) {
|
||||
equals(num, i + 1, 'each iterators provide value and iteration count');
|
||||
equal(num, i + 1, 'each iterators provide value and iteration count');
|
||||
});
|
||||
|
||||
var answers = [];
|
||||
_.each([1, 2, 3], function(num){ answers.push(num * this.multiplier);}, {multiplier : 5});
|
||||
equals(answers.join(', '), '5, 10, 15', 'context object property accessed');
|
||||
equal(answers.join(', '), '5, 10, 15', 'context object property accessed');
|
||||
|
||||
answers = [];
|
||||
_.forEach([1, 2, 3], function(num){ answers.push(num); });
|
||||
equals(answers.join(', '), '1, 2, 3', 'aliased as "forEach"');
|
||||
equal(answers.join(', '), '1, 2, 3', 'aliased as "forEach"');
|
||||
|
||||
answers = [];
|
||||
var obj = {one : 1, two : 2, three : 3};
|
||||
obj.constructor.prototype.four = 4;
|
||||
_.each(obj, function(value, key){ answers.push(key); });
|
||||
equals(answers.join(", "), 'one, two, three', 'iterating over objects works, and ignores the object prototype.');
|
||||
equal(answers.join(", "), 'one, two, three', 'iterating over objects works, and ignores the object prototype.');
|
||||
delete obj.constructor.prototype.four;
|
||||
|
||||
answer = null;
|
||||
@@ -28,21 +28,21 @@ $(document).ready(function() {
|
||||
|
||||
answers = 0;
|
||||
_.each(null, function(){ ++answers; });
|
||||
equals(answers, 0, 'handles a null properly');
|
||||
equal(answers, 0, 'handles a null properly');
|
||||
});
|
||||
|
||||
test('collections: map', function() {
|
||||
var doubled = _.map([1, 2, 3], function(num){ return num * 2; });
|
||||
equals(doubled.join(', '), '2, 4, 6', 'doubled numbers');
|
||||
equal(doubled.join(', '), '2, 4, 6', 'doubled numbers');
|
||||
|
||||
doubled = _.collect([1, 2, 3], function(num){ return num * 2; });
|
||||
equals(doubled.join(', '), '2, 4, 6', 'aliased as "collect"');
|
||||
equal(doubled.join(', '), '2, 4, 6', 'aliased as "collect"');
|
||||
|
||||
var tripled = _.map([1, 2, 3], function(num){ return num * this.multiplier; }, {multiplier : 3});
|
||||
equals(tripled.join(', '), '3, 6, 9', 'tripled numbers with context');
|
||||
equal(tripled.join(', '), '3, 6, 9', 'tripled numbers with context');
|
||||
|
||||
var doubled = _([1, 2, 3]).map(function(num){ return num * 2; });
|
||||
equals(doubled.join(', '), '2, 4, 6', 'OO-style doubled numbers');
|
||||
equal(doubled.join(', '), '2, 4, 6', 'OO-style doubled numbers');
|
||||
|
||||
var ids = _.map($('div.underscore-test').children(), function(n){ return n.id; });
|
||||
ok(_.include(ids, 'qunit-header'), 'can use collection methods on NodeLists');
|
||||
@@ -54,25 +54,25 @@ $(document).ready(function() {
|
||||
ok(_.isArray(ifnull) && ifnull.length === 0, 'handles a null properly');
|
||||
|
||||
var length = _.map(Array(2), function(v) { return v; }).length;
|
||||
equals(length, 2, "can preserve a sparse array's length");
|
||||
equal(length, 2, "can preserve a sparse array's length");
|
||||
});
|
||||
|
||||
test('collections: reduce', function() {
|
||||
var sum = _.reduce([1, 2, 3], function(sum, num){ return sum + num; }, 0);
|
||||
equals(sum, 6, 'can sum up an array');
|
||||
equal(sum, 6, 'can sum up an array');
|
||||
|
||||
var context = {multiplier : 3};
|
||||
sum = _.reduce([1, 2, 3], function(sum, num){ return sum + num * this.multiplier; }, 0, context);
|
||||
equals(sum, 18, 'can reduce with a context object');
|
||||
equal(sum, 18, 'can reduce with a context object');
|
||||
|
||||
sum = _.inject([1, 2, 3], function(sum, num){ return sum + num; }, 0);
|
||||
equals(sum, 6, 'aliased as "inject"');
|
||||
equal(sum, 6, 'aliased as "inject"');
|
||||
|
||||
sum = _([1, 2, 3]).reduce(function(sum, num){ return sum + num; }, 0);
|
||||
equals(sum, 6, 'OO-style reduce');
|
||||
equal(sum, 6, 'OO-style reduce');
|
||||
|
||||
var sum = _.reduce([1, 2, 3], function(sum, num){ return sum + num; });
|
||||
equals(sum, 6, 'default initial value');
|
||||
equal(sum, 6, 'default initial value');
|
||||
|
||||
var ifnull;
|
||||
try {
|
||||
@@ -83,24 +83,24 @@ $(document).ready(function() {
|
||||
ok(ifnull instanceof TypeError, 'handles a null (without inital value) properly');
|
||||
|
||||
ok(_.reduce(null, function(){}, 138) === 138, 'handles a null (with initial value) properly');
|
||||
equals(_.reduce([], function(){}, undefined), undefined, 'undefined can be passed as a special case');
|
||||
equal(_.reduce([], function(){}, undefined), undefined, 'undefined can be passed as a special case');
|
||||
raises(function() { _.reduce([], function(){}); }, TypeError, 'throws an error for empty arrays with no initial value');
|
||||
|
||||
var sparseArray = [];
|
||||
sparseArray[0] = 20;
|
||||
sparseArray[2] = -5;
|
||||
equals(_.reduce(sparseArray, function(a, b){ return a - b; }), 25, 'initially-sparse arrays with no memo');
|
||||
equal(_.reduce(sparseArray, function(a, b){ return a - b; }), 25, 'initially-sparse arrays with no memo');
|
||||
});
|
||||
|
||||
test('collections: reduceRight', function() {
|
||||
var list = _.reduceRight(["foo", "bar", "baz"], function(memo, str){ return memo + str; }, '');
|
||||
equals(list, 'bazbarfoo', 'can perform right folds');
|
||||
equal(list, 'bazbarfoo', 'can perform right folds');
|
||||
|
||||
var list = _.foldr(["foo", "bar", "baz"], function(memo, str){ return memo + str; }, '');
|
||||
equals(list, 'bazbarfoo', 'aliased as "foldr"');
|
||||
equal(list, 'bazbarfoo', 'aliased as "foldr"');
|
||||
|
||||
var list = _.foldr(["foo", "bar", "baz"], function(memo, str){ return memo + str; });
|
||||
equals(list, 'bazbarfoo', 'default initial value');
|
||||
equal(list, 'bazbarfoo', 'default initial value');
|
||||
|
||||
var ifnull;
|
||||
try {
|
||||
@@ -112,31 +112,31 @@ $(document).ready(function() {
|
||||
|
||||
ok(_.reduceRight(null, function(){}, 138) === 138, 'handles a null (with initial value) properly');
|
||||
|
||||
equals(_.reduceRight([], function(){}, undefined), undefined, 'undefined can be passed as a special case');
|
||||
equal(_.reduceRight([], function(){}, undefined), undefined, 'undefined can be passed as a special case');
|
||||
raises(function() { _.reduceRight([], function(){}); }, TypeError, 'throws an error for empty arrays with no initial value');
|
||||
|
||||
var sparseArray = [];
|
||||
sparseArray[0] = 20;
|
||||
sparseArray[2] = -5;
|
||||
equals(_.reduceRight(sparseArray, function(a, b){ return a - b; }), -25, 'initially-sparse arrays with no memo');
|
||||
equal(_.reduceRight(sparseArray, function(a, b){ return a - b; }), -25, 'initially-sparse arrays with no memo');
|
||||
});
|
||||
|
||||
test('collections: detect', function() {
|
||||
var result = _.detect([1, 2, 3], function(num){ return num * 2 == 4; });
|
||||
equals(result, 2, 'found the first "2" and broke the loop');
|
||||
equal(result, 2, 'found the first "2" and broke the loop');
|
||||
});
|
||||
|
||||
test('collections: select', function() {
|
||||
var evens = _.select([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
|
||||
equals(evens.join(', '), '2, 4, 6', 'selected each even number');
|
||||
equal(evens.join(', '), '2, 4, 6', 'selected each even number');
|
||||
|
||||
evens = _.filter([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
|
||||
equals(evens.join(', '), '2, 4, 6', 'aliased as "filter"');
|
||||
equal(evens.join(', '), '2, 4, 6', 'aliased as "filter"');
|
||||
});
|
||||
|
||||
test('collections: reject', function() {
|
||||
var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
|
||||
equals(odds.join(', '), '1, 3, 5', 'rejected each even number');
|
||||
equal(odds.join(', '), '1, 3, 5', 'rejected each even number');
|
||||
});
|
||||
|
||||
test('collections: all', function() {
|
||||
@@ -172,15 +172,15 @@ $(document).ready(function() {
|
||||
test('collections: invoke', function() {
|
||||
var list = [[5, 1, 7], [3, 2, 1]];
|
||||
var result = _.invoke(list, 'sort');
|
||||
equals(result[0].join(', '), '1, 5, 7', 'first array sorted');
|
||||
equals(result[1].join(', '), '1, 2, 3', 'second array sorted');
|
||||
equal(result[0].join(', '), '1, 5, 7', 'first array sorted');
|
||||
equal(result[1].join(', '), '1, 2, 3', 'second array sorted');
|
||||
});
|
||||
|
||||
test('collections: invoke w/ function reference', function() {
|
||||
var list = [[5, 1, 7], [3, 2, 1]];
|
||||
var result = _.invoke(list, Array.prototype.sort);
|
||||
equals(result[0].join(', '), '1, 5, 7', 'first array sorted');
|
||||
equals(result[1].join(', '), '1, 2, 3', 'second array sorted');
|
||||
equal(result[0].join(', '), '1, 5, 7', 'first array sorted');
|
||||
equal(result[1].join(', '), '1, 2, 3', 'second array sorted');
|
||||
});
|
||||
|
||||
// Relevant when using ClojureScript
|
||||
@@ -190,72 +190,72 @@ $(document).ready(function() {
|
||||
};
|
||||
var list = [[5, 1, 7], [3, 2, 1]];
|
||||
var s = "foo";
|
||||
equals(s.call(), 42, "call function exists");
|
||||
equal(s.call(), 42, "call function exists");
|
||||
var result = _.invoke(list, 'sort');
|
||||
equals(result[0].join(', '), '1, 5, 7', 'first array sorted');
|
||||
equals(result[1].join(', '), '1, 2, 3', 'second array sorted');
|
||||
equal(result[0].join(', '), '1, 5, 7', 'first array sorted');
|
||||
equal(result[1].join(', '), '1, 2, 3', 'second array sorted');
|
||||
delete String.prototype.call;
|
||||
equals(s.call, undefined, "call function removed");
|
||||
equal(s.call, undefined, "call function removed");
|
||||
});
|
||||
|
||||
test('collections: pluck', function() {
|
||||
var people = [{name : 'moe', age : 30}, {name : 'curly', age : 50}];
|
||||
equals(_.pluck(people, 'name').join(', '), 'moe, curly', 'pulls names out of objects');
|
||||
equal(_.pluck(people, 'name').join(', '), 'moe, curly', 'pulls names out of objects');
|
||||
});
|
||||
|
||||
test('collections: max', function() {
|
||||
equals(3, _.max([1, 2, 3]), 'can perform a regular Math.max');
|
||||
equal(3, _.max([1, 2, 3]), 'can perform a regular Math.max');
|
||||
|
||||
var neg = _.max([1, 2, 3], function(num){ return -num; });
|
||||
equals(neg, 1, 'can perform a computation-based max');
|
||||
equal(neg, 1, 'can perform a computation-based max');
|
||||
|
||||
equals(-Infinity, _.max({}), 'Maximum value of an empty object');
|
||||
equals(-Infinity, _.max([]), 'Maximum value of an empty array');
|
||||
equal(-Infinity, _.max({}), 'Maximum value of an empty object');
|
||||
equal(-Infinity, _.max([]), 'Maximum value of an empty array');
|
||||
});
|
||||
|
||||
test('collections: min', function() {
|
||||
equals(1, _.min([1, 2, 3]), 'can perform a regular Math.min');
|
||||
equal(1, _.min([1, 2, 3]), 'can perform a regular Math.min');
|
||||
|
||||
var neg = _.min([1, 2, 3], function(num){ return -num; });
|
||||
equals(neg, 3, 'can perform a computation-based min');
|
||||
equal(neg, 3, 'can perform a computation-based min');
|
||||
|
||||
equals(Infinity, _.min({}), 'Minimum value of an empty object');
|
||||
equals(Infinity, _.min([]), 'Minimum value of an empty array');
|
||||
equal(Infinity, _.min({}), 'Minimum value of an empty object');
|
||||
equal(Infinity, _.min([]), 'Minimum value of an empty array');
|
||||
|
||||
var now = new Date(9999999999);
|
||||
var then = new Date(0);
|
||||
equals(_.min([now, then]), then);
|
||||
equal(_.min([now, then]), then);
|
||||
});
|
||||
|
||||
test('collections: sortBy', function() {
|
||||
var people = [{name : 'curly', age : 50}, {name : 'moe', age : 30}];
|
||||
people = _.sortBy(people, function(person){ return person.age; });
|
||||
equals(_.pluck(people, 'name').join(', '), 'moe, curly', 'stooges sorted by age');
|
||||
equal(_.pluck(people, 'name').join(', '), 'moe, curly', 'stooges sorted by age');
|
||||
});
|
||||
|
||||
test('collections: groupBy', function() {
|
||||
var parity = _.groupBy([1, 2, 3, 4, 5, 6], function(num){ return num % 2; });
|
||||
ok('0' in parity && '1' in parity, 'created a group for each value');
|
||||
equals(parity[0].join(', '), '2, 4, 6', 'put each even number in the right group');
|
||||
equal(parity[0].join(', '), '2, 4, 6', 'put each even number in the right group');
|
||||
|
||||
var list = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"];
|
||||
var grouped = _.groupBy(list, 'length');
|
||||
equals(grouped['3'].join(' '), 'one two six ten');
|
||||
equals(grouped['4'].join(' '), 'four five nine');
|
||||
equals(grouped['5'].join(' '), 'three seven eight');
|
||||
equal(grouped['3'].join(' '), 'one two six ten');
|
||||
equal(grouped['4'].join(' '), 'four five nine');
|
||||
equal(grouped['5'].join(' '), 'three seven eight');
|
||||
});
|
||||
|
||||
test('collections: sortedIndex', function() {
|
||||
var numbers = [10, 20, 30, 40, 50], num = 35;
|
||||
var index = _.sortedIndex(numbers, num);
|
||||
equals(index, 3, '35 should be inserted at index 3');
|
||||
equal(index, 3, '35 should be inserted at index 3');
|
||||
});
|
||||
|
||||
test('collections: shuffle', function() {
|
||||
var numbers = _.range(10);
|
||||
var shuffled = _.shuffle(numbers).sort();
|
||||
notStrictEqual(numbers, shuffled, 'original object is unmodified');
|
||||
equals(shuffled.join(','), numbers.join(','), 'contains the same members before and after shuffle');
|
||||
equal(shuffled.join(','), numbers.join(','), 'contains the same members before and after shuffle');
|
||||
});
|
||||
|
||||
test('collections: toArray', function() {
|
||||
@@ -263,14 +263,14 @@ $(document).ready(function() {
|
||||
ok(_.isArray(_.toArray(arguments)), 'arguments object converted into array');
|
||||
var a = [1,2,3];
|
||||
ok(_.toArray(a) !== a, 'array is cloned');
|
||||
equals(_.toArray(a).join(', '), '1, 2, 3', 'cloned array contains same elements');
|
||||
equal(_.toArray(a).join(', '), '1, 2, 3', 'cloned array contains same elements');
|
||||
|
||||
var numbers = _.toArray({one : 1, two : 2, three : 3});
|
||||
equals(numbers.join(', '), '1, 2, 3', 'object flattened into array');
|
||||
equal(numbers.join(', '), '1, 2, 3', 'object flattened into array');
|
||||
});
|
||||
|
||||
test('collections: size', function() {
|
||||
equals(_.size({one : 1, two : 2, three : 3}), 3, 'can compute the size of an object');
|
||||
equal(_.size({one : 1, two : 2, three : 3}), 3, 'can compute the size of an object');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -6,26 +6,26 @@ $(document).ready(function() {
|
||||
var context = {name : 'moe'};
|
||||
var func = function(arg) { return "name: " + (this.name || arg); };
|
||||
var bound = _.bind(func, context);
|
||||
equals(bound(), 'name: moe', 'can bind a function to a context');
|
||||
equal(bound(), 'name: moe', 'can bind a function to a context');
|
||||
|
||||
bound = _(func).bind(context);
|
||||
equals(bound(), 'name: moe', 'can do OO-style binding');
|
||||
equal(bound(), 'name: moe', 'can do OO-style binding');
|
||||
|
||||
bound = _.bind(func, null, 'curly');
|
||||
equals(bound(), 'name: curly', 'can bind without specifying a context');
|
||||
equal(bound(), 'name: curly', 'can bind without specifying a context');
|
||||
|
||||
func = function(salutation, name) { return salutation + ': ' + name; };
|
||||
func = _.bind(func, this, 'hello');
|
||||
equals(func('moe'), 'hello: moe', 'the function was partially applied in advance');
|
||||
equal(func('moe'), 'hello: moe', 'the function was partially applied in advance');
|
||||
|
||||
var func = _.bind(func, this, 'curly');
|
||||
equals(func(), 'hello: curly', 'the function was completely applied in advance');
|
||||
equal(func(), 'hello: curly', 'the function was completely applied in advance');
|
||||
|
||||
var func = function(salutation, firstname, lastname) { return salutation + ': ' + firstname + ' ' + lastname; };
|
||||
func = _.bind(func, this, 'hello', 'moe', 'curly');
|
||||
equals(func(), 'hello: moe curly', 'the function was partially applied in advance and can accept multiple arguments');
|
||||
equal(func(), 'hello: moe curly', 'the function was partially applied in advance and can accept multiple arguments');
|
||||
|
||||
func = function(context, message) { equals(this, context, message); };
|
||||
func = function(context, message) { equal(this, context, message); };
|
||||
_.bind(func, 0, 0, 'can bind a function to `0`')();
|
||||
_.bind(func, '', '', 'can bind a function to an empty string')();
|
||||
_.bind(func, false, false, 'can bind a function to `false`')();
|
||||
@@ -47,8 +47,8 @@ $(document).ready(function() {
|
||||
curly.getName = moe.getName;
|
||||
_.bindAll(moe, 'getName', 'sayHi');
|
||||
curly.sayHi = moe.sayHi;
|
||||
equals(curly.getName(), 'name: curly', 'unbound function is bound to current object');
|
||||
equals(curly.sayHi(), 'hi: moe', 'bound function is still bound to original object');
|
||||
equal(curly.getName(), 'name: curly', 'unbound function is bound to current object');
|
||||
equal(curly.sayHi(), 'hi: moe', 'bound function is still bound to original object');
|
||||
|
||||
curly = {name : 'curly'};
|
||||
moe = {
|
||||
@@ -58,7 +58,7 @@ $(document).ready(function() {
|
||||
};
|
||||
_.bindAll(moe);
|
||||
curly.sayHi = moe.sayHi;
|
||||
equals(curly.sayHi(), 'hi: moe', 'calling bindAll with no arguments binds all functions to the object');
|
||||
equal(curly.sayHi(), 'hi: moe', 'calling bindAll with no arguments binds all functions to the object');
|
||||
});
|
||||
|
||||
test("functions: memoize", function() {
|
||||
@@ -66,15 +66,15 @@ $(document).ready(function() {
|
||||
return n < 2 ? n : fib(n - 1) + fib(n - 2);
|
||||
};
|
||||
var fastFib = _.memoize(fib);
|
||||
equals(fib(10), 55, 'a memoized version of fibonacci produces identical results');
|
||||
equals(fastFib(10), 55, 'a memoized version of fibonacci produces identical results');
|
||||
equal(fib(10), 55, 'a memoized version of fibonacci produces identical results');
|
||||
equal(fastFib(10), 55, 'a memoized version of fibonacci produces identical results');
|
||||
|
||||
var o = function(str) {
|
||||
return str;
|
||||
};
|
||||
var fastO = _.memoize(o);
|
||||
equals(o('toString'), 'toString', 'checks hasOwnProperty');
|
||||
equals(fastO('toString'), 'toString', 'checks hasOwnProperty');
|
||||
equal(o('toString'), 'toString', 'checks hasOwnProperty');
|
||||
equal(fastO('toString'), 'toString', 'checks hasOwnProperty');
|
||||
});
|
||||
|
||||
asyncTest("functions: delay", 2, function() {
|
||||
@@ -113,8 +113,8 @@ $(document).ready(function() {
|
||||
setTimeout(function(){ throttledUpdate(4); }, 120);
|
||||
setTimeout(function(){ throttledUpdate(5); }, 140);
|
||||
setTimeout(function(){ throttledUpdate(6); }, 250);
|
||||
_.delay(function(){ equals(value, 1, "updated to latest value"); }, 40);
|
||||
_.delay(function(){ equals(value, 6, "updated to latest value"); start(); }, 400);
|
||||
_.delay(function(){ equal(value, 1, "updated to latest value"); }, 40);
|
||||
_.delay(function(){ equal(value, 6, "updated to latest value"); start(); }, 400);
|
||||
});
|
||||
|
||||
asyncTest("functions: throttle once", 1, function() {
|
||||
@@ -151,18 +151,18 @@ $(document).ready(function() {
|
||||
var increment = _.once(function(){ num++; });
|
||||
increment();
|
||||
increment();
|
||||
equals(num, 1);
|
||||
equal(num, 1);
|
||||
});
|
||||
|
||||
test("functions: wrap", function() {
|
||||
var greet = function(name){ return "hi: " + name; };
|
||||
var backwards = _.wrap(greet, function(func, name){ return func(name) + ' ' + name.split('').reverse().join(''); });
|
||||
equals(backwards('moe'), 'hi: moe eom', 'wrapped the saluation function');
|
||||
equal(backwards('moe'), 'hi: moe eom', 'wrapped the saluation function');
|
||||
|
||||
var inner = function(){ return "Hello "; };
|
||||
var obj = {name : "Moe"};
|
||||
obj.hi = _.wrap(inner, function(fn){ return fn() + this.name; });
|
||||
equals(obj.hi(), "Hello Moe");
|
||||
equal(obj.hi(), "Hello Moe");
|
||||
|
||||
var noop = function(){};
|
||||
var wrapped = _.wrap(noop, function(fn){ return Array.prototype.slice.call(arguments, 0); });
|
||||
@@ -174,10 +174,10 @@ $(document).ready(function() {
|
||||
var greet = function(name){ return "hi: " + name; };
|
||||
var exclaim = function(sentence){ return sentence + '!'; };
|
||||
var composed = _.compose(exclaim, greet);
|
||||
equals(composed('moe'), 'hi: moe!', 'can compose a function that takes another');
|
||||
equal(composed('moe'), 'hi: moe!', 'can compose a function that takes another');
|
||||
|
||||
composed = _.compose(greet, exclaim);
|
||||
equals(composed('moe'), 'hi: moe!', 'in this case, the functions are also commutative');
|
||||
equal(composed('moe'), 'hi: moe!', 'in this case, the functions are also commutative');
|
||||
});
|
||||
|
||||
test("functions: after", function() {
|
||||
@@ -190,9 +190,9 @@ $(document).ready(function() {
|
||||
return afterCalled;
|
||||
};
|
||||
|
||||
equals(testAfter(5, 5), 1, "after(N) should fire after being called N times");
|
||||
equals(testAfter(5, 4), 0, "after(N) should not fire unless called N times");
|
||||
equals(testAfter(0, 0), 1, "after(0) should fire immediately");
|
||||
equal(testAfter(5, 5), 1, "after(N) should fire after being called N times");
|
||||
equal(testAfter(5, 4), 0, "after(N) should not fire unless called N times");
|
||||
equal(testAfter(0, 0), 1, "after(0) should fire immediately");
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -4,10 +4,10 @@ $(document).ready(function() {
|
||||
|
||||
test("objects: keys", function() {
|
||||
var exception = /object/;
|
||||
equals(_.keys({one : 1, two : 2}).join(', '), 'one, two', 'can extract the keys from an object');
|
||||
equal(_.keys({one : 1, two : 2}).join(', '), 'one, two', 'can extract the keys from an object');
|
||||
// the test above is not safe because it relies on for-in enumeration order
|
||||
var a = []; a[1] = 0;
|
||||
equals(_.keys(a).join(', '), '1', 'is not fooled by sparse arrays; see issue #95');
|
||||
equal(_.keys(a).join(', '), '1', 'is not fooled by sparse arrays; see issue #95');
|
||||
raises(function() { _.keys(null); }, exception, 'throws an error for `null` values');
|
||||
raises(function() { _.keys(void 0); }, exception, 'throws an error for `undefined` values');
|
||||
raises(function() { _.keys(1); }, exception, 'throws an error for number primitives');
|
||||
@@ -16,7 +16,7 @@ $(document).ready(function() {
|
||||
});
|
||||
|
||||
test("objects: values", function() {
|
||||
equals(_.values({one : 1, two : 2}).join(', '), '1, 2', 'can extract the values from an object');
|
||||
equal(_.values({one : 1, two : 2}).join(', '), '1, 2', 'can extract the values from an object');
|
||||
});
|
||||
|
||||
test("objects: functions", function() {
|
||||
@@ -25,20 +25,20 @@ $(document).ready(function() {
|
||||
|
||||
var Animal = function(){};
|
||||
Animal.prototype.run = function(){};
|
||||
equals(_.functions(new Animal).join(''), 'run', 'also looks up functions on the prototype');
|
||||
equal(_.functions(new Animal).join(''), 'run', 'also looks up functions on the prototype');
|
||||
});
|
||||
|
||||
test("objects: extend", function() {
|
||||
var result;
|
||||
equals(_.extend({}, {a:'b'}).a, 'b', 'can extend an object with the attributes of another');
|
||||
equals(_.extend({a:'x'}, {a:'b'}).a, 'b', 'properties in source override destination');
|
||||
equals(_.extend({x:'x'}, {a:'b'}).x, 'x', 'properties not in source dont get overriden');
|
||||
equal(_.extend({}, {a:'b'}).a, 'b', 'can extend an object with the attributes of another');
|
||||
equal(_.extend({a:'x'}, {a:'b'}).a, 'b', 'properties in source override destination');
|
||||
equal(_.extend({x:'x'}, {a:'b'}).x, 'x', 'properties not in source dont get overriden');
|
||||
result = _.extend({x:'x'}, {a:'a'}, {b:'b'});
|
||||
ok(_.isEqual(result, {x:'x', a:'a', b:'b'}), 'can extend from multiple source objects');
|
||||
result = _.extend({x:'x'}, {a:'a', x:2}, {a:'b'});
|
||||
ok(_.isEqual(result, {x:2, a:'b'}), 'extending from multiple source objects last property trumps');
|
||||
result = _.extend({}, {a: void 0, b: null});
|
||||
equals(_.keys(result).join(''), 'ab', 'extend does not copy undefined values');
|
||||
equal(_.keys(result).join(''), 'ab', 'extend does not copy undefined values');
|
||||
});
|
||||
|
||||
test("objects: defaults", function() {
|
||||
@@ -46,30 +46,30 @@ $(document).ready(function() {
|
||||
var options = {zero: 0, one: 1, empty: "", nan: NaN, string: "string"};
|
||||
|
||||
_.defaults(options, {zero: 1, one: 10, twenty: 20});
|
||||
equals(options.zero, 0, 'value exists');
|
||||
equals(options.one, 1, 'value exists');
|
||||
equals(options.twenty, 20, 'default applied');
|
||||
equal(options.zero, 0, 'value exists');
|
||||
equal(options.one, 1, 'value exists');
|
||||
equal(options.twenty, 20, 'default applied');
|
||||
|
||||
_.defaults(options, {empty: "full"}, {nan: "nan"}, {word: "word"}, {word: "dog"});
|
||||
equals(options.empty, "", 'value exists');
|
||||
equal(options.empty, "", 'value exists');
|
||||
ok(_.isNaN(options.nan), "NaN isn't overridden");
|
||||
equals(options.word, "word", 'new value is added, first one wins');
|
||||
equal(options.word, "word", 'new value is added, first one wins');
|
||||
});
|
||||
|
||||
test("objects: clone", function() {
|
||||
var moe = {name : 'moe', lucky : [13, 27, 34]};
|
||||
var clone = _.clone(moe);
|
||||
equals(clone.name, 'moe', 'the clone as the attributes of the original');
|
||||
equal(clone.name, 'moe', 'the clone as the attributes of the original');
|
||||
|
||||
clone.name = 'curly';
|
||||
ok(clone.name == 'curly' && moe.name == 'moe', 'clones can change shallow attributes without affecting the original');
|
||||
|
||||
clone.lucky.push(101);
|
||||
equals(_.last(moe.lucky), 101, 'changes to deep attributes are shared with the original');
|
||||
equal(_.last(moe.lucky), 101, 'changes to deep attributes are shared with the original');
|
||||
|
||||
equals(_.clone(undefined), void 0, 'non objects should not be changed by clone');
|
||||
equals(_.clone(1), 1, 'non objects should not be changed by clone');
|
||||
equals(_.clone(null), null, 'non objects should not be changed by clone');
|
||||
equal(_.clone(undefined), void 0, 'non objects should not be changed by clone');
|
||||
equal(_.clone(1), 1, 'non objects should not be changed by clone');
|
||||
equal(_.clone(null), null, 'non objects should not be changed by clone');
|
||||
});
|
||||
|
||||
test("objects: isEqual", function() {
|
||||
@@ -291,7 +291,7 @@ $(document).ready(function() {
|
||||
|
||||
// Chaining.
|
||||
ok(!_.isEqual(_({x: 1, y: undefined}).chain(), _({x: 1, z: 2}).chain()), 'Chained objects containing different values are not equal');
|
||||
equals(_({x: 1, y: 2}).chain().isEqual(_({x: 1, y: 2}).chain()).value(), true, '`isEqual` can be chained');
|
||||
equal(_({x: 1, y: 2}).chain().isEqual(_({x: 1, y: 2}).chain()).value(), true, '`isEqual` can be chained');
|
||||
|
||||
// Custom `isEqual` methods.
|
||||
var isEqualObj = {isEqual: function (o) { return o.isEqual == this.isEqual; }, unique: {}};
|
||||
@@ -522,8 +522,8 @@ $(document).ready(function() {
|
||||
var intercepted = null;
|
||||
var interceptor = function(obj) { intercepted = obj; };
|
||||
var returned = _.tap(1, interceptor);
|
||||
equals(intercepted, 1, "passes tapped object to interceptor");
|
||||
equals(returned, 1, "returns tapped object");
|
||||
equal(intercepted, 1, "passes tapped object to interceptor");
|
||||
equal(returned, 1, "returns tapped object");
|
||||
|
||||
returned = _([1,2,3]).chain().
|
||||
map(function(n){ return n * 2; }).
|
||||
|
||||
@@ -6,19 +6,19 @@ $(document).ready(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');
|
||||
equal(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');
|
||||
equal(_.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');
|
||||
equal(_.uniq(ids).length, ids.length, 'can generate a globally-unique stream of ids');
|
||||
});
|
||||
|
||||
test("utility: times", function() {
|
||||
@@ -37,38 +37,38 @@ $(document).ready(function() {
|
||||
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');
|
||||
equal(_.myReverse('panacea'), 'aecanap', 'mixed in a function to _');
|
||||
equal(_('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 &amp; Moe");
|
||||
equal(_.escape("Curly & Moe"), "Curly & Moe");
|
||||
equal(_.escape("Curly & Moe"), "Curly &amp; 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');
|
||||
equal(result, "This is gettin' on my noives!", 'can do basic attribute interpolation');
|
||||
|
||||
var sansSemicolonTemplate = _.template("A <% this %> B");
|
||||
equals(sansSemicolonTemplate(), "A B");
|
||||
equal(sansSemicolonTemplate(), "A B");
|
||||
|
||||
var backslashTemplate = _.template("<%= thing %> is \\ridanculous");
|
||||
equals(backslashTemplate({thing: 'This'}), "This is \\ridanculous");
|
||||
equal(backslashTemplate({thing: 'This'}), "This is \\ridanculous");
|
||||
|
||||
var escapeTemplate = _.template('<%= a ? "checked=\\"checked\\"" : "" %>');
|
||||
equals(escapeTemplate({a: true}), 'checked="checked"', 'can handle slash escapes in interpolations.');
|
||||
equal(escapeTemplate({a: true}), 'checked="checked"', 'can handle slash escapes in interpolations.');
|
||||
|
||||
var fancyTemplate = _.template("<ul><% \
|
||||
for (key in people) { \
|
||||
%><li><%= people[key] %></li><% } %></ul>");
|
||||
result = fancyTemplate({people : {moe : "Moe", larry : "Larry", curly : "Curly"}});
|
||||
equals(result, "<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>", 'can run arbitrary javascript in templates');
|
||||
equal(result, "<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>", 'can run arbitrary javascript in templates');
|
||||
|
||||
var escapedCharsInJavascriptTemplate = _.template("<ul><% _.each(numbers.split('\\n'), function(item) { %><li><%= item %></li><% }) %></ul>");
|
||||
result = escapedCharsInJavascriptTemplate({numbers: "one\ntwo\nthree\nfour"});
|
||||
equals(result, "<ul><li>one</li><li>two</li><li>three</li><li>four</li></ul>", 'Can use escaped characters (e.g. \\n) in Javascript');
|
||||
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><% }); %>");
|
||||
result = namespaceCollisionTemplate({
|
||||
@@ -79,36 +79,36 @@ $(document).ready(function() {
|
||||
3: "p3-thumbnail.gif"
|
||||
}
|
||||
});
|
||||
equals(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>");
|
||||
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>");
|
||||
|
||||
var noInterpolateTemplate = _.template("<div><p>Just some text. Hey, I know this is silly but it aids consistency.</p></div>");
|
||||
result = noInterpolateTemplate();
|
||||
equals(result, "<div><p>Just some text. Hey, I know this is silly but it aids consistency.</p></div>");
|
||||
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");
|
||||
equals(quoteTemplate({}), "It's its, not it's");
|
||||
equal(quoteTemplate({}), "It's its, not it's");
|
||||
|
||||
var quoteInStatementAndBody = _.template("<%\
|
||||
if(foo == 'bar'){ \
|
||||
%>Statement quotes and 'quotes'.<% } %>");
|
||||
equals(quoteInStatementAndBody({foo: "bar"}), "Statement quotes and 'quotes'.");
|
||||
equal(quoteInStatementAndBody({foo: "bar"}), "Statement quotes and 'quotes'.");
|
||||
|
||||
var withNewlinesAndTabs = _.template('This\n\t\tis: <%= x %>.\n\tok.\nend.');
|
||||
equals(withNewlinesAndTabs({x: 'that'}), 'This\n\t\tis: that.\n\tok.\nend.');
|
||||
equal(withNewlinesAndTabs({x: 'that'}), 'This\n\t\tis: that.\n\tok.\nend.');
|
||||
|
||||
var template = _.template("<i><%- value %></i>");
|
||||
var result = template({value: "<script>"});
|
||||
equals(result, '<i><script></i>');
|
||||
equal(result, '<i><script></i>');
|
||||
|
||||
var stooge = {
|
||||
name: "Moe",
|
||||
template: _.template("I'm <%= this.name %>")
|
||||
};
|
||||
equals(stooge.template(), "I'm Moe");
|
||||
equal(stooge.template(), "I'm Moe");
|
||||
|
||||
if (!$.browser.msie) {
|
||||
var fromHTML = _.template($('#template').html());
|
||||
equals(fromHTML({data : 12345}).replace(/\s/g, ''), '<li>24690</li>');
|
||||
equal(fromHTML({data : 12345}).replace(/\s/g, ''), '<li>24690</li>');
|
||||
}
|
||||
|
||||
_.templateSettings = {
|
||||
@@ -118,13 +118,13 @@ $(document).ready(function() {
|
||||
|
||||
var custom = _.template("<ul>{{ for (key in people) { }}<li>{{= people[key] }}</li>{{ } }}</ul>");
|
||||
result = custom({people : {moe : "Moe", larry : "Larry", curly : "Curly"}});
|
||||
equals(result, "<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>", 'can run arbitrary javascript in templates');
|
||||
equal(result, "<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>", 'can run arbitrary javascript in templates');
|
||||
|
||||
var customQuote = _.template("It's its, not it's");
|
||||
equals(customQuote({}), "It's its, not it's");
|
||||
equal(customQuote({}), "It's its, not it's");
|
||||
|
||||
var quoteInStatementAndBody = _.template("{{ if(foo == 'bar'){ }}Statement quotes and 'quotes'.{{ } }}");
|
||||
equals(quoteInStatementAndBody({foo: "bar"}), "Statement quotes and 'quotes'.");
|
||||
equal(quoteInStatementAndBody({foo: "bar"}), "Statement quotes and 'quotes'.");
|
||||
|
||||
_.templateSettings = {
|
||||
evaluate : /<\?([\s\S]+?)\?>/g,
|
||||
@@ -133,23 +133,23 @@ $(document).ready(function() {
|
||||
|
||||
var customWithSpecialChars = _.template("<ul><? for (key in people) { ?><li><?= people[key] ?></li><? } ?></ul>");
|
||||
result = customWithSpecialChars({people : {moe : "Moe", larry : "Larry", curly : "Curly"}});
|
||||
equals(result, "<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>", 'can run arbitrary javascript in templates');
|
||||
equal(result, "<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>", 'can run arbitrary javascript in templates');
|
||||
|
||||
var customWithSpecialCharsQuote = _.template("It's its, not it's");
|
||||
equals(customWithSpecialCharsQuote({}), "It's its, not it's");
|
||||
equal(customWithSpecialCharsQuote({}), "It's its, not it's");
|
||||
|
||||
var quoteInStatementAndBody = _.template("<? if(foo == 'bar'){ ?>Statement quotes and 'quotes'.<? } ?>");
|
||||
equals(quoteInStatementAndBody({foo: "bar"}), "Statement quotes and 'quotes'.");
|
||||
equal(quoteInStatementAndBody({foo: "bar"}), "Statement quotes and 'quotes'.");
|
||||
|
||||
_.templateSettings = {
|
||||
interpolate : /\{\{(.+?)\}\}/g
|
||||
};
|
||||
|
||||
var mustache = _.template("Hello {{planet}}!");
|
||||
equals(mustache({planet : "World"}), "Hello World!", "can mimic mustache.js");
|
||||
equal(mustache({planet : "World"}), "Hello World!", "can mimic mustache.js");
|
||||
|
||||
var templateWithNull = _.template("a null undefined {{planet}}");
|
||||
equals(templateWithNull({planet : "world"}), "a null undefined world", "can handle missing escape and evaluate settings");
|
||||
equal(templateWithNull({planet : "world"}), "a null undefined world", "can handle missing escape and evaluate settings");
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
54
test/vendor/qunit.css
vendored
54
test/vendor/qunit.css
vendored
@@ -1,7 +1,17 @@
|
||||
/**
|
||||
* QUnit v1.2.0 - A JavaScript Unit Testing Framework
|
||||
*
|
||||
* http://docs.jquery.com/QUnit
|
||||
*
|
||||
* Copyright (c) 2011 John Resig, Jörn Zaefferer
|
||||
* Dual licensed under the MIT (MIT-LICENSE.txt)
|
||||
* or GPL (GPL-LICENSE.txt) licenses.
|
||||
*/
|
||||
|
||||
/** Font Family and Sizes */
|
||||
|
||||
#qunit-tests, #qunit-header, .qunit-header, #qunit-banner, #qunit-testrunner-toolbar, #qunit-userAgent, #qunit-testresult {
|
||||
font-family: "Helvetica Neue Light", "HelveticaNeue-Light", "Helvetica Neue", Calibri, Helvetica, Arial;
|
||||
#qunit-tests, #qunit-header, #qunit-banner, #qunit-testrunner-toolbar, #qunit-userAgent, #qunit-testresult {
|
||||
font-family: "Helvetica Neue Light", "HelveticaNeue-Light", "Helvetica Neue", Calibri, Helvetica, Arial, sans-serif;
|
||||
}
|
||||
|
||||
#qunit-testrunner-toolbar, #qunit-userAgent, #qunit-testresult, #qunit-tests li { font-size: small; }
|
||||
@@ -18,7 +28,7 @@
|
||||
|
||||
/** Header */
|
||||
|
||||
#qunit-header, .qunit-header {
|
||||
#qunit-header {
|
||||
padding: 0.5em 0 0.5em 1em;
|
||||
|
||||
color: #8699a4;
|
||||
@@ -49,7 +59,9 @@
|
||||
}
|
||||
|
||||
#qunit-testrunner-toolbar {
|
||||
padding: 0em 0 0.5em 2em;
|
||||
padding: 0.5em 0 0.5em 2em;
|
||||
color: #5E740B;
|
||||
background-color: #eee;
|
||||
}
|
||||
|
||||
#qunit-userAgent {
|
||||
@@ -72,10 +84,24 @@
|
||||
list-style-position: inside;
|
||||
}
|
||||
|
||||
#qunit-tests.hidepass li.pass, #qunit-tests.hidepass li.running {
|
||||
display: none;
|
||||
}
|
||||
|
||||
#qunit-tests li strong {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
#qunit-tests li a {
|
||||
padding: 0.5em;
|
||||
color: #c2ccd1;
|
||||
text-decoration: none;
|
||||
}
|
||||
#qunit-tests li a:hover,
|
||||
#qunit-tests li a:focus {
|
||||
color: #000;
|
||||
}
|
||||
|
||||
#qunit-tests ol {
|
||||
margin-top: 0.5em;
|
||||
padding: 0.5em;
|
||||
@@ -160,6 +186,14 @@
|
||||
color: #710909;
|
||||
background-color: #fff;
|
||||
border-left: 26px solid #EE5757;
|
||||
white-space: pre;
|
||||
}
|
||||
|
||||
#qunit-tests > li:last-child {
|
||||
border-radius: 0 0 15px 15px;
|
||||
-moz-border-radius: 0 0 15px 15px;
|
||||
-webkit-border-bottom-right-radius: 15px;
|
||||
-webkit-border-bottom-left-radius: 15px;
|
||||
}
|
||||
|
||||
#qunit-tests .fail { color: #000000; background-color: #EE5757; }
|
||||
@@ -169,11 +203,10 @@
|
||||
#qunit-tests .fail .test-actual { color: #EE5757; }
|
||||
#qunit-tests .fail .test-expected { color: green; }
|
||||
|
||||
#qunit-banner.qunit-fail,
|
||||
#qunit-testrunner-toolbar { background-color: #EE5757; }
|
||||
#qunit-banner.qunit-fail { background-color: #EE5757; }
|
||||
|
||||
|
||||
/** Footer */
|
||||
/** Result */
|
||||
|
||||
#qunit-testresult {
|
||||
padding: 0.5em 0.5em 0.5em 2.5em;
|
||||
@@ -181,10 +214,7 @@
|
||||
color: #2b81af;
|
||||
background-color: #D2E0E6;
|
||||
|
||||
border-radius: 0 0 15px 15px;
|
||||
-moz-border-radius: 0 0 15px 15px;
|
||||
-webkit-border-bottom-right-radius: 15px;
|
||||
-webkit-border-bottom-left-radius: 15px;
|
||||
border-bottom: 1px solid white;
|
||||
}
|
||||
|
||||
/** Fixture */
|
||||
@@ -193,4 +223,4 @@
|
||||
position: absolute;
|
||||
top: -10000px;
|
||||
left: -10000px;
|
||||
}
|
||||
}
|
||||
|
||||
1867
test/vendor/qunit.js
vendored
1867
test/vendor/qunit.js
vendored
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user