-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Update to Microsoft.CodeAnalysis.Testing #2562
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Runtime.CompilerServices; | ||
|
||
[assembly: InternalsVisibleTo("Microsoft.ML.CodeAnalyzer.Tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010015c01ae1f50e8cc09ba9eac9147cf8fd9fce2cfe9f8dce4f7301c4132ca9fb50ce8cbf1df4dc18dd4d210e4345c744ecb3365ed327efdbc52603faa5e21daa11234c8c4a73e51f03bf192544581ebe107adee3a34928e39d04e524a9ce729d5090bfd7dad9d10c722c0def9ccc08ff0a03790e48bcd1f9b6c476063e1966a1c4")] | ||
sharwell marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,61 +3,51 @@ | |
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.Testing; | ||
using Microsoft.ML.CodeAnalyzer.Tests.Helpers; | ||
using Xunit; | ||
using VerifyCS = Microsoft.ML.CodeAnalyzer.Tests.Helpers.CSharpCodeFixVerifier< | ||
Microsoft.ML.InternalCodeAnalyzer.BestFriendOnPublicDeclarationsAnalyzer, | ||
Microsoft.CodeAnalysis.Testing.EmptyCodeFixProvider>; | ||
|
||
namespace Microsoft.ML.InternalCodeAnalyzer.Tests | ||
{ | ||
public sealed class BestFriendOnPublicDeclarationTest : DiagnosticVerifier<BestFriendOnPublicDeclarationsAnalyzer> | ||
public sealed class BestFriendOnPublicDeclarationTest | ||
{ | ||
private readonly Lazy<string> SourceAttribute = TestUtils.LazySource("BestFriendAttribute.cs"); | ||
private readonly Lazy<string> SourceDeclaration = TestUtils.LazySource("BestFriendOnPublicDeclaration.cs"); | ||
|
||
[Fact] | ||
public void BestFriendOnPublicDeclaration() | ||
public async Task BestFriendOnPublicDeclaration() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 I kept the test names the same, which means you'll keep your pass/fail history on ADO. If you prefer the |
||
{ | ||
Solution solution = null; | ||
var projA = CreateProject("ProjectA", ref solution, SourceDeclaration.Value, SourceAttribute.Value); | ||
|
||
var analyzer = new BestFriendOnPublicDeclarationsAnalyzer(); | ||
|
||
var refs = new List<MetadataReference> { | ||
RefFromType<object>(), RefFromType<Attribute>(), | ||
MetadataReference.CreateFromFile(Assembly.Load("netstandard, Version=2.0.0.0").Location), | ||
MetadataReference.CreateFromFile(Assembly.Load("System.Runtime, Version=0.0.0.0").Location) | ||
var expected = new DiagnosticResult[] { | ||
VerifyCS.Diagnostic().WithLocation(8, 6).WithArguments("PublicClass"), | ||
VerifyCS.Diagnostic().WithLocation(11, 10).WithArguments("PublicField"), | ||
VerifyCS.Diagnostic().WithLocation(14, 10).WithArguments("PublicProperty"), | ||
VerifyCS.Diagnostic().WithLocation(20, 10).WithArguments("PublicMethod"), | ||
VerifyCS.Diagnostic().WithLocation(26, 10).WithArguments("PublicDelegate"), | ||
VerifyCS.Diagnostic().WithLocation(29, 10).WithArguments("PublicClass"), | ||
VerifyCS.Diagnostic().WithLocation(35, 6).WithArguments("PublicStruct"), | ||
VerifyCS.Diagnostic().WithLocation(40, 6).WithArguments("PublicEnum"), | ||
VerifyCS.Diagnostic().WithLocation(47, 6).WithArguments("PublicInterface"), | ||
VerifyCS.Diagnostic().WithLocation(102, 10).WithArguments("PublicMethod"), | ||
}; | ||
|
||
var comp = projA.GetCompilationAsync().Result.WithReferences(refs.ToArray()); | ||
var compilationWithAnalyzers = comp.WithAnalyzers(ImmutableArray.Create((DiagnosticAnalyzer)analyzer)); | ||
var allDiags = compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync().Result; | ||
|
||
var projectTrees = new HashSet<SyntaxTree>(projA.Documents.Select(r => r.GetSyntaxTreeAsync().Result)); | ||
var diags = allDiags | ||
.Where(d => d.Location == Location.None || d.Location.IsInMetadata || projectTrees.Contains(d.Location.SourceTree)) | ||
.OrderBy(d => d.Location.SourceSpan.Start).ToArray(); | ||
|
||
var diag = analyzer.SupportedDiagnostics[0]; | ||
var expected = new DiagnosticResult[] { | ||
diag.CreateDiagnosticResult(8, 6, "PublicClass"), | ||
diag.CreateDiagnosticResult(11, 10, "PublicField"), | ||
diag.CreateDiagnosticResult(14, 10, "PublicProperty"), | ||
diag.CreateDiagnosticResult(20, 10, "PublicMethod"), | ||
diag.CreateDiagnosticResult(26, 10, "PublicDelegate"), | ||
diag.CreateDiagnosticResult(29, 10, "PublicClass"), | ||
diag.CreateDiagnosticResult(35, 6, "PublicStruct"), | ||
diag.CreateDiagnosticResult(40, 6, "PublicEnum"), | ||
diag.CreateDiagnosticResult(47, 6, "PublicInterface"), | ||
diag.CreateDiagnosticResult(102, 10, "PublicMethod") | ||
var test = new VerifyCS.Test | ||
{ | ||
TestState = | ||
{ | ||
Sources = | ||
{ | ||
SourceDeclaration.Value, | ||
("BestFriendAttribute.cs", SourceAttribute.Value), | ||
}, | ||
}, | ||
}; | ||
|
||
VerifyDiagnosticResults(diags, analyzer, expected); | ||
test.ExpectedDiagnostics.AddRange(expected); | ||
await test.RunAsync(); | ||
} | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know anything regarding analyzers. Is it normal to use beta package? Is where are stable version available?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a dependency we control. We're still stabilizing the API for the testing package (and determining whether breaking changes are allowed in updates after stable) so this is what's currently available. The package performs much more rigorous testing than previous harnesses, which will show up in some of the comments. Several other analyzers internally (dotnet/roslyn-analyzers, Microsoft/vs-threading, Microsoft/VSSDK-Analyzers) and externally (DotNetAnalyzers/StyleCopAnalyzers) are already using this.