The Immutability Contract Verifier

MbUnit v3 has a new contract verifier called VerifyImmutabilityContract. Basically, it verifies that every field of the tested type is declared with the readonly keyword. It is important to note that the verification is done recursively over all the inner field types, until some known primitive is found, such as String, Boolean, or Int32.

The original idea comes from Jeff Brown which was inspired by a post of Oren Eini.

Here is simple class that implements that sort of immutability, and a test fixture which declares the contract verifier.
public class Foo
private readonly int value;
private readonly string name;

// some code here...
public class FooTest
public readonly IContract ImmutabilityTests = new ImmutabilityContract<Foo>();
So far, the contract verifier generates only one test method named AreAllFieldsReadOnly.

No comments: