The Module, a new Design Pattern ? 5
I’m partially enthusiast about the partial classes. Here is a sample of code that a French Microsoftee presented to us :
<!
<span class="kwrd">public</span> <span class="kwrd">partial</span> <span class="kwrd">class</span> Person
{
<span class="kwrd">public</span> <span class="kwrd">string</span> firstname;
<span class="kwrd">public</span> <span class="kwrd">string</span> lastname;
<span class="kwrd">public</span> Person (<span class="kwrd">string</span> firstname, <span class="kwrd">string</span> lastname)
{
<span class="kwrd">this</span>.firstname = firstname;
<span class="kwrd">this</span>.lastname = lastname;
}
<span class="kwrd">public</span> <span class="kwrd">void</span> Format ()
{
<span class="rem">// ...</span>
}
}
<span class="kwrd">public</span> <span class="kwrd">partial</span> <span class="kwrd">class</span> Person
{
<span class="kwrd">public</span> <span class="kwrd">static</span> Person FromDataRow (DataRow r)
{
<span class="rem">// ...</span>
}
<span class="kwrd">public</span> <span class="kwrd">static</span> Person FromAnotherDataSource (DS s)
{
<span class="rem">// ..</span>
}
}
Isn’t it a neat design ? Grrrr, I know this is only a sample, to explain the partial keyword, but i know that lots of people will use it this way. Please, use only the partial keyword where it is meant to be.
April come she will
Tech’Ed
Microsoft invited me to the Tech’Ed Europe, some sessions seems very promising. The good point is that I have not yet visited Amsterdam, this is the occasion. I hope to see people there, feel free to mail.
AspectDNG
Thomas Gil have started using Cecil to produce ILML inside AspectDNG, sounds like he is waiting for me to end my job, so that he can use Cecil to convert an ILML file to a valid assembly.
OPath
Sebastien Ros offered me for Christmas his OPath parser library. Two options, I can write a clone of its Eon library that uses it, so that we could query objects graphs, just like Sebastien does. If I go this way, the Evaluate method should be a generic one, so that we could return strongly typed collections or items. The second option is to integrate natively into Cecil the ability to query the assembly, but it is too much Cecil centric, I’m sure other people wants to play with OPath !
Microsoft Research
I’m finishing my slides for Tuesday, again, the agenda is very promising, the thematic this year is Software Engineering, Architectural Design and High Performance Computing. Hope they’ll enjoy AOP AspectDNG’s way. I’ll be hosted there, (even if I live at 15 minutes from there). Every details here.
XScale
I’ve look toward installing Linux on my Dell Axim X30 (I don’t really know what to do with it), but it looks like it will be a very time consuming task, the Linux port is not really ready. However, I’ve added a new entry in my TODO list: trying to port Mini, Mono’s JIT, to this architecture, so that I can learn from inside the Mono runtime. I’ll need help, but Massi said one time that he was working on this on his spare time, and there is some interesting threads in the devel list to read fist. Sounds like it is a good holiday work, but I have to finish Cecil first!
Journée Académiques 2005
I have the honor to be one of the speaker of the “Academic Days” event, my talk is about Aspect Oriented Programming on the .net framework.
I have to admit that i’m really impressed, I’ll meet there people like John Lefor, PM for the Phoenix project at MSR. It seems that we share some ideas on compiler design, I hope I’ll be able to talk with him a little. Serge Lidin, the author of Inside .NET IL Assembler, and the writer of ildasm, ilasm, peverify, ..., will be here too, talking about the the evolution of the CIL. Be sure I’ll hear carefully ! Should be 3 exciting days ! All details here.
White box unit testing with AspectDNG 5
Last time, Tom was kidding me (softly) about the fact that we can do almost everything we want with AspectDNG. I begin to think he wasn’t wrong…
Once again, here is a very, very complex system that should be hard tested:
<!- code formatted by http://manoli.net/csharpformat/ ->
<span class="rem">// Main.cs</span>
<span class="kwrd">using</span> System;
<span class="kwrd">public</span> <span class="kwrd">class</span> ComplexSystem {
<span class="kwrd">private</span> <span class="kwrd">int</span> m_secret;
<span class="kwrd">public</span> ComplexSystem() {
m_secret = 0;
}
<span class="kwrd">private</span> <span class="kwrd">void</span> SecretOperation(<span class="kwrd">int</span> oper) {
m_secret += oper;
}
<span class="rem">// should always return a multiple of 2</span>
<span class="kwrd">public</span> <span class="kwrd">int</span> PublicOperation() {
<span class="kwrd">if</span> ((m_secret % 2) != 0) {
SecretOperation(1);
}
<span class="kwrd">return</span> m_secret;
}
}
This can be safely compiled to a Main.dll.
Ok, this ComplexSystem is the central point of your next application, you NEED to test it. What can be better than NUnit ? But here is the problem, you would like to be sure that the secret operation have a correct behaviour, and that the m_secret field is well setted… Two way to do that, first you use boring Reflection, or you can even take a look behind your back, you ensure that you’re alone, you change the visibility of all this, but you promise yourself that you’ll change it as soon as tests passed. Also you may note that somewhere not to forgot, this is a SECRET operation.
What about introducing a new way of testing this ? Using AspectDNG ? Something I may call white box unit testing ? Here is one aspect to do that :
<!
<span class="rem">// Tests.cs</span>
<span class="kwrd">using</span> System;
<span class="kwrd">using</span> NUnit.Framework;
<span class="rem">// dummy complex system, may have been generated</span>
<span class="kwrd">public</span> <span class="kwrd">class</span> ComplexSystem {
<span class="kwrd">public</span> <span class="kwrd">int</span> m_secret;
<span class="kwrd">public</span> <span class="kwrd">void</span> SecretOperation(<span class="kwrd">int</span> oper) {}
<span class="kwrd">public</span> <span class="kwrd">int</span> PublicOperation() { <span class="kwrd">return</span> 0; }
}
[TestFixture]
<span class="kwrd">public</span> <span class="kwrd">class</span> TestComplexSystem {
<span class="kwrd">private</span> ComplexSystem m_complexSystem;
[SetUp]
<span class="kwrd">public</span> <span class="kwrd">void</span> SetUp() {
m_complexSystem = <span class="kwrd">new</span> ComplexSystem();
}
[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> TestPublicOperation() {
Assert.AreEqual(0, m_complexSystem.PublicOperation() % 2);
m_complexSystem.SecretOperation(1);
m_complexSystem.SecretOperation(1);
m_complexSystem.SecretOperation(1);
Assert.AreEqual(0, m_complexSystem.PublicOperation() % 2);
Assert.AreEqual(4, m_complexSystem.PublicOperation());
}
[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> TestFieldSecret() {
Assert.AreEqual(0, m_complexSystem.m_secret);
m_complexSystem.SecretOperation(1);
Assert.AreEqual(1, m_complexSystem.m_secret);
}
[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> TestSecretOperation() {
<span class="kwrd">int</span> cursor = m_complexSystem.m_secret;
m_complexSystem.SecretOperation(12);
Assert.AreEqual(12, m_complexSystem.m_secret - cursor);
}
}
What can we see there ? I use a dummy ComplexSystem, that looks like really close to the real one no ? It is just a trick, with that, I’ll be able to compile this single file into a Tests.dll assembly. Once this is done, all I have to do it to inject the content of my TestComplexSystem into the real ComplexSystem. This can be done with the standard MetaAspect Insert from AspectDNG.
When weaving is done, after using a simple insert advice, here is the result :

“Et voila”, the ComplexSystem is now a test fixture itself. Pretty simple no ?
And this sample works with Mono. Of course it is a very lightweight one, but hey, it looks good !
UPDATE: You can download the sources of the example here. Just compile as described in the post, and run AspectDNG -w on the AspectDNG.xml provided.
So, what do you think about this kind of unit testing ? I’m curious about you opinion !
