assertion.js (3794B)
1 /*! 2 * chai 3 * http://chaijs.com 4 * Copyright(c) 2011-2014 Jake Luer <jake@alogicalparadox.com> 5 * MIT Licensed 6 */ 7 8 var config = require('./config'); 9 10 module.exports = function (_chai, util) { 11 /*! 12 * Module dependencies. 13 */ 14 15 var AssertionError = _chai.AssertionError 16 , flag = util.flag; 17 18 /*! 19 * Module export. 20 */ 21 22 _chai.Assertion = Assertion; 23 24 /*! 25 * Assertion Constructor 26 * 27 * Creates object for chaining. 28 * 29 * @api private 30 */ 31 32 function Assertion (obj, msg, stack) { 33 flag(this, 'ssfi', stack || arguments.callee); 34 flag(this, 'object', obj); 35 flag(this, 'message', msg); 36 } 37 38 Object.defineProperty(Assertion, 'includeStack', { 39 get: function() { 40 console.warn('Assertion.includeStack is deprecated, use chai.config.includeStack instead.'); 41 return config.includeStack; 42 }, 43 set: function(value) { 44 console.warn('Assertion.includeStack is deprecated, use chai.config.includeStack instead.'); 45 config.includeStack = value; 46 } 47 }); 48 49 Object.defineProperty(Assertion, 'showDiff', { 50 get: function() { 51 console.warn('Assertion.showDiff is deprecated, use chai.config.showDiff instead.'); 52 return config.showDiff; 53 }, 54 set: function(value) { 55 console.warn('Assertion.showDiff is deprecated, use chai.config.showDiff instead.'); 56 config.showDiff = value; 57 } 58 }); 59 60 Assertion.addProperty = function (name, fn) { 61 util.addProperty(this.prototype, name, fn); 62 }; 63 64 Assertion.addMethod = function (name, fn) { 65 util.addMethod(this.prototype, name, fn); 66 }; 67 68 Assertion.addChainableMethod = function (name, fn, chainingBehavior) { 69 util.addChainableMethod(this.prototype, name, fn, chainingBehavior); 70 }; 71 72 Assertion.overwriteProperty = function (name, fn) { 73 util.overwriteProperty(this.prototype, name, fn); 74 }; 75 76 Assertion.overwriteMethod = function (name, fn) { 77 util.overwriteMethod(this.prototype, name, fn); 78 }; 79 80 Assertion.overwriteChainableMethod = function (name, fn, chainingBehavior) { 81 util.overwriteChainableMethod(this.prototype, name, fn, chainingBehavior); 82 }; 83 84 /** 85 * ### .assert(expression, message, negateMessage, expected, actual, showDiff) 86 * 87 * Executes an expression and check expectations. Throws AssertionError for reporting if test doesn't pass. 88 * 89 * @name assert 90 * @param {Philosophical} expression to be tested 91 * @param {String|Function} message or function that returns message to display if expression fails 92 * @param {String|Function} negatedMessage or function that returns negatedMessage to display if negated expression fails 93 * @param {Mixed} expected value (remember to check for negation) 94 * @param {Mixed} actual (optional) will default to `this.obj` 95 * @param {Boolean} showDiff (optional) when set to `true`, assert will display a diff in addition to the message if expression fails 96 * @api private 97 */ 98 99 Assertion.prototype.assert = function (expr, msg, negateMsg, expected, _actual, showDiff) { 100 var ok = util.test(this, arguments); 101 if (true !== showDiff) showDiff = false; 102 if (true !== config.showDiff) showDiff = false; 103 104 if (!ok) { 105 var msg = util.getMessage(this, arguments) 106 , actual = util.getActual(this, arguments); 107 throw new AssertionError(msg, { 108 actual: actual 109 , expected: expected 110 , showDiff: showDiff 111 }, (config.includeStack) ? this.assert : flag(this, 'ssfi')); 112 } 113 }; 114 115 /*! 116 * ### ._obj 117 * 118 * Quick reference to stored `actual` value for plugin developers. 119 * 120 * @api private 121 */ 122 123 Object.defineProperty(Assertion.prototype, '_obj', 124 { get: function () { 125 return flag(this, 'object'); 126 } 127 , set: function (val) { 128 flag(this, 'object', val); 129 } 130 }); 131 };