dynamic C# in unit tests

by Justin Chase 24. June 2009 14:23

I’ve been writing some unit tests lately that require quite a bit of casting. This gets tiring pretty fast so I went ahead and decided to give the dynamic keyword a try. I’ll show the before and after examples and let you decide.

Here is the non-dynamic version.

[Test]
public void InvokeWithAddPlusInvokeTest()
{
    string code = "f(1 + 2) + g()";
    var b1 = (BinaryExpression)this.pipeline.Compile(code);

    var f = (MethodInvokeExpression)b1.Left;
    var g = (MethodInvokeExpression)b1.Right;

    var b2 = (BinaryExpression)f.Parameters.Single();

    var one = (PrimitiveExpression)b2.Left;
    var two = (PrimitiveExpression)b2.Right;

    Assert.That(b1.Operator == BinaryOperator.Add);
    Assert.That(b2.Operator == BinaryOperator.Add);

    Assert.That(((ReferenceExpression)f.Target).Name == "f");
    Assert.That(((ReferenceExpression)g.Target).Name == "g");
    Assert.That(one.Value == "1");
    Assert.That(two.Value == "2");
}

In this test I am compiling an expression into an AST and digging around to verify that the correct nodes were created in the right places in the tree.

Here is the dynamic version.

[Test]
public void InvokeWithAddPlusInvokeTest()
{
    string code = "f(1 + 2) + g()";
    dynamic b1 = this.pipeline.Compile(code);
    dynamic b2 = Enumerable.Single(b1.Left.Parameters);

    Assert.That(b1.Operator == BinaryOperator.Add);
    Assert.That(b2.Operator == BinaryOperator.Add);

    Assert.That(b1.Left.Target.Name == "f");
    Assert.That(b1.Right.Target.Name == "g");
    Assert.That(b2.Left.Value == "1");
    Assert.That(b2.Right.Value == "2");
}

A lot shorter that’s for sure. The only downside is that if I change the nodes I will no longer get compile time warnings… but I will get unit test errors so this shouldn’t theoretically matter. I also no longer get intellisense so I either have to just know the structure of the objects or use the debugger to figure it out. Still the simplicity is looking good.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

C# | .NET | MetaSharp

Add comment


 

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

Justin Chase

sweetest hat ever

I am a software developer from St. Paul MN and I work for Microsoft on the Expression team. This blog is about various technical topics I find myself encountering here and there. In addition to loving WPF and Xaml and Expression studio in general I have a special interest in DSLs, programming languages and games.

RecentComments

Comment RSS

Calendar

<<  March 2010  >>
MoTuWeThFrSaSu
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar