-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
943 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
bin | ||
obj | ||
*.user | ||
*.suo | ||
packages |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
using System.Collections.Generic; | ||
|
||
using NUnit.Framework; | ||
|
||
namespace Force.DeepCloner.Tests | ||
{ | ||
[TestFixture] | ||
public class ArraysSpec | ||
{ | ||
[Test] | ||
public void IntArray_Should_Be_Cloned() | ||
{ | ||
var arr = new[] { 1, 2, 3 }; | ||
var cloned = arr.DeepClone(); | ||
Assert.That(cloned.Length, Is.EqualTo(3)); | ||
CollectionAssert.AreEqual(arr, cloned); | ||
} | ||
|
||
[Test] | ||
public void StringArray_Should_Be_Cloned() | ||
{ | ||
var arr = new[] { "1", "2", "3" }; | ||
var cloned = arr.DeepClone(); | ||
Assert.That(cloned.Length, Is.EqualTo(3)); | ||
CollectionAssert.AreEqual(arr, cloned); | ||
} | ||
|
||
public class C1 | ||
{ | ||
public C1(int x) | ||
{ | ||
X = x; | ||
} | ||
|
||
public int X { get; set; } | ||
} | ||
|
||
[Test] | ||
public void ClassArray_Should_Be_Cloned() | ||
{ | ||
var arr = new[] { new C1(1), new C1(2) }; | ||
var cloned = arr.DeepClone(); | ||
Assert.That(cloned.Length, Is.EqualTo(2)); | ||
Assert.That(cloned[0].X, Is.EqualTo(1)); | ||
Assert.That(cloned[1].X, Is.EqualTo(2)); | ||
} | ||
|
||
public struct S1 | ||
{ | ||
public S1(int x) | ||
{ | ||
X = x; | ||
} | ||
|
||
public int X; | ||
} | ||
|
||
public struct S2 | ||
{ | ||
public C1 C; | ||
} | ||
|
||
[Test] | ||
public void StructArray_Should_Be_Cloned() | ||
{ | ||
var arr = new[] { new S1(1), new S1(2) }; | ||
var cloned = arr.DeepClone(); | ||
Assert.That(cloned.Length, Is.EqualTo(2)); | ||
Assert.That(cloned[0].X, Is.EqualTo(1)); | ||
Assert.That(cloned[1].X, Is.EqualTo(2)); | ||
} | ||
|
||
[Test] | ||
public void StructArray_With_Class_Should_Be_Cloned() | ||
{ | ||
var arr = new[] { new S2 { C = new C1(1) }, new S2 { C = new C1(2) } }; | ||
var cloned = arr.DeepClone(); | ||
Assert.That(cloned.Length, Is.EqualTo(2)); | ||
Assert.That(cloned[0].C.X, Is.EqualTo(1)); | ||
Assert.That(cloned[1].C.X, Is.EqualTo(2)); | ||
} | ||
|
||
[Test] | ||
public void IntList_Should_Be_Cloned() | ||
{ | ||
// TODO: better performance for this type | ||
var arr = new List<int> { 1, 2, 3 }; | ||
var cloned = arr.DeepClone(); | ||
Assert.That(cloned.Count, Is.EqualTo(3)); | ||
Assert.That(cloned[0], Is.EqualTo(1)); | ||
Assert.That(cloned[1], Is.EqualTo(2)); | ||
Assert.That(cloned[2], Is.EqualTo(3)); | ||
} | ||
|
||
[Test] | ||
public void Dictionary_Should_Be_Cloned() | ||
{ | ||
// TODO: better performance for this type | ||
var d = new Dictionary<string, decimal>(); | ||
d["a"] = 1; | ||
d["b"] = 2; | ||
var cloned = d.DeepClone(); | ||
Assert.That(cloned.Count, Is.EqualTo(2)); | ||
Assert.That(cloned["a"], Is.EqualTo(1)); | ||
Assert.That(cloned["b"], Is.EqualTo(2)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using NUnit.Framework; | ||
|
||
namespace Force.DeepCloner.Tests | ||
{ | ||
[TestFixture] | ||
public class ConstructorsSpec | ||
{ | ||
public class T1 | ||
{ | ||
private T1() | ||
{ | ||
} | ||
|
||
public static T1 Create() | ||
{ | ||
return new T1(); | ||
} | ||
|
||
public int X { get; set; } | ||
} | ||
|
||
public class T2 | ||
{ | ||
public T2(int arg1, int arg2) | ||
{ | ||
} | ||
|
||
public int X { get; set; } | ||
} | ||
|
||
[Test] | ||
public void Object_With_Private_Constructor_Should_Be_Cloned() | ||
{ | ||
var t1 = T1.Create(); | ||
t1.X = 42; | ||
var cloned = t1.DeepClone(); | ||
t1.X = 0; | ||
Assert.That(cloned.X, Is.EqualTo(42)); | ||
} | ||
|
||
[Test] | ||
public void Object_With_Complex_Constructor_Should_Be_Cloned() | ||
{ | ||
var t2 = new T2(1, 2); | ||
t2.X = 42; | ||
var cloned = t2.DeepClone(); | ||
t2.X = 0; | ||
Assert.That(cloned.X, Is.EqualTo(42)); | ||
} | ||
|
||
[Test] | ||
public void Anonymous_Object_Should_Be_Cloned() | ||
{ | ||
var t2 = new { A = 1, B = "x" }; | ||
var cloned = t2.DeepClone(); | ||
Assert.That(cloned.A, Is.EqualTo(1)); | ||
Assert.That(cloned.B, Is.EqualTo("x")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{3453CB0A-BE66-4770-B92E-7A5917AD56A1}</ProjectGuid> | ||
<OutputType>Library</OutputType> | ||
<AppDesignerFolder>Properties</AppDesignerFolder> | ||
<RootNamespace>Force.DeepCloner.Tests</RootNamespace> | ||
<AssemblyName>Force.DeepCloner.Tests</AssemblyName> | ||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="nunit.framework"> | ||
<HintPath>..\packages\NUnit.3.0.1\lib\net40\nunit.framework.dll</HintPath> | ||
</Reference> | ||
<Reference Include="System" /> | ||
<Reference Include="System.Core" /> | ||
<Reference Include="System.Xml.Linq" /> | ||
<Reference Include="System.Data.DataSetExtensions" /> | ||
<Reference Include="Microsoft.CSharp" /> | ||
<Reference Include="System.Data" /> | ||
<Reference Include="System.Xml" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="ArraysSpec.cs" /> | ||
<Compile Include="ConstructorsSpec.cs" /> | ||
<Compile Include="LoopCheckSpec.cs" /> | ||
<Compile Include="Objects\TestObject1.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
<Compile Include="SimpleObjectSpec.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="packages.config" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\DeepCloner\DeepCloner.csproj"> | ||
<Project>{6BB0A0AB-67F9-45E4-B60C-4CEED098D463}</Project> | ||
<Name>DeepCloner</Name> | ||
</ProjectReference> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. | ||
Other similar extension points exist, see Microsoft.Common.targets. | ||
<Target Name="BeforeBuild"> | ||
</Target> | ||
<Target Name="AfterBuild"> | ||
</Target> | ||
--> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using NUnit.Framework; | ||
|
||
namespace Force.DeepCloner.Tests | ||
{ | ||
[TestFixture] | ||
public class LoopCheckSpec | ||
{ | ||
public class C1 | ||
{ | ||
public int F { get; set; } | ||
|
||
public C1 A { get; set; } | ||
} | ||
|
||
[Test] | ||
public void SimpleLoop_Should_Be_Handled() | ||
{ | ||
var c1 = new C1(); | ||
var c2 = new C1(); | ||
c1.F = 1; | ||
c2.F = 2; | ||
c1.A = c2; | ||
c1.A.A = c1; | ||
var cloned = c1.DeepClone(); | ||
|
||
Assert.That(cloned.A, Is.Not.Null); | ||
Assert.That(cloned.A.A.F, Is.EqualTo(cloned.F)); | ||
Assert.That(cloned.A.A, Is.EqualTo(cloned)); | ||
} | ||
|
||
[Test] | ||
public void Object_Own_Loop_Should_Be_Handled() | ||
{ | ||
var c1 = new C1(); | ||
c1.F = 1; | ||
c1.A = c1; | ||
var cloned = c1.DeepClone(); | ||
|
||
Assert.That(cloned.A, Is.Not.Null); | ||
Assert.That(cloned.A.F, Is.EqualTo(cloned.F)); | ||
Assert.That(cloned.A, Is.EqualTo(cloned)); | ||
} | ||
|
||
[Test] | ||
public void Array_Of_Same_Objects_Should_Be_Cloned() | ||
{ | ||
var c1 = new C1(); | ||
var arr = new[] { c1, c1, c1 }; | ||
c1.F = 1; | ||
var cloned = arr.DeepClone(); | ||
|
||
Assert.That(cloned.Length, Is.EqualTo(3)); | ||
Assert.That(cloned[0], Is.EqualTo(cloned[1])); | ||
Assert.That(cloned[1], Is.EqualTo(cloned[2])); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
|
||
namespace Force.DeepCloner.Tests.Objects | ||
{ | ||
public class TestObject1 | ||
{ | ||
public byte Byte { get; set; } | ||
|
||
public short Short { get; set; } | ||
|
||
public ushort UShort { get; set; } | ||
|
||
public int Int { get; set; } | ||
|
||
public uint UInt { get; set; } | ||
|
||
public long Long { get; set; } | ||
|
||
public ulong ULong { get; set; } | ||
|
||
public float Float { get; set; } | ||
|
||
public double Double { get; set; } | ||
|
||
public decimal Decimal { get; set; } | ||
|
||
public char Char { get; set; } | ||
|
||
public string String { get; set; } | ||
|
||
public DateTime DateTime { get; set; } | ||
|
||
public bool Bool { get; set; } | ||
|
||
public IntPtr IntPtr { get; set; } | ||
|
||
public UIntPtr UIntPtr { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyTitle("DeepCloner.Tests")] | ||
[assembly: AssemblyDescription("")] | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("DeepCloner.Tests")] | ||
[assembly: AssemblyCopyright("Copyright © 2016")] | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
// The following GUID is for the ID of the typelib if this project is exposed to COM | ||
[assembly: Guid("68714d9b-2471-49e2-b720-3c9b47e14a16")] | ||
|
||
// Version information for an assembly consists of the following four values: | ||
// | ||
// Major Version | ||
// Minor Version | ||
// Build Number | ||
// Revision | ||
// | ||
// You can specify all the values or you can default the Build and Revision Numbers | ||
// by using the '*' as shown below: | ||
// [assembly: AssemblyVersion("1.0.*")] | ||
[assembly: AssemblyVersion("1.0.0.0")] | ||
[assembly: AssemblyFileVersion("1.0.0.0")] |
Oops, something went wrong.