summaryrefslogtreecommitdiff
path: root/test/parallel/test-common.js
diff options
context:
space:
mode:
authorRefael Ackermann <refack@gmail.com>2017-05-09 17:16:52 -0400
committerRefael Ackermann <refack@gmail.com>2017-05-19 15:22:44 -0400
commitfccc0bf6e68981f6225315875524e1a0b56fcc20 (patch)
tree6fbe03770f10e7edaaadbc9ec6e1bd606ad18a08 /test/parallel/test-common.js
parent46e773c5db9ebc106823594b82f4ff14a0a004f3 (diff)
downloadandroid-node-v8-fccc0bf6e68981f6225315875524e1a0b56fcc20.tar.gz
android-node-v8-fccc0bf6e68981f6225315875524e1a0b56fcc20.tar.bz2
android-node-v8-fccc0bf6e68981f6225315875524e1a0b56fcc20.zip
test: add mustCallAtLeast
PR-URL: https://github.com/nodejs/node/pull/12935 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test/parallel/test-common.js')
-rw-r--r--test/parallel/test-common.js47
1 files changed, 44 insertions, 3 deletions
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 <anonymous> function calls. Expected exactly 2, actual 1.'
+ ], [
+ join(common.fixturesDir, 'failmustcall2.js'),
+ 'Mismatched <anonymous> 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);
+ }));
+}