Beispiel #1
0
        private PropertyDescriptor[] OrderPropertyDescriptors(IEnumerable <PropertyDescriptor> propertyDescriptorsToReturn)
        {
            var unorderedProperties    = new List <PropertyDescriptor>();
            var propertiesWithOrdering = new List <Tuple <int, PropertyDescriptor> >();

            foreach (PropertyDescriptor pd in propertyDescriptorsToReturn)
            {
                PropertyOrderAttribute propertyOrderAttribute = pd.Attributes.OfType <PropertyOrderAttribute>().FirstOrDefault();
                if (propertyOrderAttribute != null)
                {
                    propertiesWithOrdering.Add(Tuple.Create(propertyOrderAttribute.Order, pd));
                    continue;
                }

                DynamicPropertyOrderAttribute dynamicPropertyOrderAttribute = pd.Attributes.OfType <DynamicPropertyOrderAttribute>().FirstOrDefault();
                if (dynamicPropertyOrderAttribute != null)
                {
                    propertiesWithOrdering.Add(Tuple.Create(DynamicPropertyOrderAttribute.PropertyOrder(WrappedObject, pd.Name), pd));
                    continue;
                }

                unorderedProperties.Add(pd);
            }

            IEnumerable <PropertyDescriptor> orderedProperties = propertiesWithOrdering.OrderBy(p => p.Item1).Select(p => p.Item2);

            return(orderedProperties.Concat(unorderedProperties).ToArray());
        }
Beispiel #2
0
        public void PropertyOrder_NoPropertyName_ReturnZero(string propertyName)
        {
            // Call
            int propertyOrder = DynamicPropertyOrderAttribute.PropertyOrder(new object(), propertyName);

            // Assert
            Assert.AreEqual(0.0, propertyOrder);
        }
Beispiel #3
0
        public void PropertyOrder_GivenPropertyDoesNotHaveDynamicPropertyOrderAttribute_ReturnZero()
        {
            // Setup
            var o = new ClassWithPropertyWithoutDynamicPropertyOrderAttribute();

            // Call
            int propertyOrder = DynamicPropertyOrderAttribute.PropertyOrder(o, "Property");

            // Assert
            Assert.AreEqual(0, propertyOrder);
        }
Beispiel #4
0
        public void PropertyOrder_ClassWithDynamicPropertyOrderProperty_ReturnResultFromEvaluationMethod(int propertyOrder)
        {
            // Setup
            var o = new ClassWithDynamicPropertyOrderProperty(propertyOrder);

            // Call
            int result = DynamicPropertyOrderAttribute.PropertyOrder(o, "Property");

            // Assert
            Assert.AreEqual(propertyOrder, result);
        }
Beispiel #5
0
        public void PropertyOrder_GivenPropertyNameDoesNotExistOnObject_ThrowMissingMemberException()
        {
            // Setup
            var o = new object();

            // Call
            TestDelegate call = () => DynamicPropertyOrderAttribute.PropertyOrder(o, "NotExistingProperty");

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

            Assert.AreEqual($"Kon eigenschap NotExistingProperty van type {o.GetType()} niet vinden.", exceptionMessage);
        }
Beispiel #6
0
        public void PropertyOrder_ClassHasMultipleDynamicPropertyOrderEvaluationMethods_ThrowsMissingMethodException()
        {
            // Setup
            var o = new InvalidClassWithDynamicPropertyOrderPropertyAndMultipleEvaluationMethods();

            // Call
            TestDelegate call = () => DynamicPropertyOrderAttribute.PropertyOrder(o, "Property");

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

            Assert.AreEqual(expectedMessage, exceptionMessage);
        }
Beispiel #7
0
        public void PropertyOrder_ClassLacksDynamicPropertyOrderValidationMethod_ThrowsMissingMethodException()
        {
            // Setup
            var o = new InvalidClassWithDynamicPropertyOrderPropertyButNoEvaluationMethod();

            // Call
            TestDelegate call = () => DynamicPropertyOrderAttribute.PropertyOrder(o, "Property");

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

            Assert.AreEqual(expectedMessage, exceptionMessage);
        }
Beispiel #8
0
        public void PropertyOrder_ClassHasDynamicPropertyOrderEvaluationMethodWithIncorrectArgumentType_ThrowsMissingMethodException()
        {
            // Setup
            var o = new InvalidClassWithDynamicPropertyOrderPropertyButEvaluationMethodArgumentNotString();

            // Call
            TestDelegate call = () => DynamicPropertyOrderAttribute.PropertyOrder(o, "Property");

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

            Assert.AreEqual(expectedMessage, exceptionMessage);
        }
Beispiel #9
0
        public void PropertyOrder_ClassHasDynamicPropertyOrderEvaluationMethodWithIncorrectArgumentCount_ThrowsMissingMethodException()
        {
            // Setup
            var o = new InvalidClassWithDynamicPropertyOrderPropertyButEvaluationMethodNotOneArgument();

            // Call
            TestDelegate call = () => DynamicPropertyOrderAttribute.PropertyOrder(o, "Property");

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

            Assert.AreEqual(expectedMessage, exceptionMessage);
        }
Beispiel #10
0
        public void PropertyOrder_ClassHasDynamicPropertyOrderEvaluationMethodWithNonIntReturnType_ThrowsMissingMethodException()
        {
            // Setup
            var o = new InvalidClassWithDynamicPropertyOrderPropertyButEvaluationMethodReturnsIncorrectValueType();

            // Call
            TestDelegate call = () => DynamicPropertyOrderAttribute.PropertyOrder(o, "Property");

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

            Assert.AreEqual(expectedMessage, exceptionMessage);
        }