Exploration Through Example

Example-driven development, Agile testing, context-driven testing, Agile programming, Ruby, and other things of interest to Brian Marick
191.8 167.2 186.2 183.6 184.0 183.2 184.6

Mon, 14 Nov 2005

Attractive Ruby tests that use multi-line strings

Suppose you're testing some method whose input is a multi-line string. You could write something like this:

  def test_tags_can_be_deeply_nested
    table = "<table>
              <tr><td>
                <table>
                 <tr>
                  <td>
                    <table>
                     <tr>
                        <td>
                             Way nested
                        </td>
                     </tr>
                    </table>
                  </td>
                 </tr>
                </table>
              </td></tr>
             </table>"
    slices = TagSlices.new(table, "table")
    # blah blah blah
  end

That's fine - unless whitespace in the middle of the string is significant. The above method has no whitespace on the string's first line, but a whole lot on the others. What if I needed it all to be flush left? This is ugly:

  def test_tags_can_be_deeply_nested
    table =
"<table>
  <tr><td>
    <table>
     <tr>
      <td>
        <table>
          <tr>
            <td>
                 Way nested
            </td>
          </tr>
        </table>
      </td>
     </tr>
    </table>
  </td></tr>
 </table>"
    slices = TagSlices.new(table, "table")
    # blah blah blah
  end

I could argue that the ugliness makes it too hard to see the structure of the test and too hard to skim a file quickly and see what the tests are. That argument may even be true, but the real reason I don't like it is that it's ugly.

So I write such tests like this:

  def test_tags_can_be_deeply_nested
    table = "<table>
            . <tr><td>
            .   <table>
            .    <tr>
            .     <td>
            .       <table>
            .         <tr>
            .           <td>
            .               Way nested
            .           </td>
            .         </tr>
            .       </table>
            .     </td>
            .    </tr>
            .   </table>
            .    
            . </td></tr>
            .</table>".unindent
    slices = TagSlices.new(table, "table")
    # blah blah blah
  end

unindent removes the whitespace at the beginnings of lines, together with the discrete margin made of dots. Its code looks like this:

class String
  def unindent
    gsub(/^\s*\./m, '')
  end
end

I've fiddled around with unindent to a ridiculous degree, changing its name, how it works, how the margin is indicated. I think I've settled on this one.

## Posted at 17:00 in category /ruby [permalink] [top]

About Brian Marick
I consult mainly on Agile software development, with a special focus on how testing fits in.

Contact me here: marick@exampler.com.

 

Syndication

 

Agile Testing Directions
Introduction
Tests and examples
Technology-facing programmer support
Business-facing team support
Business-facing product critiques
Technology-facing product critiques
Testers on agile projects
Postscript

Permalink to this list

 

Working your way out of the automated GUI testing tarpit
  1. Three ways of writing the same test
  2. A test should deduce its setup path
  3. Convert the suite one failure at a time
  4. You should be able to get to any page in one step
  5. Extract fast tests about single pages
  6. Link checking without clicking on links
  7. Workflow tests remain GUI tests
Permalink to this list

 

Design-Driven Test-Driven Design
Creating a test
Making it (barely) run
Views and presenters appear
Hooking up the real GUI

 

Popular Articles
A roadmap for testing on an agile project: When consulting on testing in Agile projects, I like to call this plan "what I'm biased toward."

Tacit knowledge: Experts often have no theory of their work. They simply perform skillfully.

Process and personality: Every article on methodology implicitly begins "Let's talk about me."

 

Related Weblogs

Wayne Allen
James Bach
Laurent Bossavit
William Caputo
Mike Clark
Rachel Davies
Esther Derby
Michael Feathers
Developer Testing
Chad Fowler
Martin Fowler
Alan Francis
Elisabeth Hendrickson
Grig Gheorghiu
Andy Hunt
Ben Hyde
Ron Jeffries
Jonathan Kohl
Dave Liebreich
Jeff Patton
Bret Pettichord
Hiring Johanna Rothman
Managing Johanna Rothman
Kevin Rutherford
Christian Sepulveda
James Shore
Jeff Sutherland
Pragmatic Dave Thomas
Glenn Vanderburg
Greg Vaughn
Eugene Wallingford
Jim Weirich

 

Where to Find Me


Software Practice Advancement

 

Archives
All of 2006
All of 2005
All of 2004
All of 2003

 

Join!

Agile Alliance Logo