|
| 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 | +} |
0 commit comments