Skip to content

Conversation

@Meir017
Copy link
Member

@Meir017 Meir017 commented Mar 9, 2020

closes #94

@Meir017
Copy link
Member Author

Meir017 commented Mar 9, 2020

@bordecal does it make sense to assume that the right assertion would be .Should().Be()?

@jnyrup
Copy link
Member

jnyrup commented Mar 9, 2020

If the subject is a collection Equals should probably just be Equal.
I think it might be difficult to guess the intended assertion.

@bordecal
Copy link

bordecal commented Mar 9, 2020

@Meir017 Do you mean for a fixer? If so, for a primitive type, .Should().Be() would probably be the only reasonable choice in most cases.

For complex types, it's more difficult to predict the right option. Maybe .Should().BeSameAs() would be the safest choice. At least it would probably break the test if it's the wrong choice, which wouldn't necessarily be the case for the less restrictive assertions.

@Meir017
Copy link
Member Author

Meir017 commented Mar 20, 2020

@jnyrup I'm pretty sure it's possible to detect the type of actual object used in the assertion. would that make sense?

@JamesDriscoll
Copy link

Have just run into this very issue in several of our code-bases today.
There are multiple copy and paste Should().Equals(...) calls that have started propagating like wild fire.
I was going to offer up a PR myself, but found this one sat dormant.
It would be great if this could be resurrected; if not, I'll try and contribute :)

If the object is a collection, Should().Be() is not available, so Should().Equal() seems correct.

For my needs, I think that Should().Be() would be more than sufficient.

For what it's worth, I think this analyzer is extremely important.
We have non-English speaking developers working offshore who don't spot that x.Should().Equals(y); is a natural English code smell vs x.Should().Be(y);

@Meir017
Copy link
Member Author

Meir017 commented Oct 5, 2021

@JamesDriscoll I have been really busy lately, I really want to get back to this project too :)

@JamesDriscoll
Copy link

Let me know if you need any help :)

@Meir017
Copy link
Member Author

Meir017 commented Oct 7, 2021

@JamesDriscoll @jnyrup Maybe we could have two code fixes suggested and each having a different message, one for primitives and the other for collection and custom classes?

@JamesDriscoll
Copy link

I think one message for collections going to Equal and another one for everything else going to Be.

A lot of tests we write create different but equivalent objects. I personally think it is much, much less likely to have tests where the actual versus expected would satisfy BeSameAs.

Perhaps the compromise would be to offer Be as the fix, but to acknowledge that BeSameAs might be preferable in the message.

@jnyrup
Copy link
Member

jnyrup commented Oct 8, 2021

Some considerations:

  • TaskCompletionSourceAssertions does not have any of Equal, Be, BeSameAs or BeEquivalentTo.
  • StreamAssertions has BeSameAs, but if I had written Equals I imagine what I wanted was to compare the content of the streams.

So it comes down to whether the fixer has to be perfect.
I'm not sure it can be perfect, not because of technical limitations, but because it will possibly have to guess among equal candidates.

@Meir017
Copy link
Member Author

Meir017 commented Oct 8, 2021

@jnyrup Great suggestions!

@Meir017 Meir017 merged commit d66d671 into master Oct 8, 2021
@Meir017 Meir017 deleted the features/should.equals-analyzer branch October 8, 2021 10:30
@JamesDriscoll
Copy link

Thanks for picking this up so swiftly - I think it will really adds a lot of value once it gets out into the wild :)

Some considerations:

  • TaskCompletionSourceAssertions does not have any of Equal, Be, BeSameAs or BeEquivalentTo.
  • StreamAssertions has BeSameAs, but if I had written Equals I imagine what I wanted was to compare the content of the streams.

So it comes down to whether the fixer has to be perfect. I'm not sure it can be perfect, not because of technical limitations, but because it will possibly have to guess among equal candidates.

I know this is already merged, but the other alternative would be not to offer a fix in all cases, but still warn.

@jnyrup
Copy link
Member

jnyrup commented Oct 9, 2021

I know this is already merged, but the other alternative would be not to offer a fix in all cases, but still warn.

That's probably the direction I would have taken.

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

Successfully merging this pull request may close these issues.

Analyzer for Should().Equals()?

5 participants