-
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
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
- Loading branch information
Showing
9 changed files
with
216 additions
and
49 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
Source/Moq.Analyzers.Test/AbstractClassTests.ShouldPassIfGoodParameters.approved.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,8 @@ | ||
Diagnostic 1 | ||
Id: Moq1002 | ||
Location: SourceFile(Test0.cs[1438..1444)) | ||
Highlight: ("42") | ||
Lines: var mock = new Mock<AbstractClassWithCtor>("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 ApprovalTests; | ||
using ApprovalTests.Reporters; | ||
|
||
using TestHelper; | ||
|
||
using Xunit; | ||
|
||
|
||
[UseReporter(typeof(DiffReporter))] | ||
public class AbstractClassTests : DiagnosticVerifier | ||
{ | ||
[Fact] | ||
public void ShouldPassIfGoodParameters() | ||
{ | ||
Approvals.Verify(VerifyCSharpDiagnostic(File.ReadAllText("Data/AbstractClass.cs"))); | ||
} | ||
|
||
protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() | ||
{ | ||
return new ConstructorArgumentsShouldMatchAnalyzer(); | ||
} | ||
} | ||
} |
14 changes: 7 additions & 7 deletions
14
...uctorArgumentsShouldMatchAnalyzerTests.ShouldFailIfClassParametersDoNotMatch.approved.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,53 @@ | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Moq.Analyzers.Test.Data | ||
{ | ||
internal abstract class AbstractClassDefaultCtor | ||
{ | ||
protected AbstractClassDefaultCtor() | ||
{ | ||
} | ||
} | ||
|
||
internal abstract class AbstractClassWithCtor | ||
{ | ||
protected AbstractClassWithCtor(int a) | ||
{ | ||
} | ||
} | ||
|
||
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); | ||
} | ||
|
||
// 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 mock2 = new Mock<AbstractClassWithCtor>(42); | ||
} | ||
|
||
private void TestForBaseWithArgsPassedAndBehavior() | ||
{ | ||
var mock3 = new Mock<AbstractClassWithCtor>(MockBehavior.Default, 42); | ||
} | ||
|
||
private void TestBad() | ||
{ | ||
var mock = new Mock<AbstractClassWithCtor>("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
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