Skip to content

Commit

Permalink
Called in order takes callCount into account. Closes sinonjs#1398.
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfcosta committed May 17, 2017
1 parent 0127365 commit e580963
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
31 changes: 25 additions & 6 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;
spies = Array.prototype.slice.call(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 @@ -200,4 +200,19 @@ describe("issues", function () {
assert(firstFake !== secondFake);
});
});

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

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

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

0 comments on commit e580963

Please sign in to comment.