-
Notifications
You must be signed in to change notification settings - Fork 468
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for detecting preview-ness in dependencies (#5395)
* Support for detecting preview-ness in dependencies Unit tests * Address comments * Change ContainingType -> ContainingSymbol
- Loading branch information
Prashanth Govindarajan
authored
Aug 23, 2021
1 parent
20d227a
commit 3f186ae
Showing
3 changed files
with
224 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
186 changes: 186 additions & 0 deletions
186
...s/UnitTests/Microsoft.NetCore.Analyzers/Runtime/DetectPreviewFeatureTests.Dependencies.cs
This file contains 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,186 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System.Threading.Tasks; | ||
using Test.Utilities; | ||
using Xunit; | ||
using VerifyCS = Test.Utilities.CSharpCodeFixVerifier< | ||
Microsoft.NetCore.Analyzers.Runtime.DetectPreviewFeatureAnalyzer, | ||
Microsoft.CodeAnalysis.Testing.EmptyCodeFixProvider>; | ||
|
||
namespace Microsoft.NetCore.Analyzers.Runtime.UnitTests | ||
{ | ||
public partial class DetectPreviewFeatureUnitTests | ||
{ | ||
[Theory] | ||
[InlineData("assembly")] | ||
[InlineData("module")] | ||
public async Task TestAssemblyDoesntUsePreviewDependency(string assemblyOrModule) | ||
{ | ||
// No diagnostic when we don't use any APIs from an assembly marked with Preview | ||
string csCurrentAssemblyCode = @" | ||
using System; | ||
public class Program | ||
{ | ||
public void ProgramMethod() | ||
{ | ||
new Program(); | ||
} | ||
}"; | ||
string csDepedencyCode = @$"[{assemblyOrModule}: System.Runtime.Versioning.RequiresPreviewFeatures]"; | ||
|
||
var test = SetupDependencyAndTestCSWithOneSourceFile(csCurrentAssemblyCode, csDepedencyCode); | ||
await test.RunAsync(); | ||
} | ||
|
||
[Theory] | ||
[InlineData("assembly")] | ||
[InlineData("module")] | ||
public async Task TestCallAPIsFromAssemblyMarkedAsPreview(string assemblyOrModule) | ||
{ | ||
string csDependencyCode = @" | ||
public class Library | ||
{ | ||
public void AMethod() { } | ||
private int _property; | ||
public int AProperty | ||
{ | ||
get => 1; | ||
set | ||
{ | ||
_property = value; | ||
} | ||
} | ||
}"; | ||
csDependencyCode = @$"[{assemblyOrModule}: System.Runtime.Versioning.RequiresPreviewFeatures] {csDependencyCode}"; | ||
|
||
string csCurrentAssemblyCode = @" | ||
using System; | ||
public class Program | ||
{ | ||
public void ProgramMethod() | ||
{ | ||
Library library = {|#1:new Library()|}; | ||
{|#0:library.AMethod()|}; | ||
int prop = {|#2:library.AProperty|}; | ||
} | ||
}"; | ||
var test = SetupDependencyAndTestCSWithOneSourceFile(csCurrentAssemblyCode, csDependencyCode); | ||
test.ExpectedDiagnostics.Add(VerifyCS.Diagnostic(DetectPreviewFeatureAnalyzer.GeneralPreviewFeatureAttributeRule).WithLocation(0).WithArguments("AMethod")); | ||
test.ExpectedDiagnostics.Add(VerifyCS.Diagnostic(DetectPreviewFeatureAnalyzer.GeneralPreviewFeatureAttributeRule).WithLocation(1).WithArguments("Library")); | ||
test.ExpectedDiagnostics.Add(VerifyCS.Diagnostic(DetectPreviewFeatureAnalyzer.GeneralPreviewFeatureAttributeRule).WithLocation(2).WithArguments("AProperty")); | ||
await test.RunAsync(); | ||
} | ||
|
||
[Theory] | ||
[InlineData("assembly")] | ||
[InlineData("module")] | ||
public async Task TestNoCallsToPreviewDependency(string assemblyOrModule) | ||
{ | ||
string csDependencyCode = @" | ||
public class Library | ||
{ | ||
public void AMethod() { } | ||
private int _property; | ||
public int AProperty | ||
{ | ||
get => 1; | ||
set | ||
{ | ||
_property = value; | ||
} | ||
} | ||
}"; | ||
csDependencyCode = @$"[{assemblyOrModule}: System.Runtime.Versioning.RequiresPreviewFeatures] {csDependencyCode}"; | ||
|
||
string csCurrentAssemblyCode = @" | ||
using System; | ||
public class Program | ||
{ | ||
public void ProgramMethod() | ||
{ | ||
} | ||
}"; | ||
var test = SetupDependencyAndTestCSWithOneSourceFile(csCurrentAssemblyCode, csDependencyCode); | ||
await test.RunAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task TestMixtureOfPreviewAPIsInDependency() | ||
{ | ||
string csDependencyCode = @" | ||
public class Library | ||
{ | ||
public void AMethod() | ||
{ | ||
#pragma warning disable CA2252 | ||
APreviewMethod(); | ||
#pragma warning enable CA2252 | ||
} | ||
[System.Runtime.Versioning.RequiresPreviewFeatures] | ||
public void APreviewMethod() { } | ||
}"; | ||
|
||
string csCurrentAssemblyCode = @" | ||
using System; | ||
public class Program | ||
{ | ||
public void ProgramMethod() | ||
{ | ||
Library library = new Library(); | ||
library.AMethod(); | ||
{|#0:library.APreviewMethod()|}; | ||
} | ||
}"; | ||
var test = SetupDependencyAndTestCSWithOneSourceFile(csCurrentAssemblyCode, csDependencyCode); | ||
test.ExpectedDiagnostics.Add(VerifyCS.Diagnostic(DetectPreviewFeatureAnalyzer.GeneralPreviewFeatureAttributeRule).WithLocation(0).WithArguments("APreviewMethod")); | ||
await test.RunAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task TestDeepNestingOfPreviewAPIsInDependency() | ||
{ | ||
string csDependencyCode = @" | ||
public class Library | ||
{ | ||
[System.Runtime.Versioning.RequiresPreviewFeatures] | ||
public class NestedClass0 | ||
{ | ||
public class NestedClass1 | ||
{ | ||
public class NestedClass2 | ||
{ | ||
public class NestedClass3 | ||
{ | ||
public void APreviewMethod() { } | ||
} | ||
} | ||
} | ||
} | ||
}"; | ||
|
||
string csCurrentAssemblyCode = @" | ||
using System; | ||
public class Program | ||
{ | ||
public void ProgramMethod() | ||
{ | ||
Library.NestedClass0.NestedClass1.NestedClass2.NestedClass3 nestedClass = {|#0:new()|}; | ||
{|#1:nestedClass.APreviewMethod()|}; | ||
} | ||
}"; | ||
var test = SetupDependencyAndTestCSWithOneSourceFile(csCurrentAssemblyCode, csDependencyCode); | ||
test.ExpectedDiagnostics.Add(VerifyCS.Diagnostic(DetectPreviewFeatureAnalyzer.GeneralPreviewFeatureAttributeRule).WithLocation(0).WithArguments("NestedClass3")); | ||
test.ExpectedDiagnostics.Add(VerifyCS.Diagnostic(DetectPreviewFeatureAnalyzer.GeneralPreviewFeatureAttributeRule).WithLocation(1).WithArguments("APreviewMethod")); | ||
await test.RunAsync(); | ||
} | ||
} | ||
} |
This file contains 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