# The unit test that won't

**URL:** <https://www.ruby-forum.com/t/the-unit-test-that-wont/91655>\
**Category:** Ruby\
**Created:** [April 11, 2007, 7:24am UTC](https://www.ruby-forum.com/t/the-unit-test-that-wont/91655 "2007-04-11T07:24:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phillip\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7ab992/32.png) [@Phillip\_G](https://www.ruby-forum.com/u/Phillip_G)\
**Post date:** [April 11, 2007, 7:24am UTC](https://www.ruby-forum.com/t/the-unit-test-that-wont/91655/1 "2007-04-11T07:24:51Z")

</div>

Aspiring to be a good programmer, I’m using Test::Unit for ClothRed,  
writing the test before I write the code, even.

Which is all well and good, but I have a test, that produces an error  
_while it is executed_ and _not_ in the error report:

test\_textformatting(TestClothRedFormatting):  
TypeError: can’t convert nil into String  
…/test/…/lib/clothred.rb:22:in `gsub!' ../test/../lib/clothred.rb:22:in `to\_textile’  
…/test/…/lib/clothred.rb:21:in `collect!' ../test/../lib/clothred.rb:21:in `to\_textile’  
…/test/test\_formatting.rb:43:in `test_textformatting' ../test/test_formatting.rb:41:in `each’  
…/test/test\_formatting.rb:41:in `test\_textformatting’

2 tests, 1 assertions, 0 failures, 1 errors

My tests look like this:

#Works:  
def test\_tags  
assert\_equal(“ **bold** ”, ClothRed.new(“ **bold** ”).to\_textile)  
end

#Doesn’t work:  
def test\_textformatting  
FORMATTING\_STRINGS.each do |html, textile|  
test\_html = ClothRed.new(html)  
test\_html.to\_textile  
assert\_equal(textile,test\_html)  
end  
end

The Array is as follows:

FORMATTING\_STRINGS = [  
[“ **bold** ”,“ **bold** ”], [“ **strong** ”,  
“_strong_”],  
[“_emphasized_”, “_emphasized_”],  
[“_italics_”, “ **italics** ”],  
[“citation”, “??citation??”],  
[“`ClothRed#to_textile`”, “@ClothRed#to\_textile@”],  
[“ ~~delete~~ ”, “-delete-”],  
[“underline”, “+underline+”],  
[“superscript”,“^superscript^”],  
[“subscript”,“~subscript~”]  
]

An improvised test run produces this:

c:\test.rb  
**bold**  
_emphasized_  
**italics**

c:\cat test.rb

# test.rb

# 11. April 2007

# 

require ‘clothred’

TEST = ‘ **bold**  
_emphasized_  
_italics_’

test = ClothRed.new(TEST)

puts test.to\_textile

My Ruby version:  
c:\ruby -v  
ruby 1.8.5 (2006-12-25 patchlevel 12) [i386-mswin32]

The output of the test suggests, that I’m doing something wrong in my  
test, that my test is buggy, and not my code.

Can somebody enlighten me?

–  
Phillip “CynicalRyan” Gawlowski  
[http://cynicalryan.110mb.com/](http://cynicalryan.110mb.com/)

Rule of Open-Source Programming #6:

The user is always right unless proven otherwise by the developer.

---

<div class="post-metadata">

**Author:** ![Phillip\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7ab992/32.png) [@Phillip\_G](https://www.ruby-forum.com/u/Phillip_G)\
**Post date:** [April 11, 2007, 7:50am UTC](https://www.ruby-forum.com/t/the-unit-test-that-wont/91655/2 "2007-04-11T07:50:53Z")

</div>

On Wed, Apr 11, 2007 at 02:24:37PM +0900, Phillip G. wrote:

> def test\_textformatting  
> FORMATTING\_STRINGS.each do |html, textile|  
> test\_html = ClothRed.new(html)  
> test\_html.to\_textile  
> assert\_equal(textile,test\_html)

Hmm, it looks like test\_html is not a string here, but some other sort  
of  
object, so did you mean

```
    assert_equal(textfile, test_html.to_s) ?

```

or else

```
    result = test_html.to_textile
    assert_equal(textile, result) ?

```

It depends whether your ‘to\_textile’ method really changes the internal  
state of your ClothRed object, or whether its return value is the thing  
you’re interested in checking.

B.

---

<div class="post-metadata">

**Author:** ![Phillip\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7ab992/32.png) [@Phillip\_G](https://www.ruby-forum.com/u/Phillip_G)\
**Post date:** [April 11, 2007, 8:04am UTC](https://www.ruby-forum.com/t/the-unit-test-that-wont/91655/3 "2007-04-11T08:04:54Z")

</div>

On 4/11/07, Phillip G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> #Doesn’t work:  
> def test\_textformatting  
> FORMATTING\_STRINGS.each do |html, textile|  
> test\_html = ClothRed.new(html)  
> test\_html.to\_textile  
> assert\_equal(textile,test\_html)  
> end  
> end

As Brian says, I’m not really sure what test\_html.to\_textile leaves  
test\_html as but why not go back to what works for you. It seems  
simpler to  
me also.

```
def test_textformatting
   FORMATTING_STRINGS.each do |html, textile|
     assert_equal(textile, ClothRed.new(html).to_textile )
   end
end

```

also a failing message wouldn’t go astray. Something like

```
def test_textformatting
   FORMATTING_STRINGS.each do |html, textile|
     transformed = ClothRed.new(html).to_textile
     assert_equal(textile, transformed ), "\"#{html}\" should have

```

transformed to "#{textile}\Â¨ but was "#{transformed}""  
end  
end

just my 0.02

---

<div class="post-metadata">

**Author:** ![Phillip\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7ab992/32.png) [@Phillip\_G](https://www.ruby-forum.com/u/Phillip_G)\
**Post date:** [April 11, 2007, 8:24am UTC](https://www.ruby-forum.com/t/the-unit-test-that-wont/91655/4 "2007-04-11T08:24:56Z")

</div>

Daniel N wrote:

> > 
> 
> As Brian says, I’m not really sure what test\_html.to\_textile leaves  
> also a failing message wouldn’t go astray. Something like

Thanks for the input guys, looks like the error _was_ in my code after  
all.  
Here’s the why:

ClothRed#to\_textile used Array#collect! to iterate over the string that  
was to be transformed, which let the first test complete successfully as  
it was only testing for _one_ condition. As soon as I change #collect!  
to #each, the code behaved as the test expected. Although why this  
happens, I’m not sure (I guess the receiver of the block is the “count”  
for the block, and not the elements in the Array).

Which leads to the discovery of another bug now, which is in my regex.

–  
Phillip “CynicalRyan” Gawlowski  
[http://cynicalryan.110mb.com/](http://cynicalryan.110mb.com/)

Eek! That was supposed to be My Special Law, _MY_ special law, I tell  
you!

T/

---

<div class="post-metadata">

**Author:** ![Phillip\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7ab992/32.png) [@Phillip\_G](https://www.ruby-forum.com/u/Phillip_G)\
**Post date:** [April 11, 2007, 9:35am UTC](https://www.ruby-forum.com/t/the-unit-test-that-wont/91655/5 "2007-04-11T09:35:07Z")

</div>

On Wed, Apr 11, 2007 at 03:23:52PM +0900, Phillip G. wrote:

> ClothRed#to\_textile used Array#collect! to iterate over the string that  
> was to be transformed, which let the first test complete successfully as  
> it was only testing for _one_ condition. As soon as I change #collect!  
> to #each, the code behaved as the test expected. Although why this  
> happens, I’m not sure (I guess the receiver of the block is the “count”  
> for the block, and not the elements in the Array).

I don’t understand what you’ve written above - you might want to paste  
an  
example. For both Array#each and Array#collect!, the values passed to  
the  
block are the elements of the array. The difference is in what happens  
to  
the value returned by the block; #each ignores it, #collect! replaces  
the  
original array element with it.

As a separate point though: if to\_textile changes the state of your  
ClothRed  
object, you might want to consider calling it something else.

Normally, a to\_foo method returns an object of type foo, and does not  
change  
the state of the receiver. Compare Object#to\_yaml, Method#to\_proc,  
Object#to\_enum and so on.

Personally I would be surprised to find a Ruby library with a  
“to\_textile”  
method, where the main purpose of the function was to change the state  
of  
the receiver, and _not_ to return a textile object.

Regards,

Brian.

---

<div class="post-metadata">

**Author:** ![Phillip\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7ab992/32.png) [@Phillip\_G](https://www.ruby-forum.com/u/Phillip_G)\
**Post date:** [April 11, 2007, 10:23am UTC](https://www.ruby-forum.com/t/the-unit-test-that-wont/91655/6 "2007-04-11T10:23:13Z")

</div>

Brian C. wrote:

> I don’t understand what you’ve written above - you might want to paste an  
> example. For both Array#each and Array#collect!, the values passed to the  
> block are the elements of the array. The difference is in what happens to  
> the value returned by the block; #each ignores it, #collect! replaces the  
> original array element with it.

Ah, that cleared up _my_ confusion. I wanted #each in the first place,  
but made mistakes in writing my code.

Well, the learning never stops.

> As a separate point though: if to\_textile changes the state of your ClothRed  
> object, you might want to consider calling it something else.

I have changed that behavior. #to\_textile is non-destructive (at least  
it should be, I have to add a test for that).

> Personally I would be surprised to find a Ruby library with a “to\_textile”  
> method, where the main purpose of the function was to change the state of  
> the receiver, and _not_ to return a textile object.

Yes, I’d expect that from #to-textile!, too, which was my intention in  
the first place.

–  
Phillip “CynicalRyan” Gawlowski  
[http://cynicalryan.110mb.com/](http://cynicalryan.110mb.com/)

Rule of Open-Source Programming #48:

The number of items on a project’s to-do list always grows or remains  
constant.
