Ejemplo n.º 1
0
        public void DeepCopy()
        {
            ClassA original = ClassExtensions.DeepCreate <ClassA>();

            original.IntVal       = 2;
            original.NullIntVal   = 2;
            original.DateTime     = DateTime.Today;
            original.NullDateTime = DateTime.MaxValue;
            original.IntList.Add(2);
            ((List <string>)original.StringEnumerable).Add("Two");
            ((Dictionary <string, int>)original.StringIntDictionary).Add("Two", 2);
            original.ChangeReadonlyInt(2);
            original.CList.Add(new ClassC(1, original));
            original.CList.Add(new ClassC(2, original));
            original.CList.Add(new ClassC(3, original));

            ClassA copy = original.DeepCopy();

            Assert.IsNotNull(copy);
            Assert.AreEqual(copy.IntVal, 2);
            Assert.AreEqual(copy.NullIntVal, 2);
            Assert.AreEqual(copy.DateTime, DateTime.Today);
            Assert.AreEqual(copy.NullDateTime, DateTime.MaxValue);
            Assert.IsNotNull(copy.IntList);
            Assert.AreEqual(copy.IntList.Count, 1);
            Assert.AreEqual(copy.IntList[0], 2);

            Assert.IsNotNull(copy.StringEnumerable);
            Assert.AreEqual(copy.StringEnumerable.Count(), 1);
            Assert.AreEqual(copy.StringEnumerable.First(), "Two");

            Assert.IsNotNull(copy.CList);
            Assert.AreEqual(copy.CList.Count, 3);
            Assert.AreEqual(copy.CList[0].IntVal, 1);
            Assert.AreEqual(copy.CList[0].Owner, copy);
            Assert.AreEqual(copy.CList[1].IntVal, 2);
            Assert.AreEqual(copy.CList[1].Owner, copy);
            Assert.AreEqual(copy.CList[2].IntVal, 3);
            Assert.AreEqual(copy.CList[2].Owner, copy);

            Assert.IsNotNull(copy.StringIntDictionary);
            Assert.AreEqual(copy.StringIntDictionary.Count, 1);
            Assert.AreEqual(copy.StringIntDictionary["Two"], 2);

            Assert.AreEqual(copy.ReadOnlyInt, -1);

            Assert.IsNotNull(copy.B);
            Assert.IsNotNull(copy.B.ClassA);
            Assert.AreSame(copy.B.ClassA, copy);
            Assert.IsNotNull(copy.B.Owner);
            Assert.AreSame(copy.B.Owner, copy);
            Assert.IsNotNull(copy.B.Parent);
            Assert.AreSame(copy.B.Parent, copy);
            Assert.IsNull(copy.B.TheresaMay);
        }