Fix inspect to not trigger dynamic properties

but to display them as special. Add unit tests to match
This commit is contained in:
Tim Caswell 2010-01-02 18:25:09 -06:00 коммит произвёл Ryan Dahl
Родитель 732c6f2036
Коммит 6c68a9679b
2 изменённых файлов: 31 добавлений и 2 удалений

Просмотреть файл

@ -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);

Просмотреть файл

@ -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)
);