-
Notifications
You must be signed in to change notification settings - Fork 0
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
29 changed files
with
1,400 additions
and
11 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
15 changes: 15 additions & 0 deletions
15
src/SolidStack.Core.Equality.Testing/AssertionExtensions.cs
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,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace SolidStack.Core.Equality.Testing | ||
{ | ||
public static class AssertionExtensions | ||
{ | ||
public static EqualityComparerAssertions<T> Should<T>(this IEqualityComparer<T> equalityComparer) | ||
where T : class => | ||
new EqualityComparerAssertions<T>(equalityComparer); | ||
|
||
public static EquatableAssertions<T> Should<T>(this IEquatable<T> equatable) => | ||
new EquatableAssertions<T>(equatable); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
src/SolidStack.Core.Equality.Testing/EqualityComparerAssertions.cs
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,79 @@ | ||
using System.Collections.Generic; | ||
using FluentAssertions; | ||
using FluentAssertions.Execution; | ||
using Moq; | ||
|
||
namespace SolidStack.Core.Equality.Testing | ||
{ | ||
public class EqualityComparerAssertions<T> | ||
where T : class | ||
{ | ||
public EqualityComparerAssertions(IEqualityComparer<T> equalityComparer) => | ||
Subject = equalityComparer; | ||
|
||
public IEqualityComparer<T> Subject { get; protected set; } | ||
|
||
public AndConstraint<EqualityComparerAssertions<T>> HandleBasicEqualitiesAndInequalites( | ||
string because = "", params object[] becauseArgs) | ||
{ | ||
Execute.Assertion | ||
.BecauseOf(because, becauseArgs) | ||
.Given(Mock.Of<T>) | ||
.ForCondition(dummy => Subject.Equals(dummy, dummy)) | ||
.FailWith( | ||
"Expected {context:comparer} to evaluate the equality of the same object as {0}{reason}, but found {1}.", | ||
true, false) | ||
.Then | ||
.ForCondition(dummy => !Subject.Equals(null, dummy) && !Subject.Equals(dummy, null)) | ||
.FailWith( | ||
"Expected {context:comparer} to evaluate the equality of null and a non-null object as {0}{reason}, but found {1}.", | ||
false, true); | ||
|
||
return new AndConstraint<EqualityComparerAssertions<T>>(this); | ||
} | ||
|
||
public AndConstraint<EqualityComparerAssertions<T>> InvalidateEqualityOf( | ||
T x, T y, string because = "", params object[] becauseArgs) | ||
{ | ||
var xCopy = x; | ||
var yCopy = y; | ||
|
||
Execute.Assertion | ||
.BecauseOf(because, becauseArgs) | ||
.ForCondition(!Subject.Equals(x, y)) | ||
.FailWith( | ||
"Expected {context:comparer} to evaluate the equality of {0} and {1} as {2}{reason}, but found {3}.", | ||
x, y, false, true) | ||
.Then | ||
.Given(() => new[] {Subject.GetHashCode(x), Subject.GetHashCode(y)}) | ||
.ForCondition(hashCodes => hashCodes[0] != hashCodes[1]) | ||
.FailWith( | ||
"Expected {context:comparer} to return different hash codes for {0} and {1}{reason}, but found {2} and {3}.", | ||
_ => xCopy, _ => yCopy, hashCodes => hashCodes[0], hashCodes => hashCodes[1]); | ||
|
||
return new AndConstraint<EqualityComparerAssertions<T>>(this); | ||
} | ||
|
||
public AndConstraint<EqualityComparerAssertions<T>> ValidateEqualityOf( | ||
T x, T y, string because = "", params object[] becauseArgs) | ||
{ | ||
var xCopy = x; | ||
var yCopy = y; | ||
|
||
Execute.Assertion | ||
.BecauseOf(because, becauseArgs) | ||
.ForCondition(Subject.Equals(x, y)) | ||
.FailWith( | ||
"Expected {context:comparer} to evaluate the equality of {0} and {1} as {2}{reason}, but found {3}.", | ||
x, y, true, false) | ||
.Then | ||
.Given(() => new[] {Subject.GetHashCode(x), Subject.GetHashCode(y)}) | ||
.ForCondition(hashCodes => hashCodes[0] == hashCodes[1]) | ||
.FailWith( | ||
"Expected {context:comparer} to return the same hash code for {0} and {1}{reason}, but found {2} and {3}.", | ||
_ => xCopy, _ => yCopy, hashCodes => hashCodes[0], hashCodes => hashCodes[1]); | ||
|
||
return new AndConstraint<EqualityComparerAssertions<T>>(this); | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/SolidStack.Core.Equality.Testing/EquatableAssertions.cs
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,63 @@ | ||
using System; | ||
using System.Reflection; | ||
using FluentAssertions; | ||
using FluentAssertions.Execution; | ||
|
||
namespace SolidStack.Core.Equality.Testing | ||
{ | ||
public class EquatableAssertions<T> | ||
{ | ||
public EquatableAssertions(IEquatable<T> equatable) => | ||
Subject = equatable; | ||
|
||
public IEquatable<T> Subject { get; protected set; } | ||
|
||
private TypeInfo SubjectType => | ||
Subject.GetType().GetTypeInfo(); | ||
|
||
public AndConstraint<EquatableAssertions<T>> BeTypeSealed( | ||
string because = "", params object[] becauseArgs) | ||
{ | ||
Execute.Assertion | ||
.BecauseOf(because, becauseArgs) | ||
.ForCondition(Subject.GetType().IsSealed) | ||
.FailWith($"Expected {{context:{Subject.GetType().Name}}} to be type sealed{{reason}}."); | ||
|
||
return new AndConstraint<EquatableAssertions<T>>(this); | ||
} | ||
|
||
public AndConstraint<EquatableAssertions<T>> OverrideEquality( | ||
string because = "", params object[] becauseArgs) | ||
{ | ||
Execute.Assertion | ||
.BecauseOf(because, becauseArgs) | ||
.ForCondition(OverridesMethod("Equals", new []{typeof(object)})) | ||
.FailWith($"Expected {{context:{Subject.GetType().Name}}} to override Equals(object){{reason}}.", Subject.GetType()) | ||
.Then | ||
.ForCondition(OverridesMethod("GetHashCode", Type.EmptyTypes)) | ||
.FailWith($"Expected {{context:{Subject.GetType().Name}}} to override GetHashCode(){{reason}}.", Subject.GetType()) | ||
.Then | ||
.ForCondition(OverridesOperator("op_Equality")) | ||
.FailWith($"Expected {{context:{Subject.GetType().Name}}} to override equality operator{{reason}}.", Subject.GetType()) | ||
.Then | ||
.ForCondition(OverridesOperator("op_Inequality")) | ||
.FailWith($"Expected {{context:{Subject.GetType().Name}}} to override inequality operator{{reason}}.", Subject.GetType()); | ||
|
||
return new AndConstraint<EquatableAssertions<T>>(this); | ||
} | ||
|
||
private bool OverridesMethod(string methodName, Type[] types) => | ||
SubjectType | ||
.GetMethod(methodName, types) | ||
?.DeclaringType != typeof(object); | ||
|
||
private bool OverridesOperator(string operatorName) => | ||
SubjectType | ||
.GetMethod( | ||
operatorName, | ||
BindingFlags.Instance | | ||
BindingFlags.Static | | ||
BindingFlags.Public | | ||
BindingFlags.FlattenHierarchy) != null; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/SolidStack.Core.Equality.Testing/SolidStack.Core.Equality.Testing.csproj
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,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FluentAssertions" Version="5.3.2" /> | ||
<PackageReference Include="Moq" Version="4.8.2" /> | ||
</ItemGroup> | ||
|
||
</Project> |
22 changes: 22 additions & 0 deletions
22
src/SolidStack.Core.Equality.Tests/Doubles/DummyByDynamicMembersEqualityComparable.cs
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,22 @@ | ||
namespace SolidStack.Core.Equality.Tests.Doubles | ||
{ | ||
public class DummyByDynamicMembersEqualityComparable | ||
{ | ||
protected dynamic FieldA; | ||
|
||
public DummyByDynamicMembersEqualityComparable(dynamic fieldA, dynamic propertyA) | ||
{ | ||
FieldA = fieldA; | ||
PropertyA = propertyA; | ||
} | ||
|
||
/// <summary> | ||
/// Constructor used to instantiate the class via reflection. | ||
/// </summary> | ||
public DummyByDynamicMembersEqualityComparable() | ||
{ | ||
} | ||
|
||
public dynamic PropertyA { get; } | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/SolidStack.Core.Equality.Tests/Doubles/DummyByKeyEqualityComparable.cs
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,17 @@ | ||
namespace SolidStack.Core.Equality.Tests.Doubles | ||
{ | ||
public class DummyByKeyEqualityComparable | ||
{ | ||
public DummyByKeyEqualityComparable(string id) => | ||
Id = id; | ||
|
||
/// <summary> | ||
/// Constructor used to instantiate the class via reflection. | ||
/// </summary> | ||
public DummyByKeyEqualityComparable() | ||
{ | ||
} | ||
|
||
public string Id { get; } | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/SolidStack.Core.Equality.Tests/Doubles/DummyByKeyEqualityComparableChild.cs
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,18 @@ | ||
namespace SolidStack.Core.Equality.Tests.Doubles | ||
{ | ||
public class DummyByKeyEqualityComparableChild : DummyByKeyEqualityComparable | ||
{ | ||
public DummyByKeyEqualityComparableChild(string id) : | ||
base(id) | ||
{ | ||
} | ||
|
||
/// <inheritdoc /> | ||
/// <summary> | ||
/// Constructor used to instantiate the class via reflection. | ||
/// </summary> | ||
public DummyByKeyEqualityComparableChild() | ||
{ | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/SolidStack.Core.Equality.Tests/Doubles/DummyByMembersEqualityComparable.cs
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,33 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace SolidStack.Core.Equality.Tests.Doubles | ||
{ | ||
public class DummyByMembersEqualityComparable | ||
{ | ||
public int FieldA; | ||
|
||
protected IEnumerable<char> FieldB; | ||
|
||
public DummyByMembersEqualityComparable( | ||
int fieldA, IEnumerable<char> fieldB, | ||
DateTime propertyA, IEnumerable<bool> propertyB) | ||
{ | ||
FieldA = fieldA; | ||
FieldB = fieldB; | ||
PropertyA = propertyA; | ||
PropertyB = propertyB; | ||
} | ||
|
||
/// <summary> | ||
/// Constructor used to instantiate the class via reflection. | ||
/// </summary> | ||
public DummyByMembersEqualityComparable() | ||
{ | ||
} | ||
|
||
public DateTime PropertyA { get; } | ||
|
||
protected IEnumerable<bool> PropertyB { get; set; } | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/SolidStack.Core.Equality.Tests/Doubles/DummyByMembersEqualityComparableChild.cs
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,29 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace SolidStack.Core.Equality.Tests.Doubles | ||
{ | ||
public class DummyByMembersEqualityComparableChild : DummyByMembersEqualityComparable | ||
{ | ||
protected string FieldC; | ||
|
||
public DummyByMembersEqualityComparableChild( | ||
int fieldA, IEnumerable<char> fieldB, string fieldC, | ||
DateTime propertyA, IEnumerable<bool> propertyB, bool propertyC) : | ||
base(fieldA, fieldB, propertyA, propertyB) | ||
{ | ||
FieldC = fieldC; | ||
PropertyC = propertyC; | ||
} | ||
|
||
/// <inheritdoc /> | ||
/// <summary> | ||
/// Constructor used to instantiate the class via reflection. | ||
/// </summary> | ||
public DummyByMembersEqualityComparableChild() | ||
{ | ||
} | ||
|
||
public bool PropertyC { get; set; } | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/SolidStack.Core.Equality.Tests/Doubles/DummyByTaggedMembersEqualityComparable.cs
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,30 @@ | ||
namespace SolidStack.Core.Equality.Tests.Doubles | ||
{ | ||
public class DummyByTaggedMembersEqualityComparable | ||
{ | ||
[EqualityMember] | ||
public int FieldA; | ||
|
||
protected string FieldB; | ||
|
||
public DummyByTaggedMembersEqualityComparable(int fieldA, string fieldB, bool propertyA, char propertyB) | ||
{ | ||
FieldA = fieldA; | ||
FieldB = fieldB; | ||
PropertyA = propertyA; | ||
PropertyB = propertyB; | ||
} | ||
|
||
/// <summary> | ||
/// Constructor used to instantiate the class via reflection. | ||
/// </summary> | ||
public DummyByTaggedMembersEqualityComparable() | ||
{ | ||
} | ||
|
||
public bool PropertyA { get; } | ||
|
||
[EqualityMember] | ||
protected char PropertyB { get; set; } | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/SolidStack.Core.Equality.Tests/Doubles/EquatableStub.cs
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,16 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace SolidStack.Core.Equality.Tests.Doubles | ||
{ | ||
public sealed class EquatableStub : Equatable<EquatableStub> | ||
{ | ||
public EquatableStub(Func<IEqualityComparer<EquatableStub>> equalityComparerAccessor) => | ||
EqualityComparerAccessor = equalityComparerAccessor; | ||
|
||
private Func<IEqualityComparer<EquatableStub>> EqualityComparerAccessor { get; } | ||
|
||
protected override IEqualityComparer<EquatableStub> GetEqualityComparer() => | ||
EqualityComparerAccessor(); | ||
} | ||
} |
Oops, something went wrong.