Front end of the Slack clone application.

assert.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. 'use strict';
  2. // compare and isBuffer taken from https://github.com/feross/buffer/blob/680e9e5e488f22aac27599a57dc844a6315928dd/index.js
  3. // original notice:
  4. /*!
  5. * The buffer module from node.js, for the browser.
  6. *
  7. * @author Feross Aboukhadijeh <feross@feross.org> <http://feross.org>
  8. * @license MIT
  9. */
  10. function compare(a, b) {
  11. if (a === b) {
  12. return 0;
  13. }
  14. var x = a.length;
  15. var y = b.length;
  16. for (var i = 0, len = Math.min(x, y); i < len; ++i) {
  17. if (a[i] !== b[i]) {
  18. x = a[i];
  19. y = b[i];
  20. break;
  21. }
  22. }
  23. if (x < y) {
  24. return -1;
  25. }
  26. if (y < x) {
  27. return 1;
  28. }
  29. return 0;
  30. }
  31. function isBuffer(b) {
  32. if (global.Buffer && typeof global.Buffer.isBuffer === 'function') {
  33. return global.Buffer.isBuffer(b);
  34. }
  35. return !!(b != null && b._isBuffer);
  36. }
  37. // based on node assert, original notice:
  38. // http://wiki.commonjs.org/wiki/Unit_Testing/1.0
  39. //
  40. // THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8!
  41. //
  42. // Originally from narwhal.js (http://narwhaljs.org)
  43. // Copyright (c) 2009 Thomas Robinson <280north.com>
  44. //
  45. // Permission is hereby granted, free of charge, to any person obtaining a copy
  46. // of this software and associated documentation files (the 'Software'), to
  47. // deal in the Software without restriction, including without limitation the
  48. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  49. // sell copies of the Software, and to permit persons to whom the Software is
  50. // furnished to do so, subject to the following conditions:
  51. //
  52. // The above copyright notice and this permission notice shall be included in
  53. // all copies or substantial portions of the Software.
  54. //
  55. // THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  56. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  57. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  58. // AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  59. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  60. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  61. var util = require('util/');
  62. var hasOwn = Object.prototype.hasOwnProperty;
  63. var pSlice = Array.prototype.slice;
  64. var functionsHaveNames = (function () {
  65. return function foo() {}.name === 'foo';
  66. }());
  67. function pToString (obj) {
  68. return Object.prototype.toString.call(obj);
  69. }
  70. function isView(arrbuf) {
  71. if (isBuffer(arrbuf)) {
  72. return false;
  73. }
  74. if (typeof global.ArrayBuffer !== 'function') {
  75. return false;
  76. }
  77. if (typeof ArrayBuffer.isView === 'function') {
  78. return ArrayBuffer.isView(arrbuf);
  79. }
  80. if (!arrbuf) {
  81. return false;
  82. }
  83. if (arrbuf instanceof DataView) {
  84. return true;
  85. }
  86. if (arrbuf.buffer && arrbuf.buffer instanceof ArrayBuffer) {
  87. return true;
  88. }
  89. return false;
  90. }
  91. // 1. The assert module provides functions that throw
  92. // AssertionError's when particular conditions are not met. The
  93. // assert module must conform to the following interface.
  94. var assert = module.exports = ok;
  95. // 2. The AssertionError is defined in assert.
  96. // new assert.AssertionError({ message: message,
  97. // actual: actual,
  98. // expected: expected })
  99. var regex = /\s*function\s+([^\(\s]*)\s*/;
  100. // based on https://github.com/ljharb/function.prototype.name/blob/adeeeec8bfcc6068b187d7d9fb3d5bb1d3a30899/implementation.js
  101. function getName(func) {
  102. if (!util.isFunction(func)) {
  103. return;
  104. }
  105. if (functionsHaveNames) {
  106. return func.name;
  107. }
  108. var str = func.toString();
  109. var match = str.match(regex);
  110. return match && match[1];
  111. }
  112. assert.AssertionError = function AssertionError(options) {
  113. this.name = 'AssertionError';
  114. this.actual = options.actual;
  115. this.expected = options.expected;
  116. this.operator = options.operator;
  117. if (options.message) {
  118. this.message = options.message;
  119. this.generatedMessage = false;
  120. } else {
  121. this.message = getMessage(this);
  122. this.generatedMessage = true;
  123. }
  124. var stackStartFunction = options.stackStartFunction || fail;
  125. if (Error.captureStackTrace) {
  126. Error.captureStackTrace(this, stackStartFunction);
  127. } else {
  128. // non v8 browsers so we can have a stacktrace
  129. var err = new Error();
  130. if (err.stack) {
  131. var out = err.stack;
  132. // try to strip useless frames
  133. var fn_name = getName(stackStartFunction);
  134. var idx = out.indexOf('\n' + fn_name);
  135. if (idx >= 0) {
  136. // once we have located the function frame
  137. // we need to strip out everything before it (and its line)
  138. var next_line = out.indexOf('\n', idx + 1);
  139. out = out.substring(next_line + 1);
  140. }
  141. this.stack = out;
  142. }
  143. }
  144. };
  145. // assert.AssertionError instanceof Error
  146. util.inherits(assert.AssertionError, Error);
  147. function truncate(s, n) {
  148. if (typeof s === 'string') {
  149. return s.length < n ? s : s.slice(0, n);
  150. } else {
  151. return s;
  152. }
  153. }
  154. function inspect(something) {
  155. if (functionsHaveNames || !util.isFunction(something)) {
  156. return util.inspect(something);
  157. }
  158. var rawname = getName(something);
  159. var name = rawname ? ': ' + rawname : '';
  160. return '[Function' + name + ']';
  161. }
  162. function getMessage(self) {
  163. return truncate(inspect(self.actual), 128) + ' ' +
  164. self.operator + ' ' +
  165. truncate(inspect(self.expected), 128);
  166. }
  167. // At present only the three keys mentioned above are used and
  168. // understood by the spec. Implementations or sub modules can pass
  169. // other keys to the AssertionError's constructor - they will be
  170. // ignored.
  171. // 3. All of the following functions must throw an AssertionError
  172. // when a corresponding condition is not met, with a message that
  173. // may be undefined if not provided. All assertion methods provide
  174. // both the actual and expected values to the assertion error for
  175. // display purposes.
  176. function fail(actual, expected, message, operator, stackStartFunction) {
  177. throw new assert.AssertionError({
  178. message: message,
  179. actual: actual,
  180. expected: expected,
  181. operator: operator,
  182. stackStartFunction: stackStartFunction
  183. });
  184. }
  185. // EXTENSION! allows for well behaved errors defined elsewhere.
  186. assert.fail = fail;
  187. // 4. Pure assertion tests whether a value is truthy, as determined
  188. // by !!guard.
  189. // assert.ok(guard, message_opt);
  190. // This statement is equivalent to assert.equal(true, !!guard,
  191. // message_opt);. To test strictly for the value true, use
  192. // assert.strictEqual(true, guard, message_opt);.
  193. function ok(value, message) {
  194. if (!value) fail(value, true, message, '==', assert.ok);
  195. }
  196. assert.ok = ok;
  197. // 5. The equality assertion tests shallow, coercive equality with
  198. // ==.
  199. // assert.equal(actual, expected, message_opt);
  200. assert.equal = function equal(actual, expected, message) {
  201. if (actual != expected) fail(actual, expected, message, '==', assert.equal);
  202. };
  203. // 6. The non-equality assertion tests for whether two objects are not equal
  204. // with != assert.notEqual(actual, expected, message_opt);
  205. assert.notEqual = function notEqual(actual, expected, message) {
  206. if (actual == expected) {
  207. fail(actual, expected, message, '!=', assert.notEqual);
  208. }
  209. };
  210. // 7. The equivalence assertion tests a deep equality relation.
  211. // assert.deepEqual(actual, expected, message_opt);
  212. assert.deepEqual = function deepEqual(actual, expected, message) {
  213. if (!_deepEqual(actual, expected, false)) {
  214. fail(actual, expected, message, 'deepEqual', assert.deepEqual);
  215. }
  216. };
  217. assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) {
  218. if (!_deepEqual(actual, expected, true)) {
  219. fail(actual, expected, message, 'deepStrictEqual', assert.deepStrictEqual);
  220. }
  221. };
  222. function _deepEqual(actual, expected, strict, memos) {
  223. // 7.1. All identical values are equivalent, as determined by ===.
  224. if (actual === expected) {
  225. return true;
  226. } else if (isBuffer(actual) && isBuffer(expected)) {
  227. return compare(actual, expected) === 0;
  228. // 7.2. If the expected value is a Date object, the actual value is
  229. // equivalent if it is also a Date object that refers to the same time.
  230. } else if (util.isDate(actual) && util.isDate(expected)) {
  231. return actual.getTime() === expected.getTime();
  232. // 7.3 If the expected value is a RegExp object, the actual value is
  233. // equivalent if it is also a RegExp object with the same source and
  234. // properties (`global`, `multiline`, `lastIndex`, `ignoreCase`).
  235. } else if (util.isRegExp(actual) && util.isRegExp(expected)) {
  236. return actual.source === expected.source &&
  237. actual.global === expected.global &&
  238. actual.multiline === expected.multiline &&
  239. actual.lastIndex === expected.lastIndex &&
  240. actual.ignoreCase === expected.ignoreCase;
  241. // 7.4. Other pairs that do not both pass typeof value == 'object',
  242. // equivalence is determined by ==.
  243. } else if ((actual === null || typeof actual !== 'object') &&
  244. (expected === null || typeof expected !== 'object')) {
  245. return strict ? actual === expected : actual == expected;
  246. // If both values are instances of typed arrays, wrap their underlying
  247. // ArrayBuffers in a Buffer each to increase performance
  248. // This optimization requires the arrays to have the same type as checked by
  249. // Object.prototype.toString (aka pToString). Never perform binary
  250. // comparisons for Float*Arrays, though, since e.g. +0 === -0 but their
  251. // bit patterns are not identical.
  252. } else if (isView(actual) && isView(expected) &&
  253. pToString(actual) === pToString(expected) &&
  254. !(actual instanceof Float32Array ||
  255. actual instanceof Float64Array)) {
  256. return compare(new Uint8Array(actual.buffer),
  257. new Uint8Array(expected.buffer)) === 0;
  258. // 7.5 For all other Object pairs, including Array objects, equivalence is
  259. // determined by having the same number of owned properties (as verified
  260. // with Object.prototype.hasOwnProperty.call), the same set of keys
  261. // (although not necessarily the same order), equivalent values for every
  262. // corresponding key, and an identical 'prototype' property. Note: this
  263. // accounts for both named and indexed properties on Arrays.
  264. } else if (isBuffer(actual) !== isBuffer(expected)) {
  265. return false;
  266. } else {
  267. memos = memos || {actual: [], expected: []};
  268. var actualIndex = memos.actual.indexOf(actual);
  269. if (actualIndex !== -1) {
  270. if (actualIndex === memos.expected.indexOf(expected)) {
  271. return true;
  272. }
  273. }
  274. memos.actual.push(actual);
  275. memos.expected.push(expected);
  276. return objEquiv(actual, expected, strict, memos);
  277. }
  278. }
  279. function isArguments(object) {
  280. return Object.prototype.toString.call(object) == '[object Arguments]';
  281. }
  282. function objEquiv(a, b, strict, actualVisitedObjects) {
  283. if (a === null || a === undefined || b === null || b === undefined)
  284. return false;
  285. // if one is a primitive, the other must be same
  286. if (util.isPrimitive(a) || util.isPrimitive(b))
  287. return a === b;
  288. if (strict && Object.getPrototypeOf(a) !== Object.getPrototypeOf(b))
  289. return false;
  290. var aIsArgs = isArguments(a);
  291. var bIsArgs = isArguments(b);
  292. if ((aIsArgs && !bIsArgs) || (!aIsArgs && bIsArgs))
  293. return false;
  294. if (aIsArgs) {
  295. a = pSlice.call(a);
  296. b = pSlice.call(b);
  297. return _deepEqual(a, b, strict);
  298. }
  299. var ka = objectKeys(a);
  300. var kb = objectKeys(b);
  301. var key, i;
  302. // having the same number of owned properties (keys incorporates
  303. // hasOwnProperty)
  304. if (ka.length !== kb.length)
  305. return false;
  306. //the same set of keys (although not necessarily the same order),
  307. ka.sort();
  308. kb.sort();
  309. //~~~cheap key test
  310. for (i = ka.length - 1; i >= 0; i--) {
  311. if (ka[i] !== kb[i])
  312. return false;
  313. }
  314. //equivalent values for every corresponding key, and
  315. //~~~possibly expensive deep test
  316. for (i = ka.length - 1; i >= 0; i--) {
  317. key = ka[i];
  318. if (!_deepEqual(a[key], b[key], strict, actualVisitedObjects))
  319. return false;
  320. }
  321. return true;
  322. }
  323. // 8. The non-equivalence assertion tests for any deep inequality.
  324. // assert.notDeepEqual(actual, expected, message_opt);
  325. assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
  326. if (_deepEqual(actual, expected, false)) {
  327. fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual);
  328. }
  329. };
  330. assert.notDeepStrictEqual = notDeepStrictEqual;
  331. function notDeepStrictEqual(actual, expected, message) {
  332. if (_deepEqual(actual, expected, true)) {
  333. fail(actual, expected, message, 'notDeepStrictEqual', notDeepStrictEqual);
  334. }
  335. }
  336. // 9. The strict equality assertion tests strict equality, as determined by ===.
  337. // assert.strictEqual(actual, expected, message_opt);
  338. assert.strictEqual = function strictEqual(actual, expected, message) {
  339. if (actual !== expected) {
  340. fail(actual, expected, message, '===', assert.strictEqual);
  341. }
  342. };
  343. // 10. The strict non-equality assertion tests for strict inequality, as
  344. // determined by !==. assert.notStrictEqual(actual, expected, message_opt);
  345. assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
  346. if (actual === expected) {
  347. fail(actual, expected, message, '!==', assert.notStrictEqual);
  348. }
  349. };
  350. function expectedException(actual, expected) {
  351. if (!actual || !expected) {
  352. return false;
  353. }
  354. if (Object.prototype.toString.call(expected) == '[object RegExp]') {
  355. return expected.test(actual);
  356. }
  357. try {
  358. if (actual instanceof expected) {
  359. return true;
  360. }
  361. } catch (e) {
  362. // Ignore. The instanceof check doesn't work for arrow functions.
  363. }
  364. if (Error.isPrototypeOf(expected)) {
  365. return false;
  366. }
  367. return expected.call({}, actual) === true;
  368. }
  369. function _tryBlock(block) {
  370. var error;
  371. try {
  372. block();
  373. } catch (e) {
  374. error = e;
  375. }
  376. return error;
  377. }
  378. function _throws(shouldThrow, block, expected, message) {
  379. var actual;
  380. if (typeof block !== 'function') {
  381. throw new TypeError('"block" argument must be a function');
  382. }
  383. if (typeof expected === 'string') {
  384. message = expected;
  385. expected = null;
  386. }
  387. actual = _tryBlock(block);
  388. message = (expected && expected.name ? ' (' + expected.name + ').' : '.') +
  389. (message ? ' ' + message : '.');
  390. if (shouldThrow && !actual) {
  391. fail(actual, expected, 'Missing expected exception' + message);
  392. }
  393. var userProvidedMessage = typeof message === 'string';
  394. var isUnwantedException = !shouldThrow && util.isError(actual);
  395. var isUnexpectedException = !shouldThrow && actual && !expected;
  396. if ((isUnwantedException &&
  397. userProvidedMessage &&
  398. expectedException(actual, expected)) ||
  399. isUnexpectedException) {
  400. fail(actual, expected, 'Got unwanted exception' + message);
  401. }
  402. if ((shouldThrow && actual && expected &&
  403. !expectedException(actual, expected)) || (!shouldThrow && actual)) {
  404. throw actual;
  405. }
  406. }
  407. // 11. Expected to throw an error:
  408. // assert.throws(block, Error_opt, message_opt);
  409. assert.throws = function(block, /*optional*/error, /*optional*/message) {
  410. _throws(true, block, error, message);
  411. };
  412. // EXTENSION! This is annoying to write outside this module.
  413. assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message) {
  414. _throws(false, block, error, message);
  415. };
  416. assert.ifError = function(err) { if (err) throw err; };
  417. var objectKeys = Object.keys || function (obj) {
  418. var keys = [];
  419. for (var key in obj) {
  420. if (hasOwn.call(obj, key)) keys.push(key);
  421. }
  422. return keys;
  423. };