/// <summary>
        /// Helper method to <see cref="VerifyDiagnosticResults"/> that checks the location of
        /// a <see cref="Diagnostic"/> and compares it with the <see cref="Location"/> in the
        /// expected <see cref="DiagnosticResult"/>.
        /// </summary>
        /// <param name="language"></param>
        /// <param name="analyzer">The <see cref="DiagnosticAnalyzer"/> that was being run on the sources.</param>
        /// <param name="actualDiagnostic">The <see cref="Diagnostic"/> that was found in the code.</param>
        /// <param name="actualLocation">The <see cref="Location"/> of the <paramref name="actualDiagnostic"/> found in the code.</param>
        /// <param name="expectedResult">The <see cref="DiagnosticResultLocation"/> that should have been found.</param>
        /// <exception cref="BadDiagnosticLocationException"></exception>
        protected virtual void VerifyDiagnosticLocation(string language, DiagnosticAnalyzer analyzer
                                                        , DiagnosticResultLocation expectedResult, Diagnostic actualDiagnostic, Location actualLocation)
        {
            var actualSpan = actualLocation.GetLineSpan();

            if (actualSpan.Path != expectedResult.Path &&
                (IsNullOrEmpty(actualSpan.Path) ||
                 !actualSpan.Path.Contains($"{DefaultFilePathPrefix}0.") ||
                 !expectedResult.Path.Contains($"{DefaultFilePathPrefix}.")))
            {
                throw new BadDiagnosticLocationException(
                          $"Expected diagnostic to be in file \"${expectedResult.Path}\""
                          + $" but was in file \"{actualSpan.Path}\""
                          , language, analyzer, PathMismatch, expectedResult, actualDiagnostic, actualLocation);
            }

            var actualLinePosition = actualSpan.StartLinePosition;

            if (actualLinePosition.Line > 0 && actualLinePosition.Line + 1 != expectedResult.Line)
            {
                throw new BadDiagnosticLocationException(
                          $"Expected diagnostic to be on line \"{expectedResult.Line}\""
                          + $" but was on line \"{actualLinePosition.Line + 1}\""
                          , language, analyzer, LineMismatch, expectedResult, actualDiagnostic, actualLocation);
            }

            if (actualLinePosition.Character > 0 && actualLinePosition.Character + 1 != expectedResult.Column)
            {
                throw new BadDiagnosticLocationException(
                          $"Expected diagnostic to start at column \"{expectedResult.Column}\""
                          + $" but was at column \"{actualLinePosition.Character + 1}\""
                          , language, analyzer, ColumnMismatch, expectedResult, actualDiagnostic, actualLocation);
            }
        }
示例#2
0
 internal BadDiagnosticLocationException(string message, string language, DiagnosticAnalyzer analyzer
                                         , VerificationResult result, DiagnosticResultLocation expectedResultLocation
                                         , Diagnostic actualDiagnostic, Location actualLocation)
     : base(message, result)
 {
     Language = language;
     Analyzer = analyzer;
     ExpectedResultLocation = expectedResultLocation;
     ActualDiagnostic       = actualDiagnostic;
     ActualLocation         = actualLocation;
 }