From d5bbaefd3ef95158f6a0ca4061cc3f59461bebcb Mon Sep 17 00:00:00 2001 From: Eero Saynatkari Date: Sun, 4 Sep 2011 18:38:01 +0800 Subject: [PATCH] Improved Pry.view_clip logic. Closes #221. --- lib/pry/pry_class.rb | 11 +++++-- test/test_pry.rb | 74 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 3 deletions(-) diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb index f16aa766..4b3bc612 100644 --- a/lib/pry/pry_class.rb +++ b/lib/pry/pry_class.rb @@ -108,12 +108,17 @@ class Pry # @param obj The object to view. # @param max_size The maximum number of chars before clipping occurs. # @return [String] The string representation of `obj`. - def self.view_clip(obj, max_size=60) - if obj.inspect.size < max_size + def self.view_clip(obj, max_length = 60) + if obj.kind_of?(Module) && obj.name && obj.name != "" && obj.name.to_s.length <= max_length + obj.name.to_s + elsif obj.inspect.length <= max_length obj.inspect - else + else "#<#{obj.class}:%#x>" % (obj.object_id << 1) end + + rescue + "unknown" end # Load Readline history if required. diff --git a/test/test_pry.rb b/test/test_pry.rb index 680bf7f5..ddfc918e 100644 --- a/test/test_pry.rb +++ b/test/test_pry.rb @@ -1076,6 +1076,80 @@ describe Pry do end end + describe "view_clip used for displaying an object in a truncated format" do + + VC_MAX_LENGTH = 60 + + describe "given an object with an #inspect string shorter than the maximum specified" do + it "returns the #inspect string" do + o = Object.new + def o.inspect; "a" * VC_MAX_LENGTH; end + + Pry.view_clip(o, VC_MAX_LENGTH).should == o.inspect + end + end + + describe "given an object with an #inspect string as long as the maximum specified" do + it "returns the #inspect string" do + o = Object.new + def o.inspect; "a" * VC_MAX_LENGTH; end + + Pry.view_clip(o, VC_MAX_LENGTH).should == o.inspect + end + end + + describe "given a regular object with an #inspect string longer than the maximum specified" do + + describe "when the object is a regular one" do + it "returns a string of the # format" do + o = Object.new + def o.inspect; "a" * (VC_MAX_LENGTH + 1); end + + Pry.view_clip(o, VC_MAX_LENGTH).should =~ /Object:0x\d+?/ + end + end + + describe "when the object is a Class or a Module" do + describe "without a name (usually a c = Class.new)" do + it "returns a string of the # format" do + c, m = Class.new, Module.new + + Pry.view_clip(c, VC_MAX_LENGTH).should =~ /Class:0x\d+/ + Pry.view_clip(m, VC_MAX_LENGTH).should =~ /Module:0x\d+/ + end + end + + describe "with a #name longer than the maximum specified" do + it "returns a string of the # format" do + c, m = Class.new, Module.new + + + def c.name; "a" * (VC_MAX_LENGTH + 1); end + def m.name; "a" * (VC_MAX_LENGTH + 1); end + + Pry.view_clip(c, VC_MAX_LENGTH).should =~ /Class:0x\d+/ + Pry.view_clip(m, VC_MAX_LENGTH).should =~ /Module:0x\d+/ + end + end + + describe "with a #name shorter than or equal to the maximum specified" do + it "returns a string of the # format" do + c, m = Class.new, Module.new + + def c.name; "a" * VC_MAX_LENGTH; end + def m.name; "a" * VC_MAX_LENGTH; end + + Pry.view_clip(c, VC_MAX_LENGTH).should == c.name + Pry.view_clip(m, VC_MAX_LENGTH).should == m.name + end + end + + end + + end + + end + it 'should set the hooks default, and the default should be overridable' do Pry.input = InputTester.new("exit-all") Pry.hooks = {