Example #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();
            }
        }
        private static void VerifyBuildErrors(ImmutableArray <Diagnostic> diagnostics, Compilation compilation)
        {
            var buildErrors = GetBuildErrors(diagnostics);

            var expectedBuildErrors = IssueLocationCollector.GetExpectedBuildErrors(compilation.SyntaxTrees.Skip(1).FirstOrDefault()?.GetText().Lines).ToList();

            CompareActualToExpected(compilation.LanguageVersionString(), buildErrors, expectedBuildErrors, true);
        }
Example #3
0
        private static void VerifyBuildErrors(ImmutableArray <Diagnostic> diagnostics, Compilation compilation)
        {
            var buildErrors         = diagnostics.Where(d => d.Id.StartsWith("CS") && d.Severity == DiagnosticSeverity.Error);
            var expectedBuildErrors = new IssueLocationCollector()
                                      .GetExpectedBuildErrors(compilation.SyntaxTrees.Skip(1).FirstOrDefault()?.GetText().Lines)
                                      .ToList();

            CompareActualToExpected(buildErrors, expectedBuildErrors, true);
        }
Example #4
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();
            }
        }
        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();
            }
        }