From fccc0bf6e68981f6225315875524e1a0b56fcc20 Mon Sep 17 00:00:00 2001 From: Refael Ackermann Date: Tue, 9 May 2017 17:16:52 -0400 Subject: [PATCH] test: add mustCallAtLeast PR-URL: https://github.com/nodejs/node/pull/12935 Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: James M Snell --- test/common/README.md | 17 +++++++++--- test/common/index.js | 35 +++++++++++++++++-------- test/fixtures/failmustcall1.js | 3 +++ test/fixtures/failmustcall2.js | 3 +++ test/parallel/test-common.js | 47 +++++++++++++++++++++++++++++++--- 5 files changed, 88 insertions(+), 17 deletions(-) create mode 100644 test/fixtures/failmustcall1.js create mode 100644 test/fixtures/failmustcall2.js diff --git a/test/common/README.md b/test/common/README.md index 267d28140c..492b5acdea 100644 --- a/test/common/README.md +++ b/test/common/README.md @@ -182,9 +182,9 @@ Gets IP of localhost Array of IPV6 hosts. -### mustCall([fn][, expected]) -* fn [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) -* expected [<Number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type) default = 1 +### mustCall([fn][, exact]) +* `fn` [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) default = `common.noop` +* `exact` [<Number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type) default = 1 * return [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) Returns a function that calls `fn`. If the returned function has not been called @@ -193,6 +193,17 @@ fail. If `fn` is not provided, `common.noop` will be used. +### mustCallAtLeast([fn][, minimum]) +* `fn` [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) default = `common.noop` +* `minimum` [<Number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type) default = 1 +* return [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) + +Returns a function that calls `fn`. If the returned function has not been called +at least `minimum` number of times when the test is complete, then the test will +fail. + +If `fn` is not provided, `common.noop` will be used. + ### nodeProcessAborted(exitCode, signal) * `exitCode` [<Number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type) * `signal` [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) diff --git a/test/common/index.js b/test/common/index.js index cb8d333a28..d657e36f06 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -459,13 +459,19 @@ function runCallChecks(exitCode) { if (exitCode !== 0) return; const failed = mustCallChecks.filter(function(context) { - return context.actual !== context.expected; + if ('minimum' in context) { + context.messageSegment = `at least ${context.minimum}`; + return context.actual < context.minimum; + } else { + context.messageSegment = `exactly ${context.exact}`; + return context.actual !== context.exact; + } }); failed.forEach(function(context) { - console.log('Mismatched %s function calls. Expected %d, actual %d.', + console.log('Mismatched %s function calls. Expected %s, actual %d.', context.name, - context.expected, + context.messageSegment, context.actual); console.log(context.stack.split('\n').slice(2).join('\n')); }); @@ -473,22 +479,29 @@ function runCallChecks(exitCode) { if (failed.length) process.exit(1); } +exports.mustCall = function(fn, exact) { + return _mustCallInner(fn, exact, 'exact'); +}; -exports.mustCall = function(fn, expected) { +exports.mustCallAtLeast = function(fn, minimum) { + return _mustCallInner(fn, minimum, 'minimum'); +}; + +function _mustCallInner(fn, criteria, field) { if (typeof fn === 'number') { - expected = fn; + criteria = fn; fn = noop; } else if (fn === undefined) { fn = noop; } - if (expected === undefined) - expected = 1; - else if (typeof expected !== 'number') - throw new TypeError(`Invalid expected value: ${expected}`); + if (criteria === undefined) + criteria = 1; + else if (typeof criteria !== 'number') + throw new TypeError(`Invalid ${field} value: ${criteria}`); const context = { - expected: expected, + [field]: criteria, actual: 0, stack: (new Error()).stack, name: fn.name || '' @@ -503,7 +516,7 @@ exports.mustCall = function(fn, expected) { context.actual++; return fn.apply(this, arguments); }; -}; +} exports.hasMultiLocalhost = function hasMultiLocalhost() { const TCP = process.binding('tcp_wrap').TCP; diff --git a/test/fixtures/failmustcall1.js b/test/fixtures/failmustcall1.js new file mode 100644 index 0000000000..a54740eb29 --- /dev/null +++ b/test/fixtures/failmustcall1.js @@ -0,0 +1,3 @@ +const common = require('../common'); +const f = common.mustCall( () => {}, 2); +f(); diff --git a/test/fixtures/failmustcall2.js b/test/fixtures/failmustcall2.js new file mode 100644 index 0000000000..89f8d23922 --- /dev/null +++ b/test/fixtures/failmustcall2.js @@ -0,0 +1,3 @@ +const common = require('../common'); +const f = common.mustCallAtLeast(() => {}, 2); +f(); diff --git a/test/parallel/test-common.js b/test/parallel/test-common.js index 2bda20acd9..0a4e1d72f0 100644 --- a/test/parallel/test-common.js +++ b/test/parallel/test-common.js @@ -22,7 +22,8 @@ 'use strict'; const common = require('../common'); const assert = require('assert'); - +const {join} = require('path'); +const {execFile} = require('child_process'); // test for leaked global detection global.gc = 42; // Not a valid global unless --expose_gc is set. @@ -33,12 +34,15 @@ delete global.gc; // common.mustCall() tests assert.throws(function() { common.mustCall(function() {}, 'foo'); -}, /^TypeError: Invalid expected value: foo$/); +}, /^TypeError: Invalid exact value: foo$/); assert.throws(function() { common.mustCall(function() {}, /foo/); -}, /^TypeError: Invalid expected value: \/foo\/$/); +}, /^TypeError: Invalid exact value: \/foo\/$/); +assert.throws(function() { + common.mustCallAtLeast(function() {}, /foo/); +}, /^TypeError: Invalid minimum value: \/foo\/$/); // assert.fail() tests assert.throws( @@ -47,3 +51,40 @@ assert.throws( code: 'ERR_ASSERTION', message: /^fhqwhgads$/ })); + +const fnOnce = common.mustCall(() => {}); +fnOnce(); +const fnTwice = common.mustCall(() => {}, 2); +fnTwice(); +fnTwice(); +const fnAtLeast1Called1 = common.mustCallAtLeast(() => {}, 1); +fnAtLeast1Called1(); +const fnAtLeast1Called2 = common.mustCallAtLeast(() => {}, 1); +fnAtLeast1Called2(); +fnAtLeast1Called2(); +const fnAtLeast2Called2 = common.mustCallAtLeast(() => {}, 2); +fnAtLeast2Called2(); +fnAtLeast2Called2(); +const fnAtLeast2Called3 = common.mustCallAtLeast(() => {}, 2); +fnAtLeast2Called3(); +fnAtLeast2Called3(); +fnAtLeast2Called3(); + +const failFixtures = [ + [ + join(common.fixturesDir, 'failmustcall1.js'), + 'Mismatched function calls. Expected exactly 2, actual 1.' + ], [ + join(common.fixturesDir, 'failmustcall2.js'), + 'Mismatched function calls. Expected at least 2, actual 1.' + ] +]; +for (const p of failFixtures) { + const [file, expected] = p; + execFile(process.argv[0], [file], common.mustCall((ex, stdout, stderr) => { + assert.ok(ex); + assert.strictEqual(stderr, ''); + const firstLine = stdout.split('\n').shift(); + assert.strictEqual(firstLine, expected); + })); +}