コード例 #1
0
        public void DeepPropertyBasedClonesOfReferenceTypesCanBeMade()
        {
            HierarchicalReferenceType original = CreateReferenceType();
            HierarchicalReferenceType clone    = this.cloneFactory.DeepPropertyClone(original);

            VerifyClone(original, clone, isDeepClone: true, isPropertyBasedClone: true);
        }
コード例 #2
0
        public void ShallowFieldBasedClonesOfReferenceTypesCanBeMade()
        {
            HierarchicalReferenceType original = CreateReferenceType();
            HierarchicalReferenceType clone    = this.cloneFactory.ShallowFieldClone(original);

            VerifyClone(original, clone, isDeepClone: false, isPropertyBasedClone: false);
        }
コード例 #3
0
        public void DeepFieldBasedClonesOfReferenceTypesCanBeMade()
        {
            HierarchicalReferenceType original = CreateReferenceType();
            HierarchicalReferenceType clone    = this.cloneFactory.DeepFieldClone(original);

            VerifyClone(original, clone, true, false);
        }
コード例 #4
0
        /// <summary>
        ///   Verifies that a cloned object exhibits the expected state for the type of
        ///   clone that has been performed
        /// </summary>
        /// <param name="original">Original instance the clone was created from</param>
        /// <param name="clone">Cloned instance that will be checked for correctness</param>
        /// <param name="isDeepClone">Whether the cloned instance is a deep clone</param>
        /// <param name="isPropertyBasedClone">
        ///   Whether a property-based clone was performed
        /// </param>
        protected static void VerifyClone(
      HierarchicalReferenceType original, HierarchicalReferenceType clone,
      bool isDeepClone, bool isPropertyBasedClone
    )
        {
            Assert.AreNotSame(original, clone);

              if(isPropertyBasedClone) {
            Assert.AreEqual(0, clone.TestField);
            Assert.AreEqual(0, clone.ValueTypeField.TestField);
            Assert.AreEqual(0, clone.ValueTypeField.TestProperty);
            Assert.AreEqual(0, clone.ValueTypeProperty.TestField);
            Assert.IsNull(clone.ReferenceTypeField);
            Assert.IsNull(clone.DerivedField);

            if(isDeepClone) {
              Assert.AreNotSame(original.ReferenceTypeProperty, clone.ReferenceTypeProperty);
              Assert.AreNotSame(
            original.ReferenceTypeArrayProperty, clone.ReferenceTypeArrayProperty
              );
              Assert.AreNotSame(original.DerivedProperty, clone.DerivedProperty);
              Assert.IsInstanceOf<DerivedReferenceType>(clone.DerivedProperty);

              var originalDerived = (DerivedReferenceType)original.DerivedProperty;
              var clonedDerived = (DerivedReferenceType)clone.DerivedProperty;
              Assert.AreEqual(originalDerived.TestProperty, clonedDerived.TestProperty);
              Assert.AreEqual(originalDerived.DerivedProperty, clonedDerived.DerivedProperty);

              Assert.AreEqual(0, clone.ReferenceTypeProperty.TestField);
              Assert.AreNotSame(
            original.ReferenceTypeArrayProperty[1, 3][0],
            clone.ReferenceTypeArrayProperty[1, 3][0]
              );
              Assert.AreNotSame(
            original.ReferenceTypeArrayProperty[1, 3][2],
            clone.ReferenceTypeArrayProperty[1, 3][2]
              );
              Assert.AreEqual(0, clone.ReferenceTypeArrayProperty[1, 3][0].TestField);
              Assert.AreEqual(0, clone.ReferenceTypeArrayProperty[1, 3][2].TestField);
            } else {
              Assert.AreSame(original.ReferenceTypeProperty, clone.ReferenceTypeProperty);
              Assert.AreSame(original.ReferenceTypeProperty, clone.ReferenceTypeProperty);
              Assert.AreSame(
            original.ReferenceTypeArrayProperty, clone.ReferenceTypeArrayProperty
              );
            }
              } else {
            Assert.AreEqual(original.TestField, clone.TestField);
            Assert.AreEqual(original.ValueTypeField.TestField, clone.ValueTypeField.TestField);
            Assert.AreEqual(original.ValueTypeField.TestProperty, clone.ValueTypeField.TestProperty);
            Assert.AreEqual(
              original.ValueTypeProperty.TestField, clone.ValueTypeProperty.TestField
            );
            Assert.AreEqual(
              original.ReferenceTypeField.TestField, clone.ReferenceTypeField.TestField
            );
            Assert.AreEqual(
              original.ReferenceTypeField.TestProperty, clone.ReferenceTypeField.TestProperty
            );
            Assert.AreEqual(
              original.ReferenceTypeProperty.TestField, clone.ReferenceTypeProperty.TestField
            );

            if(isDeepClone) {
              Assert.AreNotSame(original.ReferenceTypeField, clone.ReferenceTypeField);
              Assert.AreNotSame(original.ReferenceTypeProperty, clone.ReferenceTypeProperty);
              Assert.AreNotSame(original.DerivedField, clone.DerivedField);
              Assert.AreNotSame(original.DerivedProperty, clone.DerivedProperty);
              Assert.IsInstanceOf<DerivedReferenceType>(clone.DerivedField);
              Assert.IsInstanceOf<DerivedReferenceType>(clone.DerivedProperty);

              var originalDerived = (DerivedReferenceType)original.DerivedField;
              var clonedDerived = (DerivedReferenceType)clone.DerivedField;
              Assert.AreEqual(originalDerived.TestField, clonedDerived.TestField);
              Assert.AreEqual(originalDerived.TestProperty, clonedDerived.TestProperty);
              Assert.AreEqual(originalDerived.DerivedField, clonedDerived.DerivedField);
              Assert.AreEqual(originalDerived.DerivedProperty, clonedDerived.DerivedProperty);

              originalDerived = (DerivedReferenceType)original.DerivedProperty;
              clonedDerived = (DerivedReferenceType)clone.DerivedProperty;
              Assert.AreEqual(originalDerived.TestField, clonedDerived.TestField);
              Assert.AreEqual(originalDerived.TestProperty, clonedDerived.TestProperty);
              Assert.AreEqual(originalDerived.DerivedField, clonedDerived.DerivedField);
              Assert.AreEqual(originalDerived.DerivedProperty, clonedDerived.DerivedProperty);

              Assert.AreNotSame(
            original.ReferenceTypeArrayField, clone.ReferenceTypeArrayField
              );
              Assert.AreNotSame(
            original.ReferenceTypeArrayProperty, clone.ReferenceTypeArrayProperty
              );
              Assert.AreNotSame(
            original.ReferenceTypeArrayProperty[1, 3][0],
            clone.ReferenceTypeArrayProperty[1, 3][0]
              );
              Assert.AreNotSame(
            original.ReferenceTypeArrayProperty[1, 3][2],
            clone.ReferenceTypeArrayProperty[1, 3][2]
              );
              Assert.AreEqual(
            original.ReferenceTypeArrayProperty[1, 3][0].TestField,
            clone.ReferenceTypeArrayProperty[1, 3][0].TestField
              );
              Assert.AreEqual(
            original.ReferenceTypeArrayProperty[1, 3][2].TestField,
            clone.ReferenceTypeArrayProperty[1, 3][2].TestField
              );
            } else {
              Assert.AreSame(original.DerivedField, clone.DerivedField);
              Assert.AreSame(original.DerivedProperty, clone.DerivedProperty);
              Assert.AreSame(original.ReferenceTypeField, clone.ReferenceTypeField);
              Assert.AreSame(original.ReferenceTypeProperty, clone.ReferenceTypeProperty);
              Assert.AreSame(
            original.ReferenceTypeArrayField, clone.ReferenceTypeArrayField
              );
              Assert.AreSame(
            original.ReferenceTypeArrayProperty, clone.ReferenceTypeArrayProperty
              );
            }
              }
        }
コード例 #5
0
        /// <summary>
        ///   Verifies that a cloned object exhibits the expected state for the type of
        ///   clone that has been performed
        /// </summary>
        /// <param name="original">Original instance the clone was created from</param>
        /// <param name="clone">Cloned instance that will be checked for correctness</param>
        /// <param name="isDeepClone">Whether the cloned instance is a deep clone</param>
        /// <param name="isPropertyBasedClone">
        ///   Whether a property-based clone was performed
        /// </param>
        protected static void VerifyClone(
            HierarchicalReferenceType original, HierarchicalReferenceType clone,
            bool isDeepClone, bool isPropertyBasedClone
            )
        {
            Assert.AreNotSame(original, clone);

            if (isPropertyBasedClone)
            {
                Assert.AreEqual(0, clone.TestField);
                Assert.AreEqual(0, clone.ValueTypeField.TestField);
                Assert.AreEqual(0, clone.ValueTypeField.TestProperty);
                Assert.AreEqual(0, clone.ValueTypeProperty.TestField);
                Assert.IsNull(clone.ReferenceTypeField);
                Assert.IsNull(clone.DerivedField);

                if (isDeepClone)
                {
                    Assert.AreNotSame(original.ReferenceTypeProperty, clone.ReferenceTypeProperty);
                    Assert.AreNotSame(
                        original.ReferenceTypeArrayProperty, clone.ReferenceTypeArrayProperty
                        );
                    Assert.AreNotSame(original.DerivedProperty, clone.DerivedProperty);
                    Assert.IsInstanceOf <DerivedReferenceType>(clone.DerivedProperty);

                    var originalDerived = (DerivedReferenceType)original.DerivedProperty;
                    var clonedDerived   = (DerivedReferenceType)clone.DerivedProperty;
                    Assert.AreEqual(originalDerived.TestProperty, clonedDerived.TestProperty);
                    Assert.AreEqual(originalDerived.DerivedProperty, clonedDerived.DerivedProperty);

                    Assert.AreEqual(0, clone.ReferenceTypeProperty.TestField);
                    Assert.AreNotSame(
                        original.ReferenceTypeArrayProperty[1, 3][0],
                        clone.ReferenceTypeArrayProperty[1, 3][0]
                        );
                    Assert.AreNotSame(
                        original.ReferenceTypeArrayProperty[1, 3][2],
                        clone.ReferenceTypeArrayProperty[1, 3][2]
                        );
                    Assert.AreEqual(0, clone.ReferenceTypeArrayProperty[1, 3][0].TestField);
                    Assert.AreEqual(0, clone.ReferenceTypeArrayProperty[1, 3][2].TestField);
                }
                else
                {
                    Assert.AreSame(original.ReferenceTypeProperty, clone.ReferenceTypeProperty);
                    Assert.AreSame(original.ReferenceTypeProperty, clone.ReferenceTypeProperty);
                    Assert.AreSame(
                        original.ReferenceTypeArrayProperty, clone.ReferenceTypeArrayProperty
                        );
                }
            }
            else
            {
                Assert.AreEqual(original.TestField, clone.TestField);
                Assert.AreEqual(original.ValueTypeField.TestField, clone.ValueTypeField.TestField);
                Assert.AreEqual(original.ValueTypeField.TestProperty, clone.ValueTypeField.TestProperty);
                Assert.AreEqual(
                    original.ValueTypeProperty.TestField, clone.ValueTypeProperty.TestField
                    );
                Assert.AreEqual(
                    original.ReferenceTypeField.TestField, clone.ReferenceTypeField.TestField
                    );
                Assert.AreEqual(
                    original.ReferenceTypeField.TestProperty, clone.ReferenceTypeField.TestProperty
                    );
                Assert.AreEqual(
                    original.ReferenceTypeProperty.TestField, clone.ReferenceTypeProperty.TestField
                    );

                if (isDeepClone)
                {
                    Assert.AreNotSame(original.ReferenceTypeField, clone.ReferenceTypeField);
                    Assert.AreNotSame(original.ReferenceTypeProperty, clone.ReferenceTypeProperty);
                    Assert.AreNotSame(original.DerivedField, clone.DerivedField);
                    Assert.AreNotSame(original.DerivedProperty, clone.DerivedProperty);
                    Assert.IsInstanceOf <DerivedReferenceType>(clone.DerivedField);
                    Assert.IsInstanceOf <DerivedReferenceType>(clone.DerivedProperty);

                    var originalDerived = (DerivedReferenceType)original.DerivedField;
                    var clonedDerived   = (DerivedReferenceType)clone.DerivedField;
                    Assert.AreEqual(originalDerived.TestField, clonedDerived.TestField);
                    Assert.AreEqual(originalDerived.TestProperty, clonedDerived.TestProperty);
                    Assert.AreEqual(originalDerived.DerivedField, clonedDerived.DerivedField);
                    Assert.AreEqual(originalDerived.DerivedProperty, clonedDerived.DerivedProperty);

                    originalDerived = (DerivedReferenceType)original.DerivedProperty;
                    clonedDerived   = (DerivedReferenceType)clone.DerivedProperty;
                    Assert.AreEqual(originalDerived.TestField, clonedDerived.TestField);
                    Assert.AreEqual(originalDerived.TestProperty, clonedDerived.TestProperty);
                    Assert.AreEqual(originalDerived.DerivedField, clonedDerived.DerivedField);
                    Assert.AreEqual(originalDerived.DerivedProperty, clonedDerived.DerivedProperty);

                    Assert.AreNotSame(
                        original.ReferenceTypeArrayField, clone.ReferenceTypeArrayField
                        );
                    Assert.AreNotSame(
                        original.ReferenceTypeArrayProperty, clone.ReferenceTypeArrayProperty
                        );
                    Assert.AreNotSame(
                        original.ReferenceTypeArrayProperty[1, 3][0],
                        clone.ReferenceTypeArrayProperty[1, 3][0]
                        );
                    Assert.AreNotSame(
                        original.ReferenceTypeArrayProperty[1, 3][2],
                        clone.ReferenceTypeArrayProperty[1, 3][2]
                        );
                    Assert.AreEqual(
                        original.ReferenceTypeArrayProperty[1, 3][0].TestField,
                        clone.ReferenceTypeArrayProperty[1, 3][0].TestField
                        );
                    Assert.AreEqual(
                        original.ReferenceTypeArrayProperty[1, 3][2].TestField,
                        clone.ReferenceTypeArrayProperty[1, 3][2].TestField
                        );
                }
                else
                {
                    Assert.AreSame(original.DerivedField, clone.DerivedField);
                    Assert.AreSame(original.DerivedProperty, clone.DerivedProperty);
                    Assert.AreSame(original.ReferenceTypeField, clone.ReferenceTypeField);
                    Assert.AreSame(original.ReferenceTypeProperty, clone.ReferenceTypeProperty);
                    Assert.AreSame(
                        original.ReferenceTypeArrayField, clone.ReferenceTypeArrayField
                        );
                    Assert.AreSame(
                        original.ReferenceTypeArrayProperty, clone.ReferenceTypeArrayProperty
                        );
                }
            }
        }