Skip to content

Commit

Permalink
Improve message of multi-type a/an assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
astorije committed Sep 12, 2015
1 parent 5eb3906 commit d27ceb5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
13 changes: 9 additions & 4 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,18 @@ module.exports = function (chai, _) {
if (msg) flag(this, 'message', msg);
if (_.type(types) === 'string') types = [types];
types = types.map(function (t) { return t.toLowerCase(); });
var obj = flag(this, 'object')
, article = ~[ 'a', 'e', 'i', 'o', 'u' ].indexOf(types[0].charAt(0)) ? 'an ' : 'a ';
var obj = flag(this, 'object');

var str = types.map(function (t, index) {
var art = ~[ 'a', 'e', 'i', 'o', 'u' ].indexOf(t.charAt(0)) ? 'an' : 'a';
var or = types.length > 1 && index === types.length - 1 ? 'or ' : '';
return or + art + ' ' + t;
}).join(', ');

this.assert(
types.some(function (expected) { return _.type(obj) === expected; })
, 'expected #{this} to be ' + article + types.join(', ')
, 'expected #{this} not to be ' + article + types.join(', ')
, 'expected #{this} to be ' + str
, 'expected #{this} not to be ' + str
);
}

Expand Down
8 changes: 4 additions & 4 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,21 @@ describe('expect', function () {

err(function(){
expect([]).to.be.a(['string', 'number', 'boolean']);
}, 'expected [] to be a string, number, boolean');
}, 'expected [] to be a string, a number, or a boolean');

expect([]).to.not.be.a(['string', 'number', 'boolean']);

err(function(){
expect('foo').to.not.be.a(['string', 'number', 'boolean']);
}, 'expected \'foo\' not to be a string, number, boolean');
}, 'expected \'foo\' not to be a string, a number, or a boolean');

err(function(){
expect(42).to.not.be.a(['string', 'number', 'boolean']);
}, 'expected 42 not to be a string, number, boolean');
}, 'expected 42 not to be a string, a number, or a boolean');

err(function(){
expect(true).to.not.be.a(['string', 'number', 'boolean']);
}, 'expected true not to be a string, number, boolean');
}, 'expected true not to be a string, a number, or a boolean');
});

it('instanceof', function(){
Expand Down
8 changes: 4 additions & 4 deletions test/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,21 +158,21 @@ describe('should', function() {

err(function(){
[].should.be.a(['string', 'number', 'boolean']);
}, 'expected [] to be a string, number, boolean');
}, 'expected [] to be a string, a number, or a boolean');

[].should.not.be.a(['string', 'number', 'boolean']);

err(function(){
'foo'.should.not.be.a(['string', 'number', 'boolean']);
}, 'expected \'foo\' not to be a string, number, boolean');
}, 'expected \'foo\' not to be a string, a number, or a boolean');

err(function(){
(42).should.not.be.a(['string', 'number', 'boolean']);
}, 'expected 42 not to be a string, number, boolean');
}, 'expected 42 not to be a string, a number, or a boolean');

err(function(){
(true).should.not.be.a(['string', 'number', 'boolean']);
}, 'expected true not to be a string, number, boolean');
}, 'expected true not to be a string, a number, or a boolean');
});

it('instanceof', function(){
Expand Down

0 comments on commit d27ceb5

Please sign in to comment.