1

Resolved

Allow building tests with expressions

description

It would be nice to be able to specify names of functions using expressions instead of strings, this provides more refactoring safety. I coded up a utility class that does this for me, but would be nice to include it in the MutationTestBuilder class.
static class MTB<TUnit>
    {
        public static IMutationTest For<TRet>(Expression<Func<TUnit, TRet>> method)
        {
            var call = method.Body as MethodCallExpression;
            if (call == null)
                throw new ArgumentException("Expression body must be a method call");

            return MTB.For(call.Method);
        }

        public static IMutationTest For(Expression<Action<TUnit>> method)
        {
            var call = method.Body as MethodCallExpression;
            if (call == null)
                throw new ArgumentException("Expression body must be a method call");

            return MTB.For(call.Method);
        }
    }

    static class MTB
    {
        public static IMutationTest For<T>(Expression<Func<T>> method)
        {
            var call = method.Body as MethodCallExpression;
            if (call == null)
                throw new ArgumentException("Expression body must be a method call");

            return For(call.Method);
        }

        public static IMutationTest For<T>(Expression<Action> method)
        {
            var call = method.Body as MethodCallExpression;
            if (call == null)
                throw new ArgumentException("Expression body must be a method call");

            return For(call.Method);
        }

        public static IMutationTest For(MethodInfo method)
        {
            return MutationTestBuilder.For(method.DeclaringType.FullName, method.Name, method.GetParameters().Select(pi => pi.ParameterType).ToArray());
        }
    }
Usage (using Moq for It.Is<T>()):
MTB.For(() => MyExtensions.MyStaticMethod(null, null))
                .Run();
MTB<MyClass>.For(i => i.MyMethod(It.IsAny<string>(), It.IsAny<OtherClass>()))
                .Run();
MTB<MyClass>.For(i => i.MyVoidReturningMethod(It.IsAny<string>(), It.IsAny<OtherClass>()))
                .Run();

comments

freaky wrote Nov 19, 2014 at 8:56 AM

Grand Idea!

I'll try and take care of that in the next few days.

freaky wrote Dec 11, 2014 at 8:38 AM

Just so you know, I haven't forgotten this. Just haven't had time. I hope to have a couple of days over Christmas that will allow me to do something.

freaky wrote Jan 29, 2015 at 10:30 PM

I just committed these changes, but seem to have lost the build server. I'll find it again and get this pushed out to nuget.

freaky wrote Jan 29, 2015 at 10:30 PM

sorry for the delay by the way. just been hectic.

freaky wrote May 20, 2015 at 5:39 AM

Pull request is now part of trunk.

** Closed by freaky 05/19/2015 10:39PM