Browse Source

test: improve coverage of the util module

Add tests for untested branches and statements.
Change assert.equal to assert.strictEqual for consistency.

PR-URL: https://github.com/nodejs/node/pull/8633
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
v6.x
Michaël Zasso 8 years ago
committed by Jeremiah Senkpiel
parent
commit
1aa1740f12
  1. 86
      test/parallel/test-util-format.js
  2. 19
      test/parallel/test-util-inspect.js
  3. 63
      test/parallel/test-util.js

86
test/parallel/test-util-format.js

@ -4,61 +4,70 @@ const assert = require('assert');
const util = require('util');
const symbol = Symbol('foo');
assert.equal(util.format(), '');
assert.equal(util.format(''), '');
assert.equal(util.format([]), '[]');
assert.equal(util.format([0]), '[ 0 ]');
assert.equal(util.format({}), '{}');
assert.equal(util.format({foo: 42}), '{ foo: 42 }');
assert.equal(util.format(null), 'null');
assert.equal(util.format(true), 'true');
assert.equal(util.format(false), 'false');
assert.equal(util.format('test'), 'test');
assert.strictEqual(util.format(), '');
assert.strictEqual(util.format(''), '');
assert.strictEqual(util.format([]), '[]');
assert.strictEqual(util.format([0]), '[ 0 ]');
assert.strictEqual(util.format({}), '{}');
assert.strictEqual(util.format({foo: 42}), '{ foo: 42 }');
assert.strictEqual(util.format(null), 'null');
assert.strictEqual(util.format(true), 'true');
assert.strictEqual(util.format(false), 'false');
assert.strictEqual(util.format('test'), 'test');
// CHECKME this is for console.log() compatibility - but is it *right*?
assert.equal(util.format('foo', 'bar', 'baz'), 'foo bar baz');
assert.strictEqual(util.format('foo', 'bar', 'baz'), 'foo bar baz');
// ES6 Symbol handling
assert.equal(util.format(symbol), 'Symbol(foo)');
assert.equal(util.format('foo', symbol), 'foo Symbol(foo)');
assert.equal(util.format('%s', symbol), 'Symbol(foo)');
assert.equal(util.format('%j', symbol), 'undefined');
assert.strictEqual(util.format(symbol), 'Symbol(foo)');
assert.strictEqual(util.format('foo', symbol), 'foo Symbol(foo)');
assert.strictEqual(util.format('%s', symbol), 'Symbol(foo)');
assert.strictEqual(util.format('%j', symbol), 'undefined');
assert.throws(function() {
util.format('%d', symbol);
}, TypeError);
assert.equal(util.format('%d', 42.0), '42');
assert.equal(util.format('%d', 42), '42');
assert.equal(util.format('%s', 42), '42');
assert.equal(util.format('%j', 42), '42');
assert.strictEqual(util.format('%d', 42.0), '42');
assert.strictEqual(util.format('%d', 42), '42');
assert.strictEqual(util.format('%s', 42), '42');
assert.strictEqual(util.format('%j', 42), '42');
assert.equal(util.format('%d', '42.0'), '42');
assert.equal(util.format('%d', '42'), '42');
assert.equal(util.format('%s', '42'), '42');
assert.equal(util.format('%j', '42'), '"42"');
assert.strictEqual(util.format('%d', '42.0'), '42');
assert.strictEqual(util.format('%d', '42'), '42');
assert.strictEqual(util.format('%s', '42'), '42');
assert.strictEqual(util.format('%j', '42'), '"42"');
assert.equal(util.format('%%s%s', 'foo'), '%sfoo');
assert.strictEqual(util.format('%%s%s', 'foo'), '%sfoo');
assert.equal(util.format('%s'), '%s');
assert.equal(util.format('%s', undefined), 'undefined');
assert.equal(util.format('%s', 'foo'), 'foo');
assert.equal(util.format('%s:%s'), '%s:%s');
assert.equal(util.format('%s:%s', undefined), 'undefined:%s');
assert.equal(util.format('%s:%s', 'foo'), 'foo:%s');
assert.equal(util.format('%s:%s', 'foo', 'bar'), 'foo:bar');
assert.equal(util.format('%s:%s', 'foo', 'bar', 'baz'), 'foo:bar baz');
assert.equal(util.format('%%%s%%', 'hi'), '%hi%');
assert.equal(util.format('%%%s%%%%', 'hi'), '%hi%%');
assert.strictEqual(util.format('%s'), '%s');
assert.strictEqual(util.format('%s', undefined), 'undefined');
assert.strictEqual(util.format('%s', 'foo'), 'foo');
assert.strictEqual(util.format('%s:%s'), '%s:%s');
assert.strictEqual(util.format('%s:%s', undefined), 'undefined:%s');
assert.strictEqual(util.format('%s:%s', 'foo'), 'foo:%s');
assert.strictEqual(util.format('%s:%s', 'foo', 'bar'), 'foo:bar');
assert.strictEqual(util.format('%s:%s', 'foo', 'bar', 'baz'), 'foo:bar baz');
assert.strictEqual(util.format('%%%s%%', 'hi'), '%hi%');
assert.strictEqual(util.format('%%%s%%%%', 'hi'), '%hi%%');
assert.strictEqual(util.format('%sbc%%def', 'a'), 'abc%def');
assert.strictEqual(util.format('%d:%d', 12, 30), '12:30');
assert.strictEqual(util.format('%d:%d', 12), '12:%d');
assert.strictEqual(util.format('%d:%d'), '%d:%d');
assert.strictEqual(util.format('o: %j, a: %j', {}, []), 'o: {}, a: []');
assert.strictEqual(util.format('o: %j, a: %j', {}), 'o: {}, a: %j');
assert.strictEqual(util.format('o: %j, a: %j'), 'o: %j, a: %j');
{
const o = {};
o.o = o;
assert.equal(util.format('%j', o), '[Circular]');
assert.strictEqual(util.format('%j', o), '[Circular]');
}
// Errors
const err = new Error('foo');
assert.equal(util.format(err), err.stack);
assert.strictEqual(util.format(err), err.stack);
function CustomError(msg) {
Error.call(this);
Object.defineProperty(this, 'message',
@ -69,7 +78,7 @@ function CustomError(msg) {
}
util.inherits(CustomError, Error);
const customError = new CustomError('bar');
assert.equal(util.format(customError), customError.stack);
assert.strictEqual(util.format(customError), customError.stack);
// Doesn't capture stack trace
function BadCustomError(msg) {
Error.call(this);
@ -79,4 +88,5 @@ function BadCustomError(msg) {
{ value: 'BadCustomError', enumerable: false });
}
util.inherits(BadCustomError, Error);
assert.equal(util.format(new BadCustomError('foo')), '[BadCustomError: foo]');
assert.strictEqual(util.format(new BadCustomError('foo')),
'[BadCustomError: foo]');

19
test/parallel/test-util-inspect.js

@ -43,6 +43,12 @@ assert.strictEqual(util.inspect(Object.create({},
'{ visible: 1 }'
);
{
const regexp = /regexp/;
regexp.aprop = 42;
assert.strictEqual(util.inspect({a: regexp}, false, 0), '{ a: /regexp/ }');
}
assert(/Object/.test(
util.inspect({a: {a: {a: {a: {}}}}}, undefined, undefined, true)
));
@ -245,6 +251,11 @@ value = function() {};
value.aprop = 42;
assert.strictEqual(util.inspect(value), '{ [Function: value] aprop: 42 }');
// Anonymous function with properties
value = (() => function() {})();
value.aprop = 42;
assert.strictEqual(util.inspect(value), '{ [Function] aprop: 42 }');
// Regular expressions with properties
value = /123/ig;
value.aprop = 42;
@ -901,4 +912,12 @@ checkAlignment(new Map(big_array.map(function(y) { return [y, null]; })));
JSON.stringify(util.inspect.defaultOptions),
JSON.stringify(oldOptions)
);
assert.throws(() => {
util.inspect.defaultOptions = null;
}, /"options" must be an object/);
assert.throws(() => {
util.inspect.defaultOptions = 'bad';
}, /"options" must be an object/);
}

63
test/parallel/test-util.js

@ -1,8 +1,8 @@
'use strict';
require('../common');
var assert = require('assert');
var util = require('util');
var context = require('vm').runInNewContext;
const common = require('../common');
const assert = require('assert');
const util = require('util');
const context = require('vm').runInNewContext;
// isArray
assert.equal(true, util.isArray([]));
@ -86,3 +86,58 @@ assert.deepStrictEqual(util._extend({a: 1}, true), {a: 1});
assert.deepStrictEqual(util._extend({a: 1}, false), {a: 1});
assert.deepStrictEqual(util._extend({a: 1}, {b: 2}), {a: 1, b: 2});
assert.deepStrictEqual(util._extend({a: 1, b: 2}, {b: 3}), {a: 1, b: 3});
// deprecated
assert.strictEqual(util.isBoolean(true), true);
assert.strictEqual(util.isBoolean(false), true);
assert.strictEqual(util.isBoolean('string'), false);
assert.strictEqual(util.isNull(null), true);
assert.strictEqual(util.isNull(), false);
assert.strictEqual(util.isNull('string'), false);
assert.strictEqual(util.isUndefined(), true);
assert.strictEqual(util.isUndefined(null), false);
assert.strictEqual(util.isUndefined('string'), false);
assert.strictEqual(util.isNullOrUndefined(null), true);
assert.strictEqual(util.isNullOrUndefined(), true);
assert.strictEqual(util.isNullOrUndefined('string'), false);
assert.strictEqual(util.isNumber(42), true);
assert.strictEqual(util.isNumber(), false);
assert.strictEqual(util.isNumber('string'), false);
assert.strictEqual(util.isString('string'), true);
assert.strictEqual(util.isString(), false);
assert.strictEqual(util.isString(42), false);
assert.strictEqual(util.isSymbol(Symbol()), true);
assert.strictEqual(util.isSymbol(), false);
assert.strictEqual(util.isSymbol('string'), false);
assert.strictEqual(util.isFunction(() => {}), true);
assert.strictEqual(util.isFunction(function() {}), true);
assert.strictEqual(util.isFunction(), false);
assert.strictEqual(util.isFunction('string'), false);
const expected = [
'util.print is deprecated. Use console.log instead.',
'util.puts is deprecated. Use console.log instead.',
'util.debug is deprecated. Use console.error instead.',
'util.error is deprecated. Use console.error instead.'
];
process.on('warning', common.mustCall((warning) => {
assert.strictEqual(warning.name, 'DeprecationWarning');
assert.notStrictEqual(expected.indexOf(warning.message), -1,
`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));
util.print('test');
util.puts('test');
util.debug('test');
util.error('test');

Loading…
Cancel
Save