Skip to content

Commit

Permalink
Fix bug when passing an explicit comparer or immutable type.
Browse files Browse the repository at this point in the history
- Refactor recursive verify to share logic for fields and properties.
  • Loading branch information
JohanLarsson committed Jun 8, 2016
1 parent 502aebe commit ee5224e
Show file tree
Hide file tree
Showing 85 changed files with 981 additions and 618 deletions.
24 changes: 23 additions & 1 deletion Gu.State.Tests/CopyTests/ClassesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ namespace Gu.State.Tests.CopyTests

public abstract class ClassesTests
{
public abstract void CopyMethod<T>(T source, T target, ReferenceHandling referenceHandling = ReferenceHandling.Structural, string excluded = null) where T : class;
public abstract void CopyMethod<T>(
T source,
T target,
ReferenceHandling referenceHandling = ReferenceHandling.Structural,
string excluded = null,
Type ignoredType = null,
Type immutableType = null) where T : class;

[TestCase(ReferenceHandling.Throw)]
[TestCase(ReferenceHandling.Structural)]
Expand All @@ -29,6 +35,22 @@ public void WithSimpleHappyPath(ReferenceHandling referenceHandling)
Assert.AreEqual(StringSplitOptions.RemoveEmptyEntries, target.EnumValue);
}

[Test]
public void WithExplitImmutable()
{
var source = new WithComplexProperty
{
Name = "a",
Value = 1,
ComplexType = new ComplexType { Name = "b", Value = 2 }
};
var target = new WithComplexProperty();
this.CopyMethod(source, target, ReferenceHandling.Structural, immutableType: typeof(ComplexType));
Assert.AreEqual(source.Name, target.Name);
Assert.AreEqual(source.Value, target.Value);
Assert.AreSame(source.ComplexType, target.ComplexType);
}

[Test]
public void WithComplexStructuralHappyPath()
{
Expand Down
18 changes: 18 additions & 0 deletions Gu.State.Tests/CopyTests/CopyTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,24 @@ public With(T value)
public T Value { get; set; }
}

public class IntCollection : IReadOnlyList<int>
{
private readonly IReadOnlyList<int> ints;

public IntCollection(params int[] ints)
{
this.ints = ints;
}

public int Count => this.ints.Count;

public int this[int index] => this.ints[index];

public IEnumerator<int> GetEnumerator() => this.ints.GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();
}

