Skip to content

Commit bfcb5b1

Browse files
committed
update to latest util module code
Add tests copied from node. Also run tests in browser via zuul.
1 parent 78dc5c6 commit bfcb5b1

File tree

12 files changed

+1103
-302
lines changed

12 files changed

+1103
-302
lines changed

.zuul.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
ui: mocha-qunit
2+
browsers:
3+
- name: chrome
4+
version: 27..latest
5+
- name: firefox
6+
version: latest
7+
- name: safari
8+
version: latest
9+
- name: ie
10+
version: 6..latest

package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,14 @@
1515
"type": "git",
1616
"url": "git://github.com/defunctzombie/node-util"
1717
},
18-
"main": "./util.js"
18+
"main": "./util.js",
19+
"scripts": {
20+
"test": "node test/node/*.js && zuul test/*.js"
21+
},
22+
"devDependencies": {
23+
"zuul": "~1.0.3"
24+
},
25+
"browser": {
26+
"./support/isBuffer.js": "./support/isBufferBrowser.js"
27+
}
1928
}

support/isBuffer.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = function isBuffer(arg) {
2+
return arg instanceof Buffer;
3+
}

support/isBufferBrowser.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = function isBuffer(arg) {
2+
return arg && typeof arg === 'object'
3+
&& typeof arg.copy === 'function'
4+
&& typeof arg.fill === 'function'
5+
&& typeof arg.binarySlice === 'function'
6+
;
7+
}

test/browser/inspect.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var assert = require('assert');
23+
var util = require('../../');
24+
25+
suite('inspect');
26+
27+
test('util.inspect - test for sparse array', function () {
28+
var a = ['foo', 'bar', 'baz'];
29+
assert.equal(util.inspect(a), '[ \'foo\', \'bar\', \'baz\' ]');
30+
delete a[1];
31+
assert.equal(util.inspect(a), '[ \'foo\', , \'baz\' ]');
32+
assert.equal(util.inspect(a, true), '[ \'foo\', , \'baz\', [length]: 3 ]');
33+
assert.equal(util.inspect(new Array(5)), '[ , , , , ]');
34+
});
35+
36+
test('util.inspect - exceptions should print the error message, not \'{}\'', function () {
37+
assert.equal(util.inspect(new Error()), '[Error]');
38+
assert.equal(util.inspect(new Error('FAIL')), '[Error: FAIL]');
39+
assert.equal(util.inspect(new TypeError('FAIL')), '[TypeError: FAIL]');
40+
assert.equal(util.inspect(new SyntaxError('FAIL')), '[SyntaxError: FAIL]');
41+
});

test/browser/is.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var assert = require('assert');
23+
24+
var util = require('../../');
25+
26+
suite('is');
27+
28+
test('util.isArray', function () {
29+
assert.equal(true, util.isArray([]));
30+
assert.equal(true, util.isArray(Array()));
31+
assert.equal(true, util.isArray(new Array()));
32+
assert.equal(true, util.isArray(new Array(5)));
33+
assert.equal(true, util.isArray(new Array('with', 'some', 'entries')));
34+
assert.equal(false, util.isArray({}));
35+
assert.equal(false, util.isArray({ push: function() {} }));
36+
assert.equal(false, util.isArray(/regexp/));
37+
assert.equal(false, util.isArray(new Error()));
38+
assert.equal(false, util.isArray(Object.create(Array.prototype)));
39+
});
40+
41+
test('util.isRegExp', function () {
42+
assert.equal(true, util.isRegExp(/regexp/));
43+
assert.equal(true, util.isRegExp(RegExp()));
44+
assert.equal(true, util.isRegExp(new RegExp()));
45+
assert.equal(false, util.isRegExp({}));
46+
assert.equal(false, util.isRegExp([]));
47+
assert.equal(false, util.isRegExp(new Date()));
48+
assert.equal(false, util.isRegExp(Object.create(RegExp.prototype)));
49+
});
50+
51+
test('util.isDate', function () {
52+
assert.equal(true, util.isDate(new Date()));
53+
assert.equal(true, util.isDate(new Date(0)));
54+
assert.equal(false, util.isDate(Date()));
55+
assert.equal(false, util.isDate({}));
56+
assert.equal(false, util.isDate([]));
57+
assert.equal(false, util.isDate(new Error()));
58+
assert.equal(false, util.isDate(Object.create(Date.prototype)));
59+
});
60+
61+
test('util.isError', function () {
62+
assert.equal(true, util.isError(new Error()));
63+
assert.equal(true, util.isError(new TypeError()));
64+
assert.equal(true, util.isError(new SyntaxError()));
65+
assert.equal(false, util.isError({}));
66+
assert.equal(false, util.isError({ name: 'Error', message: '' }));
67+
assert.equal(false, util.isError([]));
68+
assert.equal(true, util.isError(Object.create(Error.prototype)));
69+
});
70+
71+
test('util._extend', function () {
72+
assert.deepEqual(util._extend({a:1}), {a:1});
73+
assert.deepEqual(util._extend({a:1}, []), {a:1});
74+
assert.deepEqual(util._extend({a:1}, null), {a:1});
75+
assert.deepEqual(util._extend({a:1}, true), {a:1});
76+
assert.deepEqual(util._extend({a:1}, false), {a:1});
77+
assert.deepEqual(util._extend({a:1}, {b:2}), {a:1, b:2});
78+
assert.deepEqual(util._extend({a:1, b:2}, {b:3}), {a:1, b:3});
79+
});

