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())); }
public void NullTemplate_ShouldThrow() { // arrange var sut = new DynamicBuilder <TestClass>(); // act Action overwriteAll = () => sut.OverwriteWithDelegate(null); // assert overwriteAll.Should().Throw <ArgumentNullException>(); }
public void InvalidName_ShouldThrow(string invalidName) { // arrange var sut = new DynamicBuilder <SomeStruct>(); // act Action getOverwrittenValue = () => sut.GetOverwrittenValue(invalidName); // assert getOverwrittenValue.Should().Throw <ArgumentNullException>(); }
public void NotOverwritten_ShouldReturnDefaultValueOfPropertyType() { // arrange var sut = new DynamicBuilder <SomeClass>(); // act object result = sut.GetOverwrittenValue(nameof(SomeClass.Int32Property)); // assert result.Should().Be(default(int)); }
public void UnknownName_ShouldThrow() { // arrange var sut = new DynamicBuilder <SomeClass>(); // act Action getOverwrittenValue = () => sut.GetOverwrittenValue("UnknownProperty"); // assert getOverwrittenValue.Should().Throw <InvalidOperationException>(); }
public void NotOverwritten_ShouldReturnFalse() { // arrange var sut = new DynamicBuilder <SomeStruct>(); // act bool result = sut.IsOverwritten(nameof(SomeStruct.Int32Property)); // assert result.Should().BeFalse(); }
public void NullBuildChildAction_ShouldThrow() { // arrange IDynamicBuilder <TestParentClass> builder = new DynamicBuilder <TestParentClass>(); // act Action withChild = () => DynamicBuilderExtensions.WithChild(builder, p => p.Child, null); // assert withChild.Should().Throw <ArgumentNullException>(); }
public void NullPropertyFunc_ShouldThrow() { // arrange IDynamicBuilder <TestParentClass> builder = new DynamicBuilder <TestParentClass>(); // act Action withChild = () => DynamicBuilderExtensions.WithChild <TestParentClass, TestChildClass>(builder, null, childBuilder => childBuilder.WithValue(c => c.Int32Property, 1)); // assert withChild.Should().Throw <ArgumentNullException>(); }
public void Overwritten_ShouldReturnTrue() { // arrange var sut = new DynamicBuilder <SomeClass>(); sut.Overwrite(nameof(SomeClass.Int32Property), 1); // act bool result = sut.IsOverwritten(nameof(SomeClass.Int32Property)); // assert result.Should().BeTrue(); }
public void Overwritten_ShouldReturnOverwrittenValue() { // arrange var sut = new DynamicBuilder <SomeClass>(); int expected = 1; sut.Overwrite(nameof(SomeClass.Int32Property), expected); // act object result = sut.GetOverwrittenValue(nameof(SomeClass.Int32Property)); // assert result.Should().Be(expected); }
public void ShouldCallPropertySetterForAllExpectedProperties() { // arrange var propertySetterMock = new Mock <IPropertySetter>(); var sut = new DynamicBuilder <TestClass>( propertySetter: propertySetterMock.Object); int expectedValue = 1; 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 => typeof(TestClass).GetProperty(propName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)); // act sut.OverwriteWithDelegate(ctx => { if (ctx.PropertyType == typeof(int)) { ctx.SetValue(expectedValue); } }); 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())); }
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); }
public void ShouldCallPropertySetterForGivenProperty(string propertyName, object expectedValue) { // arrange var expectedInstance = new TestClass(); var instanceFactoryMock = new Mock <IInstanceFactory <TestClass> >(); instanceFactoryMock .Setup(e => e.Create(It.IsAny <IEnumerable <INamedPropertyOverwriter> >())) .Returns(expectedInstance); var propertySetterMock = new Mock <IPropertySetter>(); var sut = new DynamicBuilder <TestClass>( instanceFactory: instanceFactoryMock.Object, propertySetter: propertySetterMock.Object); var expectedProperty = expectedInstance.GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); // act sut.Overwrite(propertyName, expectedValue); sut.Build(); // assert propertySetterMock .Verify(e => e.SetProperty(expectedInstance, expectedProperty, expectedValue), Times.Once()); }