Beispiel #1
0
        public void GetProperty_ImplicitInterfaceReadOnlyScalar_WithReadWriteImplementation()
        {
            var instance = ObjectFactory.Create <ClassWithReferenceType <SimpleReferenceType> > (ParamList.Empty);
            var value    = new SimpleReferenceType();

            instance.ImplicitInterfaceReadOnlyScalar = value;
            Assert.That(((IBusinessObject)instance).GetProperty("ImplicitInterfaceReadOnlyScalar"), Is.SameAs(value));
        }
Beispiel #2
0
        public void SetProperty_ExplicitInterfaceScalar()
        {
            var instance = ObjectFactory.Create <ClassWithReferenceType <SimpleReferenceType> > (ParamList.Empty);
            var value    = new SimpleReferenceType();

            ((IBusinessObject)instance).SetProperty("ExplicitInterfaceScalar", value);
            Assert.That(((IInterfaceWithReferenceType <SimpleReferenceType>)instance).ExplicitInterfaceScalar, Is.SameAs(value));
        }
Beispiel #3
0
        public void SetValue_ExplicitInterface_Integration()
        {
            IInterfaceWithReferenceType <SimpleReferenceType> instance = new ClassWithReferenceType <SimpleReferenceType>();
            SimpleReferenceType value = new SimpleReferenceType();

            _explicitInterfaceAdapter.SetValue(instance, value, null);
            Assert.That(instance.ExplicitInterfaceScalar, Is.SameAs(value));
        }
 public void InstanceMethodWithMultipleParameters(
     SimpleReferenceType referenceTypeValue, int valueTypeValue, int?nullableValueTypeValue, SimpleReferenceType staticReferenceTypeValue)
 {
     InstanceReferenceTypeValue     = referenceTypeValue;
     InstanceValueTypeValue         = valueTypeValue;
     InstanceNullableValueTypeValue = nullableValueTypeValue;
     StaticReferenceTypeValue       = staticReferenceTypeValue;
 }
Beispiel #5
0
 private void AssertCanNotSet(PropertyInfoAdapter adapter, object instance, SimpleReferenceType value)
 {
     try
     {
         adapter.SetValue(instance, value, null);
     }
     catch (ArgumentException ex)
     {
         Assert.That(ex.Message, Is.EqualTo("Property set method not found."));
     }
 }
Beispiel #6
0
        public void SetValue_WithIndexerProperty_WithOneParameter_IndexParameterArrayLengthMismatch()
        {
            var scalar       = new SimpleReferenceType();
            var instanceStub = MockRepository.GenerateStub <IInterfaceWithReferenceType <SimpleReferenceType> >();

            var interfaceDeclarationProperty = typeof(IInterfaceWithReferenceType <SimpleReferenceType>).GetProperty("Item", new[] { typeof(int) });

            _implicitInterfaceAdapter = PropertyInfoAdapter.Create(interfaceDeclarationProperty);

            _implicitInterfaceAdapter.SetValue(instanceStub, scalar, new object[0]);
        }
        public void SetValue_ImplementationAddsSetAccessor()
        {
            var instance = new ClassWithReferenceType <SimpleReferenceType>();
            var value    = new SimpleReferenceType();

            _declarationPropertyInformationStub.Stub(stub => stub.GetSetMethod(true)).Return(null);
            _implementationPropertyInformationStub.Stub(stub => stub.GetSetMethod(true)).Return(
                MethodInfoAdapter.Create(typeof(ClassWithReferenceType <SimpleReferenceType>).GetProperty("ImplicitInterfaceScalar").GetSetMethod(true)));

            _interfaceImplementationPropertyInformation.SetValue(instance, value, null);
            Assert.That(instance.ImplicitInterfaceScalar, Is.SameAs(value));
        }
