assert.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. define(function(require, exports, module) {
  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. // UTILITY
  26. var oop = require("ace/lib/oop");
  27. var pSlice = Array.prototype.slice;
  28. // 1. The assert module provides functions that throw
  29. // AssertionError's when particular conditions are not met. The
  30. // assert module must conform to the following interface.
  31. var assert = exports;
  32. // 2. The AssertionError is defined in assert.
  33. // new assert.AssertionError({ message: message,
  34. // actual: actual,
  35. // expected: expected })
  36. assert.AssertionError = function AssertionError(options) {
  37. this.name = 'AssertionError';
  38. this.message = options.message;
  39. this.actual = options.actual;
  40. this.expected = options.expected;
  41. this.operator = options.operator;
  42. var stackStartFunction = options.stackStartFunction || fail;
  43. if (Error.captureStackTrace) {
  44. Error.captureStackTrace(this, stackStartFunction);
  45. }
  46. };
  47. oop.inherits(assert.AssertionError, Error);
  48. toJSON = function(obj) {
  49. if (typeof JSON !== "undefined")
  50. return JSON.stringify(obj);
  51. else
  52. return obj.toString();
  53. }
  54. assert.AssertionError.prototype.toString = function() {
  55. if (this.message) {
  56. return [this.name + ':', this.message].join(' ');
  57. } else {
  58. return [this.name + ':',
  59. toJSON(this.expected),
  60. this.operator,
  61. toJSON(this.actual)].join(' ');
  62. }
  63. };
  64. // assert.AssertionError instanceof Error
  65. assert.AssertionError.__proto__ = Error.prototype;
  66. // At present only the three keys mentioned above are used and
  67. // understood by the spec. Implementations or sub modules can pass
  68. // other keys to the AssertionError's constructor - they will be
  69. // ignored.
  70. // 3. All of the following functions must throw an AssertionError
  71. // when a corresponding condition is not met, with a message that
  72. // may be undefined if not provided. All assertion methods provide
  73. // both the actual and expected values to the assertion error for
  74. // display purposes.
  75. function fail(actual, expected, message, operator, stackStartFunction) {
  76. throw new assert.AssertionError({
  77. message: message,
  78. actual: actual,
  79. expected: expected,
  80. operator: operator,
  81. stackStartFunction: stackStartFunction
  82. });
  83. }
  84. // EXTENSION! allows for well behaved errors defined elsewhere.
  85. assert.fail = fail;
  86. // 4. Pure assertion tests whether a value is truthy, as determined
  87. // by !!guard.
  88. // assert.ok(guard, message_opt);
  89. // This statement is equivalent to assert.equal(true, guard,
  90. // message_opt);. To test strictly for the value true, use
  91. // assert.strictEqual(true, guard, message_opt);.
  92. assert.ok = function ok(value, message) {
  93. if (!!!value) fail(value, true, message, '==', assert.ok);
  94. };
  95. // 5. The equality assertion tests shallow, coercive equality with
  96. // ==.
  97. // assert.equal(actual, expected, message_opt);
  98. assert.equal = function equal(actual, expected, message) {
  99. if (actual != expected) fail(actual, expected, message, '==', assert.equal);
  100. };
  101. // 6. The non-equality assertion tests for whether two objects are not equal
  102. // with != assert.notEqual(actual, expected, message_opt);
  103. assert.notEqual = function notEqual(actual, expected, message) {
  104. if (actual == expected) {
  105. fail(actual, expected, message, '!=', assert.notEqual);
  106. }
  107. };
  108. // 7. The equivalence assertion tests a deep equality relation.
  109. // assert.deepEqual(actual, expected, message_opt);
  110. assert.deepEqual = function deepEqual(actual, expected, message) {
  111. if (!_deepEqual(actual, expected)) {
  112. fail(actual, expected, message, 'deepEqual', assert.deepEqual);
  113. }
  114. };
  115. function _deepEqual(actual, expected) {
  116. // 7.1. All identical values are equivalent, as determined by ===.
  117. if (actual === expected) {
  118. return true;
  119. } else if (typeof Buffer !== "undefined" && Buffer.isBuffer(actual) && Buffer.isBuffer(expected)) {
  120. if (actual.length != expected.length) return false;
  121. for (var i = 0; i < actual.length; i++) {
  122. if (actual[i] !== expected[i]) return false;
  123. }
  124. return true;
  125. // 7.2. If the expected value is a Date object, the actual value is
  126. // equivalent if it is also a Date object that refers to the same time.
  127. } else if (actual instanceof Date && expected instanceof Date) {
  128. return actual.getTime() === expected.getTime();
  129. // 7.3. Other pairs that do not both pass typeof value == 'object',
  130. // equivalence is determined by ==.
  131. } else if (typeof actual != 'object' && typeof expected != 'object') {
  132. return actual == expected;
  133. // 7.4. For all other Object pairs, including Array objects, equivalence is
  134. // determined by having the same number of owned properties (as verified
  135. // with Object.prototype.hasOwnProperty.call), the same set of keys
  136. // (although not necessarily the same order), equivalent values for every
  137. // corresponding key, and an identical 'prototype' property. Note: this
  138. // accounts for both named and indexed properties on Arrays.
  139. } else {
  140. return objEquiv(actual, expected);
  141. }
  142. }
  143. function isUndefinedOrNull(value) {
  144. return value === null || value === undefined;
  145. }
  146. function isArguments(object) {
  147. return Object.prototype.toString.call(object) == '[object Arguments]';
  148. }
  149. function objEquiv(a, b) {
  150. if (isUndefinedOrNull(a) || isUndefinedOrNull(b))
  151. return false;
  152. // an identical 'prototype' property.
  153. if (a.prototype !== b.prototype) return false;
  154. //~~~I've managed to break Object.keys through screwy arguments passing.
  155. // Converting to array solves the problem.
  156. if (isArguments(a)) {
  157. if (!isArguments(b)) {
  158. return false;
  159. }
  160. a = pSlice.call(a);
  161. b = pSlice.call(b);
  162. return _deepEqual(a, b);
  163. }
  164. try {
  165. var ka = Object.keys(a),
  166. kb = Object.keys(b),
  167. key, i;
  168. } catch (e) {//happens when one is a string literal and the other isn't
  169. return false;
  170. }
  171. // having the same number of owned properties (keys incorporates
  172. // hasOwnProperty)
  173. if (ka.length != kb.length)
  174. return false;
  175. //the same set of keys (although not necessarily the same order),
  176. ka.sort();
  177. kb.sort();
  178. //~~~cheap key test
  179. for (i = ka.length - 1; i >= 0; i--) {
  180. if (ka[i] != kb[i])
  181. return false;
  182. }
  183. //equivalent values for every corresponding key, and
  184. //~~~possibly expensive deep test
  185. for (i = ka.length - 1; i >= 0; i--) {
  186. key = ka[i];
  187. if (!_deepEqual(a[key], b[key])) return false;
  188. }
  189. return true;
  190. }
  191. // 8. The non-equivalence assertion tests for any deep inequality.
  192. // assert.notDeepEqual(actual, expected, message_opt);
  193. assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
  194. if (_deepEqual(actual, expected)) {
  195. fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual);
  196. }
  197. };
  198. // 9. The strict equality assertion tests strict equality, as determined by ===.
  199. // assert.strictEqual(actual, expected, message_opt);
  200. assert.strictEqual = function strictEqual(actual, expected, message) {
  201. if (actual !== expected) {
  202. fail(actual, expected, message, '===', assert.strictEqual);
  203. }
  204. };
  205. // 10. The strict non-equality assertion tests for strict inequality, as
  206. // determined by !==. assert.notStrictEqual(actual, expected, message_opt);
  207. assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
  208. if (actual === expected) {
  209. fail(actual, expected, message, '!==', assert.notStrictEqual);
  210. }
  211. };
  212. function expectedException(actual, expected) {
  213. if (!actual || !expected) {
  214. return false;
  215. }
  216. if (expected instanceof RegExp) {
  217. return expected.test(actual);
  218. } else if (actual instanceof expected) {
  219. return true;
  220. } else if (expected.call({}, actual) === true) {
  221. return true;
  222. }
  223. return false;
  224. }
  225. function _throws(shouldThrow, block, expected, message) {
  226. var actual;
  227. if (typeof expected === 'string') {
  228. message = expected;
  229. expected = null;
  230. }
  231. try {
  232. block();
  233. } catch (e) {
  234. actual = e;
  235. }
  236. message = (expected && expected.name ? ' (' + expected.name + ').' : '.') +
  237. (message ? ' ' + message : '.');
  238. if (shouldThrow && !actual) {
  239. fail('Missing expected exception' + message);
  240. }
  241. if (!shouldThrow && expectedException(actual, expected)) {
  242. fail('Got unwanted exception' + message);
  243. }
  244. if ((shouldThrow && actual && expected &&
  245. !expectedException(actual, expected)) || (!shouldThrow && actual)) {
  246. throw actual;
  247. }
  248. }
  249. // 11. Expected to throw an error:
  250. // assert.throws(block, Error_opt, message_opt);
  251. assert.throws = function(block, /*optional*/error, /*optional*/message) {
  252. _throws.apply(this, [true].concat(pSlice.call(arguments)));
  253. };
  254. // EXTENSION! This is annoying to write outside this module.
  255. assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message) {
  256. _throws.apply(this, [false].concat(pSlice.call(arguments)));
  257. };
  258. assert.ifError = function(err) { if (err) {throw err;}};
  259. });