Skip to content

Commit ebbd5cb

Browse files
committedSep 20, 2016
test: improve coverage of the util module
Add tests for untested branches and statements. Change assert.equal to assert.strictEqual for consistency. PR-URL: #8633 Reviewed-By: Santiago Gimeno <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Evan Lucas <[email protected]>
1 parent 7c3ab1d commit ebbd5cb

File tree

3 files changed

+126
-42
lines changed

3 files changed

+126
-42
lines changed
 

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

+48-38
Original file line numberDiff line numberDiff line change
@@ -4,61 +4,70 @@ const assert = require('assert');
44
const util = require('util');
55
const symbol = Symbol('foo');
66

7-
assert.equal(util.format(), '');
8-
assert.equal(util.format(''), '');
9-
assert.equal(util.format([]), '[]');
10-
assert.equal(util.format([0]), '[ 0 ]');
11-
assert.equal(util.format({}), '{}');
12-
assert.equal(util.format({foo: 42}), '{ foo: 42 }');
13-
assert.equal(util.format(null), 'null');
14-
assert.equal(util.format(true), 'true');
15-
assert.equal(util.format(false), 'false');
16-
assert.equal(util.format('test'), 'test');
7+
assert.strictEqual(util.format(), '');
8+
assert.strictEqual(util.format(''), '');
9+
assert.strictEqual(util.format([]), '[]');
10+
assert.strictEqual(util.format([0]), '[ 0 ]');
11+
assert.strictEqual(util.format({}), '{}');
12+
assert.strictEqual(util.format({foo: 42}), '{ foo: 42 }');
13+
assert.strictEqual(util.format(null), 'null');
14+
assert.strictEqual(util.format(true), 'true');
15+
assert.strictEqual(util.format(false), 'false');
16+
assert.strictEqual(util.format('test'), 'test');
1717

1818
// CHECKME this is for console.log() compatibility - but is it *right*?
19-
assert.equal(util.format('foo', 'bar', 'baz'), 'foo bar baz');
19+
assert.strictEqual(util.format('foo', 'bar', 'baz'), 'foo bar baz');
2020

2121
// ES6 Symbol handling
22-
assert.equal(util.format(symbol), 'Symbol(foo)');
23-
assert.equal(util.format('foo', symbol), 'foo Symbol(foo)');
24-
assert.equal(util.format('%s', symbol), 'Symbol(foo)');
25-
assert.equal(util.format('%j', symbol), 'undefined');
22+
assert.strictEqual(util.format(symbol), 'Symbol(foo)');
23+
assert.strictEqual(util.format('foo', symbol), 'foo Symbol(foo)');
24+
assert.strictEqual(util.format('%s', symbol), 'Symbol(foo)');
25+
assert.strictEqual(util.format('%j', symbol), 'undefined');
2626
assert.throws(function() {
2727
util.format('%d', symbol);
2828
}, TypeError);
2929

30-
assert.equal(util.format('%d', 42.0), '42');
31-
assert.equal(util.format('%d', 42), '42');
32-
assert.equal(util.format('%s', 42), '42');
33-
assert.equal(util.format('%j', 42), '42');
30+
assert.strictEqual(util.format('%d', 42.0), '42');
31+
assert.strictEqual(util.format('%d', 42), '42');
32+
assert.strictEqual(util.format('%s', 42), '42');
33+
assert.strictEqual(util.format('%j', 42), '42');
3434

35-
assert.equal(util.format('%d', '42.0'), '42');
36-
assert.equal(util.format('%d', '42'), '42');
37-
assert.equal(util.format('%s', '42'), '42');
38-
assert.equal(util.format('%j', '42'), '"42"');
35+
assert.strictEqual(util.format('%d', '42.0'), '42');
36+
assert.strictEqual(util.format('%d', '42'), '42');
37+
assert.strictEqual(util.format('%s', '42'), '42');
38+
assert.strictEqual(util.format('%j', '42'), '"42"');
3939

40-
assert.equal(util.format('%%s%s', 'foo'), '%sfoo');
40+
assert.strictEqual(util.format('%%s%s', 'foo'), '%sfoo');
4141

