示例#1
0
        public void ExecutesAggregator()
        {
            var testee = new AggregatorStrategy <TestQuestion, string, int>();

            var context        = new Context();
            var aggregatorMock = new Mock <IAggregator <string, string, int> >();

            var expressionProviderMock = new Mock <IExpressionProvider <TestQuestion, string, int, string> >();
            var question   = new TestQuestion();
            var expression = new TestExpression <string>();

            expressionProviderMock.Setup(provider => provider.GetExpressions(question)).Returns(new[] { expression });

            var definition = new TestableDefinition <string>
            {
                Aggregator          = aggregatorMock.Object,
                ExpressionProviders = new[] { expressionProviderMock.Object }
            };

            const int Parameter = 7;

            testee.Execute(question, Parameter, definition, context);

            AssertThatAggregatorIsCalledWithExpressionsFromDefinition(aggregatorMock, new[] { expression }, Parameter);
        }
示例#2
0
        //  Add two strings
        public void AddTwoStrings()
        {
            Expression <Func <ActivityContext, string> > expression =
                (env) => DummyHelper.StaticStringField1 + DummyHelper.StaticStringField2 + DummyHelper.StaticStringField3;

            Activity <string> expectedActivity = new InvokeMethod <string>()
            {
                MethodName = "Concat",
                TargetType = typeof(string),
                Parameters =
                {
                    new InArgument <string>()
                    {
                        EvaluationOrder = 0,
                        Expression      = new InvokeMethod <string>()
                        {
                            MethodName = "Concat",
                            TargetType = typeof(string),
                            Parameters =
                            {
                                new InArgument <string>()
                                {
                                    EvaluationOrder = 0,
                                    Expression      = new FieldValue <DummyHelper, string>()
                                    {
                                        FieldName = "StaticStringField1"
                                    },
                                },
                                new InArgument <string>()
                                {
                                    EvaluationOrder = 1,
                                    Expression      = new FieldValue <DummyHelper, string>()
                                    {
                                        FieldName = "StaticStringField2"
                                    },
                                }
                            }
                        },
                    },
                    new InArgument <string>()
                    {
                        EvaluationOrder = 1,
                        Expression      = new FieldValue <DummyHelper, string>()
                        {
                            FieldName = "StaticStringField3"
                        },
                    }
                }
            };

            TestExpression expr = new TestExpression()
            {
                ResultType     = typeof(string),
                ExpectedNode   = expectedActivity,
                ExpressionTree = expression
            };

            ExpressionTestRuntime.ValidateExpressionXaml <string>(expr);
            ExpressionTestRuntime.ValidateExecutionResult(expr, null);
        }
        public void Const_MayReturnEqualToExpr()
        {
            var expected = TestExpression.Expr(() => 5);
            var actual   = TestExpression.Const(5);

            AssertAreEqualExpressions(expected, actual);
        }
        public void Var_WithEqualArguments_ReturnsEqualTestExpressions()
        {
            var expr1 = TestExpression.Var <int>("n");
            var expr2 = TestExpression.Var <int>("n");

            AssertAreEqualExpressions(expr1, expr2);
        }
        public void Const_WithEqualArguments_ReturnsEqualTestExpressions()
        {
            var expr1 = TestExpression.Const(5);
            var expr2 = TestExpression.Const(5);

            AssertAreEqualExpressions(expr1, expr2);
        }
示例#6
0
        //  Method call with ref argument
        public void MethodCallWithRefArgument()
        {
            Expression <Func <ActivityContext, int> > expression = (env) => DummyHelper.MethodCallWithRefArgument(ref DummyHelper.StaticIntField);

            string         methodName = "MethodCallWithRefArgument";
            TestExpression expr       = new TestExpression()
            {
                ResultType   = typeof(int),
                ExpectedNode = new InvokeMethod <int>()
                {
                    MethodName = methodName,
                    TargetType = typeof(DummyHelper),
                    Parameters =
                    {
                        new InOutArgument <int>()
                        {
                            Expression = new FieldReference <DummyHelper, int>
                            {
                                FieldName = "StaticIntField"
                            },
                            EvaluationOrder = 1
                        }
                    }
                },
                ExpressionTree = expression
            };

            ExpressionTestRuntime.ValidateExpressionXaml <int>(expr);
            ExpressionTestRuntime.ValidateExecutionResult(expr, null);
        }
