Beispiel #1
0
        public void TestUnsupportedSuppressionReported()
        {
            string source = @"
class C { }";

            var compilation = CreateCompilation(source);

            compilation.VerifyDiagnostics();

            const string supportedSuppressionId   = "supportedId";
            const string unsupportedSuppressionId = "unsupportedId";
            var          analyzer               = new CompilationAnalyzerWithSeverity(DiagnosticSeverity.Warning, configurable: true);
            var          suppressor             = new DiagnosticSuppressor_UnsupportedSuppressionReported(analyzer.Descriptor.Id, supportedSuppressionId, unsupportedSuppressionId);
            var          analyzersAndSuppresors = new DiagnosticAnalyzer[] { analyzer, suppressor };

            // "Reported suppression with ID '{0}' is not supported by the suppressor."
            var exceptionMessage = string.Format(CodeAnalysisResources.UnsupportedSuppressionReported, unsupportedSuppressionId);

            VerifyAnalyzerDiagnostics(compilation, analyzersAndSuppresors,
                                      Diagnostic("AD0001").WithArguments(suppressor.ToString(),
                                                                         typeof(ArgumentException).FullName,
                                                                         exceptionMessage)
                                      .WithLocation(1, 1),
                                      Diagnostic("ID1000", "class C { }").WithLocation(2, 1));

            VerifySuppressedDiagnostics(compilation, analyzersAndSuppresors);
        }
        public void TestUnsupportedSuppressionReported()
        {
            string source = @"
class C { }";

            var compilation = CreateCompilation(source);

            compilation.VerifyDiagnostics();

            const string supportedSuppressionId   = "supportedId";
            const string unsupportedSuppressionId = "unsupportedId";
            var          analyzer   = new CompilationAnalyzerWithSeverity(DiagnosticSeverity.Warning, configurable: true);
            var          suppressor = new DiagnosticSuppressor_UnsupportedSuppressionReported(analyzer.Descriptor.Id, supportedSuppressionId, unsupportedSuppressionId);
            var          analyzersAndSuppressors = new DiagnosticAnalyzer[] { analyzer, suppressor };

            // "Reported suppression with ID '{0}' is not supported by the suppressor."
            var exceptionMessage = string.Format(CodeAnalysisResources.UnsupportedSuppressionReported, unsupportedSuppressionId);

            var exceptions = new List <Exception>();
            EventHandler <FirstChanceExceptionEventArgs> firstChanceException =
                (sender, e) =>
            {
                if (e.Exception is ArgumentException &&
                    e.Exception.Message == exceptionMessage)
                {
                    exceptions.Add(e.Exception);
                }
            };

            try
            {
                AppDomain.CurrentDomain.FirstChanceException += firstChanceException;

                IFormattable context = $@"{string.Format(CodeAnalysisResources.ExceptionContext, $@"Compilation: {compilation.AssemblyName}")}

{new LazyToString(() => exceptions[0])}
-----";
                VerifyAnalyzerDiagnostics(compilation, analyzersAndSuppressors,
                                          Diagnostic("AD0001").WithArguments(suppressor.ToString(),
                                                                             typeof(ArgumentException).FullName,
                                                                             exceptionMessage,
                                                                             context)
                                          .WithLocation(1, 1),
                                          Diagnostic("ID1000", "class C { }").WithLocation(2, 1));

                VerifySuppressedDiagnostics(compilation, analyzersAndSuppressors);
            }
            finally
            {
                AppDomain.CurrentDomain.FirstChanceException -= firstChanceException;
            }
        }