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

Improve PropertiesComparer diagnostics #4710

Closed
chucker opened this issue May 13, 2024 · 1 comment · Fixed by #4712
Closed

Improve PropertiesComparer diagnostics #4710

chucker opened this issue May 13, 2024 · 1 comment · Fixed by #4712
Assignees
Milestone

Comments

@chucker
Copy link

chucker commented May 13, 2024

Can the PropertiesComparer return which property was unequal? It seems it currently fails without that information:

                        if (result == EqualMethodResult.ComparedNotEqual)
                            return result;

When a type has multiple properties, this only outputs the name of the type (or whatever ToString() happens to return), e.g.

Assert.That(result[0], Is.EqualTo(testData.ExpectedResult[0]).UsingPropertiesComparer())
  Expected: <MyTypeName>
  But was:  <MyTypeName>
@manfred-brands manfred-brands self-assigned this May 15, 2024
@manfred-brands
Copy link
Member

I improved the message, it not only says the failing property name, but also the value of that property:

Multiple failures or warnings in test:
  1)   Assert.That(new StructWithSomeToleranceAwareMembers(2, 1.1, "1.1", SomeEnum.One), Is.EqualTo(instance).UsingPropertiesComparer())
  Values differ at property StructWithSomeToleranceAwareMembers.ValueA
  Expected: 1
  But was:  2
    
  2)   Assert.That(new StructWithSomeToleranceAwareMembers(1, 1.2, "1.1", SomeEnum.One), Is.EqualTo(instance).UsingPropertiesComparer())
  Values differ at property StructWithSomeToleranceAwareMembers.ValueB
  Expected: 1.1000000000000001d
  But was:  1.2d
    
  3)   Assert.That(new StructWithSomeToleranceAwareMembers(1, 1.1, "1.2", SomeEnum.One), Is.EqualTo(instance).UsingPropertiesComparer())
  Values differ at propertyStructWithSomeToleranceAwareMembers.ValueC
  String lengths are both 3. Strings differ at index 2.
  Expected: "1.1"
  But was:  "1.2"
  -------------^
    
  4)   Assert.That(new StructWithSomeToleranceAwareMembers(1, 1.1, "1.1", SomeEnum.Two), Is.EqualTo(instance).UsingPropertiesComparer())
  Values differ at property StructWithSomeToleranceAwareMembers.ValueD
  Expected: One
  But was:  Two

If a property 3 levels down inside an array fails:

  Assert.That(two, Is.EqualTo(one).UsingPropertiesComparer())
  Values differ at property ParentClass.Two
  Values differ at property ChildClass.Values
      Expected and actual are both <NUnit.Framework.Tests.Assertions.AssertThatTests+GrandChild[2]>
      Values differ at index [1]
  Values differ at property GrandChildClass.Value
  Expected: 3
  But was:  4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants