assertion.js 3.7 KB

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