From ead9aa6d5a8ff92409ee13e111d5eb9700055bcf Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Sun, 4 Mar 2012 16:18:43 -0800 Subject: [PATCH] When pretty-printing objects, don't include inherited properties. When making assertions about complex objects, Jasmine's failure message are sometimes gigantic and difficult to read because the string representation of an object contains all of the methods and properties in its prototype chain. This commit causes the pretty printer to only display on object's own properties. --- spec/core/PrettyPrintSpec.js | 8 ++++++++ src/core/PrettyPrinter.js | 1 + 2 files changed, 9 insertions(+) diff --git a/spec/core/PrettyPrintSpec.js b/spec/core/PrettyPrintSpec.js index becf8a61..6d1cfc08 100644 --- a/spec/core/PrettyPrintSpec.js +++ b/spec/core/PrettyPrintSpec.js @@ -32,6 +32,14 @@ describe("jasmine.pp", function () { }, bar: [1, 2, 3]})).toEqual("{ foo : Function, bar : [ 1, 2, 3 ] }"); }); + it("should not include inherited properties when stringifying an object", function() { + var SomeClass = function() {}; + SomeClass.prototype.foo = "inherited foo"; + var instance = new SomeClass(); + instance.bar = "my own bar"; + expect(jasmine.pp(instance)).toEqual("{ bar : 'my own bar' }"); + }); + it("should stringify RegExp objects properly", function() { expect(jasmine.pp(/x|y|z/)).toEqual("/x|y|z/"); }); diff --git a/src/core/PrettyPrinter.js b/src/core/PrettyPrinter.js index a7d283b6..74fa4e7a 100644 --- a/src/core/PrettyPrinter.js +++ b/src/core/PrettyPrinter.js @@ -57,6 +57,7 @@ jasmine.PrettyPrinter.prototype.format = function(value) { jasmine.PrettyPrinter.prototype.iterateObject = function(obj, fn) { for (var property in obj) { + if (!obj.hasOwnProperty(property)) continue; if (property == '__Jasmine_been_here_before__') continue; fn(property, obj.__lookupGetter__ ? (obj.__lookupGetter__(property) !== jasmine.undefined && obj.__lookupGetter__(property) !== null) : false);