From 7d377603cda7257d25426d800e30ad62fd24027c Mon Sep 17 00:00:00 2001 From: tpodolak Date: Sun, 22 Mar 2020 23:53:55 +0100 Subject: [PATCH] [GH-142] - adding simplified InternalsVisibleTo("DynamicProxyGenAssembly2") when System.Runtime.CompilerServices in scope --- ...dInternalsVisibleToAttributeRefactoring.cs | 28 +++------- .../Extensions/ObjectExtensions.cs | 16 ++++++ ...ension.cs => SyntaxGeneratorExtensions.cs} | 12 ++++- ...dInternalsVisibleToAttributeRefactoring.cs | 30 +++++------ ...tupSpecificationCodeFixProviderVerifier.cs | 4 ++ .../ReceivedAsExtensionMethodTests.cs | 53 ++++++++++++++++++- .../ReceivedAsOrdinaryMethodTests.cs | 53 ++++++++++++++++++- .../ReturnsAsExtensionMethodTests.cs | 53 ++++++++++++++++++- .../ReturnsAsOrdinaryMethodTests.cs | 53 ++++++++++++++++++- .../WhenAsExtensionMethodTests.cs | 53 ++++++++++++++++++- .../WhenAsOrdinaryMethodTests.cs | 53 ++++++++++++++++++- ...tupSpecificationCodeFixProviderVerifier.cs | 2 + ...tupSpecificationCodeFixProviderVerifier.cs | 4 ++ .../ReceivedAsExtensionMethodTests.cs | 45 +++++++++++++++- .../ReceivedAsOrdinaryMethodTests.cs | 48 ++++++++++++++++- .../ReturnsAsExtensionMethodTests.cs | 45 +++++++++++++++- .../ReturnsAsOrdinaryMethodTests.cs | 45 +++++++++++++++- .../WhenAsExtensionMethodTests.cs | 49 ++++++++++++++++- .../WhenAsOrdinaryMethodTests.cs | 49 ++++++++++++++++- 19 files changed, 645 insertions(+), 50 deletions(-) create mode 100644 src/NSubstitute.Analyzers.Shared/Extensions/ObjectExtensions.cs rename src/NSubstitute.Analyzers.Shared/Extensions/{SyntaxGeneratorExtension.cs => SyntaxGeneratorExtensions.cs} (64%) diff --git a/src/NSubstitute.Analyzers.CSharp/Refactorings/AddInternalsVisibleToAttributeRefactoring.cs b/src/NSubstitute.Analyzers.CSharp/Refactorings/AddInternalsVisibleToAttributeRefactoring.cs index d95f30e0..f9d23ce5 100644 --- a/src/NSubstitute.Analyzers.CSharp/Refactorings/AddInternalsVisibleToAttributeRefactoring.cs +++ b/src/NSubstitute.Analyzers.CSharp/Refactorings/AddInternalsVisibleToAttributeRefactoring.cs @@ -5,6 +5,7 @@ using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.Editing; using NSubstitute.Analyzers.Shared.Extensions; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; @@ -14,27 +15,14 @@ internal static class AddInternalsVisibleToAttributeRefactoring { public static Task RefactorAsync(Document document, CompilationUnitSyntax compilationUnitSyntax, CancellationToken cancellationToken = default) { - var addAttributeLists = compilationUnitSyntax.AddAttributeLists( - AttributeList( - AttributeTargetSpecifier( - Token(SyntaxKind.AssemblyKeyword)), - SingletonSeparatedList( - Attribute( - QualifiedName( - QualifiedName( - QualifiedName( - IdentifierName("System"), - IdentifierName("Runtime")), - IdentifierName("CompilerServices")), - IdentifierName("InternalsVisibleTo")), - AttributeArgumentList( - SingletonSeparatedList( - AttributeArgument( - LiteralExpression( - SyntaxKind.StringLiteralExpression, - Literal("DynamicProxyGenAssembly2"))))))))); + var attributeList = SyntaxGenerator.GetGenerator(document) + .InternalVisibleToDynamicProxyAttributeList() + .Cast() + .WithTarget(AttributeTargetSpecifier( + Token(SyntaxKind.AssemblyKeyword))); - return document.ReplaceNodeAsync(compilationUnitSyntax, addAttributeLists, CancellationToken.None); + var updatedCompilationUnitSyntax = compilationUnitSyntax.AddAttributeLists(attributeList); + return document.ReplaceNodeAsync(compilationUnitSyntax, updatedCompilationUnitSyntax, cancellationToken); } public static void RegisterCodeFix(CodeFixContext context, Diagnostic diagnostic, CompilationUnitSyntax compilationUnitSyntax) diff --git a/src/NSubstitute.Analyzers.Shared/Extensions/ObjectExtensions.cs b/src/NSubstitute.Analyzers.Shared/Extensions/ObjectExtensions.cs new file mode 100644 index 00000000..b24a2572 --- /dev/null +++ b/src/NSubstitute.Analyzers.Shared/Extensions/ObjectExtensions.cs @@ -0,0 +1,16 @@ +namespace NSubstitute.Analyzers.Shared.Extensions +{ + internal static class ObjectExtensions + { + /// + /// Performs unsafe cast from to + /// + /// Source object. + /// Type to cast to. + /// Cast source. + public static T Cast(this object source) + { + return (T)source; + } + } +} \ No newline at end of file diff --git a/src/NSubstitute.Analyzers.Shared/Extensions/SyntaxGeneratorExtension.cs b/src/NSubstitute.Analyzers.Shared/Extensions/SyntaxGeneratorExtensions.cs similarity index 64% rename from src/NSubstitute.Analyzers.Shared/Extensions/SyntaxGeneratorExtension.cs rename to src/NSubstitute.Analyzers.Shared/Extensions/SyntaxGeneratorExtensions.cs index b0360fdf..09f4300e 100644 --- a/src/NSubstitute.Analyzers.Shared/Extensions/SyntaxGeneratorExtension.cs +++ b/src/NSubstitute.Analyzers.Shared/Extensions/SyntaxGeneratorExtensions.cs @@ -3,7 +3,7 @@ namespace NSubstitute.Analyzers.Shared.Extensions { - internal static class SyntaxGeneratorExtension + internal static class SyntaxGeneratorExtensions { public static SyntaxNode SubstituteForInvocationExpression( this SyntaxGenerator syntaxGenerator, @@ -21,5 +21,15 @@ public static SyntaxNode SubstituteForInvocationExpression( return invocationExpression; } + + public static SyntaxNode InternalVisibleToDynamicProxyAttributeList(this SyntaxGenerator syntaxGenerator) + { + var attributeArguments = + syntaxGenerator.AttributeArgument(syntaxGenerator.LiteralExpression("DynamicProxyGenAssembly2")); + + return syntaxGenerator.Attribute( + "System.Runtime.CompilerServices.InternalsVisibleTo", + attributeArguments); + } } } \ No newline at end of file diff --git a/src/NSubstitute.Analyzers.VisualBasic/Refactorings/AddInternalsVisibleToAttributeRefactoring.cs b/src/NSubstitute.Analyzers.VisualBasic/Refactorings/AddInternalsVisibleToAttributeRefactoring.cs index dc67f2a6..506dbd40 100644 --- a/src/NSubstitute.Analyzers.VisualBasic/Refactorings/AddInternalsVisibleToAttributeRefactoring.cs +++ b/src/NSubstitute.Analyzers.VisualBasic/Refactorings/AddInternalsVisibleToAttributeRefactoring.cs @@ -1,8 +1,10 @@ +using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeActions; using Microsoft.CodeAnalysis.CodeFixes; +using Microsoft.CodeAnalysis.Editing; using Microsoft.CodeAnalysis.VisualBasic; using Microsoft.CodeAnalysis.VisualBasic.Syntax; using NSubstitute.Analyzers.Shared.Extensions; @@ -14,23 +16,19 @@ internal static class AddInternalsVisibleToAttributeRefactoring { public static Task RefactorAsync(Document document, CompilationUnitSyntax compilationUnitSyntax, CancellationToken cancellationToken = default) { - var addAttributeLists = compilationUnitSyntax.AddAttributes( - AttributesStatement(SingletonList( - AttributeList(SingletonSeparatedList(Attribute( - AttributeTarget(Token(SyntaxKind.AssemblyKeyword)), - QualifiedName( - QualifiedName( - QualifiedName( - IdentifierName("System"), - IdentifierName("Runtime")), - IdentifierName("CompilerServices")), - IdentifierName("InternalsVisibleTo")), - ArgumentList(SingletonSeparatedList(SimpleArgument( - LiteralExpression( - SyntaxKind.StringLiteralExpression, - Literal("DynamicProxyGenAssembly2"))))))))))); + var attributeList = SyntaxGenerator.GetGenerator(document) + .InternalVisibleToDynamicProxyAttributeList() + .Cast(); - return document.ReplaceNodeAsync(compilationUnitSyntax, addAttributeLists, CancellationToken.None); + var assemblyAttributes = attributeList.Attributes.Select(attr => + attr.WithTarget(AttributeTarget(Token(SyntaxKind.AssemblyKeyword)))); + + attributeList = attributeList.WithAttributes(SeparatedList(assemblyAttributes)); + + var updatedCompilationUnitSyntax = + compilationUnitSyntax.AddAttributes(AttributesStatement(SingletonList(attributeList))); + + return document.ReplaceNodeAsync(compilationUnitSyntax, updatedCompilationUnitSyntax, cancellationToken); } public static void RegisterCodeFix(CodeFixContext context, Diagnostic diagnostic, CompilationUnitSyntax compilationUnitSyntax) diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/InternalSetupSpecificationCodeFixProviderVerifier.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/InternalSetupSpecificationCodeFixProviderVerifier.cs index 93613331..ae5a8825 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/InternalSetupSpecificationCodeFixProviderVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/InternalSetupSpecificationCodeFixProviderVerifier.cs @@ -44,5 +44,9 @@ public abstract class InternalSetupSpecificationCodeFixProviderVerifier : CSharp [InlineData(".FooBar()")] [InlineData("[0]")] public abstract Task AppendsInternalsVisibleTo_ToTopLevelCompilationUnit_WhenUsedWithInternalMember(string method, string call); + + [CombinatoryTheory] + [InlineData] + public abstract Task AppendsInternalsVisibleToWithFullyQualifiedName_WhenUsedWithInternalMemberAndCompilerServicesNotImported(string method); } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/ReceivedAsExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/ReceivedAsExtensionMethodTests.cs index 88f160f0..19e05926 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/ReceivedAsExtensionMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/ReceivedAsExtensionMethodTests.cs @@ -350,7 +350,7 @@ public void Test() var newSource = $@"using NSubstitute; using System.Runtime.CompilerServices; [assembly: InternalsVisibleTo(""OtherAssembly"")] -[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""DynamicProxyGenAssembly2"")] +[assembly: InternalsVisibleTo(""DynamicProxyGenAssembly2"")] namespace MyNamespace {{ @@ -377,6 +377,57 @@ public void Test() var x = substitute.{method}(){call}; }} }} +}}"; + await VerifyFix(oldSource, newSource, 2); + } + + public override async Task AppendsInternalsVisibleToWithFullyQualifiedName_WhenUsedWithInternalMemberAndCompilerServicesNotImported(string method) + { + var oldSource = $@"using NSubstitute; +[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""OtherAssembly"")] + +namespace MyNamespace +{{ + public class Foo + {{ + internal virtual int FooBar() + {{ + return 1; + }} + }} + + public class FooTests + {{ + public void Test() + {{ + var substitute = NSubstitute.Substitute.For(); + var x = substitute.{method}().FooBar(); + }} + }} +}}"; + + var newSource = $@"using NSubstitute; +[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""OtherAssembly"")] +[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""DynamicProxyGenAssembly2"")] + +namespace MyNamespace +{{ + public class Foo + {{ + internal virtual int FooBar() + {{ + return 1; + }} + }} + + public class FooTests + {{ + public void Test() + {{ + var substitute = NSubstitute.Substitute.For(); + var x = substitute.{method}().FooBar(); + }} + }} }}"; await VerifyFix(oldSource, newSource, 2); } diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/ReceivedAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/ReceivedAsOrdinaryMethodTests.cs index 5372de7a..1fa3110e 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/ReceivedAsOrdinaryMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/ReceivedAsOrdinaryMethodTests.cs @@ -350,7 +350,7 @@ public void Test() var newSource = $@"using NSubstitute; using System.Runtime.CompilerServices; [assembly: InternalsVisibleTo(""OtherAssembly"")] -[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""DynamicProxyGenAssembly2"")] +[assembly: InternalsVisibleTo(""DynamicProxyGenAssembly2"")] namespace MyNamespace {{ @@ -377,6 +377,57 @@ public void Test() var x = {method}(substitute){call}; }} }} +}}"; + await VerifyFix(oldSource, newSource, 2); + } + + public override async Task AppendsInternalsVisibleToWithFullyQualifiedName_WhenUsedWithInternalMemberAndCompilerServicesNotImported(string method) + { + var oldSource = $@"using NSubstitute; +[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""OtherAssembly"")] + +namespace MyNamespace +{{ + public class Foo + {{ + internal virtual int FooBar() + {{ + return 1; + }} + }} + + public class FooTests + {{ + public void Test() + {{ + var substitute = NSubstitute.Substitute.For(); + var x = {method}(substitute).FooBar(); + }} + }} +}}"; + + var newSource = $@"using NSubstitute; +[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""OtherAssembly"")] +[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""DynamicProxyGenAssembly2"")] + +namespace MyNamespace +{{ + public class Foo + {{ + internal virtual int FooBar() + {{ + return 1; + }} + }} + + public class FooTests + {{ + public void Test() + {{ + var substitute = NSubstitute.Substitute.For(); + var x = {method}(substitute).FooBar(); + }} + }} }}"; await VerifyFix(oldSource, newSource, 2); } diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/ReturnsAsExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/ReturnsAsExtensionMethodTests.cs index 977a7bb5..4a52b675 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/ReturnsAsExtensionMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/ReturnsAsExtensionMethodTests.cs @@ -347,7 +347,7 @@ public void Test() var newSource = $@"using NSubstitute; using System.Runtime.CompilerServices; [assembly: InternalsVisibleTo(""OtherAssembly"")] -[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""DynamicProxyGenAssembly2"")] +[assembly: InternalsVisibleTo(""DynamicProxyGenAssembly2"")] namespace MyNamespace {{ @@ -374,6 +374,57 @@ public void Test() var x = substitute{call}.{method}(1); }} }} +}}"; + await VerifyFix(oldSource, newSource, 2); + } + + public override async Task AppendsInternalsVisibleToWithFullyQualifiedName_WhenUsedWithInternalMemberAndCompilerServicesNotImported(string method) + { + var oldSource = $@"using NSubstitute; +[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""OtherAssembly"")] + +namespace MyNamespace +{{ + public class Foo + {{ + internal virtual int FooBar() + {{ + return 1; + }} + }} + + public class FooTests + {{ + public void Test() + {{ + var substitute = NSubstitute.Substitute.For(); + var x = substitute.FooBar().{method}(1); + }} + }} +}}"; + + var newSource = $@"using NSubstitute; +[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""OtherAssembly"")] +[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""DynamicProxyGenAssembly2"")] + +namespace MyNamespace +{{ + public class Foo + {{ + internal virtual int FooBar() + {{ + return 1; + }} + }} + + public class FooTests + {{ + public void Test() + {{ + var substitute = NSubstitute.Substitute.For(); + var x = substitute.FooBar().{method}(1); + }} + }} }}"; await VerifyFix(oldSource, newSource, 2); } diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/ReturnsAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/ReturnsAsOrdinaryMethodTests.cs index 81343375..c792c9b7 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/ReturnsAsOrdinaryMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/ReturnsAsOrdinaryMethodTests.cs @@ -347,7 +347,7 @@ public void Test() var newSource = $@"using NSubstitute; using System.Runtime.CompilerServices; [assembly: InternalsVisibleTo(""OtherAssembly"")] -[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""DynamicProxyGenAssembly2"")] +[assembly: InternalsVisibleTo(""DynamicProxyGenAssembly2"")] namespace MyNamespace {{ @@ -374,6 +374,57 @@ public void Test() var x = {method}(substitute{call}, 1); }} }} +}}"; + await VerifyFix(oldSource, newSource, 2); + } + + public override async Task AppendsInternalsVisibleToWithFullyQualifiedName_WhenUsedWithInternalMemberAndCompilerServicesNotImported(string method) + { + var oldSource = $@"using NSubstitute; +[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""OtherAssembly"")] + +namespace MyNamespace +{{ + public class Foo + {{ + internal virtual int FooBar() + {{ + return 1; + }} + }} + + public class FooTests + {{ + public void Test() + {{ + var substitute = NSubstitute.Substitute.For(); + var x = {method}(substitute.FooBar(), 1); + }} + }} +}}"; + + var newSource = $@"using NSubstitute; +[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""OtherAssembly"")] +[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""DynamicProxyGenAssembly2"")] + +namespace MyNamespace +{{ + public class Foo + {{ + internal virtual int FooBar() + {{ + return 1; + }} + }} + + public class FooTests + {{ + public void Test() + {{ + var substitute = NSubstitute.Substitute.For(); + var x = {method}(substitute.FooBar(), 1); + }} + }} }}"; await VerifyFix(oldSource, newSource, 2); } diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsExtensionMethodTests.cs index 804ebc0a..a48a6cd4 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsExtensionMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsExtensionMethodTests.cs @@ -350,7 +350,7 @@ public void Test() var newSource = $@"using NSubstitute; using System.Runtime.CompilerServices; [assembly: InternalsVisibleTo(""OtherAssembly"")] -[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""DynamicProxyGenAssembly2"")] +[assembly: InternalsVisibleTo(""DynamicProxyGenAssembly2"")] namespace MyNamespace {{ @@ -377,6 +377,57 @@ public void Test() substitute.{method}(callInfo => {{var x = callInfo{call};}}); }} }} +}}"; + await VerifyFix(oldSource, newSource, 2); + } + + public override async Task AppendsInternalsVisibleToWithFullyQualifiedName_WhenUsedWithInternalMemberAndCompilerServicesNotImported(string method) + { + var oldSource = $@"using NSubstitute; +[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""OtherAssembly"")] + +namespace MyNamespace +{{ + public class Foo + {{ + internal virtual int FooBar() + {{ + return 1; + }} + }} + + public class FooTests + {{ + public void Test() + {{ + var substitute = NSubstitute.Substitute.For(); + substitute.{method}(callInfo => {{var x = callInfo.FooBar();}}); + }} + }} +}}"; + + var newSource = $@"using NSubstitute; +[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""OtherAssembly"")] +[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""DynamicProxyGenAssembly2"")] + +namespace MyNamespace +{{ + public class Foo + {{ + internal virtual int FooBar() + {{ + return 1; + }} + }} + + public class FooTests + {{ + public void Test() + {{ + var substitute = NSubstitute.Substitute.For(); + substitute.{method}(callInfo => {{var x = callInfo.FooBar();}}); + }} + }} }}"; await VerifyFix(oldSource, newSource, 2); } diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsOrdinaryMethodTests.cs index d4c5f7b9..2f66703f 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsOrdinaryMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsOrdinaryMethodTests.cs @@ -350,7 +350,7 @@ public void Test() var newSource = $@"using NSubstitute; using System.Runtime.CompilerServices; [assembly: InternalsVisibleTo(""OtherAssembly"")] -[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""DynamicProxyGenAssembly2"")] +[assembly: InternalsVisibleTo(""DynamicProxyGenAssembly2"")] namespace MyNamespace {{ @@ -377,6 +377,57 @@ public void Test() {method}(substitute, callInfo => {{var x = callInfo{call};}}); }} }} +}}"; + await VerifyFix(oldSource, newSource, 2); + } + + public override async Task AppendsInternalsVisibleToWithFullyQualifiedName_WhenUsedWithInternalMemberAndCompilerServicesNotImported(string method) + { + var oldSource = $@"using NSubstitute; +[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""OtherAssembly"")] + +namespace MyNamespace +{{ + public class Foo + {{ + internal virtual int FooBar() + {{ + return 1; + }} + }} + + public class FooTests + {{ + public void Test() + {{ + var substitute = NSubstitute.Substitute.For(); + {method}(substitute, callInfo => {{var x = callInfo.FooBar();}}); + }} + }} +}}"; + + var newSource = $@"using NSubstitute; +[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""OtherAssembly"")] +[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""DynamicProxyGenAssembly2"")] + +namespace MyNamespace +{{ + public class Foo + {{ + internal virtual int FooBar() + {{ + return 1; + }} + }} + + public class FooTests + {{ + public void Test() + {{ + var substitute = NSubstitute.Substitute.For(); + {method}(substitute, callInfo => {{var x = callInfo.FooBar();}}); + }} + }} }}"; await VerifyFix(oldSource, newSource, 2); } diff --git a/tests/NSubstitute.Analyzers.Tests.Shared/CodeFixProviders/IInternalSetupSpecificationCodeFixProviderVerifier.cs b/tests/NSubstitute.Analyzers.Tests.Shared/CodeFixProviders/IInternalSetupSpecificationCodeFixProviderVerifier.cs index ce9aa1cf..026fc143 100644 --- a/tests/NSubstitute.Analyzers.Tests.Shared/CodeFixProviders/IInternalSetupSpecificationCodeFixProviderVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.Shared/CodeFixProviders/IInternalSetupSpecificationCodeFixProviderVerifier.cs @@ -17,5 +17,7 @@ public interface IInternalSetupSpecificationCodeFixProviderVerifier Task AppendsProtectedInternal_ToMethod_WhenUsedWithInternalMember(string method); Task AppendsInternalsVisibleTo_ToTopLevelCompilationUnit_WhenUsedWithInternalMember(string method, string call); + + Task AppendsInternalsVisibleToWithFullyQualifiedName_WhenUsedWithInternalMemberAndCompilerServicesNotImported(string method); } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/InternalSetupSpecificationCodeFixProviderVerifier.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/InternalSetupSpecificationCodeFixProviderVerifier.cs index be03b427..d18c3d6e 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/InternalSetupSpecificationCodeFixProviderVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/InternalSetupSpecificationCodeFixProviderVerifier.cs @@ -44,5 +44,9 @@ public abstract class InternalSetupSpecificationCodeFixProviderVerifier : Visual [InlineData(".FooBar()")] [InlineData("(0)")] public abstract Task AppendsInternalsVisibleTo_ToTopLevelCompilationUnit_WhenUsedWithInternalMember(string method, string call); + + [CombinatoryTheory] + [InlineData] + public abstract Task AppendsInternalsVisibleToWithFullyQualifiedName_WhenUsedWithInternalMemberAndCompilerServicesNotImported(string method); } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/ReceivedAsExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/ReceivedAsExtensionMethodTests.cs index 6373c405..bcf72280 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/ReceivedAsExtensionMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/ReceivedAsExtensionMethodTests.cs @@ -311,7 +311,7 @@ End Namespace Imports System.Runtime.CompilerServices - + Namespace MyNamespace Public Class Foo Friend Overridable ReadOnly Property Bar As Integer @@ -334,6 +334,49 @@ Public Sub Test() End Sub End Class End Namespace +"; + await VerifyFix(oldSource, newSource, 2); + } + + public override async Task AppendsInternalsVisibleToWithFullyQualifiedName_WhenUsedWithInternalMemberAndCompilerServicesNotImported(string method) + { + var oldSource = $@"Imports NSubstitute + + +Namespace MyNamespace + Public Class Foo + Friend Overridable Function FooBar() As Integer + Return 1 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + Dim x = substitute.{method}().FooBar() + End Sub + End Class +End Namespace +"; + + var newSource = $@"Imports NSubstitute + + + +Namespace MyNamespace + Public Class Foo + Friend Overridable Function FooBar() As Integer + Return 1 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + Dim x = substitute.{method}().FooBar() + End Sub + End Class +End Namespace "; await VerifyFix(oldSource, newSource, 2); } diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/ReceivedAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/ReceivedAsOrdinaryMethodTests.cs index d7a037a8..423009ef 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/ReceivedAsOrdinaryMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/ReceivedAsOrdinaryMethodTests.cs @@ -277,7 +277,8 @@ End Namespace await VerifyFix(oldSource, newSource, 0); } - public override async Task AppendsInternalsVisibleTo_ToTopLevelCompilationUnit_WhenUsedWithInternalMember(string method, string call) + public override async Task AppendsInternalsVisibleTo_ToTopLevelCompilationUnit_WhenUsedWithInternalMember( + string method, string call) { var oldSource = $@"Imports NSubstitute Imports System.Runtime.CompilerServices @@ -311,7 +312,7 @@ End Namespace Imports System.Runtime.CompilerServices - + Namespace MyNamespace Public Class Foo Friend Overridable ReadOnly Property Bar As Integer @@ -334,6 +335,49 @@ Public Sub Test() End Sub End Class End Namespace +"; + await VerifyFix(oldSource, newSource, 2); + } + + public override async Task AppendsInternalsVisibleToWithFullyQualifiedName_WhenUsedWithInternalMemberAndCompilerServicesNotImported(string method) + { + var oldSource = $@"Imports NSubstitute + + +Namespace MyNamespace + Public Class Foo + Friend Overridable Function FooBar() As Integer + Return 1 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + Dim x = {method}(substitute).FooBar() + End Sub + End Class +End Namespace +"; + + var newSource = $@"Imports NSubstitute + + + +Namespace MyNamespace + Public Class Foo + Friend Overridable Function FooBar() As Integer + Return 1 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + Dim x = {method}(substitute).FooBar() + End Sub + End Class +End Namespace "; await VerifyFix(oldSource, newSource, 2); } diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/ReturnsAsExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/ReturnsAsExtensionMethodTests.cs index 42ccf3ad..3811c259 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/ReturnsAsExtensionMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/ReturnsAsExtensionMethodTests.cs @@ -307,7 +307,7 @@ End Namespace Imports System.Runtime.CompilerServices - + Namespace MyNamespace Public Class Foo Friend Overridable ReadOnly Property Bar As Integer @@ -330,6 +330,49 @@ Public Sub Test() End Sub End Class End Namespace +"; + await VerifyFix(oldSource, newSource, 2); + } + + public override async Task AppendsInternalsVisibleToWithFullyQualifiedName_WhenUsedWithInternalMemberAndCompilerServicesNotImported(string method) + { + var oldSource = $@"Imports NSubstitute + + +Namespace MyNamespace + Public Class Foo + Friend Overridable Function FooBar() As Integer + Return 1 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + Dim x = substitute.FooBar().{method}(1) + End Sub + End Class +End Namespace +"; + + var newSource = $@"Imports NSubstitute + + + +Namespace MyNamespace + Public Class Foo + Friend Overridable Function FooBar() As Integer + Return 1 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + Dim x = substitute.FooBar().{method}(1) + End Sub + End Class +End Namespace "; await VerifyFix(oldSource, newSource, 2); } diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/ReturnsAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/ReturnsAsOrdinaryMethodTests.cs index b30c1496..50a51ddb 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/ReturnsAsOrdinaryMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/ReturnsAsOrdinaryMethodTests.cs @@ -307,7 +307,7 @@ End Namespace Imports System.Runtime.CompilerServices - + Namespace MyNamespace Public Class Foo Friend Overridable ReadOnly Property Bar As Integer @@ -330,6 +330,49 @@ Public Sub Test() End Sub End Class End Namespace +"; + await VerifyFix(oldSource, newSource, 2); + } + + public override async Task AppendsInternalsVisibleToWithFullyQualifiedName_WhenUsedWithInternalMemberAndCompilerServicesNotImported(string method) + { + var oldSource = $@"Imports NSubstitute + + +Namespace MyNamespace + Public Class Foo + Friend Overridable Function FooBar() As Integer + Return 1 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + Dim x = {method}(substitute.FooBar(), 1) + End Sub + End Class +End Namespace +"; + + var newSource = $@"Imports NSubstitute + + + +Namespace MyNamespace + Public Class Foo + Friend Overridable Function FooBar() As Integer + Return 1 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + Dim x = {method}(substitute.FooBar(), 1) + End Sub + End Class +End Namespace "; await VerifyFix(oldSource, newSource, 2); } diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsExtensionMethodTests.cs index 439d2b84..8c0411c6 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsExtensionMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsExtensionMethodTests.cs @@ -337,7 +337,7 @@ End Namespace Imports System.Runtime.CompilerServices - + Namespace MyNamespace Public Class Foo Friend Overridable ReadOnly Property Bar As Integer @@ -362,6 +362,53 @@ Public Sub Test() End Sub End Class End Namespace +"; + await VerifyFix(oldSource, newSource, 2); + } + + public override async Task AppendsInternalsVisibleToWithFullyQualifiedName_WhenUsedWithInternalMemberAndCompilerServicesNotImported(string method) + { + var oldSource = $@"Imports NSubstitute + + +Namespace MyNamespace + Public Class Foo + Friend Overridable Function FooBar() As Integer + Return 1 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + substitute.{method}(Sub(sb As Foo) + Dim x = sb.FooBar() + End Sub) + End Sub + End Class +End Namespace +"; + + var newSource = $@"Imports NSubstitute + + + +Namespace MyNamespace + Public Class Foo + Friend Overridable Function FooBar() As Integer + Return 1 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + substitute.{method}(Sub(sb As Foo) + Dim x = sb.FooBar() + End Sub) + End Sub + End Class +End Namespace "; await VerifyFix(oldSource, newSource, 2); } diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsOrdinaryMethodTests.cs index 73919b39..0c23d590 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsOrdinaryMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsOrdinaryMethodTests.cs @@ -337,7 +337,7 @@ End Namespace Imports System.Runtime.CompilerServices - + Namespace MyNamespace Public Class Foo Friend Overridable ReadOnly Property Bar As Integer @@ -362,6 +362,53 @@ Public Sub Test() End Sub End Class End Namespace +"; + await VerifyFix(oldSource, newSource, 2); + } + + public override async Task AppendsInternalsVisibleToWithFullyQualifiedName_WhenUsedWithInternalMemberAndCompilerServicesNotImported(string method) + { + var oldSource = $@"Imports NSubstitute + + +Namespace MyNamespace + Public Class Foo + Friend Overridable Function FooBar() As Integer + Return 1 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + {method}(substitute, Sub(sb As Foo) + Dim x = sb.FooBar() + End Sub) + End Sub + End Class +End Namespace +"; + + var newSource = $@"Imports NSubstitute + + + +Namespace MyNamespace + Public Class Foo + Friend Overridable Function FooBar() As Integer + Return 1 + End Function + End Class + + Public Class FooTests + Public Sub Test() + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + {method}(substitute, Sub(sb As Foo) + Dim x = sb.FooBar() + End Sub) + End Sub + End Class +End Namespace "; await VerifyFix(oldSource, newSource, 2); }