Esempio n. 1
0
        /// <summary>
        /// Helper method to VerifyDiagnosticResult that checks the location of a diagnostic and
        /// compares it with the location in the expected DiagnosticResult.
        /// </summary>
        /// <param name="analyzer">The analyzer that was being run on the sources</param>
        /// <param name="diagnostic">The diagnostic that was found in the code</param>
        /// <param name="actual">The Location of the Diagnostic found in the code</param>
        /// <param name="expected">The DiagnosticResultLocation that should have been found</param>
        private static void VerifyDiagnosticLocation(DiagnosticAnalyzer analyzer,
                                                     string documentsWithLineNumbers,
                                                     Diagnostic diagnostic,
                                                     Location actual,
                                                     DiagnosticResultLocation expected,
                                                     string language)
        {
            var actualSpan = actual.GetLineSpan();

            Assert.IsTrue(actualSpan.Path == expected.Path ||
                          (actualSpan.Path != null && actualSpan.Path.Contains("Test0.") && expected.Path.Contains("Test.")),
                          $@"{documentsWithLineNumbers}
Expected diagnostic to be in file ""{expected.Path}"" was actually in file ""{actualSpan.Path}""

Diagnostic:
    {FormatDiagnostics(analyzer, diagnostic)}
(Language: {language})
 ");

            var actualLinePosition = actualSpan.StartLinePosition;

            // Only check line position if there is an actual line in the real diagnostic
            if (actualLinePosition.Line > 0)
            {
                if (actualLinePosition.Line + 1 != expected.Line)
                {
                    Assert.IsTrue(false,
                                  $@"{documentsWithLineNumbers}
Expected diagnostic to be on line ""{expected.Line}"" was actually on line ""{actualLinePosition.Line + 1}""

Diagnostic:
    {FormatDiagnostics(analyzer, diagnostic)}
(Language: {language})
 ");
                }
            }

            // Only check column position if there is an actual column position in the real diagnostic
            if (expected.Column == -1 || actualLinePosition.Character <= 0)
            {
                return;
            }

            if (actualLinePosition.Character + 1 != expected.Column)
            {
                Assert.IsTrue(false,
                              $@"{documentsWithLineNumbers}
Expected diagnostic to start at column ""{expected.Column}"" was actually at column ""{actualLinePosition.Character + 1}""

Diagnostic:
    {FormatDiagnostics(analyzer, diagnostic)}
(Language: {language})
 ");
            }
        }
        /// <summary>
        /// Helper method to VerifyDiagnosticResult that checks the location of a diagnostic and
        /// compares it with the location in the expected DiagnosticResult.
        /// </summary>
        /// <param name="analyzer">The analyzer that was being run on the sources</param>
        /// <param name="diagnostic">The diagnostic that was found in the code</param>
        /// <param name="actual">The Location of the Diagnostic found in the code</param>
        /// <param name="expected">The DiagnosticResultLocation that should have been found</param>
        private static void VerifyDiagnosticLocation(DiagnosticAnalyzer analyzer,
                                                     string documentsWithLineNumbers,
                                                     Diagnostic diagnostic,
                                                     Location actual,
                                                     DiagnosticResultLocation expected,
                                                     string language)
        {
            var actualSpan = actual.GetLineSpan();
            var extension  = language == LanguageNames.CSharp ? CSharpDefaultFileExt : VisualBasicDefaultExt;

            Assert.IsTrue(actualSpan.Path == $"{expected.Path}.{extension}",
                          $@"{documentsWithLineNumbers}
Expected diagnostic to be in file ""{expected.Path}.{extension}"" was actually in file ""{actualSpan.Path}""

Diagnostic:
    {FormatDiagnostics(analyzer, diagnostic)}
(Language: {language})
 ");

            var actualLinePosition = actualSpan.StartLinePosition;

            // Only check line position if there is an actual line in the real diagnostic
            if (actualLinePosition.Line > 0)
            {
                if (actualLinePosition.Line + 1 != expected.Line)
                {
                    Assert.IsTrue(false,
                                  $@"{documentsWithLineNumbers}
Expected diagnostic to be on line ""{expected.Line}"" was actually on line ""{actualLinePosition.Line + 1}""

Diagnostic:
    {FormatDiagnostics(analyzer, diagnostic)}
(Language: {language})
 ");
                }
            }

            // Only check column position if there is an actual column position in the real diagnostic
            if (expected.Column == -1 || actualLinePosition.Character <= 0)
            {
                return;
            }

            if (actualLinePosition.Character + 1 != expected.Column)
            {
                Assert.IsTrue(false,
                              $@"{documentsWithLineNumbers}
Expected diagnostic to start at column ""{expected.Column}"" was actually at column ""{actualLinePosition.Character + 1}""

Diagnostic:
    {FormatDiagnostics(analyzer, diagnostic)}
(Language: {language})
 ");
            }
        }