Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ShouldBe does not use the correct comparer for IEnumerable<IEquatable> #825

Open
rick-palmsens opened this issue Sep 7, 2021 · 0 comments

Comments

@rick-palmsens
Copy link

For the following code, comparing the individual instances with ShouldBe succeeds, but comparing the lists fails. It seems to call the object version of Equals when comparing the lists, which has not been overridden. The individual comparisons however do call the Foo version of Equals.

using System;
using System.Collections.Generic;
using Shouldly;

namespace ShouldlyEquatable
{
    public class Foo : IEquatable<Foo>
    {
        public int Bar { get; set; }

        public bool Equals(Foo other)
        {
            if (ReferenceEquals(null, other)) return false;
            return Bar == other.Bar;
        }
    }

    public class FooTests
    {
        public void Equatable()
        {
            var expected = new List<Foo>
            {
                new Foo { Bar = 1 },
                new Foo { Bar = 2 },
                new Foo{ Bar = 3 }
            };

            var actual = new List<Foo>
            {
                new Foo { Bar = 1 },
                new Foo { Bar = 2 },
                new Foo{ Bar = 3 }
            };

            for (int i = 0; i < expected.Count; ++i)
            {
                actual[i].ShouldBe(expected[i]); // Succeeds
            }

            actual.ShouldBe(expected); // Fails
        }
    }
}
rainersigwald added a commit to rainersigwald/msbuild that referenced this issue May 1, 2023
rainersigwald added a commit to rainersigwald/msbuild that referenced this issue May 1, 2023
rainersigwald added a commit to dotnet/msbuild that referenced this issue Oct 30, 2023
* Shouldly 4.2.1

Almost all changes are fallout from https://docs.shouldly.org/documentation/3to4#func-less-than-string-greater-than-custommessage-removed.

Fixes were: always materialize the string up front, and sometimes
explicitly specify `customMessage:` to disambiguate methods.

* Add equatable overload

Works around shouldly/shouldly#825.

* Update Shouldly to 4.2.1 in samples to keep up with the version in tests

---------

Co-authored-by: Gang Wang <v-gaw@microsoft.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant