-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce new InternalsVisibleToAnalyzer analyzer (ACZ0112) with `Fri…
…endAttribute` concept (#7086)
- Loading branch information
1 parent
adc439e
commit 7787c1b
Showing
12 changed files
with
826 additions
and
289 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
234 changes: 234 additions & 0 deletions
234
src/dotnet/Azure.ClientSdk.Analyzers/Azure.ClientSdk.Analyzers.Tests/AZC0112Tests.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,234 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
using Verifier = Azure.ClientSdk.Analyzers.Tests.AzureAnalyzerVerifier<Azure.ClientSdk.Analyzers.InternalsVisibleToAnalyzer>; | ||
|
||
namespace Azure.ClientSdk.Analyzers.Tests | ||
{ | ||
public class AZC0112Tests | ||
{ | ||
[Fact] | ||
public async Task AZC0020WhenInheritingFromInternalInterface() | ||
{ | ||
string code = @" | ||
using System; | ||
using TestReferenceWithInternalsVisibleTo; | ||
namespace LibraryNamespace | ||
{ | ||
public class {|AZC0112:MyClass|} : IInternalInterface | ||
{ | ||
} | ||
}"; | ||
await Verifier.VerifyAnalyzerAsync(code, additionalReferences: new[] { typeof(TestReferenceWithInternalsVisibleTo.PublicClass) }); | ||
} | ||
|
||
[Fact] | ||
public async Task NoAZC0020WhenInheritingFromInternalInterfaceWithFriendAttribute() | ||
{ | ||
string code = @" | ||
using System; | ||
using TestReferenceWithInternalsVisibleTo; | ||
namespace LibraryNamespace | ||
{ | ||
public class MyClass : IInternalInterfaceWithFriendAttribute | ||
{ | ||
} | ||
}"; | ||
await Verifier.VerifyAnalyzerAsync(code, additionalReferences: new[] { typeof(TestReferenceWithInternalsVisibleTo.PublicClass) }); | ||
} | ||
|
||
[Fact] | ||
public async Task AZC0020WhenDerivingFromInternalClass() | ||
{ | ||
string code = @" | ||
using System; | ||
using TestReferenceWithInternalsVisibleTo; | ||
namespace LibraryNamespace | ||
{ | ||
internal class {|AZC0112:MyClass|} : InternalClass | ||
{ | ||
} | ||
}"; | ||
await Verifier.VerifyAnalyzerAsync(code, additionalReferences: new[] { typeof(TestReferenceWithInternalsVisibleTo.PublicClass) }); | ||
} | ||
|
||
[Fact] | ||
public async Task NoAZC0020WhenInheritingFromInternalClassWithFriendAttribute() | ||
{ | ||
string code = @" | ||
using System; | ||
using TestReferenceWithInternalsVisibleTo; | ||
namespace LibraryNamespace | ||
{ | ||
internal class MyClass : InternalClassWithFriendAttribute | ||
{ | ||
} | ||
}"; | ||
await Verifier.VerifyAnalyzerAsync(code, additionalReferences: new[] { typeof(TestReferenceWithInternalsVisibleTo.PublicClass) }); | ||
} | ||
|
||
[Fact] | ||
public async Task AZC0020WhenDeclaringInternalProperty() | ||
{ | ||
string code = @" | ||
using System; | ||
using TestReferenceWithInternalsVisibleTo; | ||
namespace LibraryNamespace | ||
{ | ||
public class MyClass | ||
{ | ||
internal InternalClass {|AZC0112:PropReferencesInternalType|} { {|AZC0112:get|}; set;} | ||
} | ||
}"; | ||
await Verifier.VerifyAnalyzerAsync(code, additionalReferences: new[] { typeof(TestReferenceWithInternalsVisibleTo.PublicClass) }); | ||
} | ||
|
||
[Fact] | ||
public async Task NoAZC0020WhenDeclaringInternalPropertyWithFriendAttribute() | ||
{ | ||
string code = @" | ||
using System; | ||
using TestReferenceWithInternalsVisibleTo; | ||
namespace LibraryNamespace | ||
{ | ||
public class MyClass | ||
{ | ||
internal InternalClassWithFriendAttribute PropReferencesInternalType { get; set;} | ||
} | ||
}"; | ||
await Verifier.VerifyAnalyzerAsync(code, additionalReferences: new[] { typeof(TestReferenceWithInternalsVisibleTo.PublicClass) }); | ||
} | ||
|
||
[Fact] | ||
public async Task NoAZC0020WhenDeclaringInternalFieldWithFriendAttribute() | ||
{ | ||
string code = @" | ||
using System; | ||
using TestReferenceWithInternalsVisibleTo; | ||
namespace LibraryNamespace | ||
{ | ||
public class MyClass | ||
{ | ||
internal InternalClassWithFriendAttribute fieldReferencesInternalType; | ||
} | ||
}"; | ||
await Verifier.VerifyAnalyzerAsync(code, additionalReferences: new[] { typeof(TestReferenceWithInternalsVisibleTo.PublicClass) }); | ||
} | ||
|
||
[Fact] | ||
public async Task AZC0020WhenDeclaringInternalField() | ||
{ | ||
string code = @" | ||
using System; | ||
using TestReferenceWithInternalsVisibleTo; | ||
namespace LibraryNamespace | ||
{ | ||
public class MyClass | ||
{ | ||
internal InternalClass {|AZC0112:fieldReferencesInternalType|}; | ||
} | ||
}"; | ||
await Verifier.VerifyAnalyzerAsync(code, additionalReferences: new[] { typeof(TestReferenceWithInternalsVisibleTo.PublicClass) }); | ||
} | ||
|
||
[Fact] | ||
public async Task AZC0020WhenReferencingInternalProperty() | ||
{ | ||
string code = @" | ||
using System; | ||
using TestReferenceWithInternalsVisibleTo; | ||
using System.Reflection; | ||
namespace LibraryNamespace | ||
{ | ||
public class MyClass | ||
{ | ||
public void MyMethod() | ||
{ | ||
var myClass = new PublicClass(); | ||
var value = {|AZC0112:myClass.InternalProperty|}; | ||
} | ||
} | ||
}"; | ||
await Verifier.VerifyAnalyzerAsync(code, additionalReferences: new[] { typeof(TestReferenceWithInternalsVisibleTo.PublicClass) }); | ||
} | ||
|
||
[Fact] | ||
public async Task NoAZC0020WhenReferencingInternalPropertyWithFriendAttribute() | ||
{ | ||
string code = @" | ||
using System; | ||
using TestReferenceWithInternalsVisibleTo; | ||
using System.Reflection; | ||
namespace LibraryNamespace | ||
{ | ||
public class MyClass | ||
{ | ||
public void MyMethod() | ||
{ | ||
var myClass = new PublicClass(); | ||
var value = myClass.InternalPropertyWithFriendAttribute; | ||
} | ||
} | ||
}"; | ||
await Verifier.VerifyAnalyzerAsync(code, additionalReferences: new[] { typeof(TestReferenceWithInternalsVisibleTo.PublicClass) }); | ||
} | ||
|
||
[Fact] | ||
public async Task AZC0020WhenReferencingInternalMethod() | ||
{ | ||
string code = @" | ||
using System; | ||
using TestReferenceWithInternalsVisibleTo; | ||
using System.Reflection; | ||
namespace LibraryNamespace | ||
{ | ||
public class MyClass | ||
{ | ||
public void MyMethod() | ||
{ | ||
var myClass = new PublicClass(); | ||
{|AZC0112:myClass.InternalMethod|}(); | ||
} | ||
} | ||
}"; | ||
await Verifier.VerifyAnalyzerAsync(code, additionalReferences: new[] { typeof(TestReferenceWithInternalsVisibleTo.PublicClass) }); | ||
} | ||
|
||
[Fact] | ||
public async Task NoAZC0020WhenReferencingInternalMethodWithFriendAttribute() | ||
{ | ||
string code = @" | ||
using System; | ||
using TestReferenceWithInternalsVisibleTo; | ||
using System.Reflection; | ||
namespace LibraryNamespace | ||
{ | ||
public class MyClass | ||
{ | ||
public void MyMethod() | ||
{ | ||
var myClass = new PublicClass(); | ||
myClass.InternalMethodWithFriendAttribute(); | ||
} | ||
} | ||
}"; | ||
await Verifier.VerifyAnalyzerAsync(code, additionalReferences: new[] { typeof(TestReferenceWithInternalsVisibleTo.PublicClass) }); | ||
} | ||
|
||
} | ||
} |
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
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
Oops, something went wrong.