-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAnalyzerConfigurationUnitTests.cs
More file actions
92 lines (77 loc) · 3.23 KB
/
AnalyzerConfigurationUnitTests.cs
File metadata and controls
92 lines (77 loc) · 3.23 KB
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
// Copyright (c) Contributors to the New StyleCop Analyzers project.
// Licensed under the MIT License. See LICENSE in the project root for license information.
#nullable disable
namespace StyleCop.Analyzers.Test
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Testing;
using Xunit;
public class AnalyzerConfigurationUnitTests
{
public static IEnumerable<object[]> AllAnalyzers
{
get
{
foreach (var type in typeof(AnalyzerCategory).Assembly.DefinedTypes)
{
if (type.GetCustomAttributes(typeof(DiagnosticAnalyzerAttribute), true).Any())
{
yield return new object[] { type };
}
}
}
}
[Theory]
[MemberData(nameof(AllAnalyzers))]
public async Task TestEmptySourceAsync(Type analyzerType)
{
await new CSharpTest(analyzerType)
{
TestCode = string.Empty,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}
[Theory]
[MemberData(nameof(AllAnalyzers))]
public void TestHelpLink(Type analyzerType)
{
var analyzer = (DiagnosticAnalyzer)Activator.CreateInstance(analyzerType);
foreach (var diagnostic in analyzer.SupportedDiagnostics)
{
if (diagnostic.DefaultSeverity == DiagnosticSeverity.Hidden && diagnostic.CustomTags.Contains(WellKnownDiagnosticTags.NotConfigurable))
{
// This diagnostic will never appear in the UI
continue;
}
string expected = $"https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/{diagnostic.Id}.md";
Assert.Equal(expected, diagnostic.HelpLinkUri);
}
}
private class CSharpTest : CodeFixTest<DefaultVerifier>
{
private readonly Type analyzerType;
public CSharpTest(Type analyzerType)
{
this.analyzerType = analyzerType;
}
public override string Language => LanguageNames.CSharp;
public override Type SyntaxKindType => typeof(SyntaxKind);
protected override string DefaultFileExt => "cs";
protected override CompilationOptions CreateCompilationOptions()
=> new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true);
protected override ParseOptions CreateParseOptions()
=> new CSharpParseOptions(LanguageVersion.CSharp6);
protected override IEnumerable<CodeFixProvider> GetCodeFixProviders()
=> new CodeFixProvider[0];
protected override IEnumerable<DiagnosticAnalyzer> GetDiagnosticAnalyzers()
=> new[] { (DiagnosticAnalyzer)Activator.CreateInstance(this.analyzerType) };
}
}
}