From f3d54d097532328b05b151bbe30575b1423ccbf9 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Thu, 3 Dec 2015 22:43:55 -0800 Subject: [PATCH] Add `_.rangeRight` tests. --- lodash.js | 12 +++------ test/test.js | 69 +++++++++++++++++++++++++++++----------------------- 2 files changed, 42 insertions(+), 39 deletions(-) diff --git a/lodash.js b/lodash.js index e329ac0c3..fe79ab633 100644 --- a/lodash.js +++ b/lodash.js @@ -3273,13 +3273,9 @@ length = nativeMax(nativeCeil((end - start) / (step || 1)), 0), result = Array(length); - while (++index < length) { - if (fromRight) { - result[index] = (end -= step); - } else { - result[index] = start; - start += step; - } + while (length--) { + result[fromRight ? length : ++index] = start; + start += step; } return result; } @@ -13348,7 +13344,7 @@ * // => [-3, -2, -1, 0] * * _.rangeRight(1, 4, 0); - * // => [4, 4, 4] + * // => [1, 1, 1] * * _.rangeRight(0); * // => [] diff --git a/test/test.js b/test/test.js index 6bd08cc41..d409beda5 100644 --- a/test/test.js +++ b/test/test.js @@ -15443,90 +15443,97 @@ /*--------------------------------------------------------------------------*/ - QUnit.module('lodash.range'); + QUnit.module('range methods'); - (function() { - QUnit.test('should infer the sign of `step` when provided only an `end` argument', function(assert) { + lodashStable.each(['range', 'rangeRight'], function(methodName) { + var func = _[methodName], + isRange = methodName == 'range'; + + function resolve(range) { + return isRange ? range : range.reverse(); + } + + QUnit.test('`_.' + methodName + '` should infer the sign of `step` when provided only an `end` argument', function(assert) { assert.expect(2); - assert.deepEqual(_.range(4), [0, 1, 2, 3]); - assert.deepEqual(_.range(-4), [0, -1, -2, -3]); + assert.deepEqual(func(4), resolve([0, 1, 2, 3])); + assert.deepEqual(func(-4), resolve([0, -1, -2, -3])); }); - QUnit.test('should infer the sign of `step` when provided only a `start` and `end` argument', function(assert) { + QUnit.test('`_.' + methodName + '` should infer the sign of `step` when provided only a `start` and `end` argument', function(assert) { assert.expect(2); - assert.deepEqual(_.range(1, 5), [1, 2, 3, 4]); - assert.deepEqual(_.range(5, 1), [5, 4, 3, 2]); + assert.deepEqual(func(1, 5), resolve([1, 2, 3, 4])); + assert.deepEqual(func(5, 1), resolve([5, 4, 3, 2])); }); - QUnit.test('should work with `start`, `end`, and `step` arguments', function(assert) { + QUnit.test('`_.' + methodName + '` should work with `start`, `end`, and `step` arguments', function(assert) { assert.expect(3); - assert.deepEqual(_.range(0, -4, -1), [0, -1, -2, -3]); - assert.deepEqual(_.range(5, 1, -1), [5, 4, 3, 2]); - assert.deepEqual(_.range(0, 20, 5), [0, 5, 10, 15]); + assert.deepEqual(func(0, -4, -1), resolve([0, -1, -2, -3])); + assert.deepEqual(func(5, 1, -1), resolve([5, 4, 3, 2])); + assert.deepEqual(func(0, 20, 5), resolve([0, 5, 10, 15])); }); - QUnit.test('should support a `step` of `0`', function(assert) { + QUnit.test('`_.' + methodName + '` should support a `step` of `0`', function(assert) { assert.expect(1); - assert.deepEqual(_.range(1, 4, 0), [1, 1, 1]); + assert.deepEqual(func(1, 4, 0), [1, 1, 1]); }); - QUnit.test('should work with a `step` larger than `end`', function(assert) { + QUnit.test('`_.' + methodName + '` should work with a `step` larger than `end`', function(assert) { assert.expect(1); - assert.deepEqual(_.range(1, 5, 20), [1]); + assert.deepEqual(func(1, 5, 20), [1]); }); - QUnit.test('should work with a negative `step` argument', function(assert) { + QUnit.test('`_.' + methodName + '` should work with a negative `step` argument', function(assert) { assert.expect(2); - assert.deepEqual(_.range(0, -4, -1), [0, -1, -2, -3]); - assert.deepEqual(_.range(21, 10, -3), [21, 18, 15, 12]); + assert.deepEqual(func(0, -4, -1), resolve([0, -1, -2, -3])); + assert.deepEqual(func(21, 10, -3), resolve([21, 18, 15, 12])); }); - QUnit.test('should support `start` of `-0`', function(assert) { + QUnit.test('`_.' + methodName + '` should support `start` of `-0`', function(assert) { assert.expect(1); - var actual = _.range(-0, 1); + var actual = func(-0, 1); assert.strictEqual(1 / actual[0], -Infinity); }); - QUnit.test('should treat falsey `start` arguments as `0`', function(assert) { + QUnit.test('`_.' + methodName + '` should treat falsey `start` arguments as `0`', function(assert) { assert.expect(13); lodashStable.each(falsey, function(value, index) { if (index) { - assert.deepEqual(_.range(value), []); - assert.deepEqual(_.range(value, 1), [0]); + assert.deepEqual(func(value), []); + assert.deepEqual(func(value, 1), [0]); } else { - assert.deepEqual(_.range(), []); + assert.deepEqual(func(), []); } }); }); - QUnit.test('should coerce arguments to finite numbers', function(assert) { + QUnit.test('`_.' + methodName + '` should coerce arguments to finite numbers', function(assert) { assert.expect(1); - var actual = [_.range('0', 1), _.range('1'), _.range(0, 1, '1'), _.range(NaN), _.range(NaN, NaN)]; + var actual = [func('0', 1), func('1'), func(0, 1, '1'), func(NaN), func(NaN, NaN)]; assert.deepEqual(actual, [[0], [0], [0], [], []]); }); - QUnit.test('should work as an iteratee for methods like `_.map`', function(assert) { + QUnit.test('`_.' + methodName + '` should work as an iteratee for methods like `_.map`', function(assert) { assert.expect(2); var array = [1, 2, 3], object = { 'a': 1, 'b': 2, 'c': 3 }, - expected = [[0], [0, 1], [0, 1, 2]]; + expected = lodashStable.map([[0], [0, 1], [0, 1, 2]], resolve); lodashStable.each([array, object], function(collection) { - var actual = lodashStable.map(collection, _.range); + var actual = lodashStable.map(collection, func); assert.deepEqual(actual, expected); }); }); - }()); + }); /*--------------------------------------------------------------------------*/