Add _.memoize test and normalize on using properties of a and b instead of x and y.

This commit is contained in:
John-David Dalton
2014-01-23 23:00:49 -08:00
parent 3bb7f0c291
commit efb78c823c

View File

@@ -1486,12 +1486,12 @@
(function() { (function() {
test('should work with functions created by `_.partial` and `_.partialRight`', 2, function() { test('should work with functions created by `_.partial` and `_.partialRight`', 2, function() {
function func() { function func() {
var result = [this.x]; var result = [this.a];
push.apply(result, arguments); push.apply(result, arguments);
return result; return result;
} }
var expected = [1, 2, 3], var expected = [1, 2, 3],
object = { 'x': 1 }, object = { 'a': 1 },
callback = _.createCallback(_.partial(func, 2), object); callback = _.createCallback(_.partial(func, 2), object);
deepEqual(callback(3), expected); deepEqual(callback(3), expected);
@@ -1514,9 +1514,10 @@
test('should return the function provided if already bound with `Function#bind`', 1, function() { test('should return the function provided if already bound with `Function#bind`', 1, function() {
function a() {} function a() {}
var object = {};
var bound = a.bind && a.bind(object); var object = {},
bound = a.bind && a.bind(object);
if (bound && !('prototype' in bound)) { if (bound && !('prototype' in bound)) {
var bound = a.bind(object); var bound = a.bind(object);
strictEqual(_.createCallback(bound, object), bound); strictEqual(_.createCallback(bound, object), bound);
@@ -5292,14 +5293,16 @@
deepEqual(actual, shadowedProps); deepEqual(actual, shadowedProps);
}); });
test('should expose a `cache` object on the `memoized` function', 2, function() { test('should expose a `cache` object on the `memoized` function', 4, function() {
var memoized = _.memoize(_.identity, _.identity); _.forEach(['_a', 'a'], function(key, index) {
var memoized = _.memoize(_.identity, index && _.identity);
memoized('x'); memoized('a');
equal(memoized.cache.x, 'x'); equal(memoized.cache[key], 'a');
memoized.cache.x = 'y'; memoized.cache[key] = 'b';
equal(memoized('x'), 'y'); equal(memoized('a'), 'b');
});
}); });
}()); }());
@@ -5900,8 +5903,8 @@
}); });
test('`_.' + methodName + '` partially applies with additional arguments', 1, function() { test('`_.' + methodName + '` partially applies with additional arguments', 1, function() {
var expected = ['a', 'b'], var fn = function(a, b) { return [a, b]; },
fn = function(a, b) { return [a, b]; }; expected = ['a', 'b'];
if (!isPartial) { if (!isPartial) {
expected.reverse(); expected.reverse();
@@ -5920,8 +5923,8 @@
}); });
test('`_.' + methodName + '` should not alter the `this` binding', 3, function() { test('`_.' + methodName + '` should not alter the `this` binding', 3, function() {
var object = { 'a': 1 }, var fn = function() { return this.a; },
fn = function() { return this.a; }; object = { 'a': 1 };
strictEqual(func(_.bind(fn, object))(), object.a); strictEqual(func(_.bind(fn, object))(), object.a);
strictEqual(_.bind(func(fn), object)(), object.a); strictEqual(_.bind(func(fn), object)(), object.a);
@@ -6003,12 +6006,12 @@
test('combinations of bound and partial functions should work', 3, function() { test('combinations of bound and partial functions should work', 3, function() {
function func() { function func() {
var result = [this.x]; var result = [this.a];
push.apply(result, arguments); push.apply(result, arguments);
return result; return result;
} }
var expected = [1, 2, 3, 4], var expected = [1, 2, 3, 4],
object = { 'func': func, 'x': 1 }; object = { 'a': 1, 'func': func };
var a = _.bindKey(object, 'func'), var a = _.bindKey(object, 'func'),
b = _.partialRight(a, 4), b = _.partialRight(a, 4),
@@ -6031,11 +6034,11 @@
test('recursively bound functions should work', 1, function() { test('recursively bound functions should work', 1, function() {
function func() { function func() {
return this.x; return this.a;
} }
var a = _.bind(func, { 'x': 1 }), var a = _.bind(func, { 'a': 1 }),
b = _.bind(a, { 'x': 2 }), b = _.bind(a, { 'a': 2 }),
c = _.bind(b, { 'x': 3 }); c = _.bind(b, { 'a': 3 });
strictEqual(c(), 1); strictEqual(c(), 1);
}); });
@@ -7663,19 +7666,17 @@
asyncTest('subsequent calls should return the result of the first call', 5, function() { asyncTest('subsequent calls should return the result of the first call', 5, function() {
if (!(isRhino && isModularize)) { if (!(isRhino && isModularize)) {
var throttled = _.throttle(function(value) { return value; }, 32), var throttled = _.throttle(function(value) { return value; }, 32),
result = [throttled('x'), throttled('y')]; result = [throttled('a'), throttled('b')];
deepEqual(result, ['x', 'x']); deepEqual(result, ['a', 'a']);
setTimeout(function() { setTimeout(function() {
var result = [throttled('a'), throttled('b')]; var result = [throttled('x'), throttled('y')];
notEqual(result[0], 'x'); notEqual(result[0], 'x');
notStrictEqual(result[0], undefined); notStrictEqual(result[0], undefined);
notEqual(result[1], 'b'); notEqual(result[1], 'y');
notStrictEqual(result[1], undefined); notStrictEqual(result[1], undefined);
QUnit.start(); QUnit.start();
}, 64); }, 64);
} }
@@ -7800,8 +7801,8 @@
return value; return value;
}, 32, {}); }, 32, {});
equal(throttled('x'), 'x'); equal(throttled('a'), 'a');
equal(throttled('y'), 'x'); equal(throttled('b'), 'a');
setTimeout(function() { setTimeout(function() {
strictEqual(count, 2); strictEqual(count, 2);
@@ -7818,12 +7819,12 @@
if (!(isRhino && isModularize)) { if (!(isRhino && isModularize)) {
_.forEach([true, { 'leading': true }], function(options) { _.forEach([true, { 'leading': true }], function(options) {
var withLeading = _.throttle(_.identity, 32, options); var withLeading = _.throttle(_.identity, 32, options);
equal(withLeading('x'), 'x'); equal(withLeading('a'), 'a');
}); });
_.forEach([false, { 'leading': false }], function(options) { _.forEach([false, { 'leading': false }], function(options) {
var withoutLeading = _.throttle(_.identity, 32, options); var withoutLeading = _.throttle(_.identity, 32, options);
strictEqual(withoutLeading('x'), undefined); strictEqual(withoutLeading('a'), undefined);
}); });
} }
else { else {
@@ -7846,11 +7847,11 @@
return value; return value;
}, 64, { 'trailing': false }); }, 64, { 'trailing': false });
equal(withTrailing('x'), 'x'); equal(withTrailing('a'), 'a');
equal(withTrailing('y'), 'x'); equal(withTrailing('b'), 'a');
equal(withoutTrailing('x'), 'x'); equal(withoutTrailing('a'), 'a');
equal(withoutTrailing('y'), 'x'); equal(withoutTrailing('b'), 'a');
setTimeout(function() { setTimeout(function() {
equal(withCount, 2); equal(withCount, 2);