Пример #1
0
        //  Method call with argument
        public void MethodCallWithArgument()
        {
            NodeExpressionPair node = ExpressionLeafs.MethodCallNoParam;

            string         methodName = "MethodCallWithArgument";
            TestExpression expr       = new TestExpression()
            {
                ResultType   = typeof(int),
                ExpectedNode = new InvokeMethod <int>()
                {
                    MethodName = methodName,
                    TargetType = typeof(DummyHelper),
                    Parameters =
                    {
                        new InArgument <int>(
                            (InvokeMethod <int>)node.GetLeafNode())
                        {
                            EvaluationOrder = 1
                        }
                    }
                },
                ExpressionTree = Expression.Call(
                    typeof(DummyHelper).GetMethod(methodName, BindingFlags.Public | BindingFlags.Static),
                    node.LeafExpression)
            };

            ExpressionTestRuntime.ValidateExpressionXaml <int>(expr);
            ExpressionTestRuntime.ValidateExecutionResult(expr, null);
        }
Пример #2
0
        //  Call generic method
        public void GenericMethodCall()
        {
            NodeExpressionPair node       = ExpressionLeafs.ConstStringValue;
            string             methodName = "GenericMethod";

            MethodInfo genericMethodHanlder            = typeof(DummyHelper).GetMethod(methodName, BindingFlags.Public | BindingFlags.Static);
            MethodInfo specializedGenericMethodHandler = genericMethodHanlder.MakeGenericMethod(typeof(string));

            TestExpression expr = new TestExpression()
            {
                ResultType   = typeof(string),
                ExpectedNode = new InvokeMethod <string>()
                {
                    MethodName           = methodName,
                    TargetType           = typeof(DummyHelper),
                    GenericTypeArguments =
                    {
                        typeof(string)
                    },
                    Parameters =
                    {
                        new InArgument <string>()
                        {
                            Expression      = (Activity <string>)node.GetLeafNode(),
                            EvaluationOrder = 1
                        }
                    }
                },
                ExpressionTree = Expression.Call(
                    specializedGenericMethodHandler, node.LeafExpression)
            };

            ExpressionTestRuntime.ValidateExpressionXaml <string>(expr);
            ExpressionTestRuntime.ValidateExecutionResult(expr, null);
        }
Пример #3
0
        private IEnumerable EnumerateTest <T>(int level, int maxLevel)
        {
            // for non-leaf node, it could be a binary expression or one of the pre-defined node
            if (level < maxLevel)
            {
                foreach (BinaryOperator op in _operators)
                {
                    TestBinaryExpression binaryExpression = new TestBinaryExpression();
                    binaryExpression.ResultType = typeof(T);
                    binaryExpression.Operator   = op;
                    foreach (TestExpression left in EnumerateTest <T>(level + 1, maxLevel))
                    {
                        left.ResultType       = typeof(T);
                        binaryExpression.Left = left;
                        foreach (TestExpression right in EnumerateTest <T>(level + 1, maxLevel))
                        {
                            right.ResultType       = typeof(T);
                            binaryExpression.Right = right;
                            yield return(binaryExpression);
                        }
                    }
                }
            }

            // leaf node return const node only
            if (level == maxLevel)
            {
                NodeExpressionPair leaf = LeafHelper.GetConstNode(typeof(T));
                yield return(new TestExpression()
                {
                    ExpectedNode = leaf.GetLeafNode(),
                    ExpressionTree = leaf.LeafExpression
                });
            }
            // root can't be leaf node
            else if (level != 0)
            {
                foreach (NodeExpressionPair leaf in _leafNode)
                {
                    Exception expectedException = null;
                    object    expectedNode      = null;
                    try
                    {
                        expectedNode = leaf.GetLeafNode();
                    }
                    catch (Exception ex)
                    {
                        expectedException = ex;
                    }

                    TestExpression te = new TestExpression()
                    {
                        ExpectedConversionException = expectedException,
                        ExpectedNode   = expectedNode,
                        ExpressionTree = leaf.LeafExpression
                    };
                    yield return(te);
                }
            }
        }
Пример #4
0
        //  Unsupported binary operator
        public void UnsupportedBinaryOperator()
        {
            NodeExpressionPair node = ExpressionLeafs.UnsupportedBinaryOperator;
            Expression <Func <ActivityContext, int> > lambdaExpression = Expression.Lambda <Func <ActivityContext, int> >((Expression)node.LeafExpression, Expression.Parameter(typeof(ActivityContext), "context"));

            NotSupportedException expectedException = new NotSupportedException(
                string.Format(ErrorStrings.UnsupportedExpressionType, ExpressionType.Coalesce));

            ExpressionTestRuntime.Convert(lambdaExpression, expectedException);
        }
