Ayende @ Rahien

It's a girl

Easy extensibility: xUnit integration for DSL

I saw several solution for extending NUnit and MbUnit to add new functionality, all of them were far too complex for me. I didn't want this complexity. Here is the entire code that I had to write in order to make xUnit integrate with my DSL:

public class DslFactAttribute : FactAttribute
{
	private readonly string path;

	public DslFactAttribute(string path)
	{
		this.path = path;
	}

	protected override IEnumerable<ITestCommand> EnumerateTestCommands(MethodInfo method)
	{
		DslFactory dslFactory = new DslFactory();
		dslFactory.Register<TestQuoteGeneratorBase>(
				new TestQuoteGenerationDslEngine());
		TestQuoteGeneratorBase[] tests = dslFactory.CreateAll<TestQuoteGeneratorBase>(path);
		for(var test in tests)
		{
			Type dslType = test.GetType();
			BindingFlags flags = BindingFlags.DeclaredOnly |
				BindingFlags.Public |
				BindingFlags.Instance;
			foreach (MethodInfo info in dslType
				.GetMethods(flags))
			{
				if (info.Name.StartsWith("with"))
					yield return new DslRunnerTestCommand(dslType, info);
			}
		}
		
	}
}

And the DslTestRunnerCommand:

public class DslRunnerTestCommand : ITestCommand
{
	private readonly MethodInfo testToRun;
	private readonly Type dslType;

	public DslRunnerTestCommand(Type dslType, MethodInfo testToRun)
	{
		this.dslType = dslType;
		this.testToRun = testToRun;
	}

	public MethodResult Execute(object ignored)
	{
		object instance = Activator.CreateInstance(dslType);
		return new TestCommand(testToRun).Execute(instance);
	}

	public string Name
	{
		get { return testToRun.Name; }
	}
}

That is what I am talking about when I am talking about easy extensibility.

Comments

vedic
05/21/2008 07:40 PM by
vedic

excelent!!

... now we only need to add .GetFileEx() after .Execute(instance).

this realy is the right stuff

Comments have been closed on this topic.