DeepEqual icon indicating copy to clipboard operation
DeepEqual copied to clipboard

Comparing object to primitive values is unexpected always deep equal.

Open tugend opened this issue 5 years ago • 5 comments

Given the example below, I would never expect the object Foo carrying the value 12 to equal 42. It seems like a bug to me, and I'm afraid it'll result in many unexpected false positives.

        [Fact]
        public void ShouldFail()
        {
            new Foo(12).ShouldDeepEqual(42);
        }

        public class Foo
        {
            public int Value { get; }

            public Foo(int value)
            {
                this.Value = value;
            }
        }

tugend avatar Oct 24 '19 13:10 tugend

This is definitely a major bug. If I get time this weekend I’ll look into it.

jamesfoster avatar Nov 08 '19 19:11 jamesfoster

Hi @jamesfoster , Have you been able to verify the bug, any status on the issue?

tugend avatar Dec 03 '19 11:12 tugend

I can confirm this. It's because the comparison returns ComparisonResult.Inconclusive, and ShouldDeepEqual only fails on ComparisonResult.Fail. I've been caught by the same in other situations.

asgerhallas avatar Oct 01 '20 14:10 asgerhallas

Is there a reason inconclusive results that bubbles all the way to the top of the comparison doesn't throw?

I made a wrapper like ShouldDeepEqual, that did just that, but then discovered the same thing goes for comparing to object instances, just the other way around:

    [Fact]
    public void ObjectsAreEqual()
    {
        var comparison = new ComparisonBuilder().Create();
        var (result, context) = comparison.Compare(new ComparisonContext(), new object(), new object());
        Assert.Equal(ComparisonResult.Pass, result); // Fails, as it is inconclusive
    }

Isn't this a sign of a missing comparer (either internal or custom)?

asgerhallas avatar Oct 01 '20 15:10 asgerhallas

These issues can be worked around though with custom comparers. https://github.com/asgerhallas/shouldbelike tries to deal with this issue, cycles, JTokens and tuples.

asgerhallas avatar Oct 01 '20 16:10 asgerhallas

Fixed in 4.0.0

jamesfoster avatar Aug 20 '22 12:08 jamesfoster