Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix XElement copy, added unit tests & vsproject #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.pdb
*.exe
**/obj/**
*.dll
*.class
**/bin/**
2 changes: 2 additions & 0 deletions ObjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Reflection;
using System.ArrayExtensions;
using System.Xml.Linq;

namespace System
{
Expand All @@ -23,6 +24,7 @@ private static Object InternalCopy(Object originalObject, IDictionary<Object, Ob
if (originalObject == null) return null;
var typeToReflect = originalObject.GetType();
if (IsPrimitive(typeToReflect)) return originalObject;
if (typeof(XElement).IsAssignableFrom(typeToReflect)) return new XElement(originalObject as XElement);
if (visited.ContainsKey(originalObject)) return visited[originalObject];
if (typeof(Delegate).IsAssignableFrom(typeToReflect)) return null;
var cloneObject = CloneMethod.Invoke(originalObject, null);
Expand Down
36 changes: 36 additions & 0 deletions Properties/AssemblyInfo.cs
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("netobjdeepcopy")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Cegekanv")]
[assembly: AssemblyProduct("netobjdeepcopy")]
[assembly: AssemblyCopyright("Copyright © Cegekanv 2014")]
[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("bfd6c69b-1477-40c5-afd2-0b8282cbed25")]

// 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")]
53 changes: 53 additions & 0 deletions netobjdeepcopy.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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>{7900459D-F2F7-461F-948A-FFE4EEFF6148}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>netobjdeepcopy</RootNamespace>
<AssemblyName>netobjdeepcopy</AssemblyName>
<TargetFrameworkVersion>v4.5</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="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="ObjectExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</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>
26 changes: 26 additions & 0 deletions netobjdeepcopy.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "netobjdeepcopy", "netobjdeepcopy.csproj", "{7900459D-F2F7-461F-948A-FFE4EEFF6148}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "unittests", "unittests\unittests.csproj", "{F3B0CDAD-046C-4023-B980-C5EA0FCE9D9B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7900459D-F2F7-461F-948A-FFE4EEFF6148}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7900459D-F2F7-461F-948A-FFE4EEFF6148}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7900459D-F2F7-461F-948A-FFE4EEFF6148}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7900459D-F2F7-461F-948A-FFE4EEFF6148}.Release|Any CPU.Build.0 = Release|Any CPU
{F3B0CDAD-046C-4023-B980-C5EA0FCE9D9B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F3B0CDAD-046C-4023-B980-C5EA0FCE9D9B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F3B0CDAD-046C-4023-B980-C5EA0FCE9D9B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F3B0CDAD-046C-4023-B980-C5EA0FCE9D9B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
4 changes: 4 additions & 0 deletions netobjdeepcopy.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/Housekeeping/UnitTestingMru/UnitTestSessionPersistentData/=01FF80D3B23A4615BC9319553B36CA53/@KeyIndexDefined">True</s:Boolean>
<s:String x:Key="/Default/Housekeeping/UnitTestingMru/UnitTestSessionPersistentData/=01FF80D3B23A4615BC9319553B36CA53/Name/@EntryValue">ObjectExtensionsTests</s:String>
<s:String x:Key="/Default/Housekeeping/UnitTestingMru/UnitTestSessionPersistentData/=01FF80D3B23A4615BC9319553B36CA53/XmlSerializedElements/@EntryValue">&lt;Session&gt;&lt;Elements&gt;&lt;UnitTestElement Provider="MSTest" Id="MsTest:unittests.ObjectExtensionsTests" type="MsTestTestClassElement" TypeName="unittests.ObjectExtensionsTests" Project="F3B0CDAD-046C-4023-B980-C5EA0FCE9D9B" /&gt;&lt;UnitTestElement Provider="MSTest" Id="MsTest:unittests.ObjectExtensionsTests.Copy_CopiesEnumerables" ParentId="MsTest:unittests.ObjectExtensionsTests" type="MsTestTestMethodElement" MethodName="Copy_CopiesEnumerables" TypeName="unittests.ObjectExtensionsTests" Project="F3B0CDAD-046C-4023-B980-C5EA0FCE9D9B" /&gt;&lt;UnitTestElement Provider="MSTest" Id="MsTest:unittests.ObjectExtensionsTests.Copy_CopiesNestedObject" ParentId="MsTest:unittests.ObjectExtensionsTests" type="MsTestTestMethodElement" MethodName="Copy_CopiesNestedObject" TypeName="unittests.ObjectExtensionsTests" Project="F3B0CDAD-046C-4023-B980-C5EA0FCE9D9B" /&gt;&lt;UnitTestElement Provider="MSTest" Id="MsTest:unittests.ObjectExtensionsTests.Copy_CopiesSingleBuiltInObjects" ParentId="MsTest:unittests.ObjectExtensionsTests" type="MsTestTestMethodElement" MethodName="Copy_CopiesSingleBuiltInObjects" TypeName="unittests.ObjectExtensionsTests" Project="F3B0CDAD-046C-4023-B980-C5EA0FCE9D9B" /&gt;&lt;UnitTestElement Provider="MSTest" Id="MsTest:unittests.ObjectExtensionsTests.Copy_CopiesSingleObject" ParentId="MsTest:unittests.ObjectExtensionsTests" type="MsTestTestMethodElement" MethodName="Copy_CopiesSingleObject" TypeName="unittests.ObjectExtensionsTests" Project="F3B0CDAD-046C-4023-B980-C5EA0FCE9D9B" /&gt;&lt;UnitTestElement Provider="MSTest" Id="MsTest:unittests.ObjectExtensionsTests.Copy_XElementWithChildren" ParentId="MsTest:unittests.ObjectExtensionsTests" type="MsTestTestMethodElement" MethodName="Copy_XElementWithChildren" TypeName="unittests.ObjectExtensionsTests" Project="F3B0CDAD-046C-4023-B980-C5EA0FCE9D9B" /&gt;&lt;/Elements&gt;&lt;/Session&gt;</s:String></wpf:ResourceDictionary>
Binary file added netobjdeepcopy.v11.suo
Binary file not shown.
94 changes: 94 additions & 0 deletions unittests/ObjectExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

#endregion

namespace unittests
{
[TestClass]
public class ObjectExtensionsTests
{
private class MySingleObject
{
public string One;
private int two;

public int Two
{
get { return two; }
set { two = value; }
}
}

private class MyNestedObject
{
public MySingleObject Single;
public string Meta;
}

[TestMethod]
public void Copy_XElementWithChildren()
{
XElement el = XElement.Parse(@"
<root>
<child attrib='wow'>hi</child>
<child attrib='yeah'>hello</child>
</root>");
XElement copied = el.Copy();

var children = copied.Elements("child").ToList();
Assert.AreEqual(2, children.Count);
Assert.AreEqual("wow", children[0].Attribute("attrib").Value);
Assert.AreEqual("hi", children[0].Value);

Assert.AreEqual("yeah", children[1].Attribute("attrib").Value);
Assert.AreEqual("hello", children[1].Value);
}

[TestMethod]
public void Copy_CopiesNestedObject()
{
MyNestedObject copied =
new MyNestedObject() {Meta = "metadata", Single = new MySingleObject() {One = "single_one"}}.Copy();

Assert.AreEqual("metadata", copied.Meta);
Assert.AreEqual("single_one", copied.Single.One);
}

[TestMethod]
public void Copy_CopiesEnumerables()
{
IList<MySingleObject> list = new List<MySingleObject>()
{
new MySingleObject() {One = "1"},
new MySingleObject() {One = "2"}
};
IList<MySingleObject> copied = list.Copy();

Assert.AreEqual(2, copied.Count);
Assert.AreEqual("1", list[0].One);
Assert.AreEqual("2", list[1].One);
}

[TestMethod]
public void Copy_CopiesSingleObject()
{
MySingleObject copied = new MySingleObject() {One = "one", Two = 2}.Copy();

Assert.AreEqual("one", copied.One);
Assert.AreEqual(2, copied.Two);
}

[TestMethod]
public void Copy_CopiesSingleBuiltInObjects()
{
Assert.AreEqual("hello there", "hello there".Copy());
Assert.AreEqual(123, 123.Copy());
}
}
}
36 changes: 36 additions & 0 deletions unittests/Properties/AssemblyInfo.cs
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("unittests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Cegekanv")]
[assembly: AssemblyProduct("unittests")]
[assembly: AssemblyCopyright("Copyright © Cegekanv 2014")]
[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("ee1ceb32-5ed9-4c74-a402-2cb2d71450f0")]

// 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")]
91 changes: 91 additions & 0 deletions unittests/unittests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{F3B0CDAD-046C-4023-B980-C5EA0FCE9D9B}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>unittests</RootNamespace>
<AssemblyName>unittests</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
</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="System" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<Choose>
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
</ItemGroup>
</When>
<Otherwise>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework" />
</ItemGroup>
</Otherwise>
</Choose>
<ItemGroup>
<Compile Include="ObjectExtensionsTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\netobjdeepcopy.csproj">
<Project>{7900459d-f2f7-461f-948a-ffe4eeff6148}</Project>
<Name>netobjdeepcopy</Name>
</ProjectReference>
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.CodedUITestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Extension, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITesting, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
</ItemGroup>
</When>
</Choose>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<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>