Пример #1
0
        public static void Verify(Compilation compilation, DiagnosticAnalyzer[] diagnosticAnalyzers, CompilationErrorBehavior checkMode)
        {
            try
            {
                SuppressionHandler.HookSuppression();

                var diagnostics = GetDiagnostics(compilation, diagnosticAnalyzers, checkMode);

                var expectedIssues = new IssueLocationCollector()
                                     .GetExpectedIssueLocations(compilation.SyntaxTrees.Skip(1).First().GetText().Lines)
                                     .ToList();

                CompareActualToExpected(diagnostics, expectedIssues, false);

                // When there are no diagnostics reported from the test (for example the FileLines analyzer
                // does not report in each call to Verifier.VerifyAnalyzer) we skip the check for the extension
                // method.
                if (diagnostics.Any())
                {
                    SuppressionHandler.ExtensionMethodsCalledForAllDiagnostics(diagnosticAnalyzers).Should()
                    .BeTrue("The ReportDiagnosticWhenActive should be used instead of ReportDiagnostic");
                }
            }
            finally
            {
                SuppressionHandler.UnHookSuppression();
            }
        }
Пример #2
0
        public static void Verify(Compilation compilation, DiagnosticAnalyzer diagnosticAnalyzer)
        {
            try
            {
                SuppressionHandler.HookSuppression();

                var diagnostics = GetDiagnostics(compilation, diagnosticAnalyzer);

                var expectedIssues = new IssueLocationCollector()
                                     .GetExpectedIssueLocations(compilation.SyntaxTrees.Skip(1).First().GetText().Lines)
                                     .ToList();

                foreach (var diagnostic in diagnostics)
                {
                    VerifyIssue(expectedIssues,
                                issue => issue.IsPrimary,
                                diagnostic.Location,
                                diagnostic.GetMessage(),
                                out var issueId);

                    var secondaryLocations = diagnostic.AdditionalLocations
                                             .Select((location, i) => diagnostic.GetSecondaryLocation(i))
                                             .OrderBy(x => x.Location.GetLineNumberToReport())
                                             .ThenBy(x => x.Location.GetLineSpan().StartLinePosition.Character);

                    foreach (var secondaryLocation in secondaryLocations)
                    {
                        VerifyIssue(expectedIssues,
                                    issue => issue.IssueId == issueId && !issue.IsPrimary,
                                    secondaryLocation.Location,
                                    secondaryLocation.Message,
                                    out issueId);
                    }
                }

                if (expectedIssues.Count != 0)
                {
                    Execute.Assertion.FailWith($"Issue expected but not raised on line(s) " +
                                               $"{string.Join(",", expectedIssues.Select(i => i.LineNumber))}.");
                }

                // When there are no diagnostics reported from the test (for example the FileLines analyzer
                // does not report in each call to Verifier.VerifyAnalyzer) we skip the check for the extension
                // method.
                if (diagnostics.Any())
                {
                    SuppressionHandler.ExtensionMethodsCalledForAllDiagnostics(diagnosticAnalyzer).Should()
                    .BeTrue("The ReportDiagnosticWhenActive should be used instead of ReportDiagnostic");
                }
            }
            finally
            {
                SuppressionHandler.UnHookSuppression();
            }
        }
Пример #3
0
        public static void Verify(Compilation compilation, DiagnosticAnalyzer[] diagnosticAnalyzers, CompilationErrorBehavior checkMode, SourceText source, string sonarProjectConfigPath = null)
        {
            SuppressionHandler.HookSuppression();
            try
            {
                var diagnostics    = GetDiagnostics(compilation, diagnosticAnalyzers, checkMode, sonarProjectConfigPath: sonarProjectConfigPath);
                var expectedIssues = IssueLocationCollector.GetExpectedIssueLocations(source.Lines).ToList();
                CompareActualToExpected(compilation.LanguageVersionString(), diagnostics, expectedIssues, false);

                // When there are no diagnostics reported from the test (for example the FileLines analyzer
                // does not report in each call to Verifier.VerifyAnalyzer) we skip the check for the extension
                // method.
                if (diagnostics.Any())
                {
                    SuppressionHandler.ExtensionMethodsCalledForAllDiagnostics(diagnosticAnalyzers).Should().BeTrue("The ReportDiagnosticWhenActive should be used instead of ReportDiagnostic");
                }
            }
            finally
            {
                SuppressionHandler.UnHookSuppression();
            }
        }