views:

173

answers:

3

And I mean this in the easiest way. Say you have a function with the following signature:

public static Expression<Func<T, bool>> CreateExpression<T>(string value)
{
    // ...
}

Usually it will create a more complex expression of some sort, but if the value is null the method should return a constant, always true expression. In other words:

public static Expression<Func<T, bool>> CreateExpression<T>(string value)
{
    if(value == null)
        return x => true;

    // ...
}

Is this something I can create a unit test for? That when I send in null as the value, I do get back a constant true expression?

+2  A: 

There's no way to do so that I know of. However, if you're willing to refactor a bit:

class Sample<T>
{
    public static Func<T, bool> Identity = x => true;

    public static Func<T, bool> CreateExpression(string value)
    {
        if(value == "foo")
            return Identity;

        return x => value.Length % 2 == 0;
    }
}

class Test
{
    public void DoTest()
    {
        Debug.Assert(Sample<string>.CreateExpression("foo") == Sample<string>.Identity);
    }
}
Anton Gogolev
This is also a very good idea, but the method in question isn't part of a class where an identity like that would be good to have. Up-voting though, cause that might be useful some other time :)
Svish
+5  A: 

It would be easy enough to test for exactly that expression (the body will be a ConstantExpression with value true). But in the general case? No - too complex.

static bool IsConstantTrue(LambdaExpression lambda)
{
    return lambda.Body.NodeType == ExpressionType.Constant
        && true.Equals(((ConstantExpression)lambda.Body).Value);
}

with

Expression<Func<SomeType, bool>> exp = x => true; // or some method that 
                                                  // returns a lambda expression
Assert.IsTrue(IsConstantTrue(exp));
Marc Gravell
That was what I was thinking of, yes. Could you make an example of how you'd write that test for an expression?
Svish
Fantastic! Thank you :)
Svish
A: 

What do you mean be "simple" here? It's kind of a woolly term...

In general, the only thing we can seay is that this is yet another manifestation of the halting problem. Consider, how can you you determine the result of a function until all possible parameters, unless you actually run it under all possible parameters? Apart from being practically infeasible, you can't even guarantee a result because of the nature of the halting problem (you don't know the method will even terminate, or what path it may take in the indefinite future).

Noldorin
But when you have the expression tree, you can check if it is a constant expression or not. I suppose you should also be able to maybe even scan through the whole expression tree and check that incoming parameters are never really used or something. But that's just me thinking. Don't know enough about expression trees :p
Svish
Oh, in this case that it's just a constant, and there's no conditional logic/loops or anything, you should be safe. Wasn't sure that's what you mean though.
Noldorin
Yeah, sorry, my question was a bit unclear :)
Svish
No worries. I'll leave this answer here just for theoretical justification if anyone wonders...
Noldorin