Example #1
0
        public void UpdateTest()
        {
            SampleClassWithProperties instance = new SampleClassWithProperties
            {
                DefaultProperty = new List <int> {
                    100, 101
                },
                AlternativeProperty = new List <int> {
                    200, 201
                }
            };

            IndexExpression  expr        = instance.DefaultIndexExpression;
            MemberExpression newProperty = Expression.Property(Expression.Constant(instance),
                                                               typeof(SampleClassWithProperties).GetProperty(nameof(instance.AlternativeProperty)));

            ConstantExpression[] newArguments = { Expression.Constant(1) };

            IndexExpression exprUpdated = expr.Update(newProperty, newArguments);

            // Replace Object and Arguments of IndexExpression.
            IndexExpressionHelpers.AssertEqual(
                exprUpdated,
                Expression.MakeIndex(newProperty, instance.DefaultIndexer, newArguments));

            // Invoke to check expression.
            IndexExpressionHelpers.AssertInvokeCorrect(100, expr);
            IndexExpressionHelpers.AssertInvokeCorrect(201, exprUpdated);
        }
        public void RewriteObjectTest()
        {
            var instance = new SampleClassWithProperties
            {
                DefaultProperty = new List <int> {
                    100, 101
                },
                AlternativeProperty = new List <int> {
                    200, 201
                }
            };

            IndexExpression  expr        = instance.DefaultIndexExpression;
            MemberExpression newProperty = Expression.Property(Expression.Constant(instance),
                                                               typeof(SampleClassWithProperties).GetProperty(nameof(instance.AlternativeProperty)));

            var             visitor  = new IndexVisitor(expr, newProperty, expr.Arguments.ToArray());
            IndexExpression actual   = (IndexExpression)visitor.Visit(expr);
            IndexExpression expected = Expression.MakeIndex(newProperty, expr.Indexer, expr.Arguments);

            // Object of ExpressionIndex replaced via Rewrite method call.
            IndexExpressionHelpers.AssertEqual(expected, actual);

            // Invoke to check expression.
            IndexExpressionHelpers.AssertInvokeCorrect(100, expr);
            IndexExpressionHelpers.AssertInvokeCorrect(200, actual);
        }
Example #3
0
        public void UpdateDifferentArgumentsTest()
        {
            SampleClassWithProperties instance = new SampleClassWithProperties {
                DefaultProperty = new List <int> {
                    100, 101
                }
            };
            IndexExpression expr = instance.DefaultIndexExpression;

            Assert.NotSame(expr, expr.Update(expr.Object, new [] { Expression.Constant(0) }));
        }
Example #4
0
        public void UpdateDoesntRepeatEnumeration()
        {
            SampleClassWithProperties instance = new SampleClassWithProperties {
                DefaultProperty = new List <int> {
                    100, 101
                }
            };
            IndexExpression expr = instance.DefaultIndexExpression;

            Assert.Same(expr, expr.Update(expr.Object, new RunOnceEnumerable <Expression>(instance.DefaultArguments)));
        }
Example #5
0
        public void UpdateSameTest()
        {
            var instance = new SampleClassWithProperties { DefaultProperty = new List<int> { 100, 101 } };
            IndexExpression expr = instance.DefaultIndexExpression;

            IndexExpression exprUpdated = expr.Update(expr.Object, expr.Arguments);

            // Has to be the same, because everything is the same.
            Assert.Same(expr, exprUpdated);

            // Invoke to check expression.
            IndexExpressionHelpers.AssertInvokeCorrect(100, expr);
            IndexExpressionHelpers.AssertInvokeCorrect(100, exprUpdated);
        }
        public void RewriteArgumentsTest()
        {
            var instance = new SampleClassWithProperties {DefaultProperty = new List<int> {100, 101}};

            IndexExpression expr = instance.DefaultIndexExpression;
            Expression[] newArguments = {Expression.Constant(1)};

            var visitor = new IndexVisitor(expr, expr.Object, newArguments);
            IndexExpression expected = Expression.MakeIndex(expr.Object, expr.Indexer, newArguments);
            var actual = (IndexExpression) visitor.Visit(expr);

            IndexExpressionHelpers.AssertEqual(expected, actual);

            // Invoke to check expression.
            IndexExpressionHelpers.AssertInvokeCorrect(100, expr);
            IndexExpressionHelpers.AssertInvokeCorrect(101, actual);
        }
