Example #1
0
        protected override void OnGetValue(PropertySpecEventArgs e)
        {
            base.OnGetValue(e);

            var attributeList = new List <Attribute>();

            attributeList.AddRange(e.Property.Attributes.ToList());

            //check all of the attributes: if we find a dynamic one, evaluate it and possibly add/overwrite a static attribute
            foreach (Attribute customAttribute in e.Property.Attributes)
            {
                if (customAttribute is DynamicReadOnlyAttribute)
                {
                    attributeList.RemoveAll(x => x is ReadOnlyAttribute);

                    if (DynamicReadOnlyAttribute.IsDynamicReadOnly(propertyObject, e.Property.Name))
                    {
                        //condition is true: the dynamic attribute should be applied (as static attribute)
                        attributeList.Add(new ReadOnlyAttribute(true)); //add static read only attribute
                    }
                }
            }

            e.Property.Attributes = attributeList.ToArray();

            var propertyInfo = propertyObject.GetType().GetProperty(e.Property.Name);
            var value        = propertyInfo.GetValue(propertyObject, null);

            var isNestedPropertiesObject = IsNestedExpandablePropertiesObject(propertyInfo);

            // if nested properties object, wrap in DynamicPropertyBag to provide support for things like DynamicReadOnly
            e.Value = isNestedPropertiesObject ? new DynamicPropertyBag(value) : value;
        }
        public void IsReadOnly_NoPropertyName_ReturnFalse(string propertyName)
        {
            // Call
            bool isReadOnly = DynamicReadOnlyAttribute.IsReadOnly(new object(), propertyName);

            // Assert
            Assert.IsFalse(isReadOnly);
        }
        public void DefaultConstructor_ExpectedValues()
        {
            // Call
            var attribute = new DynamicReadOnlyAttribute();

            // Assert
            Assert.IsInstanceOf <Attribute>(attribute);
        }
        public void IsReadOnly_GivenPropertyDoesNotHaveDynamicReadOnlyAttribute_ReturnFalse()
        {
            // Setup
            var o = new ClassWithPropertyWithoutDynamicReadOnlyAttribute();

            // Call
            bool isReadOnly = DynamicReadOnlyAttribute.IsReadOnly(o, "Property");

            // Assert
            Assert.IsFalse(isReadOnly);
        }
        public void IsReadOnly_ClassWithDynamicReadOnlyProperty_ReturnResultFromValidationMethod(bool isReadOnly)
        {
            // Setup
            var o = new ClassWithDynamicReadOnlyProperty(isReadOnly);

            // Call
            bool result = DynamicReadOnlyAttribute.IsReadOnly(o, "Property");

            // Assert
            Assert.AreEqual(isReadOnly, result);
        }
        public void IsReadOnly_GivenPropertyNameDoesNotExistOnObject_ThrowMissingMemberException()
        {
            // Setup
            var o = new object();

            // Call
            TestDelegate call = () => DynamicReadOnlyAttribute.IsReadOnly(o, "NotExistingProperty");

            // Assert
            string exceptionMessage = Assert.Throws <MissingMemberException>(call).Message;

            Assert.AreEqual($"Kon eigenschap NotExistingProperty van type {o.GetType()} niet vinden.", exceptionMessage);
        }
        public void IsReadOnly_ClassHasMultipleDynamicReadOnlyValidationMethods_ThrowsMissingMethodException()
        {
            // Setup
            var o = new InvalidClassWithDynamicReadOnlyPropertyAndMultipleValidationMethods();

            // Call
            TestDelegate call = () => DynamicReadOnlyAttribute.IsReadOnly(o, "Property");

            // Assert
            string exceptionMessage = Assert.Throws <MissingMethodException>(call).Message;
            string expectedMessage  = $"Slechts één DynamicReadOnlyValidationMethod toegestaan per klasse: {o.GetType()}.";

            Assert.AreEqual(expectedMessage, exceptionMessage);
        }
        public void IsReadOnly_ClassLacksDynamicReadOnlyValidationMethod_ThrowsMissingMethodException()
        {
            // Setup
            var o = new InvalidClassWithDynamicReadOnlyPropertyButNoValidationMethod();

            // Call
            TestDelegate call = () => DynamicReadOnlyAttribute.IsReadOnly(o, "Property");

            // Assert
            string exceptionMessage = Assert.Throws <MissingMethodException>(call).Message;
            string expectedMessage  = $"DynamicReadOnlyValidationMethod niet gevonden (of geen 'public' toegankelijkheid). Klasse: {o.GetType()}.";

            Assert.AreEqual(expectedMessage, exceptionMessage);
        }
        public void IsReadOnly_ClassHasDynamicReadOnlyValidationMethodWithIncorrectArgumentType_ThrowsMissingMethodException()
        {
            // Setup
            var o = new InvalidClassWithDynamicReadOnlyPropertyButValidationMethodArgumentNotString();

            // Call
            TestDelegate call = () => DynamicReadOnlyAttribute.IsReadOnly(o, "Property");

            // Assert
            string exceptionMessage = Assert.Throws <MissingMethodException>(call).Message;
            string expectedMessage  = $"Argument van DynamicReadOnlyValidationMethod moet van het type 'string' zijn. Klasse: {o.GetType()}.";

            Assert.AreEqual(expectedMessage, exceptionMessage);
        }
        public void IsReadOnly_ClassHasDynamicReadOnlyValidationMethodWithIncorrectArgumentCount_ThrowsMissingMethodException()
        {
            // Setup
            var o = new InvalidClassWithDynamicReadOnlyPropertyButValidationMethodNotOneArgument();

            // Call
            TestDelegate call = () => DynamicReadOnlyAttribute.IsReadOnly(o, "Property");

            // Assert
            string exceptionMessage = Assert.Throws <MissingMethodException>(call).Message;
            string expectedMessage  = $"DynamicReadOnlyValidationMethod heeft een incorrect aantal argumenten. Zou er één moeten zijn. Klasse: {o.GetType()}.";

            Assert.AreEqual(expectedMessage, exceptionMessage);
        }
        public void IsReadOnly_ClassHasDynamicReadOnlyValidationMethodWithNonBoolReturnType_ThrowsMissingMethodException()
        {
            // Setup
            var o = new InvalidClassWithDynamicReadOnlyPropertyButValidationMethodReturnsIncorrectValueType();

            // Call
            TestDelegate call = () => DynamicReadOnlyAttribute.IsReadOnly(o, "Property");

            // Assert
            string exceptionMessage = Assert.Throws <MissingMethodException>(call).Message;
            string expectedMessage  = $"DynamicReadOnlyValidationMethod moet 'bool' als 'return type' hebben. Klasse: {o.GetType()}.";

            Assert.AreEqual(expectedMessage, exceptionMessage);
        }