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

**URL:** <https://rubytalk.org/t/test-unit-useful-assert-and-diff-for-html/19524>\
**Category:** ruby-talk\
**Created:** [14 July 2005 14:20 UTC](https://rubytalk.org/t/test-unit-useful-assert-and-diff-for-html/19524 "2005-07-14T14:20:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Schuerig](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@Michael\_Schuerig](https://rubytalk.org/u/Michael_Schuerig)\
**Post date:** [14 July 2005 14:20 UTC](https://rubytalk.org/t/test-unit-useful-assert-and-diff-for-html/19524/1 "2005-07-14T14:20:51Z")

</div>

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)  
&nbsp;&nbsp;assert\_equal(clean\_html(expected), clean\_html(actual))  
end

def clean\_html(html)  
&nbsp;&nbsp;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](mailto:michael@schuerig.de) In fact, they do.  
> [http://www.schuerig.de/michael/](http://www.schuerig.de/michael/) --Bertrand Russell

---

<div class="post-metadata">

**Author:** ![Guest15](https://avatars.discourse-cdn.com/v4/letter/g/6de8d8/32.png) [@Guest15](https://rubytalk.org/u/Guest15)\
**Post date:** [14 July 2005 14:42 UTC](https://rubytalk.org/t/test-unit-useful-assert-and-diff-for-html/19524/2 "2005-07-14T14:42:46Z")

</div>

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.

> **···**
>
> 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)  
> > &nbsp;&nbsp;assert\_equal(clean\_html(expected), clean\_html(actual))  
> > end
> > 
> > def clean\_html(html)  
> > &nbsp;&nbsp;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...
> 
> ---
> 
> > 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](mailto:michael@schuerig).de In fact, they do.  
> > [Michael Schürig | Sentenced to making sense](http://www.schuerig.de/michael/) --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)

---

<div class="post-metadata">

**Author:** ![Michael\_Schuerig](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@Michael\_Schuerig](https://rubytalk.org/u/Michael_Schuerig)\
**Post date:** [14 July 2005 16:00 UTC](https://rubytalk.org/t/test-unit-useful-assert-and-diff-for-html/19524/3 "2005-07-14T16:00:52Z")

</div>

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)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/local/bin/unit\_diff.rb:121  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/local/bin/unit\_diff.rb:95:in `each'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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](mailto:michael@schuerig).de is a programmer.  
> [Michael Schürig | Sentenced to making sense](http://www.schuerig.de/michael/)

---

<div class="post-metadata">

**Author:** ![Guest15](https://avatars.discourse-cdn.com/v4/letter/g/6de8d8/32.png) [@Guest15](https://rubytalk.org/u/Guest15)\
**Post date:** [14 July 2005 16:04 UTC](https://rubytalk.org/t/test-unit-useful-assert-and-diff-for-html/19524/4 "2005-07-14T16:04:26Z")

</div>

> 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)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/local/bin/unit\_diff.rb:121  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/local/bin/unit\_diff.rb:95:in `each'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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.

> **···**
>
> On 7/14/05, Michael Schuerig \<michael@schuerig.de\> wrote:
> 
> > Michael
> > 
> > --  
> > Michael Schuerig The Fifth Rider of the Apocalypse  
> > [mailto:michael@schuerig](mailto:michael@schuerig).de is a programmer.  
> > [http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)
> 
> --  
> 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)