Beispiel #8
0
        public void SetValue_WithIndexerProperty_WithTwoParameters_IndexParameterArrayNull()
        {
            var scalar       = new SimpleReferenceType();
            var instanceStub = MockRepository.GenerateStub <IInterfaceWithReferenceType <SimpleReferenceType> >();

            var interfaceDeclarationProperty = typeof(IInterfaceWithReferenceType <SimpleReferenceType>)
                                               .GetProperty("Item", new[] { typeof(int), typeof(DateTime) });

            _implicitInterfaceAdapter = PropertyInfoAdapter.Create(interfaceDeclarationProperty);

            _implicitInterfaceAdapter.SetValue(instanceStub, scalar, null);
        }
        public void SetValue_ImplementationAddsSetAccessor_IndexedProperty()
        {
            var instance = new ClassWithReferenceType <SimpleReferenceType>();
            var value    = new SimpleReferenceType();

            _declarationPropertyInformationStub.Stub(stub => stub.GetSetMethod(true)).Return(null);
            _implementationPropertyInformationStub.Stub(stub => stub.GetSetMethod(true)).Return(
                MethodInfoAdapter.Create(typeof(ClassWithReferenceType <SimpleReferenceType>).GetProperty("Item", new[] { typeof(int) }).GetSetMethod(true)));

            _interfaceImplementationPropertyInformation.SetValue(instance, value, new object[] { 0 });
            Assert.That(instance[0], Is.SameAs(value));
        }
        public void GetValue()
        {
            var instance = new ClassWithReferenceType <SimpleReferenceType>();
            var value    = new SimpleReferenceType();

            instance.ImplicitInterfaceScalar = value;

            _declarationPropertyInformationStub.Stub(stub => stub.GetGetMethod(true)).Return(null);
            _implementationPropertyInformationStub.Stub(stub => stub.GetGetMethod(true)).Return(
                MethodInfoAdapter.Create(typeof(ClassWithReferenceType <SimpleReferenceType>).GetProperty("ImplicitInterfaceScalar").GetGetMethod(true)));

            Assert.That(_mixinIntroducedPropertyInformation.GetValue(instance, null), Is.SameAs(value));
        }
        public void ConvertToNativePropertyType_Scalar()
        {
            PropertyBase property = new StubPropertyBase(
                CreateParameters(
                    propertyInfo: GetPropertyInfo(typeof(ClassWithReferenceType <SimpleReferenceType>), "Scalar"),
                    underlyingType: typeof(SimpleReferenceType),
                    concreteType: typeof(SimpleReferenceType),
                    listInfo: null,
                    isRequired: false,
                    isReadOnly: false));
            var expected = new SimpleReferenceType();

            Assert.That(property.ConvertToNativePropertyType(expected), Is.SameAs(expected));
        }
Beispiel #12
0
        public void SetValue_WithIndexerProperty_WithOneParameter()
        {
            var scalar       = new SimpleReferenceType();
            var instanceMock = MockRepository.GenerateMock <IInterfaceWithReferenceType <SimpleReferenceType> >();

            instanceMock.Expect(mock => mock[10] = scalar);
            instanceMock.Replay();

            var interfaceDeclarationProperty = typeof(IInterfaceWithReferenceType <SimpleReferenceType>).GetProperty("Item", new[] { typeof(int) });

            _implicitInterfaceAdapter = PropertyInfoAdapter.Create(interfaceDeclarationProperty);

            _implicitInterfaceAdapter.SetValue(instanceMock, scalar, new object[] { 10 });
            instanceMock.VerifyAllExpectations();
        }
