From 6309ff7a7a9918389bda0df61de55cd326d838d9 Mon Sep 17 00:00:00 2001 From: tpodolak Date: Sun, 15 Mar 2020 23:03:27 +0100 Subject: [PATCH] [GH-139] - test infrastructure cleanup (#140) --- .../CSharpProjectOptions.cs | 34 +++ .../CSharpWorkspaceFactory.cs | 33 +++ .../CSharpCodeFixActionsVerifier.cs | 15 +- .../CSharpCodeFixVerifier.cs | 15 +- ...SharpSuppressDiagnosticSettingsVerifier.cs | 12 +- ...gumentsForInterfaceCodeFixProviderTests.cs | 14 +- ...alSetupSpecificationCodeFixActionsTests.cs | 35 ++- ...tupSpecificationCodeFixProviderVerifier.cs | 14 +- .../ReceivedAsExtensionMethodTests.cs | 9 +- .../ReceivedAsOrdinaryMethodTests.cs | 7 +- .../WhenAsExtensionMethodTests.cs | 8 +- .../WhenAsOrdinaryMethodTests.cs | 7 +- ...rSuppressDiagnosticsCodeFixActionsTests.cs | 14 +- ...tMatcherSuppressDiagnosticsCodeFixTests.cs | 14 +- ...rSuppressDiagnosticsCodeFixActionsTests.cs | 14 +- ...emberSuppressDiagnosticsCodeFixVerifier.cs | 14 +- ...edForUnsupportedTypeCodeFixActionsTests.cs | 14 +- ...dForUnsupportedTypeCodeFixProviderTests.cs | 14 +- .../ReEntrantSetupCodeFixActionsTests.cs | 14 +- .../ReEntrantSetupCodeFixVerifier.cs | 26 +- ...uteForInternalMemberCodeFixActionsTests.cs | 34 ++- ...stituteForInternalMemberCodeFixVerifier.cs | 15 +- ...ceivedInOrderCallbackDiagnosticVerifier.cs | 7 +- .../CSharpDiagnosticVerifier.cs | 12 +- .../CallInfoDiagnosticVerifier.cs | 7 +- ...ngArgumentAssignmentsDiagnosticVerifier.cs | 9 +- ...onSubstitutableMemberDiagnosticVerifier.cs | 14 +- .../ReturnsAsExtensionMethodTests.cs | 6 +- .../ReturnsAsOrdinaryMethodTests.cs | 6 +- .../ReturnsNullAsExtensionMethodTests.cs | 6 +- .../ReturnsNullAsOrdinaryMethodTests.cs | 6 +- .../ThrowsAsExtensionMethodTests.cs | 6 +- ...sionMethodWithGenericTypeSpecifiedTests.cs | 6 +- .../ThrowsAsOrdinaryMethodTests.cs | 6 +- ...naryMethodWithGenericTypeSpecifiedTests.cs | 6 +- ...MemberArgumentMatcherDiagnosticVerifier.cs | 16 +- ...SubstitutableMemberArgumentMatcherTests.cs | 2 +- ...tutableMemberReceivedDiagnosticVerifier.cs | 7 +- ...bstitutableMemberWhenDiagnosticVerifier.cs | 7 +- ...ReEntrantReturnsSetupDiagnosticVerifier.cs | 7 +- .../ReturnsAsExtensionMethodTests.cs | 8 +- .../ReturnsAsOrdinaryMethodTests.cs | 8 +- ...ivedInReceivedInOrderDiagnosticVerifier.cs | 7 +- .../SubstituteDiagnosticVerifier.cs | 7 +- .../UnusedReceivedDiagnosticVerifier.cs | 8 +- .../CodeFixCodeActionsVerifier.cs | 56 ++-- .../CodeFixProviders/CodeFixVerifier.cs | 193 ++++--------- .../SuppressDiagnosticSettingsVerifier.cs | 62 ++--- .../CodeVerifier.cs | 49 ++++ .../DiagnosticAnalyzers/DiagnosticVerifier.cs | 259 ++++-------------- .../DiagnosticComparer.cs | 33 +++ .../Extensions/AssertionScopeExtensions.cs | 14 + .../Extensions/CompilationExtensions.cs | 44 +++ .../Extensions/DocumentExtension.cs | 45 +++ .../Extensions/IEnumerableExtensions.cs | 22 ++ .../Extensions/ProjectExtensions.cs | 35 +++ .../ProjectOptions.cs | 18 ++ .../RuntimeMetadataReference.cs | 52 ++++ .../WorkspaceFactory.cs | 86 ++++++ ...gumentsForInterfaceCodeFixProviderTests.cs | 17 +- ...alSetupSpecificationCodeFixActionsTests.cs | 34 ++- ...tupSpecificationCodeFixProviderVerifier.cs | 14 +- .../ReceivedAsExtensionMethodTests.cs | 8 +- .../ReceivedAsOrdinaryMethodTests.cs | 7 +- .../WhenAsExtensionMethodTests.cs | 8 +- .../WhenAsOrdinaryMethodTests.cs | 7 +- ...rSuppressDiagnosticsCodeFixActionsTests.cs | 14 +- ...emberSuppressDiagnosticsCodeFixVerifier.cs | 14 +- ...rSuppressDiagnosticsCodeFixActionsTests.cs | 14 +- ...tMatcherSuppressDiagnosticsCodeFixTests.cs | 14 +- ...edForUnsupportedTypeCodeFixActionsTests.cs | 14 +- ...dForUnsupportedTypeCodeFixProviderTests.cs | 14 +- .../ReEntrantSetupCodeFixActionsTests.cs | 14 +- .../ReEntrantSetupCodeFixVerifier.cs | 14 +- ...uteForInternalMemberCodeFixActionsTests.cs | 32 +-- ...stituteForInternalMemberCodeFixVerifier.cs | 14 +- .../VisualBasicCodeFixActionsVerifier.cs | 15 +- .../VisualBasicCodeFixVerifier.cs | 15 +- ...BasicSuppressDiagnosticSettingsVerifier.cs | 19 +- ...ceivedInOrderCallbackDiagnosticVerifier.cs | 7 +- .../CallInfoDiagnosticVerifier.cs | 7 +- ...ngArgumentAssignmentsDiagnosticVerifier.cs | 9 +- ...onSubstitutableMemberDiagnosticVerifier.cs | 14 +- .../ReturnsAsExtensionMethodTests.cs | 6 +- .../ReturnsAsOrdinaryMethodTests.cs | 6 +- .../ReturnsNullAsExtensionMethodTests.cs | 6 +- .../ReturnsNullAsOrdinaryMethodTests.cs | 6 +- .../ThrowsAsExtensionMethodTests.cs | 6 +- .../ThrowsAsOrdinaryMethodTests.cs | 6 +- ...naryMethodWithGenericTypeSpecifiedTests.cs | 6 +- ...MemberArgumentMatcherDiagnosticVerifier.cs | 20 +- ...tutableMemberReceivedDiagnosticVerifier.cs | 7 +- ...bstitutableMemberWhenDiagnosticVerifier.cs | 7 +- ...ReEntrantReturnsSetupDiagnosticVerifier.cs | 7 +- .../ReturnsAsExtensionMethodTests.cs | 8 +- .../ReturnsAsOrdinaryMethodTests.cs | 8 +- ...ivedInReceivedInOrderDiagnosticVerifier.cs | 7 +- .../SubstituteDiagnosticVerifier.cs | 6 +- .../UnusedReceivedDiagnosticVerifier.cs | 8 +- .../VisualBasicDiagnosticVerifier.cs | 25 +- .../VisualBasicProjectOptions.cs | 36 +++ .../VisualBasicWorkspaceFactory.cs | 39 +++ 102 files changed, 1033 insertions(+), 1044 deletions(-) create mode 100644 tests/NSubstitute.Analyzers.Tests.CSharp/CSharpProjectOptions.cs create mode 100644 tests/NSubstitute.Analyzers.Tests.CSharp/CSharpWorkspaceFactory.cs create mode 100644 tests/NSubstitute.Analyzers.Tests.Shared/CodeVerifier.cs create mode 100644 tests/NSubstitute.Analyzers.Tests.Shared/DiagnosticComparer.cs create mode 100644 tests/NSubstitute.Analyzers.Tests.Shared/Extensions/AssertionScopeExtensions.cs create mode 100644 tests/NSubstitute.Analyzers.Tests.Shared/Extensions/CompilationExtensions.cs create mode 100644 tests/NSubstitute.Analyzers.Tests.Shared/Extensions/DocumentExtension.cs create mode 100644 tests/NSubstitute.Analyzers.Tests.Shared/Extensions/IEnumerableExtensions.cs create mode 100644 tests/NSubstitute.Analyzers.Tests.Shared/Extensions/ProjectExtensions.cs create mode 100644 tests/NSubstitute.Analyzers.Tests.Shared/ProjectOptions.cs create mode 100644 tests/NSubstitute.Analyzers.Tests.Shared/RuntimeMetadataReference.cs create mode 100644 tests/NSubstitute.Analyzers.Tests.Shared/WorkspaceFactory.cs create mode 100644 tests/NSubstitute.Analyzers.Tests.VisualBasic/VisualBasicProjectOptions.cs create mode 100644 tests/NSubstitute.Analyzers.Tests.VisualBasic/VisualBasicWorkspaceFactory.cs diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/CSharpProjectOptions.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/CSharpProjectOptions.cs new file mode 100644 index 00000000..ffff7742 --- /dev/null +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/CSharpProjectOptions.cs @@ -0,0 +1,34 @@ +using System.Collections.Immutable; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using NSubstitute.Analyzers.Tests.Shared; + +namespace NSubstitute.Analyzers.Tests.CSharp +{ + public class CSharpProjectOptions : ProjectOptions + { + public static CSharpProjectOptions Default { get; } = new CSharpProjectOptions( + RuntimeMetadataReference.Default, + new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); + + private CSharpProjectOptions( + ImmutableArray metadataReferences, + CompilationOptions compilationOptions) + : base(metadataReferences, compilationOptions) + { + } + + public CSharpProjectOptions WithWarningLevel(int warningLevel) + { + var compilationOptions = (CSharpCompilationOptions)CompilationOptions; + return new CSharpProjectOptions( + MetadataReferences, + new CSharpCompilationOptions(compilationOptions.OutputKind, warningLevel: warningLevel)); + } + + public CSharpProjectOptions WithAdditionalMetadataReferences(MetadataReference[] metadataReferences) + { + return new CSharpProjectOptions(MetadataReferences.AddRange(metadataReferences), CompilationOptions); + } + } +} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/CSharpWorkspaceFactory.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/CSharpWorkspaceFactory.cs new file mode 100644 index 00000000..c903a83f --- /dev/null +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/CSharpWorkspaceFactory.cs @@ -0,0 +1,33 @@ +using Microsoft.CodeAnalysis; +using NSubstitute.Analyzers.Tests.Shared; + +namespace NSubstitute.Analyzers.Tests.CSharp +{ + public class CSharpWorkspaceFactory : WorkspaceFactory + { + public static CSharpWorkspaceFactory Default { get; } = new CSharpWorkspaceFactory(CSharpProjectOptions.Default); + + protected override string DocumentExtension { get; } = "cs"; + + protected override string Language { get; } = LanguageNames.CSharp; + + protected override ProjectOptions ProjectOptions { get; } + + private CSharpProjectOptions CSharpProjectOptions => (CSharpProjectOptions)ProjectOptions; + + public CSharpWorkspaceFactory WithWarningLevel(int warningLevel) + { + return new CSharpWorkspaceFactory(CSharpProjectOptions.WithWarningLevel(warningLevel)); + } + + public CSharpWorkspaceFactory WithAdditionalMetadataReferences(MetadataReference[] metadataReferences) + { + return new CSharpWorkspaceFactory(CSharpProjectOptions.WithAdditionalMetadataReferences(metadataReferences)); + } + + private CSharpWorkspaceFactory(CSharpProjectOptions projectOptions) + { + ProjectOptions = projectOptions; + } + } +} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/CSharpCodeFixActionsVerifier.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/CSharpCodeFixActionsVerifier.cs index 43e3d02e..20a7c574 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/CSharpCodeFixActionsVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/CSharpCodeFixActionsVerifier.cs @@ -1,18 +1,17 @@ -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using NSubstitute.Analyzers.Tests.Shared.CodeFixProviders; +using NSubstitute.Analyzers.Tests.Shared.CodeFixProviders; namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests { public abstract class CSharpCodeFixActionsVerifier : CodeFixCodeActionsVerifier { - protected override string Language { get; } = LanguageNames.CSharp; - - protected override string FileExtension { get; } = "cs"; + protected CSharpCodeFixActionsVerifier() + : this(CSharpWorkspaceFactory.Default) + { + } - protected override CompilationOptions GetCompilationOptions() + protected CSharpCodeFixActionsVerifier(CSharpWorkspaceFactory factory) + : base(factory) { - return new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary); } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/CSharpCodeFixVerifier.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/CSharpCodeFixVerifier.cs index 7eed635c..c1d58c5c 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/CSharpCodeFixVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/CSharpCodeFixVerifier.cs @@ -1,18 +1,17 @@ -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using NSubstitute.Analyzers.Tests.Shared.CodeFixProviders; +using NSubstitute.Analyzers.Tests.Shared.CodeFixProviders; namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests { public abstract class CSharpCodeFixVerifier : CodeFixVerifier { - protected override string Language { get; } = LanguageNames.CSharp; - - protected override string FileExtension { get; } = "cs"; + protected CSharpCodeFixVerifier() + : this(CSharpWorkspaceFactory.Default) + { + } - protected override CompilationOptions GetCompilationOptions() + protected CSharpCodeFixVerifier(CSharpWorkspaceFactory workspaceFactory) + : base(workspaceFactory) { - return new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary); } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/CSharpSuppressDiagnosticSettingsVerifier.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/CSharpSuppressDiagnosticSettingsVerifier.cs index dec3e444..1f822905 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/CSharpSuppressDiagnosticSettingsVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/CSharpSuppressDiagnosticSettingsVerifier.cs @@ -1,18 +1,12 @@ -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using NSubstitute.Analyzers.Tests.Shared.CodeFixProviders; +using NSubstitute.Analyzers.Tests.Shared.CodeFixProviders; namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests { public abstract class CSharpSuppressDiagnosticSettingsVerifier : SuppressDiagnosticSettingsVerifier { - protected override string Language { get; } = LanguageNames.CSharp; - - protected override string FileExtension { get; } = "cs"; - - protected override CompilationOptions GetCompilationOptions() + protected CSharpSuppressDiagnosticSettingsVerifier() + : base(CSharpWorkspaceFactory.Default) { - return new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary); } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/ConstructorArgumentsForInterfaceCodeFixProviderTests/ConstructorArgumentsForInterfaceCodeFixProviderTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/ConstructorArgumentsForInterfaceCodeFixProviderTests/ConstructorArgumentsForInterfaceCodeFixProviderTests.cs index 3e9cdb50..c1b45677 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/ConstructorArgumentsForInterfaceCodeFixProviderTests/ConstructorArgumentsForInterfaceCodeFixProviderTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/ConstructorArgumentsForInterfaceCodeFixProviderTests/ConstructorArgumentsForInterfaceCodeFixProviderTests.cs @@ -10,6 +10,10 @@ namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests.ConstructorArg { public class ConstructorArgumentsForInterfaceCodeFixProviderTests : CSharpCodeFixVerifier, IConstructorArgumentsForInterfaceCodeFixVerifier { + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new SubstituteAnalyzer(); + + protected override CodeFixProvider CodeFixProvider { get; } = new ConstructorArgumentsForInterfaceCodeFixProvider(); + [Fact] public async Task RemovesInvocationArguments_WhenGenericFor_UsedWithParametersForInterface() { @@ -126,15 +130,5 @@ public void Test() }"; await VerifyFix(source, newSource); } - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new SubstituteAnalyzer(); - } - - protected override CodeFixProvider GetCodeFixProvider() - { - return new ConstructorArgumentsForInterfaceCodeFixProvider(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/InternalSetupSpecificationCodeFixActionsTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/InternalSetupSpecificationCodeFixActionsTests.cs index b042957a..415078f9 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/InternalSetupSpecificationCodeFixActionsTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/InternalSetupSpecificationCodeFixActionsTests.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; @@ -10,13 +9,26 @@ using NSubstitute.Analyzers.CSharp.CodeFixProviders; using NSubstitute.Analyzers.CSharp.DiagnosticAnalyzers; using NSubstitute.Analyzers.Tests.Shared.CodeFixProviders; -using NSubstitute.Analyzers.Tests.Shared.Extensibility; using Xunit; namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests.InternalSetupSpecificationCodeFixProviderTests { public class InternalSetupSpecificationCodeFixActionsTests : CSharpCodeFixActionsVerifier, IInternalSetupSpecificationCodeFixActionsVerifier { + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new NonSubstitutableMemberAnalyzer(); + + protected override CodeFixProvider CodeFixProvider { get; } = new InternalSetupSpecificationCodeFixProvider(); + + private static readonly MetadataReference[] AdditionalMetadataReferences = + { + GetInternalLibraryMetadataReference() + }; + + public InternalSetupSpecificationCodeFixActionsTests() + : base(CSharpWorkspaceFactory.Default.WithAdditionalMetadataReferences(AdditionalMetadataReferences)) + { + } + [Fact] public async Task CreateCodeActions_InProperOrder() { @@ -65,26 +77,11 @@ public void Test() await VerifyCodeActions(source); } - protected override IEnumerable GetAdditionalMetadataReferences() - { - return new[] { GetInternalLibraryMetadataReference() }; - } - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new NonSubstitutableMemberAnalyzer(); - } - - protected override CodeFixProvider GetCodeFixProvider() - { - return new InternalSetupSpecificationCodeFixProvider(); - } - - private static PortableExecutableReference GetInternalLibraryMetadataReference() + private static MetadataReference GetInternalLibraryMetadataReference() { var syntaxTree = CSharpSyntaxTree.ParseText($@" using System.Runtime.CompilerServices; -[assembly: InternalsVisibleTo(""{TestProjectName}"")] +[assembly: InternalsVisibleTo(""{Shared.WorkspaceFactory.DefaultProjectName}"")] namespace ExternalNamespace {{ public class InternalFoo diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/InternalSetupSpecificationCodeFixProviderVerifier.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/InternalSetupSpecificationCodeFixProviderVerifier.cs index fd655969..93613331 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/InternalSetupSpecificationCodeFixProviderVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/InternalSetupSpecificationCodeFixProviderVerifier.cs @@ -11,6 +11,10 @@ namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests.InternalSetupS { public abstract class InternalSetupSpecificationCodeFixProviderVerifier : CSharpSuppressDiagnosticSettingsVerifier, IInternalSetupSpecificationCodeFixProviderVerifier { + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new NonSubstitutableMemberAnalyzer(); + + protected override CodeFixProvider CodeFixProvider { get; } = new InternalSetupSpecificationCodeFixProvider(); + [CombinatoryTheory] [InlineData] public abstract Task ChangesInternalToPublic_ForIndexer_WhenUsedWithInternalMember(string method); @@ -40,15 +44,5 @@ public abstract class InternalSetupSpecificationCodeFixProviderVerifier : CSharp [InlineData(".FooBar()")] [InlineData("[0]")] public abstract Task AppendsInternalsVisibleTo_ToTopLevelCompilationUnit_WhenUsedWithInternalMember(string method, string call); - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new NonSubstitutableMemberAnalyzer(); - } - - protected override CodeFixProvider GetCodeFixProvider() - { - return new InternalSetupSpecificationCodeFixProvider(); - } } } \ 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 4b8bb856..88f160f0 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/ReceivedAsExtensionMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/ReceivedAsExtensionMethodTests.cs @@ -1,8 +1,6 @@ using System.Threading.Tasks; using Microsoft.CodeAnalysis.Diagnostics; using NSubstitute.Analyzers.CSharp.DiagnosticAnalyzers; -using NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.NonSubstitutableMemberReceivedAnalyzerTests; -using NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.NonSubstitutableMemberWhenAnalyzerTests; using NSubstitute.Analyzers.Tests.Shared.Extensibility; namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests.InternalSetupSpecificationCodeFixProviderTests @@ -10,6 +8,8 @@ namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests.InternalSetupS [CombinatoryData("Received")] public class ReceivedAsExtensionMethodTests : InternalSetupSpecificationCodeFixProviderVerifier { + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new NonSubstitutableMemberReceivedAnalyzer(); + public override async Task ChangesInternalToPublic_ForIndexer_WhenUsedWithInternalMember(string method) { var oldSource = $@"using NSubstitute; @@ -380,10 +380,5 @@ public void Test() }}"; await VerifyFix(oldSource, newSource, 2); } - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new NonSubstitutableMemberReceivedAnalyzer(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/ReceivedAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/ReceivedAsOrdinaryMethodTests.cs index 88f12926..5372de7a 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/ReceivedAsOrdinaryMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/ReceivedAsOrdinaryMethodTests.cs @@ -8,6 +8,8 @@ namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests.InternalSetupS [CombinatoryData("SubstituteExtensions.Received")] public class ReceivedAsOrdinaryMethodTests : InternalSetupSpecificationCodeFixProviderVerifier { + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new NonSubstitutableMemberReceivedAnalyzer(); + public override async Task ChangesInternalToPublic_ForIndexer_WhenUsedWithInternalMember(string method) { var oldSource = $@"using NSubstitute; @@ -378,10 +380,5 @@ public void Test() }}"; await VerifyFix(oldSource, newSource, 2); } - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new NonSubstitutableMemberReceivedAnalyzer(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsExtensionMethodTests.cs index b68863d1..804ebc0a 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsExtensionMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsExtensionMethodTests.cs @@ -1,7 +1,6 @@ using System.Threading.Tasks; using Microsoft.CodeAnalysis.Diagnostics; using NSubstitute.Analyzers.CSharp.DiagnosticAnalyzers; -using NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.NonSubstitutableMemberWhenAnalyzerTests; using NSubstitute.Analyzers.Tests.Shared.Extensibility; namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests.InternalSetupSpecificationCodeFixProviderTests @@ -9,6 +8,8 @@ namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests.InternalSetupS [CombinatoryData("When")] public class WhenAsExtensionMethodTests : InternalSetupSpecificationCodeFixProviderVerifier { + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new NonSubstitutableMemberWhenAnalyzer(); + public override async Task ChangesInternalToPublic_ForIndexer_WhenUsedWithInternalMember(string method) { var oldSource = $@"using NSubstitute; @@ -379,10 +380,5 @@ public void Test() }}"; await VerifyFix(oldSource, newSource, 2); } - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new NonSubstitutableMemberWhenAnalyzer(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsOrdinaryMethodTests.cs index 37552f45..d4c5f7b9 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsOrdinaryMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsOrdinaryMethodTests.cs @@ -8,6 +8,8 @@ namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests.InternalSetupS [CombinatoryData("SubstituteExtensions.When")] public class WhenAsOrdinaryMethodTests : InternalSetupSpecificationCodeFixProviderVerifier { + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new NonSubstitutableMemberWhenAnalyzer(); + public override async Task ChangesInternalToPublic_ForIndexer_WhenUsedWithInternalMember(string method) { var oldSource = $@"using NSubstitute; @@ -378,10 +380,5 @@ public void Test() }}"; await VerifyFix(oldSource, newSource, 2); } - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new NonSubstitutableMemberWhenAnalyzer(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixActionsTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixActionsTests.cs index 86541953..f02f3965 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixActionsTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixActionsTests.cs @@ -12,6 +12,10 @@ namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests.NonSubstitutab { public class NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixActionsTests : CSharpCodeFixActionsVerifier, INonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixActionsVerifier { + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new NonSubstitutableMemberArgumentMatcherAnalyzer(); + + protected override CodeFixProvider CodeFixProvider { get; } = new NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixProvider(); + [Fact] public async Task CreatesCorrectCodeFixActions_ForIndexer() { @@ -115,15 +119,5 @@ public void Test() }}"; await VerifyCodeActions(source, Array.Empty()); } - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new NonSubstitutableMemberArgumentMatcherAnalyzer(); - } - - protected override CodeFixProvider GetCodeFixProvider() - { - return new NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixProvider(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixTests.cs index 56b469fc..773dc8e4 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixTests.cs @@ -12,6 +12,10 @@ namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests.NonSubstitutab public class NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixTests : CSharpSuppressDiagnosticSettingsVerifier, INonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixVerifier { + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new NonSubstitutableMemberArgumentMatcherAnalyzer(); + + protected override CodeFixProvider CodeFixProvider { get; } = new NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixProvider(); + [Fact] public async Task SuppressesDiagnosticsInSettings_WhenArgMatcherUsedInNonVirtualMethod() { @@ -114,15 +118,5 @@ public void Test() await VerifySuppressionSettings(source, "N:MyNamespace", DiagnosticIdentifiers.NonSubstitutableMemberArgumentMatcherUsage, codeFixIndex: 2); } - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new NonSubstitutableMemberArgumentMatcherAnalyzer(); - } - - protected override CodeFixProvider GetCodeFixProvider() - { - return new NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixProvider(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/NonSubstitutableMemberSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberSuppressDiagnosticsCodeFixActionsTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/NonSubstitutableMemberSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberSuppressDiagnosticsCodeFixActionsTests.cs index bf1d527b..980036fa 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/NonSubstitutableMemberSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberSuppressDiagnosticsCodeFixActionsTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/NonSubstitutableMemberSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberSuppressDiagnosticsCodeFixActionsTests.cs @@ -11,6 +11,10 @@ namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests.NonSubstitutab { public class NonSubstitutableMemberSuppressDiagnosticsCodeFixActionsTests : CSharpCodeFixActionsVerifier, INonSubstitutableMemberSuppressDiagnosticsCodeFixActionsVerifier { + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new NonSubstitutableMemberAnalyzer(); + + protected override CodeFixProvider CodeFixProvider { get; } = new NonSubstitutableMemberSuppressDiagnosticsCodeFixProvider(); + [Fact] public async Task CreatesCorrectCodeFixActions_ForIndexer() { @@ -100,15 +104,5 @@ await VerifyCodeActions(source, new[] $"Suppress {DiagnosticIdentifiers.NonVirtualSetupSpecification} for namespace MyNamespace in nsubstitute.json" }); } - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new NonSubstitutableMemberAnalyzer(); - } - - protected override CodeFixProvider GetCodeFixProvider() - { - return new NonSubstitutableMemberSuppressDiagnosticsCodeFixProvider(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/NonSubstitutableMemberSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberSuppressDiagnosticsCodeFixVerifier.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/NonSubstitutableMemberSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberSuppressDiagnosticsCodeFixVerifier.cs index c2094b1a..c513269d 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/NonSubstitutableMemberSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberSuppressDiagnosticsCodeFixVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/NonSubstitutableMemberSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberSuppressDiagnosticsCodeFixVerifier.cs @@ -10,6 +10,10 @@ namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests.NonSubstitutab { public abstract class NonSubstitutableMemberSuppressDiagnosticsCodeFixVerifier : CSharpSuppressDiagnosticSettingsVerifier, INonSubstitutableMemberSuppressDiagnosticsCodeFixVerifier { + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new NonSubstitutableMemberAnalyzer(); + + protected override CodeFixProvider CodeFixProvider { get; } = new NonSubstitutableMemberSuppressDiagnosticsCodeFixProvider(); + [Fact] public abstract Task SuppressesDiagnosticsInSettings_WhenSettingValueForNonVirtualMethod(); @@ -33,15 +37,5 @@ public abstract class NonSubstitutableMemberSuppressDiagnosticsCodeFixVerifier : [Fact] public abstract Task SuppressesDiagnosticsInSettingsForNamespace_WhenSettingsValueForNonVirtualMember_AndSelectingNamespaceSuppression(); - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new NonSubstitutableMemberAnalyzer(); - } - - protected override CodeFixProvider GetCodeFixProvider() - { - return new NonSubstitutableMemberSuppressDiagnosticsCodeFixProvider(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/PartialSubstituteUsedForUnsupportedTypeCodeFixProviderTests/PartialSubstituteUsedForUnsupportedTypeCodeFixActionsTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/PartialSubstituteUsedForUnsupportedTypeCodeFixProviderTests/PartialSubstituteUsedForUnsupportedTypeCodeFixActionsTests.cs index c94442d6..87b1eb3c 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/PartialSubstituteUsedForUnsupportedTypeCodeFixProviderTests/PartialSubstituteUsedForUnsupportedTypeCodeFixActionsTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/PartialSubstituteUsedForUnsupportedTypeCodeFixProviderTests/PartialSubstituteUsedForUnsupportedTypeCodeFixActionsTests.cs @@ -10,6 +10,10 @@ namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests.PartialSubstit { public class PartialSubstituteUsedForUnsupportedTypeCodeFixActionsTests : CSharpCodeFixActionsVerifier, IPartialSubstituteUsedForUnsupportedTypeCodeFixActionsVerifier { + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new SubstituteAnalyzer(); + + protected override CodeFixProvider CodeFixProvider { get; } = new PartialSubstituteUsedForUnsupportedTypeCodeFixProvider(); + [Fact] public async Task CreatesCorrectCodeFixActions_ForSubstituteForPartsOf() { @@ -54,15 +58,5 @@ public void Test() }"; await VerifyCodeActions(source, "Use SubstituteFactory.Create"); } - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new SubstituteAnalyzer(); - } - - protected override CodeFixProvider GetCodeFixProvider() - { - return new PartialSubstituteUsedForUnsupportedTypeCodeFixProvider(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/PartialSubstituteUsedForUnsupportedTypeCodeFixProviderTests/PartialSubstituteUsedForUnsupportedTypeCodeFixProviderTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/PartialSubstituteUsedForUnsupportedTypeCodeFixProviderTests/PartialSubstituteUsedForUnsupportedTypeCodeFixProviderTests.cs index 63e6cbe6..c74a5271 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/PartialSubstituteUsedForUnsupportedTypeCodeFixProviderTests/PartialSubstituteUsedForUnsupportedTypeCodeFixProviderTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/PartialSubstituteUsedForUnsupportedTypeCodeFixProviderTests/PartialSubstituteUsedForUnsupportedTypeCodeFixProviderTests.cs @@ -10,6 +10,10 @@ namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests.PartialSubstit { public class PartialSubstituteUsedForUnsupportedTypeCodeFixProviderTests : CSharpCodeFixVerifier, IForPartsOfUsedForUnsupportedTypeCodeFixVerifier { + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new SubstituteAnalyzer(); + + protected override CodeFixProvider CodeFixProvider { get; } = new PartialSubstituteUsedForUnsupportedTypeCodeFixProvider(); + [Fact] public async Task ReplacesForPartsOf_WithFor_WhenUsedWithInterface() { @@ -153,15 +157,5 @@ public void Test() await VerifyFix(oldSource, newSource); } - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new SubstituteAnalyzer(); - } - - protected override CodeFixProvider GetCodeFixProvider() - { - return new PartialSubstituteUsedForUnsupportedTypeCodeFixProvider(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/ReEntrantSetupCodeFixProviderTests/ReEntrantSetupCodeFixActionsTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/ReEntrantSetupCodeFixProviderTests/ReEntrantSetupCodeFixActionsTests.cs index ab0d5773..101d2794 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/ReEntrantSetupCodeFixProviderTests/ReEntrantSetupCodeFixActionsTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/ReEntrantSetupCodeFixProviderTests/ReEntrantSetupCodeFixActionsTests.cs @@ -11,6 +11,10 @@ namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests.ReEntrantSetup { public class ReEntrantSetupCodeFixActionsTests : CSharpCodeFixActionsVerifier, IReEntrantSetupCodeFixActionsVerifier { + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new ReEntrantSetupAnalyzer(); + + protected override CodeFixProvider CodeFixProvider { get; } = new ReEntrantSetupCodeFixProvider(); + [Theory] [InlineData("await CreateReEntrantSubstituteAsync(), await CreateDefaultValue()")] [InlineData("CreateReEntrantSubstitute(), await CreateDefaultValue()")] @@ -92,15 +96,5 @@ private int CreateReEntrantSubstitute() }}"; await VerifyCodeActions(source, Array.Empty()); } - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new ReEntrantSetupAnalyzer(); - } - - protected override CodeFixProvider GetCodeFixProvider() - { - return new ReEntrantSetupCodeFixProvider(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/ReEntrantSetupCodeFixProviderTests/ReEntrantSetupCodeFixVerifier.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/ReEntrantSetupCodeFixProviderTests/ReEntrantSetupCodeFixVerifier.cs index 335a169d..b5f19c15 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/ReEntrantSetupCodeFixProviderTests/ReEntrantSetupCodeFixVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/ReEntrantSetupCodeFixProviderTests/ReEntrantSetupCodeFixVerifier.cs @@ -1,7 +1,5 @@ using System.Threading.Tasks; -using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeFixes; -using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Diagnostics; using NSubstitute.Analyzers.CSharp.CodeFixProviders; using NSubstitute.Analyzers.CSharp.DiagnosticAnalyzers; @@ -12,6 +10,15 @@ namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests.ReEntrantSetup { public abstract class ReEntrantSetupCodeFixVerifier : CSharpCodeFixVerifier, IReEntrantSetupCodeFixProviderVerifier { + protected ReEntrantSetupCodeFixVerifier() + : base(CSharpWorkspaceFactory.Default.WithWarningLevel(1)) + { + } + + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new ReEntrantSetupAnalyzer(); + + protected override CodeFixProvider CodeFixProvider { get; } = new ReEntrantSetupCodeFixProvider(); + [Theory] [InlineData("CreateReEntrantSubstitute(), CreateDefaultValue(), 1", "_ => CreateReEntrantSubstitute(), _ => CreateDefaultValue(), _ => 1")] [InlineData("CreateReEntrantSubstitute(), new [] { CreateDefaultValue(), 1 }", "_ => CreateReEntrantSubstitute(), new System.Func[] { _ => CreateDefaultValue(), _ => 1 }")] @@ -23,20 +30,5 @@ public abstract class ReEntrantSetupCodeFixVerifier : CSharpCodeFixVerifier, IRe [Fact] public abstract Task ReplacesArgumentExpression_WithLambdaWithReducedTypes_WhenGeneratingArrayParamsArgument(); - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new ReEntrantSetupAnalyzer(); - } - - protected override CodeFixProvider GetCodeFixProvider() - { - return new ReEntrantSetupCodeFixProvider(); - } - - protected override CompilationOptions GetCompilationOptions() - { - return new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, warningLevel: 1); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/SubstituteForInternalMemberCodeFixProviderTests/SubstituteForInternalMemberCodeFixActionsTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/SubstituteForInternalMemberCodeFixProviderTests/SubstituteForInternalMemberCodeFixActionsTests.cs index 5ae10237..cce2050c 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/SubstituteForInternalMemberCodeFixProviderTests/SubstituteForInternalMemberCodeFixActionsTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/SubstituteForInternalMemberCodeFixProviderTests/SubstituteForInternalMemberCodeFixActionsTests.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; @@ -16,6 +15,20 @@ namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests.SubstituteForI { public class SubstituteForInternalMemberCodeFixActionsTests : CSharpCodeFixActionsVerifier, ISubstituteForInternalMemberCodeFixActionsVerifier { + private static readonly MetadataReference[] AdditionalMetadataReferences = + { + GetInternalLibraryMetadataReference() + }; + + public SubstituteForInternalMemberCodeFixActionsTests() + : base(CSharpWorkspaceFactory.Default.WithAdditionalMetadataReferences(AdditionalMetadataReferences)) + { + } + + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new SubstituteAnalyzer(); + + protected override CodeFixProvider CodeFixProvider { get; } = new SubstituteForInternalMemberCodeFixProvider(); + [Fact] public async Task CreatesCorrectCodeFixActions_WhenSourceForInternalType_IsAvailable() { @@ -85,26 +98,11 @@ public void Test() await VerifyCodeActions(source); } - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new SubstituteAnalyzer(); - } - - protected override CodeFixProvider GetCodeFixProvider() - { - return new SubstituteForInternalMemberCodeFixProvider(); - } - - protected override IEnumerable GetAdditionalMetadataReferences() - { - return new[] { GetInternalLibraryMetadataReference() }; - } - - private static PortableExecutableReference GetInternalLibraryMetadataReference() + private static MetadataReference GetInternalLibraryMetadataReference() { var syntaxTree = CSharpSyntaxTree.ParseText($@" using System.Runtime.CompilerServices; -[assembly: InternalsVisibleTo(""{TestProjectName}"")] +[assembly: InternalsVisibleTo(""{Shared.WorkspaceFactory.DefaultProjectName}"")] namespace ExternalNamespace {{ internal class InternalFoo diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/SubstituteForInternalMemberCodeFixProviderTests/SubstituteForInternalMemberCodeFixVerifier.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/SubstituteForInternalMemberCodeFixProviderTests/SubstituteForInternalMemberCodeFixVerifier.cs index 21484ef7..0a22685e 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/SubstituteForInternalMemberCodeFixProviderTests/SubstituteForInternalMemberCodeFixVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeFixProviderTests/SubstituteForInternalMemberCodeFixProviderTests/SubstituteForInternalMemberCodeFixVerifier.cs @@ -4,12 +4,15 @@ using NSubstitute.Analyzers.CSharp.CodeFixProviders; using NSubstitute.Analyzers.CSharp.DiagnosticAnalyzers; using NSubstitute.Analyzers.Tests.Shared.CodeFixProviders; -using Xunit; namespace NSubstitute.Analyzers.Tests.CSharp.CodeFixProviderTests.SubstituteForInternalMemberCodeFixProviderTests { public abstract class SubstituteForInternalMemberCodeFixVerifier : CSharpCodeFixVerifier, ISubstituteForInternalMemberCodeFixVerifier { + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new SubstituteAnalyzer(); + + protected override CodeFixProvider CodeFixProvider { get; } = new SubstituteForInternalMemberCodeFixProvider(); + public abstract Task AppendsInternalsVisibleTo_ToTopLevelCompilationUnit_WhenUsedWithInternalClass(); public abstract Task AppendsInternalsVisibleTo_WhenUsedWithInternalClass(); @@ -21,15 +24,5 @@ public abstract class SubstituteForInternalMemberCodeFixVerifier : CSharpCodeFix public abstract Task DoesNot_AppendsInternalsVisibleTo_WhenUsedWithPublicClass(); public abstract Task DoesNot_AppendsInternalsVisibleTo_WhenInternalsVisibleToAppliedToDynamicProxyGenAssembly2(); - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new SubstituteAnalyzer(); - } - - protected override CodeFixProvider GetCodeFixProvider() - { - return new SubstituteForInternalMemberCodeFixProvider(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/AsyncReceivedInOrderCallbackAnalyzerTests/AsyncReceivedInOrderCallbackDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/AsyncReceivedInOrderCallbackAnalyzerTests/AsyncReceivedInOrderCallbackDiagnosticVerifier.cs index 7c7db66e..abff7774 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/AsyncReceivedInOrderCallbackAnalyzerTests/AsyncReceivedInOrderCallbackDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/AsyncReceivedInOrderCallbackAnalyzerTests/AsyncReceivedInOrderCallbackDiagnosticVerifier.cs @@ -13,6 +13,8 @@ public class AsyncReceivedInOrderCallbackDiagnosticVerifier : CSharpDiagnosticVe { private readonly DiagnosticDescriptor _descriptor = DiagnosticDescriptors.AsyncCallbackUsedInReceivedInOrder; + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new AsyncReceivedInOrderCallbackAnalyzer(); + [Fact] public async Task ReportsDiagnostic_WhenAsyncLambdaCallbackUsedInReceivedInOrder() { @@ -160,10 +162,5 @@ public void Test() await VerifyNoDiagnostic(source); } - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new AsyncReceivedInOrderCallbackAnalyzer(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CSharpDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CSharpDiagnosticVerifier.cs index 4e2909a9..877f1ce1 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CSharpDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CSharpDiagnosticVerifier.cs @@ -1,18 +1,12 @@ -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using NSubstitute.Analyzers.Tests.Shared.DiagnosticAnalyzers; +using NSubstitute.Analyzers.Tests.Shared.DiagnosticAnalyzers; namespace NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests { public abstract class CSharpDiagnosticVerifier : DiagnosticVerifier { - protected override string Language { get; } = LanguageNames.CSharp; - - protected override string FileExtension { get; } = "cs"; - - protected override CompilationOptions GetCompilationOptions() + protected CSharpDiagnosticVerifier() + : base(CSharpWorkspaceFactory.Default) { - return new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary); } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/CallInfoDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/CallInfoDiagnosticVerifier.cs index 2c39e93b..3f03a6b6 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/CallInfoDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/CallInfoDiagnosticVerifier.cs @@ -25,6 +25,8 @@ public abstract class CallInfoDiagnosticVerifier : CSharpDiagnosticVerifier, ICa protected DiagnosticDescriptor CallInfoArgumentSetWithIncompatibleValueDescriptor { get; } = DiagnosticDescriptors.CallInfoArgumentSetWithIncompatibleValue; + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new CallInfoAnalyzer(); + [CombinatoryTheory] [InlineData("substitute[Arg.Any()]", "callInfo.ArgAt(1);")] [InlineData("substitute[Arg.Any()]", "var x = callInfo[1];")] @@ -299,10 +301,5 @@ public abstract class CallInfoDiagnosticVerifier : CSharpDiagnosticVerifier, ICa [InlineData("IDictionary", "new Dictionary()")] [InlineData("IReadOnlyDictionary ", "new Dictionary()")] public abstract Task ReportsNoDiagnostic_WhenAssigningType_AssignableTo_Argument(string method, string left, string right); - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new CallInfoAnalyzer(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ConflictingArgumentAssignmentsAnalyzerTests/ConflictingArgumentAssignmentsDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ConflictingArgumentAssignmentsAnalyzerTests/ConflictingArgumentAssignmentsDiagnosticVerifier.cs index 59b2363a..f38b41a5 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ConflictingArgumentAssignmentsAnalyzerTests/ConflictingArgumentAssignmentsDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ConflictingArgumentAssignmentsAnalyzerTests/ConflictingArgumentAssignmentsDiagnosticVerifier.cs @@ -12,7 +12,9 @@ namespace NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.Conflicting { public abstract class ConflictingArgumentAssignmentsDiagnosticVerifier : CSharpDiagnosticVerifier, IConflictingArgumentAssignmentsDiagnosticVerifier { - public DiagnosticDescriptor Descriptor { get; } = DiagnosticDescriptors.ConflictingArgumentAssignments; + protected DiagnosticDescriptor Descriptor { get; } = DiagnosticDescriptors.ConflictingArgumentAssignments; + + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new ConflictingArgumentAssignmentsAnalyzer(); [CombinatoryTheory] [InlineData("substitute.Bar(Arg.Any())", "callInfo[1] = 1;", "[|callInfo[1]|] = 1;")] @@ -49,10 +51,5 @@ public abstract class ConflictingArgumentAssignmentsDiagnosticVerifier : CSharpD [InlineData("substitute.Barr", "callInfo[1] = 1;")] [InlineData("substitute[Arg.Any()]", "callInfo[1] = 1;")] public abstract Task ReportsNoDiagnostic_When_AndDoesMethod_SetArgument_AndPreviousMethod_IsNotUsedWithCallInfo(string method, string call, string andDoesArgAccess); - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new ConflictingArgumentAssignmentsAnalyzer(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/NonSubstitutableMemberDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/NonSubstitutableMemberDiagnosticVerifier.cs index 705c5f71..ff87af44 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/NonSubstitutableMemberDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/NonSubstitutableMemberDiagnosticVerifier.cs @@ -20,6 +20,10 @@ public abstract class NonSubstitutableMemberDiagnosticVerifier : CSharpDiagnosti protected DiagnosticDescriptor InternalSetupSpecificationDescriptor { get; } = DiagnosticDescriptors.InternalSetupSpecification; + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new NonSubstitutableMemberAnalyzer(); + + protected override string AnalyzerSettings => Settings != null ? Json.Encode(Settings) : null; + [CombinatoryTheory] [InlineData] public abstract Task ReportsDiagnostics_WhenSettingValueForNonVirtualMethod(string method); @@ -163,15 +167,5 @@ public abstract class NonSubstitutableMemberDiagnosticVerifier : CSharpDiagnosti [InlineData(".FooBar()")] [InlineData("[0]")] public abstract Task ReportsNoDiagnostics_WhenSettingValueForProtectedInternalVirtualMember(string method, string call); - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new NonSubstitutableMemberAnalyzer(); - } - - protected override string GetSettings() - { - return Settings != null ? Json.Encode(Settings) : null; - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ReturnsAsExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ReturnsAsExtensionMethodTests.cs index 5fd6fa6b..ffdbbcf7 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ReturnsAsExtensionMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ReturnsAsExtensionMethodTests.cs @@ -728,7 +728,7 @@ public void Test() "Member FooBar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -787,7 +787,7 @@ public void Test() "Member FooBar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -850,7 +850,7 @@ public void Test() "Member FooBar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ReturnsAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ReturnsAsOrdinaryMethodTests.cs index 096ad666..f6034d2c 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ReturnsAsOrdinaryMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ReturnsAsOrdinaryMethodTests.cs @@ -730,7 +730,7 @@ public void Test() "Member FooBar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -789,7 +789,7 @@ public void Test() "Member FooBar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -852,7 +852,7 @@ public void Test() "Member FooBar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ReturnsNullAsExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ReturnsNullAsExtensionMethodTests.cs index 0439d1d4..33f808e9 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ReturnsNullAsExtensionMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ReturnsNullAsExtensionMethodTests.cs @@ -738,7 +738,7 @@ public void Test() "Member FooBar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -798,7 +798,7 @@ public void Test() "Member FooBar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -862,7 +862,7 @@ public void Test() "Member FooBar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ReturnsNullAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ReturnsNullAsOrdinaryMethodTests.cs index 657eddab..28307177 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ReturnsNullAsOrdinaryMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ReturnsNullAsOrdinaryMethodTests.cs @@ -738,7 +738,7 @@ public void Test() "Member FooBar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -798,7 +798,7 @@ public void Test() "Member FooBar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -862,7 +862,7 @@ public void Test() "Member FooBar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsExtensionMethodTests.cs index f229b9ab..8dd192e9 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsExtensionMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsExtensionMethodTests.cs @@ -777,7 +777,7 @@ public void Test() "Member FooBar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -838,7 +838,7 @@ public void Test() "Member FooBar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -903,7 +903,7 @@ public void Test() "Member FooBar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsExtensionMethodWithGenericTypeSpecifiedTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsExtensionMethodWithGenericTypeSpecifiedTests.cs index 9eaf806e..c9cf8ae2 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsExtensionMethodWithGenericTypeSpecifiedTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsExtensionMethodWithGenericTypeSpecifiedTests.cs @@ -777,7 +777,7 @@ public void Test() "Member FooBar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -838,7 +838,7 @@ public void Test() "Member FooBar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -903,7 +903,7 @@ public void Test() "Member FooBar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodTests.cs index 5d21ff3b..fd9b7e38 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodTests.cs @@ -777,7 +777,7 @@ public void Test() "Member FooBar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -838,7 +838,7 @@ public void Test() "Member FooBar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -903,7 +903,7 @@ public void Test() "Member FooBar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodWithGenericTypeSpecifiedTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodWithGenericTypeSpecifiedTests.cs index 5149e4c7..10be4bed 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodWithGenericTypeSpecifiedTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodWithGenericTypeSpecifiedTests.cs @@ -777,7 +777,7 @@ public void Test() "Member FooBar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -838,7 +838,7 @@ public void Test() "Member FooBar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -903,7 +903,7 @@ public void Test() "Member FooBar can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberArgumentMatcherAnalyzerTests/NonSubstitutableMemberArgumentMatcherDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberArgumentMatcherAnalyzerTests/NonSubstitutableMemberArgumentMatcherDiagnosticVerifier.cs index 535aba33..5f4f992d 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberArgumentMatcherAnalyzerTests/NonSubstitutableMemberArgumentMatcherDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberArgumentMatcherAnalyzerTests/NonSubstitutableMemberArgumentMatcherDiagnosticVerifier.cs @@ -10,7 +10,7 @@ using NSubstitute.Analyzers.Tests.Shared.DiagnosticAnalyzers; using Xunit; -namespace NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.ArgumentMatcherAnalyzerTests +namespace NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.NonSubstitutableMemberArgumentMatcherAnalyzerTests { public abstract class NonSubstitutableMemberArgumentMatcherDiagnosticVerifier : CSharpDiagnosticVerifier, INonSubstitutableMemberArgumentMatcherDiagnosticVerifier { @@ -18,6 +18,10 @@ public abstract class NonSubstitutableMemberArgumentMatcherDiagnosticVerifier : protected DiagnosticDescriptor ArgumentMatcherUsedWithoutSpecifyingCall { get; } = DiagnosticDescriptors.NonSubstitutableMemberArgumentMatcherUsage; + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new NonSubstitutableMemberArgumentMatcherAnalyzer(); + + protected override string AnalyzerSettings => Settings != null ? Json.Encode(Settings) : null; + [Theory] [MemberData(nameof(MisusedArgTestCases))] public abstract Task ReportsDiagnostics_WhenUsedInNonVirtualMethod(string arg); @@ -102,11 +106,6 @@ public abstract class NonSubstitutableMemberArgumentMatcherDiagnosticVerifier : [MemberData(nameof(CorrectlyUsedArgTestCasesWithoutCasts))] public abstract Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressingNonVirtualMethod(string arg); - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new NonSubstitutableMemberArgumentMatcherAnalyzer(); - } - public static IEnumerable MisusedArgTestCases { get @@ -177,10 +176,5 @@ public static IEnumerable CorrectlyUsedArgTestCasesWithoutCasts yield return new object[] { "Arg.Compat.InvokeDelegate()" }; } } - - protected override string GetSettings() - { - return Settings != null ? Json.Encode(Settings) : null; - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberArgumentMatcherAnalyzerTests/NonSubstitutableMemberArgumentMatcherTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberArgumentMatcherAnalyzerTests/NonSubstitutableMemberArgumentMatcherTests.cs index 026efb23..90ae559d 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberArgumentMatcherAnalyzerTests/NonSubstitutableMemberArgumentMatcherTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberArgumentMatcherAnalyzerTests/NonSubstitutableMemberArgumentMatcherTests.cs @@ -2,7 +2,7 @@ using System.Threading.Tasks; using NSubstitute.Analyzers.Shared.Settings; -namespace NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.ArgumentMatcherAnalyzerTests +namespace NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.NonSubstitutableMemberArgumentMatcherAnalyzerTests { public class NonSubstitutableMemberArgumentMatcherTests : NonSubstitutableMemberArgumentMatcherDiagnosticVerifier { diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberReceivedAnalyzerTests/NonSubstitutableMemberReceivedDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberReceivedAnalyzerTests/NonSubstitutableMemberReceivedDiagnosticVerifier.cs index 65f9864d..6bae4d49 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberReceivedAnalyzerTests/NonSubstitutableMemberReceivedDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberReceivedAnalyzerTests/NonSubstitutableMemberReceivedDiagnosticVerifier.cs @@ -16,6 +16,8 @@ public abstract class NonSubstitutableMemberReceivedDiagnosticVerifier : CSharpD protected DiagnosticDescriptor InternalSetupSpecificationDescriptor { get; } = DiagnosticDescriptors.InternalSetupSpecification; + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new NonSubstitutableMemberReceivedAnalyzer(); + [CombinatoryTheory] [InlineData] public abstract Task ReportsDiagnostics_WhenCheckingReceivedCallsForNonVirtualMethod(string method); @@ -103,10 +105,5 @@ public abstract class NonSubstitutableMemberReceivedDiagnosticVerifier : CSharpD [CombinatoryTheory] [InlineData] public abstract Task ReportsNoDiagnostics_WhenUsingUnfortunatelyNamedMethod(string method); - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new NonSubstitutableMemberReceivedAnalyzer(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberWhenAnalyzerTests/NonSubstitutableMemberWhenDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberWhenAnalyzerTests/NonSubstitutableMemberWhenDiagnosticVerifier.cs index b956f84a..c87cd62d 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberWhenAnalyzerTests/NonSubstitutableMemberWhenDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberWhenAnalyzerTests/NonSubstitutableMemberWhenDiagnosticVerifier.cs @@ -16,6 +16,8 @@ public abstract class NonSubstitutableMemberWhenDiagnosticVerifier : CSharpDiagn protected DiagnosticDescriptor InternalSetupSpecificationDescriptor { get; } = DiagnosticDescriptors.InternalSetupSpecification; + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new NonSubstitutableMemberWhenAnalyzer(); + [CombinatoryTheory] [InlineData("sub => [|sub.Bar(Arg.Any())|]")] [InlineData("delegate(Foo sub) { [|sub.Bar(Arg.Any())|]; }")] @@ -165,10 +167,5 @@ public abstract class NonSubstitutableMemberWhenDiagnosticVerifier : CSharpDiagn [InlineData("sub => sub.FooBar(Arg.Any())")] [InlineData("sub => { var x = sub[Arg.Any()]; }")] public abstract Task ReportsNoDiagnostics_WhenSettingValueForProtectedInternalVirtualMember(string method, string call); - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new NonSubstitutableMemberWhenAnalyzer(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ReEntrantReturnsSetupAnalyzerTests/ReEntrantReturnsSetupDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ReEntrantReturnsSetupAnalyzerTests/ReEntrantReturnsSetupDiagnosticVerifier.cs index a7095a25..656eb960 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ReEntrantReturnsSetupAnalyzerTests/ReEntrantReturnsSetupDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ReEntrantReturnsSetupAnalyzerTests/ReEntrantReturnsSetupDiagnosticVerifier.cs @@ -15,6 +15,8 @@ public abstract class ReEntrantReturnsSetupDiagnosticVerifier : CSharpDiagnostic { protected DiagnosticDescriptor Descriptor { get; } = DiagnosticDescriptors.ReEntrantSubstituteCall; + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new ReEntrantSetupAnalyzer(); + [CombinatoryTheory] [InlineData("substitute.Foo().Returns(1);")] [InlineData("OtherReturn(); substitute.Foo().Returns(1);")] @@ -145,10 +147,5 @@ public abstract class ReEntrantReturnsSetupDiagnosticVerifier : CSharpDiagnostic [CombinatoryTheory] [InlineData] public abstract Task ReportsNoDiagnostic_WhenRootCallCalledWithDelegateInArrayParams_AndReEntrantReturnsForAnyArgsCallExists(string method); - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new ReEntrantSetupAnalyzer(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ReEntrantReturnsSetupAnalyzerTests/ReturnsAsExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ReEntrantReturnsSetupAnalyzerTests/ReturnsAsExtensionMethodTests.cs index bb44389d..ffcf5338 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ReEntrantReturnsSetupAnalyzerTests/ReturnsAsExtensionMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ReEntrantReturnsSetupAnalyzerTests/ReturnsAsExtensionMethodTests.cs @@ -50,7 +50,7 @@ private int OtherReturn() $"{plainMethodName}() is set with a method that itself calls Returns. This can cause problems with NSubstitute. Consider replacing with a lambda: {plainMethodName}(x => OtherReturn())." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -103,7 +103,7 @@ private int OtherReturn() $"{plainMethodName}() is set with a method that itself calls ReturnsForAnyArgs. This can cause problems with NSubstitute. Consider replacing with a lambda: {plainMethodName}(x => OtherReturn())." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -157,7 +157,7 @@ private int OtherReturn() $"{plainMethodName}() is set with a method that itself calls Do. This can cause problems with NSubstitute. Consider replacing with a lambda: {plainMethodName}(x => OtherReturn())." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -223,7 +223,7 @@ private int OtherNestedReturnThis() $"{plainMethodName}() is set with a method that itself calls {plainMethodName}. This can cause problems with NSubstitute. Consider replacing with a lambda: {plainMethodName}(x => NestedReturnThis())." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ReEntrantReturnsSetupAnalyzerTests/ReturnsAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ReEntrantReturnsSetupAnalyzerTests/ReturnsAsOrdinaryMethodTests.cs index f4a28fae..1316b290 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ReEntrantReturnsSetupAnalyzerTests/ReturnsAsOrdinaryMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ReEntrantReturnsSetupAnalyzerTests/ReturnsAsOrdinaryMethodTests.cs @@ -57,7 +57,7 @@ private int OtherReturn() $"{plainMethodName}() is set with a method that itself calls Returns. This can cause problems with NSubstitute. Consider replacing with a lambda: {plainMethodName}(x => OtherReturn())." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -110,7 +110,7 @@ private int OtherReturn() $"{plainMethodName}() is set with a method that itself calls ReturnsForAnyArgs. This can cause problems with NSubstitute. Consider replacing with a lambda: {plainMethodName}(x => OtherReturn())." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -162,7 +162,7 @@ private int OtherReturn() $"{plainMethodName}() is set with a method that itself calls Do. This can cause problems with NSubstitute. Consider replacing with a lambda: {plainMethodName}(x => OtherReturn())." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -227,7 +227,7 @@ private int OtherNestedReturnThis() $"{plainMethodName}() is set with a method that itself calls {plainMethodName}. This can cause problems with NSubstitute. Consider replacing with a lambda: {plainMethodName}(x => NestedReturnThis())." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ReceivedInReceivedInOrderAnalyzerTests/ReceivedInReceivedInOrderDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ReceivedInReceivedInOrderAnalyzerTests/ReceivedInReceivedInOrderDiagnosticVerifier.cs index e4a20471..fadf3177 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ReceivedInReceivedInOrderAnalyzerTests/ReceivedInReceivedInOrderDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/ReceivedInReceivedInOrderAnalyzerTests/ReceivedInReceivedInOrderDiagnosticVerifier.cs @@ -14,6 +14,8 @@ public abstract class ReceivedInReceivedInOrderDiagnosticVerifier : CSharpDiagno { protected DiagnosticDescriptor Descriptor { get; } = DiagnosticDescriptors.ReceivedUsedInReceivedInOrder; + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new ReceivedInReceivedInOrderAnalyzer(); + [CombinatoryTheory] [InlineData] public abstract Task ReportsDiagnostic_WhenUsingReceivedLikeMethodInReceivedInOrderBlock_ForMethod(string method); @@ -33,10 +35,5 @@ public abstract class ReceivedInReceivedInOrderDiagnosticVerifier : CSharpDiagno [CombinatoryTheory] [InlineData] public abstract Task ReportsNoDiagnostics_WhenUsingUnfortunatelyNamedMethod(string method); - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new ReceivedInReceivedInOrderAnalyzer(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/SubstituteAnalyzerTests/SubstituteDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/SubstituteAnalyzerTests/SubstituteDiagnosticVerifier.cs index 272a68e3..02fb7b3c 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/SubstituteAnalyzerTests/SubstituteDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/SubstituteAnalyzerTests/SubstituteDiagnosticVerifier.cs @@ -27,6 +27,8 @@ public abstract class SubstituteDiagnosticVerifier : CSharpDiagnosticVerifier, I protected DiagnosticDescriptor PartialSubstituteForUnsupportedTypeDescriptor { get; } = DiagnosticDescriptors.PartialSubstituteForUnsupportedType; + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new SubstituteAnalyzer(); + #pragma warning disable xUnit1013 // Public method should be marked as test [Fact] public abstract Task ReturnsDiagnostic_WhenUsedForClassWithoutPublicOrProtectedConstructor(); @@ -70,10 +72,5 @@ public abstract class SubstituteDiagnosticVerifier : CSharpDiagnosticVerifier, I [Fact] public abstract Task ReturnsNoDiagnostic_WhenUsedWithGenericArgument(); #pragma warning restore xUnit1013 // Public method should be marked as test - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new SubstituteAnalyzer(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/UnusedReceivedAnalyzerTests/UnusedReceivedDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/UnusedReceivedAnalyzerTests/UnusedReceivedDiagnosticVerifier.cs index 9c14415c..d0b41006 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/UnusedReceivedAnalyzerTests/UnusedReceivedDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/UnusedReceivedAnalyzerTests/UnusedReceivedDiagnosticVerifier.cs @@ -4,7 +4,6 @@ using NSubstitute.Analyzers.CSharp; using NSubstitute.Analyzers.CSharp.DiagnosticAnalyzers; using NSubstitute.Analyzers.Shared; -using NSubstitute.Analyzers.Tests.Shared; using NSubstitute.Analyzers.Tests.Shared.DiagnosticAnalyzers; using NSubstitute.Analyzers.Tests.Shared.Extensibility; using Xunit; @@ -15,6 +14,8 @@ public abstract class UnusedReceivedDiagnosticVerifier : CSharpDiagnosticVerifie { protected DiagnosticDescriptor Descriptor { get; } = DiagnosticDescriptors.UnusedReceived; + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new UnusedReceivedAnalyzer(); + [CombinatoryTheory] [InlineData] public abstract Task ReportDiagnostics_WhenUsedWithoutMemberCall(string method); @@ -38,10 +39,5 @@ public abstract class UnusedReceivedDiagnosticVerifier : CSharpDiagnosticVerifie [CombinatoryTheory] [InlineData] public abstract Task ReportsNoDiagnostics_WhenUsedWithUnfortunatelyNamedMethod(string method); - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new UnusedReceivedAnalyzer(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.Shared/CodeFixProviders/CodeFixCodeActionsVerifier.cs b/tests/NSubstitute.Analyzers.Tests.Shared/CodeFixProviders/CodeFixCodeActionsVerifier.cs index d4e6dd67..119da5d0 100644 --- a/tests/NSubstitute.Analyzers.Tests.Shared/CodeFixProviders/CodeFixCodeActionsVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.Shared/CodeFixProviders/CodeFixCodeActionsVerifier.cs @@ -4,43 +4,65 @@ using System.Threading; using System.Threading.Tasks; using FluentAssertions; +using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeActions; using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.Diagnostics; using NSubstitute.Analyzers.Shared.TinyJson; +using NSubstitute.Analyzers.Tests.Shared.Extensions; namespace NSubstitute.Analyzers.Tests.Shared.CodeFixProviders { - public abstract class CodeFixCodeActionsVerifier : CodeFixVerifier + public abstract class CodeFixCodeActionsVerifier : CodeVerifier { + protected CodeFixCodeActionsVerifier(WorkspaceFactory workspaceFactory) + : base(workspaceFactory) + { + } + + protected abstract CodeFixProvider CodeFixProvider { get; } + + protected abstract DiagnosticAnalyzer DiagnosticAnalyzer { get; } + + protected override string AnalyzerSettings { get; } = Json.Encode(new object()); + protected async Task VerifyCodeActions(string source, params string[] expectedCodeActionTitles) { - var codeActions = await ApplyFixProvider(GetDiagnosticAnalyzer(), GetCodeFixProvider(), source); + var codeActions = await RegisterCodeFixes(source); codeActions.Should().NotBeNull(); codeActions.Select(action => action.Title).Should().BeEquivalentTo(expectedCodeActionTitles ?? Array.Empty()); } - protected override string GetSettings() + private async Task> RegisterCodeFixes(string source) { - return Json.Encode(new object()); - } + using (var workspace = new AdhocWorkspace()) + { + var actions = new List(); + var project = AddProject(workspace.CurrentSolution, source); - private async Task> ApplyFixProvider(DiagnosticAnalyzer analyzer, CodeFixProvider codeFixProvider, string source) - { - var document = CreateDocument(source); - var analyzerDiagnostics = await GetSortedDiagnosticsFromDocuments(analyzer, new[] { document }, false); - var attempts = analyzerDiagnostics.Length; + var document = project.Documents.Single(); - var actions = new List(); + var compilation = await document.Project.GetCompilationAsync(); + var compilationDiagnostics = compilation.GetDiagnostics(); - for (var i = 0; i < attempts; ++i) - { - var context = new CodeFixContext(document, analyzerDiagnostics[0], (a, d) => actions.Add(a), CancellationToken.None); - await codeFixProvider.RegisterCodeFixesAsync(context); - } + VerifyNoCompilerDiagnosticErrors(compilationDiagnostics); - return actions; + var analyzerDiagnostics = await compilation.GetSortedAnalyzerDiagnostics( + DiagnosticAnalyzer, + project.AnalyzerOptions); + + foreach (var context in analyzerDiagnostics.Select(diagnostic => new CodeFixContext( + document, + analyzerDiagnostics[0], + (action, array) => actions.Add(action), + CancellationToken.None))) + { + await CodeFixProvider.RegisterCodeFixesAsync(context); + } + + return actions; + } } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.Shared/CodeFixProviders/CodeFixVerifier.cs b/tests/NSubstitute.Analyzers.Tests.Shared/CodeFixProviders/CodeFixVerifier.cs index 158453c3..6e716ef2 100644 --- a/tests/NSubstitute.Analyzers.Tests.Shared/CodeFixProviders/CodeFixVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.Shared/CodeFixProviders/CodeFixVerifier.cs @@ -1,186 +1,85 @@ +using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using FluentAssertions; +using FluentAssertions.Execution; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeActions; using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.Diagnostics; -using Microsoft.CodeAnalysis.Formatting; -using Microsoft.CodeAnalysis.Simplification; -using NSubstitute.Analyzers.Tests.Shared.DiagnosticAnalyzers; +using NSubstitute.Analyzers.Tests.Shared.Extensions; namespace NSubstitute.Analyzers.Tests.Shared.CodeFixProviders { - /// - /// Superclass of all Unit tests made for diagnostics with codefixes. - /// Contains methods used to verify correctness of codefixes - /// - public abstract class CodeFixVerifier : DiagnosticVerifier + public abstract class CodeFixVerifier : CodeVerifier { - /// - /// Called to test a VB codefix when applied on the inputted string as a source - /// - /// A class in the form of a string before the CodeFix was applied to it - /// A class in the form of a string after the CodeFix was applied to it - /// Index determining which codefix to apply if there are multiple - /// A bool controlling whether or not the test will fail if the CodeFix introduces other warnings after being applied - protected async Task VerifyFix(string oldSource, string newSource, int? codeFixIndex = null, bool allowNewCompilerDiagnostics = false) + protected CodeFixVerifier(WorkspaceFactory workspaceFactory) + : base(workspaceFactory) { - await VerifyFix(GetDiagnosticAnalyzer(), GetCodeFixProvider(), oldSource, newSource, codeFixIndex, allowNewCompilerDiagnostics); } - /// - /// Returns the codefix being tested - to be implemented in non-abstract class - /// - /// The CodeFixProvider to be used for code - protected abstract CodeFixProvider GetCodeFixProvider(); + protected abstract CodeFixProvider CodeFixProvider { get; } - protected Document CreateDocument(string source) - { - return CreateProject(new[] { source }).Documents.First(); - } + protected abstract DiagnosticAnalyzer DiagnosticAnalyzer { get; } - /// - /// Apply the inputted CodeAction to the inputted document. - /// Meant to be used to apply codefixes. - /// - /// The Document to apply the fix on - /// A CodeAction that will be applied to the Document. - /// A Document with the changes from the CodeAction - protected static async Task ApplyFix(Document document, CodeAction codeAction) + protected async Task VerifyFix(string oldSource, string newSource, int? codeFixIndex = null) { - var operations = await codeAction.GetOperationsAsync(CancellationToken.None); - var solution = operations.OfType().Single().ChangedSolution; - return solution.GetDocument(document.Id); - } - - /// - /// General verifier for codefixes. - /// Creates a Document from the source string, then gets diagnostics on it and applies the relevant codefixes. - /// Then gets the string after the codefix is applied and compares it with the expected result. - /// Note: If any codefix causes new diagnostics to show up, the test fails unless allowNewCompilerDiagnostics is set to true. - /// - /// The analyzer to be applied to the source code - /// The codefix to be applied to the code wherever the relevant Diagnostic is found - /// A class in the form of a string before the CodeFix was applied to it - /// A class in the form of a string after the CodeFix was applied to it - /// Index determining which codefix to apply if there are multiple - /// A bool controlling whether or not the test will fail if the CodeFix introduces other warnings after being applied - private async Task VerifyFix(DiagnosticAnalyzer analyzer, CodeFixProvider codeFixProvider, string oldSource, string newSource, int? codeFixIndex, bool allowNewCompilerDiagnostics) - { - var document = CreateDocument(oldSource); - var analyzerDiagnostics = await GetSortedDiagnosticsFromDocuments(analyzer, new[] { document }, false); - var compilerDiagnostics = await GetCompilerDiagnostics(document); - var attempts = analyzerDiagnostics.Length; - - for (var i = 0; i < attempts; ++i) + using (var workspace = new AdhocWorkspace()) { - var actions = new List(); - var context = new CodeFixContext(document, analyzerDiagnostics[0], (a, d) => actions.Add(a), CancellationToken.None); - await codeFixProvider.RegisterCodeFixesAsync(context); + var project = AddProject(workspace.CurrentSolution, oldSource); + var document = project.Documents.Single(); + var compilation = await project.GetCompilationAsync(); - if (!actions.Any()) - { - break; - } + var compilerDiagnostics = compilation.GetDiagnostics(); - if (codeFixIndex != null) - { - document = await ApplyFix(document, actions.ElementAt((int)codeFixIndex)); - break; - } + VerifyNoCompilerDiagnosticErrors(compilerDiagnostics); - document = await ApplyFix(document, actions.ElementAt(0)); - analyzerDiagnostics = await GetSortedDiagnosticsFromDocuments(analyzer, new[] { document }, false); + var analyzerDiagnostics = await compilation.GetSortedAnalyzerDiagnostics( + DiagnosticAnalyzer, + project.AnalyzerOptions); - var newCompilerDiagnostics = GetNewDiagnostics(compilerDiagnostics, await GetCompilerDiagnostics(document)); + var previousAnalyzerDiagnostics = analyzerDiagnostics; + var attempts = analyzerDiagnostics.Length; - // check if applying the code fix introduced any new compiler diagnostics - if (!allowNewCompilerDiagnostics && newCompilerDiagnostics.Any()) + for (var i = 0; i < attempts; ++i) { - // Format and get the compiler diagnostics again so that the locations make sense in the output - document = document.WithSyntaxRoot(Formatter.Format(await document.GetSyntaxRootAsync(), Formatter.Annotation, document.Project.Solution.Workspace)); - newCompilerDiagnostics = GetNewDiagnostics(compilerDiagnostics, await GetCompilerDiagnostics(document)); + var actions = new List(); + var context = new CodeFixContext(document, analyzerDiagnostics[0], (a, d) => actions.Add(a), CancellationToken.None); + await CodeFixProvider.RegisterCodeFixesAsync(context); - var diagnosticsString = string.Join("\r\n", newCompilerDiagnostics.Select(d => d.ToString())); - var newDocumentString = (await document.GetSyntaxRootAsync()).ToFullString(); - var message = - $"Fix introduced new compiler diagnostics:\r\n{diagnosticsString}\r\n\r\nNew document:\r\n{newDocumentString}\r\n"; + if (!actions.Any()) + { + break; + } - message.Should().BeEmpty(); - } + document = await document.ApplyCodeAction(actions[codeFixIndex ?? 0]); + compilation = await document.Project.GetCompilationAsync(); - // check if there are analyzer diagnostics left after the code fix - if (analyzerDiagnostics.Any()) - { - var diagnosticsString = string.Join("\r\n", analyzerDiagnostics.Select(d => d.ToString())); - var newDocumentString = (await document.GetSyntaxRootAsync()).ToFullString(); - var message = - $"Fix didn't fix compiler diagnostics:\r\n{diagnosticsString}\r\n\r\nNew document:\r\n{newDocumentString}\r\n"; - - message.Should().BeEmpty(); - } - } + compilerDiagnostics = compilation.GetDiagnostics(); - // after applying all of the code fixes, compare the resulting string to the inputted one - var actual = await GetStringFromDocument(document); - actual.Should().Be(newSource); - } + VerifyNoCompilerDiagnosticErrors(compilerDiagnostics); - /// - /// Compare two collections of Diagnostics,and return a list of any new diagnostics that appear only in the second collection. - /// Note: Considers Diagnostics to be the same if they have the same Ids. In the case of multiple diagnostics with the same Id in a row, - /// this method may not necessarily return the new one. - /// - /// The Diagnostics that existed in the code before the CodeFix was applied - /// The Diagnostics that exist in the code after the CodeFix was applied - /// A list of Diagnostics that only surfaced in the code after the CodeFix was applied - private static IEnumerable GetNewDiagnostics(IEnumerable diagnostics, IEnumerable newDiagnostics) - { - var oldArray = diagnostics.OrderBy(d => d.Location.SourceSpan.Start).ToArray(); - var newArray = newDiagnostics.OrderBy(d => d.Location.SourceSpan.Start).ToArray(); + analyzerDiagnostics = await compilation.GetSortedAnalyzerDiagnostics( + DiagnosticAnalyzer, + project.AnalyzerOptions); - var oldIndex = 0; - var newIndex = 0; + // check if there are analyzer diagnostics left after the code fix + var newAnalyzerDiagnostics = analyzerDiagnostics.Except(previousAnalyzerDiagnostics).ToList(); + if (analyzerDiagnostics.Length == previousAnalyzerDiagnostics.Length && newAnalyzerDiagnostics.Any()) + { + Execute.Assertion.Fail( + $"Fix didn't fix analyzer diagnostics: {newAnalyzerDiagnostics.ToDebugString()} New document:{Environment.NewLine}{await document.ToFullString()}"); + } - while (newIndex < newArray.Length) - { - if (oldIndex < oldArray.Length && oldArray[oldIndex].Id == newArray[newIndex].Id) - { - ++oldIndex; - ++newIndex; - } - else - { - yield return newArray[newIndex++]; + previousAnalyzerDiagnostics = analyzerDiagnostics; } - } - } - /// - /// Get the existing compiler diagnostics on the inputted document. - /// - /// The Document to run the compiler diagnostic analyzers on - /// The compiler diagnostics that were found in the code - private static async Task> GetCompilerDiagnostics(Document document) - { - var semanticModel = await document.GetSemanticModelAsync(); - return semanticModel.GetDiagnostics(); - } + var actual = await document.ToFullString(); - /// - /// Given a document, turn it into a string based on the syntax root. - /// - /// The Document to be converted to a string. - /// A string containing the syntax of the Document after formatting. - private static async Task GetStringFromDocument(Document document) - { - var simplifiedDoc = await Simplifier.ReduceAsync(document, Simplifier.Annotation); - var root = await simplifiedDoc.GetSyntaxRootAsync(); - - return root.GetText().ToString(); + actual.Should().Be(newSource); + } } } } diff --git a/tests/NSubstitute.Analyzers.Tests.Shared/CodeFixProviders/SuppressDiagnosticSettingsVerifier.cs b/tests/NSubstitute.Analyzers.Tests.Shared/CodeFixProviders/SuppressDiagnosticSettingsVerifier.cs index 73415755..1f9953c2 100644 --- a/tests/NSubstitute.Analyzers.Tests.Shared/CodeFixProviders/SuppressDiagnosticSettingsVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.Shared/CodeFixProviders/SuppressDiagnosticSettingsVerifier.cs @@ -7,28 +7,30 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeActions; using Microsoft.CodeAnalysis.CodeFixes; -using Microsoft.CodeAnalysis.Diagnostics; using NSubstitute.Analyzers.Shared.Settings; using NSubstitute.Analyzers.Shared.TinyJson; +using NSubstitute.Analyzers.Tests.Shared.Extensions; namespace NSubstitute.Analyzers.Tests.Shared.CodeFixProviders { public abstract class SuppressDiagnosticSettingsVerifier : CodeFixVerifier { + protected SuppressDiagnosticSettingsVerifier(WorkspaceFactory workspaceFactory) + : base(workspaceFactory) + { + } + + protected override string AnalyzerSettings { get; } = Json.Encode(new object()); + protected async Task VerifySuppressionSettings(string source, string target, string diagnosticRuleId, int codeFixIndex = 0) { - var originalSettings = Json.Decode(GetSettings()); + var originalSettings = Json.Decode(AnalyzerSettings); var document = await ApplySettingsSuppressionFix(source, codeFixIndex); await VerifySuppressionSettings(document, originalSettings, target, diagnosticRuleId); } - protected override string GetSettings() - { - return Json.Encode(new object()); - } - private async Task VerifySuppressionSettings(Document document, AnalyzersSettings originalSettings, string target, string diagnosticRuleId) { var textDocument = document.Project @@ -45,38 +47,34 @@ private async Task VerifySuppressionSettings(Document document, AnalyzersSetting updatedSettings.Should().BeEquivalentTo(expectedSettings); } - private async Task ApplySettingsSuppressionFix(string oldSource, int? codeFixIndex = null) + private async Task ApplySettingsSuppressionFix(string source, int? codeFixIndex = null) { - return await ApplySettingsSuppressionFix(GetDiagnosticAnalyzer(), GetCodeFixProvider(), oldSource, codeFixIndex); - } + using (var workspace = new AdhocWorkspace()) + { + var project = AddProject(workspace.CurrentSolution, source); - private async Task ApplySettingsSuppressionFix(DiagnosticAnalyzer analyzer, CodeFixProvider codeFixProvider, string oldSource, int? codeFixIndex = null) - { - var document = CreateDocument(oldSource); - var analyzerDiagnostics = await GetSortedDiagnosticsFromDocuments(analyzer, new[] { document }, false); - var attempts = analyzerDiagnostics.Length; + var document = project.Documents.Single(); + var compilation = await document.Project.GetCompilationAsync(); + var compilerDiagnostics = compilation.GetDiagnostics(); - for (var i = 0; i < attempts; ++i) - { - var actions = new List(); - var context = new CodeFixContext(document, analyzerDiagnostics[0], (a, d) => actions.Add(a), CancellationToken.None); - await codeFixProvider.RegisterCodeFixesAsync(context); + VerifyNoCompilerDiagnosticErrors(compilerDiagnostics); - if (!actions.Any()) - { - break; - } + var analyzerDiagnostics = await compilation.GetSortedAnalyzerDiagnostics( + DiagnosticAnalyzer, + project.AnalyzerOptions); - if (codeFixIndex != null) - { - document = await ApplyFix(document, actions.ElementAt((int)codeFixIndex)); - break; - } + var actions = new List(); + var context = new CodeFixContext( + document, + analyzerDiagnostics.Single(), + (a, d) => actions.Add(a), + CancellationToken.None); - document = await ApplyFix(document, actions.ElementAt(0)); - } + await CodeFixProvider.RegisterCodeFixesAsync(context); + var action = actions[codeFixIndex ?? 0]; - return document; + return await document.ApplyCodeAction(action); + } } private static AnalyzersSettings GetExpectedSettings(AnalyzersSettings originalSettings, string target, string diagnosticRuleId) diff --git a/tests/NSubstitute.Analyzers.Tests.Shared/CodeVerifier.cs b/tests/NSubstitute.Analyzers.Tests.Shared/CodeVerifier.cs new file mode 100644 index 00000000..def02658 --- /dev/null +++ b/tests/NSubstitute.Analyzers.Tests.Shared/CodeVerifier.cs @@ -0,0 +1,49 @@ +using System.Collections.Immutable; +using System.Globalization; +using System.Linq; +using System.Threading; +using FluentAssertions.Execution; +using Microsoft.CodeAnalysis; +using NSubstitute.Analyzers.Tests.Shared.Extensions; +using NSubstitute.Analyzers.Tests.Shared.Text; + +namespace NSubstitute.Analyzers.Tests.Shared +{ + public abstract class CodeVerifier + { + protected WorkspaceFactory WorkspaceFactory { get; } + + protected virtual string AnalyzerSettings { get; } + + protected CodeVerifier(WorkspaceFactory workspaceFactory) + { + WorkspaceFactory = workspaceFactory; + Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = + CultureInfo.GetCultureInfoByIetfLanguageTag("en-US"); + } + + protected static TextParser TextParser { get; } = TextParser.Default; + + protected Project AddProject(Solution solution, string source) + { + return WorkspaceFactory.AddProject(solution, source, AnalyzerSettings); + } + + protected Project AddProject(Solution solution, string[] sources) + { + return WorkspaceFactory.AddProject(solution, sources, AnalyzerSettings); + } + + protected static void VerifyNoCompilerDiagnosticErrors(ImmutableArray diagnostics) + { + var compilationErrorDiagnostics = diagnostics.Where(diagnostic => + diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error) + .ToList(); + + if (compilationErrorDiagnostics.Any()) + { + Execute.Assertion.Fail($"Compilation failed. Errors encountered {compilationErrorDiagnostics.ToDebugString()}"); + } + } + } +} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.Shared/DiagnosticAnalyzers/DiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.Shared/DiagnosticAnalyzers/DiagnosticVerifier.cs index 7b94e245..487de6ce 100644 --- a/tests/NSubstitute.Analyzers.Tests.Shared/DiagnosticAnalyzers/DiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.Shared/DiagnosticAnalyzers/DiagnosticVerifier.cs @@ -1,87 +1,52 @@ using System; using System.Collections.Generic; using System.Collections.Immutable; -using System.Globalization; using System.Linq; -using System.Linq.Expressions; -using System.Reflection; -using System.Text; -using System.Threading; using System.Threading.Tasks; using FluentAssertions; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Text; -using NSubstitute.Analyzers.Shared.Settings; using NSubstitute.Analyzers.Tests.Shared.Extensions; using NSubstitute.Analyzers.Tests.Shared.Text; namespace NSubstitute.Analyzers.Tests.Shared.DiagnosticAnalyzers { - /// - /// Class for turning strings into documents and getting the diagnostics on them. - /// All methods are static. - /// - public abstract class DiagnosticVerifier + public abstract class DiagnosticVerifier : CodeVerifier { - private static readonly MetadataReference CorlibReference = MetadataReference.CreateFromFile(typeof(object).Assembly.Location); + protected abstract DiagnosticAnalyzer DiagnosticAnalyzer { get; } - private static readonly MetadataReference SystemCoreReference = MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location); - - private static readonly MetadataReference CodeAnalysisReference = MetadataReference.CreateFromFile(typeof(Compilation).Assembly.Location); - - private static readonly MetadataReference NSubstituteReference = MetadataReference.CreateFromFile(typeof(Substitute).Assembly.Location); - - private static readonly MetadataReference TasksExtensionsReference = MetadataReference.CreateFromFile(Assembly.Load("System.Threading.Tasks.Extensions").Location); - - private static readonly MetadataReference NetStandard = MetadataReference.CreateFromFile(Assembly.Load("netstandard, Version=2.0.0.0").Location); - - private static readonly MetadataReference LinqExpressionReference = MetadataReference.CreateFromFile(typeof(Expression).Assembly.Location); - - private static readonly MetadataReference[] MetadataReferences; - - public static string DefaultFilePathPrefix { get; } = "Test"; - - public static string TestProjectName { get; } = "TestProject"; - - public static TextParser TextParser { get; } = TextParser.Default; - - protected abstract string Language { get; } - - protected abstract string FileExtension { get; } - - static DiagnosticVerifier() - { - var referencedAssemblies = typeof(DiagnosticVerifier).Assembly.GetReferencedAssemblies(); - var systemRuntimeReference = GetAssemblyReference(referencedAssemblies, "System.Runtime"); - var systemThreadingTasksReference = GetAssemblyReference(referencedAssemblies, "System.Threading.Tasks"); - MetadataReferences = new[] - { - CorlibReference, SystemCoreReference, CodeAnalysisReference, NSubstituteReference, NetStandard, - TasksExtensionsReference, systemRuntimeReference, LinqExpressionReference, systemThreadingTasksReference - }; - } - - protected DiagnosticVerifier() + protected DiagnosticVerifier(WorkspaceFactory workspaceFactory) + : base(workspaceFactory) { - Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = - CultureInfo.GetCultureInfoByIetfLanguageTag("en-US"); } - protected async Task VerifyDiagnostic(string source, DiagnosticDescriptor diagnosticDescriptor, string overridenDiagnosticMessage = null) + protected async Task VerifyDiagnostic( + string source, + DiagnosticDescriptor diagnosticDescriptor, + string overridenDiagnosticMessage = null) { - await VerifyDiagnostics(new[] { source }, diagnosticDescriptor, overridenDiagnosticMessage); + await VerifyDiagnostics( + sources: new[] { source }, + diagnosticDescriptor: diagnosticDescriptor, + overridenDiagnosticMessage: overridenDiagnosticMessage); } - protected async Task VerifyDiagnostics(string[] sources, DiagnosticDescriptor diagnosticDescriptor, string overridenDiagnosticMessage = null) + protected async Task VerifyDiagnostics( + string[] sources, + DiagnosticDescriptor diagnosticDescriptor, + string overridenDiagnosticMessage = null) { diagnosticDescriptor = overridenDiagnosticMessage == null ? diagnosticDescriptor : diagnosticDescriptor.OverrideMessage(overridenDiagnosticMessage); var textParserResult = sources.Select(source => TextParser.GetSpans(source)).ToList(); - var diagnostics = textParserResult.SelectMany(result => result.Spans.Select(span => CreateDiagnostic(diagnosticDescriptor, span.Span, span.LineSpan))).ToArray(); - await VerifyDiagnostics(textParserResult.Select(result => result.Text).ToArray(), diagnostics); + var diagnostics = textParserResult.SelectMany(result => CreateDiagnostics(diagnosticDescriptor, result)).ToArray(); + + await VerifyDiagnostics( + sources: textParserResult.Select(result => result.Text).ToArray(), + expectedDiagnostics: diagnostics); } protected async Task VerifyDiagnostic(string source, Diagnostic[] diagnostics) @@ -89,14 +54,14 @@ protected async Task VerifyDiagnostic(string source, Diagnostic[] diagnostics) await VerifyDiagnostics(new[] { source }, diagnostics); } - protected async Task VerifyDiagnostics(string[] sources, Diagnostic[] diagnostics) + protected async Task VerifyDiagnostics(string[] sources, Diagnostic[] expectedDiagnostics) { - if (diagnostics == null || diagnostics.Length == 0) + if (expectedDiagnostics == null || expectedDiagnostics.Length == 0) { - throw new ArgumentException("Diagnostics should not be empty", nameof(diagnostics)); + throw new ArgumentException("Diagnostics should not be empty", nameof(expectedDiagnostics)); } - await VerifyDiagnostics(sources, GetDiagnosticAnalyzer(), diagnostics, false); + await VerifyDiagnosticsInternal(sources, expectedDiagnostics); } protected async Task VerifyNoDiagnostic(string source) @@ -106,119 +71,46 @@ protected async Task VerifyNoDiagnostic(string source) protected async Task VerifyNoDiagnostics(string[] sources) { - await VerifyDiagnostics(sources, GetDiagnosticAnalyzer(), Array.Empty(), false); + await VerifyDiagnosticsInternal(sources, Array.Empty()); } - protected abstract DiagnosticAnalyzer GetDiagnosticAnalyzer(); - - protected abstract CompilationOptions GetCompilationOptions(); - - protected virtual IEnumerable GetAdditionalMetadataReferences() + protected IEnumerable CreateDiagnostics(DiagnosticDescriptor descriptor, TextParserResult result) { - return Enumerable.Empty(); + return result.Spans.Select(spanInfo => CreateDiagnostic(descriptor, spanInfo)); } - protected virtual string GetSettings() + protected Diagnostic CreateDiagnostic(DiagnosticDescriptor descriptor, LinePositionSpanInfo linePositionSpanInfo) { - return null; + var location = Location.Create( + $"{WorkspaceFactory.DefaultDocumentName}0.{WorkspaceFactory.DocumentExtension}", + linePositionSpanInfo.Span, + linePositionSpanInfo.LineSpan); + + return Diagnostic.Create(descriptor, location); } - protected async Task GetSortedDiagnosticsFromDocuments(DiagnosticAnalyzer analyzer, Document[] documents, bool allowCompilationErrors) + private async Task VerifyDiagnosticsInternal(string[] sources, Diagnostic[] expectedDiagnostics) { - if (documents == null) + using (var workspace = new AdhocWorkspace()) { - throw new ArgumentNullException(nameof(documents)); - } - - var projects = new HashSet(); - foreach (var document in documents) - { - projects.Add(document.Project); - } - - var diagnostics = new List(); + var project = AddProject( + workspace.CurrentSolution, + sources); - foreach (var project in projects) - { var compilation = await project.GetCompilationAsync(); - var compilationWithAnalyzers = compilation.WithAnalyzers(ImmutableArray.Create(analyzer), project.AnalyzerOptions); - if (!allowCompilationErrors) - { - AssertThatCompilationSucceeded(compilationWithAnalyzers); - } + var compilerDiagnostics = compilation.GetDiagnostics(); + VerifyNoCompilerDiagnosticErrors(compilerDiagnostics); - var diags = await compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync(); + var diagnostics = await compilation.GetSortedAnalyzerDiagnostics(DiagnosticAnalyzer, project.AnalyzerOptions); - foreach (var diag in diags) - { - if (diag.Location == Location.None || diag.Location.IsInMetadata) - { - diagnostics.Add(diag); - } - else - { - foreach (var document in documents) - { - var tree = await document.GetSyntaxTreeAsync(); - if (tree == diag.Location.SourceTree) - { - diagnostics.Add(diag); - } - } - } - } + VerifyDiagnosticResults(diagnostics, ImmutableArray.Create(expectedDiagnostics)); } - - var results = SortDiagnostics(diagnostics); - diagnostics.Clear(); - return results; } - protected Project CreateProject(string[] sources) + private static void VerifyDiagnosticResults(ImmutableArray actualResults, ImmutableArray expectedResults) { - var fileNamePrefix = DefaultFilePathPrefix; - var projectId = ProjectId.CreateNewId(TestProjectName); - - using (var adhocWorkspace = new AdhocWorkspace()) - { - var compilationOptions = GetCompilationOptions(); - - var solution = adhocWorkspace - .CurrentSolution - .AddProject(projectId, TestProjectName, TestProjectName, Language) - .WithProjectCompilationOptions(projectId, compilationOptions) - .AddMetadataReferences(projectId, MetadataReferences.Concat(GetAdditionalMetadataReferences())); - - for (var index = 0; index < sources.Length; index++) - { - var source = sources[index]; - var newFileName = fileNamePrefix + index + "." + FileExtension; - var documentId = DocumentId.CreateNewId(projectId, debugName: newFileName); - solution = solution.AddDocument(documentId, newFileName, SourceText.From(source)); - } - - var settings = GetSettings(); - if (!string.IsNullOrEmpty(settings)) - { - var documentId = DocumentId.CreateNewId(projectId); - solution = solution.AddAdditionalDocument(documentId, AnalyzersSettings.AnalyzerFileName, settings); - } - - return solution.GetProject(projectId); - } - } - - protected Diagnostic CreateDiagnostic(DiagnosticDescriptor descriptor, TextSpan span, LinePositionSpan lineSpan) - { - var location = Location.Create($"{DefaultFilePathPrefix}0.{FileExtension}", span, lineSpan); - - return Diagnostic.Create(descriptor, location); - } - - private static void VerifyDiagnosticResults(Diagnostic[] actualResults, DiagnosticAnalyzer analyzer, params Diagnostic[] expectedResults) - { - actualResults.Should().HaveSameCount(expectedResults, "because diagnostic count should match. {0}", FormatDiagnostics(actualResults)); + actualResults.Should().HaveSameCount(expectedResults, "because diagnostic count should match. {0}", actualResults.ToDebugString()); for (var i = 0; i < expectedResults.Length; i++) { var actual = actualResults.ElementAt(i); @@ -266,64 +158,5 @@ private static void VerifyLinePosition( actual.Character.Should().Be(expected.Character, $"because diagnostic should ${startOrEnd} at expected column"); } - - private static string FormatDiagnostics(params Diagnostic[] diagnostics) - { - var formattedName = diagnostics.Length == 0 ? "no diagnostic" : string.Join(Environment.NewLine, diagnostics.Select(d => d.ToString())); - - return $"{Environment.NewLine}Diagnostics:{Environment.NewLine}{formattedName}"; - } - - private async Task VerifyDiagnostics(string[] sources, DiagnosticAnalyzer analyzer, Diagnostic[] expected, bool allowCompilationErrors) - { - var diagnostics = await GetSortedDiagnostics(sources, analyzer, allowCompilationErrors); - VerifyDiagnosticResults(diagnostics, analyzer, expected); - } - - private Document[] GetDocuments(string[] sources) - { - var project = CreateProject(sources); - var documents = project.Documents.ToArray(); - - if (sources.Length != documents.Length) - { - throw new InvalidOperationException("Amount of sources did not match amount of Documents created"); - } - - return documents; - } - - private static void AssertThatCompilationSucceeded(CompilationWithAnalyzers compilationWithAnalyzers) - { - var compilationDiagnostics = compilationWithAnalyzers.Compilation.GetDiagnostics(); - - if (compilationDiagnostics.Any(diagnostic => diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error)) - { - var messageBuilder = new StringBuilder(); - messageBuilder.Append("Test code compilation failed. Error(s) encountered:"); - foreach (var diagnostic in compilationDiagnostics) - { - messageBuilder.AppendLine(); - messageBuilder.AppendFormat(" {0}", diagnostic); - } - - throw new ArgumentException(messageBuilder.ToString()); - } - } - - private async Task GetSortedDiagnostics(string[] sources, DiagnosticAnalyzer analyzer, bool allowCompilationErrors) - { - return await GetSortedDiagnosticsFromDocuments(analyzer, GetDocuments(sources), allowCompilationErrors); - } - - private Diagnostic[] SortDiagnostics(IEnumerable diagnostics) - { - return diagnostics.OrderBy(d => d.Location.SourceSpan.Start).ToArray(); - } - - private static MetadataReference GetAssemblyReference(IEnumerable assemblies, string name) - { - return MetadataReference.CreateFromFile(Assembly.Load(assemblies.First(n => n.Name == name)).Location); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.Shared/DiagnosticComparer.cs b/tests/NSubstitute.Analyzers.Tests.Shared/DiagnosticComparer.cs new file mode 100644 index 00000000..98664c04 --- /dev/null +++ b/tests/NSubstitute.Analyzers.Tests.Shared/DiagnosticComparer.cs @@ -0,0 +1,33 @@ +using System.Collections.Generic; +using Microsoft.CodeAnalysis; + +namespace NSubstitute.Analyzers.Tests.Shared +{ + public static class DiagnosticComparer + { + public static IComparer Span { get; } = new DiagnosticSpanStartComparer(); + + private class DiagnosticSpanStartComparer : IComparer + { + public int Compare(Diagnostic x, Diagnostic y) + { + if (ReferenceEquals(x, y)) + { + return 0; + } + + if (x == null) + { + return -1; + } + + if (y == null) + { + return 1; + } + + return x.Location.SourceSpan.Start.CompareTo(y.Location.SourceSpan.Start); + } + } + } +} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.Shared/Extensions/AssertionScopeExtensions.cs b/tests/NSubstitute.Analyzers.Tests.Shared/Extensions/AssertionScopeExtensions.cs new file mode 100644 index 00000000..a754d94a --- /dev/null +++ b/tests/NSubstitute.Analyzers.Tests.Shared/Extensions/AssertionScopeExtensions.cs @@ -0,0 +1,14 @@ +using System; +using FluentAssertions.Execution; + +namespace NSubstitute.Analyzers.Tests.Shared.Extensions +{ + public static class AssertionScopeExtensions + { + public static void Fail(this AssertionScope assertionScope, string message) + { + assertionScope.AddPreFormattedFailure(message); + assertionScope.FailWith(string.Empty, Array.Empty()); + } + } +} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.Shared/Extensions/CompilationExtensions.cs b/tests/NSubstitute.Analyzers.Tests.Shared/Extensions/CompilationExtensions.cs new file mode 100644 index 00000000..6499094e --- /dev/null +++ b/tests/NSubstitute.Analyzers.Tests.Shared/Extensions/CompilationExtensions.cs @@ -0,0 +1,44 @@ +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; + +namespace NSubstitute.Analyzers.Tests.Shared.Extensions +{ + public static class CompilationExtensions + { + public static Task> GetSortedAnalyzerDiagnostics( + this Compilation compilation, + DiagnosticAnalyzer analyzer, + AnalyzerOptions analyzerOptions, + CancellationToken cancellationToken = default) + { + return compilation.GetAnalyzerDiagnostics( + analyzer, + analyzerOptions, + DiagnosticComparer.Span, + cancellationToken); + } + + public static async Task> GetAnalyzerDiagnostics( + this Compilation compilation, + DiagnosticAnalyzer analyzer, + AnalyzerOptions analyzerOptions, + IComparer comparer = null, + CancellationToken cancellationToken = default) + { + var compilationWithAnalyzers = compilation.WithAnalyzers(ImmutableArray.Create(analyzer), analyzerOptions, cancellationToken); + + var diagnostics = await compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync(cancellationToken); + + if (comparer != null) + { + diagnostics = diagnostics.Sort(comparer); + } + + return diagnostics; + } + } +} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.Shared/Extensions/DocumentExtension.cs b/tests/NSubstitute.Analyzers.Tests.Shared/Extensions/DocumentExtension.cs new file mode 100644 index 00000000..a8904947 --- /dev/null +++ b/tests/NSubstitute.Analyzers.Tests.Shared/Extensions/DocumentExtension.cs @@ -0,0 +1,45 @@ +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CodeActions; +using Microsoft.CodeAnalysis.Formatting; +using Microsoft.CodeAnalysis.Simplification; + +namespace NSubstitute.Analyzers.Tests.Shared.Extensions +{ + public static class DocumentExtension + { + public static async Task ToFullString( + this Document document, + bool simplify = false, + bool format = false, + CancellationToken cancellationToken = default) + { + if (simplify) + { + document = await Simplifier.ReduceAsync(document, Simplifier.Annotation, cancellationToken: cancellationToken).ConfigureAwait(false); + } + + var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); + + if (format) + { + root = Formatter.Format(root, Formatter.Annotation, document.Project.Solution.Workspace); + } + + return root.ToFullString(); + } + + public static async Task ApplyCodeAction(this Document document, CodeAction codeAction) + { + var operations = await codeAction.GetOperationsAsync(CancellationToken.None).ConfigureAwait(false); + + return operations + .OfType() + .Single() + .ChangedSolution + .GetDocument(document.Id); + } + } +} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.Shared/Extensions/IEnumerableExtensions.cs b/tests/NSubstitute.Analyzers.Tests.Shared/Extensions/IEnumerableExtensions.cs new file mode 100644 index 00000000..0ab530d0 --- /dev/null +++ b/tests/NSubstitute.Analyzers.Tests.Shared/Extensions/IEnumerableExtensions.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.CodeAnalysis; + +namespace NSubstitute.Analyzers.Tests.Shared.Extensions +{ + public static class IEnumerableExtensions + { + public static string ToDebugString(this IEnumerable diagnostics) + { + var s = string.Join(Environment.NewLine, diagnostics.Select(d => d.ToString())); + + if (s.Length == 0) + { + s = "no diagnostics"; + } + + return $"{Environment.NewLine}Diagnostics:{Environment.NewLine}{s}{Environment.NewLine}"; + } + } +} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.Shared/Extensions/ProjectExtensions.cs b/tests/NSubstitute.Analyzers.Tests.Shared/Extensions/ProjectExtensions.cs new file mode 100644 index 00000000..99d33e15 --- /dev/null +++ b/tests/NSubstitute.Analyzers.Tests.Shared/Extensions/ProjectExtensions.cs @@ -0,0 +1,35 @@ +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; + +namespace NSubstitute.Analyzers.Tests.Shared.Extensions +{ + public static class ProjectExtensions + { + public static async Task> GetSortedAnalyzerDiagnostics( + this Project project, + DiagnosticAnalyzer analyzer, + CancellationToken cancellationToken = default) + { + var compilation = await project.GetCompilationAsync(cancellationToken); + return await compilation.GetSortedAnalyzerDiagnostics(analyzer, project.AnalyzerOptions, cancellationToken); + } + + public static async Task> GetAnalyzerDiagnostics( + this Project project, + DiagnosticAnalyzer analyzer, + IComparer comparer = null, + CancellationToken cancellationToken = default) + { + var compilation = await project.GetCompilationAsync(cancellationToken); + return await compilation.GetAnalyzerDiagnostics( + analyzer, + project.AnalyzerOptions, + comparer, + cancellationToken); + } + } +} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.Shared/ProjectOptions.cs b/tests/NSubstitute.Analyzers.Tests.Shared/ProjectOptions.cs new file mode 100644 index 00000000..184bef1a --- /dev/null +++ b/tests/NSubstitute.Analyzers.Tests.Shared/ProjectOptions.cs @@ -0,0 +1,18 @@ +using System.Collections.Immutable; +using Microsoft.CodeAnalysis; + +namespace NSubstitute.Analyzers.Tests.Shared +{ + public abstract class ProjectOptions + { + public CompilationOptions CompilationOptions { get; } + + public ImmutableArray MetadataReferences { get; } + + protected ProjectOptions(ImmutableArray metadataReferences, CompilationOptions compilationOptions) + { + MetadataReferences = metadataReferences; + CompilationOptions = compilationOptions; + } + } +} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.Shared/RuntimeMetadataReference.cs b/tests/NSubstitute.Analyzers.Tests.Shared/RuntimeMetadataReference.cs new file mode 100644 index 00000000..b9106881 --- /dev/null +++ b/tests/NSubstitute.Analyzers.Tests.Shared/RuntimeMetadataReference.cs @@ -0,0 +1,52 @@ +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Linq; +using System.Linq.Expressions; +using System.Reflection; +using Microsoft.CodeAnalysis; +using NSubstitute.Analyzers.Tests.Shared.DiagnosticAnalyzers; + +namespace NSubstitute.Analyzers.Tests.Shared +{ + public class RuntimeMetadataReference + { + private static readonly MetadataReference CorlibReference = MetadataReference.CreateFromFile(typeof(object).Assembly.Location); + + private static readonly MetadataReference SystemCoreReference = MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location); + + private static readonly MetadataReference CodeAnalysisReference = MetadataReference.CreateFromFile(typeof(Compilation).Assembly.Location); + + private static readonly MetadataReference NSubstituteReference = MetadataReference.CreateFromFile(typeof(Substitute).Assembly.Location); + + private static readonly MetadataReference TasksExtensionsReference = MetadataReference.CreateFromFile(Assembly.Load("System.Threading.Tasks.Extensions").Location); + + private static readonly MetadataReference NetStandard = MetadataReference.CreateFromFile(Assembly.Load("netstandard, Version=2.0.0.0").Location); + + private static readonly MetadataReference LinqExpressionReference = MetadataReference.CreateFromFile(typeof(Expression).Assembly.Location); + + static RuntimeMetadataReference() + { + var referencedAssemblies = typeof(DiagnosticVerifier).Assembly.GetReferencedAssemblies(); + var systemRuntimeReference = GetAssemblyReference(referencedAssemblies, "System.Runtime"); + var systemThreadingTasksReference = GetAssemblyReference(referencedAssemblies, "System.Threading.Tasks"); + + Default = ImmutableArray.Create( + CorlibReference, + SystemCoreReference, + CodeAnalysisReference, + NSubstituteReference, + NetStandard, + TasksExtensionsReference, + LinqExpressionReference, + systemRuntimeReference, + systemThreadingTasksReference); + } + + public static ImmutableArray Default { get; } + + private static MetadataReference GetAssemblyReference(IEnumerable assemblies, string name) + { + return MetadataReference.CreateFromFile(Assembly.Load(assemblies.First(n => n.Name == name)).Location); + } + } +} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.Shared/WorkspaceFactory.cs b/tests/NSubstitute.Analyzers.Tests.Shared/WorkspaceFactory.cs new file mode 100644 index 00000000..b0a662f9 --- /dev/null +++ b/tests/NSubstitute.Analyzers.Tests.Shared/WorkspaceFactory.cs @@ -0,0 +1,86 @@ +using System.Linq; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Text; +using NSubstitute.Analyzers.Shared.Settings; + +namespace NSubstitute.Analyzers.Tests.Shared +{ + public abstract class WorkspaceFactory + { + public static string DefaultDocumentName { get; } = "Test"; + + protected internal abstract string DocumentExtension { get; } + + protected abstract string Language { get; } + + public const string DefaultProjectName = "TestProject"; + + protected abstract ProjectOptions ProjectOptions { get; } + + public Project AddProject(Solution solution) + { + return AddProject(solution, ProjectOptions); + } + + public Project AddProject(Solution solution, ProjectOptions projectOptions) + { + return solution.AddProject(DefaultProjectName, DefaultProjectName, Language) + .WithMetadataReferences(projectOptions.MetadataReferences) + .WithCompilationOptions(projectOptions.CompilationOptions); + } + + public Project AddProject(Solution solution, string source) + { + return AddProject(solution, source, analyzerSettings: null); + } + + public Project AddProject(Solution solution, string source, string analyzerSettings) + { + return AddProject(solution, new[] { source }, analyzerSettings); + } + + public Project AddProject(Solution solution, string[] sources, string analyzerSettings) + { + var project = AddProject(solution); + project = AddDocuments(project, sources).Project; + if (analyzerSettings != null) + { + project = AddAnalyzerSettings(project, analyzerSettings).Project; + } + + return project; + } + + public Document AddDocument(Project project, string source) + { + var documentName = CreateDocumentName(project.Documents.Count()); + + var document = project.AddDocument(documentName, SourceText.From(source)); + + return document; + } + + public Document AddDocuments(Project project, string[] sources) + { + Document document = null; + + foreach (var source in sources) + { + document = AddDocument(project, source); + project = document.Project; + } + + return document; + } + + public TextDocument AddAnalyzerSettings(Project project, string settings) + { + return project.AddAdditionalDocument(AnalyzersSettings.AnalyzerFileName, SourceText.From(settings)); + } + + private string CreateDocumentName(int number) + { + return $"{DefaultDocumentName}{number}.{DocumentExtension}"; + } + } +} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/ConstructorArgumentsForInterfaceCodeFixProviderTests/ConstructorArgumentsForInterfaceCodeFixProviderTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/ConstructorArgumentsForInterfaceCodeFixProviderTests/ConstructorArgumentsForInterfaceCodeFixProviderTests.cs index 32c1a7b7..d286d3d5 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/ConstructorArgumentsForInterfaceCodeFixProviderTests/ConstructorArgumentsForInterfaceCodeFixProviderTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/ConstructorArgumentsForInterfaceCodeFixProviderTests/ConstructorArgumentsForInterfaceCodeFixProviderTests.cs @@ -1,5 +1,4 @@ -using System; -using System.Threading.Tasks; +using System.Threading.Tasks; using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.Diagnostics; using NSubstitute.Analyzers.Tests.Shared.CodeFixProviders; @@ -11,6 +10,10 @@ namespace NSubstitute.Analyzers.Tests.VisualBasic.CodeFixProvidersTests.Construc { public class ConstructorArgumentsForInterfaceCodeFixProviderTests : VisualBasicCodeFixVerifier, IConstructorArgumentsForInterfaceCodeFixVerifier { + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new SubstituteAnalyzer(); + + protected override CodeFixProvider CodeFixProvider { get; } = new ConstructorArgumentsForInterfaceCodeFixProvider(); + [Fact] public async Task RemovesInvocationArguments_WhenGenericFor_UsedWithParametersForInterface() { @@ -109,15 +112,5 @@ End Namespace "; await VerifyFix(source, newSource); } - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new SubstituteAnalyzer(); - } - - protected override CodeFixProvider GetCodeFixProvider() - { - return new ConstructorArgumentsForInterfaceCodeFixProvider(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/InternalSetupSpecificationCodeFixActionsTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/InternalSetupSpecificationCodeFixActionsTests.cs index 6af9be49..a23ea008 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/InternalSetupSpecificationCodeFixActionsTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/InternalSetupSpecificationCodeFixActionsTests.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; @@ -16,6 +15,20 @@ namespace NSubstitute.Analyzers.Tests.VisualBasic.CodeFixProvidersTests.Internal { public class InternalSetupSpecificationCodeFixActionsTests : VisualBasicCodeFixActionsVerifier, IInternalSetupSpecificationCodeFixActionsVerifier { + private static readonly MetadataReference[] AdditionalMetadataReferences = + { + GetInternalLibraryMetadataReference() + }; + + public InternalSetupSpecificationCodeFixActionsTests() + : base(VisualBasicWorkspaceFactory.Default.WithAdditionalMetadataReferences(AdditionalMetadataReferences)) + { + } + + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new NonSubstitutableMemberAnalyzer(); + + protected override CodeFixProvider CodeFixProvider { get; } = new InternalSetupSpecificationCodeFixProvider(); + [Fact] public async Task CreateCodeActions_InProperOrder() { @@ -58,26 +71,11 @@ End Class await VerifyCodeActions(source); } - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new NonSubstitutableMemberAnalyzer(); - } - - protected override CodeFixProvider GetCodeFixProvider() - { - return new InternalSetupSpecificationCodeFixProvider(); - } - - protected override IEnumerable GetAdditionalMetadataReferences() - { - return new[] { GetInternalLibraryMetadataReference() }; - } - - private static PortableExecutableReference GetInternalLibraryMetadataReference() + private static MetadataReference GetInternalLibraryMetadataReference() { var syntaxTree = VisualBasicSyntaxTree.ParseText($@"Imports System.Runtime.CompilerServices - + Namespace ExternalNamespace Public Class InternalFoo Friend Overridable Function Bar() As Integer diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/InternalSetupSpecificationCodeFixProviderVerifier.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/InternalSetupSpecificationCodeFixProviderVerifier.cs index 929b792e..be03b427 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/InternalSetupSpecificationCodeFixProviderVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/InternalSetupSpecificationCodeFixProviderVerifier.cs @@ -11,6 +11,10 @@ namespace NSubstitute.Analyzers.Tests.VisualBasic.CodeFixProvidersTests.Internal { public abstract class InternalSetupSpecificationCodeFixProviderVerifier : VisualBasicCodeFixVerifier, IInternalSetupSpecificationCodeFixProviderVerifier { + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new NonSubstitutableMemberAnalyzer(); + + protected override CodeFixProvider CodeFixProvider { get; } = new InternalSetupSpecificationCodeFixProvider(); + [CombinatoryTheory] [InlineData] public abstract Task ChangesInternalToPublic_ForIndexer_WhenUsedWithInternalMember(string method); @@ -40,15 +44,5 @@ public abstract class InternalSetupSpecificationCodeFixProviderVerifier : Visual [InlineData(".FooBar()")] [InlineData("(0)")] public abstract Task AppendsInternalsVisibleTo_ToTopLevelCompilationUnit_WhenUsedWithInternalMember(string method, string call); - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new NonSubstitutableMemberAnalyzer(); - } - - protected override CodeFixProvider GetCodeFixProvider() - { - return new InternalSetupSpecificationCodeFixProvider(); - } } } \ 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 c4a66cd7..6373c405 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/ReceivedAsExtensionMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/ReceivedAsExtensionMethodTests.cs @@ -1,7 +1,6 @@ using System.Threading.Tasks; using Microsoft.CodeAnalysis.Diagnostics; using NSubstitute.Analyzers.Tests.Shared.Extensibility; -using NSubstitute.Analyzers.Tests.VisualBasic.DiagnosticAnalyzersTests.NonSubstitutableMemberReceivedAnalyzerTests; using NSubstitute.Analyzers.VisualBasic.DiagnosticAnalyzers; namespace NSubstitute.Analyzers.Tests.VisualBasic.CodeFixProvidersTests.InternalSetupSpecificationCodeFixProviderTests @@ -9,6 +8,8 @@ namespace NSubstitute.Analyzers.Tests.VisualBasic.CodeFixProvidersTests.Internal [CombinatoryData("Received")] public class ReceivedAsExtensionMethodTests : InternalSetupSpecificationCodeFixProviderVerifier { + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new NonSubstitutableMemberReceivedAnalyzer(); + public override async Task ChangesInternalToPublic_ForIndexer_WhenUsedWithInternalMember(string method) { var oldSource = $@"Imports NSubstitute @@ -336,10 +337,5 @@ End Namespace "; await VerifyFix(oldSource, newSource, 2); } - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new NonSubstitutableMemberReceivedAnalyzer(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/ReceivedAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/ReceivedAsOrdinaryMethodTests.cs index 0f9cff22..d7a037a8 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/ReceivedAsOrdinaryMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/ReceivedAsOrdinaryMethodTests.cs @@ -8,6 +8,8 @@ namespace NSubstitute.Analyzers.Tests.VisualBasic.CodeFixProvidersTests.Internal [CombinatoryData("SubstituteExtensions.Received")] public class ReceivedAsOrdinaryMethodTests : InternalSetupSpecificationCodeFixProviderVerifier { + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new NonSubstitutableMemberReceivedAnalyzer(); + public override async Task ChangesInternalToPublic_ForIndexer_WhenUsedWithInternalMember(string method) { var oldSource = $@"Imports NSubstitute @@ -335,10 +337,5 @@ End Namespace "; await VerifyFix(oldSource, newSource, 2); } - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new NonSubstitutableMemberReceivedAnalyzer(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsExtensionMethodTests.cs index 3012f9b7..439d2b84 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsExtensionMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsExtensionMethodTests.cs @@ -1,7 +1,6 @@ using System.Threading.Tasks; using Microsoft.CodeAnalysis.Diagnostics; using NSubstitute.Analyzers.Tests.Shared.Extensibility; -using NSubstitute.Analyzers.VisualBasic.CodeFixProviders; using NSubstitute.Analyzers.VisualBasic.DiagnosticAnalyzers; namespace NSubstitute.Analyzers.Tests.VisualBasic.CodeFixProvidersTests.InternalSetupSpecificationCodeFixProviderTests @@ -9,6 +8,8 @@ namespace NSubstitute.Analyzers.Tests.VisualBasic.CodeFixProvidersTests.Internal [CombinatoryData("When")] public class WhenAsExtensionMethodTests : InternalSetupSpecificationCodeFixProviderVerifier { + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new NonSubstitutableMemberWhenAnalyzer(); + public override async Task ChangesInternalToPublic_ForIndexer_WhenUsedWithInternalMember(string method) { var oldSource = $@"Imports NSubstitute @@ -364,10 +365,5 @@ End Namespace "; await VerifyFix(oldSource, newSource, 2); } - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new NonSubstitutableMemberWhenAnalyzer(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsOrdinaryMethodTests.cs index fa01e0e8..73919b39 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsOrdinaryMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/InternalSetupSpecificationCodeFixProviderTests/WhenAsOrdinaryMethodTests.cs @@ -8,6 +8,8 @@ namespace NSubstitute.Analyzers.Tests.VisualBasic.CodeFixProvidersTests.Internal [CombinatoryData("SubstituteExtensions.When")] public class WhenAsOrdinaryMethodTests : InternalSetupSpecificationCodeFixProviderVerifier { + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new NonSubstitutableMemberWhenAnalyzer(); + public override async Task ChangesInternalToPublic_ForIndexer_WhenUsedWithInternalMember(string method) { var oldSource = $@"Imports NSubstitute @@ -363,10 +365,5 @@ End Namespace "; await VerifyFix(oldSource, newSource, 2); } - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new NonSubstitutableMemberWhenAnalyzer(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/NonSubstitutableMemberAnalyzerSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberSuppressDiagnosticsCodeFixActionsTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/NonSubstitutableMemberAnalyzerSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberSuppressDiagnosticsCodeFixActionsTests.cs index 7b8c9867..ce72446b 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/NonSubstitutableMemberAnalyzerSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberSuppressDiagnosticsCodeFixActionsTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/NonSubstitutableMemberAnalyzerSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberSuppressDiagnosticsCodeFixActionsTests.cs @@ -11,6 +11,10 @@ namespace NSubstitute.Analyzers.Tests.VisualBasic.CodeFixProvidersTests.NonSubst { public class NonSubstitutableMemberSuppressDiagnosticsCodeFixActionsTests : VisualBasicCodeFixActionsVerifier, INonSubstitutableMemberSuppressDiagnosticsCodeFixActionsVerifier { + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new NonSubstitutableMemberAnalyzer(); + + protected override CodeFixProvider CodeFixProvider { get; } = new NonSubstitutableMemberSuppressDiagnosticsCodeFixProvider(); + [Fact] public async Task CreatesCorrectCodeFixActions_ForIndexer() { @@ -107,15 +111,5 @@ await VerifyCodeActions(source, new[] $"Suppress {DiagnosticIdentifiers.NonVirtualSetupSpecification} for namespace MyNamespace in nsubstitute.json" }); } - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new NonSubstitutableMemberAnalyzer(); - } - - protected override CodeFixProvider GetCodeFixProvider() - { - return new NonSubstitutableMemberSuppressDiagnosticsCodeFixProvider(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/NonSubstitutableMemberAnalyzerSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberSuppressDiagnosticsCodeFixVerifier.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/NonSubstitutableMemberAnalyzerSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberSuppressDiagnosticsCodeFixVerifier.cs index 0bbc2568..b851de0b 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/NonSubstitutableMemberAnalyzerSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberSuppressDiagnosticsCodeFixVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/NonSubstitutableMemberAnalyzerSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberSuppressDiagnosticsCodeFixVerifier.cs @@ -10,6 +10,10 @@ namespace NSubstitute.Analyzers.Tests.VisualBasic.CodeFixProvidersTests.NonSubst { public abstract class NonSubstitutableMemberSuppressDiagnosticsCodeFixVerifier : VisualBasicSuppressDiagnosticSettingsVerifier, INonSubstitutableMemberSuppressDiagnosticsCodeFixVerifier { + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new NonSubstitutableMemberAnalyzer(); + + protected override CodeFixProvider CodeFixProvider { get; } = new NonSubstitutableMemberSuppressDiagnosticsCodeFixProvider(); + [Fact] public abstract Task SuppressesDiagnosticsInSettings_WhenSettingValueForNonVirtualMethod(); @@ -33,15 +37,5 @@ public abstract class NonSubstitutableMemberSuppressDiagnosticsCodeFixVerifier : [Fact] public abstract Task SuppressesDiagnosticsInSettingsForNamespace_WhenSettingsValueForNonVirtualMember_AndSelectingNamespaceSuppression(); - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new NonSubstitutableMemberAnalyzer(); - } - - protected override CodeFixProvider GetCodeFixProvider() - { - return new NonSubstitutableMemberSuppressDiagnosticsCodeFixProvider(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixActionsTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixActionsTests.cs index 35f4a003..4f36e3f1 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixActionsTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixActionsTests.cs @@ -12,6 +12,10 @@ namespace NSubstitute.Analyzers.Tests.VisualBasic.CodeFixProvidersTests.NonSubst { public class NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixActionsTests : VisualBasicCodeFixActionsVerifier, INonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixActionsVerifier { + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new NonSubstitutableMemberArgumentMatcherAnalyzer(); + + protected override CodeFixProvider CodeFixProvider { get; } = new NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixProvider(); + [Fact] public async Task CreatesCorrectCodeFixActions_ForIndexer() { @@ -112,15 +116,5 @@ End Class End Namespace"; await VerifyCodeActions(source, Array.Empty()); } - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new NonSubstitutableMemberArgumentMatcherAnalyzer(); - } - - protected override CodeFixProvider GetCodeFixProvider() - { - return new NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixProvider(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixTests.cs index 94aaf480..14cfa20d 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixProviderTests/NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixTests.cs @@ -12,6 +12,10 @@ namespace NSubstitute.Analyzers.Tests.VisualBasic.CodeFixProvidersTests.NonSubst public class NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixTests : VisualBasicSuppressDiagnosticSettingsVerifier, INonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixVerifier { + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new NonSubstitutableMemberArgumentMatcherAnalyzer(); + + protected override CodeFixProvider CodeFixProvider { get; } = new NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixProvider(); + [Fact] public async Task SuppressesDiagnosticsInSettings_WhenArgMatcherUsedInNonVirtualMethod() { @@ -113,15 +117,5 @@ End Namespace await VerifySuppressionSettings(source, "N:MyNamespace", DiagnosticIdentifiers.NonSubstitutableMemberArgumentMatcherUsage, codeFixIndex: 2); } - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new NonSubstitutableMemberArgumentMatcherAnalyzer(); - } - - protected override CodeFixProvider GetCodeFixProvider() - { - return new NonSubstitutableMemberArgumentMatcherSuppressDiagnosticsCodeFixProvider(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/PartialSubstituteUsedForUnsupportedTypeCodeFixProviderTests/PartialSubstituteUsedForUnsupportedTypeCodeFixActionsTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/PartialSubstituteUsedForUnsupportedTypeCodeFixProviderTests/PartialSubstituteUsedForUnsupportedTypeCodeFixActionsTests.cs index e8048bd6..b57f3619 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/PartialSubstituteUsedForUnsupportedTypeCodeFixProviderTests/PartialSubstituteUsedForUnsupportedTypeCodeFixActionsTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/PartialSubstituteUsedForUnsupportedTypeCodeFixProviderTests/PartialSubstituteUsedForUnsupportedTypeCodeFixActionsTests.cs @@ -10,6 +10,10 @@ namespace NSubstitute.Analyzers.Tests.VisualBasic.CodeFixProvidersTests.PartialS { public class PartialSubstituteUsedForUnsupportedTypeCodeFixActionsTests : VisualBasicCodeFixActionsVerifier, IPartialSubstituteUsedForUnsupportedTypeCodeFixActionsVerifier { + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new SubstituteAnalyzer(); + + protected override CodeFixProvider CodeFixProvider { get; } = new PartialSubstituteUsedForUnsupportedTypeCodeFixProvider(); + [Fact] public async Task CreatesCorrectCodeFixActions_ForSubstituteForPartsOf() { @@ -48,15 +52,5 @@ End Namespace "; await VerifyCodeActions(source, "Use SubstituteFactory.Create"); } - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new SubstituteAnalyzer(); - } - - protected override CodeFixProvider GetCodeFixProvider() - { - return new PartialSubstituteUsedForUnsupportedTypeCodeFixProvider(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/PartialSubstituteUsedForUnsupportedTypeCodeFixProviderTests/PartialSubstituteUsedForUnsupportedTypeCodeFixProviderTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/PartialSubstituteUsedForUnsupportedTypeCodeFixProviderTests/PartialSubstituteUsedForUnsupportedTypeCodeFixProviderTests.cs index af6c0c95..b7af470b 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/PartialSubstituteUsedForUnsupportedTypeCodeFixProviderTests/PartialSubstituteUsedForUnsupportedTypeCodeFixProviderTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/PartialSubstituteUsedForUnsupportedTypeCodeFixProviderTests/PartialSubstituteUsedForUnsupportedTypeCodeFixProviderTests.cs @@ -10,6 +10,10 @@ namespace NSubstitute.Analyzers.Tests.VisualBasic.CodeFixProvidersTests.PartialS { public class PartialSubstituteUsedForUnsupportedTypeCodeFixProviderTests : VisualBasicCodeFixVerifier, IForPartsOfUsedForUnsupportedTypeCodeFixVerifier { + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new SubstituteAnalyzer(); + + protected override CodeFixProvider CodeFixProvider { get; } = new PartialSubstituteUsedForUnsupportedTypeCodeFixProvider(); + [Fact] public async Task ReplacesForPartsOf_WithFor_WhenUsedWithInterface() { @@ -132,15 +136,5 @@ End Class await VerifyFix(oldSource, newSource); } - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new SubstituteAnalyzer(); - } - - protected override CodeFixProvider GetCodeFixProvider() - { - return new PartialSubstituteUsedForUnsupportedTypeCodeFixProvider(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/ReEntrantSetupCodeFixProviderTests/ReEntrantSetupCodeFixActionsTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/ReEntrantSetupCodeFixProviderTests/ReEntrantSetupCodeFixActionsTests.cs index e87247ac..3607118f 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/ReEntrantSetupCodeFixProviderTests/ReEntrantSetupCodeFixActionsTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/ReEntrantSetupCodeFixProviderTests/ReEntrantSetupCodeFixActionsTests.cs @@ -11,6 +11,10 @@ namespace NSubstitute.Analyzers.Tests.VisualBasic.CodeFixProvidersTests.ReEntran { public class ReEntrantSetupCodeFixActionsTests : VisualBasicCodeFixActionsVerifier, IReEntrantSetupCodeFixActionsVerifier { + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new ReEntrantSetupAnalyzer(); + + protected override CodeFixProvider CodeFixProvider { get; } = new ReEntrantSetupCodeFixProvider(); + [Theory] [InlineData("Await CreateReEntrantSubstituteAsync(), Await CreateDefaultValue()")] [InlineData("CreateReEntrantSubstitute(), Await CreateDefaultValue()")] @@ -84,15 +88,5 @@ End Namespace "; await VerifyCodeActions(source, Array.Empty()); } - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new ReEntrantSetupAnalyzer(); - } - - protected override CodeFixProvider GetCodeFixProvider() - { - return new ReEntrantSetupCodeFixProvider(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/ReEntrantSetupCodeFixProviderTests/ReEntrantSetupCodeFixVerifier.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/ReEntrantSetupCodeFixProviderTests/ReEntrantSetupCodeFixVerifier.cs index 86199d0a..a9fb6d7b 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/ReEntrantSetupCodeFixProviderTests/ReEntrantSetupCodeFixVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/ReEntrantSetupCodeFixProviderTests/ReEntrantSetupCodeFixVerifier.cs @@ -10,6 +10,10 @@ namespace NSubstitute.Analyzers.Tests.VisualBasic.CodeFixProvidersTests.ReEntran { public abstract class ReEntrantSetupCodeFixVerifier : VisualBasicCodeFixVerifier, IReEntrantSetupCodeFixProviderVerifier { + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new ReEntrantSetupAnalyzer(); + + protected override CodeFixProvider CodeFixProvider { get; } = new ReEntrantSetupCodeFixProvider(); + [Theory] [InlineData("CreateReEntrantSubstitute(), CreateDefaultValue(), 1", "Function(x) CreateReEntrantSubstitute(), Function(x) CreateDefaultValue(), Function(x) 1")] [InlineData("CreateReEntrantSubstitute(), { CreateDefaultValue(), 1 }", "Function(x) CreateReEntrantSubstitute(), New System.Func(Of Core.CallInfo, Integer)() {Function(x) CreateDefaultValue(), Function(x) 1}")] @@ -19,15 +23,5 @@ public abstract class ReEntrantSetupCodeFixVerifier : VisualBasicCodeFixVerifier [Fact] public abstract Task ReplacesArgumentExpression_WithLambdaWithReducedTypes_WhenGeneratingArrayParamsArgument(); - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new ReEntrantSetupAnalyzer(); - } - - protected override CodeFixProvider GetCodeFixProvider() - { - return new ReEntrantSetupCodeFixProvider(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/SubstituteForInternalMemberCodeFixProviderTests/SubstituteForInternalMemberCodeFixActionsTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/SubstituteForInternalMemberCodeFixProviderTests/SubstituteForInternalMemberCodeFixActionsTests.cs index c3834b63..2fbc1899 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/SubstituteForInternalMemberCodeFixProviderTests/SubstituteForInternalMemberCodeFixActionsTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/SubstituteForInternalMemberCodeFixProviderTests/SubstituteForInternalMemberCodeFixActionsTests.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; @@ -16,6 +15,20 @@ namespace NSubstitute.Analyzers.Tests.VisualBasic.CodeFixProvidersTests.Substitu { public class SubstituteForInternalMemberCodeFixActionsTests : VisualBasicCodeFixActionsVerifier, ISubstituteForInternalMemberCodeFixActionsVerifier { + private static readonly MetadataReference[] AdditionalMetadataReferences = + { + GetInternalLibraryMetadataReference() + }; + + public SubstituteForInternalMemberCodeFixActionsTests() + : base(VisualBasicWorkspaceFactory.Default.WithAdditionalMetadataReferences(AdditionalMetadataReferences)) + { + } + + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new SubstituteAnalyzer(); + + protected override CodeFixProvider CodeFixProvider { get; } = new SubstituteForInternalMemberCodeFixProvider(); + [Fact] public async Task CreatesCorrectCodeFixActions_WhenSourceForInternalType_IsAvailable() { @@ -77,27 +90,12 @@ End Namespace await VerifyCodeActions(source); } - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new SubstituteAnalyzer(); - } - - protected override CodeFixProvider GetCodeFixProvider() - { - return new SubstituteForInternalMemberCodeFixProvider(); - } - - protected override IEnumerable GetAdditionalMetadataReferences() - { - return new[] { GetInternalLibraryMetadataReference() }; - } - private static PortableExecutableReference GetInternalLibraryMetadataReference() { var syntaxTree = VisualBasicSyntaxTree.ParseText($@" Imports System.Runtime.CompilerServices - + Namespace ExternalNamespace Friend Class InternalFoo End Class diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/SubstituteForInternalMemberCodeFixProviderTests/SubstituteForInternalMemberCodeFixVerifier.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/SubstituteForInternalMemberCodeFixProviderTests/SubstituteForInternalMemberCodeFixVerifier.cs index eaeadcf9..3f18d2a7 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/SubstituteForInternalMemberCodeFixProviderTests/SubstituteForInternalMemberCodeFixVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/SubstituteForInternalMemberCodeFixProviderTests/SubstituteForInternalMemberCodeFixVerifier.cs @@ -9,6 +9,10 @@ namespace NSubstitute.Analyzers.Tests.VisualBasic.CodeFixProvidersTests.Substitu { public abstract class SubstituteForInternalMemberCodeFixVerifier : VisualBasicCodeFixVerifier, ISubstituteForInternalMemberCodeFixVerifier { + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new SubstituteAnalyzer(); + + protected override CodeFixProvider CodeFixProvider { get; } = new SubstituteForInternalMemberCodeFixProvider(); + public abstract Task AppendsInternalsVisibleTo_ToTopLevelCompilationUnit_WhenUsedWithInternalClass(); public abstract Task AppendsInternalsVisibleTo_WhenUsedWithInternalClass(); @@ -20,15 +24,5 @@ public abstract class SubstituteForInternalMemberCodeFixVerifier : VisualBasicCo public abstract Task DoesNot_AppendsInternalsVisibleTo_WhenUsedWithPublicClass(); public abstract Task DoesNot_AppendsInternalsVisibleTo_WhenInternalsVisibleToAppliedToDynamicProxyGenAssembly2(); - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new SubstituteAnalyzer(); - } - - protected override CodeFixProvider GetCodeFixProvider() - { - return new SubstituteForInternalMemberCodeFixProvider(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/VisualBasicCodeFixActionsVerifier.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/VisualBasicCodeFixActionsVerifier.cs index 937f5776..70f9db2f 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/VisualBasicCodeFixActionsVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/VisualBasicCodeFixActionsVerifier.cs @@ -1,18 +1,17 @@ -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.VisualBasic; -using NSubstitute.Analyzers.Tests.Shared.CodeFixProviders; +using NSubstitute.Analyzers.Tests.Shared.CodeFixProviders; namespace NSubstitute.Analyzers.Tests.VisualBasic.CodeFixProvidersTests { public abstract class VisualBasicCodeFixActionsVerifier : CodeFixCodeActionsVerifier { - protected override string Language { get; } = LanguageNames.VisualBasic; - - protected override string FileExtension { get; } = "vb"; + protected VisualBasicCodeFixActionsVerifier() + : this(VisualBasicWorkspaceFactory.Default) + { + } - protected override CompilationOptions GetCompilationOptions() + protected VisualBasicCodeFixActionsVerifier(VisualBasicWorkspaceFactory factory) + : base(factory) { - return new VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary, optionStrict: OptionStrict.On); } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/VisualBasicCodeFixVerifier.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/VisualBasicCodeFixVerifier.cs index ec4c750e..0e858550 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/VisualBasicCodeFixVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/VisualBasicCodeFixVerifier.cs @@ -1,19 +1,18 @@ -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.VisualBasic; -using NSubstitute.Analyzers.Tests.Shared; +using NSubstitute.Analyzers.Tests.Shared; using NSubstitute.Analyzers.Tests.Shared.CodeFixProviders; namespace NSubstitute.Analyzers.Tests.VisualBasic.CodeFixProvidersTests { public abstract class VisualBasicCodeFixVerifier : CodeFixVerifier { - protected override string Language { get; } = LanguageNames.VisualBasic; - - protected override string FileExtension { get; } = "vb"; + protected VisualBasicCodeFixVerifier() + : this(VisualBasicWorkspaceFactory.Default) + { + } - protected override CompilationOptions GetCompilationOptions() + protected VisualBasicCodeFixVerifier(WorkspaceFactory workspaceFactory) + : base(workspaceFactory) { - return new VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary, optionStrict: OptionStrict.Off); } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/VisualBasicSuppressDiagnosticSettingsVerifier.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/VisualBasicSuppressDiagnosticSettingsVerifier.cs index 49ec870d..045f8b47 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/VisualBasicSuppressDiagnosticSettingsVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeFixProvidersTests/VisualBasicSuppressDiagnosticSettingsVerifier.cs @@ -1,6 +1,4 @@ -using System.Collections.Generic; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.VisualBasic; +using Microsoft.CodeAnalysis; using Microsoft.VisualBasic.CompilerServices; using NSubstitute.Analyzers.Tests.Shared.CodeFixProviders; @@ -13,18 +11,11 @@ public abstract class VisualBasicSuppressDiagnosticSettingsVerifier : SuppressDi MetadataReference.CreateFromFile(typeof(StandardModuleAttribute).Assembly.Location) }; - protected override string Language { get; } = LanguageNames.VisualBasic; - - protected override string FileExtension { get; } = "vb"; - - protected override CompilationOptions GetCompilationOptions() - { - return new VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary, optionStrict: OptionStrict.On); - } - - protected override IEnumerable GetAdditionalMetadataReferences() + protected VisualBasicSuppressDiagnosticSettingsVerifier() + : base(VisualBasicWorkspaceFactory.Default + .WithOptionStrictOn() + .WithAdditionalMetadataReferences(AdditionalReferences)) { - return AdditionalReferences; } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/AsyncReceivedInOrderCallbackAnalyzerTests/AsyncReceivedInOrderCallbackDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/AsyncReceivedInOrderCallbackAnalyzerTests/AsyncReceivedInOrderCallbackDiagnosticVerifier.cs index 1fc1114d..58d27e7e 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/AsyncReceivedInOrderCallbackAnalyzerTests/AsyncReceivedInOrderCallbackDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/AsyncReceivedInOrderCallbackAnalyzerTests/AsyncReceivedInOrderCallbackDiagnosticVerifier.cs @@ -13,6 +13,8 @@ public class AsyncReceivedInOrderCallbackDiagnosticVerifier : VisualBasicDiagnos { private readonly DiagnosticDescriptor _descriptor = DiagnosticDescriptors.AsyncCallbackUsedInReceivedInOrder; + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new AsyncReceivedInOrderCallbackAnalyzer(); + [Fact] public async Task ReportsDiagnostic_WhenAsyncLambdaCallbackUsedInReceivedInOrder() { @@ -138,10 +140,5 @@ End Namespace await VerifyNoDiagnostic(source); } - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new AsyncReceivedInOrderCallbackAnalyzer(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/CallInfoDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/CallInfoDiagnosticVerifier.cs index 23c1a0f4..0b3f2fb1 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/CallInfoDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/CallInfoDiagnosticVerifier.cs @@ -24,6 +24,8 @@ public abstract class CallInfoDiagnosticVerifier : VisualBasicDiagnosticVerifier protected DiagnosticDescriptor CallInfoArgumentSetWithIncompatibleValueDescriptor { get; } = DiagnosticDescriptors.CallInfoArgumentSetWithIncompatibleValue; + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new CallInfoAnalyzer(); + [CombinatoryTheory] [InlineData("substitute(Arg.Any(Of Integer)())", "callInfo.ArgAt(Of Integer)(1)")] [InlineData("substitute(Arg.Any(Of Integer)())", "Dim x = callInfo(1)")] @@ -334,10 +336,5 @@ public abstract class CallInfoDiagnosticVerifier : VisualBasicDiagnosticVerifier [InlineData("IDictionary(Of String, Object)", "New Dictionary(Of String, Object)()")] [InlineData("IReadOnlyDictionary(Of String, Object)", "New Dictionary(Of String, Object)()")] public abstract Task ReportsNoDiagnostic_WhenAssigningType_AssignableTo_Argument(string method, string left, string right); - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new CallInfoAnalyzer(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ConflictingArgumentAssignmentsAnalyzerTests/ConflictingArgumentAssignmentsDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ConflictingArgumentAssignmentsAnalyzerTests/ConflictingArgumentAssignmentsDiagnosticVerifier.cs index de71d879..588ca225 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ConflictingArgumentAssignmentsAnalyzerTests/ConflictingArgumentAssignmentsDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ConflictingArgumentAssignmentsAnalyzerTests/ConflictingArgumentAssignmentsDiagnosticVerifier.cs @@ -12,7 +12,9 @@ namespace NSubstitute.Analyzers.Tests.VisualBasic.DiagnosticAnalyzersTests.Confl { public abstract class ConflictingArgumentAssignmentsDiagnosticVerifier : VisualBasicDiagnosticVerifier, IConflictingArgumentAssignmentsDiagnosticVerifier { - public DiagnosticDescriptor Descriptor { get; } = DiagnosticDescriptors.ConflictingArgumentAssignments; + protected DiagnosticDescriptor Descriptor { get; } = DiagnosticDescriptors.ConflictingArgumentAssignments; + + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new ConflictingArgumentAssignmentsAnalyzer(); [CombinatoryTheory] [InlineData("substitute.Bar(Arg.Any(Of Integer)())", "callInfo(1) = 1", "[|callInfo(1)|] = 1")] @@ -49,10 +51,5 @@ public abstract class ConflictingArgumentAssignmentsDiagnosticVerifier : VisualB [InlineData("substitute.Barr", "callInfo(1) = 1")] [InlineData("substitute(Arg.Any(Of Integer)())", "callInfo(1) = 1")] public abstract Task ReportsNoDiagnostic_When_AndDoesMethod_SetArgument_AndPreviousMethod_IsNotUsedWithCallInfo(string method, string call, string andDoesArgAccess); - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new ConflictingArgumentAssignmentsAnalyzer(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/NonSubstitutableMemberDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/NonSubstitutableMemberDiagnosticVerifier.cs index f13ed552..94c3bba3 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/NonSubstitutableMemberDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/NonSubstitutableMemberDiagnosticVerifier.cs @@ -20,6 +20,10 @@ public abstract class NonSubstitutableMemberDiagnosticVerifier : VisualBasicDiag protected DiagnosticDescriptor InternalSetupSpecificationDescriptor { get; } = DiagnosticDescriptors.InternalSetupSpecification; + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new NonSubstitutableMemberAnalyzer(); + + protected override string AnalyzerSettings => Settings != null ? Json.Encode(Settings) : null; + [CombinatoryTheory] [InlineData] public abstract Task ReportsDiagnostics_WhenSettingValueForNonVirtualMethod(string method); @@ -163,15 +167,5 @@ public abstract class NonSubstitutableMemberDiagnosticVerifier : VisualBasicDiag [InlineData(".FooBar()")] [InlineData("(0)")] public abstract Task ReportsNoDiagnostics_WhenSettingValueForProtectedInternalVirtualMember(string method, string call); - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new NonSubstitutableMemberAnalyzer(); - } - - protected override string GetSettings() - { - return Settings != null ? Json.Encode(Settings) : null; - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ReturnsAsExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ReturnsAsExtensionMethodTests.cs index 99dd2eaa..bded30a7 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ReturnsAsExtensionMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ReturnsAsExtensionMethodTests.cs @@ -736,7 +736,7 @@ End Namespace "Member FooBar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -800,7 +800,7 @@ End Namespace "Member FooBar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -867,7 +867,7 @@ End Namespace "Member FooBar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ReturnsAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ReturnsAsOrdinaryMethodTests.cs index 0eea2b4b..78249005 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ReturnsAsOrdinaryMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ReturnsAsOrdinaryMethodTests.cs @@ -737,7 +737,7 @@ End Namespace "Member FooBar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -801,7 +801,7 @@ End Namespace "Member FooBar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -868,7 +868,7 @@ End Namespace "Member FooBar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ReturnsNullAsExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ReturnsNullAsExtensionMethodTests.cs index 62f54c4e..8b89d329 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ReturnsNullAsExtensionMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ReturnsNullAsExtensionMethodTests.cs @@ -746,7 +746,7 @@ End Namespace "Member FooBar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -811,7 +811,7 @@ End Namespace "Member FooBar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -879,7 +879,7 @@ End Namespace "Member FooBar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ReturnsNullAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ReturnsNullAsOrdinaryMethodTests.cs index 20c99c24..a0f834fd 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ReturnsNullAsOrdinaryMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ReturnsNullAsOrdinaryMethodTests.cs @@ -746,7 +746,7 @@ End Namespace "Member FooBar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -811,7 +811,7 @@ End Namespace "Member FooBar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -879,7 +879,7 @@ End Namespace "Member FooBar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsExtensionMethodTests.cs index 3511321c..45d6d9f9 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsExtensionMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsExtensionMethodTests.cs @@ -782,7 +782,7 @@ End Namespace "Member FooBar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -848,7 +848,7 @@ End Namespace "Member FooBar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -917,7 +917,7 @@ End Namespace "Member FooBar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodTests.cs index a89a4a45..b86bfe44 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodTests.cs @@ -781,7 +781,7 @@ End Namespace "Member FooBar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -847,7 +847,7 @@ End Namespace "Member FooBar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -916,7 +916,7 @@ End Namespace "Member FooBar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodWithGenericTypeSpecifiedTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodWithGenericTypeSpecifiedTests.cs index 3ffece12..a681ccdf 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodWithGenericTypeSpecifiedTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodWithGenericTypeSpecifiedTests.cs @@ -781,7 +781,7 @@ End Namespace "Member FooBar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -847,7 +847,7 @@ End Namespace "Member FooBar can not be intercepted. Only interface members and overrideable, overriding, and must override members can be intercepted." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -917,7 +917,7 @@ End Namespace }; var diagnostics = textParserResult.Spans.Select((span, idx) => - CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + CreateDiagnostic(NonVirtualSetupSpecificationDescriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberArgumentMatcherAnalyzerTests/NonSubstitutableMemberArgumentMatcherDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberArgumentMatcherAnalyzerTests/NonSubstitutableMemberArgumentMatcherDiagnosticVerifier.cs index b99ba0c7..8364cbe2 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberArgumentMatcherAnalyzerTests/NonSubstitutableMemberArgumentMatcherDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberArgumentMatcherAnalyzerTests/NonSubstitutableMemberArgumentMatcherDiagnosticVerifier.cs @@ -3,7 +3,6 @@ using System.Threading.Tasks; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; -using Microsoft.CodeAnalysis.VisualBasic; using NSubstitute.Analyzers.Shared; using NSubstitute.Analyzers.Shared.Settings; using NSubstitute.Analyzers.Shared.TinyJson; @@ -20,6 +19,10 @@ public abstract class NonSubstitutableMemberArgumentMatcherDiagnosticVerifier : protected DiagnosticDescriptor ArgumentMatcherUsedWithoutSpecifyingCall { get; } = DiagnosticDescriptors.NonSubstitutableMemberArgumentMatcherUsage; + protected override string AnalyzerSettings => Settings != null ? Json.Encode(Settings) : null; + + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new NonSubstitutableMemberArgumentMatcherAnalyzer(); + [Theory] [MemberData(nameof(MisusedArgTestCases))] public abstract Task ReportsDiagnostics_WhenUsedInNonVirtualMethod(string arg); @@ -108,11 +111,6 @@ public abstract class NonSubstitutableMemberArgumentMatcherDiagnosticVerifier : [Fact] public abstract Task ReportsNoDiagnostic_WhenOverloadCannotBeInferred(); - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new NonSubstitutableMemberArgumentMatcherAnalyzer(); - } - public static IEnumerable MisusedArgTestCases { get { return MisusedArgs.Select(argArray => argArray.Select(arg => arg).ToArray()); } @@ -226,15 +224,5 @@ public static IEnumerable MisusedArgs }; } } - - protected override string GetSettings() - { - return Settings != null ? Json.Encode(Settings) : null; - } - - protected override CompilationOptions GetCompilationOptions() - { - return new VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary, optionStrict: OptionStrict.Off); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberReceivedAnalyzerTests/NonSubstitutableMemberReceivedDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberReceivedAnalyzerTests/NonSubstitutableMemberReceivedDiagnosticVerifier.cs index 3fa4744e..566daa13 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberReceivedAnalyzerTests/NonSubstitutableMemberReceivedDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberReceivedAnalyzerTests/NonSubstitutableMemberReceivedDiagnosticVerifier.cs @@ -16,6 +16,8 @@ public abstract class NonSubstitutableMemberReceivedDiagnosticVerifier : VisualB protected DiagnosticDescriptor InternalSetupSpecificationDescriptor { get; } = DiagnosticDescriptors.InternalSetupSpecification; + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new NonSubstitutableMemberReceivedAnalyzer(); + [CombinatoryTheory] [InlineData] public abstract Task ReportsDiagnostics_WhenCheckingReceivedCallsForNonVirtualMethod(string method); @@ -103,10 +105,5 @@ public abstract class NonSubstitutableMemberReceivedDiagnosticVerifier : VisualB [CombinatoryTheory] [InlineData] public abstract Task ReportsNoDiagnostics_WhenUsingUnfortunatelyNamedMethod(string method); - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new NonSubstitutableMemberReceivedAnalyzer(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberWhenAnalyzerTests/NonSubstitutableMemberWhenDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberWhenAnalyzerTests/NonSubstitutableMemberWhenDiagnosticVerifier.cs index ea937eaf..f3b8ebb5 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberWhenAnalyzerTests/NonSubstitutableMemberWhenDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberWhenAnalyzerTests/NonSubstitutableMemberWhenDiagnosticVerifier.cs @@ -16,6 +16,8 @@ public abstract class NonSubstitutableMemberWhenDiagnosticVerifier : VisualBasic protected DiagnosticDescriptor InternalSetupSpecificationDescriptor { get; } = DiagnosticDescriptors.InternalSetupSpecification; + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new NonSubstitutableMemberWhenAnalyzer(); + [CombinatoryTheory] [InlineData("Sub(sb) [|sb.Bar(Arg.Any(Of Integer)())|]")] [InlineData(@"Function(ByVal [sub] As Foo) [|[sub].Bar(Arg.Any(Of Integer)())|]")] @@ -228,10 +230,5 @@ Dim x as Integer Dim x = sb(Arg.Any(Of Integer)()) End Sub")] public abstract Task ReportsNoDiagnostics_WhenSettingValueForProtectedInternalVirtualMember(string method, string call); - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new NonSubstitutableMemberWhenAnalyzer(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ReEntrantReturnsSetupAnalyzerTests/ReEntrantReturnsSetupDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ReEntrantReturnsSetupAnalyzerTests/ReEntrantReturnsSetupDiagnosticVerifier.cs index 4c5561ca..a5568dc4 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ReEntrantReturnsSetupAnalyzerTests/ReEntrantReturnsSetupDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ReEntrantReturnsSetupAnalyzerTests/ReEntrantReturnsSetupDiagnosticVerifier.cs @@ -14,6 +14,8 @@ public abstract class ReEntrantReturnsSetupDiagnosticVerifier : VisualBasicDiagn { protected DiagnosticDescriptor Descriptor { get; } = DiagnosticDescriptors.ReEntrantSubstituteCall; + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new ReEntrantSetupAnalyzer(); + [CombinatoryTheory] [InlineData("substitute.Foo().Returns(1)")] [InlineData("substitute.Foo().Returns(1) \n\rOtherReturn()")] @@ -130,10 +132,5 @@ Dim barr As IBar [CombinatoryTheory] [InlineData] public abstract Task ReportsNoDiagnostic_WhenRootCallCalledWithDelegateInArrayParams_AndReEntrantReturnsForAnyArgsCallExists(string method); - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new ReEntrantSetupAnalyzer(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ReEntrantReturnsSetupAnalyzerTests/ReturnsAsExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ReEntrantReturnsSetupAnalyzerTests/ReturnsAsExtensionMethodTests.cs index 456dd6b6..4fecfc5b 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ReEntrantReturnsSetupAnalyzerTests/ReturnsAsExtensionMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ReEntrantReturnsSetupAnalyzerTests/ReturnsAsExtensionMethodTests.cs @@ -48,7 +48,7 @@ End Namespace $"{method}() is set with a method that itself calls Returns. This can cause problems with NSubstitute. Consider replacing with a lambda: {method}(Function(x) OtherReturn())." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -93,7 +93,7 @@ End Namespace $"{method}() is set with a method that itself calls ReturnsForAnyArgs. This can cause problems with NSubstitute. Consider replacing with a lambda: {method}(Function(x) OtherReturn())." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -137,7 +137,7 @@ End Namespace $"{method}() is set with a method that itself calls Do. This can cause problems with NSubstitute. Consider replacing with a lambda: {method}(Function(x) OtherReturn())." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -193,7 +193,7 @@ End Namespace $"{method}() is set with a method that itself calls {method}. This can cause problems with NSubstitute. Consider replacing with a lambda: {method}(Function(x) NestedReturnThis())." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ReEntrantReturnsSetupAnalyzerTests/ReturnsAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ReEntrantReturnsSetupAnalyzerTests/ReturnsAsOrdinaryMethodTests.cs index b32c0769..c5124855 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ReEntrantReturnsSetupAnalyzerTests/ReturnsAsOrdinaryMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ReEntrantReturnsSetupAnalyzerTests/ReturnsAsOrdinaryMethodTests.cs @@ -49,7 +49,7 @@ End Namespace $"{plainMethodName}() is set with a method that itself calls Returns. This can cause problems with NSubstitute. Consider replacing with a lambda: {plainMethodName}(Function(x) OtherReturn())." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -96,7 +96,7 @@ End Namespace $"{plainMethodName}() is set with a method that itself calls ReturnsForAnyArgs. This can cause problems with NSubstitute. Consider replacing with a lambda: {plainMethodName}(Function(x) OtherReturn())." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -141,7 +141,7 @@ End Namespace $"{plainMethodName}() is set with a method that itself calls Do. This can cause problems with NSubstitute. Consider replacing with a lambda: {plainMethodName}(Function(x) OtherReturn())." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } @@ -198,7 +198,7 @@ End Namespace $"{plainMethodName}() is set with a method that itself calls {plainMethodName}. This can cause problems with NSubstitute. Consider replacing with a lambda: {plainMethodName}(Function(x) NestedReturnThis())." }; - var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span.Span, span.LineSpan)).ToArray(); + var diagnostics = textParserResult.Spans.Select((span, idx) => CreateDiagnostic(Descriptor.OverrideMessage(diagnosticMessages[idx]), span)).ToArray(); await VerifyDiagnostic(textParserResult.Text, diagnostics); } diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ReceivedInReceivedInOrderAnalyzerTests/ReceivedInReceivedInOrderDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ReceivedInReceivedInOrderAnalyzerTests/ReceivedInReceivedInOrderDiagnosticVerifier.cs index 233d1e02..4e41e51a 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ReceivedInReceivedInOrderAnalyzerTests/ReceivedInReceivedInOrderDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/ReceivedInReceivedInOrderAnalyzerTests/ReceivedInReceivedInOrderDiagnosticVerifier.cs @@ -14,6 +14,8 @@ public abstract class ReceivedInReceivedInOrderDiagnosticVerifier : VisualBasicD { protected DiagnosticDescriptor Descriptor { get; } = DiagnosticDescriptors.ReceivedUsedInReceivedInOrder; + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new ReceivedInReceivedInOrderAnalyzer(); + [CombinatoryTheory] [InlineData] public abstract Task ReportsDiagnostic_WhenUsingReceivedLikeMethodInReceivedInOrderBlock_ForMethod(string method); @@ -33,10 +35,5 @@ public abstract class ReceivedInReceivedInOrderDiagnosticVerifier : VisualBasicD [CombinatoryTheory] [InlineData] public abstract Task ReportsNoDiagnostics_WhenUsingUnfortunatelyNamedMethod(string method); - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new ReceivedInReceivedInOrderAnalyzer(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/SubstituteAnalyzerTests/SubstituteDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/SubstituteAnalyzerTests/SubstituteDiagnosticVerifier.cs index 2102c5be..052ab8b1 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/SubstituteAnalyzerTests/SubstituteDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/SubstituteAnalyzerTests/SubstituteDiagnosticVerifier.cs @@ -27,6 +27,8 @@ public abstract class SubstituteDiagnosticVerifier : VisualBasicDiagnosticVerifi protected DiagnosticDescriptor PartialSubstituteForUnsupportedTypeDescriptor { get; } = DiagnosticDescriptors.PartialSubstituteForUnsupportedType; + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new SubstituteAnalyzer(); + #pragma warning disable xUnit1013 // Public method should be marked as test [Fact] public abstract Task ReturnsDiagnostic_WhenUsedForClassWithoutPublicOrProtectedConstructor(); @@ -64,9 +66,5 @@ public abstract class SubstituteDiagnosticVerifier : VisualBasicDiagnosticVerifi public abstract Task ReturnsNoDiagnostic_WhenUsedWithGenericArgument(); #pragma warning restore xUnit1013 // Public method should be marked as test - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new SubstituteAnalyzer(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/UnusedReceivedAnalyzerTests/UnusedReceivedDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/UnusedReceivedAnalyzerTests/UnusedReceivedDiagnosticVerifier.cs index aa74cfec..4df069d0 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/UnusedReceivedAnalyzerTests/UnusedReceivedDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/UnusedReceivedAnalyzerTests/UnusedReceivedDiagnosticVerifier.cs @@ -2,7 +2,6 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; using NSubstitute.Analyzers.Shared; -using NSubstitute.Analyzers.Tests.Shared; using NSubstitute.Analyzers.Tests.Shared.DiagnosticAnalyzers; using NSubstitute.Analyzers.Tests.Shared.Extensibility; using NSubstitute.Analyzers.VisualBasic; @@ -15,6 +14,8 @@ public abstract class UnusedReceivedDiagnosticVerifier : VisualBasicDiagnosticVe { protected DiagnosticDescriptor Descriptor { get; } = DiagnosticDescriptors.UnusedReceived; + protected override DiagnosticAnalyzer DiagnosticAnalyzer { get; } = new UnusedReceivedAnalyzer(); + [CombinatoryTheory] [InlineData] public abstract Task ReportDiagnostics_WhenUsedWithoutMemberCall(string method); @@ -38,10 +39,5 @@ public abstract class UnusedReceivedDiagnosticVerifier : VisualBasicDiagnosticVe [CombinatoryTheory] [InlineData] public abstract Task ReportsNoDiagnostics_WhenUsedWithUnfortunatelyNamedMethod(string method); - - protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() - { - return new UnusedReceivedAnalyzer(); - } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/VisualBasicDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/VisualBasicDiagnosticVerifier.cs index 63ac5b7b..ad8553b0 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/VisualBasicDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/VisualBasicDiagnosticVerifier.cs @@ -1,31 +1,12 @@ -using System.Collections.Generic; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.VisualBasic; -using Microsoft.VisualBasic.CompilerServices; -using NSubstitute.Analyzers.Tests.Shared; -using NSubstitute.Analyzers.Tests.Shared.DiagnosticAnalyzers; +using NSubstitute.Analyzers.Tests.Shared.DiagnosticAnalyzers; namespace NSubstitute.Analyzers.Tests.VisualBasic.DiagnosticAnalyzersTests { public abstract class VisualBasicDiagnosticVerifier : DiagnosticVerifier { - private static readonly MetadataReference[] AdditionalReferences = + protected VisualBasicDiagnosticVerifier() + : base(VisualBasicWorkspaceFactory.Default) { - MetadataReference.CreateFromFile(typeof(StandardModuleAttribute).Assembly.Location) - }; - - protected override string Language { get; } = LanguageNames.VisualBasic; - - protected override string FileExtension { get; } = "vb"; - - protected override CompilationOptions GetCompilationOptions() - { - return new VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary, optionStrict: OptionStrict.Off); - } - - protected override IEnumerable GetAdditionalMetadataReferences() - { - return AdditionalReferences; } } } \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/VisualBasicProjectOptions.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/VisualBasicProjectOptions.cs new file mode 100644 index 00000000..ff976b9c --- /dev/null +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/VisualBasicProjectOptions.cs @@ -0,0 +1,36 @@ +using System.Collections.Immutable; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.VisualBasic; +using Microsoft.VisualBasic.CompilerServices; +using NSubstitute.Analyzers.Tests.Shared; + +namespace NSubstitute.Analyzers.Tests.VisualBasic +{ + public class VisualBasicProjectOptions : ProjectOptions + { + public static VisualBasicProjectOptions Default { get; } = new VisualBasicProjectOptions( + RuntimeMetadataReference.Default.Add(MetadataReference.CreateFromFile(typeof(StandardModuleAttribute).Assembly.Location)), + new VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); + + private VisualBasicProjectOptions( + ImmutableArray metadataReferences, + CompilationOptions compilationOptions) + : base(metadataReferences, compilationOptions) + { + } + + public VisualBasicProjectOptions WithOptionStrict(OptionStrict optionStrict) + { + var compilationOptions = (VisualBasicCompilationOptions)CompilationOptions; + + return new VisualBasicProjectOptions( + MetadataReferences, + new VisualBasicCompilationOptions(compilationOptions.OutputKind, optionStrict: optionStrict)); + } + + public VisualBasicProjectOptions WithAdditionalMetadataReferences(MetadataReference[] metadataReferences) + { + return new VisualBasicProjectOptions(MetadataReferences.AddRange(metadataReferences), CompilationOptions); + } + } +} \ No newline at end of file diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/VisualBasicWorkspaceFactory.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/VisualBasicWorkspaceFactory.cs new file mode 100644 index 00000000..6f4e322f --- /dev/null +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/VisualBasicWorkspaceFactory.cs @@ -0,0 +1,39 @@ +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.VisualBasic; +using NSubstitute.Analyzers.Tests.Shared; + +namespace NSubstitute.Analyzers.Tests.VisualBasic +{ + public class VisualBasicWorkspaceFactory : WorkspaceFactory + { + public static VisualBasicWorkspaceFactory Default { get; } = new VisualBasicWorkspaceFactory(VisualBasicProjectOptions.Default); + + protected override string DocumentExtension { get; } = "vb"; + + protected override string Language { get; } = LanguageNames.VisualBasic; + + protected override ProjectOptions ProjectOptions { get; } + + private VisualBasicProjectOptions VisualBasicProjectOptions => (VisualBasicProjectOptions)ProjectOptions; + + private VisualBasicWorkspaceFactory(ProjectOptions projectOptions) + { + ProjectOptions = projectOptions; + } + + public VisualBasicWorkspaceFactory WithOptionStrictOn() + { + return WithOptionStrict(OptionStrict.On); + } + + public VisualBasicWorkspaceFactory WithOptionStrict(OptionStrict optionStrict) + { + return new VisualBasicWorkspaceFactory(VisualBasicProjectOptions.WithOptionStrict(optionStrict)); + } + + public VisualBasicWorkspaceFactory WithAdditionalMetadataReferences(MetadataReference[] metadataReferences) + { + return new VisualBasicWorkspaceFactory(VisualBasicProjectOptions.WithAdditionalMetadataReferences(metadataReferences)); + } + } +} \ No newline at end of file