There are several ways to generate data for tests (not only unit tests), for example, Object Mother, collectors, etc. Another useful approach is to write test data in plain text:
product: Main; prices: 145, 255; Expire: 10-Apr-2011; qty: 2; includes: Sub
product: Sub; prices: 145, 255; Expire: 10-Apr-2011; qty: 2
and then parse it into C # objects. It is easy to use in unit tests (since deep internal collections can be written on one line), it is even more convenient to use in a FitNesse-like system (because this DSL fits naturally into a wiki), etc.
So, I use this and write a parser, but it is tedious to write each time. I am not a great expert in DSL / language parsers, but I think they can help here. What would be the right option? I only heard about:
- DSL (I mean, any DSL)
- Boo (which I think DSL can do)
- ANTLR
but I don’t even know which one to choose and where to start.
So the question is: is it wise to use some kind of DSL to generate test data? What would you suggest to do? Are there any existing cases?
Update: it looks like I was not clear enough. This is not about converting the source string to an object. Look at the first line and bind it to
var main = Product.New("Main")
.AddPrice(Price.New(145).WithType(PriceType.Main).AndQty(2))
.AddPrice(Price.New(255).WithType(PriceType.Maintenance).AndQty(2))
.Expiration(new DateTime(10, 04, 2011));
var sub = Product
.New("Sub").Parent(main)
.AddPrice(...));
main.AddSubProduct(sub);
products.Add(main);
products.Add(sub);
And note that I first create an additional product, and then add it to main, even if it is listed in reverse order. Prices are handled in a special way. I want to specify the name of the Sub-product and get a link to it - created. I want to list all the product properties - FLAT and NON REPEATATIVE - on one line. I want to use default values for properties. Etc.
: , DSL, . DSL.