Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use DiagnosticResult from Microsoft.CodeAnalysis.Testing #1801

Merged
merged 1 commit into from
Sep 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
<MicrosoftCodeAnalysisAnalyersVersion>2.6.1</MicrosoftCodeAnalysisAnalyersVersion>
<MicrosoftCodeAnalysisTestResourcesProprietaryVersion>2.0.0-pre-20160714</MicrosoftCodeAnalysisTestResourcesProprietaryVersion>

<!-- Roslyn Testing -->
<MicrosoftCodeAnalysisTestingVersion>1.0.0-beta1-63230-04</MicrosoftCodeAnalysisTestingVersion>

<!-- CoreFX -->
<SystemCollectionsImmutableVersion>1.3.1</SystemCollectionsImmutableVersion>
<SystemReflectionMetadataVersion>1.4.2</SystemReflectionMetadataVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Testing;
using Test.Utilities;
using Xunit;

Expand Down Expand Up @@ -122,7 +123,7 @@ private static void VerifyDiagnosticResults(IEnumerable<Diagnostic> actualResult
Diagnostic actual = actualResults.ElementAt(i);
DiagnosticResult expected = expectedResults[i];

if (expected.Line == -1 && expected.Column == -1)
if (expected.HasLocation)
{
if (actual.Location != Location.None)
{
Expand All @@ -133,20 +134,20 @@ private static void VerifyDiagnosticResults(IEnumerable<Diagnostic> actualResult
}
else
{
VerifyDiagnosticLocation(analyzer, actual, actual.Location, expected.Locations.First());
VerifyDiagnosticLocation(analyzer, actual, actual.Location, expected.Spans[0]);
Location[] additionalLocations = actual.AdditionalLocations.ToArray();

if (additionalLocations.Length != expected.Locations.Length - 1)
if (additionalLocations.Length != expected.Spans.Length - 1)
{
Assert.True(false,
string.Format("Expected {0} additional locations but got {1} for Diagnostic:\r\n {2}\r\n",
expected.Locations.Length - 1, additionalLocations.Length,
expected.Spans.Length - 1, additionalLocations.Length,
FormatDiagnostics(analyzer, actual)));
}

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

Expand Down Expand Up @@ -180,7 +181,7 @@ private static void VerifyDiagnosticResults(IEnumerable<Diagnostic> actualResult
/// <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, Diagnostic diagnostic, Location actual, DiagnosticResultLocation expected)
private static void VerifyDiagnosticLocation(DiagnosticAnalyzer analyzer, Diagnostic diagnostic, Location actual, FileLinePositionSpan expected)
{
FileLinePositionSpan actualSpan = actual.GetLineSpan();

Expand All @@ -191,24 +192,24 @@ private static void VerifyDiagnosticLocation(DiagnosticAnalyzer analyzer, Diagno
Microsoft.CodeAnalysis.Text.LinePosition actualLinePosition = actualSpan.StartLinePosition;

// Only check line position if there is an actual line in the real diagnostic
if (actualLinePosition.Line > 0)
if (expected.StartLinePosition.Line > 0)
{
if (actualLinePosition.Line + 1 != expected.Line)
if (actualLinePosition.Line + 1 != expected.StartLinePosition.Line)
{
Assert.True(false,
string.Format("Expected diagnostic to be on line \"{0}\" was actually on line \"{1}\"\r\n\r\nDiagnostic:\r\n {2}\r\n",
expected.Line, actualLinePosition.Line + 1, FormatDiagnostics(analyzer, diagnostic)));
expected.StartLinePosition.Line, actualLinePosition.Line + 1, FormatDiagnostics(analyzer, diagnostic)));
}
}

// 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)
if (actualLinePosition.Character + 1 != expected.StartLinePosition.Character)
{
Assert.True(false,
string.Format("Expected diagnostic to start at column \"{0}\" was actually at column \"{1}\"\r\n\r\nDiagnostic:\r\n {2}\r\n",
expected.Column, actualLinePosition.Character + 1, FormatDiagnostics(analyzer, diagnostic)));
expected.StartLinePosition.Character, actualLinePosition.Character + 1, FormatDiagnostics(analyzer, diagnostic)));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.CodeAnalysis.Analyzers.MetaAnalyzers.Fixers;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Testing;
using Test.Utilities;
using Xunit;

Expand Down Expand Up @@ -735,52 +736,44 @@ protected override DiagnosticAnalyzer GetBasicDiagnosticAnalyzer()
private static DiagnosticResult GetCSharpOverrideCodeActionEquivalenceKeyExpectedDiagnostic(int line, int column, string customCodeActionName)
{
string message = string.Format(CodeAnalysisDiagnosticsResources.OverrideCodeActionEquivalenceKeyMessage, customCodeActionName, "EquivalenceKey");
return GetExpectedDiagnostic(LanguageNames.CSharp, line, column, FixerWithFixAllAnalyzer.OverrideCodeActionEquivalenceKeyRule.Id, message);
return GetExpectedDiagnostic(line, column, FixerWithFixAllAnalyzer.OverrideCodeActionEquivalenceKeyRule.Id, message);
}

private static DiagnosticResult GetBasicOverrideCodeActionEquivalenceKeyExpectedDiagnostic(int line, int column, string customCodeActionName)
{
string message = string.Format(CodeAnalysisDiagnosticsResources.OverrideCodeActionEquivalenceKeyMessage, customCodeActionName, "EquivalenceKey");
return GetExpectedDiagnostic(LanguageNames.VisualBasic, line, column, FixerWithFixAllAnalyzer.OverrideCodeActionEquivalenceKeyRule.Id, message);
return GetExpectedDiagnostic(line, column, FixerWithFixAllAnalyzer.OverrideCodeActionEquivalenceKeyRule.Id, message);
}

private static DiagnosticResult GetCSharpCreateCodeActionWithEquivalenceKeyExpectedDiagnostic(int line, int column)
{
string message = string.Format(CodeAnalysisDiagnosticsResources.CreateCodeActionWithEquivalenceKeyMessage, "equivalenceKey");
return GetExpectedDiagnostic(LanguageNames.CSharp, line, column, FixerWithFixAllAnalyzer.CreateCodeActionEquivalenceKeyRule.Id, message);
return GetExpectedDiagnostic(line, column, FixerWithFixAllAnalyzer.CreateCodeActionEquivalenceKeyRule.Id, message);
}

private static DiagnosticResult GetBasicCreateCodeActionWithEquivalenceKeyExpectedDiagnostic(int line, int column)
{
string message = string.Format(CodeAnalysisDiagnosticsResources.CreateCodeActionWithEquivalenceKeyMessage, "equivalenceKey");
return GetExpectedDiagnostic(LanguageNames.VisualBasic, line, column, FixerWithFixAllAnalyzer.CreateCodeActionEquivalenceKeyRule.Id, message);
return GetExpectedDiagnostic(line, column, FixerWithFixAllAnalyzer.CreateCodeActionEquivalenceKeyRule.Id, message);
}

private static DiagnosticResult GetCSharpOverrideGetFixAllProviderExpectedDiagnostic(int line, int column, string codeFixProviderTypeName)
{
string message = string.Format(CodeAnalysisDiagnosticsResources.OverrideGetFixAllProviderMessage, codeFixProviderTypeName);
return GetExpectedDiagnostic(LanguageNames.CSharp, line, column, FixerWithFixAllAnalyzer.OverrideGetFixAllProviderRule.Id, message);
return GetExpectedDiagnostic(line, column, FixerWithFixAllAnalyzer.OverrideGetFixAllProviderRule.Id, message);
}

private static DiagnosticResult GetBasicOverrideGetFixAllProviderExpectedDiagnostic(int line, int column, string codeFixProviderTypeName)
{
string message = string.Format(CodeAnalysisDiagnosticsResources.OverrideGetFixAllProviderMessage, codeFixProviderTypeName);
return GetExpectedDiagnostic(LanguageNames.VisualBasic, line, column, FixerWithFixAllAnalyzer.OverrideGetFixAllProviderRule.Id, message);
return GetExpectedDiagnostic(line, column, FixerWithFixAllAnalyzer.OverrideGetFixAllProviderRule.Id, message);
}

private static DiagnosticResult GetExpectedDiagnostic(string language, int line, int column, string id, string message)
private static DiagnosticResult GetExpectedDiagnostic(int line, int column, string id, string message)
{
string fileName = language == LanguageNames.CSharp ? "Test0.cs" : "Test0.vb";
return new DiagnosticResult
{
Id = id,
Message = message,
Severity = DiagnosticHelpers.DefaultDiagnosticSeverity,
Locations = new[]
{
new DiagnosticResultLocation(fileName, line, column)
}
};
return new DiagnosticResult(id, DiagnosticHelpers.DefaultDiagnosticSeverity)
.WithLocation(line, column)
.WithMessageFormat(message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

using System.Diagnostics;
using System.Linq;
using Analyzer.Utilities;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Testing;
using Test.Utilities;
using Xunit;

Expand Down Expand Up @@ -233,7 +235,7 @@ private void VerifyAcrossTwoAssemblies(string source1, string source2, string la
.AddProjectReference(new ProjectReference(project1.Id));

DiagnosticAnalyzer analyzer = language == LanguageNames.CSharp ? GetCSharpDiagnosticAnalyzer() : GetBasicDiagnosticAnalyzer();
GetSortedDiagnostics(analyzer, project2.Documents.ToArray()).Verify(analyzer, expected);
GetSortedDiagnostics(analyzer, project2.Documents.ToArray()).Verify(analyzer, GetDefaultPath(language), expected);
}

private void VerifyCSharpAcrossTwoAssemblies(string source1, string source2, params DiagnosticResult[] expected)
Expand All @@ -258,27 +260,20 @@ protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer()

private static DiagnosticResult GetCSharpExpectedDiagnostic(int line, int column, string typeName, string interfaceName)
{
return GetExpectedDiagnostic(LanguageNames.CSharp, line, column, typeName, interfaceName);
return GetExpectedDiagnostic(line, column, typeName, interfaceName);
}

private static DiagnosticResult GetBasicExpectedDiagnostic(int line, int column, string typeName, string interfaceName)
{
return GetExpectedDiagnostic(LanguageNames.VisualBasic, line, column, typeName, interfaceName);
return GetExpectedDiagnostic(line, column, typeName, interfaceName);
}

private static DiagnosticResult GetExpectedDiagnostic(string language, int line, int column, string typeName, string interfaceName)
private static DiagnosticResult GetExpectedDiagnostic(int line, int column, string typeName, string interfaceName)
{
string fileName = language == LanguageNames.CSharp ? "Test0.cs" : "Test0.vb";
return new DiagnosticResult
{
Id = DiagnosticIds.InternalImplementationOnlyRuleId,
Message = string.Format(CodeAnalysisDiagnosticsResources.InternalImplementationOnlyMessage, typeName, interfaceName),
Severity = DiagnosticSeverity.Error,
Locations = new[]
{
new DiagnosticResultLocation(fileName, line, column)
}
};
return new DiagnosticResult(DiagnosticIds.InternalImplementationOnlyRuleId, DiagnosticSeverity.Error)
.WithLocation(line, column)
.WithMessageFormat(CodeAnalysisDiagnosticsResources.InternalImplementationOnlyMessage)
.WithArguments(typeName, interfaceName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.CodeAnalysis.Analyzers.MetaAnalyzers;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Testing;
using Test.Utilities;
using Xunit;

Expand Down Expand Up @@ -191,27 +192,20 @@ protected override DiagnosticAnalyzer GetBasicDiagnosticAnalyzer()

private static DiagnosticResult GetCSharpExpectedDiagnostic(int line, int column, string analyzerTypeName, string missingLanguageName)
{
return GetExpectedDiagnostic(LanguageNames.CSharp, line, column, analyzerTypeName, missingLanguageName);
return GetExpectedDiagnostic(line, column, analyzerTypeName, missingLanguageName);
}

private static DiagnosticResult GetBasicExpectedDiagnostic(int line, int column, string analyzerTypeName, string missingLanguageName)
{
return GetExpectedDiagnostic(LanguageNames.VisualBasic, line, column, analyzerTypeName, missingLanguageName);
return GetExpectedDiagnostic(line, column, analyzerTypeName, missingLanguageName);
}

private static DiagnosticResult GetExpectedDiagnostic(string language, int line, int column, string analyzerTypeName, string missingLanguageName)
private static DiagnosticResult GetExpectedDiagnostic(int line, int column, string analyzerTypeName, string missingLanguageName)
{
string fileName = language == LanguageNames.CSharp ? "Test0.cs" : "Test0.vb";
return new DiagnosticResult
{
Id = DiagnosticIds.AddLanguageSupportToAnalyzerRuleId,
Message = string.Format(CodeAnalysisDiagnosticsResources.AddLanguageSupportToAnalyzerMessage, analyzerTypeName, missingLanguageName),
Severity = DiagnosticHelpers.DefaultDiagnosticSeverity,
Locations = new[]
{
new DiagnosticResultLocation(fileName, line, column)
}
};
return new DiagnosticResult(DiagnosticIds.AddLanguageSupportToAnalyzerRuleId, DiagnosticHelpers.DefaultDiagnosticSeverity)
.WithLocation(line, column)
.WithMessageFormat(CodeAnalysisDiagnosticsResources.AddLanguageSupportToAnalyzerMessage)
.WithArguments(analyzerTypeName, missingLanguageName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Analyzers.MetaAnalyzers;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.CodeAnalysis.VisualBasic.Analyzers.MetaAnalyzers;
using Test.Utilities;
using Xunit;
Expand Down Expand Up @@ -941,39 +942,40 @@ protected override DiagnosticAnalyzer GetBasicDiagnosticAnalyzer()

private static DiagnosticResult GetCSharpExpectedDiagnostic(int line, int column, string analyzerTypeName, string violatingTypes)
{
return GetExpectedDiagnostic(LanguageNames.CSharp, line, column, analyzerTypeName, violatingIndirectTypesOpt: null, violatingTypes: violatingTypes);
return GetExpectedDiagnostic(line, column, analyzerTypeName, violatingIndirectTypesOpt: null, violatingTypes: violatingTypes);
}

private static DiagnosticResult GetCSharpExpectedDiagnostic(int line, int column, string analyzerTypeName, string violatingIndirectTypes, string violatingTypes)
{
return GetExpectedDiagnostic(LanguageNames.CSharp, line, column, analyzerTypeName, violatingIndirectTypes, violatingTypes);
return GetExpectedDiagnostic(line, column, analyzerTypeName, violatingIndirectTypes, violatingTypes);
}

private static DiagnosticResult GetBasicExpectedDiagnostic(int line, int column, string analyzerTypeName, string violatingTypes)
{
return GetExpectedDiagnostic(LanguageNames.VisualBasic, line, column, analyzerTypeName, violatingIndirectTypesOpt: null, violatingTypes: violatingTypes);
return GetExpectedDiagnostic(line, column, analyzerTypeName, violatingIndirectTypesOpt: null, violatingTypes: violatingTypes);
}

private static DiagnosticResult GetBasicExpectedDiagnostic(int line, int column, string analyzerTypeName, string violatingIndirectTypes, string violatingTypes)
{
return GetExpectedDiagnostic(LanguageNames.VisualBasic, line, column, analyzerTypeName, violatingIndirectTypes, violatingTypes);
return GetExpectedDiagnostic(line, column, analyzerTypeName, violatingIndirectTypes, violatingTypes);
}

private static DiagnosticResult GetExpectedDiagnostic(string language, int line, int column, string analyzerTypeName, string violatingIndirectTypesOpt, string violatingTypes)
private static DiagnosticResult GetExpectedDiagnostic(int line, int column, string analyzerTypeName, string violatingIndirectTypesOpt, string violatingTypes)
{
string fileName = language == LanguageNames.CSharp ? "Test0.cs" : "Test0.vb";
return new DiagnosticResult
var result = new DiagnosticResult(DiagnosticIds.DoNotUseTypesFromAssemblyRuleId, DiagnosticHelpers.DefaultDiagnosticSeverity)
.WithLocation(line, column);
if (violatingIndirectTypesOpt is null)
{
Id = DiagnosticIds.DoNotUseTypesFromAssemblyRuleId,
Message = violatingIndirectTypesOpt == null ?
string.Format(CodeAnalysisDiagnosticsResources.DoNotUseTypesFromAssemblyRuleDirectMessage, analyzerTypeName, violatingTypes) :
string.Format(CodeAnalysisDiagnosticsResources.DoNotUseTypesFromAssemblyRuleIndirectMessage, analyzerTypeName, violatingIndirectTypesOpt, violatingTypes),
Severity = DiagnosticHelpers.DefaultDiagnosticSeverity,
Locations = new[]
{
new DiagnosticResultLocation(fileName, line, column)
}
};
return result
.WithMessageFormat(CodeAnalysisDiagnosticsResources.DoNotUseTypesFromAssemblyRuleDirectMessage)
.WithArguments(analyzerTypeName, violatingTypes);
}
else
{
return result
.WithMessageFormat(CodeAnalysisDiagnosticsResources.DoNotUseTypesFromAssemblyRuleIndirectMessage)
.WithArguments(analyzerTypeName, violatingIndirectTypesOpt, violatingTypes);
}
}
}
}
Loading