diff options
author | Cameron Little <cameron@camlittle.com> | 2017-03-13 09:04:54 -0700 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2017-04-30 01:00:35 +0200 |
commit | 74f61e8e1fba908e05d087a1130930da925cce5b (patch) | |
tree | 52d6ae68f93164808fd52adeb3ac1f6ebaf579cc /test | |
parent | 765be6c2e14da3d4649a3a2c1ccb605d518e8e96 (diff) | |
download | android-node-v8-74f61e8e1fba908e05d087a1130930da925cce5b.tar.gz android-node-v8-74f61e8e1fba908e05d087a1130930da925cce5b.tar.bz2 android-node-v8-74f61e8e1fba908e05d087a1130930da925cce5b.zip |
test: support multiple warnings in checkWarning
This allows the common.checkWarning() test method to accept a map of
warning names to description(s), to allow testing code that generates
multiple types of warnings.
PR-URL: https://github.com/nodejs/node/pull/11640
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'test')
-rw-r--r-- | test/common.js | 42 |
1 files changed, 35 insertions, 7 deletions
diff --git a/test/common.js b/test/common.js index 2d61fb0dc5..25a2d9c1d2 100644 --- a/test/common.js +++ b/test/common.js @@ -583,17 +583,45 @@ exports.isAlive = function isAlive(pid) { } }; -exports.expectWarning = function(name, expected) { - if (typeof expected === 'string') - expected = [expected]; - process.on('warning', exports.mustCall((warning) => { +function expectWarning(name, expectedMessages) { + return exports.mustCall((warning) => { assert.strictEqual(warning.name, name); - assert.ok(expected.includes(warning.message), + assert.ok(expectedMessages.includes(warning.message), `unexpected error message: "${warning.message}"`); // Remove a warning message after it is seen so that we guarantee that we // get each message only once. - expected.splice(expected.indexOf(warning.message), 1); - }, expected.length)); + expectedMessages.splice(expectedMessages.indexOf(warning.message), 1); + }, expectedMessages.length); +} + +function expectWarningByName(name, expected) { + if (typeof expected === 'string') { + expected = [expected]; + } + process.on('warning', expectWarning(name, expected)); +} + +function expectWarningByMap(warningMap) { + const catchWarning = {}; + Object.keys(warningMap).forEach((name) => { + let expected = warningMap[name]; + if (typeof expected === 'string') { + expected = [expected]; + } + 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 +exports.expectWarning = function(nameOrMap, expected) { + if (typeof nameOrMap === 'string') { + expectWarningByName(nameOrMap, expected); + } else { + expectWarningByMap(nameOrMap); + } }; Object.defineProperty(exports, 'hasIntl', { |