BeOneOf for object comparisons with custom comparer support
See original GitHub issueBackground 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
Issue Analytics
- State:
- Created 8 months ago
- Reactions:2
- Comments:15 (15 by maintainers)
Top Results From Across the Web
Compare two objects using serialization C# - Stack Overflow
I am asking this because I want to create a simple and generic Comparator for a Unit Test project, so the performance of...
Read more >Strings
BeOneOf ( "That is a String", "This is a String", ); theString.Should(). ... of literal and wildcard characters, but it doesn't support regular...
Read more >Bitmap checkpoint comparer Interfaces - ADM Help Centers
A picture object (output). A bitmap (created by the custom comparer) that reflects the difference between the actual and expected bitmaps. UFT ...
Read more >Comparing objects with disparate members in Fluent Assertions
Some examples on how to use the new WithMapping API to compare objects with differently named properties or fields.
Read more >`CStringT`** Class
The class requires CRT support and searches for resource strings in the module ... CStringT::Compare, Compares two strings (case-sensitive).
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
An alternative API
The difference comes for classes deriving from
ObjectAssertions<TSubject, TAssertions>
e.g.HttpResponseMessageAssertions
. The proposed API would allowHttpResponseMessageAssertions.BeOneOf()
to take input of arbitrary types, while it currently only allows input ofHttpResponseMessage
.If not providing an
IEqualityComparer
, wouldn’t the existingBeOneOf
suffice? See this example: https://dotnetfiddle.net/N2Rpt6