Esempio n. 1
0
        public void BuildsNewInstanceFromExisting()
        {
            var obj    = new ClassWithProperties();
            var result = obj.NewInstance();

            Assert.Equal(obj.GetType(), result.GetType());
        }
Esempio n. 2
0
        public void SetValue_ProperInstanceTypeAndValueType_PropertyIsUpdated()
        {
            // Setup
            var target = new ClassWithProperties();

            var propertySpec = new PropertySpec(target.GetType().GetProperty("IntegerProperty"));

            // Call
            propertySpec.SetValue(target, 2);

            // Assert
            Assert.AreEqual(2, target.IntegerProperty);
        }
        public void ShouldSerializeValue_ReturnFalse()
        {
            // Setup
            var instance           = new ClassWithProperties();
            var spec               = new PropertySpec(instance.GetType().GetProperty("PropertyWithOnlyGetter"));
            var propertyDescriptor = new PropertySpecDescriptor(spec, instance);

            // Call
            bool shouldSerializeValue = propertyDescriptor.ShouldSerializeValue(instance);

            // Assert
            Assert.IsFalse(shouldSerializeValue);
        }
        public void CanResetValue_ReturnFalse()
        {
            // Setup
            var instance           = new ClassWithProperties();
            var spec               = new PropertySpec(instance.GetType().GetProperty("PropertyWithOnlyGetter"));
            var propertyDescriptor = new PropertySpecDescriptor(spec, instance);

            // Call
            bool canReset = propertyDescriptor.CanResetValue(instance);

            // Assert
            Assert.IsFalse(canReset);
        }
        public void GetValue_ObjectValueProperty_ReturnPropertyValue()
        {
            // Setup
            var instance           = new ClassWithProperties();
            var spec               = new PropertySpec(instance.GetType().GetProperty("ComplexSubProperty"));
            var propertyDescriptor = new PropertySpecDescriptor(spec, instance);

            // Call
            object value = propertyDescriptor.GetValue(instance);

            // Assert
            Assert.AreSame(instance.ComplexSubProperty, value);
        }
Esempio n. 6
0
        public void SetValue_InstanceIsNull_ThrowTargetException()
        {
            // Setup
            var target = new ClassWithProperties();

            var propertySpec = new PropertySpec(target.GetType().GetProperty("IntegerProperty"));

            // Call
            TestDelegate call = () => propertySpec.SetValue(null, 2);

            // Assert
            Assert.Throws <TargetException>(call);
        }
Esempio n. 7
0
        public void IsNonCustomExpandableObjectProperty_PropertyWithSomeTypeConverter_ReturnFalse()
        {
            // Setup
            var target = new ClassWithProperties();

            var propertySpec = new PropertySpec(target.GetType().GetProperty("StringPropertyWithSomeTypeConverter"));

            // Call
            bool hasExpandableObjectTypeConverter = propertySpec.IsNonCustomExpandableObjectProperty();

            // Assert
            Assert.False(hasExpandableObjectTypeConverter);
        }
        public void IsReadOnly_PropertyHasNoAttributeAndOnlyGetter_ReturnTrue()
        {
            // Setup
            var instance   = new ClassWithProperties();
            var spec       = new PropertySpec(instance.GetType().GetProperty("PropertyWithOnlyGetter"));
            var descriptor = new PropertySpecDescriptor(spec, instance);

            // Call
            bool isReadOnly = descriptor.IsReadOnly;

            // Assert
            Assert.IsTrue(isReadOnly);
        }
        public void IsReadOnly_PropertyHasReadOnlyFalseAttribute_ReturnFalse()
        {
            // Setup
            var instance   = new ClassWithProperties();
            var spec       = new PropertySpec(instance.GetType().GetProperty("PropertyWithReadOnlyFalseAttribute"));
            var descriptor = new PropertySpecDescriptor(spec, instance);

            // Call
            bool isReadOnly = descriptor.IsReadOnly;

            // Assert
            Assert.IsFalse(isReadOnly);
        }
        public void GetValue_SimpleValueProperty_ReturnPropertyValue()
        {
            // Setup
            var instance           = new ClassWithProperties();
            var spec               = new PropertySpec(instance.GetType().GetProperty("PropertyWithOnlyGetter"));
            var propertyDescriptor = new PropertySpecDescriptor(spec, instance);

            // Call
            object value = propertyDescriptor.GetValue(instance);

            // Assert
            Assert.AreEqual(instance.PropertyWithOnlyGetter, value);
        }
        public void IsBrowsable_PropertyHasBrowsableFalseAttribute_ReturnFalse()
        {
            // Setup
            var instance   = new ClassWithProperties();
            var spec       = new PropertySpec(instance.GetType().GetProperty("PropertyWithBrowsableFalseAttribute"));
            var descriptor = new PropertySpecDescriptor(spec, instance);

            // Call
            bool isBrowsable = descriptor.IsBrowsable;

            // Assert
            Assert.IsFalse(isBrowsable);
        }
Esempio n. 12
0
        public void IsNonCustomExpandableObjectProperty_PropertyWithCustomExpandableObjectTypeConverter_ReturnFalse()
        {
            // Setup
            var target = new ClassWithProperties();

            var propertySpec = new PropertySpec(target.GetType().GetProperty("StringPropertyWithCustomExpandableObjectConverter"));

            // Call
            bool hasExpandableObjectTypeConverter = propertySpec.IsNonCustomExpandableObjectProperty();

            // Assert
            Assert.False(hasExpandableObjectTypeConverter,
                         "As we cannot copy the same behavior of a ExpandableObjectConverter with customizations, we should not recognize it as such.");
        }
Esempio n. 13
0
        public void SetValue_PropertyWithoutPublicSet_ThrowInvalidOperationException()
        {
            // Setup
            var target = new ClassWithProperties();

            var propertySpec = new PropertySpec(target.GetType().GetProperty("DoublePropertyWithOnlyGetter"));

            // Call
            TestDelegate call = () => propertySpec.SetValue(target, 2);

            // Assert
            var exception = Assert.Throws <InvalidOperationException>(call);

            Assert.AreEqual("Property lacks public setter!", exception.Message);
        }
Esempio n. 14
0
        public void GetValue_InstanceIsNull_ThrowArgumentException()
        {
            // Setup
            var target = new ClassWithProperties();

            var propertySpec = new PropertySpec(target.GetType().GetProperty("IntegerProperty"));

            // Call
            TestDelegate call = () => propertySpec.GetValue(null);

            // Assert
            var exception = Assert.Throws <ArgumentException>(call);

            Assert.IsInstanceOf <TargetException>(exception.InnerException);
        }
Esempio n. 15
0
        public void GetValue_PropertyHasNoPublicGetter_ThrowInvalidOperationException()
        {
            // Setup
            var target = new ClassWithProperties();

            var propertySpec = new PropertySpec(target.GetType().GetProperty("DoublePropertyWithOnlyPublicSet"));

            // Call
            TestDelegate call = () => propertySpec.GetValue(target);

            // Assert
            string message = Assert.Throws <InvalidOperationException>(call).Message;

            Assert.AreEqual("Property lacks public getter!", message);
        }
Esempio n. 16
0
        public void SetValue_SettingValueOfIncorrectType_ThrowArgumentException()
        {
            // Setup
            var target = new ClassWithProperties();

            var propertySpec = new PropertySpec(target.GetType().GetProperty("IntegerProperty"));

            // Call
            TestDelegate call = () => propertySpec.SetValue(target, new object());

            // Assert
            var exception = Assert.Throws <ArgumentException>(call);

            Assert.IsNull(exception.InnerException);
        }
