How to use Data TablesSuppose 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: - a header describing the content of the columns
- rows being valid combinations
- a function which applied to each row, specifies the expected behavior
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 specificationsWhen you import org.specs.Sugar._ you get some syntactic sugar that you can add to enhance your specifications: - tuples can behave as lists: (1, 2, 3).tail must_== List(2, 3)
- you can use a times method to iterate on a block of code:
3.times {println _}
var j = 0
3 times {j += _}
j must_== 6println any object with myObject.println or myObject.pln. If you want the myObject to be printed and returned you can use myObject.pp ("print and pass") you can use the constants ok and ko as equivalents of true and false you can wait for a certain amount of time mixing the WaitFor trait and using the waitFor method: waitFor(10.ms)
|
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.
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.