_assert.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // implement assert interface using already written peaces of should.js
  2. // http://wiki.commonjs.org/wiki/Unit_Testing/1.0
  3. //
  4. // THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8!
  5. //
  6. // Originally from narwhal.js (http://narwhaljs.org)
  7. // Copyright (c) 2009 Thomas Robinson <280north.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the 'Software'), to
  11. // deal in the Software without restriction, including without limitation the
  12. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  13. // sell copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  23. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. // when used in node, this will actually load the util module we depend on
  26. // versus loading the builtin util module as happens otherwise
  27. // this is a bug in node module loading as far as I am concerned
  28. var Assertion = require('./../assertion');
  29. var _deepEqual = require('should-equal');
  30. var pSlice = Array.prototype.slice;
  31. // 1. The assert module provides functions that throw
  32. // AssertionError's when particular conditions are not met. The
  33. // assert module must conform to the following interface.
  34. var assert = module.exports = ok;
  35. // 3. All of the following functions must throw an AssertionError
  36. // when a corresponding condition is not met, with a message that
  37. // may be undefined if not provided. All assertion methods provide
  38. // both the actual and expected values to the assertion error for
  39. // display purposes.
  40. /**
  41. * Node.js standard [`assert.fail`](http://nodejs.org/api/assert.html#assert_assert_fail_actual_expected_message_operator).
  42. * @static
  43. * @memberOf should
  44. * @category assertion assert
  45. * @param {*} actual Actual object
  46. * @param {*} expected Expected object
  47. * @param {string} message Message for assertion
  48. * @param {string} operator Operator text
  49. */
  50. function fail(actual, expected, message, operator, stackStartFunction) {
  51. var a = new Assertion(actual);
  52. a.params = {
  53. operator: operator,
  54. expected: expected,
  55. message: message,
  56. stackStartFunction: stackStartFunction || fail
  57. };
  58. a.fail();
  59. }
  60. // EXTENSION! allows for well behaved errors defined elsewhere.
  61. assert.fail = fail;
  62. // 4. Pure assertion tests whether a value is truthy, as determined
  63. // by !!guard.
  64. // assert.ok(guard, message_opt);
  65. // This statement is equivalent to assert.equal(true, !!guard,
  66. // message_opt);. To test strictly for the value true, use
  67. // assert.strictEqual(true, guard, message_opt);.
  68. /**
  69. * Node.js standard [`assert.ok`](http://nodejs.org/api/assert.html#assert_assert_value_message_assert_ok_value_message).
  70. * @static
  71. * @memberOf should
  72. * @category assertion assert
  73. * @param {*} value
  74. * @param {string} [message]
  75. */
  76. function ok(value, message) {
  77. if(!value) fail(value, true, message, '==', assert.ok);
  78. }
  79. assert.ok = ok;
  80. // 5. The equality assertion tests shallow, coercive equality with
  81. // ==.
  82. // assert.equal(actual, expected, message_opt);
  83. /**
  84. * Node.js standard [`assert.equal`](http://nodejs.org/api/assert.html#assert_assert_equal_actual_expected_message).
  85. * @static
  86. * @memberOf should
  87. * @category assertion assert
  88. * @param {*} actual
  89. * @param {*} expected
  90. * @param {string} [message]
  91. */
  92. assert.equal = function equal(actual, expected, message) {
  93. if(actual != expected) fail(actual, expected, message, '==', assert.equal);
  94. };
  95. // 6. The non-equality assertion tests for whether two objects are not equal
  96. // with != assert.notEqual(actual, expected, message_opt);
  97. /**
  98. * Node.js standard [`assert.notEqual`](http://nodejs.org/api/assert.html#assert_assert_notequal_actual_expected_message).
  99. * @static
  100. * @memberOf should
  101. * @category assertion assert
  102. * @param {*} actual
  103. * @param {*} expected
  104. * @param {string} [message]
  105. */
  106. assert.notEqual = function notEqual(actual, expected, message) {
  107. if(actual == expected) {
  108. fail(actual, expected, message, '!=', assert.notEqual);
  109. }
  110. };
  111. // 7. The equivalence assertion tests a deep equality relation.
  112. // assert.deepEqual(actual, expected, message_opt);
  113. /**
  114. * Node.js standard [`assert.deepEqual`](http://nodejs.org/api/assert.html#assert_assert_deepequal_actual_expected_message).
  115. * @static
  116. * @memberOf should
  117. * @category assertion assert
  118. * @param {*} actual
  119. * @param {*} expected
  120. * @param {string} [message]
  121. */
  122. assert.deepEqual = function deepEqual(actual, expected, message) {
  123. if(!_deepEqual(actual, expected).result) {
  124. fail(actual, expected, message, 'deepEqual', assert.deepEqual);
  125. }
  126. };
  127. // 8. The non-equivalence assertion tests for any deep inequality.
  128. // assert.notDeepEqual(actual, expected, message_opt);
  129. /**
  130. * Node.js standard [`assert.notDeepEqual`](http://nodejs.org/api/assert.html#assert_assert_notdeepequal_actual_expected_message).
  131. * @static
  132. * @memberOf should
  133. * @category assertion assert
  134. * @param {*} actual
  135. * @param {*} expected
  136. * @param {string} [message]
  137. */
  138. assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
  139. if(_deepEqual(actual, expected).result) {
  140. fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual);
  141. }
  142. };
  143. // 9. The strict equality assertion tests strict equality, as determined by ===.
  144. // assert.strictEqual(actual, expected, message_opt);
  145. /**
  146. * Node.js standard [`assert.strictEqual`](http://nodejs.org/api/assert.html#assert_assert_strictequal_actual_expected_message).
  147. * @static
  148. * @memberOf should
  149. * @category assertion assert
  150. * @param {*} actual
  151. * @param {*} expected
  152. * @param {string} [message]
  153. */
  154. assert.strictEqual = function strictEqual(actual, expected, message) {
  155. if(actual !== expected) {
  156. fail(actual, expected, message, '===', assert.strictEqual);
  157. }
  158. };
  159. // 10. The strict non-equality assertion tests for strict inequality, as
  160. // determined by !==. assert.notStrictEqual(actual, expected, message_opt);
  161. /**
  162. * Node.js standard [`assert.notStrictEqual`](http://nodejs.org/api/assert.html#assert_assert_notstrictequal_actual_expected_message).
  163. * @static
  164. * @memberOf should
  165. * @category assertion assert
  166. * @param {*} actual
  167. * @param {*} expected
  168. * @param {string} [message]
  169. */
  170. assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
  171. if(actual === expected) {
  172. fail(actual, expected, message, '!==', assert.notStrictEqual);
  173. }
  174. };
  175. function expectedException(actual, expected) {
  176. if(!actual || !expected) {
  177. return false;
  178. }
  179. if(Object.prototype.toString.call(expected) == '[object RegExp]') {
  180. return expected.test(actual);
  181. } else if(actual instanceof expected) {
  182. return true;
  183. } else if(expected.call({}, actual) === true) {
  184. return true;
  185. }
  186. return false;
  187. }
  188. function _throws(shouldThrow, block, expected, message) {
  189. var actual;
  190. if(typeof expected == 'string') {
  191. message = expected;
  192. expected = null;
  193. }
  194. try {
  195. block();
  196. } catch(e) {
  197. actual = e;
  198. }
  199. message = (expected && expected.name ? ' (' + expected.name + ')' : '.') +
  200. (message ? ' ' + message : '.');
  201. if(shouldThrow && !actual) {
  202. fail(actual, expected, 'Missing expected exception' + message);
  203. }
  204. if(!shouldThrow && expectedException(actual, expected)) {
  205. fail(actual, expected, 'Got unwanted exception' + message);
  206. }
  207. if((shouldThrow && actual && expected && !expectedException(actual, expected)) || (!shouldThrow && actual)) {
  208. throw actual;
  209. }
  210. }
  211. // 11. Expected to throw an error:
  212. // assert.throws(block, Error_opt, message_opt);
  213. /**
  214. * Node.js standard [`assert.throws`](http://nodejs.org/api/assert.html#assert_assert_throws_block_error_message).
  215. * @static
  216. * @memberOf should
  217. * @category assertion assert
  218. * @param {Function} block
  219. * @param {Function} [error]
  220. * @param {String} [message]
  221. */
  222. assert.throws = function(block, /*optional*/error, /*optional*/message) {
  223. _throws.apply(this, [true].concat(pSlice.call(arguments)));
  224. };
  225. // EXTENSION! This is annoying to write outside this module.
  226. /**
  227. * Node.js standard [`assert.doesNotThrow`](http://nodejs.org/api/assert.html#assert_assert_doesnotthrow_block_message).
  228. * @static
  229. * @memberOf should
  230. * @category assertion assert
  231. * @param {Function} block
  232. * @param {String} [message]
  233. */
  234. assert.doesNotThrow = function(block, /*optional*/message) {
  235. _throws.apply(this, [false].concat(pSlice.call(arguments)));
  236. };
  237. /**
  238. * Node.js standard [`assert.ifError`](http://nodejs.org/api/assert.html#assert_assert_iferror_value).
  239. * @static
  240. * @memberOf should
  241. * @category assertion assert
  242. * @param {Error} err
  243. */
  244. assert.ifError = function(err) {
  245. if(err) {
  246. throw err;
  247. }
  248. };