diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2018-05-02 19:26:32 +0200 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2018-05-10 13:17:22 +0200 |
commit | cf7be86cd927c99547f4774d79ea6d12d621033f (patch) | |
tree | fa2117ec46b92ea7e852898f340dec76131c1128 /lib | |
parent | 81b99da96e9a27fc7e55e002656968d99596a31d (diff) | |
download | android-node-v8-cf7be86cd927c99547f4774d79ea6d12d621033f.tar.gz android-node-v8-cf7be86cd927c99547f4774d79ea6d12d621033f.tar.bz2 android-node-v8-cf7be86cd927c99547f4774d79ea6d12d621033f.zip |
assert: accept regular expressions to validate
This makes sure regular expressions on validation objects validate
against strings when used with `assert.throws` and `assert.rejects`.
PR-URL: https://github.com/nodejs/node/pull/20485
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaƫl Zasso <targos@protonmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/assert.js | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/lib/assert.js b/lib/assert.js index 05b43b5b54..ac9fd30793 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -35,7 +35,7 @@ const { } } = require('internal/errors'); const { openSync, closeSync, readSync } = require('fs'); -const { inspect, types: { isPromise } } = require('util'); +const { inspect, types: { isPromise, isRegExp } } = require('util'); const { EOL } = require('os'); const { NativeModule } = require('internal/bootstrap/loaders'); @@ -362,10 +362,18 @@ assert.notStrictEqual = function notStrictEqual(actual, expected, message) { }; class Comparison { - constructor(obj, keys) { + constructor(obj, keys, actual) { for (const key of keys) { - if (key in obj) - this[key] = obj[key]; + if (key in obj) { + if (actual !== undefined && + typeof actual[key] === 'string' && + isRegExp(obj[key]) && + obj[key].test(actual[key])) { + this[key] = actual[key]; + } else { + this[key] = obj[key]; + } + } } } } @@ -375,7 +383,7 @@ function compareExceptionKey(actual, expected, key, message, keys) { if (!message) { // Create placeholder objects to create a nice output. const a = new Comparison(actual, keys); - const b = new Comparison(expected, keys); + const b = new Comparison(expected, keys, actual); const tmpLimit = Error.stackTraceLimit; Error.stackTraceLimit = 0; @@ -415,6 +423,11 @@ function expectedException(actual, expected, msg) { keys.push('name', 'message'); } for (const key of keys) { + if (typeof actual[key] === 'string' && + isRegExp(expected[key]) && + expected[key].test(actual[key])) { + continue; + } compareExceptionKey(actual, expected, key, msg, keys); } return true; |