Increase test coverage of trim methods and _.sortBy.

This commit is contained in:
John-David Dalton
2014-01-13 00:39:19 -08:00
parent ab62875ab1
commit b7e2396269

View File

@@ -477,11 +477,16 @@
_.forEach(['trim', 'trimLeft', 'trimRight'], function(methodName) { _.forEach(['trim', 'trimLeft', 'trimRight'], function(methodName) {
try { try {
var actual = lodashBizarro[methodName](whitespace + 'a b c' + whitespace); var actual = [
lodashBizarro[methodName](whitespace + 'a b c' + whitespace),
lodashBizarro[methodName](''),
lodashBizarro[methodName]('-_-a-b-c-_-', '_-'),
lodashBizarro[methodName]('', '_-')
];
} catch(e) { } catch(e) {
actual = null; actual = null;
} }
equal(typeof actual, 'string', message('String#' + methodName)); ok(_.every(actual, _.isString), message('String#' + methodName));
}); });
} }
else { else {
@@ -902,9 +907,10 @@
equal(_.capitalize(' fred'), ' fred'); equal(_.capitalize(' fred'), ' fred');
}); });
test('should return an empty string when provided `null` or `undefined`', 2, function() { test('should return an empty string when provided `null`, `undefined`, or empty strings', 3, function() {
strictEqual(_.capitalize(null), ''); strictEqual(_.capitalize(null), '');
strictEqual(_.capitalize(undefined), ''); strictEqual(_.capitalize(undefined), '');
strictEqual(_.capitalize(''), '');
}); });
}()); }());
@@ -2072,9 +2078,10 @@
equal(_.escape('abc'), 'abc'); equal(_.escape('abc'), 'abc');
}); });
test('should return an empty string when provided `null` or `undefined`', 2, function() { test('should return an empty string when provided `null`, `undefined`, or empty strings', 3, function() {
strictEqual(_.escape(null), ''); strictEqual(_.escape(null), '');
strictEqual(_.escape(undefined), ''); strictEqual(_.escape(undefined), '');
strictEqual(_.escape(''), '');
}); });
}()); }());
@@ -7004,6 +7011,11 @@
QUnit.module('lodash.sortBy'); QUnit.module('lodash.sortBy');
(function() { (function() {
function Pair(x, y) {
this.x = x;
this.y = y;
}
var objects = [ var objects = [
{ 'a': 'x', 'b': 3 }, { 'a': 'x', 'b': 3 },
{ 'a': 'y', 'b': 4 }, { 'a': 'y', 'b': 4 },
@@ -7011,6 +7023,19 @@
{ 'a': 'y', 'b': 2 } { 'a': 'y', 'b': 2 }
]; ];
var stableOrder = [
new Pair(1, 1),
new Pair(1, 1), new Pair(1, 2),
new Pair(1, 3), new Pair(1, 4),
new Pair(1, 5), new Pair(1, 6),
new Pair(2, 1), new Pair(2, 2),
new Pair(2, 3), new Pair(2, 4),
new Pair(2, 5), new Pair(2, 6),
new Pair(undefined, 1), new Pair(undefined, 2),
new Pair(undefined, 3), new Pair(undefined, 4),
new Pair(undefined, 5), new Pair(undefined, 6)
];
test('should sort in ascending order', 1, function() { test('should sort in ascending order', 1, function() {
var actual = _.pluck(_.sortBy(objects, function(object) { var actual = _.pluck(_.sortBy(objects, function(object) {
return object.b; return object.b;
@@ -7020,28 +7045,11 @@
}); });
test('should perform a stable sort (test in IE > 8, Opera, and V8)', 1, function() { test('should perform a stable sort (test in IE > 8, Opera, and V8)', 1, function() {
function Pair(x, y) { var actual = _.sortBy(stableOrder, function(pair) {
this.x = x;
this.y = y;
}
var collection = [
new Pair(1, 1), new Pair(1, 2),
new Pair(1, 3), new Pair(1, 4),
new Pair(1, 5), new Pair(1, 6),
new Pair(2, 1), new Pair(2, 2),
new Pair(2, 3), new Pair(2, 4),
new Pair(2, 5), new Pair(2, 6),
new Pair(undefined, 1), new Pair(undefined, 2),
new Pair(undefined, 3), new Pair(undefined, 4),
new Pair(undefined, 5), new Pair(undefined, 6)
];
var actual = _.sortBy(collection, function(pair) {
return pair.x; return pair.x;
}); });
deepEqual(actual, collection); deepEqual(actual, stableOrder);
}); });
test('should work with `undefined` values', 1, function() { test('should work with `undefined` values', 1, function() {
@@ -7072,21 +7080,6 @@
deepEqual(actual, [3, 1, 2]); deepEqual(actual, [3, 1, 2]);
}); });
test('should work with an array for `callback`', 1, function() {
var actual = _.sortBy(objects, ['a', 'b']);
deepEqual(actual, [objects[2], objects[0], objects[3], objects[1]]);
});
test('should coerce arrays returned from a `callback`', 1, function() {
var actual = _.sortBy(objects, function(object) {
var result = [object.a, object.b];
result.toString = function() { return String(this[0]); };
return result;
});
deepEqual(actual, [objects[0], objects[2], objects[1], objects[3]]);
});
test('should work with a string for `callback`', 1, function() { test('should work with a string for `callback`', 1, function() {
var actual = _.pluck(_.sortBy(objects, 'b'), 'b'); var actual = _.pluck(_.sortBy(objects, 'b'), 'b');
deepEqual(actual, [1, 2, 3, 4]); deepEqual(actual, [1, 2, 3, 4]);
@@ -7099,6 +7092,26 @@
deepEqual(actual, [3, 1, 2]); deepEqual(actual, [3, 1, 2]);
}); });
test('should support sorting by an array of properties', 1, function() {
var actual = _.sortBy(objects, ['a', 'b']);
deepEqual(actual, [objects[2], objects[0], objects[3], objects[1]]);
});
test('should perform a stable sort when sorting by multiple properties (test in IE > 8, Opera, and V8)', 1, function() {
var actual = _.sortBy(stableOrder, ['x', 'y']);
deepEqual(actual, stableOrder);
});
test('should coerce arrays returned from a `callback`', 1, function() {
var actual = _.sortBy(objects, function(object) {
var result = [object.a, object.b];
result.toString = function() { return String(this[0]); };
return result;
});
deepEqual(actual, [objects[0], objects[2], objects[1], objects[3]]);
});
}()); }());
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
@@ -8094,9 +8107,12 @@
strictEqual(func(string, object), (index == 2 ? '-_-' : '') + 'a-b-c' + (index == 1 ? '-_-' : '')); strictEqual(func(string, object), (index == 2 ? '-_-' : '') + 'a-b-c' + (index == 1 ? '-_-' : ''));
}); });
test('`_.' + methodName + '` should return an empty string when provided `null` or `undefined`', 2, function() { test('`_.' + methodName + '` should return an empty string when provided `null`, `undefined`, or empty strings', 6, function() {
strictEqual(func(null), ''); _.forEach([null, '_-'], function(arg) {
strictEqual(func(undefined), ''); strictEqual(func.call(_, null, arg), '');
strictEqual(func.call(_, undefined, arg), '');
strictEqual(func.call(_, '', arg), '');
});
}); });
}); });
@@ -8128,9 +8144,10 @@
equal(_.unescape(_.escape(unescaped)), unescaped); equal(_.unescape(_.escape(unescaped)), unescaped);
}); });
test('should return an empty string when provided `null` or `undefined`', 2, function() { test('should return an empty string when provided `null`, `undefined`, or empty strings', 3, function() {
strictEqual(_.unescape(null), ''); strictEqual(_.unescape(null), '');
strictEqual(_.unescape(undefined), ''); strictEqual(_.unescape(undefined), '');
strictEqual(_.unescape(''), '');
}); });
}()); }());