public class WithReadonlyField
{
public readonly int ReadonlyValue;
Expand Down
21 changes: 16 additions & 5 deletions Gu.State.Tests/CopyTests/FieldValues/Classes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,29 @@

public class Classes : ClassesTests
{
public override void CopyMethod<T>(T source, T target, ReferenceHandling referenceHandling = ReferenceHandling.Structural, string excluded = null)
public override void CopyMethod<T>(
T source,
T target,
ReferenceHandling referenceHandling = ReferenceHandling.Structural,
string excluded = null,
Type ignoredType = null,
Type immutableType = null)
{
var builder = FieldsSettings.Build();
if (excluded != null)
{
builder.AddIgnoredField<T>(excluded);
}

//if (excludedType != null)
//{
// builder.IgnoreType(excludedType);
//}
if (ignoredType != null)
{
builder.IgnoreType(ignoredType);
}

if (immutableType != null)
{
builder.AddImmutableType(immutableType);
}

var settings = builder.CreateSettings(referenceHandling);
Copy.FieldValues(source, target, settings);
Expand Down
26 changes: 24 additions & 2 deletions Gu.State.Tests/CopyTests/FieldValues/Verify.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// ReSharper disable RedundantArgumentDefaultValue
namespace Gu.State.Tests.CopyTests.FieldValues
{
using System;
using System.Reflection;

using NUnit.Framework;
Expand All @@ -12,9 +13,30 @@ public override void VerifyMethod<T>()
Copy.VerifyCanCopyFieldValues<T>();
}

public override void VerifyMethod<T>(ReferenceHandling referenceHandling)
public override void VerifyMethod<T>(
ReferenceHandling referenceHandling,
string excludedMembers= null,
Type ignoredType = null,
Type immutableType = null)
{
Copy.VerifyCanCopyFieldValues<T>(referenceHandling);
var builder = FieldsSettings.Build();
if (excludedMembers != null)
{
builder.AddIgnoredField<T>(excludedMembers);
}

if (ignoredType != null)
{
builder.AddImmutableType(ignoredType);
}

if (immutableType != null)
{
builder.AddImmutableType(immutableType);
}

var settings = builder.CreateSettings(referenceHandling);
Copy.VerifyCanCopyFieldValues<T>(settings);
}

[Test]
Expand Down
23 changes: 18 additions & 5 deletions Gu.State.Tests/CopyTests/PropertyValues/Classes.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
namespace Gu.State.Tests.CopyTests.PropertyValues
{
using System;

using NUnit.Framework;

using static CopyTypes;

public class Classes : ClassesTests
{
public override void CopyMethod<T>(T source, T target, ReferenceHandling referenceHandling = ReferenceHandling.Structural, string excluded = null)
public override void CopyMethod<T>(
T source,
T target,
ReferenceHandling referenceHandling = ReferenceHandling.Structural,
string excluded = null,
Type ignoredType = null,
Type immutableType = null)
{
var builder = PropertiesSettings.Build();
if (excluded != null)
{
builder.IgnoreProperty<T>(excluded);
}

//if (excludedType != null)
//{
// builder.IgnoreType(excludedType);
//}
if (ignoredType != null)
{
builder.IgnoreType(ignoredType);
}

if (immutableType != null)
{
builder.AddImmutableType(immutableType);
}

var settings = builder.CreateSettings(referenceHandling);
Copy.PropertyValues(source, target, settings);
Expand Down
28 changes: 26 additions & 2 deletions Gu.State.Tests/CopyTests/PropertyValues/Verify.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
namespace Gu.State.Tests.CopyTests.PropertyValues
{
using System;
using System.Reflection;

public class Verify : VerifyTests
{
public override void VerifyMethod<T>()
{
Copy.VerifyCanCopyPropertyValues<T>();
}

public override void VerifyMethod<T>(ReferenceHandling referenceHandling)
public override void VerifyMethod<T>(
ReferenceHandling referenceHandling,
string excludedMembers = null,
Type ignoredType = null,
Type immutableType = null)
{
Copy.VerifyCanCopyPropertyValues<T>(referenceHandling);
var builder = PropertiesSettings.Build();
if (excludedMembers != null)
{
builder.IgnoreProperty<T>(excludedMembers);
}

if (ignoredType != null)
{
builder.AddImmutableType(ignoredType);
}

if (immutableType != null)
{
builder.AddImmutableType(immutableType);
}

var settings = builder.CreateSettings(referenceHandling);
Copy.VerifyCanCopyPropertyValues<T>(settings);
}
}
}
13 changes: 12 additions & 1 deletion Gu.State.Tests/CopyTests/VerifyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ public abstract class VerifyTests
{
public abstract void VerifyMethod<T>() where T : class;

public abstract void VerifyMethod<T>(ReferenceHandling referenceHandling) where T : class;
public abstract void VerifyMethod<T>(
ReferenceHandling referenceHandling,
string excludedMembers = null,
Type ignoredType = null,
Type immutableType = null) where T : class;

[TestCase(ReferenceHandling.Throw)]
[TestCase(ReferenceHandling.References)]
Expand Down Expand Up @@ -207,5 +211,12 @@ public void DetectsReferenceLoop()
Assert.DoesNotThrow(() => this.VerifyMethod<Parent>(ReferenceHandling.Structural));
Assert.DoesNotThrow(() => this.VerifyMethod<Parent>(ReferenceHandling.References));
}

[TestCase(ReferenceHandling.References)]
[TestCase(ReferenceHandling.Structural)]
public void WithExplicitImmutable(ReferenceHandling referenceHandling)
{
this.VerifyMethod<With<IntCollection>>(referenceHandling, immutableType: typeof(IntCollection));
}
}
}
5 changes: 3 additions & 2 deletions Gu.State.Tests/DiffTests/ClassesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public abstract Diff DiffMethod<T>(
T y,
ReferenceHandling referenceHandling = ReferenceHandling.Structural,
string excludedMembers = null,
Type excludedType = null) where T : class;
Type ignoredType = null,
Type immutableType = null) where T : class;

[TestCase("b", "b", "Empty")]
[TestCase("b", "c", "WithSimpleProperties <member> x: b y: c")]
Expand Down Expand Up @@ -352,7 +353,7 @@ this is FieldValues.Classes
x,
y,
ReferenceHandling.Structural,
excludedType: typeof(ComplexType));
ignoredType: typeof(ComplexType));
Assert.AreEqual(expected, result.ToString("", " "));
}
}
Expand Down
17 changes: 14 additions & 3 deletions Gu.State.Tests/DiffTests/FieldValues/Classes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,28 @@

