Mittwoch, 24. April 2013

Simple Table DSL in Groovy

Sometimes the best way to model data is as a table. A good example where you could use tables is the spock framework. Spock is a great testing framework for Java and Groovy. In spock the test parameters can be defined as a table. The spock framework use Groovy AST transformations to implement the DSL for the tables. I think about if there is a simpler way to parse an embedded table DSL in groovy. Not with the same features then the spock table DSL. My requirements for the DSL are to define a table and to have support for variables in the table.

Here a short example of such a table:

To implement the DSL parser in groovy I use two groovy languages feature:
  1. The category feature to define the OR operators, for the different types of the table values 
  2. The dynamic of groovy for the variable support, the getProperty is overwritten and creates a object of type Var for a undefined groovy variable
The code below contains a simple groovy implementation of a table DSL parser. The simple parser now can be used to parse the table DSL from the example above. How the DSL can be use in JUnit is show in the parameterized test in the listing below. Not a perfect example, because for testing you should use Spock instead.

Kommentare:

  1. Merci pour logiciel et pour les informations


    Si tu veux telecharger des logiciels gratuit free ICIIII


    Et pour telecharger la meilleure version de logiciel test adsl

    AntwortenLöschen
  2. I like the solution and I use it. But I have a question: Do you know a soluation that supports tables with one column?

    AntwortenLöschen
  3. Hey Christian,
    My name is Ben Warner. I'm a Java developer working out of London. I've been trying to get in touch with you regarding an API I've written which does similar things to what is here in your blog post.

    This blog post was one of the inspirations to the API, and I'd like to chat about giving appropriate credits to yourself in the Javadoc and API homepage.

    Could you please get in touch>

    email is bjwarner at gmail.com

    Cheers!

    AntwortenLöschen