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

Update analyzer, refactor code, and add abstract class support #20

Merged
merged 19 commits into from
May 31, 2024

Conversation

rjmurillo
Copy link
Owner

@rjmurillo rjmurillo commented May 30, 2024

This commit includes several updates and improvements.

  • The ConstructorArgumentsShouldMatchAnalyzerTests.ShouldFailIfClassParametersDoNotMatch.approved.txt file has been updated to reflect changes in the source code.
  • The FooAbstract class and its usage have been removed from ConstructorArgumentsShouldMatch.cs.
  • Defensive programming practices have been added to DiagnosticVerifier.Helper.cs to ensure non-null arguments.
  • A new file AbstractClass.cs has been added to Moq.Analyzers.Test.csproj.
  • The Analyze method in ConstructorArgumentsShouldMatchAnalyzer.cs has been refactored for better readability and maintainability, and now supports abstract classes.
  • New test class AbstractClassTests and related files have been added to verify the functionality of the updated analyzer.

Fixes #1

This commit includes several updates and improvements. The `ConstructorArgumentsShouldMatchAnalyzerTests.ShouldFailIfClassParametersDoNotMatch.approved.txt` file has been updated to reflect changes in the source code. The `FooAbstract` class and its usage have been removed from `ConstructorArgumentsShouldMatch.cs`. Defensive programming practices have been added to `DiagnosticVerifier.Helper.cs` to ensure non-null arguments. A new file `AbstractClass.cs` has been added to `Moq.Analyzers.Test.csproj`. The `Analyze` method in `ConstructorArgumentsShouldMatchAnalyzer.cs` has been refactored for better readability and maintainability, and now supports abstract classes. The `Moq.Analyzers.csproj` now uses the latest C# language version and the assembly version has been updated to `0.0.6.0`. New test class `AbstractClassTests` and related files have been added to verify the functionality of the updated analyzer.

Fixes #1
@rjmurillo
Copy link
Owner Author

@MattKotsenas the build action introduced in #9 didn't run on this PR

@MattKotsenas
Copy link
Collaborator

@MattKotsenas the build action introduced in #9 didn't run on this PR

Copy/paste error on my part. The default branch is master and my code uses main.

Will update to master until we have the ability to change the default branch.

@MattKotsenas
Copy link
Collaborator

Actions issue is fixed in #21.

@rjmurillo rjmurillo marked this pull request as ready for review May 30, 2024 21:26
@rjmurillo rjmurillo self-assigned this May 30, 2024
@rjmurillo rjmurillo marked this pull request as draft May 30, 2024 23:00
@rjmurillo rjmurillo added this to the Next Major Release milestone May 30, 2024
rjmurillo added 7 commits May 31, 2024 08:19
Updated `AbstractClassTests.ShouldFailIfBadParameters.verified.txt` and `AbstractClassTests.ShouldPassIfGoodParameters.verified.txt` to reflect changes in test conditions. Refactored `AbstractClassTests.cs` to remove `ApprovalTests` namespace, change `ShouldPassIfGoodParameters` to a Task method, and modify `VerifyCSharpDiagnostic` to take an array of file contents. Added `AbstractClass.Bad.cs` for testing incorrect parameters and updated `AbstractClass.Good.cs` with necessary changes. Updated `Moq.Analyzers.Test.csproj` to include new and updated files with `CopyToOutputDirectory` set to `Always`.
Renamed the test method `ShouldPassIfGoodParameters` to `ShouldPassIfGoodParametersAndFailOnTypeMismatch` in `AbstractClassTests`. Removed the diagnostic message for the test `ShouldFailIfBadParameters`. Cleared the content of `AbstractClassTests.ShouldPassIfGoodParameters.verified.txt`. Added a new diagnostic message to `AbstractClassTests.ShouldPassIfGoodParametersAndFailOnTypeMismatch.verified.txt` indicating a `Moq1002` warning for a line in `Test1.cs` where the parameters provided to the `Mock<AbstractClassWithCtor>` constructor do not match any existing constructors.
…arameters.approved.txt

This file is no longer needed
@rjmurillo
Copy link
Owner Author

rjmurillo commented May 31, 2024

This is failing between platforms. On Windows and through Ubuntu in WSL, the line numbers are a certain value. On a stand alone Ubuntu VM, they're about 50 chars off.

Diagnostic 2
	Id: Moq1300
-	Location: SourceFile(Test0.cs[1594..1604))
+	Location: SourceFile(Test0.cs[1534..1544))
	Highlight: OtherClass
	Lines: mock.As<OtherClass>();
	Severity: Error
	Message: Mock.As() should take interfaces only

This is because the exemplar code under test is checked in with CRLF, which is stripped in the Linux environment and causing the span positions to be different on the text run. This was corrected in #33 so we're always EOL LF

rjmurillo added 5 commits May 31, 2024 14:05
# Conflicts:
#	Source/Moq.Analyzers.Test/ConstructorArgumentsShouldMatchAnalyzerTests.ShouldFailIfClassParametersDoNotMatch.verified.txt
#	Source/Moq.Analyzers.Test/Data/ConstructorArgumentsShouldMatch.cs
#	Source/Moq.Analyzers.Test/Helpers/DiagnosticVerifier.Helper.cs
#	Source/Moq.Analyzers.Test/Moq.Analyzers.Test.csproj
#	Source/Moq.Analyzers/ConstructorArgumentsShouldMatchAnalyzer.cs
#	Source/Moq.Analyzers/Moq.Analyzers.csproj
@rjmurillo rjmurillo marked this pull request as ready for review May 31, 2024 21:26
{
internal class MyBadUnitTests
{
private void TestBad()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From conversation, some test cases missing:

  • Pass an argument to a constructor that takes no arguments
  • Pass (string, int) to a constructor that takes (int, string) or equivalent test
  • Pass the incorrect number of arguments to constructor

@rjmurillo rjmurillo merged commit 6e96f79 into master May 31, 2024
3 checks passed
@rjmurillo rjmurillo deleted the repro-issue-1 branch May 31, 2024 23:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bad constructor parameters does not work for abstract classes
2 participants