Skip to content

Commit 03f41c5

Browse files
committed
Implement TestEmptySourceAsync and TestHelpLink independently of DiagnosticVerifier
1 parent 42bf8da commit 03f41c5

3 files changed

Lines changed: 88 additions & 34 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.CSharp7
5+
{
6+
public class AnalyzerConfigurationTestsCSharp7 : AnalyzerConfigurationTests
7+
{
8+
}
9+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test
5+
{
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Threading;
10+
using System.Threading.Tasks;
11+
using Microsoft.CodeAnalysis;
12+
using Microsoft.CodeAnalysis.CodeFixes;
13+
using Microsoft.CodeAnalysis.Diagnostics;
14+
using StyleCop.Analyzers.Test.Verifiers;
15+
using Xunit;
16+
17+
public class AnalyzerConfigurationTests
18+
{
19+
public static IEnumerable<object[]> AllAnalyzers
20+
{
21+
get
22+
{
23+
foreach (var type in typeof(AnalyzerCategory).Assembly.DefinedTypes)
24+
{
25+
if (type.GetCustomAttributes(typeof(DiagnosticAnalyzerAttribute), true).Any())
26+
{
27+
yield return new object[] { type };
28+
}
29+
}
30+
}
31+
}
32+
33+
[Theory]
34+
[MemberData(nameof(AllAnalyzers))]
35+
public async Task TestEmptySourceAsync(Type analyzerType)
36+
{
37+
await new CSharpTest(analyzerType)
38+
{
39+
TestCode = string.Empty,
40+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
41+
}
42+
43+
[Theory]
44+
[MemberData(nameof(AllAnalyzers))]
45+
public void TestHelpLink(Type analyzerType)
46+
{
47+
var analyzer = (DiagnosticAnalyzer)Activator.CreateInstance(analyzerType);
48+
foreach (var diagnostic in analyzer.SupportedDiagnostics)
49+
{
50+
if (diagnostic.DefaultSeverity == DiagnosticSeverity.Hidden && diagnostic.CustomTags.Contains(WellKnownDiagnosticTags.NotConfigurable))
51+
{
52+
// This diagnostic will never appear in the UI
53+
continue;
54+
}
55+
56+
string expected = $"https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/{diagnostic.Id}.md";
57+
Assert.Equal(expected, diagnostic.HelpLinkUri);
58+
}
59+
}
60+
61+
private class CSharpTest : GenericAnalyzerTest
62+
{
63+
private readonly Type analyzerType;
64+
65+
public CSharpTest(Type analyzerType)
66+
{
67+
this.analyzerType = analyzerType;
68+
}
69+
70+
public override string Language => LanguageNames.CSharp;
71+
72+
protected override IEnumerable<CodeFixProvider> GetCodeFixProviders()
73+
=> new CodeFixProvider[0];
74+
75+
protected override IEnumerable<DiagnosticAnalyzer> GetDiagnosticAnalyzers()
76+
=> new[] { (DiagnosticAnalyzer)Activator.CreateInstance(this.analyzerType) };
77+
}
78+
}
79+
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/Verifiers/DiagnosticVerifier.cs

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -72,40 +72,6 @@ public int TabSize
7272

7373
protected static DiagnosticResult[] EmptyDiagnosticResults { get; } = { };
7474

75-
/// <summary>
76-
/// Verifies that the analyzer will properly handle an empty source.
77-
/// </summary>
78-
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
79-
[Fact]
80-
public async Task TestEmptySourceAsync()
81-
{
82-
var testCode = string.Empty;
83-
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
84-
}
85-
86-
/// <summary>
87-
/// Verifies that each diagnostics contains a <see cref="DiagnosticDescriptor.HelpLinkUri"/> in the expected
88-
/// format.
89-
/// </summary>
90-
[Fact]
91-
public void TestHelpLink()
92-
{
93-
foreach (var diagnosticAnalyzer in this.GetCSharpDiagnosticAnalyzers())
94-
{
95-
foreach (var diagnostic in diagnosticAnalyzer.SupportedDiagnostics)
96-
{
97-
if (diagnostic.DefaultSeverity == DiagnosticSeverity.Hidden && diagnostic.CustomTags.Contains(WellKnownDiagnosticTags.NotConfigurable))
98-
{
99-
// This diagnostic will never appear in the UI.
100-
continue;
101-
}
102-
103-
string expected = $"https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/{diagnostic.Id}.md";
104-
Assert.Equal(expected, diagnostic.HelpLinkUri);
105-
}
106-
}
107-
}
108-
10975
/// <summary>
11076
/// Gets the C# analyzers being tested.
11177
/// </summary>

0 commit comments

Comments
 (0)