Updates pretty printer to include array properties

[fixes #766][finishes #87644044]
This commit is contained in:
Greg Cobb and Gregg Van Hove
2015-02-04 11:02:53 -08:00
parent 2ab2a83a3b
commit d5dfbc98c3
3 changed files with 111 additions and 39 deletions

View File

@@ -1642,6 +1642,23 @@ getJasmineRequireObj().pp = function(j$) {
if(array.length > length){
this.append(', ...');
}
var self = this;
var first = array.length === 0;
this.iterateObject(array, function(property, isGetter) {
if (property.match(/^\d+$/)) {
return;
}
if (first) {
first = false;
} else {
self.append(', ');
}
self.formatProperty(array, property, isGetter);
});
this.append(' ]');
};
@@ -1664,18 +1681,22 @@ getJasmineRequireObj().pp = function(j$) {
self.append(', ');
}
self.append(property);
self.append(': ');
if (isGetter) {
self.append('<getter>');
} else {
self.format(obj[property]);
}
self.formatProperty(obj, property, isGetter);
});
this.append(' })');
};
StringPrettyPrinter.prototype.formatProperty = function(obj, property, isGetter) {
this.append(property);
this.append(': ');
if (isGetter) {
this.append('<getter>');
} else {
this.format(obj[property]);
}
};
StringPrettyPrinter.prototype.append = function(value) {
this.string += value;
};

View File

@@ -14,21 +14,63 @@ describe("j$.pp", function () {
expect(j$.pp(-0)).toEqual("-0");
});
it("should stringify arrays properly", function() {
expect(j$.pp([1, 2])).toEqual("[ 1, 2 ]");
expect(j$.pp([1, 'foo', {}, jasmine.undefined, null])).toEqual("[ 1, 'foo', Object({ }), undefined, null ]");
});
describe('stringify arrays', function() {
it("should stringify arrays properly", function() {
expect(j$.pp([1, 2])).toEqual("[ 1, 2 ]");
expect(j$.pp([1, 'foo', {}, jasmine.undefined, null])).toEqual("[ 1, 'foo', Object({ }), undefined, null ]");
});
it("should indicate circular array references", function() {
var array1 = [1, 2];
var array2 = [array1];
array1.push(array2);
expect(j$.pp(array1)).toEqual("[ 1, 2, [ <circular reference: Array> ] ]");
});
it("should truncate arrays that are longer than j$.MAX_PRETTY_PRINT_ARRAY_LENGTH", function() {
var originalMaxLength = j$.MAX_PRETTY_PRINT_ARRAY_LENGTH;
var array = [1, 2, 3];
it("should not indicate circular references incorrectly", function() {
var array = [ [1] ];
expect(j$.pp(array)).toEqual("[ [ 1 ] ]");
try {
j$.MAX_PRETTY_PRINT_ARRAY_LENGTH = 2;
expect(j$.pp(array)).toEqual("[ 1, 2, ... ]");
} finally {
j$.MAX_PRETTY_PRINT_ARRAY_LENGTH = originalMaxLength;
}
});
it("should stringify arrays with properties properly", function() {
var arr = [1, 2];
arr.foo = 'bar';
arr.baz = {};
expect(j$.pp(arr)).toEqual("[ 1, 2, foo: 'bar', baz: Object({ }) ]");
});
it("should stringify empty arrays with properties properly", function() {
var empty = [];
empty.foo = 'bar';
empty.baz = {};
expect(j$.pp(empty)).toEqual("[ foo: 'bar', baz: Object({ }) ]");
});
it("should stringify long arrays with properties properly", function() {
var originalMaxLength = j$.MAX_PRETTY_PRINT_ARRAY_LENGTH;
var long = [1,2,3];
long.foo = 'bar';
long.baz = {};
try {
j$.MAX_PRETTY_PRINT_ARRAY_LENGTH = 2;
expect(j$.pp(long)).toEqual("[ 1, 2, ..., foo: 'bar', baz: Object({ }) ]");
} finally {
j$.MAX_PRETTY_PRINT_ARRAY_LENGTH = originalMaxLength;
}
});
it("should indicate circular array references", function() {
var array1 = [1, 2];
var array2 = [array1];
array1.push(array2);
expect(j$.pp(array1)).toEqual("[ 1, 2, [ <circular reference: Array> ] ]");
});
it("should not indicate circular references incorrectly", function() {
var array = [ [1] ];
expect(j$.pp(array)).toEqual("[ [ 1 ] ]");
});
});
it("should stringify objects properly", function() {
@@ -77,18 +119,6 @@ describe("j$.pp", function () {
}
});
it("should truncate arrays that are longer than j$.MAX_PRETTY_PRINT_ARRAY_LENGTH", function() {
var originalMaxLength = j$.MAX_PRETTY_PRINT_ARRAY_LENGTH;
var array = [1, 2, 3];
try {
j$.MAX_PRETTY_PRINT_ARRAY_LENGTH = 2;
expect(j$.pp(array)).toEqual("[ 1, 2, ... ]");
} finally {
j$.MAX_PRETTY_PRINT_ARRAY_LENGTH = originalMaxLength;
}
});
it("should stringify RegExp objects properly", function() {
expect(j$.pp(/x|y|z/)).toEqual("/x|y|z/");
});

View File

@@ -93,6 +93,23 @@ getJasmineRequireObj().pp = function(j$) {
if(array.length > length){
this.append(', ...');
}
var self = this;
var first = array.length === 0;
this.iterateObject(array, function(property, isGetter) {
if (property.match(/^\d+$/)) {
return;
}
if (first) {
first = false;
} else {
self.append(', ');
}
self.formatProperty(array, property, isGetter);
});
this.append(' ]');
};
@@ -115,18 +132,22 @@ getJasmineRequireObj().pp = function(j$) {
self.append(', ');
}
self.append(property);
self.append(': ');
if (isGetter) {
self.append('<getter>');
} else {
self.format(obj[property]);
}
self.formatProperty(obj, property, isGetter);
});
this.append(' })');
};
StringPrettyPrinter.prototype.formatProperty = function(obj, property, isGetter) {
this.append(property);
this.append(': ');
if (isGetter) {
this.append('<getter>');
} else {
this.format(obj[property]);
}
};
StringPrettyPrinter.prototype.append = function(value) {
this.string += value;
};