[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Test::Unit: useful assert and diff for HTML?

Michael Schuerig

7/14/2005 2:17:00 PM


I have unit tests for several Rails helper methods that generate HTML.
For these I'm using this rather imperfect assert method

def assert_equal_ignoring_whitespace(expected, actual)
assert_equal(clean_html(expected), clean_html(actual))
end

def clean_html(html)
html.strip.gsub(/\s+/m, ' ').gsub(/>\s+</m, '><')
end

Semantics may be distorted as differences in significant whitespace can
be distorted. The advantage is that I can lay out the expected HTML
fragment in a legible fashion. I'd prefer to use a stricter check,
though.

Another issue is how the mismatch of actual and expected values is
displayed out of the box. For strings of several hundred characters of
length, it is not very helpful to have just the two strings. It would
be a whole lot nicer to have the differences emphasized.

Before I dig into the Test::Unit code myself, I hope that someone has
already put in the effort...

Michael

--
Michael Schuerig Most people would rather die than think.
mailto:michael@schuerig.de In fact, they do.
http://www.schuerig.d... --Bertrand Russell

3 Answers

pat eyler

7/14/2005 2:43:00 PM

0

On 7/14/05, Michael Schuerig <michael@schuerig.de> wrote:
>
> I have unit tests for several Rails helper methods that generate HTML.
> For these I'm using this rather imperfect assert method
>
> def assert_equal_ignoring_whitespace(expected, actual)
> assert_equal(clean_html(expected), clean_html(actual))
> end
>
> def clean_html(html)
> html.strip.gsub(/\s+/m, ' ').gsub(/>\s+</m, '><')
> end
>
> Semantics may be distorted as differences in significant whitespace can
> be distorted. The advantage is that I can lay out the expected HTML
> fragment in a legible fashion. I'd prefer to use a stricter check,
> though.
>
> Another issue is how the mismatch of actual and expected values is
> displayed out of the box. For strings of several hundred characters of
> length, it is not very helpful to have just the two strings. It would
> be a whole lot nicer to have the differences emphasized.
>
> Before I dig into the Test::Unit code myself, I hope that someone has
> already put in the effort...

I don't know about the Test::Unit code, but for highlighting diffs, the
wonderful unit_diff script that comes with ZenTest is perfect. Here's
an example:


$ ts_bigfailure.rb | unit_diff.rb
Listing 14. Test::Unit output
$ ruby ts_long.rb
Loaded suite ts_long
Started
F
Finished in 0.02506 seconds.

1) Failure:
test_long(TestLong) [ts_long.rb:33]:
<"If you use open source software, you're sure to
brush up against some of its rough edges. It may be a
bug or a place where the documentation isn't clear (or doesn't exist),
or maybe it's not available in your language.\n\nIf you want to
give back to the community, you can work on any of these problems,
even if you're not a programmer.\n\nYou could write a bug report
for the developers (and submit a patch if you're a programmer).\n\nYou
could write or extend existing documentation.\n\nIf you're really up
for a challenge. You could even work on translating the application
and/or documentation.\n\nHowever you choose to get involved, you
should start by contacting the developers."> expected but was
<"If you use open source software, you're sure to brush up
against some of its rough edges. It may be a bug, or a place
where the documentation isn't clear (or doesn't exist), or maybe
it's not available in your language.\n\nIf you want to give back
to the community you can work on any of these problems, even if
you're not a programmer.\n\nYou could write a bug report for the
developers (and submit a patch if you're a programmer).\n\nYou
could write or extend existing documentation.\n\nIf you're really
up for a challenge, you could even work on translating the
application and/or documentation.\n\nHowever you choose to get
involved, you should start by contacting the developers.">.

1 tests, 1 assertions, 1 failures, 0 errors
$ ruby ts_long.rb | unit_diff.rb
Loaded suite ts_long
Started
F
Finished in 0.017676 seconds.

1) Failure:
test_long(TestLong) [ts_long.rb:33]:
3c3
< If you want to give back to the community, you can work on
any of these problems, even if you're not a programmer.
---
> If you want to give back to the community you can work on
any of these problems, even if you're not a programmer.
1 tests, 1 assertions, 1 failures, 0 errors





>
> Michael
>
> --
> Michael Schuerig Most people would rather die than think.
> mailto:michael@schuerig.de In fact, they do.
> http://www.schuerig.d... --Bertrand Russell
>
>
>


--
thanks,
-pate
-------------------------
We are often unable to tell people what they need to know, because
they want to know something else, and would therefore only
misunderstand what we said
- the Raven (George MacDonald, Lilith)


Michael Schuerig

7/14/2005 3:59:00 PM

0

pat eyler wrote:

> I don't know about the Test::Unit code, but for highlighting diffs,
> the
> wonderful unit_diff script that comes with ZenTest is perfect. Here's
> an example:
>
>
> $ ts_bigfailure.rb | unit_diff.rb
> Listing 14. Test::Unit output
> $ ruby ts_long.rb
> Loaded suite ts_long
> Started
> F
> Finished in 0.02506 seconds.

Looks good in principle, but gives me an exception.

/usr/local/bin/unit_diff.rb:78:in `temp_file': private method `gsub!'
called for nil:NilClass (NoMethodError)
from /usr/local/bin/unit_diff.rb:121
from /usr/local/bin/unit_diff.rb:95:in `each'
from /usr/local/bin/unit_diff.rb:95

Well, it shouldn't be too hard to track this down.

Michael

--
Michael Schuerig The Fifth Rider of the Apocalypse
mailto:michael@schuerig.de is a programmer.
http://www.schuerig.d...

pat eyler

7/14/2005 4:04:00 PM

0

On 7/14/05, Michael Schuerig <michael@schuerig.de> wrote:
> pat eyler wrote:
>
> > I don't know about the Test::Unit code, but for highlighting diffs,
> > the
> > wonderful unit_diff script that comes with ZenTest is perfect. Here's
> > an example:
> >
> >
> > $ ts_bigfailure.rb | unit_diff.rb
> > Listing 14. Test::Unit output
> > $ ruby ts_long.rb
> > Loaded suite ts_long
> > Started
> > F
> > Finished in 0.02506 seconds.
>
> Looks good in principle, but gives me an exception.
>
> /usr/local/bin/unit_diff.rb:78:in `temp_file': private method `gsub!'
> called for nil:NilClass (NoMethodError)
> from /usr/local/bin/unit_diff.rb:121
> from /usr/local/bin/unit_diff.rb:95:in `each'
> from /usr/local/bin/unit_diff.rb:95
>
> Well, it shouldn't be too hard to track this down.

Please pass the fix back to zenspider and eric hodel from the seattle.rb --
I'm sure they'd appreciate it.

>
> Michael
>
> --
> Michael Schuerig The Fifth Rider of the Apocalypse
> mailto:michael@schuerig.de is a programmer.
> http://www.schuerig.d...
>
>
>


--
thanks,
-pate
-------------------------
We are often unable to tell people what they need to know, because
they want to know something else, and would therefore only
misunderstand what we said
- the Raven (George MacDonald, Lilith)