Example #1
0
        public void Build_OneDefinitionInput_OneInfoOutput()
        {
            var          sourceTestData     = new SourceTestData();
            var          targetTestData     = new TargetTestData();
            const string sourcePropertyName = nameof(SourceTestData.SourceName);
            const string targetPropertyName = nameof(TargetTestData.TargetName);
            var          sourceProperty     = sourceTestData.GetType().GetProperty(sourcePropertyName);
            var          targetProperty     = targetTestData.GetType().GetProperty(targetPropertyName);

            var linkDefinitions = new List <PropertyLinkDefinition>
            {
                new()
                {
                    Source             = sourceTestData,
                    Target             = targetTestData,
                    SourceProperty     = sourceProperty,
                    TargetType         = targetTestData.GetType(),
                    TargetPropertyName = targetPropertyName,
                    LinkDirection      = LinkDirection.OneWayToTarget
                }
            };

            var items = new PropertyLinkItemsBuilder(linkDefinitions).Build().ToArray();

            Assert.Single(items);
            var item = items.First();

            Assert.Equal(sourceTestData, item.Info.Source);
            Assert.Equal(targetTestData, item.Info.Target);
            Assert.Equal(sourceProperty, item.Info.SourceProperty);
            Assert.Equal(targetProperty, item.Info.TargetProperty);
            Assert.Equal(LinkDirection.OneWayToTarget, item.Info.Direction);
        }
Example #2
0
        public void Build_TwoDefinitionWithSameDataButDifferentConverterParametersInput_TwoInfoOutput()
        {
            var          sourceTestData     = new SourceTestData();
            var          targetTestData     = new TargetTestData();
            const string sourcePropertyName = nameof(SourceTestData.SourceName);
            const string targetPropertyName = nameof(TargetTestData.TargetName);
            var          sourceProperty     = sourceTestData.GetType().GetProperty(sourcePropertyName);
            var          targetProperty     = targetTestData.GetType().GetProperty(targetPropertyName);

            var linkDefinitions = new List <PropertyLinkDefinition>
            {
                new()
                {
                    Source             = sourceTestData,
                    Target             = targetTestData,
                    SourceProperty     = sourceProperty,
                    TargetType         = targetTestData.GetType(),
                    TargetPropertyName = targetPropertyName,
                    LinkDirection      = LinkDirection.OneWayToTarget,
                    Converter          = typeof(StringSourcePropertyConverter <int>),
                    ConverterParameter = 1
                },
                new()
                {
                    Source             = sourceTestData,
                    Target             = targetTestData,
                    SourceProperty     = sourceProperty,
                    TargetType         = targetTestData.GetType(),
                    TargetPropertyName = targetPropertyName,
                    LinkDirection      = LinkDirection.OneWayToTarget,
                    Converter          = typeof(StringSourcePropertyConverter <int>),
                    ConverterParameter = 2
                }
            };

            var items = new PropertyLinkItemsBuilder(linkDefinitions).Build().ToArray();

            Assert.Equal(2, items.Length);
            var firstItem = items[0];

            Assert.Equal(sourceTestData, firstItem.Info.Source);
            Assert.Equal(targetTestData, firstItem.Info.Target);
            Assert.Equal(sourceProperty, firstItem.Info.SourceProperty);
            Assert.Equal(targetProperty, firstItem.Info.TargetProperty);
            Assert.IsType <StringSourcePropertyConverter <int> >(firstItem.Info.Converter);
            Assert.Equal(1, firstItem.Info.ConverterParameter);
            Assert.Equal(LinkDirection.OneWayToTarget, firstItem.Info.Direction);

            var secondItem = items[1];

            Assert.Equal(sourceTestData, secondItem.Info.Source);
            Assert.Equal(targetTestData, secondItem.Info.Target);
            Assert.Equal(sourceProperty, secondItem.Info.SourceProperty);
            Assert.Equal(targetProperty, secondItem.Info.TargetProperty);
            Assert.IsType <StringSourcePropertyConverter <int> >(secondItem.Info.Converter);
            Assert.Equal(2, secondItem.Info.ConverterParameter);
            Assert.Equal(LinkDirection.OneWayToTarget, secondItem.Info.Direction);
        }
    }
