-
Notifications
You must be signed in to change notification settings - Fork 733
Closed
Labels
api-approvedAPI was approved, it can be implementedAPI was approved, it can be implemented
Description
Background and motivation
Support for custom comparer to compare objects
API Proposal
public class ObjectAssertions<TSubject, TAssertions> : ReferenceTypeAssertions<TSubject, TAssertions>
where TAssertions : ObjectAssertions<TSubject, TAssertions>
{
public AndConstraint<TAssertions> BeOneOf<TExpectation>(IEnumerable<TExpectation> validValues, IEqualityComparer<TExpectation> comparer, string because = "", params object[] becauseArgs);
}API Usage
var obj = new SomeClass(1);
var expected = new[] { new SomeClass(1), new SomeClass(2), new SomeClass(3) };
obj .Should().BeOneOf(expected, new SomeClassEqualityComparer ());
internal class SomeClass
{
public SomeClass(int key)
{
Key = key;
}
public int Key { get; }
public override string ToString()
{
return $"SomeClass({Key})";
}
}
internal class SomeClassEqualityComparer : IEqualityComparer<SomeClass>
{
public bool Equals(SomeClass x, SomeClass y)
{
if (ReferenceEquals(x, y))
{
return true;
}
if (ReferenceEquals(x, null))
{
return false;
}
if (ReferenceEquals(y, null))
{
return false;
}
if (x.GetType() != y.GetType())
{
return false;
}
return x.Key == y.Key;
}
public int GetHashCode(SomeClass obj)
{
return obj.Key;
}
}Alternative Designs
No response
Risks
No response
alexbereznikov and dombrovsky
Metadata
Metadata
Assignees
Labels
api-approvedAPI was approved, it can be implementedAPI was approved, it can be implemented