node/test/parallel/test-util-internal.js
James M Snell 1fdbaed2f2
test: begin normalizing fixtures use
Adds a new `../common/fixtures' module to begin normalizing
`test/fixtures` use. Our test code is a bit inconsistent with
regards to use of the fixtures directory. Some code uses
`path.join()`, some code uses string concats, some other
code uses template strings, etc. In mnay cases, significant
duplication of code is seen when accessing fixture files, etc.

This updates many (but by no means all) of the tests in the
test suite to use the new consistent API. There are still
many more to update, which would make an excelent Code-n-Learn
exercise.

Backport-PR-URL: https://github.com/nodejs/node/pull/16265
PR-URL: https://github.com/nodejs/node/pull/14332
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
2017-10-26 00:21:30 -04:00

64 lines
2.1 KiB
JavaScript

'use strict';
// Flags: --expose_internals
require('../common');
const assert = require('assert');
const fixtures = require('../common/fixtures');
const internalUtil = require('internal/util');
const spawnSync = require('child_process').spawnSync;
function getHiddenValue(obj, name) {
return function() {
internalUtil.getHiddenValue(obj, name);
};
}
function setHiddenValue(obj, name, val) {
return function() {
internalUtil.setHiddenValue(obj, name, val);
};
}
const errMessageObj = /obj must be an object/;
const errMessageStr = /name must be a string/;
assert.throws(getHiddenValue(), errMessageObj);
assert.throws(getHiddenValue(null, 'foo'), errMessageObj);
assert.throws(getHiddenValue(undefined, 'foo'), errMessageObj);
assert.throws(getHiddenValue('bar', 'foo'), errMessageObj);
assert.throws(getHiddenValue(85, 'foo'), errMessageObj);
assert.throws(getHiddenValue({}), errMessageStr);
assert.throws(getHiddenValue({}, null), errMessageStr);
assert.throws(getHiddenValue({}, []), errMessageStr);
assert.deepStrictEqual(internalUtil.getHiddenValue({}, 'foo'), undefined);
assert.throws(setHiddenValue(), errMessageObj);
assert.throws(setHiddenValue(null, 'foo'), errMessageObj);
assert.throws(setHiddenValue(undefined, 'foo'), errMessageObj);
assert.throws(setHiddenValue('bar', 'foo'), errMessageObj);
assert.throws(setHiddenValue(85, 'foo'), errMessageObj);
assert.throws(setHiddenValue({}), errMessageStr);
assert.throws(setHiddenValue({}, null), errMessageStr);
assert.throws(setHiddenValue({}, []), errMessageStr);
const obj = {};
assert.strictEqual(internalUtil.setHiddenValue(obj, 'foo', 'bar'), true);
assert.strictEqual(internalUtil.getHiddenValue(obj, 'foo'), 'bar');
let arrowMessage;
try {
require(fixtures.path('syntax', 'bad_syntax'));
} catch (err) {
arrowMessage = internalUtil.getHiddenValue(err, 'node:arrowMessage');
}
assert(/bad_syntax\.js:1/.test(arrowMessage));
const args = [
'--expose-internals',
'-e',
"require('internal/util').error('foo %d', 5)"
];
const result = spawnSync(process.argv[0], args, { encoding: 'utf8' });
assert.strictEqual(result.stderr.indexOf('%'), -1);
assert(/foo 5/.test(result.stderr));