From 33e4f5efbedd359dd8a04ed44d30fad838082d12 Mon Sep 17 00:00:00 2001 From: mikemoraned Date: Thu, 24 Jan 2013 21:25:32 +0000 Subject: [PATCH] Allow users to set the maximum length of array that the pretty-printer will print out. Currently, jasmine's pretty printer will iterate over an entire array, formatting every element recursively. For very large arrays, this can crash the page, or cause a 'slow script' warning. This commit exposes a 'MAX_PRETTY_PRINT_ARRAY_LENGTH' option. If an array larger than this is encountered, recursion will stop and the array length will be printed instead e.g. "Array[20000000]". The 'MAX_PRETTY_PRINT_ARRAY_LENGTH' option defaults to 100. This is length of array will not kill your browser, but will allow you to see big arrays, if you can stomach the output. --- lib/jasmine-core/jasmine.js | 8 ++++++-- spec/core/PrettyPrintSpec.js | 12 ++++++++++++ src/core/PrettyPrinter.js | 7 +++++-- src/core/base.js | 1 + 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 7cb27026..f2ef18f7 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -101,6 +101,7 @@ getJasmineRequireObj().base = (function (jasmineGlobal) { }; j$.MAX_PRETTY_PRINT_DEPTH = 40; + j$.MAX_PRETTY_PRINT_ARRAY_LENGTH = 100; j$.DEFAULT_TIMEOUT_INTERVAL = 5000; j$.getGlobal = function() { @@ -1497,14 +1498,17 @@ getJasmineRequireObj().pp = function(j$) { this.append('Array'); return; } - + var length = Math.min(array.length, j$.MAX_PRETTY_PRINT_ARRAY_LENGTH); this.append('[ '); - for (var i = 0; i < array.length; i++) { + for (var i = 0; i < length; i++) { if (i > 0) { this.append(', '); } this.format(array[i]); } + if(array.length > length){ + this.append(', ...'); + } this.append(' ]'); }; diff --git a/spec/core/PrettyPrintSpec.js b/spec/core/PrettyPrintSpec.js index a844730f..a0e23e03 100644 --- a/spec/core/PrettyPrintSpec.js +++ b/spec/core/PrettyPrintSpec.js @@ -70,6 +70,18 @@ describe("j$.pp", function () { expect(j$.pp(frozenObject)).toEqual("{ foo: { bar: 'baz' }, circular: }"); }); + 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 f59bfc3c..2c11bc62 100644 --- a/src/core/PrettyPrinter.js +++ b/src/core/PrettyPrinter.js @@ -82,14 +82,17 @@ getJasmineRequireObj().pp = function(j$) { this.append('Array'); return; } - + var length = Math.min(array.length, j$.MAX_PRETTY_PRINT_ARRAY_LENGTH); this.append('[ '); - for (var i = 0; i < array.length; i++) { + for (var i = 0; i < length; i++) { if (i > 0) { this.append(', '); } this.format(array[i]); } + if(array.length > length){ + this.append(', ...'); + } this.append(' ]'); }; diff --git a/src/core/base.js b/src/core/base.js index e152294d..d895cebb 100644 --- a/src/core/base.js +++ b/src/core/base.js @@ -9,6 +9,7 @@ getJasmineRequireObj().base = (function (jasmineGlobal) { }; j$.MAX_PRETTY_PRINT_DEPTH = 40; + j$.MAX_PRETTY_PRINT_ARRAY_LENGTH = 100; j$.DEFAULT_TIMEOUT_INTERVAL = 5000; j$.getGlobal = function() {