Exemplo n.º 1
0
        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
            Assert2.NotNull(func);
            var result = func(model);

            Assert2.Equal(expected, result);
        }
Exemplo n.º 2
0
        public void CreateInstance_WithAbstractTypeAndPublicConstructor_ThrowsCorrectException()
        {
            // Act & Assert
            var ex = Assert2.Throws <InvalidOperationException>(() => ActivatorUtilities.CreateInstance(default(IServiceProvider), typeof(AbstractFoo)));

            //var msg = "A suitable constructor for type 'Microsoft.Extensions.DependencyInjection.Specification.DependencyInjectionSpecificationTests+AbstractFoo' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.";
            Assert2.NotNull(ex.Message);
        }
Exemplo n.º 3
0
        //[Theory]
        //[MemberData(nameof(CreateInstanceFuncs))]
        public void TypeActivatorWorksWithStaticCtor_Theory(CreateInstanceFunc createFunc)
        {
            // Act
            var anotherClass = CreateInstance <ClassWithStaticCtor>(createFunc, provider: null);

            // Assert
            Assert2.NotNull(anotherClass);
        }
Exemplo n.º 4
0
        public void ObjectToDictionary_WithPlainObjectType_ReturnsEmptyDictionary()
        {
            // Arrange
            var value = new object();

            // Act
            var dictValues = PropertyHelper.ObjectToDictionary(value);

            // Assert
            Assert2.NotNull(dictValues);
            Assert2.Equal(0, dictValues.Count);
        }
Exemplo n.º 5
0
        public void ObjectToDictionary_WithNullObject_ReturnsEmptyDictionary()
        {
            // Arrange
            object value = null;

            // Act
            var dictValues = PropertyHelper.ObjectToDictionary(value);

            // Assert
            Assert2.NotNull(dictValues);
            Assert2.Equal(0, dictValues.Count);
        }
Exemplo n.º 6
0
        public void PropertyHelper_ReturnsGetterDelegate()
        {
            // Arrange
            var anonymous = new { bar = "baz" };
            var property  = PropertyHelper.GetPropertyAccessors(anonymous.GetType()).First().Property;

            // Act
            var helper = new PropertyHelper.PropertyAccessor(property);

            // Assert
            Assert2.NotNull(helper.ValueGetter);
            Assert2.Equal("baz", helper.ValueGetter(anonymous));
        }
Exemplo n.º 7
0
        public void Process_ConstFieldLookup_WthNullModel()
        {
            // Arrange
            var expression = GetTestModelExpression(m => DifferentModel.Constant);

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

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

            Assert2.Equal(10, result);
        }
Exemplo n.º 8
0
        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
            Assert2.NotNull(func1);
            Assert2.Same(func1, func2);
        }
Exemplo n.º 9
0
        public void ObjectToDictionary_ReturnsCaseInsensitiveDictionary()
        {
            // Arrange
            var value = new { TEST = "value", oThEr = 1 };

            // Act
            var dictValues = PropertyHelper.ObjectToDictionary(value);

            // Assert
            Assert2.NotNull(dictValues);
            Assert2.Equal(2, dictValues.Count);
            Assert2.Equal("value", dictValues["test"]);
            Assert2.Equal(1, dictValues["other"]);
        }
Exemplo n.º 10
0
        public void ObjectToDictionary_WithAnonymousType_LooksUpProperties()
        {
            // Arrange
            var value = new { test = "value", other = 1 };

            // Act
            var dictValues = PropertyHelper.ObjectToDictionary(value);

            // Assert
            Assert2.NotNull(dictValues);
            Assert2.Equal(2, dictValues.Count);
            Assert2.Equal("value", dictValues["test"]);
            Assert2.Equal(1, dictValues["other"]);
        }
Exemplo n.º 11
0
        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
            Assert2.NotNull(func1);
            Assert2.Same(func1, func2);
        }
Exemplo n.º 12
0
        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
            Assert2.NotNull(func1);
            Assert2.Same(func1, func2);
        }
Exemplo n.º 13
0
        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
            Assert2.NotNull(func1);
            Assert2.Same(func1, func2);
        }
Exemplo n.º 14
0
        public async Task TargetMethodReturningValueTaskOfValueType_CanBeInvokedViaExecuteAsync()
        {
            // Arrange
            var executor = GetExecutorForMethod(nameof(TestObject.ValueTaskOfValueType));

            // Act
            var result = await executor.ExecuteAsync(_targetObject, new object[] { 123 });

            // Assert
            Assert2.True(executor.IsMethodAsync);
            Assert2.Same(typeof(int), executor.AsyncResultType);
            Assert2.NotNull(result);
            Assert2.Equal(123, (int)result);
        }
