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

Commit

Permalink
Merge 923382f into 7bfd8b4
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeChampion committed Nov 27, 2018
2 parents 7bfd8b4 + 923382f commit fa3e42e
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 @@ -320,6 +320,11 @@ Assert that `actual > expected`.
Assert that `actual >= expected`.


### 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 @@ -300,6 +300,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);
}
};


// Aliases
// -------
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 @@ -1114,6 +1114,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'));
});

});

});

}());

0 comments on commit fa3e42e

Please sign in to comment.