-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
MissingCombinativeValuesAnalyzer.cs
71 lines (61 loc) · 2.48 KB
/
MissingCombinativeValuesAnalyzer.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
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using TUnit.Analyzers.Extensions;
using TUnit.Analyzers.Helpers;
namespace TUnit.Analyzers;
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class MissingMatrixAnalyzer : ConcurrentDiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
ImmutableArray.Create(Rules.NoTestDataProvided);
protected override void InitializeInternal(AnalysisContext context)
{
context.RegisterSymbolAction(AnalyzeSymbol, SymbolKind.Method);
}
private void AnalyzeSymbol(SymbolAnalysisContext context)
{
if (context.Symbol is not IMethodSymbol methodSymbol)
{
return;
}
if (!methodSymbol.GetAttributes().Any(x =>
x.AttributeClass?.ToDisplayString(DisplayFormats.FullyQualifiedNonGenericWithGlobalPrefix)
== "global::TUnit.Core.MatrixTestAttribute"))
{
return;
}
var parameters = methodSymbol.Parameters.IsDefaultOrEmpty
? []
: methodSymbol.Parameters.ToList();
if (!parameters.Any())
{
context.ReportDiagnostic(
Diagnostic.Create(Rules.NoTestDataProvided,
context.Symbol.Locations.FirstOrDefault())
);
return;
}
if (methodSymbol.HasTimeoutAttribute(out _)
&& SymbolEqualityComparer.Default.Equals(parameters.LastOrDefault()?.Type,
context.Compilation.GetTypeByMetadataName(typeof(CancellationToken).FullName!)))
{
parameters.RemoveAt(parameters.Count - 1);
}
foreach (var parameterSymbol in parameters)
{
var matrixAttribute = parameterSymbol.GetAttributes().FirstOrDefault(attribute =>
attribute.AttributeClass?.ToDisplayString(DisplayFormats.FullyQualifiedNonGenericWithGlobalPrefix)
== WellKnown.AttributeFullyQualifiedClasses.Matrix);
if (matrixAttribute is null
|| matrixAttribute.ConstructorArguments.IsDefaultOrEmpty
|| matrixAttribute.ConstructorArguments[0].Values.IsDefaultOrEmpty)
{
context.ReportDiagnostic(
Diagnostic.Create(Rules.NoTestDataProvided,
parameterSymbol.Locations.FirstOrDefault())
);
}
}
}
}