From b0746e7f8aa546519c7ff4232c782159dbf4fd46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Fri, 7 Oct 2016 12:59:17 +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 | 79 +++++++++++++++++------------- test/parallel/test-util-inspect.js | 13 ++++- test/parallel/test-util.js | 45 +++++++++++++++-- 3 files changed, 99 insertions(+), 38 deletions(-) diff --git a/test/parallel/test-util-format.js b/test/parallel/test-util-format.js index 00028ddbb74728..937b39f375f61b 100644 --- a/test/parallel/test-util-format.js +++ b/test/parallel/test-util-format.js @@ -4,54 +4,65 @@ var assert = require('assert'); var util = require('util'); var symbol = Symbol('foo'); -assert.equal(util.format(), ''); -assert.equal(util.format(''), ''); -assert.equal(util.format([]), '[]'); -assert.equal(util.format({}), '{}'); -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'); (function() { var o = {}; o.o = o; - assert.equal(util.format('%j', o), '[Circular]'); + assert.strictEqual(util.format('%j', o), '[Circular]'); })(); // Errors @@ -64,4 +75,4 @@ function CustomError(msg) { { value: 'CustomError', enumerable: false }); } util.inherits(CustomError, Error); -assert.equal(util.format(new CustomError('bar')), '[CustomError: bar]'); +assert.strictEqual(util.format(new CustomError('bar')), '[CustomError: bar]'); diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 98567027dd3519..3c227d8a3fc407 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -41,6 +41,12 @@ assert.equal(util.inspect(Object.create({}, '{ visible: 1 }' ); +{ + const regexp = /regexp/; + regexp.aprop = 42; + assert.strictEqual(util.inspect({a: regexp}, false, 0), '{ a: /regexp/ }'); +} + // Due to the hash seed randomization it's not deterministic the order that // the following ways this hash is displayed. // See http://codereview.chromium.org/9124004/ @@ -99,6 +105,11 @@ value = function() {}; value.aprop = 42; assert.equal(util.inspect(value), '{ [Function] 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; @@ -512,5 +523,5 @@ checkAlignment(new Map(big_array.map(function(y) { return [y, null]; }))); { const x = Object.create(null); - assert.equal(util.inspect(x), '{}'); + assert.strictEqual(util.inspect(x), '{}'); } diff --git a/test/parallel/test-util.js b/test/parallel/test-util.js index 84cd058714df02..ee59e03b631fac 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 assert = require('assert'); +const util = require('util'); +const context = require('vm').runInNewContext; // isArray assert.equal(true, util.isArray([])); @@ -83,3 +83,42 @@ 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); + +util.print('test'); +util.puts('test'); +util.debug('test'); +util.error('test');