assert.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Should
  3. * Copyright(c) 2010-2014 TJ Holowaychuk <tj@vision-media.ca>
  4. * MIT Licensed
  5. */
  6. var util = require('../util')
  7. , assert = require('./_assert')
  8. , AssertionError = require('../assertion-error');
  9. module.exports = function(should) {
  10. var i = should.format;
  11. /*
  12. * Expose assert to should
  13. *
  14. * This allows you to do things like below
  15. * without require()ing the assert module.
  16. *
  17. * should.equal(foo.bar, undefined);
  18. *
  19. */
  20. util.merge(should, assert);
  21. /**
  22. * Assert _obj_ exists, with optional message.
  23. *
  24. * @static
  25. * @memberOf should
  26. * @category assertion assert
  27. * @alias should.exists
  28. * @param {*} obj
  29. * @param {String} [msg]
  30. * @example
  31. *
  32. * should.exist(1);
  33. * should.exist(new Date());
  34. */
  35. should.exist = should.exists = function(obj, msg) {
  36. if(null == obj) {
  37. throw new AssertionError({
  38. message: msg || ('expected ' + i(obj) + ' to exist'), stackStartFunction: should.exist
  39. });
  40. }
  41. };
  42. should.not = {};
  43. /**
  44. * Asserts _obj_ does not exist, with optional message.
  45. *
  46. * @name not.exist
  47. * @static
  48. * @memberOf should
  49. * @category assertion assert
  50. * @alias should.not.exists
  51. * @param {*} obj
  52. * @param {String} [msg]
  53. * @example
  54. *
  55. * should.not.exist(null);
  56. * should.not.exist(void 0);
  57. */
  58. should.not.exist = should.not.exists = function(obj, msg) {
  59. if(null != obj) {
  60. throw new AssertionError({
  61. message: msg || ('expected ' + i(obj) + ' to not exist'), stackStartFunction: should.not.exist
  62. });
  63. }
  64. };
  65. };