-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use backing fields when available (#24)
* Use backing fields when available * Use backing field for properties declared in base class * Fix couple Unit Tests + make them run in AppVeyor (hopefuly) * Fix an exception thrown when type being cloned has a shadowed property
- Loading branch information
1 parent
63a438f
commit 586157b
Showing
8 changed files
with
408 additions
and
320 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 |
---|---|---|
|
@@ -178,3 +178,4 @@ FakesAssemblies/ | |
# Roslyn | ||
*.sln.ide/ | ||
BenchmarkDotNet.Artifacts/results/ | ||
.vscode/ |
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
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
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
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 |
---|---|---|
@@ -1,13 +1,18 @@ | ||
namespace CloneExtensions.Benchmarks | ||
{ | ||
public class SimpleClass | ||
{ | ||
public int Int { get; set; } | ||
public uint UInt { get; set; } | ||
public long Long { get; set; } | ||
public ulong ULong { get; set; } | ||
public double Double { get; set; } | ||
public float Float { get; set; } | ||
public string String { get; set; } | ||
} | ||
public class SimpleClassBase | ||
{ | ||
public int BaseInt { get; set; } | ||
} | ||
|
||
public class SimpleClass : SimpleClassBase | ||
{ | ||
public int Int { get; set; } | ||
public uint UInt { get; set; } | ||
public long Long { get; set; } | ||
public ulong ULong { get; set; } | ||
public double Double { get; set; } | ||
public float Float { get; set; } | ||
public string String { 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
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 |
---|---|---|
@@ -1,90 +1,114 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using CloneExtensions.UnitTests.Base; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace CloneExtensions.UnitTests | ||
{ | ||
[TestClass] | ||
public class CollectionTests : TestBase | ||
{ | ||
[TestMethod] | ||
public void GetClone_ListOfInts_Cloned() | ||
{ | ||
var source = Enumerable.Range(0, 10).ToList(); | ||
var target = CloneFactory.GetClone(source); | ||
Assert.AreNotSame(source, target); | ||
Assert.IsTrue(source.SequenceEqual(target)); | ||
} | ||
|
||
[TestMethod] | ||
public void GetClone_ListOfIntsCloningFlagsCollectionItemsNotProvided_ItemsNotCloned() | ||
{ | ||
var source = Enumerable.Range(0, 10).ToList(); | ||
var target = CloneFactory.GetClone(source, CloningFlags.Properties); | ||
Assert.AreNotSame(source, target); | ||
Assert.AreEqual(source.Capacity, target.Capacity); | ||
Assert.AreEqual(0, target.Count); | ||
} | ||
|
||
[TestMethod] | ||
public void GetClone_ListOfClass_ReferenceEqualityReturnsFalse() | ||
{ | ||
var source = Enumerable.Range(1, 10).Select(x => new MyClass() { _field = x, Property = x }).ToList(); | ||
var target = CloneFactory.GetClone(source); | ||
Assert.AreNotSame(source, target); | ||
Assert.IsTrue(source.SequenceEqual(target)); | ||
Assert.IsFalse(source.Zip(target, (s, t) => new { s, t }).Any(x => ReferenceEquals(x.s, x.t))); | ||
} | ||
|
||
[TestMethod] | ||
public void GetClone_IListOfClass_ReferenceEqualityReturnsFalse() | ||
{ | ||
IList<MyClass> source = Enumerable.Range(1, 10).Select(x => new MyClass() { _field = x, Property = x }).ToList(); | ||
var initializers = new Dictionary<Type, Func<object, object>>() { | ||
{ typeof(IList<MyClass>), (s) => new List<MyClass>() } | ||
}; | ||
var target = CloneFactory.GetClone(source, initializers); | ||
Assert.AreNotSame(source, target); | ||
Assert.IsTrue(source.SequenceEqual(target)); | ||
Assert.IsFalse(source.Zip(target, (s, t) => new { s, t }).Any(x => ReferenceEquals(x.s, x.t))); | ||
} | ||
|
||
[TestMethod] | ||
public void GetClone_NullListOfInt_NullCloned() | ||
{ | ||
List<int> source = null; | ||
var target = CloneFactory.GetClone(source); | ||
Assert.IsNull(target); | ||
} | ||
|
||
[TestMethod] | ||
public void GetClone_Dictionary_Cloned() | ||
{ | ||
var source = new Dictionary<int, string>() { { 1, "one" }, { 2, "two" } }; | ||
var target = CloneFactory.GetClone(source); | ||
Assert.IsTrue(source.SequenceEqual(target)); | ||
} | ||
|
||
class MyClass | ||
{ | ||
public int _field; | ||
public int Property { get; set; } | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
var other = obj as MyClass; | ||
if (other == null) | ||
return false; | ||
|
||
return other._field == _field && other.Property == Property; | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
return _field.GetHashCode() ^ Property.GetHashCode(); | ||
} | ||
} | ||
} | ||
} | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using CloneExtensions.UnitTests.Base; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace CloneExtensions.UnitTests | ||
{ | ||
[TestClass] | ||
public class CollectionTests : TestBase | ||
{ | ||
[TestMethod] | ||
public void GetClone_ListOfInts_Cloned() | ||
{ | ||
var source = Enumerable.Range(0, 10).ToList(); | ||
var target = CloneFactory.GetClone(source); | ||
Assert.AreNotSame(source, target); | ||
Assert.IsTrue(source.SequenceEqual(target)); | ||
} | ||
|
||
[TestMethod] | ||
public void GetClone_ListOfIntsCloningFlagsCollectionItemsNotProvided_ItemsNotCloned() | ||
{ | ||
var source = Enumerable.Range(0, 10).ToList(); | ||
var target = CloneFactory.GetClone(source, CloningFlags.Properties); | ||
Assert.AreNotSame(source, target); | ||
Assert.AreEqual(source.Count, target.Capacity); | ||
Assert.AreEqual(0, target.Count); | ||
} | ||
|
||
[TestMethod] | ||
public void GetClone_ListOfClass_ReferenceEqualityReturnsFalse() | ||
{ | ||
var source = Enumerable.Range(1, 10).Select(x => new MyClass() { _field = x, Property = x }).ToList(); | ||
var target = CloneFactory.GetClone(source); | ||
Assert.AreNotSame(source, target); | ||
Assert.IsTrue(source.SequenceEqual(target)); | ||
Assert.IsFalse(source.Zip(target, (s, t) => new { s, t }).Any(x => ReferenceEquals(x.s, x.t))); | ||
} | ||
|
||
[TestMethod] | ||
public void GetClone_IListOfClass_ReferenceEqualityReturnsFalse() | ||
{ | ||
IList<MyClass> source = Enumerable.Range(1, 10).Select(x => new MyClass() { _field = x, Property = x }).ToList(); | ||
var initializers = new Dictionary<Type, Func<object, object>>() { | ||
{ typeof(IList<MyClass>), (s) => new List<MyClass>() } | ||
}; | ||
var target = CloneFactory.GetClone(source, initializers); | ||
Assert.AreNotSame(source, target); | ||
Assert.IsTrue(source.SequenceEqual(target)); | ||
Assert.IsFalse(source.Zip(target, (s, t) => new { s, t }).Any(x => ReferenceEquals(x.s, x.t))); | ||
} | ||
|
||
[TestMethod] | ||
public void GetClone_NullListOfInt_NullCloned() | ||
{ | ||
List<int> source = null; | ||
var target = CloneFactory.GetClone(source); | ||
Assert.IsNull(target); | ||
} | ||
|
||
[TestMethod] | ||
public void GetClone_Dictionary_Cloned() | ||
{ | ||
var source = new Dictionary<int, string>() { { 1, "one" }, { 2, "two" } }; | ||
var target = CloneFactory.GetClone(source); | ||
Assert.IsTrue(source.SequenceEqual(target)); | ||
} | ||
|
||
[TestMethod] | ||
public void GetClone_DerivedTypeWithShadowedProperty_ClonnedProperly() | ||
{ | ||
DerivedClass source = new DerivedClass() { Property = 1 }; | ||
((BaseClass)source).Property = 2; | ||
|
||
var target = CloneFactory.GetClone(source); | ||
|
||
Assert.AreEqual(1, target.Property); | ||
|
||
// TODO: Make it work ... | ||
// Assert.AreEqual(2, ((BaseClass)target).Property); | ||
} | ||
|
||
class MyClass | ||
{ | ||
public int _field; | ||
public int Property { get; set; } | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
var other = obj as MyClass; | ||
if (other == null) | ||
return false; | ||
|
||
return other._field == _field && other.Property == Property; | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
return _field.GetHashCode() ^ Property.GetHashCode(); | ||
} | ||
} | ||
|
||
class BaseClass | ||
{ | ||
public int Property { get; set; } | ||
} | ||
|
||
class DerivedClass : BaseClass | ||
{ | ||
public new int Property { get; set; } | ||
} | ||
} | ||
} |
Oops, something went wrong.