From d5dfbc98c3fac2646d6a3dce427e817fa654cee8 Mon Sep 17 00:00:00 2001 From: Greg Cobb and Gregg Van Hove Date: Wed, 4 Feb 2015 11:02:53 -0800 Subject: [PATCH] Updates pretty printer to include array properties [fixes #766][finishes #87644044] --- lib/jasmine-core/jasmine.js | 35 ++++++++++++---- spec/core/PrettyPrintSpec.js | 80 +++++++++++++++++++++++++----------- src/core/PrettyPrinter.js | 35 ++++++++++++---- 3 files changed, 111 insertions(+), 39 deletions(-) diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index b5c2708a..75cc8024 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -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(''); - } 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(''); + } else { + this.format(obj[property]); + } + }; + StringPrettyPrinter.prototype.append = function(value) { this.string += value; }; diff --git a/spec/core/PrettyPrintSpec.js b/spec/core/PrettyPrintSpec.js index d22f6e75..2686c154 100644 --- a/spec/core/PrettyPrintSpec.js +++ b/spec/core/PrettyPrintSpec.js @@ -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, [ ] ]"); - }); + 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, [ ] ]"); + }); + + 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/"); }); diff --git a/src/core/PrettyPrinter.js b/src/core/PrettyPrinter.js index 49084b69..8f86b5d8 100644 --- a/src/core/PrettyPrinter.js +++ b/src/core/PrettyPrinter.js @@ -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(''); - } 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(''); + } else { + this.format(obj[property]); + } + }; + StringPrettyPrinter.prototype.append = function(value) { this.string += value; };