public static InArgument <T> GetInArgumentFromExpectedNode <T>(TestExpression te)
        {
            InArgument <T> retInArg = null;

            // leaf node:
            if (te.GetType() == typeof(TestExpression))
            {
                if (te.ExpectedNode is Activity <T> )
                {
                    retInArg = (Activity <T>)te.ExpectedNode;
                }
                else if (te.ExpectedNode is Variable <T> )
                {
                    retInArg = (Variable <T>)te.ExpectedNode;
                }
                else
                {
                    //Log.TraceInternal("Expect a generic type with type parameter: {0}", typeof(T).ToString());
                    //Log.TraceInternal("Expected node type is: {0}", te.ExpectedNode.GetType().ToString());
                    throw new NotSupportedException("Not supported expected node type");
                }
            }
            // non-leaf node:
            else if (te is TestBinaryExpression || te is TestUnaryExpression)
            {
                retInArg = (Activity <T>)te.CreateExpectedActivity();
            }

            return(retInArg);
        }
        // variablesActual is set when variable comparison makes the difference. Example: memberExpression.NoneGenericVariableGet
        public static void ValidateExecutionResult(TestExpression te, List <Variable> variablesExpected, List <Variable> variablesActual, params Type[] refTypes)
        {
            //MethodInfo createExpectedWorkflowMethodInfo = te.GetType().GetMethod("CreateExpectedWorkflow", BindingFlags.Public | BindingFlags.Instance);
            //Sequence expectedSequence = (Sequence)createExpectedWorkflowMethodInfo.MakeGenericMethod(te.ResultType).Invoke(te, BindingFlags.Instance | BindingFlags.InvokeMethod, null, null, null);
            //if (variablesExpected != null)
            //{
            //    foreach (Variable v in variablesExpected)
            //    {
            //        expectedSequence.Variables.Add(v);
            //    }
            //}

            //string xaml = PartialTrustXamlServices.Save(expectedSequence);
            ////Log.TraceInternal("Expected workflow:\n{0}", xaml);

            //Activity clonedWorkflowElement = (Activity)PartialTrustXamlServices.Load(new StringReader(xaml));

            //var namespaces = from t in refTypes
            //                 select t.Namespace;

            //var assemblies = from t in refTypes
            //                 select new AssemblyReference() { Assembly = t.Assembly };

            //// TextExpression.SetNamespaces(clonedWorkflowElement, namespaces.ToArray());
            //// TextExpression.SetReferences(clonedWorkflowElement, assemblies.ToArray());

            //Exception expectedException = null;
            //string expectedResult = ExecuteWorkflowAndGetResult(clonedWorkflowElement, out expectedException);
            ////Log.TraceInternal("Expected result: {0}\n", expectedResult);

            //MethodInfo createActualWorkflowMethodInfo = te.GetType().GetMethod("CreateActualWorkflow", BindingFlags.Public | BindingFlags.Instance);
            //Sequence actualSequence = (Sequence)createActualWorkflowMethodInfo.MakeGenericMethod(te.ResultType).Invoke(te, BindingFlags.Instance | BindingFlags.InvokeMethod, null, null, null);
            //// TextExpression.SetNamespaces(actualSequence, namespaces.ToArray());
            //// TextExpression.SetReferences(actualSequence, assemblies.ToArray());

            //List<Variable> varList = variablesActual ?? variablesExpected;
            //if (varList != null)
            //{
            //    foreach (Variable v in varList)
            //    {
            //        actualSequence.Variables.Add(v);
            //    }
            //}

            //// xaml = PartialTrustXamlServices.Save(actualSequence);
            ////Log.TraceInternal("Actual workflow:\n{0}", xaml);
            //// clonedWorkflowElement = (Sequence)XamlServices.Load(new StringReader(xaml));
            //Exception actualException = null;
            //string actualResult = ExecuteWorkflowAndGetResult(actualSequence, out actualException);
            ////Log.TraceInternal("Actual result: {0}\n", actualResult);

            //ExpressionTestRuntime.ValidateException(expectedException, actualException);

            //if (expectedResult != actualResult)
            //{
            //    throw new Exception("Inconsistent execution result!");
            //}
        }
        public static void ValidateReferenceExpressionXaml <T>(TestExpression te)
        {
            Expression <Func <ActivityContext, T> > lambdaExpression = (Expression <Func <ActivityContext, T> >)te.CreateLambdaExpresson <T>();
            //Log.TraceInternal("Expression: {0}", lambdaExpression.ToString());

            Activity expectedActivity = te.CreateExpectedActivity() as Activity;
            Activity actualActivity   = ExpressionServices.ConvertReference(lambdaExpression);

            ValidateActivity(expectedActivity, actualActivity);
        }
        private static Activity <TResult> HandleUnaryExpression <TOperand, TResult>(UnaryOperator op, TestExpression operand)
        {
            Activity <TResult> we = null;

            switch (op)
            {
            case UnaryOperator.Cast:
                we = new Cast <TOperand, TResult>()
                {
                    Checked = false,
                    Operand = TestExpression.GetInArgumentFromExpectedNode <TOperand>(operand),
                };
                break;

            case UnaryOperator.CheckedCast:
                we = new Cast <TOperand, TResult>()
                {
                    Checked = true,
                    Operand = TestExpression.GetInArgumentFromExpectedNode <TOperand>(operand),
                };
                break;

            case UnaryOperator.Not:
                we = new Not <TOperand, TResult>()
                {
                    Operand = TestExpression.GetInArgumentFromExpectedNode <TOperand>(operand),
                };
                break;

            case UnaryOperator.TypeAs:
                we = new As <TOperand, TResult>()
                {
                    Operand = TestExpression.GetInArgumentFromExpectedNode <TOperand>(operand),
                };
                break;

            default:
                throw new NotSupportedException(string.Format("Operator: {0} is unsupported", op.ToString()));
            }

            return(we);
        }
        private static readonly string s_path = string.Empty; //DirectoryAssistance.GetTestBinsDirectory("TempFile.txt");

        public static void ValidateExpressionXaml <T>(TestExpression te)
        {
            Activity expectedActivity = null, actualActivity = null;

            Expression <Func <ActivityContext, T> > lambdaExpression = null;

            lambdaExpression = (Expression <Func <ActivityContext, T> >)te.CreateLambdaExpresson <T>();
            //Log.TraceInternal("Expression: {0}", lambdaExpression.ToString());

            expectedActivity = te.CreateExpectedActivity() as Activity;

            if (te.ExpectedConversionException != null)
            {
                ExceptionHelpers.CheckForException(
                    te.ExpectedConversionException.GetType(), te.ExpectedConversionException.Message,
                    () => { actualActivity = ExpressionServices.Convert(lambdaExpression); }, true);
            }
            else
            {
                actualActivity = ExpressionServices.Convert(lambdaExpression);
                ValidateActivity(expectedActivity, actualActivity);
            }
        }
        static protected Activity <T> GetWorkflowElementFromExpectedNode <T>(TestExpression te)
        {
            Activity <T> we = null;

            if (te.ExpectedConversionException != null)
            {
                return(null);
            }

            // leaf node:
            if (te.GetType() == typeof(TestExpression))
            {
                if (te.ExpectedNode is Activity <T> )
                {
                    we = (Activity <T>)te.ExpectedNode;
                }
                else if (te.ExpectedNode is Variable <T> )
                {
                    we = new VariableValue <T>()
                    {
                        Variable = (Variable <T>)te.ExpectedNode
                    };
                }
                else
                {
                    throw new NotSupportedException("Not supported expected node type");
                }
            }
            // non-leaf node:
            else if (te is TestBinaryExpression || te is TestUnaryExpression)
            {
                we = (Activity <T>)te.CreateExpectedActivity();
            }

            return(we);
        }
 public static void ValidateExecutionResult(TestExpression te, List <Variable> variables, params Type[] refTypes)
 {
     ValidateExecutionResult(te, variables, null, refTypes);
 }
