Underscore 0.4.4, with isNumber, isString, and isEqual(NaN, NaN)

This commit is contained in:
Jeremy Ashkenas
2009-11-18 16:09:55 -05:00
parent b932867dec
commit c9e46262ab
6 changed files with 99 additions and 35 deletions

View File

@@ -1,68 +1,80 @@
$(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');
ok(_(moe).isEqual(clone), 'OO-style deep equality works');
ok(!_.isEqual(5, NaN), '5 is not equal to NaN');
ok(_.isEqual(NaN, NaN), 'NaN is equal to NaN');
});
test("objects: isEmpty", function() {
ok(!_([1]).isEmpty(), '[1] is not empty');
ok(_.isEmpty([]), '[] is empty');
ok(!_.isEmpty({one : 1}), '{one : 1} is not empty');
ok(_.isEmpty({}), '{} is empty');
var obj = {one : 1};
delete obj.one;
ok(_.isEmpty(obj), 'deleting all the keys from an object empties it');
});
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: isString", function() {
ok(!_.isString(document.body), 'the document body is not a string');
ok(_.isString([1, 2, 3].join(', ')), 'but strings are');
});
test("objects: isNumber", function() {
ok(!_.isNumber(arguments), 'the arguments object is not a number');
ok(_.isNumber(3 * 4 - 7 / 10), 'but numbers 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');

View File

@@ -13,7 +13,7 @@
<script type="text/javascript" src="objects.js"></script>
<script type="text/javascript" src="utility.js"></script>
<script type="text/javascript" src="chaining.js"></script>
<script type="text/javascript" src="speed.js"></script>
<script type="text/javascript" src="speed.js"></script>
</head>
<body>
<h1 id="qunit-header">Underscore Test Suite</h1>

View File

@@ -1,7 +1,7 @@
$(document).ready(function() {
module("Utility functions (uniqueId, template)");
test("utility: noConflict", function() {
var underscore = _.noConflict();
ok(underscore.isUndefined(_), "The '_' variable has been returned to its previous state.");
@@ -9,12 +9,12 @@ $(document).ready(function() {
equals(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');
});
test('utility: breakLoop', function() {
var result = null;
_([1,2,3,4,5,6]).each(function(num) {
@@ -23,25 +23,25 @@ $(document).ready(function() {
});
equals(result, 3, 'broke out of a loop');
});
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: functions", function() {
var expected = ["all", "any", "bind", "bindAll", "breakLoop", "clone", "compact", "compose",
"defer", "delay", "detect", "each", "every", "extend", "filter", "first",
"flatten", "foldl", "foldr", "forEach", "functions", "identity", "include",
"indexOf", "inject", "intersect", "invoke", "isArray", "isElement", "isEmpty", "isEqual",
"isFunction", "isUndefined", "keys", "last", "lastIndexOf", "map", "max",
"methods", "min", "pluck", "reduce", "reduceRight", "reject", "select",
"size", "some", "sortBy", "sortedIndex", "template", "toArray", "uniq",
var expected = ["all", "any", "bind", "bindAll", "breakLoop", "clone", "compact", "compose",
"defer", "delay", "detect", "each", "every", "extend", "filter", "first",
"flatten", "foldl", "foldr", "forEach", "functions", "identity", "include",
"indexOf", "inject", "intersect", "invoke", "isArray", "isElement", "isEmpty", "isEqual",
"isFunction", "isNumber", "isString", "isUndefined", "keys", "last", "lastIndexOf", "map", "max",
"methods", "min", "pluck", "reduce", "reduceRight", "reject", "select",
"size", "some", "sortBy", "sortedIndex", "template", "toArray", "uniq",
"uniqueId", "values", "without", "wrap", "zip"];
ok(_(expected).isEqual(_.methods()), 'provides a sorted list of functions');
});
test("utility: template", function() {
var basicTemplate = _.template("<%= thing %> is gettin' on my noives!");
var result = basicTemplate({thing : 'This'});
@@ -50,5 +50,5 @@ $(document).ready(function() {
result = fancyTemplate({people : {moe : "Moe", larry : "Larry", curly : "Curly"}});
equals(result, "<li>Moe</li><li>Larry</li><li>Curly</li>", 'can run arbitrary javascript in templates');
});
});