Beispiel #13
0
        public void SetValue_WithIndexerProperty_WithThreeParameters()
        {
            SimpleReferenceType scalar = new SimpleReferenceType();
            IInterfaceWithReferenceType <SimpleReferenceType> instanceMock = MockRepository.GenerateMock <IInterfaceWithReferenceType <SimpleReferenceType> >();

            instanceMock.Expect(mock => mock[10, new DateTime(2000, 1, 1), "foo"] = scalar);
            instanceMock.Replay();

            var interfaceDeclarationProperty = typeof(IInterfaceWithReferenceType <SimpleReferenceType>)
                                               .GetProperty("Item", new[] { typeof(int), typeof(DateTime), typeof(string) });

            _implicitInterfaceAdapter = PropertyInfoAdapter.Create(interfaceDeclarationProperty);

            _implicitInterfaceAdapter.SetValue(instanceMock, scalar, new object[] { 10, new DateTime(2000, 1, 1), "foo" });
            instanceMock.VerifyAllExpectations();
        }
        public void EmitMethodBody_ForClosedGeneric()
        {
            Type declaringType = typeof(ClassWithMethods);
            var  methodInfo    = declaringType.GetMethod("GenericInstanceMethod", BindingFlags.Public | BindingFlags.Instance)
                                 .MakeGenericMethod(typeof(SimpleReferenceType));

            Type returnType = typeof(object);

            Type[] parameterTypes = new[] { typeof(object), typeof(object) };
            var    method         = GetWrapperMethodFromEmitter(MethodInfo.GetCurrentMethod(), parameterTypes, returnType, methodInfo);

            var obj   = new ClassWithMethods();
            var value = new SimpleReferenceType();

            Assert.That(BuildTypeAndInvokeMethod(method, obj, value), Is.SameAs(value));
        }
        public void EmitMethodBody_ForStaticMethodWithReferenceTypeParameter()
        {
            Type declaringType = typeof(ClassWithMethods);
            var  methodInfo    = declaringType.GetMethod("StaticMethodWithReferenceTypeParameter", BindingFlags.Public | BindingFlags.Static);

            Type returnType = typeof(void);

            Type[] parameterTypes = new[] { typeof(object), typeof(object) };
            var    method         = GetWrapperMethodFromEmitter(MethodInfo.GetCurrentMethod(), parameterTypes, returnType, methodInfo);

            var value = new SimpleReferenceType();

            BuildTypeAndInvokeMethod(method, new object[] { null, value });

            Assert.That(ClassWithMethods.StaticReferenceTypeValue, Is.SameAs(value));
        }
        public void EmitMethodBody_ForInstanceMethodWithReferenceTypeParameter_ParameterTypesMatch()
        {
            Type declaringType = typeof(ClassWithMethods);
            var  methodInfo    = declaringType.GetMethod("InstanceMethodWithReferenceTypeParameter", BindingFlags.Public | BindingFlags.Instance);

            Type returnType = typeof(void);

            Type[] parameterTypes = new[] { typeof(object), typeof(SimpleReferenceType) };
            var    method         = GetWrapperMethodFromEmitter(MethodInfo.GetCurrentMethod(), parameterTypes, returnType, methodInfo);

            var value = new SimpleReferenceType();
            var obj   = new ClassWithMethods();

            BuildTypeAndInvokeMethod(method, obj, value);

            Assert.That(obj.InstanceReferenceTypeValue, Is.SameAs(value));
        }
