/// <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) { string 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) { string 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) { string msg = GetNotInExpectedColumn(analyzer, diagnostic, expected, actualLinePosition); return(VerifyDiagnosticAnalyzerResult.Fail(msg)); } } return(VerifyDiagnosticAnalyzerResult.Ok()); }
/// <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.Count(); var actualCount = actualResults.Count(); if (expectedCount != actualCount) { var diagnosticsOutput = actualResults.Any() ? FormatDiagnostics(analyzer, actualResults.ToArray()) : " NONE."; string 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) { string 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) { string 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) { string msg = GetNoExpectedDiagnosticId(analyzer, actual, expected); return(VerifyDiagnosticAnalyzerResult.Fail(msg)); } if (actual.Severity != expected.Severity) { string msg = GetNotExpectedSeverityMessage(analyzer, actual, expected); return(VerifyDiagnosticAnalyzerResult.Fail(msg)); } if (actual.GetMessage() != expected.Message) { string msg = GetNotExcpectedMessage(analyzer, actual, expected); return(VerifyDiagnosticAnalyzerResult.Fail(msg)); } } return(VerifyDiagnosticAnalyzerResult.Ok()); }