-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathTestMethodShouldContainAssertion.cs
164 lines (146 loc) · 8.01 KB
/
TestMethodShouldContainAssertion.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* SonarAnalyzer for .NET
* Copyright (C) 2015-2022 SonarSource SA
* mailto: contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using SonarAnalyzer.Helpers;
using SonarAnalyzer.Wrappers;
using StyleCop.Analyzers.Lightup;
namespace SonarAnalyzer.Rules.CSharp
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class TestMethodShouldContainAssertion : SonarDiagnosticAnalyzer
{
internal const string DiagnosticId = "S2699";
private const string MessageFormat = "Add at least one assertion to this test case.";
private const string CustomAssertionAttributeName = "AssertionMethodAttribute";
private const int MaxInvocationDepth = 2; // Consider BFS instead of DFS if this gets increased
private static readonly Dictionary<string, KnownType> KnownAssertions = new Dictionary<string, KnownType>
{
{"DidNotReceive", KnownType.NSubstitute_SubstituteExtensions},
{"DidNotReceiveWithAnyArgs", KnownType.NSubstitute_SubstituteExtensions},
{"Received", KnownType.NSubstitute_SubstituteExtensions},
{"ReceivedWithAnyArgs", KnownType.NSubstitute_SubstituteExtensions},
{"InOrder", KnownType.NSubstitute_Received }
};
/// The assertions in the Shouldly library are supported by <see cref="UnitTestHelper.KnownAssertionMethodParts"/> (they all contain "Should")
private static readonly ImmutableArray<KnownType> KnownAssertionTypes = ImmutableArray.Create(
KnownType.Microsoft_VisualStudio_TestTools_UnitTesting_Assert,
KnownType.NFluent_Check,
KnownType.NUnit_Framework_Assert,
KnownType.Xunit_Assert);
private static readonly ImmutableArray<KnownType> KnownAsertionExceptionTypes = ImmutableArray.Create(
KnownType.Microsoft_VisualStudio_TestTools_UnitTesting_AssertFailedException,
KnownType.NFluent_FluentCheckException,
KnownType.NFluent_Kernel_FluentCheckException,
KnownType.NUnit_Framework_AssertionException,
KnownType.Xunit_Sdk_AssertException,
KnownType.Xunit_Sdk_XunitException);
private static readonly DiagnosticDescriptor Rule = DescriptorFactory.Create(DiagnosticId, MessageFormat);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule);
protected override void Initialize(SonarAnalysisContext context) =>
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
{
var methodDeclaration = MethodDeclarationFactory.Create(c.Node);
if (!methodDeclaration.Identifier.IsMissing
&& methodDeclaration.HasImplementation
&& c.SemanticModel.GetDeclaredSymbol(c.Node) is IMethodSymbol methodSymbol
&& IsTestMethod(methodSymbol, methodDeclaration.IsLocal)
&& !methodSymbol.HasExpectedExceptionAttribute()
&& !methodSymbol.HasAssertionInAttribute()
&& !IsTestIgnored(methodSymbol)
&& !ContainsAssertion(c.Node, c.SemanticModel, new HashSet<IMethodSymbol>(), 0))
{
c.ReportIssue(Diagnostic.Create(Rule, methodDeclaration.Identifier.GetLocation()));
}
},
SyntaxKind.MethodDeclaration,
SyntaxKindEx.LocalFunctionStatement);
// only xUnit allows local functions to be test methods.
private static bool IsTestMethod(IMethodSymbol symbol, bool isLocalFunction) =>
isLocalFunction ? IsXunitTestMethod(symbol) : symbol.IsTestMethod();
private static bool IsXunitTestMethod(IMethodSymbol methodSymbol) =>
methodSymbol.AnyAttributeDerivesFromAny(UnitTestHelper.KnownTestMethodAttributesOfxUnit);
private static bool ContainsAssertion(SyntaxNode methodDeclaration, SemanticModel previousSemanticModel, ISet<IMethodSymbol> visitedSymbols, int level)
{
var currentSemanticModel = methodDeclaration.EnsureCorrectSemanticModelOrDefault(previousSemanticModel);
if (currentSemanticModel == null)
{
return false;
}
var descendantNodes = methodDeclaration.DescendantNodes();
var invocations = descendantNodes.OfType<InvocationExpressionSyntax>().ToArray();
if (invocations.Any(x => IsAssertion(x))
|| descendantNodes.OfType<ThrowStatementSyntax>().Any(x => x.Expression != null && currentSemanticModel.GetTypeInfo(x.Expression).Type.DerivesFromAny(KnownAsertionExceptionTypes)))
{
return true;
}
var invokedSymbols = invocations.Select(expression => currentSemanticModel.GetSymbolInfo(expression).Symbol).OfType<IMethodSymbol>();
if (invokedSymbols.Any(symbol => IsKnownAssertion(symbol) || IsCustomAssertion(symbol)))
{
return true;
}
if (level == MaxInvocationDepth)
{
return false;
}
foreach (var symbol in invokedSymbols.Where(x => !visitedSymbols.Contains(x)))
{
visitedSymbols.Add(symbol);
foreach (var invokedDeclaration in symbol.DeclaringSyntaxReferences.Select(x => x.GetSyntax()).OfType<MethodDeclarationSyntax>())
{
if (ContainsAssertion(invokedDeclaration, currentSemanticModel, visitedSymbols, level + 1))
{
return true;
}
}
}
return false;
}
private static bool IsTestIgnored(IMethodSymbol method)
{
if (method.IsMsTestOrNUnitTestIgnored())
{
return true;
}
// Checking whether an Xunit test is ignore or not needs to be done at the syntax level i.e. language-specific
var factAttributeSyntax = method.FindXUnitTestAttribute()
?.ApplicationSyntaxReference.GetSyntax() as AttributeSyntax;
return factAttributeSyntax?.ArgumentList != null
&& factAttributeSyntax.ArgumentList.Arguments.Any(x => x.NameEquals.Name.Identifier.ValueText == "Skip");
}
private static bool IsAssertion(InvocationExpressionSyntax invocation) =>
invocation.Expression
.ToString()
.SplitCamelCaseToWords()
.Intersect(UnitTestHelper.KnownAssertionMethodParts)
.Any();
private static bool IsKnownAssertion(ISymbol methodSymbol) =>
(KnownAssertions.GetValueOrDefault(methodSymbol.Name) is { } type && methodSymbol.ContainingType.ConstructedFrom.Is(type))
|| methodSymbol.ContainingType.DerivesFromAny(KnownAssertionTypes);
private static bool IsCustomAssertion(ISymbol methodSymbol) =>
methodSymbol.GetAttributes().Any(x => x.AttributeClass.Name == CustomAssertionAttributeName);
}
}