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
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
21226ac
Update analyzer, refactor code, and add abstract class support
rjmurillo May 30, 2024
8789898
retrigger checks
rjmurillo May 30, 2024
24ba0d6
Merge branch 'master' into repro-issue-1
rjmurillo May 30, 2024
246ab16
Merge branch 'master' into repro-issue-1
rjmurillo May 31, 2024
6a5857b
Updated test files and refactored AbstractClassTests.cs
rjmurillo May 31, 2024
56c9516
`Updated test methods and diagnostic messages in AbstractClassTests`
rjmurillo May 31, 2024
012524a
Delete Source/Moq.Analyzers.Test/AbstractClassTests.ShouldPassIfGoodP…
rjmurillo May 31, 2024
7560816
Update DiagnosticVerifier.Helper.cs to move using inside namespace
rjmurillo May 31, 2024
b568ce6
Update ConstructorArgumentsShouldMatchAnalyzer.cs comment about abstr…
rjmurillo May 31, 2024
1ffbc5e
Update Moq.Analyzers.csproj to remove language version override
rjmurillo May 31, 2024
d889cdf
Merge branch 'master' into repro-issue-1
rjmurillo May 31, 2024
c552220
Update baselines for abstract class and ctor arguments due to refactor
rjmurillo May 31, 2024
5243b0e
Merge branch 'master' into repro-issue-1
rjmurillo May 31, 2024
300fde8
Rebaseline constructor arguments using Verify.Terminal
rjmurillo May 31, 2024
b034462
Merge branch 'repro-issue-1' of https://github.com/Litee/moq.analyzer…
rjmurillo May 31, 2024
0107aeb
Added new cases to analyze abstract classes' constructor arguments
rjmurillo May 31, 2024
b3205bb
Rebaseline constructor arguments using Verify.Terminal
rjmurillo May 31, 2024
c3c0a3d
Fix line endings on test csharp
rjmurillo May 31, 2024
03ade9d
Add positive and negative cases for generic
rjmurillo May 31, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
"nbgv"
],
"rollForward": false
},
"verify.tool": {
"version": "0.6.0",
"commands": [
"dotnet-verify"
],
"rollForward": false
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Diagnostic 1
Id: Moq1002
Location: SourceFile(Test1.cs[252..258))
Highlight: ("42")
Lines: var mock = new Mock<AbstractClassWithCtor>("42");
Severity: Warning
Message: Parameters provided into mock do not match any existing constructors.

29 changes: 29 additions & 0 deletions Source/Moq.Analyzers.Test/AbstractClassTests.cs
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();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
Diagnostic 1
Id: Moq1002
Location: SourceFile(Test0.cs[1321..1330))
Location: SourceFile(Test0.cs[1227..1236))
Highlight: (1, true)
Lines: var mock1 = new Moq.Mock<Foo>(1, true);
Severity: Warning
Message: Parameters provided into mock do not match any existing constructors.

Diagnostic 2
Id: Moq1002
Location: SourceFile(Test0.cs[1401..1410))
Location: SourceFile(Test0.cs[1307..1316))
Highlight: (2, true)
Lines: var mock2 = new Mock<ConstructorArgumentsShouldMatch.Foo>(2, true);
Severity: Warning
Message: Parameters provided into mock do not match any existing constructors.

Diagnostic 3
Id: Moq1002
Location: SourceFile(Test0.cs[1481..1489))
Location: SourceFile(Test0.cs[1387..1395))
Highlight: ("1", 3)
Lines: var mock3 = new Mock<ConstructorArgumentsShouldMatch.Foo>("1", 3);
Severity: Warning
Message: Parameters provided into mock do not match any existing constructors.

Diagnostic 4
Id: Moq1002
Location: SourceFile(Test0.cs[1560..1583))
Location: SourceFile(Test0.cs[1466..1489))
Highlight: (new int[] { 1, 2, 3 })
Lines: var mock4 = new Mock<ConstructorArgumentsShouldMatch.Foo>(new int[] { 1, 2, 3 });
Severity: Warning
Message: Parameters provided into mock do not match any existing constructors.

Diagnostic 5
Id: Moq1002
Location: SourceFile(Test0.cs[1675..1705))
Location: SourceFile(Test0.cs[1581..1611))
Highlight: (MockBehavior.Strict, 4, true)
Lines: var mock1 = new Mock<Foo>(MockBehavior.Strict, 4, true);
Severity: Warning
Message: Parameters provided into mock do not match any existing constructors.

Diagnostic 6
Id: Moq1002
Location: SourceFile(Test0.cs[1780..1809))
Location: SourceFile(Test0.cs[1686..1715))
Highlight: (MockBehavior.Loose, 5, true)
Lines: var mock2 = new Moq.Mock<ConstructorArgumentsShouldMatch.Foo>(MockBehavior.Loose, 5, true);
Severity: Warning
Message: Parameters provided into mock do not match any existing constructors.

Diagnostic 7
Id: Moq1002
Location: SourceFile(Test0.cs[1884..1912))
Location: SourceFile(Test0.cs[1790..1818))
Highlight: (MockBehavior.Loose, "2", 6)
Lines: var mock3 = new Moq.Mock<ConstructorArgumentsShouldMatch.Foo>(MockBehavior.Loose, "2", 6);
Severity: Warning
Expand Down
11 changes: 11 additions & 0 deletions Source/Moq.Analyzers.Test/Data/AbstractClass.Bad.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Moq.Analyzers.Test.Data
{
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

{
// The class has a constructor that takes an int but passes a string
var mock = new Mock<AbstractClassWithCtor>("42");
}
}
}
42 changes: 42 additions & 0 deletions Source/Moq.Analyzers.Test/Data/AbstractClass.Good.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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);
var mock3 = new Mock<AbstractClassWithCtor>(MockBehavior.Default, 42);
}
}
}
11 changes: 0 additions & 11 deletions Source/Moq.Analyzers.Test/Data/ConstructorArgumentsShouldMatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ public Foo(params DateTime[] dates) { }
public Foo(List<string> l, string s = "A") { }
}