Example #7
0
        public void UpdateSameTest()
        {
            SampleClassWithProperties instance = new SampleClassWithProperties {
                DefaultProperty = new List <int> {
                    100, 101
                }
            };
            IndexExpression expr = instance.DefaultIndexExpression;

            IndexExpression exprUpdated = expr.Update(expr.Object, instance.DefaultArguments);

            // Has to be the same, because everything is the same.
            Assert.Same(expr, exprUpdated);

            // Invoke to check expression.
            IndexExpressionHelpers.AssertInvokeCorrect(100, expr);
            IndexExpressionHelpers.AssertInvokeCorrect(100, exprUpdated);
        }
        public void RewriteArgumentsTest()
        {
            var instance = new SampleClassWithProperties {
                DefaultProperty = new List <int> {
                    100, 101
                }
            };

            IndexExpression expr = instance.DefaultIndexExpression;

            Expression[] newArguments = { Expression.Constant(1) };

            var             visitor  = new IndexVisitor(expr, expr.Object, newArguments);
            IndexExpression expected = Expression.MakeIndex(expr.Object, expr.Indexer, newArguments);
            var             actual   = (IndexExpression)visitor.Visit(expr);

            IndexExpressionHelpers.AssertEqual(expected, actual);

            // Invoke to check expression.
            IndexExpressionHelpers.AssertInvokeCorrect(100, expr);
            IndexExpressionHelpers.AssertInvokeCorrect(101, actual);
        }
        public void RewriteObjectTest()
        {
            var instance = new SampleClassWithProperties
            {
                DefaultProperty = new List<int> { 100, 101 },
                AlternativeProperty = new List<int> { 200, 201 }
            };

            IndexExpression expr = instance.DefaultIndexExpression;
            MemberExpression newProperty = Expression.Property(Expression.Constant(instance),
                typeof(SampleClassWithProperties).GetProperty(nameof(instance.AlternativeProperty)));

            var visitor = new IndexVisitor(expr, newProperty, expr.Arguments.ToArray());
            IndexExpression actual = (IndexExpression)visitor.Visit(expr);
            IndexExpression expected = Expression.MakeIndex(newProperty, expr.Indexer, expr.Arguments);

            // Object of ExpressionIndex replaced via Rewrite method call.
            IndexExpressionHelpers.AssertEqual(expected, actual);

            // Invoke to check expression.
            IndexExpressionHelpers.AssertInvokeCorrect(100, expr);
            IndexExpressionHelpers.AssertInvokeCorrect(200, actual);
        }
Example #10
0
        public void UpdateTest()
        {
            var instance = new SampleClassWithProperties
            {
                DefaultProperty = new List<int> { 100, 101 },
                AlternativeProperty = new List<int> { 200, 201 }
            };

            IndexExpression expr = instance.DefaultIndexExpression;
            MemberExpression newProperty = Expression.Property(Expression.Constant(instance),
                typeof(SampleClassWithProperties).GetProperty(nameof(instance.AlternativeProperty)));
            ConstantExpression[] newArguments = {Expression.Constant(1)};

            IndexExpression exprUpdated = expr.Update(newProperty, newArguments);

            // Replace Object and Arguments of IndexExpression.
            IndexExpressionHelpers.AssertEqual(
                exprUpdated,
                Expression.MakeIndex(newProperty, instance.DefaultIndexer, newArguments));

            // Invoke to check expression.
            IndexExpressionHelpers.AssertInvokeCorrect(100, expr);
            IndexExpressionHelpers.AssertInvokeCorrect(201, exprUpdated);
        }