From cf61edffa8c7b62ceb44bfc5f290775007f9ea4d Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Mon, 26 Oct 2009 08:43:10 -0400 Subject: [PATCH] merged some tests --- test/arrays.js | 1 + test/collections.js | 22 +++++++++------------- test/objects.js | 2 ++ 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/test/arrays.js b/test/arrays.js index 90a1e2e25..97ff295a9 100644 --- a/test/arrays.js +++ b/test/arrays.js @@ -27,6 +27,7 @@ $(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'); + 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'); }); diff --git a/test/collections.js b/test/collections.js index 1cb3f9e5f..4aebedba4 100644 --- a/test/collections.js +++ b/test/collections.js @@ -3,24 +3,16 @@ $(document).ready(function() { module("Collection functions (each, any, select, and so on...)"); 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'); }); - }); - - test('collections: each and throwing "__break__"', function() { + var answer = null; - _.each([1, 2, 3], function(num){ - if ((answer = num) == 2) throw '__break__'; - }); + _.each([1, 2, 3], function(num){ if ((answer = num) == 2) throw '__break__'; }); equals(answer, 2, 'the loop broke in the middle'); - }); - - test('collections: each can take a context object', function() { + var answers = []; - _.each([1, 2, 3], function(num) { - answers.push(num * this.multiplier); - }, {multiplier : 5}); + _.each([1, 2, 3], function(num) { answers.push(num * this.multiplier);}, {multiplier : 5}); equals(answers.join(', '), '5, 10, 15', 'context object property accessed'); }); @@ -43,6 +35,7 @@ $(document).ready(function() { test('collections: map', function() { var doubled = _.map([1, 2, 3], function(num){ return num * 2; }); equals(doubled.join(', '), '2, 4, 6', 'doubled numbers'); + var tripled = _.map([1, 2, 3], function(num){ return num * this.multiplier; }, {multiplier : 3}); equals(tripled.join(', '), '3, 6, 9', 'tripled numbers with context'); }); @@ -87,12 +80,14 @@ $(document).ready(function() { test('collections: max', function() { equals(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'); }); test('collections: min', function() { equals(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'); }); @@ -112,6 +107,7 @@ $(document).ready(function() { test('collections: toArray', function() { ok(!_.isArray(arguments), 'arguments object is not an array'); ok(_.isArray(_.toArray(arguments)), 'arguments object converted into array'); + var numbers = _.toArray({one : 1, two : 2, three : 3}); equals(_.pluck(numbers, '0').join(', '), 'one, two, three', 'object flattened into array'); }); diff --git a/test/objects.js b/test/objects.js index 347230b59..624d67bfe 100644 --- a/test/objects.js +++ b/test/objects.js @@ -20,8 +20,10 @@ $(document).ready(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'); + 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'); });