Vadim's Weblog

Never stop learning.

Archive for September 5th, 2007

MbUnit: Testing Internal classes

Posted by Vadim on September 5, 2007

Jonathan ‘Peli’ de Halleux wrote an article ‘Pex It: Testing Internal classes‘.   I just want to let the world know that you also can test non-public classes with MbUnit.  You need  version 2.4.1.220 or higher to use this feature.

In example bellow we test IsWhite(Char) method of System.Number class inside mscorlib assembly.  Using Reflector you can see that Number is an internal class and IsWhite is a private method of this class.  We can test this method event if we don’t have the source code.

private static readonly string MSCorLibAssembly = 
    Environment.GetEnvironmentVariable("SystemRoot")
    + @"\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll";
 
[Test]
public void InternalClassTest()
{
    string className = "System.Number";
    object obj = 
        Reflector.CreateInstance(MSCorLibAssembly, className);
    Assert.IsNotNull(obj);
    Assert.AreEqual(true,
        Reflector.InvokeMethod(
            AccessModifier.Default, obj, "IsWhite", ' '));
    Assert.AreEqual(false,
        Reflector.InvokeMethod(
            AccessModifier.Default, obj, "IsWhite", 'V'));
}

The Number class has only default constructor.  It means we don’t need to send parameters when creating an instance.

Let’s look at another example.  This time we’re going to write test for the Key and Value properties of System.Collections.KeyValuePairs class.  This class is also internal.  If you don’t believe me, use Reflector.  KeyValuePairs class has only one constructor that takes two Object parameters.

[Test]
public void InternalClassNonDefaultConstructor()
{
    string className = "System.Collections.KeyValuePairs";
    object obj = 
        Reflector.CreateInstance(MSCorLibAssembly, className, 1, 'A');
    Assert.IsNotNull(obj);
    Assert.AreEqual(1, Reflector.GetProperty(obj, "Key"));
    Assert.AreEqual('A', Reflector.GetProperty(obj, "Value"));
}

 Happy Testing :)

Posted in .Net, C#, Coding, MbUnit, TDD | 4 Comments »

Get Array type.

Posted by Vadim on September 5, 2007

Let assume that you have an int array.

int[] intArr = new int[] { 1, 2 };

If you try to get the type of the array, you’ll get int[] type.

Assert.AreEqual(typeof(int[]), intArr.GetType());

So far so good.  But what if code needs to perform different logic for array of ValueType and Reference Type elements.  We all know that int is a ValueType. However, array of int is not. So the if statement below will output ‘Reference Type’.

if (intArr.GetType().IsValueType)
    Console.Write("ValueType");
else
    Console.Write("Reference Type");

Microsoft provided us with GetElementType method that returns the Type of the object encompassed by the array.

if (intArr.GetType().GetElementType().IsValueType)
    Console.Write("ValueType");
else
    Console.Write("Reference Type");

The code above outputs ‘ValueType’ as expected.

Posted in .Net, C#, Coding, Visual Studio | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.