|
| 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.Collections.Generic; |
| 7 | + using System.Collections.Immutable; |
| 8 | + using System.Threading; |
| 9 | + using System.Threading.Tasks; |
| 10 | + using Microsoft.CodeAnalysis; |
| 11 | + using Microsoft.CodeAnalysis.CSharp; |
| 12 | + using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 13 | + using Microsoft.CodeAnalysis.Diagnostics; |
| 14 | + using StyleCop.Analyzers.SpecialRules; |
| 15 | + using TestHelper; |
| 16 | + using Xunit; |
| 17 | + |
| 18 | + public class AnalyzerExtensionsTests : DiagnosticVerifier |
| 19 | + { |
| 20 | + private bool invokedBlockCallback; |
| 21 | + private bool invokedMethodDeclarationCallback; |
| 22 | + |
| 23 | + [Fact] |
| 24 | + public async Task TestCompilationCallbackWithSettingsAsync() |
| 25 | + { |
| 26 | + string testCode = @" |
| 27 | +class TypeName |
| 28 | +{ |
| 29 | + void MethodName() |
| 30 | + { |
| 31 | + } |
| 32 | +} |
| 33 | +"; |
| 34 | + |
| 35 | + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 36 | + Assert.True(this.invokedBlockCallback); |
| 37 | + Assert.True(this.invokedMethodDeclarationCallback); |
| 38 | + } |
| 39 | + |
| 40 | + protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers() |
| 41 | + { |
| 42 | + yield return new CompilationStartDiagnosticAnalyzer { AnalyzerExtensionsTests = this }; |
| 43 | + } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Note that I wanted to just extend <see cref="SA0002InvalidSettingsFile"/>, but errors in the meta analyzers |
| 47 | + /// were resulting in AD0001 during the build. |
| 48 | + /// </summary> |
| 49 | + [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 50 | + internal class CompilationStartDiagnosticAnalyzer : DiagnosticAnalyzer |
| 51 | + { |
| 52 | + public const string DiagnosticId = "SA0002"; |
| 53 | + private const string HelpLink = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA0002.md"; |
| 54 | + private static readonly LocalizableString Title = new LocalizableResourceString(nameof(SpecialResources.SA0002Title), SpecialResources.ResourceManager, typeof(SpecialResources)); |
| 55 | + private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(SpecialResources.SA0002MessageFormat), SpecialResources.ResourceManager, typeof(SpecialResources)); |
| 56 | + private static readonly LocalizableString Description = new LocalizableResourceString(nameof(SpecialResources.SA0002Description), SpecialResources.ResourceManager, typeof(SpecialResources)); |
| 57 | + |
| 58 | + private static readonly DiagnosticDescriptor Descriptor = |
| 59 | + new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.SpecialRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink); |
| 60 | + |
| 61 | + /// <inheritdoc/> |
| 62 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = |
| 63 | + ImmutableArray.Create(Descriptor); |
| 64 | + |
| 65 | + public AnalyzerExtensionsTests AnalyzerExtensionsTests |
| 66 | + { |
| 67 | + get; |
| 68 | + set; |
| 69 | + } |
| 70 | + |
| 71 | + public override void Initialize(AnalysisContext context) |
| 72 | + { |
| 73 | + context.RegisterCompilationStartAction( |
| 74 | + compilationStartContext => |
| 75 | + { |
| 76 | + compilationStartContext.RegisterSyntaxNodeAction( |
| 77 | + (syntaxNodeContext, settings) => |
| 78 | + { |
| 79 | + Assert.IsAssignableFrom<BlockSyntax>(syntaxNodeContext.Node); |
| 80 | + Assert.NotNull(settings); |
| 81 | + this.AnalyzerExtensionsTests.invokedBlockCallback = true; |
| 82 | + }, |
| 83 | + SyntaxKind.Block); |
| 84 | + compilationStartContext.RegisterSyntaxNodeAction( |
| 85 | + (syntaxNodeContext, settings) => |
| 86 | + { |
| 87 | + Assert.IsAssignableFrom<MethodDeclarationSyntax>(syntaxNodeContext.Node); |
| 88 | + Assert.NotNull(settings); |
| 89 | + this.AnalyzerExtensionsTests.invokedMethodDeclarationCallback = true; |
| 90 | + }, |
| 91 | + ImmutableArray.Create(SyntaxKind.MethodDeclaration)); |
| 92 | + }); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments