#region Apache License 2.0 /* Nuclex .NET Framework Copyright (C) 2002-2024 Markus Ewald / Nuclex Development Labs Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #endregion // Apache License 2.0 #if !NO_SETS #if UNITTEST using System; using System.Collections.Generic; using NUnit.Framework; namespace Nuclex.Support.Cloning { /// Unit Test for the expression tree-based cloner [TestFixture] internal class ExpressionTreeClonerTest : CloneFactoryTest { /// Initializes a new unit test suite for the reflection cloner public ExpressionTreeClonerTest() { this.cloneFactory = new ExpressionTreeCloner(); } /// Verifies that cloning a null object simply returns null [Test] public void CloningNullYieldsNull() { Assert.IsNull(this.cloneFactory.DeepFieldClone(null)); Assert.IsNull(this.cloneFactory.DeepPropertyClone(null)); Assert.IsNull(this.cloneFactory.ShallowFieldClone(null)); Assert.IsNull(this.cloneFactory.ShallowPropertyClone(null)); } /// /// Verifies that clones of objects whose class doesn't possess a default constructor /// can be made /// [Test] public void ClassWithoutDefaultConstructorCanBeCloned() { var original = new ClassWithoutDefaultConstructor(1234); ClassWithoutDefaultConstructor clone = this.cloneFactory.DeepFieldClone(original); Assert.AreNotSame(original, clone); Assert.AreEqual(original.Dummy, clone.Dummy); } /// Verifies that clones of primitive types can be created [Test] public void PrimitiveTypesCanBeCloned() { int original = 12345; int clone = this.cloneFactory.DeepFieldClone(original); Assert.AreEqual(original, clone); } /// Verifies that shallow clones of arrays can be made [Test] public void ReferenceTypesCanBeCloned() { var original = new TestReferenceType() { TestField = 123, TestProperty = 456 }; TestReferenceType clone = this.cloneFactory.DeepFieldClone(original); Assert.AreNotSame(original, clone); Assert.AreEqual(original.TestField, clone.TestField); Assert.AreEqual(original.TestProperty, clone.TestProperty); } /// Verifies that shallow clones of arrays can be made [Test] public void PrimitiveArraysCanBeCloned() { var original = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int[] clone = this.cloneFactory.DeepFieldClone(original); Assert.AreNotSame(original, clone); CollectionAssert.AreEqual(original, clone); } /// Verifies that shallow clones of arrays can be made [Test] public void ShallowClonesOfArraysCanBeMade() { var original = new TestReferenceType[] { new TestReferenceType() { TestField = 123, TestProperty = 456 } }; TestReferenceType[] clone = this.cloneFactory.ShallowFieldClone(original); Assert.AreSame(original[0], clone[0]); } /// Verifies that deep clones of arrays can be made [Test] public void DeepClonesOfArraysCanBeMade() { var original = new TestReferenceType[,] { { new TestReferenceType() { TestField = 123, TestProperty = 456 } } }; TestReferenceType[,] clone = this.cloneFactory.DeepFieldClone(original); Assert.AreNotSame(original[0, 0], clone[0, 0]); Assert.AreEqual(original[0, 0].TestField, clone[0, 0].TestField); Assert.AreEqual(original[0, 0].TestProperty, clone[0, 0].TestProperty); } /// Verifies that deep clones of a generic list can be made [Test] public void GenericListsCanBeCloned() { var original = new List(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }); List clone = this.cloneFactory.DeepFieldClone(original); CollectionAssert.AreEqual(original, clone); } /// Verifies that deep clones of a generic dictionary can be made [Test] public void GenericDictionariesCanBeCloned() { var original = new Dictionary(); original.Add(1, "one"); Dictionary clone = this.cloneFactory.DeepFieldClone(original); Assert.AreEqual("one", clone[1]); } /// /// Verifies that a field-based shallow clone of a value type can be performed /// [Test] public void ShallowFieldBasedClonesOfValueTypesCanBeMade() { HierarchicalValueType original = CreateValueType(); HierarchicalValueType clone = this.cloneFactory.ShallowFieldClone(original); VerifyClone(ref original, ref clone, isDeepClone: false, isPropertyBasedClone: false); } /// /// Verifies that a field-based shallow clone of a reference type can be performed /// [Test] public void ShallowFieldBasedClonesOfReferenceTypesCanBeMade() { HierarchicalReferenceType original = CreateReferenceType(); HierarchicalReferenceType clone = this.cloneFactory.ShallowFieldClone(original); VerifyClone(original, clone, isDeepClone: false, isPropertyBasedClone: false); } /// /// Verifies that a field-based deep clone of a value type can be performed /// [Test] public void DeepFieldBasedClonesOfValueTypesCanBeMade() { HierarchicalValueType original = CreateValueType(); HierarchicalValueType clone = this.cloneFactory.DeepFieldClone(original); VerifyClone(ref original, ref clone, isDeepClone: true, isPropertyBasedClone: false); } /// /// Verifies that a field-based deep clone of a reference type can be performed /// [Test] public void DeepFieldBasedClonesOfReferenceTypesCanBeMade() { HierarchicalReferenceType original = CreateReferenceType(); HierarchicalReferenceType clone = this.cloneFactory.DeepFieldClone(original); VerifyClone(original, clone, isDeepClone: true, isPropertyBasedClone: false); } /// /// Verifies that a property-based shallow clone of a value type can be performed /// [Test] public void ShallowPropertyBasedClonesOfValueTypesCanBeMade() { HierarchicalValueType original = CreateValueType(); HierarchicalValueType clone = this.cloneFactory.ShallowPropertyClone(original); VerifyClone(ref original, ref clone, isDeepClone: false, isPropertyBasedClone: true); } /// /// Verifies that a property-based shallow clone of a reference type can be performed /// [Test] public void ShallowPropertyBasedClonesOfReferenceTypesCanBeMade() { HierarchicalReferenceType original = CreateReferenceType(); HierarchicalReferenceType clone = this.cloneFactory.ShallowPropertyClone(original); VerifyClone(original, clone, isDeepClone: false, isPropertyBasedClone: true); } /// /// Verifies that a property-based deep clone of a value type can be performed /// [Test] public void DeepPropertyBasedClonesOfValueTypesCanBeMade() { HierarchicalValueType original = CreateValueType(); HierarchicalValueType clone = this.cloneFactory.DeepPropertyClone(original); VerifyClone(ref original, ref clone, isDeepClone: true, isPropertyBasedClone: true); } /// /// Verifies that a property-based deep clone of a reference type can be performed /// [Test] public void DeepPropertyBasedClonesOfReferenceTypesCanBeMade() { HierarchicalReferenceType original = CreateReferenceType(); HierarchicalReferenceType clone = this.cloneFactory.DeepPropertyClone(original); VerifyClone(original, clone, isDeepClone: true, isPropertyBasedClone: true); } /// Clone factory being tested private ICloneFactory cloneFactory; } } // namespace Nuclex.Support.Cloning #endif // UNITTEST #endif // !NO_SETS