示例#1
0
        public void TestExceptionFromSuppressor()
        {
            string source = "class C { }";

            var compilation = CreateCompilation(source);

            compilation.VerifyDiagnostics();

            var expectedException = new NotImplementedException();
            var analyzer          = new CompilationAnalyzerWithSeverity(
                DiagnosticSeverity.Warning,
                configurable: true
                );
            var suppressor = new DiagnosticSuppressorThrowsExceptionFromReportedSuppressions(
                analyzer.Descriptor.Id,
                expectedException
                );
            var exceptions = new List <Exception>();
            EventHandler <FirstChanceExceptionEventArgs> firstChanceException = (sender, e) =>
            {
                if (e.Exception == expectedException)
                {
                    exceptions.Add(e.Exception);
                }
            };

            try
            {
                AppDomain.CurrentDomain.FirstChanceException += firstChanceException;

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

{new LazyToString(() => exceptions[0])}
-----";
                var analyzersAndSuppresors = new DiagnosticAnalyzer[] { analyzer, suppressor };
                VerifyAnalyzerDiagnostics(
                    compilation,
                    analyzersAndSuppresors,
                    Diagnostic("AD0001")
                    .WithArguments(
                        suppressor.ToString(),
                        typeof(NotImplementedException).FullName,
                        new NotImplementedException().Message,
                        context
                        )
                    .WithLocation(1, 1),
                    Diagnostic("ID1000", "class C { }").WithLocation(1, 1)
                    );

                VerifySuppressedDiagnostics(compilation, analyzersAndSuppresors);
            }
            finally
            {
                AppDomain.CurrentDomain.FirstChanceException -= firstChanceException;
            }
        }
        public void TestExceptionFromSuppressor()
        {
            string source = "class C { }";

            var compilation = CreateCompilation(source);

            compilation.VerifyDiagnostics();

            var analyzer               = new CompilationAnalyzerWithSeverity(DiagnosticSeverity.Warning, configurable: true);
            var suppressor             = new DiagnosticSuppressorThrowsExceptionFromReportedSuppressions(analyzer.Descriptor.Id);
            var analyzersAndSuppresors = new DiagnosticAnalyzer[] { analyzer, suppressor };

            VerifyAnalyzerDiagnostics(compilation, analyzersAndSuppresors,
                                      Diagnostic("AD0001").WithArguments(suppressor.ToString(), typeof(NotImplementedException).FullName, new NotImplementedException().Message).WithLocation(1, 1),
                                      Diagnostic("ID1000", "class C { }").WithLocation(1, 1));

            VerifySuppressedDiagnostics(compilation, analyzersAndSuppresors);
        }