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
10 changed files
with
549 additions
and
215 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
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,65 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using AutoFixture.Kernel; | ||
|
||
namespace AutoFixture.Idioms | ||
{ | ||
/// <summary> | ||
/// A base class for building classes that encapsulate a unit test that verifies that a type implementing <see cref="IEqualityComparer{T}"/> implements it correctly. | ||
/// </summary> | ||
public abstract class EqualityComparerEqualsAssertion : IdiomaticAssertion | ||
{ | ||
/// <summary> | ||
/// Gets the builder supplied by the constructor. | ||
/// </summary> | ||
public ISpecimenBuilder Builder { get; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="EqualityComparerEqualsAssertion"/> class. | ||
/// </summary> | ||
/// <param name="builder"> | ||
/// A composer which can create instances required to implement the idiomatic unit test, | ||
/// such as the owner of the property, as well as the value to be assigned and read from | ||
/// the member. | ||
/// </param> | ||
/// <remarks> | ||
/// <para> | ||
/// <paramref name="builder" /> will typically be a <see cref="Fixture" /> instance. | ||
/// </para> | ||
/// </remarks> | ||
protected EqualityComparerEqualsAssertion(ISpecimenBuilder builder) | ||
{ | ||
this.Builder = builder ?? throw new ArgumentNullException(nameof(builder)); | ||
} | ||
|
||
/// <summary> | ||
/// Forwards the call to <see cref="VerifyEquals"/> if the supplied method is an implementation of <see cref="IEqualityComparer{T}.Equals(T,T)"/>. | ||
/// </summary> | ||
/// <param name="methodInfo">The method to verify.</param> | ||
public override void Verify(MethodInfo methodInfo) | ||
{ | ||
if (methodInfo == null) throw new ArgumentNullException(nameof(methodInfo)); | ||
|
||
if (methodInfo is { Name: nameof(IEqualityComparer.Equals), ReflectedType: { } type } && methodInfo.GetParameters().Length == 2 && type.ImplementsGenericInterfaceDefinition(typeof(IEqualityComparer<>))) | ||
{ | ||
var argumentType = methodInfo.GetParameters()[0].ParameterType; | ||
|
||
if (!methodInfo.ReflectedType.ImplementsGenericInterface(typeof(IEqualityComparer<>), argumentType)) | ||
{ | ||
return; | ||
} | ||
|
||
this.VerifyEquals(methodInfo, argumentType); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Abstract method used to verify if the method implements correctly <see cref="IEqualityComparer{T}.Equals(T,T)"/>. | ||
/// </summary> | ||
/// <param name="methodInfo">The method to verify.</param> | ||
/// <param name="argumentType">The argument type of <see cref="IEqualityComparer{T}.Equals(T,T)"/>.</param> | ||
protected abstract void VerifyEquals(MethodInfo methodInfo, Type argumentType); | ||
} | ||
} |
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,65 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Reflection; | ||
using AutoFixture.Kernel; | ||
|
||
namespace AutoFixture.Idioms | ||
{ | ||
/// <summary> | ||
/// Encapsulates a unit test that verifies that a type implementing <see cref="IEqualityComparer{T}"/> implements it correctly | ||
/// with respect of the rule: calling Equals(x, null) should return false. | ||
/// </summary> | ||
public class EqualityComparerEqualsNullAssertion : EqualityComparerEqualsAssertion | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="EqualityComparerEqualsNullAssertion"/> class. | ||
/// </summary> | ||
/// <param name="builder"> | ||
/// A composer which can create instances required to implement the idiomatic unit test, | ||
/// such as the owner of the property, as well as the value to be assigned and read from | ||
/// the member. | ||
/// </param> | ||
/// <remarks> | ||
/// <para> | ||
/// <paramref name="builder" /> will typically be a <see cref="Fixture" /> instance. | ||
/// </para> | ||
/// </remarks> | ||
public EqualityComparerEqualsNullAssertion(ISpecimenBuilder builder) | ||
: base(builder) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Verifies that `calling Equals(x, null) should return false` | ||
/// if the supplied method is an implementation of <see cref="IEqualityComparer{T}.Equals(T,T)"/>. | ||
/// </summary> | ||
/// <param name="methodInfo">The method to verify.</param> | ||
/// <param name="argumentType">The argument type of <see cref="IEqualityComparer{T}.Equals(T,T)"/>.</param> | ||
protected override void VerifyEquals(MethodInfo methodInfo, Type argumentType) | ||
{ | ||
if (methodInfo == null) throw new ArgumentNullException(nameof(methodInfo)); | ||
|
||
if (argumentType == null) throw new ArgumentNullException(nameof(argumentType)); | ||
|
||
if (methodInfo.ReflectedType!.IsValueType) | ||
{ | ||
return; | ||
} | ||
|
||
var comparer = this.Builder.CreateAnonymous(methodInfo.ReflectedType); | ||
|
||
var testSubject = this.Builder.CreateAnonymous(argumentType); | ||
|
||
var result = (bool)methodInfo.Invoke(comparer, new[] { testSubject, null }); | ||
|
||
if (result) | ||
{ | ||
throw new EqualityComparerImplementationException(string.Format(CultureInfo.CurrentCulture, | ||
"The type '{0}' implements the `IEqualityComparer<T>` interface incorrectly: " + | ||
"calling Equals(x, null) should return false.", | ||
methodInfo.ReflectedType!.FullName)); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.