Skip to content

Commit

Permalink
Merge branch 'master' into empty-object
Browse files Browse the repository at this point in the history
* master:
  don't switch act/exp
  Update the compiled verison.
  Add `assert.operator`.
  Notes on messages. #22
  browser build
  have been test
  below tests
  browser build
  remove unnecessary fail export
  full support for actual/expected where relevant
  Assertion.assert support expected value
  clean up error
  • Loading branch information
vesln committed Mar 7, 2012
2 parents 15f67ba + df1a82d commit f1e6a9a
Show file tree
Hide file tree
Showing 8 changed files with 288 additions and 156 deletions.
185 changes: 108 additions & 77 deletions chai.js

Large diffs are not rendered by default.

129 changes: 80 additions & 49 deletions lib/assertion.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function Assertion (obj, msg, stack) {

/*!
* ## Assertion.includeStack
* , toString = Object.prototype.toString
* , toString = Object.prototype.toString
*
* User configurable property, influences whether stack trace
* is included in Assertion error message. Default of false
Expand All @@ -98,15 +98,17 @@ Assertion.includeStack = false;
* @api private
*/

Assertion.prototype.assert = function (expr, msg, negateMsg) {
Assertion.prototype.assert = function (expr, msg, negateMsg, expected, actual) {
actual = actual || this.obj;
var msg = (this.negate ? negateMsg : msg)
, ok = this.negate ? !expr : expr;

if (!ok) {
throw new AssertionError({
operator: this.msg,
message: msg,
stackStartFunction: (Assertion.includeStack) ? this.assert : this.ssfi
message: this.msg ? this.msg + ': ' + msg : msg // include custom message if available
, actual: actual
, expected: expected
, stackStartFunction: (Assertion.includeStack) ? this.assert : this.ssfi
});
}
};
Expand All @@ -123,8 +125,8 @@ Assertion.prototype.assert = function (expr, msg, negateMsg) {
Object.defineProperty(Assertion.prototype, 'inspect',
{ get: function () {
return inspect(this.obj);
},
configurable: true
}
, configurable: true
});

/**
Expand All @@ -139,8 +141,8 @@ Object.defineProperty(Assertion.prototype, 'inspect',
Object.defineProperty(Assertion.prototype, 'to',
{ get: function () {
return this;
},
configurable: true
}
, configurable: true
});

/**
Expand All @@ -155,8 +157,8 @@ Object.defineProperty(Assertion.prototype, 'to',
Object.defineProperty(Assertion.prototype, 'be',
{ get: function () {
return this;
},
configurable: true
}
, configurable: true
});

/**
Expand All @@ -173,8 +175,8 @@ Object.defineProperty(Assertion.prototype, 'been',
{ get: function () {
this.tense = 'past';
return this;
},
configurable: true
}
, configurable: true
});

/**
Expand All @@ -189,8 +191,8 @@ Object.defineProperty(Assertion.prototype, 'been',
Object.defineProperty(Assertion.prototype, 'an',
{ get: function () {
return this;
},
configurable: true
}
, configurable: true
});
/**
* # is
Expand All @@ -204,8 +206,8 @@ Object.defineProperty(Assertion.prototype, 'an',
Object.defineProperty(Assertion.prototype, 'is',
{ get: function () {
return this;
},
configurable: true
}
, configurable: true
});

/**
Expand All @@ -220,8 +222,8 @@ Object.defineProperty(Assertion.prototype, 'is',
Object.defineProperty(Assertion.prototype, 'and',
{ get: function () {
return this;
},
configurable: true
}
, configurable: true
});

/**
Expand All @@ -236,8 +238,8 @@ Object.defineProperty(Assertion.prototype, 'and',
Object.defineProperty(Assertion.prototype, 'have',
{ get: function () {
return this;
},
configurable: true
}
, configurable: true
});

/**
Expand All @@ -252,8 +254,8 @@ Object.defineProperty(Assertion.prototype, 'have',
Object.defineProperty(Assertion.prototype, 'with',
{ get: function () {
return this;
},
configurable: true
}
, configurable: true
});

/**
Expand All @@ -269,8 +271,8 @@ Object.defineProperty(Assertion.prototype, 'not',
{ get: function () {
this.negate = true;
return this;
},
configurable: true
}
, configurable: true
});

/**
Expand All @@ -295,8 +297,8 @@ Object.defineProperty(Assertion.prototype, 'ok',
, 'expected ' + this.inspect + ' to be falsy');

return this;
},
configurable: true
}
, configurable: true
});

/**
Expand All @@ -313,11 +315,13 @@ Object.defineProperty(Assertion.prototype, 'true',
this.assert(
true === this.obj
, 'expected ' + this.inspect + ' to be true'
, 'expected ' + this.inspect + ' to be false');
, 'expected ' + this.inspect + ' to be false'
, this.negate ? false : true
);

return this;
},
configurable: true
}
, configurable: true
});

/**
Expand All @@ -334,11 +338,13 @@ Object.defineProperty(Assertion.prototype, 'false',
this.assert(
false === this.obj
, 'expected ' + this.inspect + ' to be false'
, 'expected ' + this.inspect + ' to be true');
, 'expected ' + this.inspect + ' to be true'
, this.negate ? true : false
);

return this;
},
configurable: true
}
, configurable: true
});

/**
Expand All @@ -360,11 +366,12 @@ Object.defineProperty(Assertion.prototype, 'exist',
this.assert(
null != this.obj
, 'expected ' + this.inspect + ' to exist'
, 'expected ' + this.inspect + ' to not exist');
, 'expected ' + this.inspect + ' to not exist'
);

return this;
},
configurable: true
}
, configurable: true
});

/**
Expand Down Expand Up @@ -394,8 +401,8 @@ Object.defineProperty(Assertion.prototype, 'empty',
, 'expected ' + this.inspect + ' not to be empty');

return this;
},
configurable: true
}
, configurable: true
});

/**
Expand All @@ -416,11 +423,14 @@ Object.defineProperty(Assertion.prototype, 'arguments',
this.assert(
'[object Arguments]' == Object.prototype.toString.call(this.obj)
, 'expected ' + this.inspect + ' to be arguments'
, 'expected ' + this.inspect + ' to not be arguments');
, 'expected ' + this.inspect + ' to not be arguments'
, '[object Arguments]'
, Object.prototype.toString.call(this.obj)
);

return this;
},
configurable: true
}
, configurable: true
});

/**
Expand All @@ -439,7 +449,8 @@ Assertion.prototype.equal = function (val) {
this.assert(
val === this.obj
, 'expected ' + this.inspect + ' to equal ' + inspect(val)
, 'expected ' + this.inspect + ' to not equal ' + inspect(val));
, 'expected ' + this.inspect + ' to not equal ' + inspect(val)
, val );

return this;
};
Expand All @@ -460,7 +471,9 @@ Assertion.prototype.eql = function (obj) {
this.assert(
eql(obj, this.obj)
, 'expected ' + this.inspect + ' to equal ' + inspect(obj)
, 'expected ' + this.inspect + ' to not equal ' + inspect(obj));
, 'expected ' + this.inspect + ' to not equal ' + inspect(obj)
, obj );

return this;
};

Expand Down Expand Up @@ -548,7 +561,10 @@ Assertion.prototype.a = function (type) {
this.assert(
'[object ' + klass + ']' === toString.call(this.obj)
, 'expected ' + this.inspect + ' to be a ' + type
, 'expected ' + this.inspect + ' not to be a ' + type);
, 'expected ' + this.inspect + ' not to be a ' + type
, '[object ' + klass + ']'
, toString.call(this.obj)
);

return this;
};
Expand Down Expand Up @@ -613,7 +629,10 @@ Assertion.prototype.property = function (name, val) {
val === this.obj[name]
, 'expected ' + this.inspect + ' to have a property ' + inspect(name) + ' of ' +
inspect(val) + ', but got ' + inspect(this.obj[name])
, 'expected ' + this.inspect + ' to not have a property ' + inspect(name) + ' of ' + inspect(val));
, 'expected ' + this.inspect + ' to not have a property ' + inspect(name) + ' of ' + inspect(val)
, val
, this.obj[val]
);
}

this.obj = this.obj[name];
Expand Down Expand Up @@ -662,7 +681,10 @@ Assertion.prototype.length = function (n) {
this.assert(
len == n
, 'expected ' + this.inspect + ' to have a length of ' + n + ' but got ' + len
, 'expected ' + this.inspect + ' to not have a length of ' + len);
, 'expected ' + this.inspect + ' to not have a length of ' + len
, n
, len
);

return this;
};
Expand Down Expand Up @@ -809,7 +831,10 @@ Assertion.prototype.keys = function(keys) {
this.assert(
ok
, 'expected ' + this.inspect + ' to ' + str
, 'expected ' + this.inspect + ' to not ' + str);
, 'expected ' + this.inspect + ' to not ' + str
, keys
, Object.keys(this.obj)
);

return this;
}
Expand Down Expand Up @@ -886,7 +911,10 @@ Assertion.prototype.respondTo = function (method) {
this.assert(
'function' === typeof context
, 'expected ' + this.inspect + ' to respond to ' + inspect(method)
, 'expected ' + this.inspect + ' to not respond to ' + inspect(method));
, 'expected ' + this.inspect + ' to not respond to ' + inspect(method)
, 'function'
, typeof context
);

return this;
};
Expand All @@ -907,7 +935,10 @@ Assertion.prototype.satisfy = function (matcher) {
this.assert(
matcher(this.obj)
, 'expected ' + this.inspect + ' to satisfy ' + inspect(matcher)
, 'expected ' + this.inspect + ' to not satisfy' + inspect(matcher));
, 'expected ' + this.inspect + ' to not satisfy' + inspect(matcher)
, this.negate ? false : true
, matcher(this.obj)
);

return this;
};
Expand Down
10 changes: 0 additions & 10 deletions lib/chai.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,6 @@ exports.use = function (fn) {
return this;
};

exports.fail = function (actual, expected, message, operator, stackStartFunction) {
throw new exports.AssertionError({
message: message,
actual: actual,
expected: expected,
operator: operator,
stackStartFunction: stackStartFunction
});
};

var expect = require('./interface/expect');
exports.use(expect);

Expand Down
20 changes: 2 additions & 18 deletions lib/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,6 @@ function AssertionError (options) {

AssertionError.prototype.__proto__ = Error.prototype;

AssertionError.prototype.summary = function() {
var str = '';

if (this.operator) {
str += 'In: \'' + this.operator + '\'\n\t';
}

str += '' + this.name + (this.message ? ': ' + this.message : '');

return str;
};

AssertionError.prototype.details = function() {
return this.summary();
};

AssertionError.prototype.toString = function() {
return this.summary();
};
return this.message;
};
23 changes: 23 additions & 0 deletions lib/interface/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,29 @@ module.exports = function (chai) {
new Assertion(fn, msg).to.not.throw(type);
};

/**
* # .operator(val, operator, val2, [message])
*
* Compare two values using operator.
*
* assert.operator(1, '<', 2, 'everything is ok');
* assert.operator(1, '>', 2, 'this will fail');
*
* @name operator
* @param {*} object to test
* @param {String} operator
* @param {*} second object
* @param {String} message
* @api public
*/

assert.operator = function (val, operator, val2, msg) {
if (!~['==', '===', '>', '>=', '<', '<=', '!=', '!=='].indexOf(operator)) {
throw new Error('Invalid operator "' + operator + '"');
}
new Assertion(eval(val + operator + val2), msg).to.be.true;
};

/*!
* Undocumented / untested
*/
Expand Down
Loading

0 comments on commit f1e6a9a

Please sign in to comment.