Beispiel #8
0
        private static Activity HandleBinaryExpression <TLeft, TRight, TResult>(BinaryOperator op, TestExpression left, TestExpression right)
        {
            Activity           we           = null;
            InArgument <TLeft> leftArgument = (InArgument <TLeft>)TestExpression.GetInArgumentFromExpectedNode <TLeft>(left);

            leftArgument.EvaluationOrder = 0;
            InArgument <TRight> rightArgument = (InArgument <TRight>)TestExpression.GetInArgumentFromExpectedNode <TRight>(right);

            rightArgument.EvaluationOrder = 1;

            switch (op)
            {
            case BinaryOperator.Add:
                we = new Add <TLeft, TRight, TResult>()
                {
                    Checked = false,
                    Left    = leftArgument,
                    Right   = rightArgument
                };
                break;

            case BinaryOperator.And:
                we = new And <TLeft, TRight, TResult>()
                {
                    Left  = leftArgument,
                    Right = rightArgument
                };
                break;

            case BinaryOperator.AndAlso:
                we = new AndAlso()
                {
                    Left  = TestExpression.GetWorkflowElementFromExpectedNode <bool>(left),
                    Right = TestExpression.GetWorkflowElementFromExpectedNode <bool>(right)
                };
                break;

            case BinaryOperator.CheckedAdd:
                we = new Add <TLeft, TRight, TResult>()
                {
                    Checked = true,
                    Left    = leftArgument,
                    Right   = rightArgument
                };
                break;

            case BinaryOperator.CheckedMultiply:
                we = new Multiply <TLeft, TRight, TResult>()
                {
                    Checked = true,
                    Left    = leftArgument,
                    Right   = rightArgument
                };
                break;

            case BinaryOperator.CheckedSubtract:
                we = new Subtract <TLeft, TRight, TResult>()
                {
                    Checked = true,
                    Left    = leftArgument,
                    Right   = rightArgument
                };
                break;

            case BinaryOperator.Divide:
                we = new Divide <TLeft, TRight, TResult>()
                {
                    Left  = leftArgument,
                    Right = rightArgument
                };
                break;

            case BinaryOperator.Equal:
                we = new Equal <TLeft, TRight, TResult>()
                {
                    Left  = leftArgument,
                    Right = rightArgument
                };
                break;

            case BinaryOperator.GreaterThan:
                we = new GreaterThan <TLeft, TRight, TResult>()
                {
                    Left  = leftArgument,
                    Right = rightArgument
                };
                break;

            case BinaryOperator.GreaterThanOrEqual:
                we = new GreaterThanOrEqual <TLeft, TRight, TResult>()
                {
                    Left  = leftArgument,
                    Right = rightArgument
                };
                break;

            case BinaryOperator.LessThan:
                we = new LessThan <TLeft, TRight, TResult>()
                {
                    Left  = leftArgument,
                    Right = rightArgument
                };
                break;

            case BinaryOperator.LessThanOrEqual:
                we = new LessThanOrEqual <TLeft, TRight, TResult>()
                {
                    Left  = leftArgument,
                    Right = rightArgument
                };
                break;

            case BinaryOperator.Or:
                we = new Or <TLeft, TRight, TResult>()
                {
                    Left  = leftArgument,
                    Right = rightArgument
                };
                break;

            case BinaryOperator.Multiply:
                we = new Multiply <TLeft, TRight, TResult>()
                {
                    Checked = false,
                    Left    = leftArgument,
                    Right   = rightArgument
                };
                break;

            case BinaryOperator.NotEqual:
                we = new NotEqual <TLeft, TRight, TResult>()
                {
                    Left  = leftArgument,
                    Right = rightArgument
                };
                break;

            case BinaryOperator.OrElse:
                we = new OrElse()
                {
                    Left  = TestExpression.GetWorkflowElementFromExpectedNode <bool>(left),
                    Right = TestExpression.GetWorkflowElementFromExpectedNode <bool>(right)
                };
                break;

            case BinaryOperator.Subtract:
                we = new Subtract <TLeft, TRight, TResult>()
                {
                    Checked = false,
                    Left    = leftArgument,
                    Right   = rightArgument
                };
                break;

            default:
                throw new NotSupportedException(string.Format("Operator: {0} is unsupported", op.ToString()));
            }

            return(we);
        }