2009/01/28

Generic syntax for the MbUnit reflective assertions.

The MbUnit v3 assertions related to type checking have been extended with a generic syntax counterpart. The four assertion methods:
  • Assert.IsInstanceOfType(Type expectedType, object actualObject);
  • Assert.IsNotInstanceOfType(Type unexpectedType, object actualObject);
  • Assert.IsAssignableFrom(Type expectedType, object actualObject);
  • Assert.IsNotAssignableFrom(Type unexpectedType, object actualObject);
And their overloads with a custom message as well, do now exist with the following syntax:
  • Assert.IsInstanceOfType<TExpected>(object actualObject);
  • Assert.IsNotInstanceOfType<TUnexpected>(object actualObject);
  • Assert.IsAssignableFrom<TExpected>(object actualObject);
  • Assert.IsNotAssignableFrom<TUnexpected>(object actualObject);
Those new methods are available in Gallio v3.0.6.649 and later.

2009/01/07

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...
}
[TestFixture]
public class FooTest
{
[VerifyContract]
public readonly IContract ImmutabilityTests = new ImmutabilityContract<Foo>();
}
So far, the contract verifier generates only one test method named AreAllFieldsReadOnly.