Unit testing a DSL

time to read 1 min | 136 words

There is something that really bothers me when I want to test this code:

specification @vacations:
	requires @scheduling_work
	requires @external_connections

And I come up with this test:

[TestFixture]
public class QuoteGenerationTest
{
	private DslFactory dslFactory;

	[SetUp]
	public void SetUp()
	{
		dslFactory = new DslFactory();
		dslFactory.Register<QuoteGeneratorRule>(new QuoteGenerationDslEngine());
	}

	[Test]
	public void CanCompile()
	{
		QuoteGeneratorRule rule = dslFactory.Create<QuoteGeneratorRule>(
			@"Quotes/simple.boo",
			new RequirementsInformation(200, "vacations"));
		Assert.IsNotNull(rule);
	}

	[Test]
	public void WhenUsingVacations_SchedulingWork_And_ExternalConnections_AreRequired()
	{
		QuoteGeneratorRule rule = dslFactory.Create<QuoteGeneratorRule>(
			@"Quotes/simple.boo",
			new RequirementsInformation(200, "vacations"));
		rule.Evaluate();

		SystemModule module = rule.Modules[0];
		Assert.AreEqual("vacations", module.Name);
		Assert.AreEqual(2, module.Requirements.Count);
		Assert.AreEqual("scheduling_work", module.Requirements[0]);
		Assert.AreEqual("external_connections", module.Requirements[1]);
	}

	[Test]
	public void WhenUsingSchedulingWork_HasNoRequirements()
	{
		QuoteGeneratorRule rule = dslFactory.Create<QuoteGeneratorRule>(
			@"Quotes/simple.boo",
			new RequirementsInformation(200, "scheduling_work"));
		rule.Evaluate();

		Assert.AreEqual(0, rule.Modules.Count);
	}
}

I mean, I heard about disparity in number of lines, but I think that this is beyond ridiculous.