Ejemplo n.º 1
0
        public void TestNonReportedDiagnosticCannotBeSuppressed()
        {
            string source      = @"
class C { }";
            var    compilation = CreateCompilation(source);

            compilation.VerifyDiagnostics();

            const string nonReportedDiagnosticId = "NonReportedId";
            var          analyzer   = new CompilationAnalyzerWithSeverity(DiagnosticSeverity.Warning, configurable: true);
            var          suppressor = new DiagnosticSuppressor_NonReportedDiagnosticCannotBeSuppressed(analyzer.Descriptor.Id, nonReportedDiagnosticId);
            var          analyzersAndSuppressors = new DiagnosticAnalyzer[] { analyzer, suppressor };

            // "Non-reported diagnostic with ID '{0}' cannot be suppressed."
            var exceptionMessage = string.Format(CodeAnalysisResources.NonReportedDiagnosticCannotBeSuppressed, nonReportedDiagnosticId);

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

            VerifySuppressedDiagnostics(compilation, analyzersAndSuppressors);
        }
        public void TestNonReportedDiagnosticCannotBeSuppressed()
        {
            string source      = @"
class C { }";
            var    compilation = CreateCompilation(source);

            compilation.VerifyDiagnostics();

            const string nonReportedDiagnosticId = "NonReportedId";
            var          analyzer   = new CompilationAnalyzerWithSeverity(DiagnosticSeverity.Warning, configurable: true);
            var          suppressor = new DiagnosticSuppressor_NonReportedDiagnosticCannotBeSuppressed(analyzer.Descriptor.Id, nonReportedDiagnosticId);
            var          analyzersAndSuppressors = new DiagnosticAnalyzer[] { analyzer, suppressor };

            // "Non-reported diagnostic with ID '{0}' cannot be suppressed."
            var exceptionMessage = string.Format(CodeAnalysisResources.NonReportedDiagnosticCannotBeSuppressed, nonReportedDiagnosticId);

            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(System.ArgumentException).FullName,
                                                                             exceptionMessage,
                                                                             context)
                                          .WithLocation(1, 1),
                                          Diagnostic("ID1000", "class C { }").WithLocation(2, 1));

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