Beispiel #1
0
        public void PropertyContainer_Construct_AbstractFieldConstructsNewInstanceFromDynamicSourceType()
        {
            var src = new StructContainerWithNestedDynamicContainer {
                Container = new DynamicContainer(typeof(DerivedClassA).AssemblyQualifiedName)
            };
            var dst = new ClassContainerWithAbstractField {
                Container = null
            };

            using (var result = PropertyContainer.Construct(ref dst, ref src,
                                                            new PropertyContainerConstructOptions {
                TypeIdentifierKey = DynamicContainer.TypeIdentifierKey
            }))
            {
                Assert.That(result.Succeeded, Is.True);
                Assert.That(dst.Container, Is.Not.Null);
            }

            src = new StructContainerWithNestedDynamicContainer {
                Container = new DynamicContainer("unknown type")
            };
            dst = new ClassContainerWithAbstractField {
                Container = null
            };

            using (var result = PropertyContainer.Construct(ref dst, ref src, new PropertyContainerConstructOptions {
                TypeIdentifierKey = DynamicContainer.TypeIdentifierKey
            }))
            {
                Assert.That(result.Succeeded, Is.False);
                Assert.That(result.Exceptions.Count(), Is.EqualTo(1));
                Assert.Throws <InvalidOperationException>(result.Exceptions.First().Throw);
            }
        }
Beispiel #2
0
        static T CopyComponent <T>(T value)
        {
            var result = TypeConstruction.Construct <T>(value.GetType());

            PropertyContainer.Construct(ref result, ref value).Dispose();
            PropertyContainer.Transfer(ref result, ref value).Dispose();
            return(result);
        }
        public void PropertyContainer_Construct_NullSrcContainerShouldGiveUnderstandableErrorMessage()
        {
            var src = (CustomDataFoo)null;
            var dst = new CustomDataFoo();

            var ex = Assert.Throws <ArgumentNullException>(() => PropertyContainer.Construct(ref dst, ref src));

            Assert.That(ex.ParamName, Is.EqualTo("srcContainer"));
            Assert.That(ex.Message, Is.EqualTo("Value cannot be null." + Environment.NewLine + "Parameter name: srcContainer"));
        }
        public void PropertyContainer_Construct_ShouldConstructWhenDstContainerIsNull()
        {
            var src = new ClassContainerWithNestedClass {
                Container = new ClassContainerWithPrimitives()
            };
            var dst = (ClassContainerWithNestedClass)null;

            using (var result = PropertyContainer.Construct(ref dst, ref src))
            {
                Assert.That(result.Succeeded, Is.True);
                Assert.That(dst, Is.Not.Null);
                Assert.That(dst.Container, Is.Not.Null);
            }
        }
Beispiel #5
0
        public void PropertyContainer_Construct_ArrayFieldAssignsNull()
        {
            var src = new ClassContainerWithArrays {
                IntArray = null
            };
            var dst = new ClassContainerWithArrays {
                IntArray = new int[5]
            };

            using (var result = PropertyContainer.Construct(ref dst, ref src))
            {
                Assert.That(result.Succeeded);
                Assert.That(dst.IntArray, Is.Null);
            }
        }
        public void PropertyContainer_Construct_ListFieldAssignsNull()
        {
            var src = new ClassContainerWithLists {
                IntList = null
            };
            var dst = new ClassContainerWithLists {
                IntList = new List <int>()
            };

            using (var result = PropertyContainer.Construct(ref dst, ref src))
            {
                Assert.That(result.Succeeded, Is.True);
                Assert.That(dst.IntList, Is.Null);
            }
        }
        public void PropertyContainer_Construct_ReferenceFieldConstructsNewInstanceWithDifferentSourceType()
        {
            var src = new StructContainerWithNestedStruct {
                Container = new StructContainerWithPrimitives()
            };
            var dst = new ClassContainerWithNestedClass {
                Container = null
            };

            using (var result = PropertyContainer.Construct(ref dst, ref src))
            {
                Assert.That(result.Succeeded, Is.True);
                Assert.That(dst.Container, Is.Not.Null);
            }
        }
        public void PropertyContainer_Construct_ReferenceFieldAssignsNull()
        {
            var src = new ClassContainerWithNestedClass {
                Container = null
            };
            var dst = new ClassContainerWithNestedClass {
                Container = new ClassContainerWithPrimitives()
            };

            using (var result = PropertyContainer.Construct(ref dst, ref src))
            {
                Assert.That(result.Succeeded, Is.True);
                Assert.That(dst.Container, Is.Null);
            }
        }
        public void PropertyContainer_Construct_ListFieldConstructsNewInstance()
        {
            var src = new ClassContainerWithLists {
                IntList = new List <int>()
            };
            var dst = new ClassContainerWithLists {
                IntList = null
            };

            using (var result = PropertyContainer.Construct(ref dst, ref src))
            {
                Assert.That(result.Succeeded, Is.True);
                Assert.That(dst.IntList, Is.Not.Null);
                Assert.That(!ReferenceEquals(src.IntList, dst.IntList));
            }
        }
Beispiel #10
0
        public void PropertyContainer_Construct_AbstractFieldConstructsInstanceOfDifferentDerivedType()
        {
            var src = new ClassContainerWithAbstractField {
                Container = new DerivedClassA()
            };
            var dst = new ClassContainerWithAbstractField {
                Container = new DerivedClassB()
            };

            using (var result = PropertyContainer.Construct(ref dst, ref src))
            {
                Assert.That(result.Succeeded, Is.True);
                Assert.That(dst.Container, Is.Not.Null);
                Assert.That(dst.Container, Is.TypeOf <DerivedClassA>());
            }
        }
Beispiel #11
0
        public void PropertyContainer_Construct_ArrayFieldConstructsNewInstance()
        {
            var src = new ClassContainerWithArrays {
                IntArray = new int[5]
            };
            var dst = new ClassContainerWithArrays {
                IntArray = null
            };

            using (var result = PropertyContainer.Construct(ref dst, ref src))
            {
                Assert.That(result.Succeeded, Is.True);
                Assert.That(dst.IntArray, Is.Not.Null);
                Assert.That(!ReferenceEquals(src.IntArray, dst.IntArray));
            }
        }
        public void PropertyContainer_Construct_ListFieldDoesNotDestroyExistingInstance()
        {
            var src = new ClassContainerWithLists {
                IntList = new List <int>()
            };
            var dst = new ClassContainerWithLists {
                IntList = new List <int>()
            };

            var reference = dst.IntList;

            using (var result = PropertyContainer.Construct(ref dst, ref src))
            {
                Assert.That(result.Succeeded, Is.True);
                Assert.That(ReferenceEquals(reference, dst.IntList));
                Assert.That(!ReferenceEquals(src.IntList, dst.IntList));
            }
        }
Beispiel #13
0
        public void PropertyContainer_Construct_AbstractFieldDoesNotCreateNewInstance()
        {
            var src = new ClassContainerWithAbstractField {
                Container = new DerivedClassA()
            };
            var dst = new ClassContainerWithAbstractField {
                Container = new DerivedClassA()
            };

            var reference = dst.Container;

            using (var result = PropertyContainer.Construct(ref dst, ref src))
            {
                Assert.That(result.Succeeded, Is.True);
                Assert.That(ReferenceEquals(reference, dst.Container));
                Assert.That(!ReferenceEquals(src.Container, dst.Container));
            }
        }
        public void PropertyContainer_Construct_ReferenceFieldDoesNotDestroyExistingInstance()
        {
            var src = new ClassContainerWithNestedClass {
                Container = new ClassContainerWithPrimitives()
            };
            var dst = new ClassContainerWithNestedClass {
                Container = new ClassContainerWithPrimitives()
            };

            var reference = dst.Container;

            using (var result = PropertyContainer.Construct(ref dst, ref src))
            {
                Assert.That(result.Succeeded, Is.True);
                Assert.That(ReferenceEquals(reference, dst.Container));
                Assert.That(!ReferenceEquals(src.Container, dst.Container));
            }
        }
Beispiel #15
0
        public void PropertyContainer_Construct_AbstractFieldConstructsNewInstanceWithCorrectDerivedType()
        {
            var src = new ClassContainerWithAbstractField {
                Container = new DerivedClassA {
                    BaseIntValue = 1, A = 5
                }
            };
            var dst = new ClassContainerWithAbstractField {
                Container = null
            };

            using (var result = PropertyContainer.Construct(ref dst, ref src))
            {
                Assert.That(result.Succeeded, Is.True);
                Assert.That(dst.Container, Is.Not.Null);
                Assert.That(dst.Container, Is.TypeOf <DerivedClassA>());
            }
        }
        public void PropertyContainer_Construct_ListFieldElementConstructsNewInstance()
        {
            var src = new ClassContainerWithLists {
                ContainerWithPrimitivesList = new List <ClassContainerWithPrimitives>
                {
                    new ClassContainerWithPrimitives()
                }
            };
            var dst = new ClassContainerWithLists {
                ContainerWithPrimitivesList = new List <ClassContainerWithPrimitives>()
            };

            using (var result = PropertyContainer.Construct(ref dst, ref src))
            {
                Assert.That(result.Succeeded, Is.True);
                Assert.That(dst.ContainerWithPrimitivesList, Is.Not.Null);
                Assert.That(!ReferenceEquals(src.ContainerWithPrimitivesList, dst.ContainerWithPrimitivesList));
                Assert.That(dst.ContainerWithPrimitivesList.Count, Is.EqualTo(1));
                Assert.That(!ReferenceEquals(src.ContainerWithPrimitivesList[0], dst.ContainerWithPrimitivesList[0]));
            }
        }
Beispiel #17
0
        static VisitResult Deserialize <TContainer>(SerializedObjectReader reader, ref TContainer container)
        {
            var source = reader.ReadObject();
            var result = VisitResult.GetPooled();

            try
            {
                using (var construction = PropertyContainer.Construct(ref container, ref source, new PropertyContainerConstructOptions {
                    TypeIdentifierKey = JsonVisitor.Style.TypeInfoKey
                }))
                    result.TransferEvents(construction);

                using (var transfer = PropertyContainer.Transfer(ref container, ref source))
                    result.TransferEvents(transfer);
            }
            catch (Exception)
            {
                reader.Dispose();
                throw;
            }

            return(result);
        }
Beispiel #18
0
 void CopyComponent(ref TComponent dst, ref TComponent src)
 {
     PropertyContainer.Construct(ref dst, ref src).Dispose();
     PropertyContainer.Transfer(ref dst, ref src).Dispose();
 }