Fix PropertiesComparer when properties are not comparable - #4934
Conversation
|
@manfred-brands |
| EqualMethodResult result = equalityComparer.AreEqual(xPropertyValue, yPropertyValue, ref tolerance, comparisonState); | ||
| if (result == EqualMethodResult.ComparedNotEqual) | ||
|
|
||
| if (result == EqualMethodResult.ComparedNotEqual || result == EqualMethodResult.TypesNotSupported) |
There was a problem hiding this comment.
Should it not return the TypesNotSupported?
The idea is that if one comparer doesn't support items, it bubble up and the next comparer is called.
As the PropertiesComparer is the last, the result is likely the same.
There was a problem hiding this comment.
Originally I returned it, but the error message is not very helpful. It prints root object type name, something like Can't find comparer for types 'TypeWithObjectProperty' and 'TypeWithObjectProperty'. In case of assertion failure it prints exact property, which is much more useful.
Plus if I return TypesNotSupported, it fails with NotSupportedException, AssertionException is probably more suitable here.
There was a problem hiding this comment.
It depends if we want to distinguish between comparing as different vs not being able to compare.
What does normal comparison do when comparing instances of different types?
There was a problem hiding this comment.
In regular case, it's NotSupportedException.
Which makes sense - as I have explicitly passed two incomparrable values to an assert method, i.e. I have done something wrong.
In this case, however - I'm passing two TypeWithObjectProperty instances, that's a perfectly valid thing to do. I can't know if some deep down property values have different types - that's exactly what I'm testing. Therefore - it simply should be an assertion failure.
There was a problem hiding this comment.
One final thing. What does a regular compare on a dictionary with different element types return?
There was a problem hiding this comment.
AssertionException:
Assert.That(actual, Is.EqualTo(expected))
Expected: <NUnit.Framework.Tests.TypeWithObjectProperty>
But was: <NUnit.Framework.Tests.TypeWithObjectProperty>
There was a problem hiding this comment.
Oh, Dictionary, not object. Still,
Assert.That(actual, Is.EqualTo(expected))
Expected and actual are both <System.Collections.Generic.Dictionary`2[System.String,System.Object]> with 1 elements
manfred-brands
left a comment
There was a problem hiding this comment.
Thanks. Nothing further.
|
Thanks for another contribution @Dreamescaper |
Fixes #4933 .
Turns out the problem is not Dictionary specific. It reproduces even for something like this:
The problem is that PropertiesComparer didn't check for NotSupported result - it basically treated it like a successul comparision.