示例#7
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);
        }
示例#8
0
 protected override void GenerateInner(CodeGenerator generator, CodeStatementEmitOptions emitOptions)
 {
     generator.WriteBlankLineBeforeEnteringBlock();
     generator.EnterLocalScope();
     generator.Write(TokenType.Keyword, "for");
     generator.Write(TokenType.Space, ' ');
     generator.Write(TokenType.Punctuation, '(');
     InitStatement.Generate(generator, CodeStatementEmitOptions.OmitSemiColon);
     generator.Write(TokenType.Punctuation, ';');
     generator.Write(TokenType.Space, ' ');
     TestExpression.Generate(generator);
     generator.Write(TokenType.Punctuation, ';');
     generator.Write(TokenType.Space, ' ');
     IncrementExpression.Generate(generator);
     generator.Write(TokenType.Punctuation, ')');
     if (Statements.Count > 0)
     {
         generator.WriteOpeningBrace();
         generator.Indent++;
         generator.EnterLocalScope();
         Statements.ReserveLocals(generator, default(CodeStatementEmitOptions));
         Statements.Generate(generator, default(CodeStatementEmitOptions));
         generator.ExitLocalScope();
         generator.Indent--;
         generator.WriteClosingBrace();
     }
     else
     {
         generator.WriteEmptyBlock();
     }
     generator.ExitLocalScope();
 }
        public void Expr_WithSameParameter_AreEqual()
        {
            var expr1 = TestExpression.Expr(() => 5);
            var expr2 = TestExpression.Expr(() => 5);

            AssertAreEqualExpressions(expr1, expr2);
        }
示例#10
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);
                }
            }
        }
示例#11
0
        //  None generic variable get
        public void NoneGenericVariableGet()
        {
            Variable <int> var = new Variable <int>()
            {
                Name = "NoneGenericVariable"
            };

            Activity <int> expectedActivity = new Cast <object, int>()
            {
                Operand = var,
                Checked = false
            };

            Expression <Func <ActivityContext, int> > lambda = (env) => (int)DummyHelper.NoneGenericVariable.Get(env);

            TestExpression expr = new TestExpression()
            {
                ResultType     = typeof(int),
                ExpectedNode   = expectedActivity,
                ExpressionTree = lambda
            };

            List <Variable> varsActual = new List <Variable>()
            {
                DummyHelper.NoneGenericVariable
            };

            List <Variable> varsExpected = new List <Variable>()
            {
                var
            };

            ExpressionTestRuntime.ValidateExpressionXaml <int>(expr);
            ExpressionTestRuntime.ValidateExecutionResult(expr, varsExpected, varsActual);
        }
示例#12
0
        public void NewWithArgument()
        {
            TestExpression expr = new TestExpression()
            {
                ResultType   = typeof(String),
                ExpectedNode = new New <String>()
                {
                    Arguments =
                    {
                        new InArgument <char>('a')
                        {
                            EvaluationOrder = 0
                        },
                        new InArgument <int>(1)
                        {
                            EvaluationOrder = 1,
                        },
                    }
                },
                ExpressionTree = Expression.New(
                    typeof(String).GetConstructor(new Type[] { typeof(char), typeof(int) }),
                    Expression.Constant('a'),
                    Expression.Constant(1))
            };

            ExpressionTestRuntime.ValidateExpressionXaml <String>(expr);
            ExpressionTestRuntime.ValidateExecutionResult(expr, null);
        }
