Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

assert.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. // http://wiki.commonjs.org/wiki/Unit_Testing/1.0
  2. //
  3. // THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8!
  4. //
  5. // Copyright (c) 2011 Jxck
  6. //
  7. // Originally from node.js (http://nodejs.org)
  8. // Copyright Joyent, Inc.
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining a copy
  11. // of this software and associated documentation files (the 'Software'), to
  12. // deal in the Software without restriction, including without limitation the
  13. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  14. // sell copies of the Software, and to permit persons to whom the Software is
  15. // furnished to do so, subject to the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be included in
  18. // all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. // AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  24. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. (function(global) {
  27. // Object.create compatible in IE
  28. var create = Object.create || function(p) {
  29. if (!p) throw Error('no type');
  30. function f() {};
  31. f.prototype = p;
  32. return new f();
  33. };
  34. // UTILITY
  35. var util = {
  36. inherits: function(ctor, superCtor) {
  37. ctor.super_ = superCtor;
  38. ctor.prototype = create(superCtor.prototype, {
  39. constructor: {
  40. value: ctor,
  41. enumerable: false,
  42. writable: true,
  43. configurable: true
  44. }
  45. });
  46. },
  47. isArray: function(ar) {
  48. return Array.isArray(ar);
  49. },
  50. isBoolean: function(arg) {
  51. return typeof arg === 'boolean';
  52. },
  53. isNull: function(arg) {
  54. return arg === null;
  55. },
  56. isNullOrUndefined: function(arg) {
  57. return arg == null;
  58. },
  59. isNumber: function(arg) {
  60. return typeof arg === 'number';
  61. },
  62. isString: function(arg) {
  63. return typeof arg === 'string';
  64. },
  65. isSymbol: function(arg) {
  66. return typeof arg === 'symbol';
  67. },
  68. isUndefined: function(arg) {
  69. return arg === void 0;
  70. },
  71. isRegExp: function(re) {
  72. return util.isObject(re) && util.objectToString(re) === '[object RegExp]';
  73. },
  74. isObject: function(arg) {
  75. return typeof arg === 'object' && arg !== null;
  76. },
  77. isDate: function(d) {
  78. return util.isObject(d) && util.objectToString(d) === '[object Date]';
  79. },
  80. isError: function(e) {
  81. return isObject(e) &&
  82. (objectToString(e) === '[object Error]' || e instanceof Error);
  83. },
  84. isFunction: function(arg) {
  85. return typeof arg === 'function';
  86. },
  87. isPrimitive: function(arg) {
  88. return arg === null ||
  89. typeof arg === 'boolean' ||
  90. typeof arg === 'number' ||
  91. typeof arg === 'string' ||
  92. typeof arg === 'symbol' || // ES6 symbol
  93. typeof arg === 'undefined';
  94. },
  95. objectToString: function(o) {
  96. return Object.prototype.toString.call(o);
  97. }
  98. };
  99. var pSlice = Array.prototype.slice;
  100. // from https://github.com/substack/node-deep-equal
  101. var Object_keys = typeof Object.keys === 'function'
  102. ? Object.keys
  103. : function (obj) {
  104. var keys = [];
  105. for (var key in obj) keys.push(key);
  106. return keys;
  107. }
  108. ;
  109. // 1. The assert module provides functions that throw
  110. // AssertionError's when particular conditions are not met. The
  111. // assert module must conform to the following interface.
  112. var assert = ok;
  113. global['assert'] = assert;
  114. if (typeof module === 'object' && typeof module.exports === 'object') {
  115. module.exports = assert;
  116. };
  117. // 2. The AssertionError is defined in assert.
  118. // new assert.AssertionError({ message: message,
  119. // actual: actual,
  120. // expected: expected })
  121. assert.AssertionError = function AssertionError(options) {
  122. this.name = 'AssertionError';
  123. this.actual = options.actual;
  124. this.expected = options.expected;
  125. this.operator = options.operator;
  126. if (options.message) {
  127. this.message = options.message;
  128. this.generatedMessage = false;
  129. } else {
  130. this.message = getMessage(this);
  131. this.generatedMessage = true;
  132. }
  133. var stackStartFunction = options.stackStartFunction || fail;
  134. if (Error.captureStackTrace) {
  135. Error.captureStackTrace(this, stackStartFunction);
  136. } else {
  137. // try to throw an error now, and from the stack property
  138. // work out the line that called in to assert.js.
  139. try {
  140. this.stack = (new Error).stack.toString();
  141. } catch (e) {}
  142. }
  143. };
  144. // assert.AssertionError instanceof Error
  145. util.inherits(assert.AssertionError, Error);
  146. function replacer(key, value) {
  147. if (util.isUndefined(value)) {
  148. return '' + value;
  149. }
  150. if (util.isNumber(value) && (isNaN(value) || !isFinite(value))) {
  151. return value.toString();
  152. }
  153. if (util.isFunction(value) || util.isRegExp(value)) {
  154. return value.toString();
  155. }
  156. return value;
  157. }
  158. function truncate(s, n) {
  159. if (util.isString(s)) {
  160. return s.length < n ? s : s.slice(0, n);
  161. } else {
  162. return s;
  163. }
  164. }
  165. function getMessage(self) {
  166. return truncate(JSON.stringify(self.actual, replacer), 128) + ' ' +
  167. self.operator + ' ' +
  168. truncate(JSON.stringify(self.expected, replacer), 128);
  169. }
  170. // At present only the three keys mentioned above are used and
  171. // understood by the spec. Implementations or sub modules can pass
  172. // other keys to the AssertionError's constructor - they will be
  173. // ignored.
  174. // 3. All of the following functions must throw an AssertionError
  175. // when a corresponding condition is not met, with a message that
  176. // may be undefined if not provided. All assertion methods provide
  177. // both the actual and expected values to the assertion error for
  178. // display purposes.
  179. function fail(actual, expected, message, operator, stackStartFunction) {
  180. throw new assert.AssertionError({
  181. message: message,
  182. actual: actual,
  183. expected: expected,
  184. operator: operator,
  185. stackStartFunction: stackStartFunction
  186. });
  187. }
  188. // EXTENSION! allows for well behaved errors defined elsewhere.
  189. assert.fail = fail;
  190. // 4. Pure assertion tests whether a value is truthy, as determined
  191. // by !!guard.
  192. // assert.ok(guard, message_opt);
  193. // This statement is equivalent to assert.equal(true, !!guard,
  194. // message_opt);. To test strictly for the value true, use
  195. // assert.strictEqual(true, guard, message_opt);.
  196. function ok(value, message) {
  197. if (!value) fail(value, true, message, '==', assert.ok);
  198. }
  199. assert.ok = ok;
  200. // 5. The equality assertion tests shallow, coercive equality with
  201. // ==.
  202. // assert.equal(actual, expected, message_opt);
  203. assert.equal = function equal(actual, expected, message) {
  204. if (actual != expected) fail(actual, expected, message, '==', assert.equal);
  205. };
  206. // 6. The non-equality assertion tests for whether two objects are not equal
  207. // with != assert.notEqual(actual, expected, message_opt);
  208. assert.notEqual = function notEqual(actual, expected, message) {
  209. if (actual == expected) {
  210. fail(actual, expected, message, '!=', assert.notEqual);
  211. }
  212. };
  213. // 7. The equivalence assertion tests a deep equality relation.
  214. // assert.deepEqual(actual, expected, message_opt);
  215. assert.deepEqual = function deepEqual(actual, expected, message) {
  216. if (!_deepEqual(actual, expected)) {
  217. fail(actual, expected, message, 'deepEqual', assert.deepEqual);
  218. }
  219. };
  220. function _deepEqual(actual, expected) {
  221. // 7.1. All identical values are equivalent, as determined by ===.
  222. if (actual === expected) {
  223. return true;
  224. // } else if (util.isBuffer(actual) && util.isBuffer(expected)) {
  225. // if (actual.length != expected.length) return false;
  226. //
  227. // for (var i = 0; i < actual.length; i++) {
  228. // if (actual[i] !== expected[i]) return false;
  229. // }
  230. //
  231. // return true;
  232. // 7.2. If the expected value is a Date object, the actual value is
  233. // equivalent if it is also a Date object that refers to the same time.
  234. } else if (util.isDate(actual) && util.isDate(expected)) {
  235. return actual.getTime() === expected.getTime();
  236. // 7.3 If the expected value is a RegExp object, the actual value is
  237. // equivalent if it is also a RegExp object with the same source and
  238. // properties (`global`, `multiline`, `lastIndex`, `ignoreCase`).
  239. } else if (util.isRegExp(actual) && util.isRegExp(expected)) {
  240. return actual.source === expected.source &&
  241. actual.global === expected.global &&
  242. actual.multiline === expected.multiline &&
  243. actual.lastIndex === expected.lastIndex &&
  244. actual.ignoreCase === expected.ignoreCase;
  245. // 7.4. Other pairs that do not both pass typeof value == 'object',
  246. // equivalence is determined by ==.
  247. } else if (!util.isObject(actual) && !util.isObject(expected)) {
  248. return actual == expected;
  249. // 7.5 For all other Object pairs, including Array objects, equivalence is
  250. // determined by having the same number of owned properties (as verified
  251. // with Object.prototype.hasOwnProperty.call), the same set of keys
  252. // (although not necessarily the same order), equivalent values for every
  253. // corresponding key, and an identical 'prototype' property. Note: this
  254. // accounts for both named and indexed properties on Arrays.
  255. } else {
  256. return objEquiv(actual, expected);
  257. }
  258. }
  259. function isArguments(object) {
  260. return Object.prototype.toString.call(object) == '[object Arguments]';
  261. }
  262. function objEquiv(a, b) {
  263. if (util.isNullOrUndefined(a) || util.isNullOrUndefined(b))
  264. return false;
  265. // an identical 'prototype' property.
  266. if (a.prototype !== b.prototype) return false;
  267. //~~~I've managed to break Object.keys through screwy arguments passing.
  268. // Converting to array solves the problem.
  269. if (isArguments(a)) {
  270. if (!isArguments(b)) {
  271. return false;
  272. }
  273. a = pSlice.call(a);
  274. b = pSlice.call(b);
  275. return _deepEqual(a, b);
  276. }
  277. try {
  278. var ka = Object_keys(a),
  279. kb = Object_keys(b),
  280. key, i;
  281. } catch (e) {//happens when one is a string literal and the other isn't
  282. return false;
  283. }
  284. // having the same number of owned properties (keys incorporates
  285. // hasOwnProperty)
  286. if (ka.length != kb.length)
  287. return false;
  288. //the same set of keys (although not necessarily the same order),
  289. ka.sort();
  290. kb.sort();
  291. //~~~cheap key test
  292. for (i = ka.length - 1; i >= 0; i--) {
  293. if (ka[i] != kb[i])
  294. return false;
  295. }
  296. //equivalent values for every corresponding key, and
  297. //~~~possibly expensive deep test
  298. for (i = ka.length - 1; i >= 0; i--) {
  299. key = ka[i];
  300. if (!_deepEqual(a[key], b[key])) return false;
  301. }
  302. return true;
  303. }
  304. // 8. The non-equivalence assertion tests for any deep inequality.
  305. // assert.notDeepEqual(actual, expected, message_opt);
  306. assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
  307. if (_deepEqual(actual, expected)) {
  308. fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual);
  309. }
  310. };
  311. // 9. The strict equality assertion tests strict equality, as determined by ===.
  312. // assert.strictEqual(actual, expected, message_opt);
  313. assert.strictEqual = function strictEqual(actual, expected, message) {
  314. if (actual !== expected) {
  315. fail(actual, expected, message, '===', assert.strictEqual);
  316. }
  317. };
  318. // 10. The strict non-equality assertion tests for strict inequality, as
  319. // determined by !==. assert.notStrictEqual(actual, expected, message_opt);
  320. assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
  321. if (actual === expected) {
  322. fail(actual, expected, message, '!==', assert.notStrictEqual);
  323. }
  324. };
  325. function expectedException(actual, expected) {
  326. if (!actual || !expected) {
  327. return false;
  328. }
  329. if (Object.prototype.toString.call(expected) == '[object RegExp]') {
  330. return expected.test(actual);
  331. } else if (actual instanceof expected) {
  332. return true;
  333. } else if (expected.call({}, actual) === true) {
  334. return true;
  335. }
  336. return false;
  337. }
  338. function _throws(shouldThrow, block, expected, message) {
  339. var actual;
  340. if (util.isString(expected)) {
  341. message = expected;
  342. expected = null;
  343. }
  344. try {
  345. block();
  346. } catch (e) {
  347. actual = e;
  348. }
  349. message = (expected && expected.name ? ' (' + expected.name + ').' : '.') +
  350. (message ? ' ' + message : '.');
  351. if (shouldThrow && !actual) {
  352. fail(actual, expected, 'Missing expected exception' + message);
  353. }
  354. if (!shouldThrow && expectedException(actual, expected)) {
  355. fail(actual, expected, 'Got unwanted exception' + message);
  356. }
  357. if ((shouldThrow && actual && expected &&
  358. !expectedException(actual, expected)) || (!shouldThrow && actual)) {
  359. throw actual;
  360. }
  361. }
  362. // 11. Expected to throw an error:
  363. // assert.throws(block, Error_opt, message_opt);
  364. assert.throws = function(block, /*optional*/error, /*optional*/message) {
  365. _throws.apply(this, [true].concat(pSlice.call(arguments)));
  366. };
  367. // EXTENSION! This is annoying to write outside this module.
  368. assert.doesNotThrow = function(block, /*optional*/message) {
  369. _throws.apply(this, [false].concat(pSlice.call(arguments)));
  370. };
  371. assert.ifError = function(err) { if (err) {throw err;}};
  372. if (typeof define === 'function' && define.amd) {
  373. define('assert', function () {
  374. return assert;
  375. });
  376. }
  377. })(this);