The Production Grammar Framework is finally fully integrated into MbUnit as a new Fixture (GrammarFixture).
An example:
I'm currently building a new framework for generating "relevant" data for randomly populating database (similar ideas to DbMonster). As you may have noticed, I don't practive dogfood much so I decided to start now. I'm writing the test code along with the framework (of course, I'm testing myself here so it's a bit twisted). Anyway, I had to test the following collection:
public interface IDataGeneratorCollection : ICollection
{
IDataGenerator this[DataColumn column]{get;}
IDataGenerator this[String columnName]{get;}
void Add(IDataGenerator dataGenerator);
void Remove(IDataGenerator dataGenerator);
void Remove(DataColumn column);
void Remove(String columnName);
bool Contains(IDataGenerator dataGenerator);
bool Contains(DataColumn column);
bool Contains(String columnName);
void Clear();
}
At first, I decided to write a TypeFixture and produce a bunch of unit tests for that, but I quickly got bored, so I turned myself to production grammars which are much more fun. The grammar for this collection can be summarized as follows (see the stack example):
-- dg is a IDataGenerator
add := Add(dg)
remove := Remove(dg)
contains := Contains(dg)
guardedRemove := guard(InvalidOperatoinException, remove)
empty := add,guardedRemove,contains
nonEmpty := add,remove,contains
startRule := if(collection.IsEmpty) { empty } else { nonEmpty }
MbUnit Attributes
Now that we have built a grammar we need to tell MbUnit to load the grammar it feed it with seeds: GrammarFixtureAttribute describes a production grammar fixture, GrammarAttribute describes a method that return a IGrammar instance, SeedAttribute returns an object that is feeded into the production constructor.
[GrammarFixture]
public class DataGeneratorCollectionGrammar : Grammar
{
... // rules creation etc...
[Grammar]
public Grammar This()
{
return this;
}
[Seed]
public int Seed10()
{
return 10;
}
[Seed]
public int Seed20()
{
return 20;
}
[Seed]
public int Seed50()
{
return 50;
}
[Seed]
public int Seed200()
{
return 200;
}
}
Screenshot: the grammar test case + the console output
