question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BeOneOf for object comparisons with custom comparer support

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 8 months ago
  • Reactions:2
  • Comments:15 (15 by maintainers)

github_iconTop GitHub Comments

2reactions
jnyrupcommented, Jan 22, 2023

An alternative API

public class ObjectAssertions : ObjectAssertions<object, ObjectAssertions>
{
    public AndConstraint<ObjectAssertions> BeOneOf<TExpectation>(IEnumerable<TExpectation> validValues, IEqualityComparer<TExpectation> comparer, string because = "", params object[] becauseArgs);
}

public class ObjectAssertions<TSubject, TAssertions> : ReferenceTypeAssertions<TSubject, TAssertions>
    where TAssertions : ObjectAssertions<TSubject, TAssertions>
{
    public AndConstraint<TAssertions> BeOneOf(IEnumerable<TSubject> validValues, IEqualityComparer<TSubject> comparer, string because = "", params object[] becauseArgs);
}

The difference comes for classes deriving from ObjectAssertions<TSubject, TAssertions> e.g. HttpResponseMessageAssertions. The proposed API would allow HttpResponseMessageAssertions.BeOneOf() to take input of arbitrary types, while it currently only allows input of HttpResponseMessage.

1reaction
jnyrupcommented, May 19, 2023

If TSubject imlements IEquatable<TSubject> I would hope that providing IEqualityComparer<TSubject> is optional?

If not providing an IEqualityComparer, wouldn’t the existing BeOneOf suffice? See this example: https://dotnetfiddle.net/N2Rpt6

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found