示例#13
0
        public void TestRebalance()
        {
            var one   = new TestExpression(1, 1);
            var two   = new TestExpression(2, 3);
            var three = new TestExpression(3, 5);

            var left  = one;
            var right = new LeftRightExpression(two, three);
            var node  = new LeftRightExpression(left, right);

            var newNode = node.DoRebalance() as LeftRightExpression;

            Assert.That(newNode, Is.SameAs(right));
            Assert.That(newNode.Line, Is.EqualTo(one.Line));
            Assert.That(newNode.Column, Is.EqualTo(one.Column));
            Assert.That(newNode.EndLine, Is.EqualTo(three.EndLine));
            Assert.That(newNode.EndColumn, Is.EqualTo(three.EndColumn));

            Assert.That(newNode.Right, Is.SameAs(three));

            var newLeft = newNode.Left as LeftRightExpression;

            Assert.That(newLeft, Is.Not.Null);
            Assert.That(newLeft.Left, Is.SameAs(one));
            Assert.That(newLeft.Right, Is.SameAs(two));

            Assert.That(newLeft.Line, Is.EqualTo(one.Line));
            Assert.That(newLeft.Column, Is.EqualTo(one.Column));
            Assert.That(newLeft.EndLine, Is.EqualTo(two.EndLine));
            Assert.That(newLeft.EndColumn, Is.EqualTo(two.EndColumn));
        }
示例#14
0
        //  Indexer
        public void RValueIndexer()
        {
            Expression <Func <ActivityContext, string> > lambda = (env) => DummyHelper.StaticDictionary[1];

            Activity <string> expectedActivity = new InvokeMethod <string>()
            {
                MethodName = "get_Item",
                Parameters =
                {
                    new InArgument <int>(1)
                    {
                        EvaluationOrder = 1
                    }
                },
                TargetObject = new InArgument <Dictionary <int, string> >()
                {
                    EvaluationOrder = 0,
                    Expression      = new FieldValue <DummyHelper, Dictionary <int, string> >()
                    {
                        FieldName = "StaticDictionary"
                    }
                }
            };

            TestExpression expr = new TestExpression()
            {
                ResultType     = typeof(string),
                ExpectedNode   = expectedActivity,
                ExpressionTree = lambda
            };

            ExpressionTestRuntime.ValidateExpressionXaml <string>(expr);
            ExpressionTestRuntime.ValidateExecutionResult(expr, null);
        }
示例#15
0
        public void ExecutesAggregator_WhenStrategyWithMappingIsUsed()
        {
            const string Answer = "42";

            var testee = new AggregatorStrategy <TestQuestion, string, int, int>();

            var context    = new Context();
            var aggregator = A.Fake <IAggregator <int, string, int> >();

            var expressionProvider = A.Fake <IExpressionProvider <TestQuestion, string, int, int> >();
            var question           = new TestQuestion();
            var expression         = new TestExpression <int>();

            A.CallTo(() => expressionProvider.GetExpressions(question)).Returns(new[] { expression });
            var definition = new TestableDefinition <int>
            {
                Aggregator          = aggregator,
                ExpressionProviders = new[] { expressionProvider }
            };

            const int Parameter = 7;

            A.CallTo(() => aggregator.Aggregate(
                         A <IEnumerable <IExpression <int, int> > > .That.Matches(_ => _.Contains(expression) && _.Count() == 1),
                         Parameter,
                         A <Context> ._)).Returns(Answer);

            string result = testee.Execute(question, Parameter, definition, context);

            result.Should().Be(Answer);
        }
示例#16
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);
        }
 public void test_custom_expression_reduction()
 {
     TestExpression te = new TestExpression();
     var lambda = Expression.Lambda<Func<int>>(te, true);
     var f = lambda.Compile();
     Assert.AreEqual(24601, f());
 }
