Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made callsArg, returnsArg, and throwsArg more strict #1848

Merged
merged 4 commits into from
Jul 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 25 additions & 15 deletions lib/sinon/behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,24 @@ function getCallbackError(behavior, func, args) {
return "argument at index " + behavior.callArgAt + " is not a function: " + func;
}

function ensureArgs(name, behavior, args) {
// map function name to internal property
// callsArg => callArgAt
var property = name.replace(/sArg/, "ArgAt");
var index = behavior[property];

if (index >= args.length) {
throw new TypeError(
name + " failed: " + (index + 1)
+ " arguments required but only " + args.length
+ " present"
);
}
}

function callCallback(behavior, args) {
if (typeof behavior.callArgAt === "number") {
ensureArgs("callsArg", behavior, args);
var func = getCallback(behavior, args);

if (typeof func !== "function") {
Expand Down Expand Up @@ -115,6 +131,12 @@ var proto = {
},

invoke: function invoke(context, args) {
/*
* callCallback (conditionally) calls ensureArgs
*
* Note: callCallback intentionally happens before
* everything else and cannot be moved lower
*/
var returnValue = callCallback(this, args);

if (this.exception) {
Expand All @@ -124,29 +146,17 @@ var proto = {
this.exceptionCreator = undefined;
throw this.exception;
} else if (typeof this.returnArgAt === "number") {
ensureArgs("returnsArg", this, args);
return args[this.returnArgAt];
} else if (this.returnThis) {
return context;
} else if (typeof this.throwArgAt === "number") {
if (args.length < this.throwArgAt) {
throw new TypeError(
"throwArgs failed: " + this.throwArgAt
+ " arguments required but only " + args.length
+ " present"
);
}
ensureArgs("throwsArg", this, args);
throw args[this.throwArgAt];
} else if (this.fakeFn) {
return this.fakeFn.apply(context, args);
} else if (typeof this.resolveArgAt === "number") {
var len = this.resolveArgAt + 1;
if (args.length < len) {
throw new TypeError(
"resolvesArg failed: " + len
+ " arguments required but only " + args.length
+ " present"
);
}
ensureArgs("resolvesArg", this, args);
return (this.promiseLibrary || Promise).resolve(args[this.resolveArgAt]);
} else if (this.resolveThis) {
return (this.promiseLibrary || Promise).resolve(context);
Expand Down
39 changes: 33 additions & 6 deletions test/stub-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,21 @@ describe("stub", function () {
stub.returnsArg();
}, {name: "TypeError"});
});

it("should throw without enough arguments", function () {
var stub = createStub.create();
stub.returnsArg(3);

assert.exception(
function () {
stub("only", "two arguments");
},
{
name: "TypeError",
message: "returnsArg failed: 4 arguments required but only 2 present"
}
);
});
});

describe(".throwsArg", function () {
Expand Down Expand Up @@ -482,14 +497,11 @@ describe("stub", function () {
function () {
stub("only", "two arguments");
},
function (error) {
return error instanceof TypeError
&& error.message ===
"throwArgs failed: 3 arguments required but only 2 present"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of note: This message changed: throwArgs (typo?) became throwsArg.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, seems like a typo. There is throwArg implemented on calls, but singular. throwArgs doesn't even make sense 😆

;
{
name: "TypeError",
message: "throwsArg failed: 4 arguments required but only 2 present"
}
);

});

it("should work with call-based behavior", function () {
Expand Down Expand Up @@ -793,6 +805,21 @@ describe("stub", function () {
}, {name: "TypeError"});
});

it("should throw without enough arguments", function () {
var stub = createStub.create();
stub.callsArg(3);

assert.exception(
function () {
stub("only", "two arguments");
},
{
name: "TypeError",
message: "callsArg failed: 4 arguments required but only 2 present"
}
);
});

it("returns result of invocant", function () {
var stub = this.stub.callsArg(0);
var callback = createStub().returns("return value");
Expand Down