From 520d344921054665858c9ee9725b289efeb47145 Mon Sep 17 00:00:00 2001 From: Petka Antonov Date: Mon, 7 Jul 2014 13:32:55 +0300 Subject: [PATCH] Consistently reject with a RangeError when input array contains too few promises. Fixes #233 --- src/some.js | 18 ++++++++++++++---- test/mocha/when_any.js | 14 ++++++-------- test/mocha/when_some.js | 14 ++++++-------- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/some.js b/src/some.js index cce517b75..d91cfd45a 100644 --- a/src/some.js +++ b/src/some.js @@ -24,14 +24,12 @@ SomePromiseArray.prototype._init = function SomePromiseArray$_init() { this._resolve([]); return; } - this._init$(void 0, RESOLVE_ARRAY); + this._init$(void 0, RESOLVE_CALL_METHOD); var isArrayResolved = isArray(this._values); if (!this._isResolved() && isArrayResolved && this._howMany > this._canPossiblyFulfill()) { - var message = "(Promise.some) input array contains less than " + - this._howMany + " promises"; - this._reject(new RangeError(message)); + this._reject(this._getRangeError(this.length())); } }; @@ -107,6 +105,18 @@ function SomePromiseArray$_canPossiblyFulfill() { return this.length() - this._rejected(); }; +SomePromiseArray.prototype._getRangeError = +function SomePromiseArray$_getRangeError(count) { + var message = "Input array must contain at least " + + this._howMany + " items but contains only " + count + " items"; + return new RangeError(message); +}; + +SomePromiseArray.prototype._resolveEmptyArray = +function SomePromiseArray$_resolveEmptyArray() { + this._reject(this._getRangeError(0)); +}; + function Promise$_Some(promises, howMany) { if ((howMany | 0) !== howMany || howMany < 0) { return apiRejection(POSITIVE_INTEGER_ERROR); diff --git a/test/mocha/when_any.js b/test/mocha/when_any.js index 6d39e0bc3..79fed5c09 100644 --- a/test/mocha/when_any.js +++ b/test/mocha/when_any.js @@ -60,18 +60,16 @@ function contains(arr, result) { return arr.indexOf(result) > -1; } +var RangeError = when.RangeError; + describe("when.any-test", function () { - specify("should resolve to empty array with empty input array", function(done) { + specify("should reject on empty input array", function(done) { var a = []; - when.any(a).then( - function(result) { - assert(result !== a); - assert.deepEqual(result, []); - done(); - }, fail - ); + when.any(a).caught(RangeError, function() { + done(); + }); }); specify("should resolve with an input value", function(done) { diff --git a/test/mocha/when_some.js b/test/mocha/when_some.js index a58279312..072d0ead1 100644 --- a/test/mocha/when_some.js +++ b/test/mocha/when_some.js @@ -115,16 +115,14 @@ function isSubset(subset, superset) { return true; } +var RangeError = when.RangeError; + describe("when.some-test", function () { - specify("should resolve empty input", function(done) { - when.some([], 1).then( - function(result) { - assert.deepEqual(result, []); - done(); - }, - fail - ) + specify("should reject empty input", function(done) { + when.some([], 1).caught(RangeError, function() { + done(); + }); }); specify("should resolve values array", function(done) {