public class Classes : ClassesTests
{
public override Diff DiffMethod<T>(T x, T y, ReferenceHandling referenceHandling = ReferenceHandling.Structural, string excludedMembers = null, Type excludedType = null)
public override Diff DiffMethod<T>(
T x,
T y,
ReferenceHandling referenceHandling = ReferenceHandling.Structural,
string excludedMembers = null,
Type ignoredType = null,
Type immutableType = null)
{
var builder = FieldsSettings.Build();
if (excludedMembers != null)
{
builder.AddIgnoredField<T>(excludedMembers);
}

if (excludedType != null)
if (ignoredType != null)
{
builder.AddImmutableType(excludedType);
builder.IgnoreType(ignoredType);
}

if (immutableType != null)
{
builder.AddImmutableType(immutableType);
}

var settings = builder.CreateSettings(referenceHandling);
Expand Down
17 changes: 14 additions & 3 deletions Gu.State.Tests/DiffTests/PropertyValues/Classes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,28 @@

public class Classes : ClassesTests
{
public override Diff DiffMethod<T>(T x, T y, ReferenceHandling referenceHandling = ReferenceHandling.Structural, string excludedMembers = null, Type excludedType = null)
public override Diff DiffMethod<T>(
T x,
T y,
ReferenceHandling referenceHandling = ReferenceHandling.Structural,
string excludedMembers = null,
Type ignoredType = null,
Type immutableType = null)
{
var builder = PropertiesSettings.Build();
if (excludedMembers != null)
{
builder.IgnoreProperty<T>(excludedMembers);
}

if (excludedType != null)
if (ignoredType != null)
{
builder.IgnoreType(excludedType);
builder.IgnoreType(ignoredType);
}

if (immutableType != null)
{
builder.AddImmutableType(immutableType);
}

var settings = builder.CreateSettings(referenceHandling);
Expand Down
8 changes: 4 additions & 4 deletions Gu.State.Tests/EqualByTests/ClassesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public abstract bool EqualMethod<T>(
T y,
ReferenceHandling referenceHandling = ReferenceHandling.Structural,
string excludedMembers = null,
Type excludedType = null) where T : class;
Type ignoredType = null) where T : class;

public static IReadOnlyList<EqualByTestsShared.EqualsData> EqualsSource => EqualByTestsShared.EqualsSource;

Expand Down Expand Up @@ -279,9 +279,9 @@ public void IgnoresMember(int xv, int yv, ReferenceHandling? referenceHandling,
[TestCase("b", false)]
public void IgnoresType(string xv, bool expected)
{
var x = new EqualByTypes.WithComplexProperty(xv, 1, new EqualByTypes.ComplexType("b", 2));
var y = new EqualByTypes.WithComplexProperty("a", 1, new EqualByTypes.ComplexType("c", 2));
var result = this.EqualMethod(x, y, ReferenceHandling.Structural, excludedType: typeof(EqualByTypes.ComplexType));
var x = new WithComplexProperty(xv, 1, new ComplexType("b", 2));
var y = new WithComplexProperty("a", 1, new ComplexType("c", 2));
var result = this.EqualMethod(x, y, ReferenceHandling.Structural, ignoredType: typeof(ComplexType));
Assert.AreEqual(expected, result);
}
}
Expand Down
21 changes: 21 additions & 0 deletions Gu.State.Tests/EqualByTests/CollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;

using Moq;

using NUnit.Framework;

using static EqualByTypes;
Expand All @@ -12,6 +14,25 @@ public abstract class CollectionTests
{
public abstract bool EqualByMethod<T>(T source, T target, ReferenceHandling referenceHandling) where T : class;

[TestCase(ReferenceHandling.References)]
[TestCase(ReferenceHandling.Structural)]
public void WithEquatableIntCollection(ReferenceHandling referenceHandling)
{
var x = new With<EquatableIntCollection>(new EquatableIntCollection(1));
var y = new With<EquatableIntCollection>(new EquatableIntCollection(1));
var result = this.EqualByMethod(x, y, referenceHandling);
Assert.AreEqual(true, result);
}

[Test]
public void WithIntCollectionReferences()
{
var x = new With<IntCollection>(new IntCollection(1));
var y = new With<IntCollection>(new IntCollection(1));
var result = this.EqualByMethod(x, y, ReferenceHandling.References);
Assert.AreEqual(false, result);
}

[TestCase(ReferenceHandling.Throw)]
[TestCase(ReferenceHandling.References)]
[TestCase(ReferenceHandling.Structural)]
Expand Down
Loading

0 comments on commit ee5224e

Please sign in to comment.