|
| 1 | +//----------------------------------------------------------------------- |
| 2 | +// <copyright file="CallBackDelegateMustMatchMockedMethodAnalyzer.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 CallBackDelegateMustMatchMockedMethodAnalyzer : DiagnosticAnalyzer |
| 17 | + { |
| 18 | + private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor( |
| 19 | + "PosInfoMoq2003", |
| 20 | + "The Callback() delegate expression must match the signature of the mocked method", |
| 21 | + "The Callback() delegate expression must match the signature of the mocked method", |
| 22 | + "Compilation", |
| 23 | + DiagnosticSeverity.Error, |
| 24 | + isEnabledByDefault: true, |
| 25 | + description: "The Callback() delegate expression must match the signature of the mocked method."); |
| 26 | + |
| 27 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule); |
| 28 | + |
| 29 | + public override void Initialize(AnalysisContext context) |
| 30 | + { |
| 31 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 32 | + context.EnableConcurrentExecution(); |
| 33 | + |
| 34 | + context.RegisterSyntaxNodeAction(Analyze, SyntaxKind.InvocationExpression); |
| 35 | + } |
| 36 | + |
| 37 | + private static void Analyze(SyntaxNodeAnalysisContext context) |
| 38 | + { |
| 39 | + var invocationExpression = (InvocationExpressionSyntax)context.Node; |
| 40 | + |
| 41 | + var moqSymbols = MoqSymbols.FromCompilation(context.Compilation); |
| 42 | + |
| 43 | + if (moqSymbols is null) |
| 44 | + { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + var moqExpressionAnalyzer = new MoqExpressionAnalyzer(context.SemanticModel); |
| 49 | + |
| 50 | + // Try to determine if the invocation expression is a Callback() expression. |
| 51 | + var methodSymbol = context.SemanticModel.GetSymbolInfo(invocationExpression); |
| 52 | + |
| 53 | + if (!moqSymbols.IsCallback(methodSymbol.Symbol)) |
| 54 | + { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + // If yes, we extract the lambda expression of it. |
| 59 | + var callBackLambdaExpressionSymbol = moqExpressionAnalyzer.ExtractCallBackLambdaExpressionMethod(invocationExpression, out var lambdaExpression); |
| 60 | + |
| 61 | + if (callBackLambdaExpressionSymbol is null) |
| 62 | + { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + // Check each CallBack() method for the following calls. |
| 67 | + var followingMethods = invocationExpression.DescendantNodes().OfType<InvocationExpressionSyntax>(); |
| 68 | + |
| 69 | + foreach (var followingMethod in followingMethods) |
| 70 | + { |
| 71 | + // Find the symbol of the mocked method (if not symbol found, it is mean we Setup() method that not currently compile) |
| 72 | + // so we skip the analysis. |
| 73 | + var mockedMethod = moqExpressionAnalyzer.ExtractSetupMethod(followingMethod, out var _); |
| 74 | + |
| 75 | + if (mockedMethod is null) |
| 76 | + { |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + // Compare the parameters between the mocked method and lambda expression in the CallBack() method. |
| 81 | + // 1- Compare the number of the parameters |
| 82 | + if (callBackLambdaExpressionSymbol.Parameters.Length != mockedMethod.Parameters.Length) |
| 83 | + { |
| 84 | + var diagnostic = Diagnostic.Create(Rule, lambdaExpression!.ParameterList.GetLocation()); |
| 85 | + context.ReportDiagnostic(diagnostic); |
| 86 | + |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + // 2- Iterate for each parameter |
| 91 | + for (var i = 0; i < callBackLambdaExpressionSymbol.Parameters.Length; i++) |
| 92 | + { |
| 93 | + // Special case, if the argument is IsAnyType |
| 94 | + if (moqSymbols.IsAnyType(mockedMethod.Parameters[i].Type)) |
| 95 | + { |
| 96 | + // The callback parameter associated must be an object. |
| 97 | + if (callBackLambdaExpressionSymbol.Parameters[i].Type.SpecialType != SpecialType.System_Object) |
| 98 | + { |
| 99 | + var diagnostic = Diagnostic.Create(Rule, lambdaExpression!.ParameterList.Parameters[i].GetLocation()); |
| 100 | + context.ReportDiagnostic(diagnostic); |
| 101 | + |
| 102 | + continue; |
| 103 | + } |
| 104 | + } |
| 105 | + else if (!SymbolEqualityComparer.Default.Equals(callBackLambdaExpressionSymbol.Parameters[i].Type, mockedMethod.Parameters[i].Type)) |
| 106 | + { |
| 107 | + var diagnostic = Diagnostic.Create(Rule, lambdaExpression!.ParameterList.Parameters[i].GetLocation()); |
| 108 | + context.ReportDiagnostic(diagnostic); |
| 109 | + |
| 110 | + continue; |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments