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.
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 !
new DotNetIde("x-develop"); 1
This morning Hans Cratz announced in the Mono Devel List the preview release of the new x-develop IDE.
I’m currently trying it, and you know what ? I’m happy. I’m switching my current development to it, just to see. At least it seems to be very promising. I have to admit I’m a bit jealous, it seems that I’m not the only one to want an Eclipse#. Yes, it is cross platform, and if I’ve not tried it under my Linux box, be sure I will.
Here is a quote from the mail :
Feature showcase:
- Instant detection of errors throughout all files
No need to compile in order to find out if there are errors. X-develop checks all files in the solution on-the-fly in the background and displays errors in an instant.
- Refactoring
X-develop includes refactorings for renaming variables, methods, classes, changing method signature, extracting methods and more.
- Productivity features
X-develop boosts productivity with coding tools such as Organize imports, Usage search, Code formatting, Smart templates, Go to class, Go to symbol and more.
- VS.net 2005 compatibility
X-develop uses the solutio/project concept from VS.net 2005. Solutions/projects created for Windows can be loaded and modified with X-develop on Linux.
It rocks. I’m now playing with code templates, code metrics. There is a lot of good stuff in this. I’ve not enough time to write a complete review of it, but it’s an interesting article idea for DNG.
Go, try it, and make your own idea. My question now is will it be free ?
Some links :
Edit
I was so excited that I did not even notice that it is written in Java, but does it cares ? A little, I think an Eclipse# written in .net should be a very exciting thing.