Skip to content

Commit

Permalink
move async method generation to stub behavior (sinonjs#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
tf committed Oct 3, 2013
1 parent 96d193f commit eed5b48
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 89 deletions.
39 changes: 20 additions & 19 deletions lib/sinon/stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
}

sinon.behavior = (function() {
var slice = Array.prototype.slice;
var slice = Array.prototype.slice, proto;

function getCallback(behavior, args) {
var callArgAt = behavior.callArgAt;
Expand Down Expand Up @@ -92,7 +92,7 @@
}
}

return {
proto = {
create: function(stubFunctionName) {
var behavior = sinon.extend({}, sinon.behavior);
delete behavior.create;
Expand Down Expand Up @@ -207,6 +207,24 @@
return this;
}
};


// create asynchronous versions of callsArg* and yields* methods
for (var method in proto) {
// need to avoid creating anotherasync versions of the newly added async methods
if (proto.hasOwnProperty(method) &&
method.match(/^(callsArg|yields|thenYields$)/) &&
!method.match(/Async/)) {
proto[method + 'Async'] = (function (syncFnName) {
return function () {
this.callbackAsync = true;
return this[syncFnName].apply(this, arguments);
};
})(method);
}
}

return proto;
}());

function stub(object, property, func) {
Expand Down Expand Up @@ -359,7 +377,6 @@
proto[method] = (function(behaviorMethod) {
return function() {
var behavior = sinon.behavior.create(sinon.functionName(this));
behavior.callbackAsync = this.callbackAsync;
behavior[behaviorMethod].apply(behavior, arguments);
this.behaviors.push(behavior);

Expand All @@ -369,23 +386,7 @@
}
}

// create asynchronous versions of callsArg* and yields* methods
for (var method in proto) {
// need to avoid creating anotherasync versions of the newly added async methods
if (proto.hasOwnProperty(method) &&
method.match(/^(callsArg|yields|thenYields$)/) &&
!method.match(/Async/)) {
proto[method + 'Async'] = (function (syncFnName) {
return function () {
this.callbackAsync = true;
return this[syncFnName].apply(this, arguments);
};
})(method);
}
}

return proto;

}()));

if (commonJSModule) {
Expand Down
70 changes: 0 additions & 70 deletions test/sinon/stub_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1052,14 +1052,6 @@ buster.testCase("sinon.stub", {
this.stub = sinon.stub.create();
},

"passes call to callsArg": function () {
var spy = sinon.spy(this.stub, "callsArg");

this.stub.callsArgAsync(2);

assert(spy.calledWith(2));
},

"asynchronously calls argument at specified index": function (done) {
this.stub.callsArgAsync(2);
var callback = sinon.spy(done);
Expand All @@ -1075,15 +1067,6 @@ buster.testCase("sinon.stub", {
this.stub = sinon.stub.create();
},

"passes call to callsArgWith": function () {
var object = {};
sinon.spy(this.stub, "callsArgWith");

this.stub.callsArgWithAsync(1, object);

assert(this.stub.callsArgWith.calledWith(1, object));
},

"asynchronously calls callback at specified index with multiple args": function (done) {
var object = {};
var array = [];
Expand All @@ -1107,14 +1090,6 @@ buster.testCase("sinon.stub", {
};
},

"passes call to callsArgOn": function () {
sinon.spy(this.stub, "callsArgOn");

this.stub.callsArgOnAsync(2, this.fakeContext);

assert(this.stub.callsArgOn.calledWith(2, this.fakeContext));
},

"asynchronously calls argument at specified index with specified context": function (done) {
var context = this.fakeContext;
this.stub.callsArgOnAsync(2, context);
Expand All @@ -1135,15 +1110,6 @@ buster.testCase("sinon.stub", {
this.fakeContext = { foo: "bar" };
},

"passes call to callsArgOnWith": function () {
var object = {};
sinon.spy(this.stub, "callsArgOnWith");

this.stub.callsArgOnWithAsync(1, this.fakeContext, object);

assert(this.stub.callsArgOnWith.calledWith(1, this.fakeContext, object));
},

"asynchronously calls argument at specified index with provided context and args": function (done) {
var object = {};
var context = this.fakeContext;
Expand All @@ -1161,15 +1127,6 @@ buster.testCase("sinon.stub", {
},

"yieldsAsync": {
"passes call to yields": function () {
var stub = sinon.stub();
sinon.spy(stub, "yields");

stub.yieldsAsync();

assert(stub.yields.calledWith());
},

"asynchronously invokes only argument as callback": function (done) {
var stub = sinon.stub().yieldsAsync();

Expand All @@ -1187,15 +1144,6 @@ buster.testCase("sinon.stub", {
this.fakeContext = { foo: "bar" };
},

"passes call to yieldsOn": function () {
var stub = sinon.stub();
sinon.spy(stub, "yieldsOn");

stub.yieldsOnAsync(this.fakeContext);

assert(stub.yieldsOn.calledWith(this.fakeContext));
},

"asynchronously invokes only argument as callback with given context": function (done) {
var context = this.fakeContext;
this.stub.yieldsOnAsync(context);
Expand All @@ -1213,15 +1161,6 @@ buster.testCase("sinon.stub", {
},

"yieldsToAsync": {
"passes call to yieldsTo": function () {
var stub = sinon.stub();
sinon.spy(stub, "yieldsTo");

stub.yieldsToAsync("success");

assert(stub.yieldsTo.calledWith("success"));
},

"asynchronously yields to property of object argument": function (done) {
var stub = sinon.stub().yieldsToAsync("success");

Expand All @@ -1242,15 +1181,6 @@ buster.testCase("sinon.stub", {
this.fakeContext = { foo: "bar" };
},

"passes call to yieldsToOn": function () {
var stub = sinon.stub();
sinon.spy(stub, "yieldsToOn");

stub.yieldsToOnAsync("success", this.fakeContext);

assert(stub.yieldsToOn.calledWith("success", this.fakeContext));
},

"asynchronously yields to property of object argument with given context": function (done) {
var context = this.fakeContext;
this.stub.yieldsToOnAsync("success", context);
Expand Down

0 comments on commit eed5b48

Please sign in to comment.