Skip to content
This repository has been archived by the owner on Mar 25, 2022. It is now read-only.

Commit

Permalink
Add helper method for testing function is named correctly (#56)
Browse files Browse the repository at this point in the history
* Add helper method for testing function is named correctly

* fix linting issues

* ignore line as it is used only in really old js engines

* Update proclaim.js
  • Loading branch information
JakeChampion authored Nov 28, 2018
1 parent 8ea4bef commit 472915f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,11 @@ Assert that `obj[property]` is not enumerable.

Assert that `obj[property]` is enumerable.

### proclaim.hasName( fn, expected, [message] )

Assert that `fn.name === expected`.


Why?
----

Expand Down
10 changes: 10 additions & 0 deletions lib/proclaim.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,16 @@
}
};

var functionsHaveNames = (function foo() { }).name === 'foo';
proclaim.hasName = function(fn, expected, msg) {
if (functionsHaveNames) {
proclaim.strictEqual(fn.name, expected, msg);
} else {
/* istanbul ignore next */
proclaim.equal(Function.prototype.toString.call(fn).match(/function\s*([^\s]*)\s*\(/)[1], expected, msg);
}
};

// Assert that a function has the expected number of arguments
proclaim.arity = function(fn, expected, msg) {
proclaim.isFunction(fn, msg);
Expand Down
25 changes: 25 additions & 0 deletions test/unit/lib/proclaim.js
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,31 @@

});

describe('.hasName()', function() {

it('should be a function', function() {
assert.isFunction(proclaim.hasName);
});

it('should not throw when called with a function that has the same name as the expected value', function() {
assert.doesNotThrow(callFn(proclaim.hasName, function foo() {}, 'foo'));
});

it('should throw when called with a value that is not a function', function() {
assert.throws(callFn(proclaim.hasName, 1, 'foo'));
assert.throws(callFn(proclaim.hasName, {}, 'foo'));
assert.throws(callFn(proclaim.hasName, false, 'foo'));
assert.throws(callFn(proclaim.hasName, [], 'foo'));
assert.throws(callFn(proclaim.hasName, /./, 'foo'));
});

it('should throw when called with a value that is a function that has a different name than the expected value', function() {
assert.throws(callFn(proclaim.hasName, function() {}, 'foo'));
assert.throws(callFn(proclaim.hasName, function bar() {}, 'foo'));
});

});

describe('.arity()', function() {

it('should be a function', function() {
Expand Down

0 comments on commit 472915f

Please sign in to comment.