public void ShouldCallRightPropertySetterForAmbiguousProperties()
            {
                // arrange
                var propertySetterMock = new Mock <IPropertySetter>();
                var sut = new DynamicBuilder <SmallTestClass>(
                    propertySetter: propertySetterMock.Object);

                int expectedValue = 1;
                var template      = new SmallTestClassTemplate
                {
                    someProperty = expectedValue,
                    someproperty = expectedValue + 1
                };
                var expectedProperties = new[]
                {
                    nameof(SmallTestClass.SomeProperty)
                }
                .Select(propName => typeof(SmallTestClass).GetProperty(propName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic));

                // act
                sut.OverwriteWithTemplate(template);
                sut.Build();

                // assert
                foreach (var expectedProperty in expectedProperties)
                {
                    propertySetterMock.Verify(e => e.SetProperty(It.IsAny <object>(), expectedProperty, expectedValue), Times.Once());
                }
                propertySetterMock.Verify(e => e.SetProperty(It.IsAny <object>(), It.IsAny <PropertyInfo>(), It.IsAny <object>()), Times.Exactly(expectedProperties.Count()));
            }
Пример #2
0
            public void NullTemplate_ShouldThrow()
            {
                // arrange
                var sut = new DynamicBuilder <TestClass>();

                // act
                Action overwriteAll = () => sut.OverwriteWithTemplate(null);

                // assert
                overwriteAll.Should().Throw <ArgumentNullException>();
            }
Пример #3
0
                public void ShouldBuildObjectWithOverwrittenProperties()
                {
                    // arrange
                    var template = new SomeStruct(
                        intProperty: 1,
                        stringProperty: "some text");
                    int    expectedIntProperty    = template.IntProperty;
                    string expectedStringProperty = "other text";

                    var sut = new DynamicBuilder <SomeStruct>();

                    sut.OverwriteWithTemplate(template);
                    sut.Overwrite(nameof(SomeStruct.StringProperty), expectedStringProperty);

                    // act
                    var result = sut.Build();

                    // assert
                    result.IntProperty.Should().Be(expectedIntProperty);
                    result.StringProperty.Should().Be(expectedStringProperty);
                }
Пример #4
0
            public void ShouldCallPropertySetterForAllExpectedProperties()
            {
                // arrange
                var propertySetterMock = new Mock <IPropertySetter>();
                var sut = new DynamicBuilder <TestClass>(
                    propertySetter: propertySetterMock.Object);

                int expectedValue      = 1;
                var template           = new TestClass(expectedValue);
                var expectedProperties = new[]
                {
                    nameof(TestClass.PublicGetPublicSet),
                    nameof(TestClass.PublicGetInternalSet),
                    nameof(TestClass.PublicGetProtectedSet),
                    nameof(TestClass.PublicGetPrivateSet),
                    nameof(TestClass.PublicReadOnlyAutoProperty),
                    nameof(TestClass.PublicGetOfPrivateField),
                    nameof(TestClass.InternalGetInternalSet),
                    nameof(TestClass.InternalGetPrivateSet),
                    nameof(TestClass.ProtectedInternalGetProtectedInternalSet),
                    nameof(TestClass.ProtectedInternalGetInternalSet),
                    nameof(TestClass.ProtectedInternalGetPrivateSet)
                }
                .Select(propName => template.GetType().GetProperty(propName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic));

                // act
                sut.OverwriteWithTemplate(template);
                sut.Build();

                // assert
                foreach (var expectedProperty in expectedProperties)
                {
                    propertySetterMock.Verify(e => e.SetProperty(It.IsAny <object>(), expectedProperty, expectedValue), Times.Once());
                }
                propertySetterMock.Verify(e => e.SetProperty(It.IsAny <object>(), It.IsAny <PropertyInfo>(), It.IsAny <object>()), Times.Exactly(expectedProperties.Count()));
            }