summaryrefslogtreecommitdiff
path: root/test/common
diff options
context:
space:
mode:
authorRuben Bridgewater <ruben@bridgewater.de>2018-12-28 14:33:33 +0100
committerRuben Bridgewater <ruben@bridgewater.de>2019-01-10 03:22:12 +0100
commitbaa4b9b4255922f63ea2b0e3ac92005ae273e3dc (patch)
tree1bfd28d5534f53aa3525f1330236ad1f578a153a /test/common
parent284b20bf75ced9f29161275ae3b0d6eac6abe5ef (diff)
downloadandroid-node-v8-baa4b9b4255922f63ea2b0e3ac92005ae273e3dc.tar.gz
android-node-v8-baa4b9b4255922f63ea2b0e3ac92005ae273e3dc.tar.bz2
android-node-v8-baa4b9b4255922f63ea2b0e3ac92005ae273e3dc.zip
test: refactor `common.expectWarning()`
The current API is somewhat confusing at times and simpler usage is possible. This overloads the arguments further to accept objects with deprecation codes as property keys. It also adds documentation for the different possible styles. Besides that it is now going to validate for the code being present in case of deprecations but not for other cases. The former validation was not consistent as it only validated some cases and accepted undefined instead of `common.noWarnCode`. This check is removed due to the lack of consistency. `common.noWarnCode` is completely removed due to just being sugar for `undefined`. This also verifies that the warning order is identical to the order in which they are triggered. PR-URL: https://github.com/nodejs/node/pull/25251 Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test/common')
-rw-r--r--test/common/README.md52
-rw-r--r--test/common/index.js66
-rw-r--r--test/common/index.mjs2
3 files changed, 70 insertions, 50 deletions
diff --git a/test/common/README.md b/test/common/README.md
index 121bb20bb1..0542c38bbc 100644
--- a/test/common/README.md
+++ b/test/common/README.md
@@ -109,14 +109,51 @@ Indicates if there is more than 1gb of total memory.
returned function has not been called exactly `exact` number of times when the
test is complete, then the test will fail.
-### expectWarning(name, expected, code)
-* `name` [&lt;string>]
-* `expected` [&lt;string>] | [&lt;Array>]
+### expectWarning(name[, expected[, code]])
+* `name` [&lt;string>] | [&lt;Object>]
+* `expected` [&lt;string>] | [&lt;Array>] | [&lt;Object>]
* `code` [&lt;string>]
-Tests whether `name`, `expected`, and `code` are part of a raised warning. If
-an expected warning does not have a code then `common.noWarnCode` can be used
-to indicate this.
+Tests whether `name`, `expected`, and `code` are part of a raised warning.
+
+The code is required in case the name is set to `'DeprecationWarning'`.
+
+Examples:
+
+```js
+const { expectWarning } = require('../common');
+
+expectWarning('Warning', 'Foobar is really bad');
+
+expectWarning('DeprecationWarning', 'Foobar is deprecated', 'DEP0XXX');
+
+expectWarning('DeprecationWarning', [
+ 'Foobar is deprecated', 'DEP0XXX'
+]);
+
+expectWarning('DeprecationWarning', [
+ ['Foobar is deprecated', 'DEP0XXX'],
+ ['Baz is also deprecated', 'DEP0XX2']
+]);
+
+expectWarning('DeprecationWarning', {
+ DEP0XXX: 'Foobar is deprecated',
+ DEP0XX2: 'Baz is also deprecated'
+});
+
+expectWarning({
+ DeprecationWarning: {
+ DEP0XXX: 'Foobar is deprecated',
+ DEP0XX1: 'Baz is also deprecated'
+ },
+ Warning: [
+ ['Multiple array entries are fine', 'SpecialWarningCode'],
+ ['No code is also fine']
+ ],
+ SingleEntry: ['This will also work', 'WarningCode'],
+ SingleString: 'Single string entries without code will also work'
+});
+```
### getArrayBufferViews(buf)
* `buf` [&lt;Buffer>]
@@ -262,9 +299,6 @@ Returns `true` if the exit code `exitCode` and/or signal name `signal` represent
the exit code and/or signal name of a node process that aborted, `false`
otherwise.
-### noWarnCode
-See `common.expectWarning()` for usage.
-
### opensslCli
* [&lt;boolean>]
diff --git a/test/common/index.js b/test/common/index.js
index 5d9a4b31e3..ea59009ba5 100644
--- a/test/common/index.js
+++ b/test/common/index.js
@@ -497,54 +497,43 @@ function isAlive(pid) {
}
}
-function _expectWarning(name, expected) {
- const map = new Map(expected);
+function _expectWarning(name, expected, code) {
+ if (typeof expected === 'string') {
+ expected = [[expected, code]];
+ } else if (!Array.isArray(expected)) {
+ expected = Object.entries(expected).map(([a, b]) => [b, a]);
+ } else if (!(Array.isArray(expected[0]))) {
+ expected = [[expected[0], expected[1]]];
+ }
+ // Deprecation codes are mandatory, everything else is not.
+ if (name === 'DeprecationWarning') {
+ expected.forEach(([_, code]) => assert(code, expected));
+ }
return mustCall((warning) => {
+ const [ message, code ] = expected.shift();
assert.strictEqual(warning.name, name);
- assert.ok(map.has(warning.message),
- `unexpected error message: "${warning.message}"`);
- const code = map.get(warning.message);
+ assert.strictEqual(warning.message, message);
assert.strictEqual(warning.code, code);
- // Remove a warning message after it is seen so that we guarantee that we
- // get each message only once.
- map.delete(expected);
}, expected.length);
}
-function expectWarningByName(name, expected, code) {
- if (typeof expected === 'string') {
- expected = [[expected, code]];
- }
- process.on('warning', _expectWarning(name, expected));
-}
+let catchWarning;
-function expectWarningByMap(warningMap) {
- const catchWarning = {};
- Object.keys(warningMap).forEach((name) => {
- let expected = warningMap[name];
- if (!Array.isArray(expected)) {
- throw new Error('warningMap entries must be arrays consisting of two ' +
- 'entries: [message, warningCode]');
- }
- if (!(Array.isArray(expected[0]))) {
- if (expected.length === 0) {
- return;
- }
- expected = [[expected[0], expected[1]]];
- }
- catchWarning[name] = _expectWarning(name, expected);
- });
- process.on('warning', (warning) => catchWarning[warning.name](warning));
-}
-
-// Accepts a warning name and description or array of descriptions or a map
-// of warning names to description(s)
-// ensures a warning is generated for each name/description pair
+// Accepts a warning name and description or array of descriptions or a map of
+// warning names to description(s) ensures a warning is generated for each
+// name/description pair.
+// The expected messages have to be unique per `expectWarning()` call.
function expectWarning(nameOrMap, expected, code) {
+ if (catchWarning === undefined) {
+ catchWarning = {};
+ process.on('warning', (warning) => catchWarning[warning.name](warning));
+ }
if (typeof nameOrMap === 'string') {
- expectWarningByName(nameOrMap, expected, code);
+ catchWarning[nameOrMap] = _expectWarning(nameOrMap, expected, code);
} else {
- expectWarningByMap(nameOrMap);
+ Object.keys(nameOrMap).forEach((name) => {
+ catchWarning[name] = _expectWarning(name, nameOrMap[name]);
+ });
}
}
@@ -758,7 +747,6 @@ module.exports = {
mustCallAtLeast,
mustNotCall,
nodeProcessAborted,
- noWarnCode: undefined,
PIPE,
platformTimeout,
printSkipMessage,
diff --git a/test/common/index.mjs b/test/common/index.mjs
index 068dd35049..de9119f37e 100644
--- a/test/common/index.mjs
+++ b/test/common/index.mjs
@@ -37,7 +37,6 @@ const {
nodeProcessAborted,
busyLoop,
isAlive,
- noWarnCode,
expectWarning,
expectsError,
skipIfInspectorDisabled,
@@ -84,7 +83,6 @@ export {
nodeProcessAborted,
busyLoop,
isAlive,
- noWarnCode,
expectWarning,
expectsError,
skipIfInspectorDisabled,