test/node/debug.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var assert = require('assert');
23+
var util = require('../../');
24+
25+
if (process.argv[2] === 'child')
26+
child();
27+
else
28+
parent();
29+
30+
function parent() {
31+
test('foo,tud,bar', true);
32+
test('foo,tud', true);
33+
test('tud,bar', true);
34+
test('tud', true);
35+
test('foo,bar', false);
36+
test('', false);
37+
}
38+
39+
function test(environ, shouldWrite) {
40+
var expectErr = '';
41+
if (shouldWrite) {
42+
expectErr = 'TUD %PID%: this { is: \'a\' } /debugging/\n' +
43+
'TUD %PID%: number=1234 string=asdf obj={"foo":"bar"}\n';
44+
}
45+
var expectOut = 'ok\n';
46+
var didTest = false;
47+
48+
var spawn = require('child_process').spawn;
49+
var child = spawn(process.execPath, [__filename, 'child'], {
50+
env: { NODE_DEBUG: environ }
51+
});
52+
53+
expectErr = expectErr.split('%PID%').join(child.pid);
54+
55+
var err = '';
56+
child.stderr.setEncoding('utf8');
57+
child.stderr.on('data', function(c) {
58+
err += c;
59+
});
60+
61+
var out = '';
62+
child.stdout.setEncoding('utf8');
63+
child.stdout.on('data', function(c) {
64+
out += c;
65+
});
66+
67+
child.on('close', function(c) {
68+
assert(!c);
69+
assert.equal(err, expectErr);
70+
assert.equal(out, expectOut);
71+
didTest = true;
72+
console.log('ok %j %j', environ, shouldWrite);
73+
});
74+
75+
process.on('exit', function() {
76+
assert(didTest);
77+
});
78+
}
79+
80+
81+
function child() {
82+
var debug = util.debuglog('tud');
83+
debug('this', { is: 'a' }, /debugging/);
84+
debug('number=%d string=%s obj=%j', 1234, 'asdf', { foo: 'bar' });
85+
console.log('ok');
86+
}

test/node/format.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
23+
24+
25+
var assert = require('assert');
26+
var util = require('../../');
27+
28+
assert.equal(util.format(), '');
29+
assert.equal(util.format(''), '');
30+
assert.equal(util.format([]), '[]');
31+
assert.equal(util.format({}), '{}');
32+
assert.equal(util.format(null), 'null');
33+
assert.equal(util.format(true), 'true');
34+
assert.equal(util.format(false), 'false');
35+
assert.equal(util.format('test'), 'test');
36+
37+
// CHECKME this is for console.log() compatibility - but is it *right*?
38+
assert.equal(util.format('foo', 'bar', 'baz'), 'foo bar baz');
39+
40+
assert.equal(util.format('%d', 42.0), '42');
41+
assert.equal(util.format('%d', 42), '42');
42+
assert.equal(util.format('%s', 42), '42');
43+
assert.equal(util.format('%j', 42), '42');
44+
45+
assert.equal(util.format('%d', '42.0'), '42');
46+
assert.equal(util.format('%d', '42'), '42');
47+
assert.equal(util.format('%s', '42'), '42');
48+
assert.equal(util.format('%j', '42'), '"42"');
49+
50+
assert.equal(util.format('%%s%s', 'foo'), '%sfoo');
51+
52+
assert.equal(util.format('%s'), '%s');
53+
assert.equal(util.format('%s', undefined), 'undefined');
54+
assert.equal(util.format('%s', 'foo'), 'foo');
55+
assert.equal(util.format('%s:%s'), '%s:%s');
56+
assert.equal(util.format('%s:%s', undefined), 'undefined:%s');
57+
assert.equal(util.format('%s:%s', 'foo'), 'foo:%s');
58+
assert.equal(util.format('%s:%s', 'foo', 'bar'), 'foo:bar');
59+
assert.equal(util.format('%s:%s', 'foo', 'bar', 'baz'), 'foo:bar baz');
60+
assert.equal(util.format('%%%s%%', 'hi'), '%hi%');
61+
assert.equal(util.format('%%%s%%%%', 'hi'), '%hi%%');
62+
63+
(function() {
64+
var o = {};
65+
o.o = o;
66+
assert.equal(util.format('%j', o), '[Circular]');
67+
})();
68+
69+
// Errors
70+
assert.equal(util.format(new Error('foo')), '[Error: foo]');
71+
function CustomError(msg) {
72+
Error.call(this);
73+
Object.defineProperty(this, 'message', { value: msg, enumerable: false });
74+
Object.defineProperty(this, 'name', { value: 'CustomError', enumerable: false });
75+
}
76+
util.inherits(CustomError, Error);
77+
assert.equal(util.format(new CustomError('bar')), '[CustomError: bar]');

0 commit comments

Comments
 (0)