Skip to content
This repository has been archived by the owner on Nov 4, 2020. It is now read-only.

Commit

Permalink
Added check for zero argument assertions.
Browse files Browse the repository at this point in the history
It is better to throw something instead of silently fail
  • Loading branch information
btd committed Sep 13, 2017
1 parent fe6b136 commit f5c9cc3
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 deletions.
21 changes: 8 additions & 13 deletions lib/assertion.js
Expand Up @@ -116,6 +116,12 @@ Assertion.prototype = {
*/
fail: function() {
return this.assert(false);
},

assertZeroArguments: function(args) {
if (args.length !== 0) {
throw new TypeError("This assertion does not expect any arguments. You may need to check your code");
}
}
};

Expand Down Expand Up @@ -274,22 +280,11 @@ Assertion.addChain = function(name, onCall) {
Assertion.alias = function(from, to) {
var desc = Object.getOwnPropertyDescriptor(Assertion.prototype, from);
if (!desc) {
throw new Error(
"Alias " +
from +
" -> " +
to +
" could not be created as " +
from +
" not defined"
);
throw new Error("Alias " + from + " -> " + to + " could not be created as " + from + " not defined");
}
Object.defineProperty(Assertion.prototype, to, desc);

var desc2 = Object.getOwnPropertyDescriptor(
PromisedAssertion.prototype,
from
);
var desc2 = Object.getOwnPropertyDescriptor(PromisedAssertion.prototype, from);
if (desc2) {
Object.defineProperty(PromisedAssertion.prototype, to, desc2);
}
Expand Down
1 change: 1 addition & 0 deletions lib/ext/bool.js
Expand Up @@ -63,6 +63,7 @@ export default function(should, Assertion) {
* (0).should.not.be.ok();
*/
Assertion.add("ok", function() {
this.assertZeroArguments(arguments);
this.params = { operator: "to be truthy" };

this.assert(this.obj);
Expand Down
2 changes: 2 additions & 0 deletions lib/ext/number.js
Expand Up @@ -17,6 +17,7 @@ export default function(should, Assertion) {
* NaN.should.be.NaN();
*/
Assertion.add("NaN", function() {
this.assertZeroArguments(arguments);
this.params = { operator: "to be NaN" };

this.assert(this.obj !== this.obj);
Expand All @@ -34,6 +35,7 @@ export default function(should, Assertion) {
* NaN.should.not.be.Infinity();
*/
Assertion.add("Infinity", function() {
this.assertZeroArguments(arguments);
this.params = { operator: "to be Infinity" };

this.is.a
Expand Down
3 changes: 3 additions & 0 deletions lib/ext/promise.js
Expand Up @@ -22,6 +22,7 @@ export default function(should, Assertion) {
* (10).should.not.be.a.Promise()
*/
Assertion.add("Promise", function() {
this.assertZeroArguments(arguments);
this.params = { operator: "to be promise" };

var obj = this.obj;
Expand Down Expand Up @@ -51,6 +52,7 @@ export default function(should, Assertion) {
* });
*/
Assertion.prototype.fulfilled = function Assertion$fulfilled() {
this.assertZeroArguments(arguments);
this.params = { operator: "to be fulfilled" };

should(this.obj).be.a.Promise();
Expand Down Expand Up @@ -95,6 +97,7 @@ export default function(should, Assertion) {
* });
*/
Assertion.prototype.rejected = function() {
this.assertZeroArguments(arguments);
this.params = { operator: "to be rejected" };

should(this.obj).be.a.Promise();
Expand Down
22 changes: 16 additions & 6 deletions lib/ext/type.js
Expand Up @@ -15,6 +15,7 @@ export default function(should, Assertion) {
* @category assertion types
*/
Assertion.add("Number", function() {
this.assertZeroArguments(arguments);
this.params = { operator: "to be a number" };

this.have.type("number");
Expand All @@ -28,6 +29,7 @@ export default function(should, Assertion) {
* @category assertion types
*/
Assertion.add("arguments", function() {
this.assertZeroArguments(arguments);
this.params = { operator: "to be arguments" };

this.have.class("Arguments");
Expand Down Expand Up @@ -76,6 +78,7 @@ export default function(should, Assertion) {
* @category assertion types
*/
Assertion.add("Function", function() {
this.assertZeroArguments(arguments);
this.params = { operator: "to be a function" };

this.have.type("function");
Expand All @@ -88,6 +91,7 @@ export default function(should, Assertion) {
* @category assertion types
*/
Assertion.add("Object", function() {
this.assertZeroArguments(arguments);
this.params = { operator: "to be an object" };

this.is.not.null().and.have.type("object");
Expand All @@ -100,6 +104,7 @@ export default function(should, Assertion) {
* @category assertion types
*/
Assertion.add("String", function() {
this.assertZeroArguments(arguments);
this.params = { operator: "to be a string" };

this.have.type("string");
Expand All @@ -112,6 +117,7 @@ export default function(should, Assertion) {
* @category assertion types
*/
Assertion.add("Array", function() {
this.assertZeroArguments(arguments);
this.params = { operator: "to be an array" };

this.have.class("Array");
Expand All @@ -124,6 +130,7 @@ export default function(should, Assertion) {
* @category assertion types
*/
Assertion.add("Boolean", function() {
this.assertZeroArguments(arguments);
this.params = { operator: "to be a boolean" };

this.have.type("boolean");
Expand All @@ -136,6 +143,7 @@ export default function(should, Assertion) {
* @category assertion types
*/
Assertion.add("Error", function() {
this.assertZeroArguments(arguments);
this.params = { operator: "to be an error" };

this.have.instanceOf(Error);
Expand All @@ -148,6 +156,7 @@ export default function(should, Assertion) {
* @category assertion types
*/
Assertion.add("Date", function() {
this.assertZeroArguments(arguments);
this.params = { operator: "to be a date" };

this.have.instanceOf(Date);
Expand All @@ -161,6 +170,7 @@ export default function(should, Assertion) {
* @category assertion types
*/
Assertion.add("null", function() {
this.assertZeroArguments(arguments);
this.params = { operator: "to be null" };

this.assert(this.obj === null);
Expand All @@ -178,9 +188,7 @@ export default function(should, Assertion) {
Assertion.add("class", function(cls) {
this.params = { operator: "to have [[Class]] " + cls };

this.assert(
Object.prototype.toString.call(this.obj) === "[object " + cls + "]"
);
this.assert(Object.prototype.toString.call(this.obj) === "[object " + cls + "]");
});

Assertion.alias("class", "Class");
Expand All @@ -193,6 +201,7 @@ export default function(should, Assertion) {
* @category assertion types
*/
Assertion.add("undefined", function() {
this.assertZeroArguments(arguments);
this.params = { operator: "to be undefined" };

this.assert(this.obj === void 0);
Expand All @@ -208,6 +217,7 @@ export default function(should, Assertion) {
* @category assertion es6
*/
Assertion.add("iterable", function() {
this.assertZeroArguments(arguments);
this.params = { operator: "to be iterable" };

should(this.obj)
Expand All @@ -223,6 +233,7 @@ export default function(should, Assertion) {
* @category assertion es6
*/
Assertion.add("iterator", function() {
this.assertZeroArguments(arguments);
this.params = { operator: "to be iterator" };

should(this.obj)
Expand All @@ -237,10 +248,9 @@ export default function(should, Assertion) {
* @category assertion es6
*/
Assertion.add("generator", function() {
this.assertZeroArguments(arguments);
this.params = { operator: "to be generator" };

should(this.obj).be.iterable.and.iterator.and.it.is.equal(
this.obj[Symbol.iterator]()
);
should(this.obj).be.iterable.and.iterator.and.it.is.equal(this.obj[Symbol.iterator]());
});
}

0 comments on commit f5c9cc3

Please sign in to comment.