From ebbd5cb0df9696ce914ea5ca61822b194bb5d539 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Sat, 17 Sep 2016 18:07:03 +0200 Subject: [PATCH] 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 Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: Evan Lucas --- test/parallel/test-util-format.js | 86 +++++++++++++++++------------- test/parallel/test-util-inspect.js | 19 +++++++ test/parallel/test-util.js | 63 ++++++++++++++++++++-- 3 files changed, 126 insertions(+), 42 deletions(-) diff --git a/test/parallel/test-util-format.js b/test/parallel/test-util-format.js index b8977e57b5139c..71265e4b6a37f8 100644 --- a/test/parallel/test-util-format.js +++ b/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]'); diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 3becf5b318d0a9..b2d23d32557a6d 100644 --- a/test/parallel/test-util-inspect.js +++ b/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/); } diff --git a/test/parallel/test-util.js b/test/parallel/test-util.js index 023583ca4c7e7d..6072f9ae9a249e 100644 --- a/test/parallel/test-util.js +++ b/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');