-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update analyzer, refactor code, and add abstract class support (#20)
* Update analyzer, refactor code, and add abstract class support 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 * retrigger checks * Updated test files and refactored AbstractClassTests.cs 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`. * `Updated test methods and diagnostic messages in AbstractClassTests` 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. * Delete Source/Moq.Analyzers.Test/AbstractClassTests.ShouldPassIfGoodParameters.approved.txt This file is no longer needed * Update DiagnosticVerifier.Helper.cs to move using inside namespace * Update ConstructorArgumentsShouldMatchAnalyzer.cs comment about abstract type ctor * Update Moq.Analyzers.csproj to remove language version override * Update baselines for abstract class and ctor arguments due to refactor * Rebaseline constructor arguments using Verify.Terminal * Added new cases to analyze abstract classes' constructor arguments * Rebaseline constructor arguments using Verify.Terminal * Fix line endings on test csharp * Add positive and negative cases for generic
- Loading branch information
Showing
11 changed files
with
320 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...zers.Test/AbstractClassTests.ShouldPassIfGoodParametersAndFailOnTypeMismatch.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
Diagnostic 1 | ||
Id: Moq1002 | ||
Location: SourceFile(Test1.cs[247..253)) | ||
Highlight: ("42") | ||
Lines: var mock = new Mock<AbstractClassWithCtor>("42"); | ||
Severity: Warning | ||
Message: Parameters provided into mock do not match any existing constructors. | ||
|
||
Diagnostic 2 | ||
Id: Moq1002 | ||
Location: SourceFile(Test1.cs[420..430)) | ||
Highlight: ("42", 42) | ||
Lines: var mock1 = new Mock<AbstractClassWithCtor>("42", 42); | ||
Severity: Warning | ||
Message: Parameters provided into mock do not match any existing constructors. | ||
|
||
Diagnostic 3 | ||
Id: Moq1002 | ||
Location: SourceFile(Test1.cs[559..563)) | ||
Highlight: (42) | ||
Lines: var mock2 = new Mock<AbstractClassDefaultCtor>(42); | ||
Severity: Warning | ||
Message: Parameters provided into mock do not match any existing constructors. | ||
|
||
Diagnostic 4 | ||
Id: Moq1002 | ||
Location: SourceFile(Test1.cs[780..786)) | ||
Highlight: ("42") | ||
Lines: var mock = new Mock<AbstractGenericClassWithCtor<object>>("42"); | ||
Severity: Warning | ||
Message: Parameters provided into mock do not match any existing constructors. | ||
|
||
Diagnostic 5 | ||
Id: Moq1002 | ||
Location: SourceFile(Test1.cs[968..978)) | ||
Highlight: ("42", 42) | ||
Lines: var mock1 = new Mock<AbstractGenericClassWithCtor<object>>("42", 42); | ||
Severity: Warning | ||
Message: Parameters provided into mock do not match any existing constructors. | ||
|
||
Diagnostic 6 | ||
Id: Moq1002 | ||
Location: SourceFile(Test1.cs[1122..1126)) | ||
Highlight: (42) | ||
Lines: var mock2 = new Mock<AbstractGenericClassDefaultCtor<object>>(42); | ||
Severity: Warning | ||
Message: Parameters provided into mock do not match any existing constructors. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace Moq.Analyzers.Test | ||
{ | ||
using System.IO; | ||
|
||
using TestHelper; | ||
|
||
using Xunit; | ||
|
||
public class AbstractClassTests : DiagnosticVerifier | ||
{ | ||
[Fact] | ||
public Task ShouldPassIfGoodParametersAndFailOnTypeMismatch() | ||
{ | ||
return Verify(VerifyCSharpDiagnostic( | ||
[ | ||
File.ReadAllText("Data/AbstractClass.Good.cs"), | ||
File.ReadAllText("Data/AbstractClass.Bad.cs") | ||
] | ||
)); | ||
} | ||
|
||
protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() | ||
{ | ||
return new ConstructorArgumentsShouldMatchAnalyzer(); | ||
} | ||
} | ||
} |
14 changes: 7 additions & 7 deletions
14
...uctorArgumentsShouldMatchAnalyzerTests.ShouldFailIfClassParametersDoNotMatch.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
namespace Moq.Analyzers.Test.Data | ||
{ | ||
internal class MyBadUnitTests | ||
{ | ||
private void TestBad() | ||
{ | ||
// The class has a ctor that takes an Int32 but passes a String | ||
var mock = new Mock<AbstractClassWithCtor>("42"); | ||
|
||
// The class has a ctor with two arguments [Int32, String], but they are passed in reverse order | ||
var mock1 = new Mock<AbstractClassWithCtor>("42", 42); | ||
|
||
// The class has a ctor but does not take any arguments | ||
var mock2 = new Mock<AbstractClassDefaultCtor>(42); | ||
} | ||
|
||
private void TestBadWithGeneric() | ||
{ | ||
// The class has a constructor that takes an Int32 but passes a String | ||
var mock = new Mock<AbstractGenericClassWithCtor<object>>("42"); | ||
|
||
// The class has a ctor with two arguments [Int32, String], but they are passed in reverse order | ||
var mock1 = new Mock<AbstractGenericClassWithCtor<object>>("42", 42); | ||
|
||
// The class has a ctor but does not take any arguments | ||
var mock2 = new Mock<AbstractGenericClassDefaultCtor<object>>(42); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
namespace Moq.Analyzers.Test.Data | ||
{ | ||
internal abstract class AbstractGenericClassDefaultCtor<T> | ||
{ | ||
protected AbstractGenericClassDefaultCtor() | ||
{ | ||
} | ||
} | ||
|
||
internal abstract class AbstractGenericClassWithCtor<T> | ||
{ | ||
protected AbstractGenericClassWithCtor(int a) | ||
{ | ||
} | ||
|
||
protected AbstractGenericClassWithCtor(int a, string b) | ||
{ | ||
} | ||
} | ||
|
||
internal abstract class AbstractClassDefaultCtor | ||
{ | ||
protected AbstractClassDefaultCtor() | ||
{ | ||
} | ||
} | ||
|
||
internal abstract class AbstractClassWithCtor | ||
{ | ||
protected AbstractClassWithCtor(int a) | ||
{ | ||
} | ||
|
||
protected AbstractClassWithCtor(int a, string b) | ||
{ | ||
} | ||
} | ||
|
||
internal class MyUnitTests | ||
{ | ||
// Base case that we can handle abstract types | ||
private void TestForBaseNoArgs() | ||
{ | ||
var mock = new Mock<AbstractClassDefaultCtor>(); | ||
mock.As<AbstractClassDefaultCtor>(); | ||
|
||
var mock2 = new Mock<AbstractClassWithCtor>(); | ||
var mock3 = new Mock<AbstractClassDefaultCtor>(MockBehavior.Default); | ||
} | ||
|
||
private void TestForBaseGenericNoArgs() | ||
{ | ||
var mock = new Mock<AbstractGenericClassDefaultCtor<object>>(); | ||
mock.As<AbstractGenericClassDefaultCtor<object>>(); | ||
|
||
var mock1 = new Mock<AbstractGenericClassDefaultCtor<object>>(); | ||
|
||
var mock2 = new Mock<AbstractGenericClassDefaultCtor<object>>(MockBehavior.Default); | ||
} | ||
|
||
// This is syntatically not allowed by C#, but you can do it with Moq | ||
private void TestForBaseWithArgsNonePassed() | ||
{ | ||
var mock = new Mock<AbstractClassWithCtor>(); | ||
mock.As<AbstractClassWithCtor>(); | ||
} | ||
|
||
private void TestForBaseWithArgsPassed() | ||
{ | ||
var mock = new Mock<AbstractClassWithCtor>(42); | ||
var mock2 = new Mock<AbstractClassWithCtor>(MockBehavior.Default, 42); | ||
|
||
var mock3 = new Mock<AbstractClassWithCtor>(42, "42"); | ||
var mock4 = new Mock<AbstractClassWithCtor>(MockBehavior.Default, 42, "42"); | ||
|
||
var mock5 = new Mock<AbstractGenericClassWithCtor<object>>(42); | ||
var mock6 = new Mock<AbstractGenericClassWithCtor<object>>(MockBehavior.Default, 42); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.