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

Add quickfix that swaps actual and expected value #1

Open
inyutin-maxim opened this issue Jun 1, 2022 · 7 comments
Open

Add quickfix that swaps actual and expected value #1

inyutin-maxim opened this issue Jun 1, 2022 · 7 comments
Labels
help wanted Extra attention is needed

Comments

@inyutin-maxim
Copy link
Contributor

For example:

"video1234_1234_test".Should().Be(result);

should be replaced with:

result.Should().Be("video1234_1234_test");
@meziantou
Copy link
Owner

meziantou commented Jun 1, 2022

Good catch! What is the original assert code? Also, is the code using NUnit or xUnit?

@inyutin-maxim
Copy link
Contributor Author

Good catch! What is the original assert code? Also, is the code using NUnit or xUnit?

This code from my very old project, tests writen used NUnit.
Original Assert:

Assert.AreEqual(result, "video1234_1234_test");
                ^^^^^^^, ^^^^^^^^
               expected, actual

but i think this problem can reproduce and for xUnit

@meziantou
Copy link
Owner

It seems you swapped the expected and actual parameters in the original call (documentation).

The analyzer uses the following semantic:

Assert.AreEqual(expected, actual); // expected is the first parameter
actual.Should().Be(expected)
Assert.AreEqual(result, "video1234_1234_test");
// Should be
Assert.AreEqual("video1234_1234_test", result);

@inyutin-maxim
Copy link
Contributor Author

It seems you swapped the expected and actual parameters in the original call (documentation).

I did this deliberately to show what the problem was, but in those distant times it was not at all obvious to me.
I started this problem to have a convenient tool for automatic replacement. I understand that a deeper semantic analysis is needed to smartly identify a potential error, and a warning need not always be shown, but this could make life easier. If you consider the problem redundant, you can close it

@inyutin-maxim
Copy link
Contributor Author

I see an order like this:

  1. I apply the fix for the original code:
Assert.AreEqual(result, "video1234_1234_test");
  1. I get the following code:
"video1234_1234_test".Should().Be(result);
  1. I apply the next fix for the new code:
"video1234_1234_test".Should().Be(result);
  1. I get the following code:
result.Should().Be("video1234_1234_test");

@meziantou
Copy link
Owner

I thought you were reporting a bug, but it's a feature request!

Something we can try is detecting constant values on the left side and report them because I don't think there is a valid reason to have a constant before "Should".

@inyutin-maxim
Copy link
Contributor Author

yes you are right this new functionality, perhaps I did not exactly express myself when describing

@meziantou meziantou added the help wanted Extra attention is needed label Jun 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants