| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent b916035 commit d831dc1
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -425,6 +425,42 @@ parameter is undefined, a default error message is assigned. If the `message` | |||
| 425 | 425 | parameter is an instance of an [`Error`][] then it will be thrown instead of the | |
| 426 | 426 | `AssertionError`. | |
| 427 | 427 | ||
| 428 | + ## `assert.doesNotMatch(string, regexp[, message])` | ||
| 429 | + <!-- YAML | ||
| 430 | + added: REPLACEME | ||
| 431 | + --> | ||
| 432 | + | ||
| 433 | + * `string` {string} | ||
| 434 | + * `regexp` {RegExp} | ||
| 435 | + * `message` {string|Error} | ||
| 436 | + | ||
| 437 | + > Stability: 1 - Experimental | ||
| 438 | + | ||
| 439 | + Expects the `string` input not to match the regular expression. | ||
| 440 | + | ||
| 441 | + This feature is currently experimental and the name might change or it might be | ||
| 442 | + completely removed again. | ||
| 443 | + | ||
| 444 | + ```js | ||
| 445 | + const assert = require('assert').strict; | ||
| 446 | + | ||
| 447 | + assert.doesNotMatch('I will fail', /fail/); | ||
| 448 | + // AssertionError [ERR_ASSERTION]: The input was expected to not match the ... | ||
| 449 | + | ||
| 450 | + assert.doesNotMatch(123, /pass/); | ||
| 451 | + // AssertionError [ERR_ASSERTION]: The "string" argument must be of type string. | ||
| 452 | + | ||
| 453 | + assert.doesNotMatch('I will pass', /different/); | ||
| 454 | + // OK | ||
| 455 | + ``` | ||
| 456 | + | ||
| 457 | + If the values do match, or if the `string` argument is of another type than | ||
| 458 | + `string`, an [`AssertionError`][] is thrown with a `message` property set equal | ||
| 459 | + to the value of the `message` parameter. If the `message` parameter is | ||
| 460 | + undefined, a default error message is assigned. If the `message` parameter is an | ||
| 461 | + instance of an [`Error`][] then it will be thrown instead of the | ||
| 462 | + [`AssertionError`][]. | ||
| 463 | + | ||
| 428 | 464 | ## `assert.doesNotReject(asyncFn[, error][, message])` | |
| 429 | 465 | <!-- YAML | |
| 430 | 466 | added: v10.0.0 | |
@@ -728,6 +764,42 @@ let err; | |||
| 728 | 764 | // at errorFrame | |
| 729 | 765 | ``` | |
| 730 | 766 | ||
| 767 | + ## `assert.match(string, regexp[, message])` | ||
| 768 | + <!-- YAML | ||
| 769 | + added: REPLACEME | ||
| 770 | + --> | ||
| 771 | + | ||
| 772 | + * `string` {string} | ||
| 773 | + * `regexp` {RegExp} | ||
| 774 | + * `message` {string|Error} | ||
| 775 | + | ||
| 776 | + > Stability: 1 - Experimental | ||
| 777 | + | ||
| 778 | + Expects the `string` input to match the regular expression. | ||
| 779 | + | ||
| 780 | + This feature is currently experimental and the name might change or it might be | ||
| 781 | + completely removed again. | ||
| 782 | + | ||
| 783 | + ```js | ||
| 784 | + const assert = require('assert').strict; | ||
| 785 | + | ||
| 786 | + assert.match('I will fail', /pass/); | ||
| 787 | + // AssertionError [ERR_ASSERTION]: The input did not match the regular ... | ||
| 788 | + | ||
| 789 | + assert.match(123, /pass/); | ||
| 790 | + // AssertionError [ERR_ASSERTION]: The "string" argument must be of type string. | ||
| 791 | + | ||
| 792 | + assert.match('I will pass', /pass/); | ||
| 793 | + // OK | ||
| 794 | + ``` | ||
| 795 | + | ||
| 796 | + If the values do not match, or if the `string` argument is of another type than | ||
| 797 | + `string`, an [`AssertionError`][] is thrown with a `message` property set equal | ||
| 798 | + to the value of the `message` parameter. If the `message` parameter is | ||
| 799 | + undefined, a default error message is assigned. If the `message` parameter is an | ||
| 800 | + instance of an [`Error`][] then it will be thrown instead of the | ||
| 801 | + [`AssertionError`][]. | ||
| 802 | + | ||
| 731 | 803 | ## `assert.notDeepEqual(actual, expected[, message])` | |
| 732 | 804 | <!-- YAML | |
| 733 | 805 | added: v0.1.21 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,6 +25,7 @@ const { | |||
| 25 | 25 | ObjectIs, | |
| 26 | 26 | ObjectKeys, | |
| 27 | 27 | ObjectPrototypeIsPrototypeOf, | |
| 28 | + RegExpPrototypeTest, | ||
| 28 | 29 | } = primordials; | |
| 29 | 30 | ||
| 30 | 31 | const { Buffer } = require('buffer'); | |
@@ -532,7 +533,7 @@ class Comparison { | |||
| 532 | 533 | if (actual !== undefined && | |
| 533 | 534 | typeof actual[key] === 'string' && | |
| 534 | 535 | isRegExp(obj[key]) && | |
| 535 | - obj[key].test(actual[key])) { | ||
| 536 | + RegExpPrototypeTest(obj[key], actual[key])) { | ||
| 536 | 537 | this[key] = actual[key]; | |
| 537 | 538 | } else { | |
| 538 | 539 | this[key] = obj[key]; | |
@@ -578,7 +579,7 @@ function expectedException(actual, expected, message, fn) { | |||
| 578 | 579 | // Handle regular expressions. | |
| 579 | 580 | if (isRegExp(expected)) { | |
| 580 | 581 | const str = String(actual); | |
| 581 | - if (expected.test(str)) | ||
| 582 | + if (RegExpPrototypeTest(expected, str)) | ||
| 582 | 583 | return; | |
| 583 | 584 | ||
| 584 | 585 | if (!message) { | |
@@ -613,7 +614,7 @@ function expectedException(actual, expected, message, fn) { | |||
| 613 | 614 | for (const key of keys) { | |
| 614 | 615 | if (typeof actual[key] === 'string' && | |
| 615 | 616 | isRegExp(expected[key]) && | |
| 616 | - expected[key].test(actual[key])) { | ||
| 617 | + RegExpPrototypeTest(expected[key], actual[key])) { | ||
| 617 | 618 | continue; | |
| 618 | 619 | } | |
| 619 | 620 | compareExceptionKey(actual, expected, key, message, keys, fn); | |
@@ -779,7 +780,7 @@ function hasMatchingError(actual, expected) { | |||
| 779 | 780 | if (typeof expected !== 'function') { | |
| 780 | 781 | if (isRegExp(expected)) { | |
| 781 | 782 | const str = String(actual); | |
| 782 | - return expected.test(str); | ||
| 783 | + return RegExpPrototypeTest(expected, str); | ||
| 783 | 784 | } | |
| 784 | 785 | throw new ERR_INVALID_ARG_TYPE( | |
| 785 | 786 | 'expected', ['Function', 'RegExp'], expected | |
@@ -884,6 +885,49 @@ assert.ifError = function ifError(err) { | |||
| 884 | 885 | } | |
| 885 | 886 | }; | |
| 886 | 887 | ||
| 888 | + function internalMatch(string, regexp, message, fn) { | ||
| 889 | + if (!isRegExp(regexp)) { | ||
| 890 | + throw new ERR_INVALID_ARG_TYPE( | ||
| 891 | + 'regexp', 'RegExp', regexp | ||
| 892 | + ); | ||
| 893 | + } | ||
| 894 | + const match = fn.name === 'match'; | ||
| 895 | + if (typeof string !== 'string' || | ||
| 896 | + RegExpPrototypeTest(regexp, string) !== match) { | ||
| 897 | + if (message instanceof Error) { | ||
| 898 | + throw message; | ||
| 899 | + } | ||
| 900 | + | ||
| 901 | + const generatedMessage = !message; | ||
| 902 | + | ||
| 903 | + // 'The input was expected to not match the regular expression ' + | ||
| 904 | + message = message || (typeof string !== 'string' ? | ||
| 905 | + 'The "string" argument must be of type string. Received type ' + | ||
| 906 | + `${typeof string} (${inspect(string)})` : | ||
| 907 | + (match ? | ||
| 908 | + 'The input did not match the regular expression ' : | ||
| 909 | + 'The input was expected to not match the regular expression ') + | ||
| 910 | + `${inspect(regexp)}. Input:\n\n${inspect(string)}\n`); | ||
| 911 | + const err = new AssertionError({ | ||
| 912 | + actual: string, | ||
| 913 | + expected: regexp, | ||
| 914 | + message, | ||
| 915 | + operator: fn.name, | ||
| 916 | + stackStartFn: fn | ||
| 917 | + }); | ||
| 918 | + err.generatedMessage = generatedMessage; | ||
| 919 | + throw err; | ||
| 920 | + } | ||
| 921 | + } | ||
| 922 | + | ||
| 923 | + assert.match = function match(string, regexp, message) { | ||
| 924 | + internalMatch(string, regexp, message, match); | ||
| 925 | + }; | ||
| 926 | + | ||
| 927 | + assert.doesNotMatch = function doesNotMatch(string, regexp, message) { | ||
| 928 | + internalMatch(string, regexp, message, doesNotMatch); | ||
| 929 | + }; | ||
| 930 | + | ||
| 887 | 931 | // Expose a strict only variant of assert | |
| 888 | 932 | function strict(...args) { | |
| 889 | 933 | innerOk(strict, args.length, ...args); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1361,3 +1361,103 @@ assert.throws( | |||
| 1361 | 1361 | 'prototype.\n\nError message:\n\nfoobar' | |
| 1362 | 1362 | } | |
| 1363 | 1363 | ); | |
| 1364 | + | ||
| 1365 | + // Multiple assert.match() tests. | ||
| 1366 | + { | ||
| 1367 | + assert.throws( | ||
| 1368 | + () => assert.match(/abc/, 'string'), | ||
| 1369 | + { | ||
| 1370 | + code: 'ERR_INVALID_ARG_TYPE', | ||
| 1371 | + message: 'The "regexp" argument must be an instance of RegExp. ' + | ||
| 1372 | + "Received type string ('string')" | ||
| 1373 | + } | ||
| 1374 | + ); | ||
| 1375 | + assert.throws( | ||
| 1376 | + () => assert.match('string', /abc/), | ||
| 1377 | + { | ||
| 1378 | + actual: 'string', | ||
| 1379 | + expected: /abc/, | ||
| 1380 | + operator: 'match', | ||
| 1381 | + message: 'The input did not match the regular expression /abc/. ' + | ||
| 1382 | + "Input:\n\n'string'\n", | ||
| 1383 | + generatedMessage: true | ||
| 1384 | + } | ||
| 1385 | + ); | ||
| 1386 | + assert.throws( | ||
| 1387 | + () => assert.match('string', /abc/, 'foobar'), | ||
| 1388 | + { | ||
| 1389 | + actual: 'string', | ||
| 1390 | + expected: /abc/, | ||
| 1391 | + operator: 'match', | ||
| 1392 | + message: 'foobar', | ||
| 1393 | + generatedMessage: false | ||
| 1394 | + } | ||
| 1395 | + ); | ||
| 1396 | + const errorMessage = new RangeError('foobar'); | ||
| 1397 | + assert.throws( | ||
| 1398 | + () => assert.match('string', /abc/, errorMessage), | ||
| 1399 | + errorMessage | ||
| 1400 | + ); | ||
| 1401 | + assert.throws( | ||
| 1402 | + () => assert.match({ abc: 123 }, /abc/), | ||
| 1403 | + { | ||
| 1404 | + actual: { abc: 123 }, | ||
| 1405 | + expected: /abc/, | ||
| 1406 | + operator: 'match', | ||
| 1407 | + message: 'The "string" argument must be of type string. ' + | ||
| 1408 | + 'Received type object ({ abc: 123 })', | ||
| 1409 | + generatedMessage: true | ||
| 1410 | + } | ||
| 1411 | + ); | ||
| 1412 | + assert.match('I will pass', /pass$/); | ||
| 1413 | + } | ||
| 1414 | + | ||
| 1415 | + // Multiple assert.doesNotMatch() tests. | ||
| 1416 | + { | ||
| 1417 | + assert.throws( | ||
| 1418 | + () => assert.doesNotMatch(/abc/, 'string'), | ||
| 1419 | + { | ||
| 1420 | + code: 'ERR_INVALID_ARG_TYPE', | ||
| 1421 | + message: 'The "regexp" argument must be an instance of RegExp. ' + | ||
| 1422 | + "Received type string ('string')" | ||
| 1423 | + } | ||
| 1424 | + ); | ||
| 1425 | + assert.throws( | ||
| 1426 | + () => assert.doesNotMatch('string', /string/), | ||
| 1427 | + { | ||
| 1428 | + actual: 'string', | ||
| 1429 | + expected: /string/, | ||
| 1430 | + operator: 'doesNotMatch', | ||
| 1431 | + message: 'The input was expected to not match the regular expression ' + | ||
| 1432 | + "/string/. Input:\n\n'string'\n", | ||
| 1433 | + generatedMessage: true | ||
| 1434 | + } | ||
| 1435 | + ); | ||
| 1436 | + assert.throws( | ||
| 1437 | + () => assert.doesNotMatch('string', /string/, 'foobar'), | ||
| 1438 | + { | ||
| 1439 | + actual: 'string', | ||
| 1440 | + expected: /string/, | ||
| 1441 | + operator: 'doesNotMatch', | ||
| 1442 | + message: 'foobar', | ||
| 1443 | + generatedMessage: false | ||
| 1444 | + } | ||
| 1445 | + ); | ||
| 1446 | + const errorMessage = new RangeError('foobar'); | ||
| 1447 | + assert.throws( | ||
| 1448 | + () => assert.doesNotMatch('string', /string/, errorMessage), | ||
| 1449 | + errorMessage | ||
| 1450 | + ); | ||
| 1451 | + assert.throws( | ||
| 1452 | + () => assert.doesNotMatch({ abc: 123 }, /abc/), | ||
| 1453 | + { | ||
| 1454 | + actual: { abc: 123 }, | ||
| 1455 | + expected: /abc/, | ||
| 1456 | + operator: 'doesNotMatch', | ||
| 1457 | + message: 'The "string" argument must be of type string. ' + | ||
| 1458 | + 'Received type object ({ abc: 123 })', | ||
| 1459 | + generatedMessage: true | ||
| 1460 | + } | ||
| 1461 | + ); | ||
| 1462 | + assert.doesNotMatch('I will pass', /different$/); | ||
| 1463 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments