Exemplo n.º 1
0
        private static string GetIndexerInvocation(
            Expression expression,
            ParameterExpression[] parameters)
        {
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            var converted     = Expression.Convert(expression, typeof(object));
            var fakeParameter = Expression.Parameter(typeof(object), null);
            var lambda        = Expression.Lambda <Func <object, object> >(converted, fakeParameter);
            Func <object, object> func;

            try
            {
                func = CachedExpressionCompiler.Process(lambda);
            }
            catch (InvalidOperationException ex)
            {
                throw new InvalidOperationException(
                          Resources.FormatExpressionHelper_InvalidIndexerExpression(expression, parameters[0].Name),
                          ex);
            }

            return("[" + Convert.ToString(func(null), CultureInfo.InvariantCulture) + "]");
        }
        public void Process_ChainedMemberAccess_LongChain_WithNullableValueType()
        {
            // Arrange
            var expected = "TestVal";
            var model    = new Chain0Model
            {
                Chain1 = new Chain1Model
                {
                    NullableValueTypeModel = new ValueType1
                    {
                        TestModel = new TestModel {
                            DifferentModel = new DifferentModel {
                                Name = expected
                            }
                        }
                    }
                }
            };

            var expression = GetExpression <Chain0Model, string>(m => m.Chain1.NullableValueTypeModel.Value.TestModel.DifferentModel.Name);

            // Act
            var func = CachedExpressionCompiler.Process(expression);

            // Assert
            Assert.NotNull(func);
            var result = func(model);

            Assert.Equal(expected, result);
        }
        public void Process_ArrayMemberAccess_ReturnsNull()
        {
            // Arrange
            var expression = GetTestModelExpression(m => m.Sizes[1]);

            // Act
            var func = CachedExpressionCompiler.Process(expression);

            // Assert
            Assert.Null(func);
        }
        public void Process_MemberAccess_OnCapturedVariable_ReturnsNull()
        {
            // Arrange
            var differentModel = "Hello world";
            var expression     = GetTestModelExpression(m => differentModel.Length);

            // Act
            var func = CachedExpressionCompiler.Process(expression);

            // Assert
            Assert.Null(func);
        }
        public void Process_CachesIdentityExpression()
        {
            // Arrange
            var expression1 = GetTestModelExpression(m => m);
            var expression2 = GetTestModelExpression(m => m);

            // Act
            var func1 = CachedExpressionCompiler.Process(expression1);
            var func2 = CachedExpressionCompiler.Process(expression2);

            // Assert
            Assert.NotNull(func1);
            Assert.Same(func1, func2);
        }
        public void Process_ConstFieldLookup_WthNullModel()
        {
            // Arrange
            var expression = GetTestModelExpression(m => DifferentModel.Constant);

            // Act
            var func = CachedExpressionCompiler.Process(expression);

            // Assert
            Assert.NotNull(func);
            var result = func(null);

            Assert.Equal(10, result);
        }
        public void Process_CachesStaticPropertyAccess()
        {
            // Arrange
            var expression1 = GetTestModelExpression(m => TestModel.StaticProperty);
            var expression2 = GetTestModelExpression(m => TestModel.StaticProperty);

            // Act
            var func1 = CachedExpressionCompiler.Process(expression1);
            var func2 = CachedExpressionCompiler.Process(expression2);

            // Assert
            Assert.NotNull(func1);
            Assert.Same(func1, func2);
        }
        public void Process_CachesSimpleMemberAccess()
        {
            // Arrange
            var expression1 = GetTestModelExpression(m => m.Name);
            var expression2 = GetTestModelExpression(m => m.Name);

            // Act
            var func1 = CachedExpressionCompiler.Process(expression1);
            var func2 = CachedExpressionCompiler.Process(expression2);

            // Assert
            Assert.NotNull(func1);
            Assert.Same(func1, func2);
        }
        public void Process_CachesChainedMemberAccess_ToValueType()
        {
            // Arrange
            var expression1 = GetTestModelExpression(m => m.Date.Year);
            var expression2 = GetTestModelExpression(m => m.Date.Year);

            // Act
            var func1 = CachedExpressionCompiler.Process(expression1);
            var func2 = CachedExpressionCompiler.Process(expression2);

            // Assert
            Assert.NotNull(func1);
            Assert.Same(func1, func2);
        }
        public void Process_MemberAccessOnCapturedVariable_ReturnsNull()
        {
            // Arrange
            var differentModel = new DifferentModel {
                Name = "Test"
            };
            var expression = GetTestModelExpression(m => differentModel.Name);

            // Act
            var func = CachedExpressionCompiler.Process(expression);

            // Assert
            Assert.Null(func);
        }
        public void Process_SimpleMemberAccess_OnNullableValueType_WithNullValue()
        {
            // Arrange
            var nullableModel = (DateTime?)null;
            var expression    = GetExpression <DateTime?, DateTime>(m => m.Value);

            // Act
            var func = CachedExpressionCompiler.Process(expression);

            // Assert
            Assert.NotNull(func);
            var result = func(nullableModel);

            Assert.Null(result);
        }
        public void Process_StaticFieldAccess()
        {
            // Arrange
            var model      = new TestModel();
            var expression = GetTestModelExpression(m => TestModel.StaticField);

            // Act
            var func = CachedExpressionCompiler.Process(expression);

            // Assert
            Assert.NotNull(func);
            var result = func(model);

            Assert.Equal("StaticValue", result);
        }
        public void Process_ConstLookup_WithPrimitiveConstant()
        {
            // Arrange
            var model      = new TestModel();
            var expression = GetTestModelExpression(m => 10);

            // Act
            var func = CachedExpressionCompiler.Process(expression);

            // Assert
            Assert.NotNull(func);
            var result = func(model);

            Assert.Equal(10, result);
        }
        public void Process_SimpleMemberAccess_OnValueType_WithDefaultValue()
        {
            // Arrange
            var model      = default(DateTime);
            var expression = GetExpression <DateTime, int>(m => m.Year);

            // Act
            var func = CachedExpressionCompiler.Process(expression);

            // Assert
            Assert.NotNull(func);
            var result = func(model);

            Assert.Equal(1, result);
        }
        public void Process_SimpleMemberAccess_OnTypeWithBadEqualityComparer_WithDefaultValue()
        {
            // Arrange
            var model      = (BadEqualityValueTypeModel) default;
            var expression = GetExpression <BadEqualityValueTypeModel, int>(m => m.Id);

            // Act
            var func = CachedExpressionCompiler.Process(expression);

            // Assert
            Assert.NotNull(func);
            var result = func(model);

            Assert.Equal(model.Id, result);
        }
        public void Process_SimpleMemberAccess_OnTypeWithBadEqualityComparer_WithNullModel()
        {
            // Arrange
            var model      = (BadEqualityModel)null;
            var expression = GetExpression <BadEqualityModel, int>(m => m.Id);

            // Act
            var func = CachedExpressionCompiler.Process(expression);

            // Assert
            Assert.NotNull(func);
            var result = func(model);

            Assert.Null(result);
        }
        public void Process_SimpleMemberAccess_ToPrimitive_WithNullModel()
        {
            // Arrange
            var model      = (TestModel)null;
            var expression = GetTestModelExpression(m => m.Age);

            // Act
            var func = CachedExpressionCompiler.Process(expression);

            // Assert
            Assert.NotNull(func);
            var result = func(model);

            Assert.Null(result);
        }
        public void Process_ConstLookup_ReturningNull()
        {
            // Arrange
            var model      = new TestModel();
            var expression = GetTestModelExpression(m => (DifferentModel)null);

            // Act
            var func = CachedExpressionCompiler.Process(expression);

            // Assert
            Assert.NotNull(func);
            var result = func(model);

            Assert.Null(result);
        }
        public void Process_IdentityExpression()
        {
            // Arrange
            var model      = new TestModel();
            var expression = GetTestModelExpression(m => m);

            // Act
            var func = CachedExpressionCompiler.Process(expression);

            // Assert
            Assert.NotNull(func);
            var result = func(model);

            Assert.Same(model, result);
        }
        public void Process_ConstLookup_WithNullModel()
        {
            // Arrange
            var differentModel = new DifferentModel();
            var expression     = GetTestModelExpression(m => differentModel);

            // Act
            var func = CachedExpressionCompiler.Process(expression);

            // Assert
            Assert.NotNull(func);
            var result = func(null);

            Assert.Same(differentModel, result);
        }
        public void Process_ChainedMemberAccess_ToReferenceType_WithNullModel()
        {
            // Arrange
            var model      = (TestModel)null;
            var expression = GetTestModelExpression(m => m.DifferentModel.Name);

            // Act
            var func = CachedExpressionCompiler.Process(expression);

            // Assert
            Assert.NotNull(func);
            var result = func(model);

            Assert.Null(result);
        }
Exemplo n.º 22
0
        private static void InsertIndexerInvocationText(
            StringBuilder builder,
            Expression indexExpression,
            LambdaExpression parentExpression)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (indexExpression == null)
            {
                throw new ArgumentNullException(nameof(indexExpression));
            }

            if (parentExpression == null)
            {
                throw new ArgumentNullException(nameof(parentExpression));
            }

            if (parentExpression.Parameters == null)
            {
                throw new ArgumentException(Resources.FormatPropertyOfTypeCannotBeNull(
                                                nameof(parentExpression.Parameters),
                                                nameof(parentExpression)));
            }

            var converted     = Expression.Convert(indexExpression, typeof(object));
            var fakeParameter = Expression.Parameter(typeof(object), null);
            var lambda        = Expression.Lambda <Func <object, object> >(converted, fakeParameter);
            Func <object, object> func;

            try
            {
                func = CachedExpressionCompiler.Process(lambda) ?? lambda.Compile();
            }
            catch (InvalidOperationException ex)
            {
                var parameters = parentExpression.Parameters.ToArray();
                throw new InvalidOperationException(
                          Resources.FormatExpressionHelper_InvalidIndexerExpression(indexExpression, parameters[0].Name),
                          ex);
            }

            builder.Insert(0, ']');
            builder.Insert(0, Convert.ToString(func(null), CultureInfo.InvariantCulture));
            builder.Insert(0, '[');
        }
        public void Process_CapturedVariable_WithNullModel()
        {
            // Arrange
            var differentModel = new DifferentModel();
            var model          = (TestModel)null;
            var expression     = GetTestModelExpression(m => differentModel);

            // Act
            var func = CachedExpressionCompiler.Process(expression);

            // Assert
            Assert.NotNull(func);
            var result = func(model);

            Assert.Same(differentModel, result);
        }
        public void Process_SimpleMemberAccess_OnNullableValueType()
        {
            // Arrange
            var model         = new DateTime(2000, 1, 1);
            var nullableModel = (DateTime?)model;
            var expression    = GetExpression <DateTime?, DateTime>(m => m.Value);

            // Act
            var func = CachedExpressionCompiler.Process(expression);

            // Assert
            Assert.NotNull(func);
            var result = func(nullableModel);

            Assert.Equal(model, result);
        }
        public void Process_StaticPropertyAccess_WithNullModel()
        {
            // Arrange
            var expected = "TestValue";

            TestModel.StaticProperty = expected;
            var expression = GetTestModelExpression(m => TestModel.StaticProperty);

            // Act
            var func = CachedExpressionCompiler.Process(expression);

            // Assert
            Assert.NotNull(func);
            var result = func(null);

            Assert.Equal(expected, result);
        }
        public void Process_SimpleMemberAccess()
        {
            // Arrange
            var model = new TestModel {
                Name = "Test"
            };
            var expression = GetTestModelExpression(m => m.Name);

            // Act
            var func = CachedExpressionCompiler.Process(expression);

            // Assert
            Assert.NotNull(func);
            var result = func(model);

            Assert.Equal("Test", result);
        }
        public void Process_ComplexChainedMemberAccess_ReturnsNull()
        {
            // Arrange
            var expected = "SomeName";
            var model    = new TestModel {
                DifferentModels = new[] { new DifferentModel {
                                              Name = expected
                                          } }
            };
            var expression = GetTestModelExpression(m => m.DifferentModels[0].Name);

            // Act
            var func = CachedExpressionCompiler.Process(expression);

            // Assert
            Assert.Null(func);
        }
        public void Process_SimpleMemberAccess_ToPrimitive()
        {
            // Arrange
            var model = new TestModel {
                Age = 12
            };
            var expression = GetTestModelExpression(m => m.Age);

            // Act
            var func = CachedExpressionCompiler.Process(expression);

            // Assert
            Assert.NotNull(func);
            var result = func(model);

            Assert.Equal(12, result);
        }
        public void Process_ChainedMemberAccess_OfValueTypes_WithNullValuedNullable_ReturningValueType()
        {
            // Arrange
            var model = new ValueType1 {
                NullableValueType2 = null
            };
            var expression = GetExpression <ValueType1, DateTime>(m => m.NullableValueType2.Value.Date);

            // Act
            var func = CachedExpressionCompiler.Process(expression);

            // Assert
            Assert.NotNull(func);
            var result = func(model);

            Assert.Null(result);
        }
        public void Process_ChainedMemberAccess_ToValueType()
        {
            // Arrange
            var dateTime = new DateTime(2000, 1, 1);
            var model    = new TestModel {
                Date = dateTime
            };
            var expression = GetTestModelExpression(m => m.Date.Year);

            // Act
            var func = CachedExpressionCompiler.Process(expression);

            // Assert
            Assert.NotNull(func);
            var result = func(model);

            Assert.Equal(2000, result);
        }