My favorites | Sign in
Logo
                
Search
for
Updated Dec 18, 2008 by etorreborre
AdvancedSpecifications  
How to use DataTables and syntactic sugar to write more concise examples

How to use Data Tables

Suppose you want to specify that a directory path and a file name should be combined to provide a proper full path. One way to specify this is to provide examples of different possible combinations and results (taken from the xmlRunnerUnit specification):

import org.specs.util.DataTables

object xmlRunnerSpec extends Specification with DataTables { // dont forget to mix-in the DataTables trait!
...
    "create an xml file in the specified output directory, handling file separators" in {
       "output dir" | 	"spec name" | 	"file path"  		|>
       ""           ! 	"spec1"     !	"./spec1.xml"		|  
       "result"     !	"spec1"     !	"./result/spec1.xml" 	|  
       "result/"    !	"spec1"     !	"./result/spec1.xml" 	|  
       "result\\"   !	"spec1"     !	"./result/spec1.xml" 	|  
       "/result"    !	"spec1"     !	"/result/spec1.xml" 	|
       "\\result"   !	"spec1"     !	"/result/spec1.xml" 	|
       "result/xml" ! 	"spec1"     !	"./result/xml/spec1.xml"| { (dir, spec, result) =>
           xmlRunner.outputDir = dir
           spec1.name = spec
           xmlRunner.execute
           xmlRunner.files must haveKey(result)
       }
    }
...

In the example above, you have a DataTable with:

The resulting output in case of a failure would be:

 |"output dir" | "spec name" | "file path"           |
x|"wrong"      | "spec1"     | "./spec1.xml"         | Map(./bad/spec1.xml -> <...>) doesn't have key './spec1.xml'  
 |"result"     | "spec1"     | "./result/spec1.xml"  |  

Please note the small > on the border of the table. This is what makes the table being actually executed in the specification. Think about it a the "play" command (it can be placed on any row).

How to add syntactic sugar to your specifications

When you import org.specs.Sugar._ you get some syntactic sugar that you can add to enhance your specifications:


Comment by brianchina60221, Jun 27, 2008

it was pretty frustrating to have to go through the source to find that I needed to specify "extends Specification with DataTables?" to make that syntax work.

Comment by etorreborre, Jul 14, 2008

Sorry for that (and for the late answer, Google doesn't warn me when there are new comments :-( )

I updated the page (and the home page which is also mentioning DataTables?) to specify that.


Sign in to add a comment
Hosted by Google Code