Пример #1
0
        public void Activate_SetsPropertyValue()
        {
            // Arrange
            var instance  = new TestClass();
            var typeInfo  = instance.GetType().GetTypeInfo();
            var property  = typeInfo.GetDeclaredProperty("IntProperty");
            var activator = new PropertyActivator <int>(property, valueAccessor: (val) => val + 1);

            // Act
            activator.Activate(instance, 123);

            // Assert
            Assert.Equal(124, instance.IntProperty);
        }
Пример #2
0
        public void GetPropertiesToActivate_IncludesNonPublic()
        {
            // Arrange
            var instance = new TestClassWithPropertyVisiblity();
            var typeInfo = instance.GetType().GetTypeInfo();

            // Act
            var propertiesToActivate = PropertyActivator <int> .GetPropertiesToActivate(
                typeof(TestClassWithPropertyVisiblity),
                typeof(TestActivateAttribute),
                (propertyInfo) => new PropertyActivator <int>(propertyInfo, valueAccessor : (val) => val),
                includeNonPublic : true);

            // Assert
            Assert.Equal(5, propertiesToActivate.Length);
        }
Пример #3
0
        public void GetPropertiesToActivate_ExcludesNonPublic()
        {
            // Arrange
            var instance             = new TestClassWithPropertyVisiblity();
            var typeInfo             = instance.GetType().GetTypeInfo();
            var expectedPropertyInfo = typeInfo.GetDeclaredProperty("Public");

            // Act
            var propertiesToActivate = PropertyActivator <int> .GetPropertiesToActivate(
                typeof(TestClassWithPropertyVisiblity),
                typeof(TestActivateAttribute),
                (propertyInfo) => new PropertyActivator <int>(propertyInfo, valueAccessor : (val) => val));

            // Assert
            Assert.Equal(1, propertiesToActivate.Length);
            Assert.Single(propertiesToActivate, p => p.PropertyInfo == expectedPropertyInfo);
        }
Пример #4
0
        public void Activate_InvokesValueAccessorWithExpectedValue()
        {
            // Arrange
            var instance    = new TestClass();
            var typeInfo    = instance.GetType().GetTypeInfo();
            var property    = typeInfo.GetDeclaredProperty("IntProperty");
            var invokedWith = -1;
            var activator   = new PropertyActivator <int>(
                property,
                valueAccessor: (val) =>
            {
                invokedWith = val;
                return(val);
            });

            // Act
            activator.Activate(instance, 123);

            // Assert
            Assert.Equal(123, invokedWith);
        }
Пример #5
0
        public void GetPropertiesToActivate_CanCreateCustomPropertyActivators()
        {
            // Arrange
            var instance             = new TestClass();
            var typeInfo             = instance.GetType().GetTypeInfo();
            var expectedPropertyInfo = typeInfo.GetDeclaredProperty("IntProperty");

            // Act
            var propertiesToActivate = PropertyActivator <int> .GetPropertiesToActivate(
                type : typeof(TestClass),
                activateAttributeType : typeof(TestActivateAttribute),
                createActivateInfo :
                (propertyInfo) => new PropertyActivator <int>(expectedPropertyInfo, valueAccessor: (val) => val + 1));

            // Assert
            Assert.Collection(
                propertiesToActivate,
                (activator) =>
            {
                Assert.Equal(expectedPropertyInfo, activator.PropertyInfo);
            });
        }