From 3beb5da558db964c4b2b6491c60adab2f63c8234 Mon Sep 17 00:00:00 2001 From: Josh Cheek Date: Fri, 28 Feb 2014 16:58:37 -0600 Subject: [PATCH] Works for objects with missing/pathalogical inspect methods Fixes #20 --- lib/seeing_is_believing/line.rb | 7 ++++++- spec/line_spec.rb | 13 +++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/seeing_is_believing/line.rb b/lib/seeing_is_believing/line.rb index fa58e2d..3019524 100644 --- a/lib/seeing_is_believing/line.rb +++ b/lib/seeing_is_believing/line.rb @@ -28,7 +28,12 @@ def initialize(array = [], max_number_of_captures=Float::INFINITY) end def record_result(value) - inspected = value.inspect # only invoke inspect once, b/c the inspection may be recorded + begin + inspected = value.inspect.to_str # only invoke inspect once, b/c the inspection may be recorded + rescue NoMethodError + inspected = "#" + end + if size < @max_number_of_captures then @array << inspected elsif size == @max_number_of_captures then @array << '...' end diff --git a/spec/line_spec.rb b/spec/line_spec.rb index d8d6810..c8ccad9 100644 --- a/spec/line_spec.rb +++ b/spec/line_spec.rb @@ -19,6 +19,19 @@ def line_for(*args) line.inspect.should == '#' end + it "doesn't blow up when there is no #inspect available e.g. BasicObject" do + obj = BasicObject.new + line_for(obj).inspect.should == '#"] (1, 23) no exception>' + end + + it "doesn't blow up when #inspect returns a not-String (e.g. pathalogical libraries like FactoryGirl)" do + obj = BasicObject.new + def obj.inspect + nil + end + line_for(obj).inspect.should == '#"] (1, 23) no exception>' + end + it 'knows when it has an exception' do exception = RuntimeError.new 'omg' line = Line.new