Using IEnumerable<T>.ShouldBe( IEnumerable<T>)

Posted in General by Mark Ashfield Tue Jun 25 2019 08:23:56 GMT+0000 (Coordinated Universal Time)·1·Viewed 81 times

When using IEnumerable<T>.ShouldBe( IEnumerable<T>) where T is an object that implements IEqualityComparer it does not seem to use or obtain the comparer, so does instance comparision.. when i debug this I am seeing the following in the shouldly library.... public static bool Equal<T>(T expected, T actual) // here both T are objects that implement IEqualityComparer { return Equal(expected, actual, GetEqualityComparer<T>()); } static IEqualityComparer<T> GetEqualityComparer<T>(IEqualityComparer innerComparer = null) // nothing is passed here so it uses null and does an object instance compare. { return new EqualityComparer<T>(innerComparer); } Am I doing something wrong here, or is there an issue with comparing two collections?
Mark Ashfield
June 25, 2019

i.e. to use one of your examples to clarify...

[Test]
public void IEnumerable_ShouldBeSubsetOf()
{
var simpsonsKids = new List<Person>()
{
new Person() { Name = "Bart"},
new Person() { Name = "Lisa"},
new Person() { Name = "Maggie"},
new Person() { Name = "Ralph"}
};
var simpsonsFamily = new List<Person>()
{
new Person() { Name = "Lisa"},
new Person() { Name = "Bart"},
new Person() { Name = "Maggie"},
new Person() { Name = "Homer"},
new Person() { Name = "Marge"}
};

        simpsonsKids.ShouldBeSubsetOf(simpsonsFamily);
    }

    public class Person : IEqualityComparer<Person>
    {
        public string Name { get; set; }

        public bool Equals(
            Person x,
            Person y )
        {
            return x.Name == y.Name;
        }

        public int GetHashCode(
            Person obj )
        {
            return obj.Name.GetHashCode();
        }
    }

All items are outside the subset

  
Markdown is allowed