[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

change usage of pp in Test::Unit assertions

mmalaidini

4/13/2008 3:14:00 AM

Hi all.

I have some Test::Unit testcases that deal with Date and DateTime. If,
for whatever reason, I have a failing assert_equal on either Date or
DateTime, the resulting message is quite difficult to read: the reason
obviously is that pp dumps the content of the object, it doesn't rely
on to_s or other more readable ways.

Is there any way to change this behaviour and make assert_equal print
Date.to_s and DateTime.to_s when the comparison fail?

TIA
MM
7 Answers

Phlip

4/13/2008 3:07:00 PM

0

mmalaidini wrote:

> I have some Test::Unit testcases that deal with Date and DateTime. If,
> for whatever reason, I have a failing assert_equal on either Date or
> DateTime, the resulting message is quite difficult to read: the reason
> obviously is that pp dumps the content of the object, it doesn't rely
> on to_s or other more readable ways.

(Use my assert{ 2.0 } instead of assert_equal), but more importantly you
need to override .inspect.

class Date
def inspect
"#{ whatever }"
end
end

Assertions generally call inspect when they fail. (My assert{ 2.0 } also
reflects the name of the variable, and the intermediate source and values of
any intermediate expressions in the assertion.)

--
Phlip
http://www.oreilly.com/catalog/9780...
"Test Driven Ajax (on Rails)"
assert_xpath, assert_javascript, & assert_ajax


Jano Svitok

4/13/2008 5:04:00 PM

0

On Sun, Apr 13, 2008 at 5:15 AM, <mmalaidini@gmail.com> wrote:
> Hi all.
>
> I have some Test::Unit testcases that deal with Date and DateTime. If,
> for whatever reason, I have a failing assert_equal on either Date or
> DateTime, the resulting message is quite difficult to read: the reason
> obviously is that pp dumps the content of the object, it doesn't rely
> on to_s or other more readable ways.
>
> Is there any way to change this behaviour and make assert_equal print
> Date.to_s and DateTime.to_s when the comparison fail?

You have two possibilities:

1. set Test::Unit::Assertions.use_pp to false, thus using Date#inspect, or
2. create your own assertion that will use to_s. See documentation for
Test::Unit::Assertions.
(the easiest way would be to copy the code for assert_equal from
assertions.rb and change the first line with build_message)

J.

Phlip

4/13/2008 5:58:00 PM

0

> You have two possibilities:
>
> 1. set Test::Unit::Assertions.use_pp to false, thus using Date#inspect

or override pretty_inspect?


mmalaidini

4/13/2008 10:56:00 PM

0

On Apr 13, 1:03 pm, Jano Svitok <jan.svi...@gmail.com> wrote:
> On Sun, Apr 13, 2008 at 5:15 AM,  <mmalaid...@gmail.com> wrote:
> >  Is there any way to change this behaviour and make assert_equal print
> >  Date.to_s and DateTime.to_s when the comparison fail?
>
> You have two possibilities:
>
> 1. set Test::Unit::Assertions.use_pp to false, thus using Date#inspect, or

That made it. I was playing with Date#inspect, but noticed that
Test::Unit::Assertions was using PP anyway.

Thanks
MM.

mmalaidini

4/13/2008 10:59:00 PM

0

On Apr 13, 11:07 am, "Phlip" <phlip2...@gmail.com> wrote:
> (Use my assert{ 2.0 } instead of assert_equal), but more importantly you
> need to override .inspect.

Oh nice, I didn't know about it, I'll give it a try.

And if you, random reader, are ignorant like me, take a look at
http://www.oreillynet.com/ruby/blog/2008/02/as... :-)

Thanks
MM

Phlip

4/14/2008 1:33:00 AM

0

mmalaidini wrote:

> Oh nice, I didn't know about it, I'll give it a try.

> And if you, random reader, are ignorant like me, take a look at
> http://www.oreillynet.com/ruby/blog/2008/02/as... :-)

Thanks for the plug, but assert{ 2.0 } also, ultimately, calls .inspect.
With no system to switch in .pretty_inspect. You can naturally still
override your .inspect to reveal useful details, but assert{ 2.0 } truncates
run-on inspections.

Whatever your assertion, you should generally assert data that don't run-on.
Instead of saying assert SimCity, say assert SimCity.aBuilding.aRoom, to
keep the diagnostics short and sweet!

--
Phlip


Eric Hodel

4/15/2008 9:16:00 AM

0

On Apr 13, 2008, at 10:03 AM, Jano Svitok wrote:
> On Sun, Apr 13, 2008 at 5:15 AM, <mmalaidini@gmail.com> wrote:
>> Hi all.
>>
>> I have some Test::Unit testcases that deal with Date and DateTime.
>> If,
>> for whatever reason, I have a failing assert_equal on either Date or
>> DateTime, the resulting message is quite difficult to read: the
>> reason
>> obviously is that pp dumps the content of the object, it doesn't rely
>> on to_s or other more readable ways.
>>
>> Is there any way to change this behaviour and make assert_equal print
>> Date.to_s and DateTime.to_s when the comparison fail?
>
> You have two possibilities:
>
> 1. set Test::Unit::Assertions.use_pp to false, thus using
> Date#inspect, or
> 2. create your own assertion that will use to_s. See documentation for
> Test::Unit::Assertions.
> (the easiest way would be to copy the code for assert_equal from
> assertions.rb and change the first line with build_message)

3. create a Date#pretty_print and DateTime#pretty_print method.