merged some tests

This commit is contained in:
Jeremy Ashkenas
2009-10-26 08:43:10 -04:00
parent 5c46c60b06
commit cf61edffa8
3 changed files with 12 additions and 13 deletions

View File

@@ -27,6 +27,7 @@ $(document).ready(function() {
test("arrays: uniq", function() { test("arrays: uniq", function() {
var list = [1, 2, 1, 3, 1, 4]; 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'); equals(_.uniq(list).join(', '), '1, 2, 3, 4', 'can find the unique values of an unsorted array');
var list = [1, 1, 1, 2, 2, 3]; 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'); equals(_.uniq(list, true).join(', '), '1, 2, 3', 'can find the unique values of a sorted array faster');
}); });

View File

@@ -3,24 +3,16 @@ $(document).ready(function() {
module("Collection functions (each, any, select, and so on...)"); module("Collection functions (each, any, select, and so on...)");
test("collections: each", function() { test("collections: each", function() {
_.each([1, 2, 3], function(num, i){ _.each([1, 2, 3], function(num, i) {
equals(num, i + 1, 'each iterators provide value and iteration count'); equals(num, i + 1, 'each iterators provide value and iteration count');
}); });
});
test('collections: each and throwing "__break__"', function() {
var answer = null; var answer = null;
_.each([1, 2, 3], function(num){ _.each([1, 2, 3], function(num){ if ((answer = num) == 2) throw '__break__'; });
if ((answer = num) == 2) throw '__break__';
});
equals(answer, 2, 'the loop broke in the middle'); equals(answer, 2, 'the loop broke in the middle');
});
test('collections: each can take a context object', function() {
var answers = []; var answers = [];
_.each([1, 2, 3], function(num) { _.each([1, 2, 3], function(num) { answers.push(num * this.multiplier);}, {multiplier : 5});
answers.push(num * this.multiplier);
}, {multiplier : 5});
equals(answers.join(', '), '5, 10, 15', 'context object property accessed'); equals(answers.join(', '), '5, 10, 15', 'context object property accessed');
}); });
@@ -43,6 +35,7 @@ $(document).ready(function() {
test('collections: map', function() { test('collections: map', function() {
var doubled = _.map([1, 2, 3], function(num){ return num * 2; }); var doubled = _.map([1, 2, 3], function(num){ return num * 2; });
equals(doubled.join(', '), '2, 4, 6', 'doubled numbers'); equals(doubled.join(', '), '2, 4, 6', 'doubled numbers');
var tripled = _.map([1, 2, 3], function(num){ return num * this.multiplier; }, {multiplier : 3}); var tripled = _.map([1, 2, 3], function(num){ return num * this.multiplier; }, {multiplier : 3});
equals(tripled.join(', '), '3, 6, 9', 'tripled numbers with context'); equals(tripled.join(', '), '3, 6, 9', 'tripled numbers with context');
}); });
@@ -87,12 +80,14 @@ $(document).ready(function() {
test('collections: max', function() { test('collections: max', function() {
equals(3, _.max([1, 2, 3]), 'can perform a regular Math.max'); equals(3, _.max([1, 2, 3]), 'can perform a regular Math.max');
var neg = _.max([1, 2, 3], function(num){ return -num; }); var neg = _.max([1, 2, 3], function(num){ return -num; });
equals(neg, 1, 'can perform a computation-based max'); equals(neg, 1, 'can perform a computation-based max');
}); });
test('collections: min', function() { test('collections: min', function() {
equals(1, _.min([1, 2, 3]), 'can perform a regular Math.min'); equals(1, _.min([1, 2, 3]), 'can perform a regular Math.min');
var neg = _.min([1, 2, 3], function(num){ return -num; }); var neg = _.min([1, 2, 3], function(num){ return -num; });
equals(neg, 3, 'can perform a computation-based min'); equals(neg, 3, 'can perform a computation-based min');
}); });
@@ -112,6 +107,7 @@ $(document).ready(function() {
test('collections: toArray', function() { test('collections: toArray', function() {
ok(!_.isArray(arguments), 'arguments object is not an array'); ok(!_.isArray(arguments), 'arguments object is not an array');
ok(_.isArray(_.toArray(arguments)), 'arguments object converted into array'); ok(_.isArray(_.toArray(arguments)), 'arguments object converted into array');
var numbers = _.toArray({one : 1, two : 2, three : 3}); var numbers = _.toArray({one : 1, two : 2, three : 3});
equals(_.pluck(numbers, '0').join(', '), 'one, two, three', 'object flattened into array'); equals(_.pluck(numbers, '0').join(', '), 'one, two, three', 'object flattened into array');
}); });

View File

@@ -20,8 +20,10 @@ $(document).ready(function() {
var moe = {name : 'moe', lucky : [13, 27, 34]}; var moe = {name : 'moe', lucky : [13, 27, 34]};
var clone = _.clone(moe); var clone = _.clone(moe);
equals(clone.name, 'moe', 'the clone as the attributes of the original'); equals(clone.name, 'moe', 'the clone as the attributes of the original');
clone.name = 'curly'; clone.name = 'curly';
ok(clone.name == 'curly' && moe.name == 'moe', 'clones can change shallow attributes without affecting the original'); ok(clone.name == 'curly' && moe.name == 'moe', 'clones can change shallow attributes without affecting the original');
clone.lucky.push(101); clone.lucky.push(101);
equals(_.last(moe.lucky), 101, 'changes to deep attributes are shared with the original'); equals(_.last(moe.lucky), 101, 'changes to deep attributes are shared with the original');
}); });