Example #1
0
        public static void VerifyAnalyzer(string path, SonarDiagnosticAnalyzer diagnosticAnalyzer, ParseOptions options = null,
            params MetadataReference[] additionalReferences)
        {
            var file = new FileInfo(path);
            var parseOptions = GetParseOptionsAlternatives(options, file.Extension);

            using (var workspace = new AdhocWorkspace())
            {
                var document = GetDocument(file, GeneratedAssemblyName, workspace, additionalReferences: additionalReferences);
                var project = document.Project;

                foreach (var parseOption in parseOptions)
                {
                    if (parseOption != null)
                    {
                        project = project.WithParseOptions(parseOption);
                    }

                    var compilation = project.GetCompilationAsync().Result;
                    var diagnostics = GetDiagnostics(compilation, diagnosticAnalyzer);
                    var expectedIssues = ExpectedIssues(compilation.SyntaxTrees.First());

                    var language = file.Extension == CSharpFileExtension
                        ? LanguageNames.CSharp
                        : LanguageNames.VisualBasic;
                    var expectedLocations = ExpectedIssueLocations(compilation.SyntaxTrees.First(), language);

                    foreach (var diagnostic in diagnostics)
                    {
                        var line = diagnostic.GetLineNumberToReport();
                        if (!expectedIssues.ContainsKey(line))
                        {
                            Assert.Fail($"Issue not expected on line {line}");
                        }

                        var expectedMessage = expectedIssues[line];
                        if (expectedMessage != null)
                        {
                            var message = diagnostic.GetMessage();

                            Assert.AreEqual(expectedMessage,
                                message,
                                $"Message does not match expected on line {line}");
                        }

                        if (expectedLocations.ContainsKey(line))
                        {
                            var expectedLocation = expectedLocations[line];

                            var diagnosticStart = diagnostic.Location.GetLineSpan().StartLinePosition.Character;
                            Assert.AreEqual(expectedLocation.StartPosition, diagnosticStart,
                                $"The start position of the diagnostic ({diagnosticStart}) doesn't match " +
                                $"the expected value ({expectedLocation.StartPosition}) in line {line}.");

                            Assert.AreEqual(expectedLocation.Length, diagnostic.Location.SourceSpan.Length,
                                $"The length of the diagnostic ({diagnostic.Location.SourceSpan.Length}) doesn't match " +
                                $"the expected value ({expectedLocation.Length}) in line {line}.");

                            expectedLocations.Remove(line);
                        }

                        expectedIssues.Remove(line);
                    }

                    expectedLocations.Should().BeEmpty();
                    Assert.AreEqual(0, expectedIssues.Count, $"Issue not found on line {string.Join(",", expectedIssues.Keys)}.");
                }
            }
        }
Example #2
0
        public static void VerifyCodeFix(string path, string pathToExpected, string pathToBatchExpected, SonarDiagnosticAnalyzer diagnosticAnalyzer,
            SonarCodeFixProvider codeFixProvider, string codeFixTitle)
        {
            using (var workspace = new AdhocWorkspace())
            {
                var file = new FileInfo(path);
                var parseOptions = GetParseOptionsWithDifferentLanguageVersions(null, file.Extension);

                foreach (var parseOption in parseOptions)
                {
                    var document = GetDocument(file, GeneratedAssemblyName, workspace, removeAnalysisComments: true);
                    RunCodeFixWhileDocumentChanges(diagnosticAnalyzer, codeFixProvider, codeFixTitle, document, parseOption, pathToExpected);
                }
            }

            VerifyFixAllCodeFix(path, pathToBatchExpected, diagnosticAnalyzer, codeFixProvider, codeFixTitle);
        }
Example #3
0
 public static void VerifyCodeFix(string path, string pathToExpected, string pathToBatchExpected, SonarDiagnosticAnalyzer diagnosticAnalyzer,
     SonarCodeFixProvider codeFixProvider)
 {
     VerifyCodeFix(path, pathToExpected, pathToBatchExpected, diagnosticAnalyzer, codeFixProvider, null);
 }
Example #4
0
 public static void VerifyCodeFix(string path, string pathToExpected, SonarDiagnosticAnalyzer diagnosticAnalyzer,
     SonarCodeFixProvider codeFixProvider, string codeFixTitle)
 {
     VerifyCodeFix(path, pathToExpected, pathToExpected, diagnosticAnalyzer, codeFixProvider, codeFixTitle);
 }
Example #5
0
 public static void VerifyNoIssueReported(string path, SonarDiagnosticAnalyzer diagnosticAnalyzer)
 {
     VerifyNoIssueReported(path, GeneratedAssemblyName, diagnosticAnalyzer);
 }