-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathTestMethodShouldContainAssertion.cs
153 lines (130 loc) · 7.09 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
/*
* SonarAnalyzer for .NET
* Copyright (C) 2014-2025 SonarSource SA
* mailto:info AT sonarsource DOT com
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
*
* 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 Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
using SonarAnalyzer.CFG.Extensions;
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()
{
{"DidNotReceive", [KnownType.NSubstitute_SubstituteExtensions] },
{"DidNotReceiveWithAnyArgs", [KnownType.NSubstitute_SubstituteExtensions] },
{"Received", [KnownType.NSubstitute_SubstituteExtensions, KnownType.NSubstitute_ReceivedExtensions_ReceivedExtensions] },
{"ReceivedWithAnyArgs", [KnownType.NSubstitute_SubstituteExtensions, KnownType.NSubstitute_ReceivedExtensions_ReceivedExtensions] },
{"InOrder", [KnownType.NSubstitute_Received] }
};
/// The assertions in the Shouldly and Moq libraries are supported by <see cref="UnitTestHelper.KnownAssertionMethodParts"/>
/// - All assertions in Shouldly contain "Should" in their name.
/// - All assertions in Moq contain "Verify" in their name.
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> KnownAssertionExceptionTypes = 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.RegisterNodeAction(
c =>
{
var methodDeclaration = MethodDeclarationFactory.Create(c.Node);
if (!methodDeclaration.Identifier.IsMissing
&& methodDeclaration.HasImplementation
&& c.Model.GetDeclaredSymbol(c.Node) is IMethodSymbol methodSymbol
&& IsTestMethod(methodSymbol, methodDeclaration.IsLocal)
&& !methodSymbol.HasExpectedExceptionAttribute()
&& !methodSymbol.HasAssertionInAttribute()
&& !IsTestIgnored(methodSymbol)
&& !ContainsAssertion(c.Node, c.Model, new HashSet<IMethodSymbol>(), 0))
{
c.ReportIssue(Rule, methodDeclaration.Identifier);
}
},
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 model, ISet<IMethodSymbol> visitedSymbols, int level)
{
var currentModel = methodDeclaration.EnsureCorrectSemanticModelOrDefault(model);
if (currentModel is null)
{
return false;
}
var descendantNodes = methodDeclaration.DescendantNodes();
var invocations = descendantNodes.OfType<InvocationExpressionSyntax>().ToArray();
if (Array.Exists(invocations, IsAssertion)
|| descendantNodes.OfType<ThrowStatementSyntax>().Any(x => x.Expression is not null && currentModel.GetTypeInfo(x.Expression).Type.DerivesFromAny(KnownAssertionExceptionTypes)))
{
return true;
}
var invokedSymbols = invocations.Select(x => currentModel.GetSymbolInfo(x).Symbol).OfType<IMethodSymbol>();
if (invokedSymbols.Any(x => IsKnownAssertion(x) || IsCustomAssertion(x)))
{
return true;
}
if (level == MaxInvocationDepth)
{
return false;
}
foreach (var symbol in invokedSymbols.Where(x => !visitedSymbols.Contains(x)))
{
visitedSymbols.Add(symbol);
if (symbol.DeclaringSyntaxReferences.Select(x => x.GetSyntax()).OfType<MethodDeclarationSyntax>().Any(x => ContainsAssertion(x, currentModel, 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 is not 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 { } types && Array.Exists(types, x => methodSymbol.ContainingType.ConstructedFrom.Is(x)))
|| methodSymbol.ContainingType.DerivesFromAny(KnownAssertionTypes);
private static bool IsCustomAssertion(ISymbol methodSymbol) =>
methodSymbol.GetAttributesWithInherited().Any(x => x.AttributeClass.Name == CustomAssertionAttributeName);
}