Beispiel #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 VerifyDiagnosticAnalyzerResult VerifyDiagnosticLocation(DiagnosticAnalyzer analyzer,
                                                                               Diagnostic diagnostic,
                                                                               Location actual,
                                                                               DiagnosticResultLocation expected)
        {
            var actualSpan = actual.GetLineSpan();

            var isInExpectedFile = actualSpan.Path == expected.Path ||
                                   actualSpan.Path != null &&
                                   actualSpan.Path.Contains("Test0.") &&
                                   expected.Path.Contains("Test.");

            if (!isInExpectedFile)
            {
                var msg = GetNotInExpectedFileMessage(analyzer, diagnostic, expected, actualSpan);

                return(VerifyDiagnosticAnalyzerResult.Fail(msg));
            }

            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)
                {
                    var msg = GetNotInExpectedLineMessage(analyzer, diagnostic, expected, actualLinePosition);

                    return(VerifyDiagnosticAnalyzerResult.Fail(msg));
                }
            }

            // Only check column position if there is an actual column position in the real diagnostic
            if (actualLinePosition.Character > 0)
            {
                if (actualLinePosition.Character + 1 != expected.Column)
                {
                    var msg = GetNotInExpectedColumn(analyzer, diagnostic, expected, actualLinePosition);

                    return(VerifyDiagnosticAnalyzerResult.Fail(msg));
                }
            }

            return(VerifyDiagnosticAnalyzerResult.Ok());
        }
Beispiel #2
0
        /// <summary>
        /// Checks each of the actual Diagnostics found and compares them with the
        /// corresponding DiagnosticResult in the array of expected results.
        /// Diagnostics are considered equal only if the DiagnosticResultLocation, Id,
        /// Severity, and Message of the DiagnosticResult match the actual diagnostic.
        /// </summary>
        /// <param name="actualResults">
        /// The Diagnostics found by the compiler after running the analyzer on the source
        /// code
        /// </param>
        /// <param name="analyzer"> The analyzer that was being run on the sources </param>
        /// <param name="expectedResults">
        /// Diagnostic Results that should have appeared in
        /// the code
        /// </param>
        public static VerifyDiagnosticAnalyzerResult VerifyDiagnosticResults(this DiagnosticAnalyzer analyzer,
                                                                             IEnumerable <Diagnostic> actualResults,
                                                                             DiagnosticResult[] expectedResults)
        {
            var expectedCount = expectedResults.Length;
            var actualCount   = actualResults.Count();

            if (expectedCount != actualCount)
            {
                var diagnosticsOutput = actualResults.Any()
                                        ? FormatDiagnostics(analyzer, actualResults.ToArray())
                                        : "    NONE.";

                var msg = GetMismatchNumberOfDiagnosticsMessage(expectedCount, actualCount, diagnosticsOutput);

                return(VerifyDiagnosticAnalyzerResult.Fail(msg));
            }

            for (var i = 0; i < expectedResults.Length; i++)
            {
                var actual   = actualResults.ElementAt(i);
                var expected = expectedResults[i];

                if (expected.Line == -1 && expected.Column == -1)
                {
                    if (actual.Location != Location.None)
                    {
                        var msg = GetExpectedDiagnosticWithNoLocation(analyzer, actual);

                        return(VerifyDiagnosticAnalyzerResult.Fail(msg));
                    }
                }
                else
                {
                    var locationResult =
                        VerifyDiagnosticLocation(analyzer, actual, actual.Location, expected.Locations.First());

                    if (!locationResult.Success)
                    {
                        return(locationResult);
                    }

                    var additionalLocations = actual.AdditionalLocations.ToArray();

                    if (additionalLocations.Length != expected.Locations.Length - 1)
                    {
                        var msg = GetNotExpectedLocation(analyzer, actual, expected, additionalLocations);

                        return(VerifyDiagnosticAnalyzerResult.Fail(msg));
                    }

                    for (var j = 0; j < additionalLocations.Length; ++j)
                    {
                        locationResult = VerifyDiagnosticLocation(analyzer,
                                                                  actual,
                                                                  additionalLocations[j],
                                                                  expected.Locations[j + 1]);

                        if (!locationResult.Success)
                        {
                            return(locationResult);
                        }
                    }
                }

                if (actual.Id != expected.Id)
                {
                    var msg = GetNoExpectedDiagnosticId(analyzer, actual, expected);

                    return(VerifyDiagnosticAnalyzerResult.Fail(msg));
                }

                if (actual.Severity != expected.Severity)
                {
                    var msg = GetNotExpectedSeverityMessage(analyzer, actual, expected);

                    return(VerifyDiagnosticAnalyzerResult.Fail(msg));
                }

                if (actual.GetMessage() != expected.Message)
                {
                    var msg = GetNotExcpectedMessage(analyzer, actual, expected);

                    return(VerifyDiagnosticAnalyzerResult.Fail(msg));
                }
            }

            return(VerifyDiagnosticAnalyzerResult.Ok());
        }