A couple of days ago I wrote my first post on TDD using Grails. Now let's move on to something more interesting: testing dynamic taglibs.

In another post I showed how to create a simple tag. Which looked like this:

GROOVY:
  1. import com.plink.plextile.*;
  2. class TextileTagLib {
  3.   TextParser textileParser
  4.   def textile = { attrs ->
  5.     out <<textileParser.parseTextile(attrs.text, true)
  6.   }
  7. }

When generating this test using the "grails create-tag-lib" command Grails also created a skeleton for the corresponding test:

GROOVY:
  1. class TextileTagLibTests extends GroovyTestCase {
  2.   void testSomething() {
  3.   }
  4. }

Testing calling the taglib is simple, as opposed to JspTags they don't rely on a container. You can just call the closure with the necessary attributes:

GROOVY:
  1. class TextileTagLibTests extends GroovyTestCase {   
  2.   void testRenderTextile() {
  3.     TextileTagLib tl = new TextileTagLib()
  4.     def res = tl.renderTextile(value: "# ab\n# cd\n# ef")
  5.     assert ol != res
  6.   }
  7. }

Since the textile taglib converts the given textile markup to HTML we'll need something to parse the resulting output. Enter XmlSlurper. XmlSluper is a powerful XML parser with lower overheads than XmlParser due to lazy evaluation. XmlSlurper only supports read operations. Using XmlSlurper we can actually test the output of the tag.

We'll enter something like this:

CODE:
  1. # ab
  2. # cd
  3. # ef

Which should result in something like this:

HTML:
  1.   <li>ab</li>
  2.   <li>cd</li>
  3.   <li>ef</li>
  4. </ol>

And here the actual implementation of the test:

GROOVY:
  1. class TextileTagLibTests extends GroovyTestCase {   
  2.   void testRenderTextile() {
  3.     TextileTagLib tl = new TextileTagLib()
  4.     def res = tl.renderTextile(value: "# ab\n# cd\n# ef")
  5.  
  6.     def ol = new XmlSlurper().parseText(res)
  7.    
  8.     assert ol != null
  9.        
  10.     assertEquals(3, ol.li.size())
  11.     assertEqualsTr("ab", ol.li[0].text())
  12.     assertEqualsTr("cd", ol.li[1].text())
  13.     assertEqualsTr("ef", ol.li[2].text())
  14.   }
  15.    
  16.   def assertEqualsTr(expected, actual) {
  17.     assertEquals(expected, actual.trim())
  18.   }
  19. }

Surely I should have started out with the test in first place, but hey... I first had to see how Grails taglibs work in this case! Since testing is this simple I'll actually consider doing so next time.


0 Responses to “Grails and tests - Part II: Testing dynamic taglibs using XmlSlurper”

  1. No Comments

Leave a Reply





About

Welcome to the weblog of Peter Maas. Here you'll find various posts related to stuff I like (like my kids and espresso) and stuff I do (like developing software).

JavaOne 2008 Pictures


smashmouth pub Stage being build in the nearby park javaone 2008 goodybag Charles Nutter & Guillaume Laforge Acme Anvile at CommunityOne Keynote golden_gate_warning_sign alcatraz javaone2008 keynote Joshua Bloch at JavaOne2008 Moscone Center - JavaOne Java + You on a cab nearby hotel Rudie sea_lion Stretched Limo Community One Keynote Golden Gate Cable Car line Greenland
View more photos >

Categories



Meld u aan voor PayPal en begin direct met het accepteren van creditcardbetalingen.