diff --git a/test/arrays.js b/test/arrays.js new file mode 100644 index 000000000..90a1e2e25 --- /dev/null +++ b/test/arrays.js @@ -0,0 +1,45 @@ +$(document).ready(function() { + + module("Array-only functions (last, compact, uniq, and so on...)"); + + test("arrays: first", function() { + equals(_.first([1,2,3]), 1, 'can pull out the first element of an array'); + }); + + test("arrays: last", function() { + equals(_.last([1,2,3]), 3, 'can pull out the last element of an array'); + }); + + test("arrays: compact", function() { + equals(_.compact([0, 1, false, 2, false, 3]).length, 3, 'can trim out all falsy values'); + }); + + test("arrays: flatten", function() { + var list = [1, [2], [3, [[[4]]]]]; + equals(_.flatten(list).join(', '), '1, 2, 3, 4', 'can flatten nested arrays'); + }); + + 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'); + }); + + 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'); + }); + + test("arrays: intersect", function() { + var stooges = ['moe', 'curly', 'larry'], leaders = ['moe', 'groucho']; + equals(_.intersect(stooges, leaders).join(''), 'moe', 'can take the set intersection of two arrays'); + }); + + 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'); + }); + +}); diff --git a/test/collections.js b/test/collections.js new file mode 100644 index 000000000..1cb3f9e5f --- /dev/null +++ b/test/collections.js @@ -0,0 +1,123 @@ +$(document).ready(function() { + + module("Collection functions (each, any, select, and so on...)"); + + test("collections: each", function() { + _.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__'; + }); + 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}); + equals(answers.join(', '), '5, 10, 15', 'context object property accessed'); + }); + + test('collections: all', function() { + ok(_.all([]), 'the empty set'); + ok(_.all([true, true, true]), 'all true values'); + ok(!_.all([true, false, true]), 'one false value'); + ok(_.all([0, 10, 28], function(num){ return num % 2 == 0; }), 'even numbers'); + ok(!_.all([0, 11, 28], function(num){ return num % 2 == 0; }), 'an odd number'); + }); + + test('collections: any', function() { + ok(!_.any([]), 'the empty set'); + ok(!_.any([false, false, false]), 'all false values'); + ok(_.any([false, false, true]), 'one true value'); + ok(!_.any([1, 11, 29], function(num){ return num % 2 == 0; }), 'all odd numbers'); + ok(_.any([1, 10, 29], function(num){ return num % 2 == 0; }), 'an even number'); + }); + + 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'); + }); + + test('collections: inject', function() { + var sum = _.inject([1,2,3], 0, function(sum, num){ return sum + num; }); + equals(sum, 6, 'can sum up an array'); + }); + + 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'); + }); + + 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'); + }); + + 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'); + }); + + test('collections: include', function() { + ok(_.include([1,2,3], 2), 'two is in the array'); + ok(!_.include([1,3,9], 2), 'two is not in the array'); + ok(_.include({moe:1, larry:3, curly:9}, 3), '_.include on objects checks their values'); + }); + + 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'); + }); + + 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'); + }); + + 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'); + }); + + 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'); + }); + + test('collections: sortedIndex', function() { + var numbers = [10, 20, 30, 40, 50], num = 35; + var index = _.sortedIndex(numbers, function(a, b) { return a < b ? -1 : a > b ? 1 : 0; }, num); + equals(index, 3, '35 should be inserted at index 3'); + }); + + 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'); + }); + + test('collections: size', function() { + equals(_.size({one : 1, two : 2, three : 3}), 3, 'can compute the size of an object'); + }); + +}); diff --git a/test/example.js b/test/example.js deleted file mode 100644 index 48ed383fe..000000000 --- a/test/example.js +++ /dev/null @@ -1,29 +0,0 @@ -$(document).ready(function(){ - - test("a basic test example", function() { - ok( true, "this test is fine" ); - var value = "hello"; - equals( "hello", value, "We expect value to be hello" ); - }); - - module("Module A"); - - test("first test within module", function() { - ok( true, "all pass" ); - }); - - test("second test within module", function() { - ok( true, "all pass" ); - }); - - module("Module B"); - - test("some other test", function() { - expect(2); - equals( true, false, "failing test" ); - equals( true, true, "passing test" ); - }); - -}); - - diff --git a/test/functions.js b/test/functions.js new file mode 100644 index 000000000..0f9362def --- /dev/null +++ b/test/functions.js @@ -0,0 +1,25 @@ +$(document).ready(function() { + + module("Function functions (bind, bindAll, and so on...)"); + + test("functions: bind", function() { + var context = {name : 'moe'}; + var func = function() { return "name: " + this.name; }; + var bound = _.bind(func, context); + equals(bound(), 'name: moe', 'can bind a function to a context'); + }); + + test("functions: bindAll", function() { + var curly = {name : 'curly'}, moe = { + name : 'moe', + getName : function() { return 'name: ' + this.name; }, + sayHi : function() { return 'hi: ' + this.name; } + }; + curly.getName = moe.getName; + _.bindAll('getName', 'sayHi', moe); + 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'); + }); + +}); diff --git a/test/objects.js b/test/objects.js new file mode 100644 index 000000000..347230b59 --- /dev/null +++ b/test/objects.js @@ -0,0 +1,64 @@ +$(document).ready(function() { + + module("Object functions (values, extend, isEqual, and so on...)"); + + test("objects: keys", function() { + equals(_.keys({one : 1, two : 2}).join(', '), 'one, two', 'can extract the keys from an object'); + }); + + test("objects: values", function() { + equals(_.values({one : 1, two : 2}).join(', '), '1, 2', 'can extract the values from an object'); + }); + + test("objects: extend", function() { + var source = {name : 'moe'}, dest = {age : 50}; + _.extend(dest, source); + equals(dest.name, 'moe', 'can extend an object with the attributes of another'); + }); + + 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'); + 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'); + }); + + test("objects: isEqual", function() { + var moe = {name : 'moe', lucky : [13, 27, 34]}; + var clone = {name : 'moe', lucky : [13, 27, 34]}; + ok(moe != clone, 'basic equality between objects is false'); + ok(_.isEqual(moe, clone), 'deep equality is true'); + }); + + test("objects: isElement", function() { + ok(!_.isElement('div'), 'strings are not dom elements'); + ok(_.isElement($('html')[0]), 'the html tag is a DOM element'); + }); + + test("objects: isArray", function() { + ok(!_.isArray(arguments), 'the arguments object is not an array'); + ok(_.isArray([1, 2, 3]), 'but arrays are'); + }); + + test("objects: isFunction", function() { + ok(!_.isFunction([1, 2, 3]), 'arrays are not functions'); + ok(!_.isFunction('moe'), 'strings are not functions'); + ok(_.isFunction(_.isFunction), 'but functions are'); + }); + + test("objects: isUndefined", function() { + ok(!_.isUndefined(1), 'numbers are defined'); + ok(!_.isUndefined(null), 'null is defined'); + ok(!_.isUndefined(false), 'false is defined'); + ok(_.isUndefined(), 'nothing is undefined'); + ok(_.isUndefined(undefined), 'undefined is undefined'); + }); + + test("objects: toString", function() { + equals(_.toString([1, 2, 3]), '1,2,3', 'object can be converted to printable strings'); + }); + +}); diff --git a/test/test.html b/test/test.html index 962718958..9c8220344 100644 --- a/test/test.html +++ b/test/test.html @@ -6,7 +6,11 @@ - + + + + +
diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 569cbe455..000000000 --- a/test/test.js +++ /dev/null @@ -1,59 +0,0 @@ -$(document).ready(function() { - - module("Collection functions (each, any, select, and so on...)"); - - test("collections: _.each", function() { - _.each(numbers, 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(numbers, function(num){ - if ((answer = num) == 2) throw '__break__'; - }); - equals(2, answer, 'the loop broke in the middle'); - }); - - test('collections: _.each can take a context object', function() { - var answers = []; - _.each(numbers, function(num) { - answers.push(num * this.multiplier); - }, {multiplier : 5}); - equals('5, 10, 15', answers.join(', '), 'context object property accessed'); - }); - - test('collections: _.all', function() { - ok(_.all([]), 'the empty set'); - ok(_.all([true, true, true]), 'all true values'); - ok(!_.all([true, false, true]), 'one false value'); - ok(_.all([0, 10, 28], function(num){ return num % 2 == 0; }), 'even numbers'); - ok(!_.all([0, 11, 28], function(num){ return num % 2 == 0; }), 'an odd number'); - }); - - test('collections: _.any', function() { - ok(!_.any([]), 'the empty set'); - ok(!_.any([false, false, false]), 'all false values'); - ok(_.any([false, false, true]), 'one true value'); - ok(!_.any([1, 11, 29], function(num){ return num % 2 == 0; }), 'all odd numbers'); - ok(_.any([1, 10, 29], function(num){ return num % 2 == 0; }), 'an even number'); - }); - - test('collections: _.map', function() { - var doubled = _.map(numbers, function(num){ return num * 2; }); - equals('2, 4, 6', doubled.join(', '), 'doubled numbers'); - var tripled = _.map(numbers, function(num){ return num * this.multiplier; }, {multiplier : 3}); - equals('3, 6, 9', tripled.join(', '), 'tripled numbers with context'); - }); - - test('collections: _.detect', function() { - var result = _.detect(numbers, function(num){ return num * 2 == 4; }); - equals(2, result, 'found the first "2" and broke the loop'); - }); - - test('collections: _.select', function() { - var evens = _.select(function([1,2,3,4,5,6], )) - }); - -}); \ No newline at end of file diff --git a/test/utility.js b/test/utility.js new file mode 100644 index 000000000..cfaaaace3 --- /dev/null +++ b/test/utility.js @@ -0,0 +1,20 @@ +$(document).ready(function() { + + module("Utility functions (uniqueId, template)"); + + 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'); + }); + + 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'); + var fancyTemplate = _.template("<% for (key in people) { %>