Skip to content

Commit

Permalink
Merge pull request #1406 from lucasfcosta/called-in-order-considers-c…
Browse files Browse the repository at this point in the history
…allcount

Called in order considers callCount
  • Loading branch information
fearphage committed May 26, 2017
2 parents 3af985a + 54b5837 commit 2f941e2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
29 changes: 24 additions & 5 deletions lib/sinon/util/core/called-in-order.js
@@ -1,15 +1,34 @@
"use strict";

var every = Array.prototype.every;

module.exports = function calledInOrder(spies) {
var callMap = {};

function hasCallsLeft(spy) {
if (callMap[spy.id] === undefined) {
callMap[spy.id] = 0;
}

return callMap[spy.id] < spy.callCount;
}

if (arguments.length > 1) {
spies = arguments;
}

for (var i = 1, l = spies.length; i < l; i++) {
if (!spies[i - 1].calledBefore(spies[i]) || !spies[i].called) {
return false;
return every.call(spies, function checkAdjacentCalls(spy, i) {
var calledBeforeNext = true;

if (i !== spies.length - 1) {
calledBeforeNext = spy.calledBefore(spies[i + 1]);
}

if (hasCallsLeft(spy) && calledBeforeNext) {
callMap[spy.id] += 1;
return true;
}
}

return true;
return false;
});
};
15 changes: 15 additions & 0 deletions test/issues/issues-test.js
Expand Up @@ -186,4 +186,19 @@ describe("issues", function () {
assert(firstFake !== secondFake);
});
});

describe("#1398", function () {
it("Call order takes into account both calledBefore and callCount", function () {
var s1 = sinon.spy();
var s2 = sinon.spy();

s1();
s2();
s1();

assert.exception(function () {
sinon.assert.callOrder(s2, s1, s2);
});
});
});
});

0 comments on commit 2f941e2

Please sign in to comment.