Пример #1
0
        public void ast_loop_with_block_expression_return_block_expression_that_calcul_even_occurence_between_start_value_and_end_value()
        {
            int val1 = new Random().Next(0, 1000);
            int val2;

            do
            {
                val2 = new Random().Next(0, 5000);
            } while (val1 > val2);

            // Creating a parameter expression.
            ParameterExpression startValue = Expression.Parameter(typeof(int), "startValue");
            ParameterExpression endValue   = Expression.Parameter(typeof(int), "endValue");

            // Compile and run an expression tree.
            BlockExpression block = (BlockExpression)NoMonkeyTree.AstLoopWithBlock(val1, startValue, endValue);

            ExpressionType.Loop.Should().Be(block.Result.NodeType);

            int result = Expression.Lambda <Func <int, int, int> >(block, startValue, endValue).Compile()(val1, val2);

            int evenOccurence = 0;

            for (int i = ++val1; i <= val2; i++)
            {
                if ((i & 1) != 1)
                {
                    evenOccurence++;
                }
            }

            evenOccurence.Should().Be(result);
        }
Пример #2
0
        public void ast_play_with_string_should_return_expression_concat_of_two_strings()
        {
            BinaryExpression stringExpr = NoMonkeyTree.AstStringOperator();

            ((ConstantExpression)stringExpr.Left).Value.Should().Be("toto");
            ((ConstantExpression)stringExpr.Right).Value.Should().Be("tata");

            Expression.Lambda <Func <string> >(stringExpr).Compile()().Should().Be("tototata");
        }
Пример #3
0
        public void ast_return_expression_that_call_our_own_method_substract()
        {
            Random     rand = new Random();
            int        c1   = rand.Next(Int32.MinValue, Int32.MaxValue);
            int        c2   = rand.Next(Int32.MinValue, Int32.MaxValue);
            Expression expr = NoMonkeyTree.AstCallCustomFunctionSubstract(c1, c2);
            var        func = Expression.Lambda <Func <int> >(expr).Compile();

            func.Invoke().Should().Be(c1 - c2);
        }
Пример #4
0
        public void ast_string_and_datetime_loop_should_return_toto_and_Me_for_even_date_else_You()
        {
            for (int i = 0; i < 500; i++)
            {
                var date         = DateTime.UtcNow.Millisecond;
                var expression   = NoMonkeyTree.AstStringAndDateTime(date);
                var shouldResult = ("toto" + ((date & 1) == 1 ? "You" : "Me"));

                expression.Left.NodeType.Should().Be(ExpressionType.Constant);
                expression.Right.NodeType.Should().Be(ExpressionType.Constant);

                var result = Expression.Lambda <Func <string> >(expression).Compile()();
                result.Should().Be(shouldResult);
            }
        }
Пример #5
0
        public void ast_create_simple_expression_with_basic_operators_and_return_linqexpression_that_do_the_calcul()

        {
            BinaryExpression ast = NoMonkeyTree.AstSimpleOperator();

            ast.NodeType.Should().Be(ExpressionType.Divide);


            BinaryExpression   astMultiply = (BinaryExpression)ast.Left;
            ConstantExpression const1      = (ConstantExpression)ast.Right;

            astMultiply.NodeType.Should().Be(ExpressionType.Multiply);
            const1.Value.Should().Be(4);


            BinaryExpression   astAddition = (BinaryExpression)astMultiply.Left;
            ConstantExpression const2      = (ConstantExpression)astMultiply.Right;

            astAddition.NodeType.Should().Be(ExpressionType.Add);
            const2.Value.Should().Be(3);


            ConstantExpression const3 = (ConstantExpression)astAddition.Left;
            ConstantExpression const4 = (ConstantExpression)astAddition.Right;

            const3.Value.Should().Be(3);
            const4.Value.Should().Be(5);


            ast.ToString().Should().Be("(((3 + 5) * 3) / 4)");


            var result = Expression.Lambda <Func <int> >(ast).Compile()();

            result.Should().Be(6);
        }
Пример #6
0
        public void ast_usage_of_func_should_return_result_multiplication_of_two_numbers()
        {
            Expression <Func <int, int, int> > expr = (Expression <Func <int, int, int> >)NoMonkeyTree.AstFuncMultilplication();

            expr.NodeType.Should().Be(ExpressionType.Lambda);

            expr.Parameters[0].NodeType.Should().Be(ExpressionType.Parameter);
            expr.Parameters[1].NodeType.Should().Be(ExpressionType.Parameter);

            expr.Compile()(3, 5).Should().Be(15);
            expr.Compile()(7, 5).Should().Be(35);
            expr.Compile()(9, 9).Should().Be(81);
            expr.Compile()(10, 5).Should().Be(50);
        }