# Friday, January 29, 2010

We just released a new version of Pex and Moles that brings a number of bug fixes and various improvements to the behaviors for SharePoint and Asp.NET.


  • Interaction with the Source Control provider has been significantly improved for Moles
  • Support for Moles of nested types.
  • Improved logging in the Host Type
  • Improved Behaved collections
  • Added Behaved types for mscorlib and System.Web types.

Bug fixes

  • Updated several outdated section in the documentation
  • Updated missing classes in the SharePoint samples
  • Fixed a limitation of the profiler that would silently fail to instrument certain methods

Breaking Changes

  • We have formalized the naming convention of Moles and Stubs and found some holes along the way. Some mole method might have a new name under this version of the compiler.
posted on Friday, January 29, 2010 9:48:59 PM (Pacific Standard Time, UTC-08:00)  #    Comments [0]

We did a super-short movie with Nikolai Tillmann on how to use Moles to test code depending on DateTime.Now. Enjoy!


posted on Friday, January 29, 2010 3:19:31 PM (Pacific Standard Time, UTC-08:00)  #    Comments [0]

In WPF, one needs to implement the DependencyProperty pattern to make properties bindable. It usually involves a lot of boiler plate code:

  • adding a static field that names the depency property,
  • adding the instance property to the type (and make sure you follow the naming convention),
  • add validation methods and wire them in the dependency constructor

These are a number steps that need to be done again and again if you are building new WPF controls.

This is where a little transformation using CciSharp really helps with such boiler plate code. Using the DependencyAutoProperty mutator, you can define dependency properties with a single attribute. The rest is taken and validated by the compiler.

The simple scenario: Add [DependencyAutoProperty]


The rewritten code will contain a dependency property for the Value property, and will rewrite the value property getter and setters to use GetValue, SetValue instead.


Supporting default values

Default values can be specified in the DependencyProperty register method, so we want to support this as well through the DependencyAutoProperty constructor.


Supporting validation

Callbacks can be passed in the constructor of DependencyProperty to validate the values of the property. To support this, we use a simple naming convention: if you specify the Validate = true constructor argument, the rewritter will look for a static “Validate” + property name method whose signature is Func<T, bool>, where T is the property type. The rewritter will make sure this method is used – or raise a compilation error if it is missing.


An interresting point here is that the callbacks passed in the DependencyProperty.Register method are untyped (Func<object, bool>) and the user needs to make the appropriate casts himself. This is taken care of by the rewritter:


Have fun!

posted on Friday, January 29, 2010 9:11:50 AM (Pacific Standard Time, UTC-08:00)  #    Comments [0]
# Thursday, January 28, 2010

The new version of Visual Studio 2010 supports a very cool graph viewer (powered by MSAGL) and a simple XML file format (DGML) to load your own graph. I’ve added support in QuickGraph to emit .dgml file from any graph (you will need to build the sources). For example, this snippet display a very simple graph:


And the result opens magically in Visual Studio 2010!


posted on Thursday, January 28, 2010 9:09:24 AM (Pacific Standard Time, UTC-08:00)  #    Comments [1]
# Wednesday, January 27, 2010

Je présenterai une session en francais sur l’utilisation de Pex et Moles pour tester des services SharePoint aux TechDays Paris le 8 Février de 17:30 à 18:30.

Test Unitaire de Services SharePoint avec Pex et Moles.

Durant cette session, nous montrerons comment appliquer Moles et Pex pour écrire des Test Unitaires de Services SharePoint. Moles est un outil qui permet de remplacer n'importe quelle fonction .NET par un délégué ce qui permet d'isoler le code par rapport aux objets SharePoint. De plus, à partir d'un test unitaire paramétré, Pex génère automatiquement une suite de test unitaire afin de couvrir un maximum de code. Plus d'information sur Pex et Moles sur research.microsoft.com/pex

posted on Wednesday, January 27, 2010 8:58:22 AM (Pacific Standard Time, UTC-08:00)  #    Comments [0]
# Friday, January 15, 2010

We just released Pex 0.21.50115.2. This release brings bug fixes, a big renaming from “Stubs” to “Moles” and improved infrastructure to build behaved types (formerly known as beavers).

Bug Fixes

  • The Moles VsHost fails to execute unit tests in different assemblies.
  • Pex deletes the report folder it is currently writing to.
  • Support for named indexers in Stubs
  • Fixed bugs in how Pex reasons about System.Convert.ToBase64, DateTime
  • Invalid support for protected members in the stubs generation

Breaking changes

  • The Stubs framework was renamed to Moles framework. We have decided to make the Moles the center of the framework and as a consequence, renamed ‘Stubs’ to ‘Moles’. (This does not mean that we encourage writing untestable code, as Mole help to make it testable. You should still refactor your code to make it testable whenever possible, and only use Moles when that’s the only choice). The impact is that
    • Microsoft.Stubs.Framework was renamed to Microsoft.Moles.Framework
    • The moles and stubs get generated in subnamespaces ‘.Moles’ rather ‘.Stubs’.
    • See below for the list of steps to upgrade your applications.
  • BaseMembers in Moles have been deprecated: this helper is not useful as it can be acheive in a better way through a constructor. We decided to remove it to reduce code size. The second reason is that BaseMembers would only work for types inside of the same assembly, which might seem inconsistent.
  • PexGoal.Reached is replaced by PexAssert.ReachEventually(). The PexGoal class has been integrated into PexAssert through the ReachEventually method which should be used with the [PexAssertReachEventually] attribute.
  • PexChoose simplified: we’ve simplified the PexChoose API; you can now get auxiliary test inputs with a single method call: PexChoose.Value<T>(“foo”).

