protected override Expression VisitMethodCall(MethodCallExpression m)
        {
            if (m.Method.Name == "GetKey" && m.Arguments.Count == 1) {
                var fakeProperty = new FakePropertyInfo(RootClass.DefaultIdentifierColumnName, m.Arguments[0].Type, m.Type);
                return Expression.Property(m.Arguments[0], fakeProperty);
            }

            return base.VisitMethodCall(m);
        }
        protected override PropertyInfo GetPropertyImpl(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
        {
            PropertyInfo info = base.GetPropertyImpl(name, bindingAttr, binder, returnType, types, modifiers);

            if (name == "ItemTemplate" || name == "AlternatingItemTemplate")
                info = new FakePropertyInfo(info, this.repeaterItemType);

            return info;
        }
        public void Intercept_InterceptsMultiplePropertiesOnDifferentInterfaces_ReturnsExpectedPropertyValues()
        {
            //Arrange   
            var service = Substitute.For<IAbstractService>();
            var config1 = new StubAbstractTypeConfiguration();
            var config2 = new StubAbstractTypeConfiguration();

            var property1 = new StubAbstractPropertyConfiguration();
            var property2 = new StubAbstractPropertyConfiguration();

            var mapper1 = new StubAbstractDataMapper();
            var mapper2 = new StubAbstractDataMapper();

            var expected1 = "test random 1";
            var expected2 = "some other random";

            var propertyName1 = "Property1";
            var propertyName2 = "Property2";


            var info1 = new FakePropertyInfo(typeof(string), propertyName1, typeof(IStubTarget));
            var info2 = new FakePropertyInfo(typeof(string), propertyName2, typeof(IStubTarget2));

            config1.AddProperty(property1);
            config2.AddProperty(property2);

            property1.Mapper = mapper1;
            property2.Mapper = mapper2;
            property1.PropertyInfo = info1;
            property2.PropertyInfo = info2;

            mapper1.Value = expected1;
            mapper2.Value = expected2;

            var args = new ObjectConstructionArgs(null, null, config1, service);
            args.Parameters = new Dictionary<string, object>();
            args.Parameters[CreateMultiInferaceTask.MultiInterfaceConfigsKey] = new[] {config2};
            var interceptor = new MultiInterfacePropertyInterceptor(args);

            var invocation1 = Substitute.For<IInvocation>();
            var invocation2 = Substitute.For<IInvocation>();

            invocation1.Method.Returns(typeof(IStubTarget).GetProperty(propertyName1).GetGetMethod());
            invocation2.Method.Returns(typeof(IStubTarget2).GetProperty(propertyName2).GetGetMethod());

            //Act
            interceptor.Intercept(invocation1);
            interceptor.Intercept(invocation2);

            //Assert
            Assert.AreEqual(expected1, invocation1.ReturnValue);
            Assert.AreEqual(expected2, invocation2.ReturnValue);

        }
        public void Intercept_InterceptsMultiplePropertiesOnDifferentInterfaces_ReturnsExpectedPropertyValues()
        {
            //Arrange
            var service = Substitute.For <IAbstractService>();
            var config1 = new StubAbstractTypeConfiguration();
            var config2 = new StubAbstractTypeConfiguration();

            var property1 = new StubAbstractPropertyConfiguration();
            var property2 = new StubAbstractPropertyConfiguration();

            var mapper1 = new StubAbstractDataMapper();
            var mapper2 = new StubAbstractDataMapper();

            var expected1 = "test random 1";
            var expected2 = "some other random";

            var propertyName1 = "Property1";
            var propertyName2 = "Property2";


            var info1 = new FakePropertyInfo(typeof(string), propertyName1, typeof(IStubTarget));
            var info2 = new FakePropertyInfo(typeof(string), propertyName2, typeof(IStubTarget2));

            config1.AddProperty(property1);
            config2.AddProperty(property2);

            property1.Mapper       = mapper1;
            property2.Mapper       = mapper2;
            property1.PropertyInfo = info1;
            property2.PropertyInfo = info2;

            mapper1.Value = expected1;
            mapper2.Value = expected2;

            var args = new ObjectConstructionArgs(null, null, config1, service, new ModelCounter());

            args.Parameters = new Dictionary <string, object>();
            args.Parameters[CreateMultiInferaceTask.MultiInterfaceConfigsKey] = new[] { config2 };
            var interceptor = new MultiInterfacePropertyInterceptor(args);

            var invocation1 = Substitute.For <IInvocation>();
            var invocation2 = Substitute.For <IInvocation>();

            invocation1.Method.Returns(typeof(IStubTarget).GetProperty(propertyName1).GetGetMethod());
            invocation2.Method.Returns(typeof(IStubTarget2).GetProperty(propertyName2).GetGetMethod());

            //Act
            interceptor.Intercept(invocation1);
            interceptor.Intercept(invocation2);

            //Assert
            Assert.AreEqual(expected1, invocation1.ReturnValue);
            Assert.AreEqual(expected2, invocation2.ReturnValue);
        }
        protected override PropertyInfo GetPropertyImpl(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
        {
            PropertyInfo info = base.GetPropertyImpl(name, bindingAttr, binder, returnType, types, modifiers);

            if (name == "ItemTemplate" || name == "AlternatingItemTemplate")
            {
                info = new FakePropertyInfo(info, this.repeaterItemType);
            }

            return(info);
        }
Beispiel #6
0
        public void Intercept_InterceptsProperties_SetsExpectedPropertyValue()
        {
            //Arrange
            var service = Substitute.For <IAbstractService>();
            var config  = new StubAbstractTypeConfiguration();
            var context = Substitute.For <AbstractTypeCreationContext>();

            context.Options = new GetOptions {
                Lazy = LazyLoading.Enabled
            };

            var propertyName = "Property1";

            var info = typeof(IStubTarget).GetProperty(propertyName);

            var property = new StubAbstractPropertyConfiguration();

            property.PropertyInfo = info;
            var mapper = new StubAbstractDataMapper();

            var expected = "test random 1";

            var info1 = new FakePropertyInfo(typeof(string), propertyName, typeof(IStubTarget));

            config.AddProperty(property);
            config.Type = typeof(IStubTarget);

            property.Mapper       = mapper;
            property.PropertyInfo = info1;

            var args = new ObjectConstructionArgs(null, context, config, service)
            {
                Parameters = new Dictionary <string, object>()
            };
            var interceptor = new InterfacePropertyInterceptor(args, new LazyLoadingHelper());

            var setInvocation = Substitute.For <IInvocation>();

            setInvocation.Arguments.Returns(new[] { expected });
            setInvocation.Method.Returns(typeof(IStubTarget).GetProperty(propertyName).GetSetMethod());

            //Act
            interceptor.Intercept(setInvocation);

            //Assert
            var getInvocation = Substitute.For <IInvocation>();

            getInvocation.Method.Returns(typeof(IStubTarget).GetProperty(propertyName).GetGetMethod());

            interceptor.Intercept(getInvocation);

            Assert.AreEqual(expected, getInvocation.ReturnValue);
        }
        public void Test_Construct_WithPropInfo_ShouldSetName()
        {
            //---------------Set up test pack-------------------
            var          propName     = GetRandomString();
            PropertyInfo propertyInfo = new FakePropertyInfo(propName);

            //---------------Assert Precondition----------------
            Assert.AreEqual(propName, propertyInfo.Name);
            //---------------Execute Test ----------------------
            var propDescriptor = new PropertyDescriptorPropInfoSpy(propertyInfo);

            //---------------Test Result -----------------------
            Assert.AreEqual(propName, propDescriptor.Name);
        }
        public void Test_DisplayName_ShouldBePropertyName_CamelCaseDelimited()
        {
            //---------------Set up test pack-------------------
            const string       propName       = "ThisIsCamelCased";
            var                propertyInfo   = new FakePropertyInfo(propName);
            PropertyDescriptor propDescriptor = new PropertyDescriptorPropInfoSpy(propertyInfo);

            //---------------Assert Precondition----------------
            Assert.AreEqual(propName, propertyInfo.Name);
            //---------------Execute Test ----------------------
            var actualPropDescriptorDisplayName = propDescriptor.DisplayName;

            //---------------Test Result -----------------------
            Assert.AreEqual("This Is Camel Cased", actualPropDescriptorDisplayName);
        }
        public void Test_PropertyType_ShouldReturnPropertyTypePropInfo()
        {
            //---------------Set up test pack-------------------
            var expectedPropType = typeof(int);
            var propertyInfo     = new FakePropertyInfo(GetRandomString(), expectedPropType);

            PropertyDescriptorPropInfo propDescriptor = new PropertyDescriptorPropInfoSpy(propertyInfo);

            //---------------Assert Precondition----------------
            Assert.AreEqual(expectedPropType, propertyInfo.PropertyType);
            //---------------Execute Test ----------------------
            var propertyType = propDescriptor.PropertyType;

            //---------------Test Result -----------------------
            Assert.AreEqual(expectedPropType, propertyType);
        }
        public void Test_ComponentType_ShouldReturnGridColumnsClassDefsClassType()
        {
            //---------------Set up test pack-------------------
            var propertyInfo          = new FakePropertyInfo();
            var expectedComponentType = typeof(FakeBO);

            propertyInfo.SetReflectedType(expectedComponentType);
            PropertyDescriptorPropInfo propDescriptor = new PropertyDescriptorPropInfoSpy(propertyInfo);

            //---------------Assert Precondition----------------
            Assert.IsNotNull(propertyInfo.ReflectedType);
            //---------------Execute Test ----------------------
            var componentType = propDescriptor.ComponentType;

            //---------------Test Result -----------------------
            Assert.AreSame(expectedComponentType, componentType);
        }
        public void Test_IsReadOnly_WhenHasSetMethod_AndReadOnlyAttributeTrue_ShouldReturnTrue()
        {
            //---------------Set up test pack-------------------
            FakePropertyInfo propertyInfo = GetMockPropertyInfo();

            propertyInfo.Stub(info => info.GetCustomAttributes(typeof(ReadOnlyAttribute), true)).Return(new object[] { new ReadOnlyAttribute(true) });
            PropertyDescriptorPropInfo propDescriptor = new PropertyDescriptorPropInfoSpy(propertyInfo);

            //---------------Assert Precondition----------------
            Assert.IsNotNull(propertyInfo.GetSetMethod());
            Assert.IsTrue(propertyInfo.GetAttribute <ReadOnlyAttribute>().IsReadOnly);
            //---------------Execute Test ----------------------
            var isReadOnly = propDescriptor.IsReadOnly;

            //---------------Test Result -----------------------
            Assert.IsTrue(isReadOnly, "If has setter but ReadOnlyAttribute is true should be ReadOnly.");
        }
        public IPropertyConfiguration <TEntity, TProperty> Property <TProperty>(string propertyName)
        {
            var property = PropertyConfigurationDictionary.Keys.FirstOrDefault(p => p.Name == propertyName);

            if (property != null)
            {
                return((IPropertyConfiguration <TEntity, TProperty>)PropertyConfigurationDictionary[property]);
            }

            var propertyType = typeof(TProperty);

            property = new FakePropertyInfo(EntityType, propertyType, propertyName);

            var propertyConfigurationType =
                typeof(PropertyConfiguration <,>).MakeGenericType(EntityType, propertyType);
            var propertyConfiguration = (PropertyConfiguration)Activator.CreateInstance(propertyConfigurationType, new object[] { property });

            PropertyConfigurationDictionary[property] = propertyConfiguration;

            return((IPropertyConfiguration <TEntity, TProperty>)propertyConfiguration);
        }
        public void Intercept_InterceptsProperties_ReturnsExpectedPropertyValue()
        {
            //Arrange
            var service = Substitute.For <IAbstractService>();
            var config  = new StubAbstractTypeConfiguration();
            var context = Substitute.For <AbstractTypeCreationContext>();

            var property = new StubAbstractPropertyConfiguration();
            var mapper   = new StubAbstractDataMapper();

            var expected     = "test random 1";
            var propertyName = "Property1";

            var info = new FakePropertyInfo(typeof(string), propertyName, typeof(IStubTarget));

            config.AddProperty(property);
            config.Type = typeof(IStubTarget);

            property.Mapper       = mapper;
            property.PropertyInfo = info;

            mapper.Value = expected;

            var args = new ObjectConstructionArgs(null, context, config, service)
            {
                Parameters = new Dictionary <string, object>()
            };
            var interceptor = new InterfacePropertyInterceptor(args);

            var invocation = Substitute.For <IInvocation>();

            invocation.Method.Returns(typeof(IStubTarget).GetProperty(propertyName).GetGetMethod());

            //Act
            interceptor.Intercept(invocation);

            //Assert
            Assert.AreEqual(expected, invocation.ReturnValue);
        }
        public void Test_GetValue_WhenComponentNull_ShouldRaiseError()
        {
            //---------------Set up test pack-------------------
            var propertyInfo = new FakePropertyInfo();
            PropertyDescriptorPropInfo propDescriptor = new PropertyDescriptorPropInfoSpy(propertyInfo);
            object x = null;

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            try
            {
                propDescriptor.GetValue(x);
                Assert.Fail("Expected to throw an ArgumentNullException");
            }
            //---------------Test Result -----------------------
            catch (ArgumentNullException ex)
            {
                StringAssert.Contains("component", ex.ParamName);
                StringAssert.Contains("Value cannot be null.", ex.Message);
            }
        }
        public void Intercept_InterceptsProperties_ReturnsExpectedPropertyValue()
        {
            //Arrange   
            var service = Substitute.For<IAbstractService>();
            var config = new StubAbstractTypeConfiguration();
            var context = Substitute.For<AbstractTypeCreationContext>();

            var property = new StubAbstractPropertyConfiguration();
            var mapper = new StubAbstractDataMapper();

            var expected = "test random 1";
            var propertyName = "Property1";

            var info = new FakePropertyInfo(typeof(string), propertyName, typeof(IStubTarget));

            config.AddProperty(property);
            config.Type = typeof(IStubTarget);

            property.Mapper = mapper;
            property.PropertyInfo = info;

            mapper.Value = expected;

            var args = new ObjectConstructionArgs(null, context, config, service, new ModelCounter())
            {
                Parameters = new Dictionary<string, object>()
            };
            var interceptor = new InterfacePropertyInterceptor(args);

            var invocation = Substitute.For<IInvocation>();

            invocation.Method.Returns(typeof(IStubTarget).GetProperty(propertyName).GetGetMethod());

            //Act
            interceptor.Intercept(invocation);

            //Assert
            Assert.AreEqual(expected, invocation.ReturnValue);
        }