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

M

Michael Schuerig

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
 
P

pat eyler

=20
I have unit tests for several Rails helper methods that generate HTML.
For these I'm using this rather imperfect assert method
=20
def assert_equal_ignoring_whitespace(expected, actual)
assert_equal(clean_html(expected), clean_html(actual))
end
=20
def clean_html(html)
html.strip.gsub(/\s+/m, ' ').gsub(/>\s+</m, '><')
end
=20
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.
=20
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.
=20
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=20
brush up against some of its rough edges. It may be a=20
bug or a place where the documentation isn't clear (or doesn't exist),=20
or maybe it's not available in your language.\n\nIf you want to=20
give back to the community, you can work on any of these problems,=20
even if you're not a programmer.\n\nYou could write a bug report=20
for the developers (and submit a patch if you're a programmer).\n\nYou=20
could write or extend existing documentation.\n\nIf you're really up=20
for a challenge. You could even work on translating the application=20
and/or documentation.\n\nHowever you choose to get involved, you=20
should start by contacting the developers."> expected but was
<"If you use open source software, you're sure to brush up=20
against some of its rough edges. It may be a bug, or a place=20
where the documentation isn't clear (or doesn't exist), or maybe=20
it's not available in your language.\n\nIf you want to give back=20
to the community you can work on any of these problems, even if=20
you're not a programmer.\n\nYou could write a bug report for the=20
developers (and submit a patch if you're a programmer).\n\nYou=20
could write or extend existing documentation.\n\nIf you're really=20
up for a challenge, you could even work on translating the=20
application and/or documentation.\n\nHowever you choose to get=20
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=20
any of these problems, even if you're not a programmer.
---
If you want to give back to the community you can work on=20
any of these problems, even if you're not a programmer.
1 tests, 1 assertions, 1 failures, 0 errors




=20
Michael
=20
--
Michael Schuerig Most people would rather die than think.
mailto:[email protected] In fact, they do.
http://www.schuerig.de/michael/ --Bertrand Russell
=20
=20
=20


--=20
thanks,
-pate
 
M

Michael Schuerig

pat said:
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
 
P

pat eyler

pat eyler wrote:
=20
=20
Looks good in principle, but gives me an exception.
=20
/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
=20
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.
=20
Michael
=20
--
Michael Schuerig The Fifth Rider of the Apocalypse
mailto:[email protected] is a programmer.
http://www.schuerig.de/michael/
=20
=20
=20


--=20
thanks,
-pate
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,944
Members
47,492
Latest member
gabbywilliam

Latest Threads

Top