Esempio n. 17
0
        public void SetValue_SettingValueResultsInException_ThrowTargetInvocationException()
        {
            // Setup
            var target = new ClassWithProperties();

            var propertySpec = new PropertySpec(target.GetType().GetProperty("ThrowsException"));

            // Call
            TestDelegate call = () => propertySpec.SetValue(target, "");

            // Assert
            Exception innerException = Assert.Throws <TargetInvocationException>(call).InnerException;

            Assert.IsInstanceOf <ArgumentException>(innerException);
        }
        public void IsBrowsable_PropertyHasDynamicBrowsableProperty_ReturnExpectedValue(bool isPropertyVisible)
        {
            // Setup
            var instance = new ClassWithProperties
            {
                IsPropertyBrowsable = isPropertyVisible
            };
            var propertySpec = new PropertySpec(instance.GetType().GetProperty("IntegerPropertyWithDynamicVisibility"));
            var descriptor   = new PropertySpecDescriptor(propertySpec, instance);

            // Call
            bool isBrowsable = descriptor.IsBrowsable;

            // Assert
            Assert.AreEqual(isPropertyVisible, isBrowsable);
        }
Esempio n. 19
0
        public void GetValue_ProperInstanceType_ReturnPropertyValue()
        {
            // Setup
            var target = new ClassWithProperties
            {
                IntegerProperty = 5
            };

            var propertySpec = new PropertySpec(target.GetType().GetProperty("IntegerProperty"));

            // Call
            object value = propertySpec.GetValue(target);

            // Assert
            Assert.AreEqual(target.IntegerProperty, value);
        }
        public void IsReadOnly_PropertyHasDynamicReadOnlyProperty_ReturnExpectedValue(bool isPropertyReadOnly)
        {
            // Setup
            var instance = new ClassWithProperties
            {
                IsPropertyReadOnly = isPropertyReadOnly
            };
            var propertySpec = new PropertySpec(instance.GetType().GetProperty("IntegerPropertyWithDynamicReadOnly"));
            var descriptor   = new PropertySpecDescriptor(propertySpec, instance);

            // Call
            bool isReadOnly = descriptor.IsReadOnly;

            // Assert
            Assert.AreEqual(isPropertyReadOnly, isReadOnly);
        }
        public void ParameteredConstructor_IsPropertyReadOnlyProperty_ExpectedValues()
        {
            // Setup
            var          instance     = new ClassWithProperties();
            PropertyInfo propertyInfo = instance.GetType().GetProperty("IsPropertyReadOnly");
            var          spec         = new PropertySpec(propertyInfo);

            // Call
            var propertyDescriptor = new PropertySpecDescriptor(spec, instance);

            // Assert
            Assert.AreEqual(spec.GetType(), propertyDescriptor.ComponentType);
            Assert.IsFalse(propertyDescriptor.IsReadOnly);
            Assert.IsTrue(propertyDescriptor.IsBrowsable);
            Assert.AreEqual(propertyInfo.PropertyType, propertyDescriptor.PropertyType);
        }
        public void GetValue_ObjectValuePropertyWithExpandableObjectConverterAttribute_ReturnPropertyValueWrappedInDynamicPropertyBag()
        {
            // Setup
            var instance           = new ClassWithProperties();
            var spec               = new PropertySpec(instance.GetType().GetProperty("ComplexSubPropertyWithExandableObjectConverter"));
            var propertyDescriptor = new PropertySpecDescriptor(spec, instance);

            // Call
            object value = propertyDescriptor.GetValue(instance);

            // Assert
            var dynamicPropertyBag = (DynamicPropertyBag)value;

            Assert.IsNotNull(dynamicPropertyBag);
            Assert.AreSame(instance.ComplexSubPropertyWithExandableObjectConverter, dynamicPropertyBag.WrappedObject);
        }
        public void ResetValue_CanNotReset_ThrowsInvalidOperationException()
        {
            // Setup
            var instance           = new ClassWithProperties();
            var spec               = new PropertySpec(instance.GetType().GetProperty("PropertyWithOnlyGetter"));
            var propertyDescriptor = new PropertySpecDescriptor(spec, instance);

            // Precondition
            Assert.IsFalse(propertyDescriptor.CanResetValue(instance));

            // Call
            TestDelegate call = () => propertyDescriptor.ResetValue(instance);

            // Assert
            Assert.Throws <InvalidOperationException>(call);
        }