Beispiel #17
0
        public void GetValue_WithIndexerProperty_ThreeParameters()
        {
            var scalar       = new SimpleReferenceType();
            var instanceMock = MockRepository.GenerateMock <IInterfaceWithReferenceType <SimpleReferenceType> >();

            instanceMock.Expect(mock => mock[10, new DateTime(2000, 1, 1), "foo"]).Return(scalar);
            instanceMock.Replay();

            var interfaceDeclarationProperty = typeof(IInterfaceWithReferenceType <SimpleReferenceType>)
                                               .GetProperty("Item", new[] { typeof(int), typeof(DateTime), typeof(string) });

            _implicitInterfaceAdapter = PropertyInfoAdapter.Create(interfaceDeclarationProperty);

            object actualScalar = _implicitInterfaceAdapter.GetValue(instanceMock, new object[] { 10, new DateTime(2000, 1, 1), "foo" });

            Assert.That(actualScalar, Is.SameAs(scalar));
            instanceMock.VerifyAllExpectations();
        }
        public void SetValue_PrivateAccessor()
        {
            IPropertyInformation propertyInfo = GetPropertyInfo(typeof(ClassWithReferenceType <SimpleReferenceType>), "PrivateProperty");
            PropertyBase         propertyBase = new StubPropertyBase(
                CreateParameters(
                    propertyInfo: propertyInfo,
                    underlyingType: propertyInfo.PropertyType,
                    concreteType: propertyInfo.PropertyType,
                    listInfo: null,
                    isRequired: true,
                    isReadOnly: true));

            var instance = ObjectFactory.Create <ClassWithReferenceType <SimpleReferenceType> > (ParamList.Empty);

            var value = new SimpleReferenceType();

            propertyBase.SetValue((IBusinessObject)instance, value);

            Assert.That(PrivateInvoke.GetNonPublicProperty(instance, "PrivateProperty"), Is.SameAs(value));
        }
        public void GetValue()
        {
            IPropertyInformation propertyInfo = GetPropertyInfo(typeof(ClassWithReferenceType <SimpleReferenceType>), "Scalar");
            PropertyBase         propertyBase = new StubPropertyBase(
                CreateParameters(
                    propertyInfo: propertyInfo,
                    underlyingType: propertyInfo.PropertyType,
                    concreteType: propertyInfo.PropertyType,
                    listInfo: null,
                    isRequired: true,
                    isReadOnly: true));

            var instance = ObjectFactory.Create <ClassWithReferenceType <SimpleReferenceType> > (ParamList.Empty);

            var value = new SimpleReferenceType();

            instance.Scalar = value;

            Assert.That(propertyBase.GetValue(((IBusinessObject)instance)), Is.SameAs(value));
        }
        public void EmitMethodBody_ForInstanceMethodWithMultipleParameters()
        {
            Type declaringType = typeof(ClassWithMethods);
            var  methodInfo    = declaringType.GetMethod("InstanceMethodWithMultipleParameters", BindingFlags.Public | BindingFlags.Instance);

            Type returnType = typeof(void);

            Type[] parameterTypes = new[] { typeof(object), typeof(object), typeof(object), typeof(object), typeof(object) };
            var    method         = GetWrapperMethodFromEmitter(MethodInfo.GetCurrentMethod(), parameterTypes, returnType, methodInfo);

            var instanceReferenceTypeValue     = new SimpleReferenceType();
            int instanceValueTypeValue         = 100;
            int?instanceNullableValueTypeValue = 200;
            var staticReferenceTypeValue       = new SimpleReferenceType();
            var obj = new ClassWithMethods();

            BuildTypeAndInvokeMethod(
                method, obj, instanceReferenceTypeValue, instanceValueTypeValue, instanceNullableValueTypeValue, staticReferenceTypeValue);

            Assert.That(obj.InstanceReferenceTypeValue, Is.SameAs(instanceReferenceTypeValue));
            Assert.That(obj.InstanceValueTypeValue, Is.EqualTo(instanceValueTypeValue));
            Assert.That(obj.InstanceNullableValueTypeValue, Is.EqualTo(instanceNullableValueTypeValue));
            Assert.That(ClassWithMethods.StaticReferenceTypeValue, Is.SameAs(staticReferenceTypeValue));
        }
        public void EmitMethodBody_ForPrivatePropertyGetter_ForReferenceType()
        {
            Type declaringType = typeof(ClassWithReferenceTypeProperties);
            var  propertyInfo  = declaringType.GetProperty("PropertyWithPrivateGetterAndSetter", BindingFlags.NonPublic | BindingFlags.Instance);
            var  methodInfo    = propertyInfo.GetGetMethod(true);

            Type returnType = typeof(object);

            Type[] parameterTypes = new[] { typeof(object) };
            var    dynamicMethod  = new DynamicMethod("", returnType, parameterTypes, declaringType, false);
            var    ilGenerator    = dynamicMethod.GetILGenerator();

            var emitter = new MethodWrapperEmitter(ilGenerator, methodInfo, parameterTypes, returnType);

            emitter.EmitStaticMethodBody();

            var propertyGetter = (Func <object, object>)dynamicMethod.CreateDelegate(typeof(Func <object, object>));

            var expectedValue = new SimpleReferenceType();
            var obj           = new ClassWithReferenceTypeProperties();

            obj.SetPropertyWithPrivateGetterAndSetter(expectedValue);
            Assert.That(propertyGetter(obj), Is.SameAs(expectedValue));
        }
 public void InstanceMethodWithOptionalParameter([Optional] SimpleReferenceType value)
 {
 }
 public void InstanceMethodWithByRefParameter(ref SimpleReferenceType value)
 {
     value = InstanceReferenceTypeValue;
 }
 public void InstanceMethodWithOutParameter(out SimpleReferenceType value)
 {
     value = InstanceReferenceTypeValue;
 }
Beispiel #25
0
 public void SetPropertyWithPrivateGetterAndSetter(SimpleReferenceType value)
 {
     PropertyWithPrivateGetterAndSetter = value;
 }
 public void InstanceMethodWithReferenceTypeParameter(SimpleReferenceType value)
 {
     InstanceReferenceTypeValue = value;
 }
 public static void StaticMethodWithReferenceTypeParameter(SimpleReferenceType value)
 {
     StaticReferenceTypeValue = value;
 }
Beispiel #28
0
 private void AssertCanSet(PropertyInfoAdapter adapter, object instance, SimpleReferenceType value)
 {
     adapter.SetValue(instance, value, null);
     Assert.That(adapter.GetValue(instance, null), Is.SameAs(value));
 }