-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: extended test to makeCallback cb type check
makeCallback and makeStatsCallback are both tested intedependently. PR-URL: nodejs/node#12140 Fixes: nodejs/node#12136 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
- Loading branch information
1 parent
f40bd47
commit 0d27e35
Showing
2 changed files
with
45 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,28 @@ | ||
'use strict'; | ||
require('../common'); | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
const cbTypeError = /^TypeError: "callback" argument must be a function$/; | ||
const callbackThrowValues = [null, true, false, 0, 1, 'foo', /foo/, [], {}]; | ||
|
||
function test(cb) { | ||
const { sep } = require('path'); | ||
|
||
common.refreshTmpDir(); | ||
|
||
function testMakeCallback(cb) { | ||
return function() { | ||
// fs.stat() calls makeCallback() on its second argument | ||
fs.stat(__filename, cb); | ||
// fs.mkdtemp() calls makeCallback() on its third argument | ||
fs.mkdtemp(`${common.tmpDir}${sep}`, {}, cb); | ||
}; | ||
} | ||
|
||
// Verify the case where a callback function is provided | ||
assert.doesNotThrow(test(function() {})); | ||
// Passing undefined/nothing calls rethrow() internally | ||
assert.doesNotThrow(testMakeCallback()); | ||
|
||
// Passing undefined calls rethrow() internally, which is fine | ||
assert.doesNotThrow(test(undefined)); | ||
function invalidCallbackThrowsTests() { | ||
callbackThrowValues.forEach((value) => { | ||
assert.throws(testMakeCallback(value), cbTypeError); | ||
}); | ||
} | ||
|
||
// Anything else should throw | ||
assert.throws(test(null)); | ||
assert.throws(test(true)); | ||
assert.throws(test(false)); | ||
assert.throws(test(1)); | ||
assert.throws(test(0)); | ||
assert.throws(test('foo')); | ||
assert.throws(test(/foo/)); | ||
assert.throws(test([])); | ||
assert.throws(test({})); | ||
invalidCallbackThrowsTests(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
const cbTypeError = /^TypeError: "callback" argument must be a function$/; | ||
const callbackThrowValues = [null, true, false, 0, 1, 'foo', /foo/, [], {}]; | ||
|
||
function testMakeStatsCallback(cb) { | ||
return function() { | ||
// fs.stat() calls makeStatsCallback() on its second argument | ||
fs.stat(__filename, cb); | ||
}; | ||
} | ||
|
||
// Verify the case where a callback function is provided | ||
assert.doesNotThrow(testMakeStatsCallback(common.noop)); | ||
|
||
// Passing undefined/nothing calls rethrow() internally | ||
assert.doesNotThrow(testMakeStatsCallback()); | ||
|
||
function invalidCallbackThrowsTests() { | ||
callbackThrowValues.forEach((value) => { | ||
assert.throws(testMakeStatsCallback(value), cbTypeError); | ||
}); | ||
} | ||
|
||
invalidCallbackThrowsTests(); |