Skip to content

Commit

Permalink
Remove multi-type support for a/an assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
astorije committed Sep 12, 2015
1 parent 675b51b commit 1f8c651
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 65 deletions.
30 changes: 9 additions & 21 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ module.exports = function (chai, _) {
});

/**
* ### .a(types)
* ### .a(type)
*
* The `a` and `an` assertions are aliases that can be
* used either as language chains or to assert a value's
Expand All @@ -146,35 +146,23 @@ module.exports = function (chai, _) {
* // language chain
* expect(foo).to.be.an.instanceof(Foo);
*
* When an array of strings is passed as argument of the assertion, the
* value's type must be one of them.
*
* expect(myScalar).to.be.a(['string', 'number', 'boolean']);
* expect([]).to.not.be.a(['string', 'number', 'boolean']);
*
* @name a
* @alias an
* @param {String|Array} types
* @param {String} type
* @param {String} message _optional_
* @api public
*/

function an (types, msg) {
function an (type, msg) {
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');

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(', ');
type = type.toLowerCase();
var obj = flag(this, 'object')
, article = ~[ 'a', 'e', 'i', 'o', 'u' ].indexOf(type.charAt(0)) ? 'an ' : 'a ';

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

Expand Down
22 changes: 0 additions & 22 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,28 +119,6 @@ describe('expect', function () {
err(function(){
expect(5).to.not.be.a('number', 'blah');
}, "blah: expected 5 not to be a number");

expect('foo').to.be.a(['string', 'number', 'boolean']);
expect(42).to.be.a(['string', 'number', 'boolean']);
expect(true).to.be.a(['string', 'number', 'boolean']);

err(function(){
expect([]).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, a number, or a boolean');

err(function(){
expect(42).to.not.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, a number, or a boolean');
});

it('instanceof', function(){
Expand Down
22 changes: 0 additions & 22 deletions test/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,28 +151,6 @@ describe('should', function() {
err(function(){
(5).should.not.be.a('number');
}, "expected 5 not to be a number");

'foo'.should.be.a(['string', 'number', 'boolean']);
(42).should.be.a(['string', 'number', 'boolean']);
(true).should.be.a(['string', 'number', 'boolean']);

err(function(){
[].should.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, a number, or a boolean');

err(function(){
(42).should.not.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, a number, or a boolean');
});

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

0 comments on commit 1f8c651

Please sign in to comment.