This is a rather funny application of unit testing. Let me state the problem:
We need to write a wrapper for every type that inherits from C. To make sure we don't miss any, we start by writing a combinatorial test that makes sure every type is mapped with a wrapper type*.
[TestFixture] public class WrapperTest { // get all the types assignable to C, from the assembly SomeAssembly [Factory(typeof(Type))] public IEnumerable<Type> Types() { foreach(Type type in SomeAssembly.GetExportedTypes()) { if (typeof(C).IsAssignableFrom(type)) yield return type; } } // tests that all types have a wrapper [CombinatorialTest] public void EnsureWrapper( [UsingFactories("Types")]Type type ) { Assert.IsTrue( IsWrapped(type) ); } // returns true of the wrapper exists private bool IsWrapped(Type type) {...} }
Ok, we can run this test and see a lot of failures. So now we know wich wrapper has to be written.... but wait, why not making the test generate the wrapper as well.
[CombinatorialTest] public void EnsureWrapper( [UsingFactories("Types")]Type type ) { if (IsWrapped(type)) return; // wrapper not found, // generating the code and dumping it to Console.Out ... Assert.Fail("Wrapper not found"); }
Now, we have test cases that give us the fix in case of a failure :)
*: this could be done by a FxCop rule as well since it is pure static analysis.
Page rendered at Thursday, December 04, 2008 8:55:22 AM UTC
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.