| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 78f0c2a commit 26785a2
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,12 +20,11 @@ | |||
| 20 | 20 | ||
| 21 | 21 | 'use strict'; | |
| 22 | 22 | ||
| 23 | - // UTILITY | ||
| 24 | - const compare = process.binding('buffer').compare; | ||
| 23 | + const { compare } = process.binding('buffer'); | ||
| 25 | 24 | const util = require('util'); | |
| 26 | 25 | const { isSet, isMap } = process.binding('util'); | |
| 27 | - const objectToString = require('internal/util').objectToString; | ||
| 28 | - const Buffer = require('buffer').Buffer; | ||
| 26 | + const { objectToString } = require('internal/util'); | ||
| 27 | + const { Buffer } = require('buffer'); | ||
| 29 | 28 | ||
| 30 | 29 | var errors; | |
| 31 | 30 | function lazyErrors() { | |
@@ -47,26 +46,28 @@ const assert = module.exports = ok; | |||
| 47 | 46 | ||
| 48 | 47 | // All of the following functions must throw an AssertionError | |
| 49 | 48 | // when a corresponding condition is not met, with a message that | |
| 50 | - // may be undefined if not provided. All assertion methods provide | ||
| 49 | + // may be undefined if not provided. All assertion methods provide | ||
| 51 | 50 | // both the actual and expected values to the assertion error for | |
| 52 | 51 | // display purposes. | |
| 53 | 52 | ||
| 53 | + function innerFail(actual, expected, message, operator, stackStartFunction) { | ||
| 54 | + const errors = lazyErrors(); | ||
| 55 | + throw new errors.AssertionError({ | ||
| 56 | + message, | ||
| 57 | + actual, | ||
| 58 | + expected, | ||
| 59 | + operator, | ||
| 60 | + stackStartFunction | ||
| 61 | + }); | ||
| 62 | + } | ||
| 63 | + | ||
| 54 | 64 | function fail(actual, expected, message, operator, stackStartFunction) { | |
| 55 | 65 | if (arguments.length === 1) | |
| 56 | 66 | message = actual; | |
| 57 | 67 | if (arguments.length === 2) | |
| 58 | 68 | operator = '!='; | |
| 59 | - const errors = lazyErrors(); | ||
| 60 | - throw new errors.AssertionError({ | ||
| 61 | - message: message, | ||
| 62 | - actual: actual, | ||
| 63 | - expected: expected, | ||
| 64 | - operator: operator, | ||
| 65 | - stackStartFunction: stackStartFunction | ||
| 66 | - }); | ||
| 69 | + innerFail(actual, expected, message, operator, stackStartFunction || fail); | ||
| 67 | 70 | } | |
| 68 | - | ||
| 69 | - // EXTENSION! allows for well behaved errors defined elsewhere. | ||
| 70 | 71 | assert.fail = fail; | |
| 71 | 72 | ||
| 72 | 73 | // The AssertionError is defined in internal/error. | |
@@ -77,50 +78,39 @@ assert.AssertionError = lazyErrors().AssertionError; | |||
| 77 | 78 | ||
| 78 | 79 | ||
| 79 | 80 | // Pure assertion tests whether a value is truthy, as determined | |
| 80 | - // by !!guard. | ||
| 81 | - // assert.ok(guard, message_opt); | ||
| 82 | - // This statement is equivalent to assert.equal(true, !!guard, | ||
| 83 | - // message_opt);. To test strictly for the value true, use | ||
| 84 | - // assert.strictEqual(true, guard, message_opt);. | ||
| 85 | - | ||
| 81 | + // by !!value. | ||
| 86 | 82 | function ok(value, message) { | |
| 87 | - if (!value) fail(value, true, message, '==', assert.ok); | ||
| 83 | + if (!value) innerFail(value, true, message, '==', ok); | ||
| 88 | 84 | } | |
| 89 | 85 | assert.ok = ok; | |
| 90 | 86 | ||
| 91 | - // The equality assertion tests shallow, coercive equality with | ||
| 92 | - // ==. | ||
| 93 | - // assert.equal(actual, expected, message_opt); | ||
| 87 | + // The equality assertion tests shallow, coercive equality with ==. | ||
| 94 | 88 | /* eslint-disable no-restricted-properties */ | |
| 95 | 89 | assert.equal = function equal(actual, expected, message) { | |
| 96 | 90 | // eslint-disable-next-line eqeqeq | |
| 97 | - if (actual != expected) fail(actual, expected, message, '==', assert.equal); | ||
| 91 | + if (actual != expected) innerFail(actual, expected, message, '==', equal); | ||
| 98 | 92 | }; | |
| 99 | 93 | ||
| 100 | 94 | // The non-equality assertion tests for whether two objects are not | |
| 101 | 95 | // equal with !=. | |
| 102 | - // assert.notEqual(actual, expected, message_opt); | ||
| 103 | - | ||
| 104 | 96 | assert.notEqual = function notEqual(actual, expected, message) { | |
| 105 | 97 | // eslint-disable-next-line eqeqeq | |
| 106 | 98 | if (actual == expected) { | |
| 107 | - fail(actual, expected, message, '!=', assert.notEqual); | ||
| 99 | + innerFail(actual, expected, message, '!=', notEqual); | ||
| 108 | 100 | } | |
| 109 | 101 | }; | |
| 110 | 102 | ||
| 111 | 103 | // The equivalence assertion tests a deep equality relation. | |
| 112 | - // assert.deepEqual(actual, expected, message_opt); | ||
| 113 | - | ||
| 114 | 104 | assert.deepEqual = function deepEqual(actual, expected, message) { | |
| 115 | - if (!_deepEqual(actual, expected, false)) { | ||
| 116 | - fail(actual, expected, message, 'deepEqual', assert.deepEqual); | ||
| 105 | + if (!innerDeepEqual(actual, expected, false)) { | ||
| 106 | + innerFail(actual, expected, message, 'deepEqual', deepEqual); | ||
| 117 | 107 | } | |
| 118 | 108 | }; | |
| 119 | 109 | /* eslint-enable */ | |
| 120 | 110 | ||
| 121 | 111 | assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) { | |
| 122 | - if (!_deepEqual(actual, expected, true)) { | ||
| 123 | - fail(actual, expected, message, 'deepStrictEqual', assert.deepStrictEqual); | ||
| 112 | + if (!innerDeepEqual(actual, expected, true)) { | ||
| 113 | + innerFail(actual, expected, message, 'deepStrictEqual', deepStrictEqual); | ||
| 124 | 114 | } | |
| 125 | 115 | }; | |
| 126 | 116 | ||
@@ -149,7 +139,7 @@ function isArguments(tag) { | |||
| 149 | 139 | return tag === '[object Arguments]'; | |
| 150 | 140 | } | |
| 151 | 141 | ||
| 152 | - function _deepEqual(actual, expected, strict, memos) { | ||
| 142 | + function innerDeepEqual(actual, expected, strict, memos) { | ||
| 153 | 143 | // All identical values are equivalent, as determined by ===. | |
| 154 | 144 | if (actual === expected) { | |
| 155 | 145 | return true; | |
@@ -302,7 +292,7 @@ function setHasSimilarElement(set, val1, usedEntries, strict, memo) { | |||
| 302 | 292 | if (usedEntries && usedEntries.has(val2)) | |
| 303 | 293 | continue; | |
| 304 | 294 | ||
| 305 | - if (_deepEqual(val1, val2, strict, memo)) { | ||
| 295 | + if (innerDeepEqual(val1, val2, strict, memo)) { | ||
| 306 | 296 | if (usedEntries) | |
| 307 | 297 | usedEntries.add(val2); | |
| 308 | 298 | return true; | |
@@ -359,7 +349,7 @@ function mapHasSimilarEntry(map, key1, item1, usedEntries, strict, memo) { | |||
| 359 | 349 | // This check is not strictly necessary. The loop performs this check, but | |
| 360 | 350 | // doing it here improves performance of the common case when reference-equal | |
| 361 | 351 | // keys exist (which includes all primitive-valued keys). | |
| 362 | - if (map.has(key1) && _deepEqual(item1, map.get(key1), strict, memo)) { | ||
| 352 | + if (map.has(key1) && innerDeepEqual(item1, map.get(key1), strict, memo)) { | ||
| 363 | 353 | if (usedEntries) | |
| 364 | 354 | usedEntries.add(key1); | |
| 365 | 355 | return true; | |
@@ -376,8 +366,8 @@ function mapHasSimilarEntry(map, key1, item1, usedEntries, strict, memo) { | |||
| 376 | 366 | if (usedEntries && usedEntries.has(key2)) | |
| 377 | 367 | continue; | |
| 378 | 368 | ||
| 379 | - if (_deepEqual(key1, key2, strict, memo) && | ||
| 380 | - _deepEqual(item1, item2, strict, memo)) { | ||
| 369 | + if (innerDeepEqual(key1, key2, strict, memo) && | ||
| 370 | + innerDeepEqual(item1, item2, strict, memo)) { | ||
| 381 | 371 | if (usedEntries) | |
| 382 | 372 | usedEntries.add(key2); | |
| 383 | 373 | return true; | |
@@ -454,44 +444,39 @@ function objEquiv(a, b, strict, actualVisitedObjects) { | |||
| 454 | 444 | // Possibly expensive deep test: | |
| 455 | 445 | for (i = aKeys.length - 1; i >= 0; i--) { | |
| 456 | 446 | key = aKeys[i]; | |
| 457 | - if (!_deepEqual(a[key], b[key], strict, actualVisitedObjects)) | ||
| 447 | + if (!innerDeepEqual(a[key], b[key], strict, actualVisitedObjects)) | ||
| 458 | 448 | return false; | |
| 459 | 449 | } | |
| 460 | 450 | return true; | |
| 461 | 451 | } | |
| 462 | 452 | ||
| 463 | 453 | // The non-equivalence assertion tests for any deep inequality. | |
| 464 | - // assert.notDeepEqual(actual, expected, message_opt); | ||
| 465 | - | ||
| 466 | 454 | assert.notDeepEqual = function notDeepEqual(actual, expected, message) { | |
| 467 | - if (_deepEqual(actual, expected, false)) { | ||
| 468 | - fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual); | ||
| 455 | + if (innerDeepEqual(actual, expected, false)) { | ||
| 456 | + innerFail(actual, expected, message, 'notDeepEqual', notDeepEqual); | ||
| 469 | 457 | } | |
| 470 | 458 | }; | |
| 471 | 459 | ||
| 472 | 460 | assert.notDeepStrictEqual = notDeepStrictEqual; | |
| 473 | 461 | function notDeepStrictEqual(actual, expected, message) { | |
| 474 | - if (_deepEqual(actual, expected, true)) { | ||
| 475 | - fail(actual, expected, message, 'notDeepStrictEqual', notDeepStrictEqual); | ||
| 462 | + if (innerDeepEqual(actual, expected, true)) { | ||
| 463 | + innerFail(actual, expected, message, 'notDeepStrictEqual', | ||
| 464 | + notDeepStrictEqual); | ||
| 476 | 465 | } | |
| 477 | 466 | } | |
| 478 | 467 | ||
| 479 | 468 | // The strict equality assertion tests strict equality, as determined by ===. | |
| 480 | - // assert.strictEqual(actual, expected, message_opt); | ||
| 481 | - | ||
| 482 | 469 | assert.strictEqual = function strictEqual(actual, expected, message) { | |
| 483 | 470 | if (actual !== expected) { | |
| 484 | - fail(actual, expected, message, '===', assert.strictEqual); | ||
| 471 | + innerFail(actual, expected, message, '===', strictEqual); | ||
| 485 | 472 | } | |
| 486 | 473 | }; | |
| 487 | 474 | ||
| 488 | 475 | // The strict non-equality assertion tests for strict inequality, as | |
| 489 | 476 | // determined by !==. | |
| 490 | - // assert.notStrictEqual(actual, expected, message_opt); | ||
| 491 | - | ||
| 492 | 477 | assert.notStrictEqual = function notStrictEqual(actual, expected, message) { | |
| 493 | 478 | if (actual === expected) { | |
| 494 | - fail(actual, expected, message, '!==', assert.notStrictEqual); | ||
| 479 | + innerFail(actual, expected, message, '!==', notStrictEqual); | ||
| 495 | 480 | } | |
| 496 | 481 | }; | |
| 497 | 482 | ||
@@ -520,7 +505,7 @@ function expectedException(actual, expected) { | |||
| 520 | 505 | return expected.call({}, actual) === true; | |
| 521 | 506 | } | |
| 522 | 507 | ||
| 523 | - function _tryBlock(block) { | ||
| 508 | + function tryBlock(block) { | ||
| 524 | 509 | var error; | |
| 525 | 510 | try { | |
| 526 | 511 | block(); | |
@@ -530,7 +515,7 @@ function _tryBlock(block) { | |||
| 530 | 515 | return error; | |
| 531 | 516 | } | |
| 532 | 517 | ||
| 533 | - function _throws(shouldThrow, block, expected, message) { | ||
| 518 | + function innerThrows(shouldThrow, block, expected, message) { | ||
| 534 | 519 | var actual; | |
| 535 | 520 | ||
| 536 | 521 | if (typeof block !== 'function') { | |
@@ -544,13 +529,13 @@ function _throws(shouldThrow, block, expected, message) { | |||
| 544 | 529 | expected = null; | |
| 545 | 530 | } | |
| 546 | 531 | ||
| 547 | - actual = _tryBlock(block); | ||
| 532 | + actual = tryBlock(block); | ||
| 548 | 533 | ||
| 549 | 534 | message = (expected && expected.name ? ' (' + expected.name + ')' : '') + | |
| 550 | 535 | (message ? ': ' + message : '.'); | |
| 551 | 536 | ||
| 552 | 537 | if (shouldThrow && !actual) { | |
| 553 | - fail(actual, expected, 'Missing expected exception' + message); | ||
| 538 | + innerFail(actual, expected, 'Missing expected exception' + message, fail); | ||
| 554 | 539 | } | |
| 555 | 540 | ||
| 556 | 541 | const userProvidedMessage = typeof message === 'string'; | |
@@ -561,7 +546,7 @@ function _throws(shouldThrow, block, expected, message) { | |||
| 561 | 546 | userProvidedMessage && | |
| 562 | 547 | expectedException(actual, expected)) || | |
| 563 | 548 | isUnexpectedException) { | |
| 564 | - fail(actual, expected, 'Got unwanted exception' + message); | ||
| 549 | + innerFail(actual, expected, 'Got unwanted exception' + message, fail); | ||
| 565 | 550 | } | |
| 566 | 551 | ||
| 567 | 552 | if ((shouldThrow && actual && expected && | |
@@ -571,16 +556,12 @@ function _throws(shouldThrow, block, expected, message) { | |||
| 571 | 556 | } | |
| 572 | 557 | ||
| 573 | 558 | // Expected to throw an error. | |
| 574 | - // assert.throws(block, Error_opt, message_opt); | ||
| 575 | - | ||
| 576 | - assert.throws = function throws(block, /*optional*/error, /*optional*/message) { | ||
| 577 | - _throws(true, block, error, message); | ||
| 559 | + assert.throws = function throws(block, error, message) { | ||
| 560 | + innerThrows(true, block, error, message); | ||
| 578 | 561 | }; | |
| 579 | 562 | ||
| 580 | - // EXTENSION! This is annoying to write outside this module. | ||
| 581 | - assert.doesNotThrow = doesNotThrow; | ||
| 582 | - function doesNotThrow(block, /*optional*/error, /*optional*/message) { | ||
| 583 | - _throws(false, block, error, message); | ||
| 584 | - } | ||
| 563 | + assert.doesNotThrow = function doesNotThrow(block, error, message) { | ||
| 564 | + innerThrows(false, block, error, message); | ||
| 565 | + }; | ||
| 585 | 566 | ||
| 586 | 567 | assert.ifError = function ifError(err) { if (err) throw err; }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,53 +1,75 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | + | ||
| 2 | 3 | const common = require('../common'); | |
| 3 | 4 | const assert = require('assert'); | |
| 4 | 5 | ||
| 5 | - // no args | ||
| 6 | + // No args | ||
| 6 | 7 | assert.throws( | |
| 7 | 8 | () => { assert.fail(); }, | |
| 8 | 9 | common.expectsError({ | |
| 9 | 10 | code: 'ERR_ASSERTION', | |
| 10 | 11 | type: assert.AssertionError, | |
| 12 | + operator: undefined, | ||
| 13 | + actual: undefined, | ||
| 14 | + expected: undefined, | ||
| 11 | 15 | message: 'undefined undefined undefined' | |
| 12 | 16 | }) | |
| 13 | 17 | ); | |
| 14 | 18 | ||
| 15 | - // one arg = message | ||
| 19 | + // One arg = message | ||
| 16 | 20 | assert.throws( | |
| 17 | 21 | () => { assert.fail('custom message'); }, | |
| 18 | 22 | common.expectsError({ | |
| 19 | 23 | code: 'ERR_ASSERTION', | |
| 20 | 24 | type: assert.AssertionError, | |
| 25 | + operator: undefined, | ||
| 26 | + actual: undefined, | ||
| 27 | + expected: undefined, | ||
| 21 | 28 | message: 'custom message' | |
| 22 | 29 | }) | |
| 23 | 30 | ); | |
| 24 | 31 | ||
| 25 | - // two args only, operator defaults to '!=' | ||
| 32 | + // Two args only, operator defaults to '!=' | ||
| 26 | 33 | assert.throws( | |
| 27 | 34 | () => { assert.fail('first', 'second'); }, | |
| 28 | 35 | common.expectsError({ | |
| 29 | 36 | code: 'ERR_ASSERTION', | |
| 30 | 37 | type: assert.AssertionError, | |
| 38 | + operator: '!=', | ||
| 39 | + actual: 'first', | ||
| 40 | + expected: 'second', | ||
| 31 | 41 | message: '\'first\' != \'second\'' | |
| 32 | 42 | }) | |
| 33 | 43 | ); | |
| 34 | 44 | ||
| 35 | - // three args | ||
| 45 | + // Three args | ||
| 36 | 46 | assert.throws( | |
| 37 | 47 | () => { assert.fail('ignored', 'ignored', 'another custom message'); }, | |
| 38 | 48 | common.expectsError({ | |
| 39 | 49 | code: 'ERR_ASSERTION', | |
| 40 | 50 | type: assert.AssertionError, | |
| 51 | + operator: undefined, | ||
| 52 | + actual: 'ignored', | ||
| 53 | + expected: 'ignored', | ||
| 41 | 54 | message: 'another custom message' | |
| 42 | 55 | }) | |
| 43 | 56 | ); | |
| 44 | 57 | ||
| 45 | - // no third arg (but a fourth arg) | ||
| 58 | + // No third arg (but a fourth arg) | ||
| 46 | 59 | assert.throws( | |
| 47 | 60 | () => { assert.fail('first', 'second', undefined, 'operator'); }, | |
| 48 | 61 | common.expectsError({ | |
| 49 | 62 | code: 'ERR_ASSERTION', | |
| 50 | 63 | type: assert.AssertionError, | |
| 64 | + operator: 'operator', | ||
| 65 | + actual: 'first', | ||
| 66 | + expected: 'second', | ||
| 51 | 67 | message: '\'first\' operator \'second\'' | |
| 52 | 68 | }) | |
| 53 | 69 | ); | |
| 70 | + | ||
| 71 | + // The stackFrameFunction should exclude the foo frame | ||
| 72 | + assert.throws( | ||
| 73 | + function foo() { assert.fail('first', 'second', 'message', '!==', foo); }, | ||
| 74 | + (err) => !/foo/m.test(err.stack) | ||
| 75 | + ); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments