public void DiagnosticForMethod() { var source = @" using System.Threading; class T { void M(CancellationToken t, int i) { } }"; var expected = new DiagnosticResult { Id = CancellationTokenParametersMustComeLastAnalyzer.RuleId, Message = string.Format(MicrosoftApiDesignGuidelinesAnalyzersResources.CancellationTokenParametersMustComeLastMessage, "T.M(System.Threading.CancellationToken, int)"), Severity = DiagnosticSeverity.Warning, Locations = new[] { new DiagnosticResultLocation("Test0.cs", 5, 10) } }; VerifyCSharp(source, expected); }
private static void VerifyDiagnostics(IEnumerable<Diagnostic> actualResults, DiagnosticResult[] expectedResults) { for (int i = 0; i < expectedResults.Length; i++) { var actual = actualResults.ElementAt(i); var expected = expectedResults[i]; if (expected.Line == -1 && expected.Column == -1) { Assert.True(actual.Location == Location.None, string.Format("Expected:\nA project diagnostic with No location\nActual:\n{0}", actual)); } else { VerifyDiagnosticLocation(actual, actual.Location, expected.Locations.First()); var additionalLocations = actual.AdditionalLocations.ToArray(); Assert.True(additionalLocations.Length == expected.Locations.Length - 1, string.Format("Expected {0} additional locations but got {1} for Diagnostic:\r\n {2}\r\n", expected.Locations.Length - 1, additionalLocations.Length, actual)); for (int j = 0; j < additionalLocations.Length; ++j) { VerifyDiagnosticLocation(actual, additionalLocations[j], expected.Locations[j + 1]); } } Assert.True(actual.Id == expected.Id, string.Format("Expected diagnostic id to be \"{0}\" was \"{1}\"\r\n\r\nDiagnostic:\r\n {2}\r\n", expected.Id, actual.Id, actual)); Assert.True(actual.Severity == expected.Severity, string.Format("Expected diagnostic severity to be \"{0}\" was \"{1}\"\r\n\r\nDiagnostic:\r\n {2}\r\n", expected.Severity, actual.Severity, actual)); Assert.True(actual.GetMessage() == expected.Message, string.Format("Expected diagnostic message to be \"{0}\" was \"{1}\"\r\n\r\nDiagnostic:\r\n {2}\r\n", expected.Message, actual.GetMessage(), actual)); } }
public void NoDiagnosticOnExplicitInterfaceImplementation() { var test = @" using System.Threading; interface I { void M(CancellationToken t, int i); } class T : I { void I.M(CancellationToken t, int i) { } }"; // One diagnostic for the interface, but none for the implementation. var expected = new DiagnosticResult { Id = CancellationTokenParametersMustComeLastAnalyzer.RuleId, Message = string.Format(MicrosoftApiDesignGuidelinesAnalyzersResources.CancellationTokenParametersMustComeLastMessage, "I.M(System.Threading.CancellationToken, int)"), Severity = DiagnosticSeverity.Warning, Locations = new[] { new DiagnosticResultLocation("Test0.cs", 5, 10) } }; VerifyCSharp(test, expected); }
public void NoDiagnosticOnOverride() { var test = @" using System.Threading; class B { protected virtual void M(CancellationToken t, int i) { } } class T : B { protected override void M(CancellationToken t, int i) { } }"; // One diagnostic for the virtual, but none for the override. var expected = new DiagnosticResult { Id = CancellationTokenParametersMustComeLastAnalyzer.RuleId, Message = string.Format(MicrosoftApiDesignGuidelinesAnalyzersResources.CancellationTokenParametersMustComeLastMessage, "B.M(System.Threading.CancellationToken, int)"), Severity = DiagnosticSeverity.Warning, Locations = new[] { new DiagnosticResultLocation("Test0.cs", 5, 28) } }; VerifyCSharp(test, expected); }
private void Verify(string source, string language, DiagnosticAnalyzer analyzer, string testProjectName, DiagnosticResult[] expected) { var sources = new[] { source }; var diagnostics = GetSortedDiagnostics(sources.ToFileAndSource(), language, analyzer, true, testProjectName); diagnostics.Verify(analyzer, expected); }