2012-02-01 22:45:15 +00:00
|
|
|
|
#region CPL License
|
|
|
|
|
/*
|
|
|
|
|
Nuclex Framework
|
2013-04-25 13:11:59 +00:00
|
|
|
|
Copyright (C) 2002-2013 Nuclex Development Labs
|
2012-02-01 22:45:15 +00:00
|
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the IBM Common Public License as
|
|
|
|
|
published by the IBM Corporation; either version 1.0 of the
|
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
IBM Common Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the IBM Common Public
|
|
|
|
|
License along with this library
|
|
|
|
|
*/
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#if UNITTEST
|
|
|
|
|
|
2012-02-02 20:11:17 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
2012-02-01 22:45:15 +00:00
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
|
|
|
|
namespace Nuclex.Support.Cloning {
|
|
|
|
|
|
|
|
|
|
/// <summary>Unit Test for the reflection-based cloner</summary>
|
|
|
|
|
[TestFixture]
|
2012-02-08 15:20:39 +00:00
|
|
|
|
internal class ReflectionClonerTest : CloneFactoryTest {
|
2012-02-01 22:45:15 +00:00
|
|
|
|
|
2012-02-03 12:18:37 +00:00
|
|
|
|
/// <summary>Initializes a new unit test suite for the reflection cloner</summary>
|
|
|
|
|
public ReflectionClonerTest() {
|
|
|
|
|
this.cloneFactory = new ReflectionCloner();
|
2012-02-01 22:45:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-02-08 17:38:20 +00:00
|
|
|
|
/// <summary>Verifies that cloning a null object simply returns null</summary>
|
|
|
|
|
[Test]
|
|
|
|
|
public void CloningNullYieldsNull() {
|
|
|
|
|
Assert.IsNull(this.cloneFactory.DeepFieldClone<object>(null));
|
|
|
|
|
Assert.IsNull(this.cloneFactory.DeepPropertyClone<object>(null));
|
|
|
|
|
Assert.IsNull(this.cloneFactory.ShallowFieldClone<object>(null));
|
|
|
|
|
Assert.IsNull(this.cloneFactory.ShallowPropertyClone<object>(null));
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-08 19:26:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Verifies that clones of objects whose class doesn't possess a default constructor
|
|
|
|
|
/// can be made
|
|
|
|
|
/// </summary>
|
|
|
|
|
[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);
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-02 20:11:17 +00:00
|
|
|
|
/// <summary>Verifies that clones of primitive types can be created</summary>
|
|
|
|
|
[Test]
|
|
|
|
|
public void PrimitiveTypesCanBeCloned() {
|
|
|
|
|
int original = 12345;
|
2012-02-08 17:13:08 +00:00
|
|
|
|
int clone = this.cloneFactory.ShallowFieldClone(original);
|
2012-02-02 20:11:17 +00:00
|
|
|
|
Assert.AreEqual(original, clone);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Verifies that shallow clones of arrays can be made</summary>
|
|
|
|
|
[Test]
|
|
|
|
|
public void ShallowClonesOfArraysCanBeMade() {
|
|
|
|
|
var original = new TestReferenceType[] {
|
|
|
|
|
new TestReferenceType() { TestField = 123, TestProperty = 456 }
|
|
|
|
|
};
|
2012-02-08 17:13:08 +00:00
|
|
|
|
TestReferenceType[] clone = this.cloneFactory.ShallowFieldClone(original);
|
2012-02-02 20:11:17 +00:00
|
|
|
|
|
|
|
|
|
Assert.AreSame(original[0], clone[0]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Verifies that deep clones of arrays can be made</summary>
|
|
|
|
|
[Test]
|
|
|
|
|
public void DeepClonesOfArraysCanBeMade() {
|
|
|
|
|
var original = new TestReferenceType[] {
|
|
|
|
|
new TestReferenceType() { TestField = 123, TestProperty = 456 }
|
|
|
|
|
};
|
2012-02-08 17:13:08 +00:00
|
|
|
|
TestReferenceType[] clone = this.cloneFactory.DeepFieldClone(original);
|
2012-02-02 20:11:17 +00:00
|
|
|
|
|
|
|
|
|
Assert.AreNotSame(original[0], clone[0]);
|
|
|
|
|
Assert.AreEqual(original[0].TestField, clone[0].TestField);
|
|
|
|
|
Assert.AreEqual(original[0].TestProperty, clone[0].TestProperty);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Verifies that deep clones of a generic list can be made</summary>
|
|
|
|
|
[Test]
|
|
|
|
|
public void GenericListsCanBeCloned() {
|
|
|
|
|
var original = new List<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
|
2012-02-08 17:13:08 +00:00
|
|
|
|
List<int> clone = this.cloneFactory.DeepFieldClone(original);
|
2012-02-02 20:11:17 +00:00
|
|
|
|
|
|
|
|
|
CollectionAssert.AreEqual(original, clone);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Verifies that deep clones of a generic dictionary can be made</summary>
|
|
|
|
|
[Test]
|
|
|
|
|
public void GenericDictionariesCanBeCloned() {
|
|
|
|
|
var original = new Dictionary<int, string>();
|
|
|
|
|
original.Add(1, "one");
|
2012-02-08 17:13:08 +00:00
|
|
|
|
Dictionary<int, string> clone = this.cloneFactory.DeepFieldClone(original);
|
2012-02-02 20:11:17 +00:00
|
|
|
|
|
|
|
|
|
Assert.AreEqual("one", clone[1]);
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-01 22:45:15 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Verifies that a field-based shallow clone of a value type can be performed
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
|
|
|
|
public void ShallowFieldBasedClonesOfValueTypesCanBeMade() {
|
2012-02-03 12:18:37 +00:00
|
|
|
|
HierarchicalValueType original = CreateValueType();
|
2012-02-08 17:13:08 +00:00
|
|
|
|
HierarchicalValueType clone = this.cloneFactory.ShallowFieldClone(original);
|
2012-02-03 12:18:37 +00:00
|
|
|
|
VerifyClone(ref original, ref clone, isDeepClone: false, isPropertyBasedClone: false);
|
2012-02-01 22:45:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Verifies that a field-based shallow clone of a reference type can be performed
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
|
|
|
|
public void ShallowFieldBasedClonesOfReferenceTypesCanBeMade() {
|
2012-02-03 12:18:37 +00:00
|
|
|
|
HierarchicalReferenceType original = CreateReferenceType();
|
2012-02-08 17:13:08 +00:00
|
|
|
|
HierarchicalReferenceType clone = this.cloneFactory.ShallowFieldClone(original);
|
2012-02-03 12:18:37 +00:00
|
|
|
|
VerifyClone(original, clone, isDeepClone: false, isPropertyBasedClone: false);
|
2012-02-01 22:45:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Verifies that a field-based deep clone of a value type can be performed
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
|
|
|
|
public void DeepFieldBasedClonesOfValueTypesCanBeMade() {
|
2012-02-03 12:18:37 +00:00
|
|
|
|
HierarchicalValueType original = CreateValueType();
|
2012-02-08 17:13:08 +00:00
|
|
|
|
HierarchicalValueType clone = this.cloneFactory.DeepFieldClone(original);
|
2012-02-03 12:18:37 +00:00
|
|
|
|
VerifyClone(ref original, ref clone, isDeepClone: true, isPropertyBasedClone: false);
|
2012-02-01 22:45:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Verifies that a field-based deep clone of a reference type can be performed
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
|
|
|
|
public void DeepFieldBasedClonesOfReferenceTypesCanBeMade() {
|
2012-02-03 12:18:37 +00:00
|
|
|
|
HierarchicalReferenceType original = CreateReferenceType();
|
2012-02-08 17:13:08 +00:00
|
|
|
|
HierarchicalReferenceType clone = this.cloneFactory.DeepFieldClone(original);
|
2012-02-03 12:18:37 +00:00
|
|
|
|
VerifyClone(original, clone, isDeepClone: true, isPropertyBasedClone: false);
|
2012-02-01 22:45:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Verifies that a property-based shallow clone of a value type can be performed
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
|
|
|
|
public void ShallowPropertyBasedClonesOfValueTypesCanBeMade() {
|
2012-02-03 12:18:37 +00:00
|
|
|
|
HierarchicalValueType original = CreateValueType();
|
2012-02-08 17:13:08 +00:00
|
|
|
|
HierarchicalValueType clone = this.cloneFactory.ShallowPropertyClone(original);
|
2012-02-03 12:18:37 +00:00
|
|
|
|
VerifyClone(ref original, ref clone, isDeepClone: false, isPropertyBasedClone: true);
|
2012-02-01 22:45:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Verifies that a property-based shallow clone of a reference type can be performed
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
|
|
|
|
public void ShallowPropertyBasedClonesOfReferenceTypesCanBeMade() {
|
2012-02-03 12:18:37 +00:00
|
|
|
|
HierarchicalReferenceType original = CreateReferenceType();
|
2012-02-08 17:13:08 +00:00
|
|
|
|
HierarchicalReferenceType clone = this.cloneFactory.ShallowPropertyClone(original);
|
2012-02-03 12:18:37 +00:00
|
|
|
|
VerifyClone(original, clone, isDeepClone: false, isPropertyBasedClone: true);
|
2012-02-01 22:45:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Verifies that a property-based deep clone of a value type can be performed
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
|
|
|
|
public void DeepPropertyBasedClonesOfValueTypesCanBeMade() {
|
2012-02-03 12:18:37 +00:00
|
|
|
|
HierarchicalValueType original = CreateValueType();
|
2012-02-08 17:13:08 +00:00
|
|
|
|
HierarchicalValueType clone = this.cloneFactory.DeepPropertyClone(original);
|
2012-02-03 12:18:37 +00:00
|
|
|
|
VerifyClone(ref original, ref clone, isDeepClone: true, isPropertyBasedClone: true);
|
2012-02-01 22:45:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Verifies that a property-based deep clone of a reference type can be performed
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
|
|
|
|
public void DeepPropertyBasedClonesOfReferenceTypesCanBeMade() {
|
2012-02-03 12:18:37 +00:00
|
|
|
|
HierarchicalReferenceType original = CreateReferenceType();
|
2012-02-08 17:13:08 +00:00
|
|
|
|
HierarchicalReferenceType clone = this.cloneFactory.DeepPropertyClone(original);
|
2012-02-03 12:18:37 +00:00
|
|
|
|
VerifyClone(original, clone, isDeepClone: true, isPropertyBasedClone: true);
|
2012-02-01 22:45:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-02-03 12:18:37 +00:00
|
|
|
|
/// <summary>Clone factory being tested</summary>
|
|
|
|
|
private ICloneFactory cloneFactory;
|
2012-02-01 22:45:15 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Nuclex.Support.Cloning
|
|
|
|
|
|
|
|
|
|
#endif // UNITTEST
|