[rspec-devel] [ rspec-Patches-6207 ] Allows --diff option to diff target and expected's #inspect output

noreply at rubyforge.org noreply at rubyforge.org
Sat Oct 21 09:38:57 EDT 2006


Patches item #6207, was opened at 2006-10-17 22:32
You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3151&aid=6207&group_id=797

Category: None
Group: None
>Status: Closed
>Resolution: Accepted
Priority: 3
Submitted By: Lachie Cox (lachie)
>Assigned to: Aslak Hellesoy (aslak_hellesoy)
Summary: Allows --diff option to diff target and expected's #inspect output

Initial Comment:
The --diff option mixes in unified diffing to Spec::Expectations::ShouldBase's default message generation.

This patch adds diffing of arbitrary objects to this mixin.

If the 'target' and 'expected' objects both respond to #inspect (which they probably do because Object does) the diff of their inspect strings is added to the message.

The existing behaviour is still default (i.e. diffing of an expected String).

Motivation: This behaviour is most useful when the objects being compared pretty print their structure when #inspect'ed.

For example, this is the case with ASTs constructed by CAST (http://cast.rubyforge.org/).

It's almost useless with Rails models, as their #inspect strings are ugly printed (all on the same line, diff doesn't work so well).

----------------------------------------------------------------------

>Comment By: Aslak Hellesoy (aslak_hellesoy)
Date: 2006-10-21 09:38

Message:
Applied. Will be in 0.7.0

----------------------------------------------------------------------

Comment By: Lachie Cox (lachie)
Date: 2006-10-21 01:46

Message:
sorry, forgot to add a description to the latest version of
the patch

----------------------------------------------------------------------

Comment By: Lachie Cox (lachie)
Date: 2006-10-21 01:44

Message:
OK, I found both those problems too and fixed them.

No diffing takes place if expected ==
:no_expectation_specified allowing unary expectations to
give messages as expected.

I'm looking at the output wrt lambdas and raising, for
example in "should_not_raise should fail when specific
exception is raised".

The patch now skips diffing if target is a Proc. I don't
think diffing makes sense in this case, as the message
output is more diagnostic of the failure of the example than
descriptive of its subjects -- the lambda Proc isn't really
the subject of the example.

After excepting Procs, there was only one example to update
"Mock should fail if expectation block fails"

----------------------------------------------------------------------

Comment By: Aslak Hellesoy (aslak_hellesoy)
Date: 2006-10-19 18:27

Message:
The patch doesn't apply to HEAD of trunk. Further, the "should output unified diff message of two objects" spec assumes a particular PP rendering of the hash - which to my judgement is non-deterministic (the ordering of the key/value pairs).

It would be better to use something that PP renders more deterministically - perhaps we ought to use a particular class in the spec with custom deterministic (multiline) PP rendering.

Can you fix that?

----------------------------------------------------------------------

Comment By: Aslak Hellesoy (aslak_hellesoy)
Date: 2006-10-19 12:43

Message:
Yes, please add a full patch with updated specs. I'll review it and try to get it in for 0.7.

I love the idea!

----------------------------------------------------------------------

Comment By: Lachie Cox (lachie)
Date: 2006-10-19 09:01

Message:
Yes, if we think the patched behaviour is desirable and correct.

Should I add a patch to update the failing specs?

----------------------------------------------------------------------

Comment By: Aslak Hellesoy (aslak_hellesoy)
Date: 2006-10-19 06:27

Message:
Would it make sense to just update those failing specs so that they reflect the new diffing behaviour?

----------------------------------------------------------------------

Comment By: Lachie Cox (lachie)
Date: 2006-10-18 19:32

Message:
Here is the patch, now with pretty-printing and spec!

The method alias-hook (#default_message) of the existing
behaviour has no spec. Its covered in an rcov sense because
the --diff option is on in the dogfood specs.

This is also why the patch breaks 7 existing dogfood specs
-- failed message output under --diff is changed by the patch.

I'm not sure how to handle that.

----------------------------------------------------------------------

Comment By: Aslak Hellesoy (aslak_hellesoy)
Date: 2006-10-18 15:15

Message:
Looks like the patch didn't get attached. The Rubyforge UI tries to fool you (it happened to me too).

Try to attach the patch again - with specs please.

----------------------------------------------------------------------

Comment By: Lachie Cox (lachie)
Date: 2006-10-17 23:39

Message:
<sigh> it seems to break a few specs

----------------------------------------------------------------------

Comment By: Lachie Cox (lachie)
Date: 2006-10-17 23:34

Message:
here's a snippet for making ActiveRecord::Base#inspect
pretty print:

require 'pp'
class ActiveRecord::Base
  def inspect
    PP.pp(self,"")
  end
  def pretty_print(q)
    q.pp_object(self)
  end
end

A caveat is that diff will notice the differences between
the hex object ids printed:
-#<Stat:0xb7904b40
+#<Stat:0xb65fe704

This could be improved by manually implementing some of the
pretty printing strategy.

----------------------------------------------------------------------

You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3151&aid=6207&group_id=797


More information about the rspec-devel mailing list