Friday, 28 May 2010

Code Contracts and Assert() weirdness

One of my first steps was to build a set of tests to verify the correctness of my implementation of the Contracts namespace. However, a curious problem occured with Asserts in the Microsoft System.Diagnostics.Contracts namespace. By default a failed Assert shows a dialog box with an error message and a stack-trace. This behaviour can be changed by adding/removing listeners in System.Diagnostics.Debug.Listeners - for example, by clearing all the listeners a failed assert is ignored:

This code does not show a dialog box with the assert information because the default listener has been removed. It is completely possibly to add custom listeners that perform whatever behaviour is required.

However, the weirdness is that when using the Contract.Assert() method in System.Diagnostics.Contracts namespace, the assert behaviour cannot be modified using the Listeners collection. Although it appears to be a normal assert, it cannot be - it does not use the Debug.Listeners collection to define its behaviour, and there doesn't appear to be any other Listeners collection that could effect the behaviour.

Therefore the following code:

shows an assert dialog box.

This means it is not possible to run a test on the Microsoft implementation which uses a custom listener to check that the asserts generated are correct. So my strategy of running a set of tests against the Microsoft implementation and my implementation to ensure they produce the same behaviour is not going to work.


1 comment:

  1. statically check code contracts working from mono develop would be very cool. It would also provide a competitive advantage to monodevelop. Statically checked code contracts are only available in the very expensive MSDN licences.