Migrating from previous version of Pex

Since we’ve renamed Stubs to Moles, any existing .stubx files will not work anymore.

Take a deep breath, and apply the following steps to adapt your projects:

  • change the project reference from Microsoft.Stubs.Framework.dll to Microsoft.Moles.Framework.dll
  • rename all .stubx files to .moles, and
    • rename the top <Stubs xml element to <Moles.
    • Change the XSD namespace to http://schemas.microsoft.com/moles/2010/
    • Right click on the .moles file in the Solution Explorer and change the Custom Tool Name to ‘MolesGenerator’.
    • Delete all the nested files under the .moles files
  • Remove references to any compiled .Stubs.dll files in your project
  • In general, remove all .Stubs.dll, .Stubs.xml files from your projects.
  • Rename .Stubs namespace suffixes to .Moles.
  • replace all [HostType(“Pex”)] attribute with [HostType(“Moles”)]
  • in PexAssemblyInfo.cs,
    • rename using Microsoft.Pex.Framework.Stubs to Microsoft.Pex.Framework.Moles
    • rename [assembly: PexChooseAsStubFallbackBehavior] to [assembly: PexChooseAsBehavedCurrentBehavior]
    • rename [assembly: PexChooseAsStubFallbackBehavior] to [assembly: PexChooseAsMoleCurrentBehavior]
  • In general, the ‘Fallback’ prefix has been dropped in the following methods:
    • rename FallbackAsNotImplemented() to BehaveAsNotImplemented()
    • rename class MoleFallbackBehavior to MoleBehaviors
    • rename class StubFallbackBehavior to BehavedBehavors
posted on Friday, January 15, 2010 8:14:07 PM (Pacific Standard Time, UTC-08:00)  #    Comments [0]
# Tuesday, January 12, 2010

Picture-in-Picture is a very cool feature of Camtasia that allows to embed a movie stream into a screencast. It makes the screencast more lively while keeping a perfect quality of the computer screen. You've seen this kind of screencast all over the place on Channel9. Unfortunately, this feature was limited for screen recording and whiteboard session would never look that good since we had to zoom in and out to capture… not anymore.

We invested in an eBeam device that can capture the pen strokes on any whiteboard and display them on the computer. Using this device, we can do Picture-in-Picture for Whiteboard recordings! Using that device, we use the following setup: a camera on a tripod to record the person on the whiteboard, the eBeam capture software running and Camtasia recording the computer screen. The first result of this experiment is the first episode of a new show, The Verification Corner, where Rustan Leino explains Loop Invariants. The other benefit of recording the whiteboard is that you get a hard copy out it. If you go to the Channel9 page, you’ll see that you can also download a .pdf or .pptx of all the whiteboard that Rustan wrote during the session… Go check it out and tell us what you think…


posted on Tuesday, January 12, 2010 12:24:51 AM (Pacific Standard Time, UTC-08:00)  #    Comments [0]
# Sunday, January 10, 2010

We recently acquired an eBeam system at work. This eBeam is a device that allows to record the strokes of your pens on a whiteboard or use a simple use a special pen as a mouse on a projected surface. The main application for us is to produce better RiSE Channel9 videos but there was another use that obviously cooler… projecting Craxyon Deluxe on the wall and let my 3 year old toddler take command…

posted on Sunday, January 10, 2010 9:54:26 AM (Pacific Standard Time, UTC-08:00)  #    Comments [1]
# Friday, January 01, 2010

In the previous post the fun of enhancing assertion messages, I eluded about CciSharp… but what is it really?

CciSharp is a post-compiler framework for .NET. (In a sense, it is just a sample of things that can be done with  Common Compiler Infrastructure (CCI) ). CciSharp was designed with the following idea in mind: enable to easily write assembly mutators and integrate them into the build. In that sense, CciSharp provides

  • A minimalistic layer on top of the CCI mutable source model.
  • Provide the MSBuild ‘plumbing’ to integrate into a build so you don’t have to worry about it.

Do you have some Mutators examples?

Are you wondering the kind of problems can you solve with a post-compiler? Let’s take a look at a bunch of example (the full source of those samples is available at http://ccisamples.codeplex.com):

  • Lazy Property: A lazy property computes its result once and caches it. Since it is not supported out of the box by C# or VB.NET, it requires boiler plate code to implement it. I’ve written mutator that takes a property makred with a [Lazy] attribute and implements this pattern. Here’s an example before and after of a property that returns the Environment.TickCount.
    image image
  • Weak Lazy Property: The way we cached the property result above has a dangerous side-effect: it bloats your memory. Instead of storing the result has a ‘hard’ reference, one can use a WeakReference instead. That way, the GC can reclaim the cached references whenever memory pressure becomes too high (the GC is really a great built-in caching mechanism in .NET). Again, it takes a bunch of boiler plate code to implement this pattern, which is generated automatically by the another CciSharp mutator. Here’s another before/after example:
    image image
  • ReadOnly Auto Property: Auto-properties are a great feature of C# but somehow I miss the ‘readonly’ keyword. The best that C# gives us is an auto-property with a private setter, which is not quite the same as ‘readonly’ fields. CciSharp contains a mutator that, given an auto-property with a getter and private setter,  (1) deletes  the setter, (2) makes the backing field readonly and (3) updates all the calls to the setter to direct access to the backing field. That way I get exactly what I want: a truly readonly auto-property. The snippet below shows the before/after transformation.
    image image

Those are a couple scenarios that tools like CciSharp enable. In the next post, we’ll look at the DependencyProperty pattern and how CciSharp can really help there.


posted on Friday, January 01, 2010 10:35:54 AM (Pacific Standard Time, UTC-08:00)  #    Comments [0]