Browse files

Add tests for util.inspect

* Constructor detection and formatting
* Quote formatting
* Property name quoting
* RegExp formatting
* Accessor formatting
* Circular references
* Recurse limit
  • Loading branch information...
1 parent 48c4b8c commit cc9881adb435de500eb5d663ec3790e49c723431 @Benvie committed Dec 25, 2011
Showing with 46 additions and 21 deletions.
  1. +43 −20 lib/util.js
  2. +3 −1 test/simple/test-util-inspect.js
View
63 lib/util.js
@@ -170,9 +170,10 @@ var styles = {
// callbind parameterizes `this`
var callbind = Function.prototype.call.bind.bind(Function.prototype.call);
+var errorToString = callbind(Error.prototype.toString);
// formatter for functions shared with constructor formatter
-function functionLabel(fn, isCtor){
+function functionLabel(fn, isCtor) {
var type = isCtor ? 'Constructor' : 'Function',
label = fn.name ? ': ' + fn.name : '';
return '[' + type + label + ']';
@@ -184,7 +185,7 @@ var formatters = {
Boolean : String,
Constructor : function(f){ return functionLabel(f, true); },
Date : callbind(Date.prototype.toString),
- Error : function(e){ return '[' + Error.prototype.toString.call(e) + ']'; },
+ Error : function(e){ return '[' + errorToString(e) + ']'; },
Function : function(f){ return functionLabel(f, false); },
Null : String,
Number : String,
@@ -217,7 +218,7 @@ var qMatch = [/(')/g, /(")/g];
// quote string preferably with quote type not found in the string
// then escape slashes and opposite quotes if string had both types
-function quotes(s){
+function quotes(s) {
s = String(s).replace(/\\/g, '\\\\');
var qWith = +(s.match(qMatch[0]) === null);
return q[qWith] + s.replace(qMatch[1-qWith], '\\$1') + q[qWith];
@@ -308,7 +309,7 @@ function formatValue(value, depth, settings) {
properties = Object.getOwnPropertyNames(value);
if (typeof value === 'function') {
- properties = properties.filter(function(key){
+ properties = properties.filter(function(key) {
// hide useless properties every function has
return !(key in Function);
});
@@ -332,13 +333,20 @@ function formatValue(value, depth, settings) {
settings.seen.push(value);
var output = [];
+
+ // iterate array indexes first
if (array) {
for (var i = 0, len = value.length; i < len; i++) {
- output.push(formatProperty(value, i, depth, settings, array));
+ if (typeof value[i] === 'undefined') {
+ output.push('');
+ } else {
+ output.push(formatProperty(value, i, depth, settings, array));
+ }
}
}
- properties.forEach(function(key){
+ // properties on objects and named array properties
+ properties.forEach(function(key) {
if (!array || !numeric.test(key)) {
var prop = formatProperty(value, key, depth, settings, array);
if (prop.length) {
@@ -355,15 +363,21 @@ function formatProperty(value, key, depth, settings, array) {
var str = [];
var val = Object.getOwnPropertyDescriptor(value, key);
- // weird edge case V8 c++ accessors like process.env
+ // V8 c++ accessors like process.env that don't correctly
+ // work with Object.getOwnPropertyDescriptor
if (typeof val === 'undefined') {
val = { value: value[key], enumerable: true };
}
+<<<<<<< HEAD
// check for accessors but don't resolve them
<<<<<<< HEAD
val.set && str.push('Setter');
val.get && str.push('Getter');
=======
+=======
+
+ // check for accessors
+>>>>>>> Add tests for util.inspect
val.get && str.push('Getter');
val.set && str.push('Setter');
>>>>>>> /bad-path/
@@ -377,11 +391,13 @@ function formatProperty(value, key, depth, settings, array) {
=======
// accessor descriptor
str = settings.style(str, 'Accessor', true);
+<<<<<<< HEAD
>>>>>>> /bad-path/
+=======
+>>>>>>> Add tests for util.inspect
} else {
// data descriptor
-
if (~settings.seen.indexOf(val.value)) {
// already seen
<<<<<<< HEAD
@@ -397,14 +413,12 @@ function formatProperty(value, key, depth, settings, array) {
} else {
// recurse to subproperties
- str = formatValue(val.value, depth === null ? null : depth - 1, settings);
+ depth = depth === null ? null : depth - 1;
+ str = formatValue(val.value, depth, settings);
// prepend indentation for multiple lines
if (~str.indexOf('\n')) {
- str = str.split('\n')
- .map(function(line){ return ' ' + line; })
- .join('\n');
-
+ str = indent(str);
// trim the edges
str = array ? substring(str, 2) : '\n' + str;
}
@@ -413,7 +427,7 @@ function formatProperty(value, key, depth, settings, array) {
// array indexes don't display their name
if (array && numeric.test(key)) {
- return str === 'undefined' ? '' : str;
+ return str;
}
var nameFormat;
@@ -434,15 +448,24 @@ function formatProperty(value, key, depth, settings, array) {
key = quotes(key);
}
- // allow different coloring for non-enumerable names
if (!val.enumerable) {
- nameFormat = 'H' + nameFormat;
- key = '[' + key + ']';
+ if (settings.style.name !== 'color') {
+ // add brackets if colors are disabled
+ key = '[' + key + ']';
+ } else {
+ // use different coloring otherwise
+ nameFormat = 'H' + nameFormat;
+ }
}
return settings.style(key, nameFormat) + ': ' + str;
}
+function indent(str){
+ return str.split('\n')
+ .map(function(line) { return ' ' + line; })
+ .join('\n');
+}
function combine(output, base, braces) {
var lines = 0;
@@ -500,18 +523,18 @@ function isError(e) {
exports.isError = isError;
-function objectToString(o){
+function objectToString(o) {
return Object.prototype.toString.call(o);
}
// slice '[object Class]' to 'Class' for use in dict lookups
-function getClass(o){
+function getClass(o) {
return objectToString(o).slice(8, -1);
}
// returns true for strings, numbers, booleans, null, undefined, NaN
-function isPrimitive(o){
+function isPrimitive(o) {
return Object(o) !== o;
}
exports.isPrimitive = isPrimitive;
View
4 test/simple/test-util-inspect.js
@@ -155,5 +155,7 @@ assert.equal(util.inspect(circular), '{ p: \u00ABCircular\u00BB }');
// Recurse limit
var depth = { p1: { p2: { p3: { p4: {} } } } };
+
assert.equal(util.inspect(depth), '{ p1: { p2: { p3: \u00ABMore\u00BB } } }');
-assert.equal(util.inspect(depth, null, 3), '{ p1: { p2: { p3: { p4: \u00ABMore\u00BB } } } }');
+assert.equal(util.inspect(depth, null, 3), '{ p1: { p2: { p3: { p4: \u00ABMore\u00BB } } } }');
+assert.equal(util.inspect(depth, null, 4), '{ p1: { p2: { p3: { p4: {} } } } }');

0 comments on commit cc9881a

Please sign in to comment.