Exemplo n.º 15
0
        public async Task TargetMethodReturningCustomAwaitableOfReferenceType_CanInvokeViaExecute()
        {
            // Arrange
            var executor = GetExecutorForMethod(nameof(TestObject.CustomAwaitableOfReferenceTypeAsync));

            // Act
            var result = await(TestAwaitable <TestObject>) executor.Execute(_targetObject, new object[] { "Hello", 123 });

            // Assert
            Assert2.True(executor.IsMethodAsync);
            Assert2.Same(typeof(TestObject), executor.AsyncResultType);
            Assert2.NotNull(result);
            Assert2.Equal("Hello 123", result.value);
        }
Exemplo n.º 16
0
        public void Process_ConstLookup_WithNullModel()
        {
            // Arrange
            var differentModel = new DifferentModel();
            var expression     = GetTestModelExpression(m => differentModel);

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

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

            Assert2.Same(differentModel, result);
        }
Exemplo n.º 17
0
        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
            Assert2.NotNull(func);
            var result = func(model);

            Assert2.Null(result);
        }
Exemplo n.º 18
0
        public void Process_IdentityExpression()
        {
            // Arrange
            var model      = new TestModel();
            var expression = GetTestModelExpression(m => m);

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

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

            Assert2.Same(model, result);
        }
Exemplo n.º 19
0
        public void Process_ConstLookup_ReturningNull()
        {
            // Arrange
            var model      = new TestModel();
            var expression = GetTestModelExpression(m => (DifferentModel)null);

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

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

            Assert2.Null(result);
        }
Exemplo n.º 20
0
        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
            Assert2.NotNull(func);
            var result = func(nullableModel);

            Assert2.Null(result);
        }
Exemplo n.º 21
0
        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
            Assert2.NotNull(func);
            var result = func(model);

            Assert2.Equal(1, result);
        }
Exemplo n.º 22
0
        public void Process_ConstLookup_WithPrimitiveConstant()
        {
            // Arrange
            var model      = new TestModel();
            var expression = GetTestModelExpression(m => 10);

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

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

            Assert2.Equal(10, result);
        }
Exemplo n.º 23
0
        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
            Assert2.NotNull(func);
            var result = func(model);

            Assert2.Null(result);
        }
Exemplo n.º 24
0
        public void Process_SimpleMemberAccess_ToPrimitive_WithNullModel()
        {
            // Arrange
            var model      = (TestModel)null;
            var expression = GetTestModelExpression(m => m.Age);

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

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

            Assert2.Null(result);
        }
Exemplo n.º 25
0
        public void Process_StaticFieldAccess()
        {
            // Arrange
            var model      = new TestModel();
            var expression = GetTestModelExpression(m => TestModel.StaticField);

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

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

            Assert2.Equal("StaticValue", result);
        }
Exemplo n.º 26
0
        public void Process_SimpleMemberAccess_OnTypeWithBadEqualityComparer_WithDefaultValue()
        {
            // Arrange
            var model      = default(BadEqualityValueTypeModel);
            var expression = GetExpression <BadEqualityValueTypeModel, int>(m => m.Id);

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

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

            Assert2.Equal(model.Id, result);
        }
Exemplo n.º 27
0
        public async Task TargetMethodReturningCustomAwaitableOfValueType_CanInvokeViaExecuteAsync()
        {
            // Arrange
            var executor = GetExecutorForMethod(nameof(TestObject.CustomAwaitableOfValueTypeAsync));

            // Act
            var result = await executor.ExecuteAsync(_targetObject, new object[] { 123, 456 });

            // Assert
            Assert2.True(executor.IsMethodAsync);
            Assert2.Same(typeof(int), executor.AsyncResultType);
            Assert2.NotNull(result);
            Assert2.IsType <int>(result);
            Assert2.Equal(579, (int)result);
        }
Exemplo n.º 28
0
        public void GetProperties_ExcludesIndexersAndPropertiesWithoutPublicGetters()
        {
            // Arrange
            var type = typeof(DerivedClassWithNonReadableProperties);

            // Act
            var result = PropertyHelper.GetPropertyAccessors(type).ToArray();

            // Assert
            Assert2.NotNull(result);

            //Assert2.Equal(3, result.Length);
            //Assert2.Equal("Visible", result[0].Name);
            //Assert2.Equal("PropA", result[1].Name);
            //Assert2.Equal("PropB", result[2].Name);
        }
Exemplo n.º 29
0
        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
            Assert2.NotNull(func);
            var result = func(nullableModel);

            Assert2.Equal(model, result);
        }
Exemplo n.º 30
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
            Assert2.NotNull(func);
            var result = func(model);

            Assert2.Same(differentModel, result);
        }