|
| 1 | +//----------------------------------------------------------------------- |
| 2 | +// <copyright file="ItArgumentsMustMatchMockedMethodArgumentsAnalyzer.cs" company="P.O.S Informatique"> |
| 3 | +// Copyright (c) P.O.S Informatique. All rights reserved. |
| 4 | +// </copyright> |
| 5 | +//----------------------------------------------------------------------- |
| 6 | + |
| 7 | +namespace PosInformatique.Moq.Analyzers |
| 8 | +{ |
| 9 | + using System.Collections.Immutable; |
| 10 | + using Microsoft.CodeAnalysis; |
| 11 | + using Microsoft.CodeAnalysis.CSharp; |
| 12 | + using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 13 | + using Microsoft.CodeAnalysis.Diagnostics; |
| 14 | + |
| 15 | + [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 16 | + public class ItArgumentsMustMatchMockedMethodArgumentsAnalyzer : DiagnosticAnalyzer |
| 17 | + { |
| 18 | + internal static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor( |
| 19 | + "PosInfoMoq1006", |
| 20 | + "The It.IsAny<T>() or It.Is<T>() arguments must match the parameters of the mocked method", |
| 21 | + "The It.IsAny<T>() or It.Is<T>() arguments must match the parameters of the mocked method", |
| 22 | + "Design", |
| 23 | + DiagnosticSeverity.Warning, |
| 24 | + isEnabledByDefault: true, |
| 25 | + description: "The It.IsAny<T>() or It.Is<T>() arguments must match the parameters of the mocked method.", |
| 26 | + helpLinkUri: "https://posinformatique.github.io/PosInformatique.Moq.Analyzers/docs/Design/PosInfoMoq1006.html"); |
| 27 | + |
| 28 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule); |
| 29 | + |
| 30 | + public override void Initialize(AnalysisContext context) |
| 31 | + { |
| 32 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 33 | + context.EnableConcurrentExecution(); |
| 34 | + |
| 35 | + context.RegisterSyntaxNodeAction(Analyze, SyntaxKind.InvocationExpression); |
| 36 | + } |
| 37 | + |
| 38 | + private static void Analyze(SyntaxNodeAnalysisContext context) |
| 39 | + { |
| 40 | + var invocationExpression = (InvocationExpressionSyntax)context.Node; |
| 41 | + |
| 42 | + var moqSymbols = MoqSymbols.FromCompilation(context.Compilation); |
| 43 | + |
| 44 | + if (moqSymbols is null) |
| 45 | + { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + // Extracts the setup method from the Callback() method call. |
| 50 | + var moqExpressionAnalyzer = new MoqExpressionAnalyzer(moqSymbols, context.SemanticModel); |
| 51 | + var setupMethod = moqExpressionAnalyzer.ExtractSetupMethod(invocationExpression, context.CancellationToken); |
| 52 | + |
| 53 | + if (setupMethod is null) |
| 54 | + { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + // Iterate for each parameter |
| 59 | + for (var i = 0; i < setupMethod.InvocationArguments.Count; i++) |
| 60 | + { |
| 61 | + var invocationArgument = setupMethod.InvocationArguments[i]; |
| 62 | + |
| 63 | + // Check if the parameter is "It.IsAny<xxx>()" |
| 64 | + var itIsAnyType = moqSymbols.GetItIsAnyType(invocationArgument.Symbol); |
| 65 | + if (itIsAnyType is not null) |
| 66 | + { |
| 67 | + if (!SymbolEqualityComparer.Default.Equals(itIsAnyType, invocationArgument.ParameterSymbol.Type)) |
| 68 | + { |
| 69 | + context.ReportDiagnostic(Rule, invocationArgument.Syntax.GetLocation()); |
| 70 | + continue; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // Check if the parameter is "It.Is<xxx>()" |
| 75 | + var itIsType = moqSymbols.GetItIsType(invocationArgument.Symbol); |
| 76 | + if (itIsType is not null) |
| 77 | + { |
| 78 | + if (!SymbolEqualityComparer.Default.Equals(itIsType, invocationArgument.ParameterSymbol.Type)) |
| 79 | + { |
| 80 | + context.ReportDiagnostic(Rule, invocationArgument.Syntax.GetLocation()); |
| 81 | + continue; |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments