2011-03-10 09:54:52 +01:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2010-12-05 00:20:34 +01:00
|
|
|
var common = require('../common');
|
2010-12-05 20:15:30 +01:00
|
|
|
var assert = require('assert');
|
2009-12-01 11:20:14 +01:00
|
|
|
|
2010-12-03 02:03:18 +01:00
|
|
|
assert.equal('0', common.inspect(0));
|
|
|
|
assert.equal('1', common.inspect(1));
|
|
|
|
assert.equal('false', common.inspect(false));
|
|
|
|
assert.equal("''", common.inspect(''));
|
|
|
|
assert.equal("'hello'", common.inspect('hello'));
|
|
|
|
assert.equal('[Function]', common.inspect(function() {}));
|
2010-07-15 20:47:25 +02:00
|
|
|
assert.equal('undefined', common.inspect(undefined));
|
|
|
|
assert.equal('null', common.inspect(null));
|
|
|
|
assert.equal('/foo(bar\\n)?/gi', common.inspect(/foo(bar\n)?/gi));
|
2010-02-14 13:11:59 +01:00
|
|
|
assert.equal('Sun, 14 Feb 2010 11:48:40 GMT',
|
2010-12-03 02:03:18 +01:00
|
|
|
common.inspect(new Date('Sun, 14 Feb 2010 11:48:40 GMT')));
|
2009-12-01 11:20:14 +01:00
|
|
|
|
2010-12-03 02:03:18 +01:00
|
|
|
assert.equal("'\\n\\u0001'", common.inspect('\n\u0001'));
|
2009-12-07 05:44:41 +01:00
|
|
|
|
2010-07-15 20:47:25 +02:00
|
|
|
assert.equal('[]', common.inspect([]));
|
2011-10-26 20:22:16 +02:00
|
|
|
assert.equal('{}', common.inspect(Object.create([])));
|
2010-07-15 20:47:25 +02:00
|
|
|
assert.equal('[ 1, 2 ]', common.inspect([1, 2]));
|
|
|
|
assert.equal('[ 1, [ 2, 3 ] ]', common.inspect([1, [2, 3]]));
|
2009-12-01 11:20:14 +01:00
|
|
|
|
2010-07-15 20:47:25 +02:00
|
|
|
assert.equal('{}', common.inspect({}));
|
|
|
|
assert.equal('{ a: 1 }', common.inspect({a: 1}));
|
|
|
|
assert.equal('{ a: [Function] }', common.inspect({a: function() {}}));
|
|
|
|
assert.equal('{ a: 1, b: 2 }', common.inspect({a: 1, b: 2}));
|
|
|
|
assert.equal('{ a: {} }', common.inspect({'a': {}}));
|
|
|
|
assert.equal('{ a: { b: 2 } }', common.inspect({'a': {'b': 2}}));
|
2010-12-03 02:03:18 +01:00
|
|
|
assert.equal('{ a: { b: { c: [Object] } } }',
|
|
|
|
common.inspect({'a': {'b': { 'c': { 'd': 2 }}}}));
|
|
|
|
assert.equal('{ a: { b: { c: { d: 2 } } } }',
|
|
|
|
common.inspect({'a': {'b': { 'c': { 'd': 2 }}}}, false, null));
|
|
|
|
assert.equal('[ 1, 2, 3, [length]: 3 ]', common.inspect([1, 2, 3], true));
|
|
|
|
assert.equal('{ a: [Object] }',
|
|
|
|
common.inspect({'a': {'b': { 'c': 2}}}, false, 0));
|
|
|
|
assert.equal('{ a: { b: [Object] } }',
|
|
|
|
common.inspect({'a': {'b': { 'c': 2}}}, false, 1));
|
|
|
|
assert.equal('{ visible: 1 }',
|
|
|
|
common.inspect(Object.create({},
|
|
|
|
{visible: {value: 1, enumerable: true}, hidden: {value: 2}}))
|
2010-02-09 17:50:05 +01:00
|
|
|
);
|
2010-12-03 02:03:18 +01:00
|
|
|
assert.equal('{ [hidden]: 2, visible: 1 }',
|
|
|
|
common.inspect(Object.create({},
|
|
|
|
{visible: {value: 1, enumerable: true}, hidden: {value: 2}}), true)
|
2010-02-09 17:50:05 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Objects without prototype
|
2010-12-03 02:03:18 +01:00
|
|
|
assert.equal('{ [hidden]: \'secret\', name: \'Tim\' }',
|
|
|
|
common.inspect(Object.create(null,
|
|
|
|
{name: {value: 'Tim', enumerable: true},
|
|
|
|
hidden: {value: 'secret'}}), true)
|
2010-02-09 17:50:05 +01:00
|
|
|
);
|
2010-12-03 02:03:18 +01:00
|
|
|
assert.equal('{ name: \'Tim\' }',
|
|
|
|
common.inspect(Object.create(null,
|
|
|
|
{name: {value: 'Tim', enumerable: true},
|
|
|
|
hidden: {value: 'secret'}}))
|
2010-02-09 17:50:05 +01:00
|
|
|
);
|
|
|
|
|
2009-12-01 11:20:14 +01:00
|
|
|
|
2010-01-03 01:25:09 +01:00
|
|
|
// Dynamic properties
|
2010-12-03 02:03:18 +01:00
|
|
|
assert.equal('{ readonly: [Getter] }',
|
|
|
|
common.inspect({get readonly() {}}));
|
|
|
|
|
|
|
|
assert.equal('{ readwrite: [Getter/Setter] }',
|
|
|
|
common.inspect({get readwrite() {},set readwrite(val) {}}));
|
|
|
|
|
|
|
|
assert.equal('{ writeonly: [Setter] }',
|
|
|
|
common.inspect({set writeonly(val) {}}));
|
2010-01-03 01:25:09 +01:00
|
|
|
|
2009-12-31 18:41:35 +01:00
|
|
|
var value = {};
|
2009-12-01 11:20:14 +01:00
|
|
|
value['a'] = value;
|
2010-07-15 20:47:25 +02:00
|
|
|
assert.equal('{ a: [Circular] }', common.inspect(value));
|
2010-01-03 01:25:09 +01:00
|
|
|
|
|
|
|
// Array with dynamic properties
|
2010-12-03 02:03:18 +01:00
|
|
|
value = [1, 2, 3];
|
|
|
|
value.__defineGetter__('growingLength', function() {
|
|
|
|
this.push(true); return this.length;
|
|
|
|
});
|
|
|
|
assert.equal('[ 1, 2, 3, growingLength: [Getter] ]', common.inspect(value));
|
2010-02-09 17:50:05 +01:00
|
|
|
|
|
|
|
// Function with properties
|
2010-12-03 02:03:18 +01:00
|
|
|
value = function() {};
|
2010-02-09 17:50:05 +01:00
|
|
|
value.aprop = 42;
|
2010-12-03 02:03:18 +01:00
|
|
|
assert.equal('{ [Function] aprop: 42 }', common.inspect(value));
|
2010-02-09 17:50:05 +01:00
|
|
|
|
|
|
|
// Regular expressions with properties
|
|
|
|
value = /123/ig;
|
|
|
|
value.aprop = 42;
|
2010-12-03 02:03:18 +01:00
|
|
|
assert.equal('{ /123/gi aprop: 42 }', common.inspect(value));
|
2010-02-14 13:11:59 +01:00
|
|
|
|
|
|
|
// Dates with properties
|
2010-12-03 02:03:18 +01:00
|
|
|
value = new Date('Sun, 14 Feb 2010 11:48:40 GMT');
|
2010-02-14 13:11:59 +01:00
|
|
|
value.aprop = 42;
|
2010-12-03 02:03:18 +01:00
|
|
|
assert.equal('{ Sun, 14 Feb 2010 11:48:40 GMT aprop: 42 }',
|
|
|
|
common.inspect(value)
|
2010-02-14 13:11:59 +01:00
|
|
|
);
|