Пример #5
0
        //  Static property
        public void StaticProperty()
        {
            NodeExpressionPair node = ExpressionLeafs.StaticIntProperty;
            TestExpression     expr = new TestExpression()
            {
                ResultType     = typeof(int),
                ExpectedNode   = node.GetLeafNode(),
                ExpressionTree = node.LeafExpression
            };

            ExpressionTestRuntime.ValidateExpressionXaml <int>(expr);
            ExpressionTestRuntime.ValidateExecutionResult(expr, null);
        }
Пример #6
0
        //  Constant value
        public void ConstantValue()
        {
            NodeExpressionPair node = ExpressionLeafs.ConstIntValue;
            TestExpression     expr = new TestExpression()
            {
                ResultType     = typeof(int),
                ExpectedNode   = node.GetLeafNode(),
                ExpressionTree = node.LeafExpression
            };

            ExpressionTestRuntime.ValidateExpressionXaml <int>(expr);
            ExpressionTestRuntime.ValidateExecutionResult(expr, null);
        }
Пример #7
0
        //  Method call without argument
        public void MethodCallWithoutArgument()
        {
            NodeExpressionPair node = ExpressionLeafs.MethodCallNoParam;

            TestExpression expr = new TestExpression()
            {
                ResultType     = typeof(int),
                ExpectedNode   = node.GetLeafNode(),
                ExpressionTree = node.LeafExpression
            };

            ExpressionTestRuntime.ValidateExpressionXaml <int>(expr);
            ExpressionTestRuntime.ValidateExecutionResult(expr, null);
        }
Пример #8
0
        private IEnumerable EnumerateTest <T>(int level, int maxLevel)
        {
            // for non-leaf node, it could be a unary expression or one of the pre-defined node
            if (level < maxLevel)
            {
                foreach (UnaryOperator op in _operators)
                {
                    TestUnaryExpression expression = new TestUnaryExpression
                    {
                        ResultType = typeof(T)
                    };
                    expression.ResultType = typeof(T);
                    expression.Operator   = op;
                    foreach (TestExpression operand in EnumerateTest <T>(level + 1, maxLevel))
                    {
                        operand.ResultType = typeof(T);
                        expression.Operand = operand;
                        yield return(expression);
                    }
                }
            }

            // leaf node return const node only
            if (level == maxLevel)
            {
                NodeExpressionPair leaf = LeafHelper.GetConstNode(typeof(T));
                yield return(new TestExpression()
                {
                    ExpectedNode = leaf.GetLeafNode(),
                    ExpressionTree = leaf.LeafExpression
                });
            }
            // root can't be leaf node
            else if (level != 0)
            {
                foreach (NodeExpressionPair leaf in _leafNode)
                {
                    TestExpression te = new TestExpression()
                    {
                        ExpectedNode   = leaf.GetLeafNode(),
                        ExpressionTree = leaf.LeafExpression
                    };
                    yield return(te);
                }
            }
        }
Пример #9
0
        //  Static delegate call
        public void StaticDelegateCall()
        {
            NodeExpressionPair node = ExpressionLeafs.MethodCallNoParam;

            string         delegateName = "StaticDelegate";
            TestExpression expr         = new TestExpression()
            {
                ResultType   = typeof(int),
                ExpectedNode = new InvokeMethod <int>()
                {
                    MethodName = "Invoke",
                    Parameters =
                    {
                        new InArgument <int>()
                        {
                            Expression      = (Activity <int>)node.GetLeafNode(),
                            EvaluationOrder = 1,
                        }
                    },
                    TargetObject = new InArgument <Func <int, int> >()
                    {
                        Expression = new FieldValue <DummyHelper, Func <int, int> >()
                        {
                            FieldName = delegateName,
                            Operand   = null,
                        },
                        EvaluationOrder = 0
                    }
                },
                ExpressionTree = Expression.Invoke(
                    Expression.Field(
                        null, typeof(DummyHelper).GetField(delegateName, BindingFlags.Public | BindingFlags.Static)),
                    node.LeafExpression)
            };

            ExpressionTestRuntime.ValidateExpressionXaml <int>(expr);
            ExpressionTestRuntime.ValidateExecutionResult(expr, null);
        }
Пример #10
0
        //  Convert cast expression
        public void ConvertCast()
        {
            NodeExpressionPair node = ExpressionLeafs.StaticIntField;

            Expression linq = Expression.Convert(node.LeafExpression, typeof(double));

            Activity <double> expectedActivity = new Cast <int, double>()
            {
                Checked = false,
                Operand = new InArgument <int>(
                    (Activity <int>)node.GetLeafNode())
            };

            TestExpression expr = new TestExpression()
            {
                ResultType     = typeof(double),
                ExpectedNode   = expectedActivity,
                ExpressionTree = linq
            };

            ExpressionTestRuntime.ValidateExpressionXaml <double>(expr);
            ExpressionTestRuntime.ValidateExecutionResult(expr, null);
        }