Browse Source

Fix inspect to not trigger dynamic properties

but to display them as special. Add unit tests to match
v0.7.4-release
Tim Caswell 15 years ago
committed by Ryan Dahl
parent
commit
6c68a9679b
  1. 17
      lib/sys.js
  2. 16
      test/mjsunit/test-sys.js

17
lib/sys.js

@ -98,7 +98,21 @@ var formatter = function(value, indent, parents) {
}); });
} else { } else {
return formatObject(value, indent, parents, '{}', function(x, f) { return formatObject(value, indent, parents, '{}', function(x, f) {
return f(x) + ': ' + f(value[x]); var child;
if (value.__lookupGetter__(x)) {
if (value.__lookupSetter__(x)) {
child = "[Dynamic Property]";
} else {
child = "[Dynamic Property Read-only]";
}
} else {
if (value.__lookupSetter__(x)) {
child = "[Dynamic Property Write-only]";
} else {
child = f(value[x]);
}
}
return f(x) + ': ' + child;
}); });
} }
return buffer; return buffer;
@ -113,6 +127,7 @@ var formatter = function(value, indent, parents) {
var formatObject = function(obj, indent, parents, parenthesis, entryFormatter) { var formatObject = function(obj, indent, parents, parenthesis, entryFormatter) {
var buffer = parenthesis[0]; var buffer = parenthesis[0];
var values = []; var values = [];
var x;
var localFormatter = function(value) { var localFormatter = function(value) {
return formatter(value, indent + ' ', parents); return formatter(value, indent + ' ', parents);

16
test/mjsunit/test-sys.js

@ -24,9 +24,23 @@ assert.equal('{\n "a": 1,\n "b": 2\n}', inspect({a: 1, b: 2}));
assert.equal('{\n "a": {}\n}', inspect({'a': {}})); assert.equal('{\n "a": {}\n}', inspect({'a': {}}));
assert.equal('{\n "a": {\n "b": 2\n }\n}', inspect({'a': {'b': 2}})); assert.equal('{\n "a": {\n "b": 2\n }\n}', inspect({'a': {'b': 2}}));
// Dynamic properties
assert.equal(
"{\n \"readonly\": [Dynamic Property Read-only],\n \"readwrite\": [Dynamic Property],\n \"writeonly\": [Dynamic Property Write-only]\n}",
inspect({get readonly() {return 1;},get readwrite(){return 2;},set readwrite(value){},set writeonly(val){}})
);
var value = {}; var value = {};
value['a'] = value; value['a'] = value;
assert.equal('{\n "a": [Circular]\n}', inspect(value)); assert.equal('{\n "a": [Circular]\n}', inspect(value));
value = Object.create([]); value = Object.create([]);
value.push(1); value.push(1);
assert.equal('{\n "0": 1,\n "length": 1\n}', inspect(value)); assert.equal('{\n "0": 1,\n "length": 1\n}', inspect(value));
// Array with dynamic properties
value = [1,2,3];
value.__defineGetter__('growingLength', function () { this.push(true); return this.length; });
assert.equal(
"{\n \"0\": 1,\n \"1\": 2,\n \"2\": 3,\n \"growingLength\": [Dynamic Property Read-only]\n}",
inspect(value)
);
Loading…
Cancel
Save