Example #3
0
        public void LinkOneWayToTarget_SetSourceProperty_TargetPropertyIsUpdated()
        {
            var sourceTestData = new SourceTestData();
            var targetTestData = new TargetTestData();

            var _ = new ObjectLinkBuilder(sourceTestData, targetTestData).Build();

            sourceTestData.SourceText = "Test";

            Assert.Equal("Test", targetTestData.TargetText);
        }
Example #4
0
        public void LinkOneWayFromTarget_SetTargetProperty_SourcePropertyIsUpdated()
        {
            var sourceTestData = new SourceTestData();
            var targetTestData = new TargetTestData();

            var _ = new ObjectLinkBuilder(sourceTestData, targetTestData).Build();

            sourceTestData.SourceName = "Test";

            Assert.Equal("Test", targetTestData.FromSourceName);
        }
Example #5
0
        public void LinkOneWayToTarget_InitialTargetPropertySetBeforeLinking_SourcePropertyIsNotUpdated()
        {
            var sourceTestData = new SourceTestData();
            var targetTestData = new TargetTestData {
                TargetText = "Test"
            };

            var _ = new ObjectLinkBuilder(sourceTestData, targetTestData).Build();

            Assert.Null(sourceTestData.SourceText);
        }
Example #6
0
        public void LinkOneWayToTarget_InitialSourcePropertySetBeforeLinking_TargetPropertyIsUpdated()
        {
            var sourceTestData = new SourceTestData {
                SourceText = "Test"
            };
            var targetTestData = new TargetTestData();

            var _ = new ObjectLinkBuilder(sourceTestData, targetTestData).Build();

            Assert.Equal("Test", targetTestData.TargetText);
        }
Example #7
0
        public void LinkOneWayFromTarget_InitialSourcePropertySetBeforeLinking_TargetPropertyIsNotUpdated()
        {
            var sourceTestData = new SourceTestData {
                SourceName = "Test"
            };
            var targetTestData = new TargetTestData();

            var _ = new ObjectLinkBuilder(sourceTestData, targetTestData).Build();

            Assert.Null(targetTestData.TargetName);
        }
Example #8
0
        public void LinkOneWayFromTarget_InitialTargetPropertySetBeforeLinking_SourcePropertyIsUpdated()
        {
            var sourceTestData = new SourceTestData();
            var targetTestData = new TargetTestData {
                TargetName = "Hello"
            };

            var _ = new ObjectLinkBuilder(sourceTestData, targetTestData).Build();

            Assert.Equal("Hello", sourceTestData.SourceName);
        }
Example #9
0
        public void Build_TwoDifferentDefinitionInput_TwoInfoOutput()
        {
            var          sourceTestData      = new SourceTestData();
            var          targetTestData      = new TargetTestData();
            const string sourcePropertyName0 = nameof(SourceTestData.SourceName);
            const string targetPropertyName0 = nameof(TargetTestData.TargetName);
            const string sourcePropertyName1 = nameof(SourceTestData.SourceText);
            const string targetPropertyName1 = nameof(TargetTestData.TargetText);
            var          sourceProperty0     = sourceTestData.GetType().GetProperty(sourcePropertyName0);
            var          targetProperty0     = targetTestData.GetType().GetProperty(targetPropertyName0);
            var          sourceProperty1     = sourceTestData.GetType().GetProperty(sourcePropertyName1);
            var          targetProperty1     = targetTestData.GetType().GetProperty(targetPropertyName1);

            var linkDefinitions = new List <PropertyLinkDefinition>
            {
                new()
                {
                    Source             = sourceTestData,
                    Target             = targetTestData,
                    SourceProperty     = sourceProperty0,
                    TargetType         = targetTestData.GetType(),
                    TargetPropertyName = targetPropertyName0,
                    LinkDirection      = LinkDirection.OneWayToTarget
                },
                new()
                {
                    Source             = sourceTestData,
                    Target             = targetTestData,
                    SourceProperty     = sourceProperty1,
                    TargetType         = targetTestData.GetType(),
                    TargetPropertyName = targetPropertyName1,
                    LinkDirection      = LinkDirection.OneWayFromTarget
                }
            };

            var items = new PropertyLinkItemsBuilder(linkDefinitions).Build().ToArray();

            Assert.Equal(2, items.Length);
            var item0 = items.First();

            Assert.Equal(sourceTestData, item0.Info.Source);
            Assert.Equal(targetTestData, item0.Info.Target);
            Assert.Equal(sourceProperty0, item0.Info.SourceProperty);
            Assert.Equal(targetProperty0, item0.Info.TargetProperty);
            Assert.Equal(LinkDirection.OneWayToTarget, item0.Info.Direction);

            var item1 = items.Last();

            Assert.Equal(sourceTestData, item1.Info.Source);
            Assert.Equal(targetTestData, item1.Info.Target);
            Assert.Equal(sourceProperty1, item1.Info.SourceProperty);
            Assert.Equal(targetProperty1, item1.Info.TargetProperty);
            Assert.Equal(LinkDirection.OneWayFromTarget, item1.Info.Direction);
        }
Example #10
0
        public void LinkTwoWay_SetPropertyWithConverterOnNotConvertibleValue_LinkedPropertyIsNotUpdated()
        {
            var sourceTestData = new SourceTestData();
            var targetTestData = new TargetTestData();

            var _ = new ObjectLinkBuilder(sourceTestData, targetTestData).Build();

            sourceTestData.SecondIntValue = 1;
            targetTestData.DoubleValue    = 1234;

            Assert.Equal(1, sourceTestData.SecondIntValue);
        }
Example #11
0
        public void LinkTwoWay_SetPropertyWithConverterToNotConvertibleValue_LinkedPropertyIsNotUpdated()
        {
            var sourceTestData = new SourceTestData();
            var targetTestData = new TargetTestData();

            var _ = new ObjectLinkBuilder(sourceTestData, targetTestData).Build();

            sourceTestData.IntValue    = 1234;
            targetTestData.StringValue = "Hello";

            Assert.Equal(1234, sourceTestData.IntValue);
        }
Example #12
0
        public void Dispose_SetSourcePropertyAfterDispose_TargetPropertyIsNotUpdated()
        {
            var sourceTestData = new SourceTestData();
            var targetTestData = new TargetTestData();

            var link = new ObjectLinkBuilder(sourceTestData, targetTestData).Build();

            link.Dispose();

            sourceTestData.SourceText = "Test";

            Assert.Null(targetTestData.TargetText);
        }
Example #13
0
        public void LinkTwoWay_SetPropertyWithConverter_LinkedPropertyIsUpdated()
        {
            var sourceTestData = new SourceTestData();
            var targetTestData = new TargetTestData();

            var _ = new ObjectLinkBuilder(sourceTestData, targetTestData).Build();

            sourceTestData.IntValue = 5678;

            Assert.Equal("5678", targetTestData.StringValue);

            targetTestData.StringValue = "1234";

            Assert.Equal(1234, sourceTestData.IntValue);
        }
Example #14
0
        public void LinkTwoWay_SetPropertyOnInheritedClass_LinkedPropertyIsUpdated()
        {
            var sourceTestData = new SourceTestData();
            var targetTestData = new NewTargetTestData();

            var _ = new ObjectLinkBuilder(sourceTestData, targetTestData).Build();

            sourceTestData.TwoWayProperty = "Test";

            Assert.Equal("Test", targetTestData.TwoWayProperty);
            Assert.Equal("Test", sourceTestData.TwoWayProperty);

            targetTestData.TwoWayProperty = "1234";

            Assert.Equal("1234", sourceTestData.TwoWayProperty);
            Assert.Equal("1234", targetTestData.TwoWayProperty);
        }