// Test Cast TypeAs unary expression public void CastTypeAsTest() { _operators = new List <UnaryOperator>() { UnaryOperator.Cast, UnaryOperator.CheckedCast, UnaryOperator.TypeAs }; _leafNode = new List <NodeExpressionPair>() { ExpressionLeafs.ConstStringValue, ExpressionLeafs.StaticStringField, ExpressionLeafs.StaticStringProperty, }; Variable <String> stringVar = LeafHelper.GetVariable <string>(); stringVar.Default = "String Variable"; List <Variable> variables = new List <Variable>() { stringVar }; int numberOfTests = 0; foreach (TestUnaryExpression expr in EnumerateTest <String>(0, 2)) { numberOfTests++; //Log.Info(numberOfTests.ToString()); ExpressionTestRuntime.ValidateExpressionXaml <string>(expr); ExpressionTestRuntime.ValidateExecutionResult(expr, variables); } }
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); } } }
// Test value type unary expression public void ValueTypeUnaryExpressionTest() { _operators = new List <UnaryOperator>() { UnaryOperator.Not, UnaryOperator.Cast, UnaryOperator.CheckedCast }; _leafNode = new List <NodeExpressionPair>() { ExpressionLeafs.ConstIntValue, ExpressionLeafs.StaticIntField, ExpressionLeafs.StaticIntProperty, }; List <Variable> variables = new List <Variable>() { LeafHelper.GetVariable <int>() }; int numberOfTests = 0; foreach (TestUnaryExpression expr in EnumerateTest <int>(0, 2)) { numberOfTests++; //Log.Info(numberOfTests.ToString()); ExpressionTestRuntime.ValidateExpressionXaml <int>(expr); ExpressionTestRuntime.ValidateExecutionResult(expr, variables); } }
// Logical operator test public void LogicalOperatorTest() { _operators = new List <BinaryOperator>() { BinaryOperator.And, BinaryOperator.OrElse, BinaryOperator.Or, BinaryOperator.AndAlso }; _leafNode = new List <NodeExpressionPair>() { ExpressionLeafs.ConstBooleanTrue, ExpressionLeafs.ConstBooleanFalse, ExpressionLeafs.StaticBooleanField, ExpressionLeafs.StaticBooleanProperty, }; List <Variable> variables = new List <Variable>() { LeafHelper.GetVariable <Boolean>() }; int numberOfTests = 0; foreach (TestBinaryExpression binExpr in EnumerateTest <Boolean>(0, 2)) { numberOfTests++; //Log.Info(numberOfTests.ToString()); ExpressionTestRuntime.ValidateExpressionXaml <Boolean>(binExpr); ExpressionTestRuntime.ValidateExecutionResult(binExpr, variables); } }
// Bitwise operator test public void BitwiseOperatorTest() { _operators = new List <BinaryOperator>() { BinaryOperator.And, BinaryOperator.Or }; _leafNode = new List <NodeExpressionPair>() { ExpressionLeafs.ConstIntValue, ExpressionLeafs.StaticIntField, ExpressionLeafs.StaticIntProperty, ExpressionLeafs.UnsupportedBinaryOperator }; List <Variable> variables = new List <Variable>() { LeafHelper.GetVariable <int>() }; int numberOfTests = 0; foreach (TestBinaryExpression binExpr in EnumerateTest <int>(0, 2)) { numberOfTests++; //Log.Info(numberOfTests.ToString()); ExpressionTestRuntime.ValidateExpressionXaml <int>(binExpr); if (binExpr.ExpectedConversionException == null) { ExpressionTestRuntime.ValidateExecutionResult(binExpr, variables); } } }
// Comments copied from ExpressionServices.cs: // This is to handle the leaf nodes as a variable // // Linq actually generate a temp class wrapping all the local variables. // // The real expression object look like // new TempClass() { A = a }.A.Get(env) // // A is a field // // This is why the logic of the code below follows. // This is pretty dependent on Linq implementation. // The test is to simulate the scenario by creating the Linq expression as: // (env) => TempClass().A.Get(env) public static Expression GetMemberAccessVariableExpression <T>() { MethodInfo getMethodInfo = typeof(Variable <T>).GetMethod("Get", new Type[] { typeof(ActivityContext) }); DummyHelper dummy = new DummyHelper(); // DummyHelper.<T>Var = LeafHelper.GetVariable<T>(); FieldInfo fieldInfo; fieldInfo = typeof(DummyHelper).GetField(typeof(T).Name + "Var"); fieldInfo.SetValue(dummy, LeafHelper.GetVariable <T>()); return(Expression.Call( Expression.Field(Expression.Constant(dummy), typeof(DummyHelper).GetField(typeof(T).Name + "Var")), getMethodInfo, TestExpression.EnvParameter)); }
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); } } }
// Comparison operator test public void ComparisonOperatorTest() { _operators = new List <BinaryOperator>() { BinaryOperator.Add, BinaryOperator.CheckedAdd, BinaryOperator.Subtract, BinaryOperator.CheckedSubtract, BinaryOperator.Multiply, BinaryOperator.CheckedMultiply, BinaryOperator.Divide, BinaryOperator.And, BinaryOperator.Or }; List <BinaryOperator> comparisonOperators = new List <BinaryOperator>() { BinaryOperator.Equal, BinaryOperator.GreaterThan, BinaryOperator.GreaterThanOrEqual, BinaryOperator.LessThan, BinaryOperator.LessThanOrEqual }; _leafNode = new List <NodeExpressionPair>() { ExpressionLeafs.ConstIntValue, ExpressionLeafs.StaticIntField, ExpressionLeafs.StaticIntProperty, ExpressionLeafs.VariableInWrapper }; List <Variable> variables = new List <Variable>() { LeafHelper.GetVariable <int>() }; int numberOfTests = 0; foreach (BinaryOperator op in comparisonOperators) { TestBinaryExpression binExpr = new TestBinaryExpression() { Operator = op, ResultType = typeof(Boolean), }; foreach (TestExpression left in EnumerateTest <int>(0, 1)) { numberOfTests++; //Log.Info(numberOfTests.ToString()); binExpr.Left = left; foreach (TestExpression right in EnumerateTest <int>(0, 1)) { binExpr.Right = right; } ExpressionTestRuntime.ValidateExpressionXaml <bool>(binExpr); if (binExpr.ExpectedConversionException == null) { ExpressionTestRuntime.ValidateExecutionResult(binExpr, variables); } } } }