internal abstract class FooAbstract
{
public FooAbstract(string s) { }
}

internal class MyUnitTests
#pragma warning restore SA1402 // File may only contain a single class
{
Expand Down Expand Up @@ -72,11 +67,5 @@ private void TestGood1()
var mock15 = new Mock<Foo>(MockBehavior.Default, new List<string>(), "8");
var mock16 = new Mock<Foo>(MockBehavior.Default, new List<string>());
}

private void TestAbstractGood()
{
var mock1 = new Mock<FooAbstract>("9");
var mock2 = new Mock<FooAbstract>(MockBehavior.Default, "10");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
Expand Down Expand Up @@ -42,6 +43,8 @@ public abstract partial class DiagnosticVerifier
/// <returns>An IEnumerable of Diagnostics that surfaced in the source code, sorted by Location.</returns>
protected static Diagnostic[] GetSortedDiagnosticsFromDocuments(DiagnosticAnalyzer analyzer, Document[] documents)
{
Debug.Assert(documents != null, nameof(documents) + " != null");

var projects = new HashSet<Project>();
foreach (var document in documents)
{
Expand All @@ -51,6 +54,7 @@ protected static Diagnostic[] GetSortedDiagnosticsFromDocuments(DiagnosticAnalyz
var diagnostics = new List<Diagnostic>();
foreach (var project in projects)
{
Debug.Assert(analyzer != null, nameof(analyzer) + " != null");
var compilationWithAnalyzers = project.GetCompilationAsync().Result.WithAnalyzers(ImmutableArray.Create(analyzer));
var diags = compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync().Result;
foreach (var diag in diags)
Expand Down Expand Up @@ -174,4 +178,4 @@ private static Project CreateProject(string[] sources, string language = Languag
return solution.GetProject(projectId);
}
}
}
}
6 changes: 6 additions & 0 deletions Source/Moq.Analyzers.Test/Moq.Analyzers.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
<ProjectReference Include="..\Moq.Analyzers\Moq.Analyzers.csproj" AddPackageAsOutput="true" />
</ItemGroup>
<ItemGroup>
<Compile Update="Data\AbstractClass.Bad.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
<Compile Update="Data\AbstractClass.Good.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
<Compile Update="Data\CallbackSignatureShouldMatchMockedMethod.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
Expand Down
Loading
Loading