Skip to content

Commit

Permalink
Use addChainableMethod to get away from __proto__ manipulation.
Browse files Browse the repository at this point in the history
Helps with #66.
  • Loading branch information
domenic committed May 27, 2012
1 parent 3a67f05 commit e1256e9
Showing 1 changed file with 26 additions and 52 deletions.
78 changes: 26 additions & 52 deletions lib/assertion.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,37 +216,23 @@ Object.defineProperty(Assertion.prototype, 'deep',
* @api public
*/

var an = function () {
var assert = function(type) {
var obj = flag(this, 'object')
, klassStart = type.charAt(0).toUpperCase()
, klass = klassStart + type.slice(1)
, article = ~[ 'A', 'E', 'I', 'O', 'U' ].indexOf(klassStart) ? 'an ' : 'a ';

this.assert(
'[object ' + klass + ']' === toString.call(obj)
, 'expected #{this} to be ' + article + type
, 'expected #{this} not to be ' + article + type
, '[object ' + klass + ']'
, toString.call(obj)
);

return this;
};

assert.__proto__ = this;
return assert;
};
function an(type) {
var obj = flag(this, 'object')
, klassStart = type.charAt(0).toUpperCase()
, klass = klassStart + type.slice(1)
, article = ~[ 'A', 'E', 'I', 'O', 'U' ].indexOf(klassStart) ? 'an ' : 'a ';

Object.defineProperty(Assertion.prototype, 'an',
{ get: an
, configurable: true
});
this.assert(
'[object ' + klass + ']' === toString.call(obj)
, 'expected #{this} to be ' + article + type
, 'expected #{this} not to be ' + article + type
, '[object ' + klass + ']'
, toString.call(obj)
);
}

Object.defineProperty(Assertion.prototype, 'a',
{ get: an
, configurable: true
});
Assertion.addChainableMethod('an', an);
Assertion.addChainableMethod('a', an);

/**
* ### .include(value)
Expand All @@ -266,32 +252,20 @@ Object.defineProperty(Assertion.prototype, 'a',
* @api public
*/

var include = function () {
function includeChainingBehavior() {
flag(this, 'contains', true);
}

var assert = function(val) {
var obj = flag(this, 'object')
this.assert(
~obj.indexOf(val)
, 'expected #{this} to include ' + util.inspect(val)
, 'expected #{this} to not include ' + util.inspect(val));

return this;
};

assert.__proto__ = this;
return assert;
};

Object.defineProperty(Assertion.prototype, 'contain',
{ get: include
, configurable: true
});
function include(val) {
var obj = flag(this, 'object')
this.assert(
~obj.indexOf(val)
, 'expected #{this} to include ' + util.inspect(val)
, 'expected #{this} to not include ' + util.inspect(val));
}

Object.defineProperty(Assertion.prototype, 'include',
{ get: include
, configurable: true
});
Assertion.addChainableMethod('include', include, includeChainingBehavior);
Assertion.addChainableMethod('contain', include, includeChainingBehavior);

/**
* ### .ok
Expand Down

2 comments on commit e1256e9

@jfirebaugh
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chai-jquery should use this too, but I think it needs overwriteChainableMethod.

@logicalparadox
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

master branch has all of the browser differences worked out of addChainableMethod to it should function as a reliable template.

Please sign in to comment.