42-
assert.equal(util.format('%s'), '%s');
43-
assert.equal(util.format('%s', undefined), 'undefined');
44-
assert.equal(util.format('%s', 'foo'), 'foo');
45-
assert.equal(util.format('%s:%s'), '%s:%s');
46-
assert.equal(util.format('%s:%s', undefined), 'undefined:%s');
47-
assert.equal(util.format('%s:%s', 'foo'), 'foo:%s');
48-
assert.equal(util.format('%s:%s', 'foo', 'bar'), 'foo:bar');
49-
assert.equal(util.format('%s:%s', 'foo', 'bar', 'baz'), 'foo:bar baz');
50-
assert.equal(util.format('%%%s%%', 'hi'), '%hi%');
51-
assert.equal(util.format('%%%s%%%%', 'hi'), '%hi%%');
42+
assert.strictEqual(util.format('%s'), '%s');
43+
assert.strictEqual(util.format('%s', undefined), 'undefined');
44+
assert.strictEqual(util.format('%s', 'foo'), 'foo');
45+
assert.strictEqual(util.format('%s:%s'), '%s:%s');
46+
assert.strictEqual(util.format('%s:%s', undefined), 'undefined:%s');
47+
assert.strictEqual(util.format('%s:%s', 'foo'), 'foo:%s');
48+
assert.strictEqual(util.format('%s:%s', 'foo', 'bar'), 'foo:bar');
49+
assert.strictEqual(util.format('%s:%s', 'foo', 'bar', 'baz'), 'foo:bar baz');
50+
assert.strictEqual(util.format('%%%s%%', 'hi'), '%hi%');
51+
assert.strictEqual(util.format('%%%s%%%%', 'hi'), '%hi%%');
52+
assert.strictEqual(util.format('%sbc%%def', 'a'), 'abc%def');
53+
54+
assert.strictEqual(util.format('%d:%d', 12, 30), '12:30');
55+
assert.strictEqual(util.format('%d:%d', 12), '12:%d');
56+
assert.strictEqual(util.format('%d:%d'), '%d:%d');
57+
58+
assert.strictEqual(util.format('o: %j, a: %j', {}, []), 'o: {}, a: []');
59+
assert.strictEqual(util.format('o: %j, a: %j', {}), 'o: {}, a: %j');
60+
assert.strictEqual(util.format('o: %j, a: %j'), 'o: %j, a: %j');
5261

5362
{
5463
const o = {};
5564
o.o = o;
56-
assert.equal(util.format('%j', o), '[Circular]');
65+
assert.strictEqual(util.format('%j', o), '[Circular]');
5766
}
5867

