Skip to content

Commit

Permalink
Merge the PosInfoMoq2004 rule to the ConstructorArgumentsMustMatchAna…
Browse files Browse the repository at this point in the history
…lyzer analyzer.
  • Loading branch information
GillesTourreau committed Oct 23, 2024
1 parent 97f6686 commit d7e9639
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 318 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ namespace PosInformatique.Moq.Analyzers
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class ConstructorArgumentsMustMatchAnalyzer : DiagnosticAnalyzer
{
internal static readonly DiagnosticDescriptor ConstructorArgumentsCanBePassedToInterfaceRule = new DiagnosticDescriptor(
"PosInfoMoq2004",
"Constructor arguments cannot be passed for interface mocks",
"Constructor arguments cannot be passed for interface mocks",
"Compilation",
DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "Constructor arguments cannot be passed for interface mocks.",
helpLinkUri: "https://posinformatique.github.io/PosInformatique.Moq.Analyzers/docs/Compilation/PosInfoMoq2004.html");

private static readonly DiagnosticDescriptor ConstructorArgumentsMustMatchMockedClassRule = new DiagnosticDescriptor(
"PosInfoMoq2005",
"Constructor arguments must match the constructors of the mocked class",
Expand Down Expand Up @@ -47,6 +57,7 @@ public class ConstructorArgumentsMustMatchAnalyzer : DiagnosticAnalyzer
helpLinkUri: "https://posinformatique.github.io/PosInformatique.Moq.Analyzers/docs/Compilation/PosInfoMoq2016.html");

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(
ConstructorArgumentsCanBePassedToInterfaceRule,
ConstructorArgumentsMustMatchMockedClassRule,
ConstructorMockedClassMustBeAccessibleRule,
ConstructorWithLambdaExpressionCanBeUseWithClassesOnlyRule);
Expand Down Expand Up @@ -100,12 +111,6 @@ private static void Analyze(SyntaxNodeAnalysisContext context)
return;
}

// Check the type is a class (other type are ignored)
if (mockedType.TypeKind != TypeKind.Class)
{
return;
}

// Check the type is a named type
if (mockedType is not INamedTypeSymbol namedTypeSymbol)
{
Expand All @@ -131,6 +136,21 @@ private static void Analyze(SyntaxNodeAnalysisContext context)
}
}

// If the type is an interface and contains arguments, raise an error.
if (mockedType.TypeKind == TypeKind.Interface)
{
if (constructorArguments.Count > 0)
{
context.ReportDiagnostic(ConstructorArgumentsCanBePassedToInterfaceRule, constructorArguments.Select(a => a.GetLocation()));
}
}

// Check the type is a class (other type are ignored)
if (mockedType.TypeKind != TypeKind.Class)
{
return;
}

var matchedConstructor = default(MatchedConstructor);

// Iterate on each constructor and check if the arguments match.
Expand Down

This file was deleted.

Loading

0 comments on commit d7e9639

Please sign in to comment.