Skip to content

Commit

Permalink
Make addType require types to have a name
Browse files Browse the repository at this point in the history
  • Loading branch information
sunesimonsen committed Aug 18, 2014
1 parent 5f91868 commit 21df785
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/Unexpected.js
Expand Up @@ -174,6 +174,10 @@ var customTypePrototype = {

Unexpected.prototype.addType = function (type) {
var baseType;
if (typeof type.name !== 'string' || trim(type.name) === '') {
throw new Error('A custom type must be given a non-empty name');
}

if (type.base) {
baseType = utils.findFirst(this.types, function (t) {
return t.name === type.base;
Expand Down
7 changes: 7 additions & 0 deletions test/unexpected.spec.js
Expand Up @@ -1754,6 +1754,7 @@ describe('unexpected', function () {
beforeEach(function () {
clonedExpect = expect.clone();
clonedExpect.addType({
name: 'box',
identify: function (obj) {
return obj && typeof obj === 'object' && obj.isBox;
},
Expand All @@ -1774,6 +1775,12 @@ describe('unexpected', function () {
});
});

it('throws an expection is the type has an empty or undefined name', function () {
expect(function () {
clonedExpect.addType({});
}, 'to throw', 'A custom type must be given a non-empty name');
});

it('should use the equal defined by the type', function () {
clonedExpect(box(123), 'to equal', box(123));
clonedExpect(box(123), 'not to equal', box(321));
Expand Down

0 comments on commit 21df785

Please sign in to comment.