示例#18
0
        //  Not operator with overloading
        public void NotOperatorWithOverloading()
        {
            Expression <Func <ActivityContext, DummyHelper> > expression = (env) => !DummyHelper.Instance;
            Activity <DummyHelper> expectedActivity = new InvokeMethod <DummyHelper>()
            {
                MethodName = "op_LogicalNot",
                TargetType = typeof(DummyHelper),
                Parameters =
                {
                    new InArgument <DummyHelper>(
                        new FieldValue <DummyHelper, DummyHelper>()
                    {
                        FieldName = "Instance"
                    })
                }
            };

            TestExpression expr = new TestExpression()
            {
                ResultType     = typeof(DummyHelper),
                ExpectedNode   = expectedActivity,
                ExpressionTree = expression
            };

            ExpressionTestRuntime.ValidateExpressionXaml <DummyHelper>(expr);
            ExpressionTestRuntime.ValidateExecutionResult(expr, null, typeof(DummyHelper));
        }
        public void Lambda1_CorrectlyCreatesLambdaExpression()
        {
            var constant = Expression.Constant(5);
            var body     = Expression.Lambda <Action>(constant);
            var expected = new TestExpression <Action>(body);

            var actual = TestExpression.Lambda((TestExpression)TestExpression.Const(5));

            AssertAreEqualExpressions(expected, actual);
        }
示例#20
0
        public void TestColumnsNoLeft()
        {
            var right = new TestExpression(2, 3);
            var node  = new LeftRightExpression(null, right);

            Assert.That(node.Line, Is.EqualTo(right.Line));
            Assert.That(node.Column, Is.EqualTo(right.Column));
            Assert.That(node.EndLine, Is.EqualTo(right.EndLine));
            Assert.That(node.EndColumn, Is.EqualTo(right.EndColumn));
        }
        public void Expr_Inserts1VariableIntoTestExpression()
        {
            var expression = Expression.Lambda <Action>(Expression.Parameter(typeof(int), "a"));
            var expected   = TestExpression.Expr(expression);

            var variable = TestExpression.Var <int>("a");
            var actual   = TestExpression.Expr(variable, a => a);

            AssertAreEqualExpressions(expected, actual);
        }
示例#22
0
        public void CompareExpressions()
        {
            var expression = new TestExpression(type);

            Assert.That(expression, Is.EqualTo(new TestExpression(type)));
            Assert.That(expression.GetHashCode(),
                        Is.EqualTo(new TestExpression(type).GetHashCode()));
            Assert.That(new TestExpression(type.Methods[0].ReturnType), Is.Not.EqualTo(new TestExpression(type)));
            Assert.That(expression.Equals((object)new TestExpression(type)), Is.True);
        }
        public void TestColumns()
        {
            var left  = new TestExpression(1, 1);
            var right = new TestExpression(2, 3);
            var node  = new LeftRightExpression(left, right);

            Assert.That(node.Location.Start.Line, Is.EqualTo(left.Location.Start.Line));
            Assert.That(node.Location.Start.Column, Is.EqualTo(left.Location.Start.Column));
            Assert.That(node.Location.End.Line, Is.EqualTo(right.Location.End.Line));
            Assert.That(node.Location.End.Column, Is.EqualTo(right.Location.End.Column));
        }
示例#24
0
        public void NewWithoutArgument()
        {
            TestExpression expr = new TestExpression()
            {
                ResultType     = typeof(int),
                ExpectedNode   = new New <int>(),
                ExpressionTree = Expression.New(typeof(int))
            };

            ExpressionTestRuntime.ValidateExpressionXaml <int>(expr);
            ExpressionTestRuntime.ValidateExecutionResult(expr, null);
        }
        internal override void AppendTo(SqlStringBuilder builder)
        {
            builder.Append("CASE WHEN ");
            TestExpression.AppendTo(builder);
            builder.Append(" THEN ");
            TrueExpression.AppendTo(builder);

            if (FalseExpression != null)
            {
                builder.Append(" ELSE ");
                FalseExpression.AppendTo(builder);
            }
        }
