Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

test.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // Copyright Joyent, Inc. and other Node contributors.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a
  4. // copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to permit
  8. // persons to whom the Software is furnished to do so, subject to the
  9. // following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included
  12. // in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. var nodeAssert = require('assert');
  22. var ourAssert = require('./');
  23. var keys = Object.keys;
  24. if (process.env.TEST_NATIVE === true) {
  25. tests(nodeAssert, 'node assert');
  26. } else {
  27. tests(ourAssert, 'our assert');
  28. }
  29. function makeBlock(f) {
  30. var args = Array.prototype.slice.call(arguments, 1);
  31. return function() {
  32. return f.apply(this, args);
  33. };
  34. }
  35. function tests (assert, what) {
  36. test('assert.ok', function () {
  37. assert.throws(makeBlock(assert, false), assert.AssertionError, 'ok(false)');
  38. assert.doesNotThrow(makeBlock(assert, true), assert.AssertionError, 'ok(true)');
  39. assert.doesNotThrow(makeBlock(assert, 'test', 'ok(\'test\')'));
  40. assert.throws(makeBlock(assert.ok, false),
  41. assert.AssertionError, 'ok(false)');
  42. assert.doesNotThrow(makeBlock(assert.ok, true),
  43. assert.AssertionError, 'ok(true)');
  44. assert.doesNotThrow(makeBlock(assert.ok, 'test'), 'ok(\'test\')');
  45. });
  46. test('assert.equal', function () {
  47. assert.throws(makeBlock(assert.equal, true, false), assert.AssertionError, 'equal');
  48. assert.doesNotThrow(makeBlock(assert.equal, null, null), 'equal');
  49. assert.doesNotThrow(makeBlock(assert.equal, undefined, undefined), 'equal');
  50. assert.doesNotThrow(makeBlock(assert.equal, null, undefined), 'equal');
  51. assert.doesNotThrow(makeBlock(assert.equal, true, true), 'equal');
  52. assert.doesNotThrow(makeBlock(assert.equal, 2, '2'), 'equal');
  53. assert.doesNotThrow(makeBlock(assert.notEqual, true, false), 'notEqual');
  54. assert.throws(makeBlock(assert.notEqual, true, true),
  55. assert.AssertionError, 'notEqual');
  56. });
  57. test('assert.strictEqual', function () {
  58. assert.throws(makeBlock(assert.strictEqual, 2, '2'),
  59. assert.AssertionError, 'strictEqual');
  60. assert.throws(makeBlock(assert.strictEqual, null, undefined),
  61. assert.AssertionError, 'strictEqual');
  62. assert.doesNotThrow(makeBlock(assert.notStrictEqual, 2, '2'), 'notStrictEqual');
  63. });
  64. test('assert.deepStrictEqual', function () {
  65. assert.throws(makeBlock(assert.deepStrictEqual, [2], ['2']),
  66. assert.AssertionError, 'deepStrictEqual');
  67. assert.throws(makeBlock(assert.deepStrictEqual, [null], [undefined]),
  68. assert.AssertionError, 'deepStrictEqual');
  69. assert.doesNotThrow(makeBlock(assert.notDeepStrictEqual, [2], ['2']), 'notDeepStrictEqual');
  70. });
  71. test('assert.deepEqual - 7.2', function () {
  72. assert.doesNotThrow(makeBlock(assert.deepEqual, new Date(2000, 3, 14),
  73. new Date(2000, 3, 14)), 'deepEqual date');
  74. assert.throws(makeBlock(assert.deepEqual, new Date(), new Date(2000, 3, 14)),
  75. assert.AssertionError,
  76. 'deepEqual date');
  77. });
  78. test('assert.deepEqual - 7.3', function () {
  79. assert.doesNotThrow(makeBlock(assert.deepEqual, /a/, /a/));
  80. assert.doesNotThrow(makeBlock(assert.deepEqual, /a/g, /a/g));
  81. assert.doesNotThrow(makeBlock(assert.deepEqual, /a/i, /a/i));
  82. assert.doesNotThrow(makeBlock(assert.deepEqual, /a/m, /a/m));
  83. assert.doesNotThrow(makeBlock(assert.deepEqual, /a/igm, /a/igm));
  84. assert.throws(makeBlock(assert.deepEqual, /ab/, /a/));
  85. assert.throws(makeBlock(assert.deepEqual, /a/g, /a/));
  86. assert.throws(makeBlock(assert.deepEqual, /a/i, /a/));
  87. assert.throws(makeBlock(assert.deepEqual, /a/m, /a/));
  88. assert.throws(makeBlock(assert.deepEqual, /a/igm, /a/im));
  89. var re1 = /a/;
  90. re1.lastIndex = 3;
  91. assert.throws(makeBlock(assert.deepEqual, re1, /a/));
  92. });
  93. test('assert.deepEqual - 7.4', function () {
  94. assert.doesNotThrow(makeBlock(assert.deepEqual, 4, '4'), 'deepEqual == check');
  95. assert.doesNotThrow(makeBlock(assert.deepEqual, true, 1), 'deepEqual == check');
  96. assert.throws(makeBlock(assert.deepEqual, 4, '5'),
  97. assert.AssertionError,
  98. 'deepEqual == check');
  99. });
  100. test('assert.deepEqual - 7.5', function () {
  101. // having the same number of owned properties && the same set of keys
  102. assert.doesNotThrow(makeBlock(assert.deepEqual, {a: 4}, {a: 4}));
  103. assert.doesNotThrow(makeBlock(assert.deepEqual, {a: 4, b: '2'}, {a: 4, b: '2'}));
  104. assert.doesNotThrow(makeBlock(assert.deepEqual, [4], ['4']));
  105. assert.throws(makeBlock(assert.deepEqual, {a: 4}, {a: 4, b: true}),
  106. assert.AssertionError);
  107. assert.doesNotThrow(makeBlock(assert.deepEqual, ['a'], {0: 'a'}));
  108. //(although not necessarily the same order),
  109. assert.doesNotThrow(makeBlock(assert.deepEqual, {a: 4, b: '1'}, {b: '1', a: 4}));
  110. var a1 = [1, 2, 3];
  111. var a2 = [1, 2, 3];
  112. a1.a = 'test';
  113. a1.b = true;
  114. a2.b = true;
  115. a2.a = 'test';
  116. assert.throws(makeBlock(assert.deepEqual, keys(a1), keys(a2)),
  117. assert.AssertionError);
  118. assert.doesNotThrow(makeBlock(assert.deepEqual, a1, a2));
  119. });
  120. test('assert.deepEqual - ES6 primitives', function () {
  121. assert.throws(makeBlock(assert.deepEqual, null, {}), assert.AssertionError);
  122. assert.throws(makeBlock(assert.deepEqual, undefined, {}), assert.AssertionError);
  123. assert.throws(makeBlock(assert.deepEqual, 'a', ['a']), assert.AssertionError);
  124. assert.throws(makeBlock(assert.deepEqual, 'a', {0: 'a'}), assert.AssertionError);
  125. assert.throws(makeBlock(assert.deepEqual, 1, {}), assert.AssertionError);
  126. assert.throws(makeBlock(assert.deepEqual, true, {}), assert.AssertionError);
  127. if (typeof Symbol === 'symbol') {
  128. assert.throws(makeBlock(assert.deepEqual, Symbol(), {}), assert.AssertionError);
  129. }
  130. });
  131. test('assert.deepEqual - object wrappers', function () {
  132. assert.doesNotThrow(makeBlock(assert.deepEqual, new String('a'), ['a']));
  133. assert.doesNotThrow(makeBlock(assert.deepEqual, new String('a'), {0: 'a'}));
  134. assert.doesNotThrow(makeBlock(assert.deepEqual, new Number(1), {}));
  135. assert.doesNotThrow(makeBlock(assert.deepEqual, new Boolean(true), {}));
  136. });
  137. test('assert.deepEqual - Buffers', function () {
  138. assert.doesNotThrow(makeBlock(assert.deepEqual, new Buffer([1, 2, 3]), new Buffer([1, 2, 3])));
  139. if (typeof global.Uint8Array === 'function') {
  140. assert.throws(makeBlock(assert.deepEqual, new Buffer([1, 2, 3]), new Uint8Array([1, 2, 3])));
  141. }
  142. if (typeof global.Uint16Array === 'function') {
  143. assert.doesNotThrow(makeBlock(assert.deepEqual, new Uint16Array([1, 2, 3]), new Uint16Array([1, 2, 3])));
  144. }
  145. });
  146. function thrower(errorConstructor) {
  147. throw new errorConstructor('test');
  148. }
  149. test('assert - testing the throwing', function () {
  150. var aethrow = makeBlock(thrower, assert.AssertionError);
  151. aethrow = makeBlock(thrower, assert.AssertionError);
  152. // the basic calls work
  153. assert.throws(makeBlock(thrower, assert.AssertionError),
  154. assert.AssertionError, 'message');
  155. assert.throws(makeBlock(thrower, assert.AssertionError), assert.AssertionError);
  156. assert.throws(makeBlock(thrower, assert.AssertionError));
  157. // if not passing an error, catch all.
  158. assert.throws(makeBlock(thrower, TypeError));
  159. // when passing a type, only catch errors of the appropriate type
  160. var threw = false;
  161. try {
  162. assert.throws(makeBlock(thrower, TypeError), assert.AssertionError);
  163. } catch (e) {
  164. threw = true;
  165. assert.ok(e instanceof TypeError, 'type');
  166. }
  167. assert.equal(true, threw,
  168. 'a.throws with an explicit error is eating extra errors',
  169. assert.AssertionError);
  170. threw = false;
  171. // doesNotThrow should pass through all errors
  172. try {
  173. assert.doesNotThrow(makeBlock(thrower, TypeError), assert.AssertionError);
  174. } catch (e) {
  175. threw = true;
  176. assert.ok(e instanceof TypeError);
  177. }
  178. assert.equal(true, threw,
  179. 'a.doesNotThrow with an explicit error is eating extra errors');
  180. // key difference is that throwing our correct error makes an assertion error
  181. try {
  182. assert.doesNotThrow(makeBlock(thrower, TypeError), TypeError);
  183. } catch (e) {
  184. threw = true;
  185. assert.ok(e instanceof assert.AssertionError);
  186. }
  187. assert.equal(true, threw,
  188. 'a.doesNotThrow is not catching type matching errors');
  189. });
  190. test('assert.ifError', function () {
  191. assert.throws(function() {assert.ifError(new Error('test error'))});
  192. assert.doesNotThrow(function() {assert.ifError(null)});
  193. assert.doesNotThrow(function() {assert.ifError()});
  194. });
  195. test('assert - make sure that validating using constructor really works', function () {
  196. var threw = false;
  197. try {
  198. assert.throws(
  199. function() {
  200. throw ({});
  201. },
  202. Array
  203. );
  204. } catch (e) {
  205. threw = true;
  206. }
  207. assert.ok(threw, 'wrong constructor validation');
  208. });
  209. test('assert - use a RegExp to validate error message', function () {
  210. assert.throws(makeBlock(thrower, TypeError), /test/);
  211. });
  212. test('assert - use a fn to validate error object', function () {
  213. assert.throws(makeBlock(thrower, TypeError), function(err) {
  214. if ((err instanceof TypeError) && /test/.test(err)) {
  215. return true;
  216. }
  217. });
  218. });
  219. test('assert - make sure deepEqual doesn\'t loop forever on circular refs', function () {
  220. var b = {};
  221. b.b = b;
  222. var c = {};
  223. c.b = c;
  224. var gotError = false;
  225. var equal = true;
  226. try {
  227. equal = assert.deepEqual(b, c);
  228. } catch (e) {
  229. gotError = true;
  230. }
  231. assert.ok(gotError || !equal, gotError ? 'got error': 'are equal');
  232. });
  233. test('assert - ensure reflexivity of deepEqual with `arguments` objects', function() {
  234. var args = (function() { return arguments; })();
  235. assert.throws(makeBlock(assert.deepEqual, [], args), assert.AssertionError);
  236. assert.throws(makeBlock(assert.deepEqual, args, []), assert.AssertionError);
  237. });
  238. test('assert - test assertion message', function () {
  239. function testAssertionMessage(actual, expected) {
  240. try {
  241. assert.equal(actual, '');
  242. } catch (e) {
  243. assert.equal(e.toString(),
  244. ['AssertionError:', expected, '==', '\'\''].join(' '));
  245. }
  246. }
  247. testAssertionMessage(undefined, 'undefined');
  248. testAssertionMessage(null, 'null');
  249. testAssertionMessage(true, 'true');
  250. testAssertionMessage(false, 'false');
  251. testAssertionMessage(0, '0');
  252. testAssertionMessage(100, '100');
  253. testAssertionMessage(NaN, 'NaN');
  254. testAssertionMessage(Infinity, 'Infinity');
  255. testAssertionMessage(-Infinity, '-Infinity');
  256. testAssertionMessage('', '""');
  257. testAssertionMessage('foo', '\'foo\'');
  258. testAssertionMessage([], '[]');
  259. testAssertionMessage([1, 2, 3], '[ 1, 2, 3 ]');
  260. testAssertionMessage(new Buffer([1, 2, 3]), '<Buffer 01 02 03>');
  261. if (typeof global.Uint8Array === 'function' && Object.getOwnPropertyNames( new Uint8Array([])).length === 0) {
  262. // todo fix util.inspect
  263. testAssertionMessage(new Uint8Array([1, 2, 3]), '{ \'0\': 1, \'1\': 2, \'2\': 3 }');
  264. }
  265. testAssertionMessage(/a/, '/a/');
  266. testAssertionMessage(function f() {}, '[Function: f]');
  267. testAssertionMessage({}, '{}');
  268. testAssertionMessage({a: undefined, b: null}, '{ a: undefined, b: null }');
  269. testAssertionMessage({a: NaN, b: Infinity, c: -Infinity},
  270. '{ a: NaN, b: Infinity, c: -Infinity }');
  271. });
  272. test('assert - regressions from node.js testcase', function () {
  273. var threw = false;
  274. try {
  275. assert.throws(function () {
  276. assert.ifError(null);
  277. });
  278. } catch (e) {
  279. threw = true;
  280. assert.equal(e.message, 'Missing expected exception..');
  281. }
  282. assert.ok(threw);
  283. try {
  284. assert.equal(1, 2);
  285. } catch (e) {
  286. assert.equal(e.toString().split('\n')[0], 'AssertionError: 1 == 2');
  287. }
  288. try {
  289. assert.equal(1, 2, 'oh no');
  290. } catch (e) {
  291. assert.equal(e.toString().split('\n')[0], 'AssertionError: oh no');
  292. }
  293. });
  294. test('assert - strict mode', function () {
  295. var assertStrict = assert.strict;
  296. assertStrict.throws(makeBlock(assertStrict.equal, 1, true), assertStrict.AssertionError);
  297. assertStrict.notEqual(0, false);
  298. assertStrict.throws(makeBlock(assertStrict.deepEqual, 1, true), assertStrict.AssertionError);
  299. assertStrict.notDeepEqual(0, false);
  300. assertStrict.equal(assertStrict.strict, assertStrict.strict.strict);
  301. assertStrict.equal(assertStrict.equal, assertStrict.strictEqual);
  302. assertStrict.equal(assertStrict.deepEqual, assertStrict.deepStrictEqual);
  303. assertStrict.equal(assertStrict.notEqual, assertStrict.notStrictEqual);
  304. assertStrict.equal(assertStrict.notDeepEqual, assertStrict.notDeepStrictEqual);
  305. assertStrict.equal(Object.keys(assertStrict).length, Object.keys(assert).length);
  306. });
  307. }