5968
// Errors
6069
const err = new Error('foo');
61-
assert.equal(util.format(err), err.stack);
70+
assert.strictEqual(util.format(err), err.stack);
6271
function CustomError(msg) {
6372
Error.call(this);
6473
Object.defineProperty(this, 'message',
@@ -69,7 +78,7 @@ function CustomError(msg) {
6978
}
7079
util.inherits(CustomError, Error);
7180
const customError = new CustomError('bar');
72-
assert.equal(util.format(customError), customError.stack);
81+
assert.strictEqual(util.format(customError), customError.stack);
7382
// Doesn't capture stack trace
7483
function BadCustomError(msg) {
7584
Error.call(this);
@@ -79,4 +88,5 @@ function BadCustomError(msg) {
7988
{ value: 'BadCustomError', enumerable: false });
8089
}
8190
util.inherits(BadCustomError, Error);
82-
assert.equal(util.format(new BadCustomError('foo')), '[BadCustomError: foo]');
91+
assert.strictEqual(util.format(new BadCustomError('foo')),
92+
'[BadCustomError: foo]');

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

+19
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ assert.strictEqual(util.inspect(Object.create({},
4343
'{ visible: 1 }'
4444
);
4545

46+
{
47+
const regexp = /regexp/;
48+
regexp.aprop = 42;
49+
assert.strictEqual(util.inspect({a: regexp}, false, 0), '{ a: /regexp/ }');
50+
}
51+
4652
assert(/Object/.test(
4753
util.inspect({a: {a: {a: {a: {}}}}}, undefined, undefined, true)
4854
));
@@ -245,6 +251,11 @@ value = function() {};
245251
value.aprop = 42;
246252
assert.strictEqual(util.inspect(value), '{ [Function: value] aprop: 42 }');
247253

254+
// Anonymous function with properties
255+
value = (() => function() {})();
256+
value.aprop = 42;
257+
assert.strictEqual(util.inspect(value), '{ [Function] aprop: 42 }');
258+
248259
// Regular expressions with properties
249260
value = /123/ig;
250261
value.aprop = 42;
@@ -901,4 +912,12 @@ checkAlignment(new Map(big_array.map(function(y) { return [y, null]; })));
901912
JSON.stringify(util.inspect.defaultOptions),
902913
JSON.stringify(oldOptions)
903914
);
915+
916+
assert.throws(() => {
917+
util.inspect.defaultOptions = null;
918+
}, /"options" must be an object/);
919+
920+
assert.throws(() => {
921+
util.inspect.defaultOptions = 'bad';
922+
}, /"options" must be an object/);
904923
}

‎test/parallel/test-util.js

+59-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
2-
require('../common');
3-
var assert = require('assert');
4-
var util = require('util');
5-
var context = require('vm').runInNewContext;
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const util = require('util');
5+
const context = require('vm').runInNewContext;
66

77
// isArray
88
assert.equal(true, util.isArray([]));
@@ -86,3 +86,58 @@ assert.deepStrictEqual(util._extend({a: 1}, true), {a: 1});
8686
assert.deepStrictEqual(util._extend({a: 1}, false), {a: 1});
8787
assert.deepStrictEqual(util._extend({a: 1}, {b: 2}), {a: 1, b: 2});
8888
assert.deepStrictEqual(util._extend({a: 1, b: 2}, {b: 3}), {a: 1, b: 3});
89+
90+
// deprecated
91+
assert.strictEqual(util.isBoolean(true), true);
92+
assert.strictEqual(util.isBoolean(false), true);
93+
assert.strictEqual(util.isBoolean('string'), false);
94+
95+
assert.strictEqual(util.isNull(null), true);
96+
assert.strictEqual(util.isNull(), false);
97+
assert.strictEqual(util.isNull('string'), false);
98+
99+
assert.strictEqual(util.isUndefined(), true);
100+
assert.strictEqual(util.isUndefined(null), false);
101+
assert.strictEqual(util.isUndefined('string'), false);
102+
103+
assert.strictEqual(util.isNullOrUndefined(null), true);
104+
assert.strictEqual(util.isNullOrUndefined(), true);
105+
assert.strictEqual(util.isNullOrUndefined('string'), false);
106+
107+
assert.strictEqual(util.isNumber(42), true);
108+
assert.strictEqual(util.isNumber(), false);
109+
assert.strictEqual(util.isNumber('string'), false);
110+
111+
assert.strictEqual(util.isString('string'), true);
112+
assert.strictEqual(util.isString(), false);
113+
assert.strictEqual(util.isString(42), false);
114+
115+
assert.strictEqual(util.isSymbol(Symbol()), true);
116+
assert.strictEqual(util.isSymbol(), false);
117+
assert.strictEqual(util.isSymbol('string'), false);
118+
119+
assert.strictEqual(util.isFunction(() => {}), true);
120+
assert.strictEqual(util.isFunction(function() {}), true);
121+
assert.strictEqual(util.isFunction(), false);
122+
assert.strictEqual(util.isFunction('string'), false);
123+
124+
const expected = [
125+
'util.print is deprecated. Use console.log instead.',
126+
'util.puts is deprecated. Use console.log instead.',
127+
'util.debug is deprecated. Use console.error instead.',
128+
'util.error is deprecated. Use console.error instead.'
129+
];
130+
131+
process.on('warning', common.mustCall((warning) => {
132+
assert.strictEqual(warning.name, 'DeprecationWarning');
133+
assert.notStrictEqual(expected.indexOf(warning.message), -1,
134+
`unexpected error message: "${warning.message}"`);
135+
// Remove a warning message after it is seen so that we guarantee that we get
136+
// each message only once.
137+
expected.splice(expected.indexOf(warning.message), 1);
138+
}, expected.length));
139+
140+
util.print('test');
141+
util.puts('test');
142+
util.debug('test');
143+
util.error('test');

0 commit comments

Comments
 (0)