示例#26
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);
        }
        public void Lambda4_CorrectlyCreatesLambdaExpression()
        {
            var constant  = Expression.Parameter(typeof(int), "variable");
            var parameter = Expression.Parameter(typeof(int), "parameter");
            var addition  = Expression.Add(parameter, constant);
            var body      = Expression.Lambda <Func <int, int> >(addition, parameter);
            var expected  = new TestExpression <Func <int, int> >(body);

            var variable = TestExpression.Var <int>("variable");
            var actual   = TestExpression.Lambda <int, int>(parameter => TestExpression.Expr(variable, b => parameter + b));

            AssertAreEqualExpressions(expected, actual);
        }
示例#28
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);
        }
示例#29
0
        //  Add with generic type as operands
        public void AddGenericTypes()
        {
            Add <Nullable <int>, Nullable <int>, Nullable <int> > expectedActivity = new Add <Nullable <int>, Nullable <int>, Nullable <int> >()
            {
                Left = new InArgument <int?>()
                {
                    EvaluationOrder = 0,
                    Expression      = new New <Nullable <int> >()
                    {
                        Arguments =
                        {
                            new InArgument <int>(10)
                            {
                                EvaluationOrder = 0
                            }
                        }
                    },
                },
                Right = new InArgument <int?>()
                {
                    EvaluationOrder = 1,
                    Expression      = new New <Nullable <int> >()
                    {
                        Arguments =
                        {
                            new InArgument <int>(20)
                            {
                                EvaluationOrder = 0
                            }
                        }
                    }
                },
                Checked = false
            };

            ConstructorInfo ctorHandler = typeof(Nullable <int>).GetConstructors()[0];

            Expression expectedExpression = Expression.Add(
                Expression.New(ctorHandler, Expression.Constant(10)),
                Expression.New(ctorHandler, Expression.Constant(20)));

            TestExpression expr = new TestExpression()
            {
                ResultType     = typeof(Nullable <int>),
                ExpectedNode   = expectedActivity,
                ExpressionTree = expectedExpression
            };

            ExpressionTestRuntime.ValidateExpressionXaml <Nullable <int> >(expr);
            ExpressionTestRuntime.ValidateExecutionResult(expr, null);
        }
        public void Expr_Inserts2VariablesIntoTestExpression()
        {
            var addition = Expression.Add(
                Expression.Parameter(typeof(int), "a"),
                Expression.Parameter(typeof(int), "b"));
            var expression = Expression.Lambda <Action>(addition);
            var expected   = TestExpression.Expr(expression);

            var variable1 = TestExpression.Var <int>("a");
            var variable2 = TestExpression.Var <int>("b");
            var actual    = TestExpression.Expr(variable1, variable2, (a, b) => a + b);

            AssertAreEqualExpressions(expected, actual);
        }
示例#31
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);
        }
示例#32
0
        public void SetCurrentValueOnStyleExpression()
        {
            TestObject element = new TestObject();
            Assert.AreEqual("defaultValue1", element.GetValue(TestObject.Value1Property));

            TestExpression expression = new TestExpression("value1", isReadOnly: false);
            Assert.AreEqual("value1", expression.Value);

            element.SetValue(TestObject.Value1Property, expression, BaseValueSource.Style);
            Assert.AreEqual("value1", element.GetValue(TestObject.Value1Property));
            AssertValueSource(element.GetValueSource(TestObject.Value1Property), isCurrent: false, isExpression: true, baseValueSource: BaseValueSource.Style);

            element.SetCurrentValue(TestObject.Value1Property, "value2");
            Assert.AreEqual("value2", element.GetValue(TestObject.Value1Property));
            Assert.AreEqual("value2", expression.Value);
            AssertValueSource(element.GetValueSource(TestObject.Value1Property), isCurrent: false, isExpression: true, baseValueSource: BaseValueSource.Style);

            element.ClearValue(TestObject.Value1Property, BaseValueSource.Local);
            Assert.AreEqual("value2", element.GetValue(TestObject.Value1Property));
            AssertValueSource(element.GetValueSource(TestObject.Value1Property), isCurrent: false, isExpression: true, baseValueSource: BaseValueSource.Style);
        }