|
| 1 | +//----------------------------------------------------------------------- |
| 2 | +// <copyright file="ReturnsMethodDelegateMustMatchMockedMethodAnalyzer.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 ReturnsMethodDelegateMustMatchMockedMethodAnalyzer : DiagnosticAnalyzer |
| 17 | + { |
| 18 | + internal static readonly DiagnosticDescriptor ReturnValueMustMatchRule = new DiagnosticDescriptor( |
| 19 | + "PosInfoMoq2012", |
| 20 | + "The delegate in the argument of the Returns() method must return a value with same type of the mocked method/property", |
| 21 | + "The delegate in the argument of the Returns() method must return a '{0}' type value", |
| 22 | + "Compilation", |
| 23 | + DiagnosticSeverity.Error, |
| 24 | + isEnabledByDefault: true, |
| 25 | + description: "The delegate in the argument of the Returns() method must return a value with same type of the mocked method/property.", |
| 26 | + helpLinkUri: "https://posinformatique.github.io/PosInformatique.Moq.Analyzers/docs/Compilation/PosInfoMoq2012.html"); |
| 27 | + |
| 28 | + private static readonly DiagnosticDescriptor ArgumentMustMatchRule = new DiagnosticDescriptor( |
| 29 | + "PosInfoMoq2013", |
| 30 | + "The delegate in the argument of the Returns()/ReturnsAsync() method must have the same parameter types of the mocked method/property", |
| 31 | + "The delegate in the argument of the Returns()/ReturnsAsync() method must have the same parameter types of the mocked method/property", |
| 32 | + "Compilation", |
| 33 | + DiagnosticSeverity.Error, |
| 34 | + isEnabledByDefault: true, |
| 35 | + description: "The delegate in the argument of the Returns()/ReturnsAsync() method must have the same parameter types of the mocked method/property.", |
| 36 | + helpLinkUri: "https://posinformatique.github.io/PosInformatique.Moq.Analyzers/docs/Compilation/PosInfoMoq2012.html"); |
| 37 | + |
| 38 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(ReturnValueMustMatchRule, ArgumentMustMatchRule); |
| 39 | + |
| 40 | + public override void Initialize(AnalysisContext context) |
| 41 | + { |
| 42 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 43 | + context.EnableConcurrentExecution(); |
| 44 | + |
| 45 | + context.RegisterSyntaxNodeAction(Analyze, SyntaxKind.InvocationExpression); |
| 46 | + } |
| 47 | + |
| 48 | + private static void Analyze(SyntaxNodeAnalysisContext context) |
| 49 | + { |
| 50 | + var invocationExpression = (InvocationExpressionSyntax)context.Node; |
| 51 | + |
| 52 | + var moqSymbols = MoqSymbols.FromCompilation(context.Compilation); |
| 53 | + |
| 54 | + if (moqSymbols is null) |
| 55 | + { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + var invocationExpressionSymbol = context.SemanticModel.GetSymbolInfo(invocationExpression, context.CancellationToken); |
| 60 | + |
| 61 | + if (invocationExpressionSymbol.Symbol is not IMethodSymbol methodSymbol) |
| 62 | + { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + if (!moqSymbols.IsReturnsMethod(methodSymbol) && !moqSymbols.IsReturnsAsyncMethod(methodSymbol)) |
| 67 | + { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + // Gets the first argument of the Returns() method. |
| 72 | + if (invocationExpression.ArgumentList.Arguments.Count != 1) |
| 73 | + { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + var firstArgumentExpression = invocationExpression.ArgumentList.Arguments[0].Expression; |
| 78 | + |
| 79 | + if (firstArgumentExpression is not ParenthesizedLambdaExpressionSyntax delegateMethodSyntax) |
| 80 | + { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + var firstArgumentSymbol = context.SemanticModel.GetSymbolInfo(firstArgumentExpression, context.CancellationToken); |
| 85 | + |
| 86 | + if (firstArgumentSymbol.Symbol is not IMethodSymbol delegateMethodSymbol) |
| 87 | + { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + var moqExpressionAnalyzer = new MoqExpressionAnalyzer(moqSymbols, context.SemanticModel); |
| 92 | + |
| 93 | + // Extract the Setup() method. |
| 94 | + var setupMethod = moqExpressionAnalyzer.ExtractSetupMethod(invocationExpression, context.CancellationToken); |
| 95 | + |
| 96 | + if (setupMethod is null) |
| 97 | + { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + // Check the return type |
| 102 | + if (!moqSymbols.IsReturnsAsyncMethod(methodSymbol)) |
| 103 | + { |
| 104 | + var expectedReturnType = setupMethod.ReturnType; |
| 105 | + |
| 106 | + if (!moqSymbols.IsAnyType(expectedReturnType) && !SymbolEqualityComparer.Default.Equals(delegateMethodSymbol.ReturnType, expectedReturnType)) |
| 107 | + { |
| 108 | + context.ReportDiagnostic(ReturnValueMustMatchRule, firstArgumentExpression.GetLocation(), expectedReturnType.Name); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // Check the argument types. |
| 113 | + if (setupMethod.IsProperty) |
| 114 | + { |
| 115 | + if (delegateMethodSymbol.Parameters.Length > 0) |
| 116 | + { |
| 117 | + // With property, the Returns() method must have no arguments. |
| 118 | + context.ReportDiagnostic(ArgumentMustMatchRule, delegateMethodSyntax.ParameterList.GetLocation()); |
| 119 | + } |
| 120 | + |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + if (delegateMethodSymbol.Parameters.Length == 0) |
| 125 | + { |
| 126 | + // No argument in the delegate method, Moq accept it. |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + if (delegateMethodSymbol.Parameters.Length != setupMethod.InvocationArguments.Count) |
| 131 | + { |
| 132 | + context.ReportDiagnostic(ArgumentMustMatchRule, delegateMethodSyntax.ParameterList.GetLocation()); |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + for (var i = 0; i < delegateMethodSymbol.Parameters.Length; i++) |
| 137 | + { |
| 138 | + if (!SymbolEqualityComparer.Default.Equals(delegateMethodSymbol.Parameters[i].Type, setupMethod.InvocationArguments[i].ParameterSymbol.Type)) |
| 139 | + { |
| 140 | + context.ReportDiagnostic(ArgumentMustMatchRule, delegateMethodSyntax.ParameterList.Parameters[i].GetLocation()); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments