diff --git a/src/NSubstitute.Analyzers.Shared/DiagnosticAnalyzers/SubstituteConstructorAnalysis.cs b/src/NSubstitute.Analyzers.Shared/DiagnosticAnalyzers/SubstituteConstructorAnalysis.cs index 7c623a06..58d9f980 100644 --- a/src/NSubstitute.Analyzers.Shared/DiagnosticAnalyzers/SubstituteConstructorAnalysis.cs +++ b/src/NSubstitute.Analyzers.Shared/DiagnosticAnalyzers/SubstituteConstructorAnalysis.cs @@ -93,22 +93,13 @@ private IMethodSymbol[] GetAccessibleConstructors(ITypeSymbol genericArgument) { var internalsVisibleToProxy = genericArgument.InternalsVisibleToProxyGenerator(); - bool IsAccessible(IMethodSymbol symbol) => symbol.DeclaredAccessibility is Accessibility.Protected or Accessibility.Public; - - bool IsVisibleToProxy(IMethodSymbol symbol) - { - if (internalsVisibleToProxy == false) - { - return false; - } - - return symbol.DeclaredAccessibility is Accessibility.Internal or Accessibility.ProtectedOrInternal; - } - return genericArgument.GetMembers().OfType().Where(symbol => symbol.MethodKind == MethodKind.Constructor && symbol.IsStatic == false && - (IsAccessible(symbol) || IsVisibleToProxy(symbol))).ToArray(); + (symbol.DeclaredAccessibility == Accessibility.Protected || + symbol.DeclaredAccessibility == Accessibility.Public || + symbol.DeclaredAccessibility == Accessibility.ProtectedOrInternal || + (internalsVisibleToProxy && symbol.DeclaredAccessibility == Accessibility.Internal))).ToArray(); } private ITypeSymbol[] GetTypeSymbols(IArrayCreationOperation arrayInitializerOperation) diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/SubstituteAnalyzerTests/ForAsGenericMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/SubstituteAnalyzerTests/ForAsGenericMethodTests.cs index 6c93c706..e914417c 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/SubstituteAnalyzerTests/ForAsGenericMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/SubstituteAnalyzerTests/ForAsGenericMethodTests.cs @@ -274,7 +274,7 @@ public void Test() await VerifyDiagnostic(source, SubstituteForWithoutAccessibleConstructorDescriptor, "Could not find accessible constructor. Make sure that type MyNamespace.Foo exposes public or protected constructors."); } - public override async Task ReportsDiagnostic_WhenUsedForClassWithProtectedInternalConstructor_AndInternalsVisibleToNotApplied() + public override async Task ReportsNoDiagnostics_WhenUsedForClassWithProtectedInternalConstructor_AndInternalsVisibleToNotApplied() { var source = @"using NSubstitute; @@ -291,11 +291,11 @@ public class FooTests { public void Test() { - var substitute = [|NSubstitute.Substitute.For()|]; + var substitute = NSubstitute.Substitute.For(); } } }"; - await VerifyDiagnostic(source, SubstituteForWithoutAccessibleConstructorDescriptor, "Could not find accessible constructor. Make sure that type MyNamespace.Foo exposes public or protected constructors."); + await this.VerifyNoDiagnostic(source); } public override async Task ReportsNoDiagnostic_WhenUsedForClassWithInternalConstructor_AndInternalsVisibleToApplied() @@ -350,6 +350,30 @@ public void Test() await VerifyNoDiagnostic(source); } + public override async Task ReportsNoDiagnostic_WhenUsedForClassWithProtectedConstructor() + { + var source = @"using NSubstitute; + +namespace MyNamespace +{ + public class Foo + { + protected Foo() + { + } + } + + public class FooTests + { + public void Test() + { + var substitute = NSubstitute.Substitute.For(); + } + } +}"; + await VerifyNoDiagnostic(source); + } + [Fact] public override async Task ReportsDiagnostic_WhenPassedParametersCount_GreaterThanCtorParametersCount() { diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/SubstituteAnalyzerTests/ForAsNonGenericMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/SubstituteAnalyzerTests/ForAsNonGenericMethodTests.cs index a0a0301e..1334d742 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/SubstituteAnalyzerTests/ForAsNonGenericMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/SubstituteAnalyzerTests/ForAsNonGenericMethodTests.cs @@ -370,7 +370,7 @@ public void Test() await VerifyDiagnostic(source, SubstituteForWithoutAccessibleConstructorDescriptor, "Could not find accessible constructor. Make sure that type MyNamespace.Foo exposes public or protected constructors."); } - public override async Task ReportsDiagnostic_WhenUsedForClassWithProtectedInternalConstructor_AndInternalsVisibleToNotApplied() + public override async Task ReportsNoDiagnostics_WhenUsedForClassWithProtectedInternalConstructor_AndInternalsVisibleToNotApplied() { var source = @"using NSubstitute; @@ -387,13 +387,13 @@ public class FooTests { public void Test() { - var substitute = [|NSubstitute.Substitute.For(new [] { typeof(Foo) }, null)|]; - var otherSubstitute = [|NSubstitute.Substitute.For(typesToProxy: new [] { typeof(Foo) }, constructorArguments: null)|]; - var yetAnotherSubstitute = [|NSubstitute.Substitute.For(constructorArguments: null, typesToProxy: new [] { typeof(Foo) })|]; + var substitute = NSubstitute.Substitute.For(new [] { typeof(Foo) }, null); + var otherSubstitute = NSubstitute.Substitute.For(typesToProxy: new [] { typeof(Foo) }, constructorArguments: null); + var yetAnotherSubstitute = NSubstitute.Substitute.For(constructorArguments: null, typesToProxy: new [] { typeof(Foo) }); } } }"; - await VerifyDiagnostic(source, SubstituteForWithoutAccessibleConstructorDescriptor, "Could not find accessible constructor. Make sure that type MyNamespace.Foo exposes public or protected constructors."); + await VerifyNoDiagnostic(source); } public override async Task ReportsNoDiagnostic_WhenUsedForClassWithInternalConstructor_AndInternalsVisibleToApplied() @@ -452,6 +452,32 @@ public void Test() await VerifyNoDiagnostic(source); } + public override async Task ReportsNoDiagnostic_WhenUsedForClassWithProtectedConstructor() + { + var source = @"using NSubstitute; + +namespace MyNamespace +{ + public class Foo + { + protected Foo() + { + } + } + + public class FooTests + { + public void Test() + { + var substitute = NSubstitute.Substitute.For(new [] { typeof(Foo) }, null); + var otherSubstitute = NSubstitute.Substitute.For(typesToProxy: new [] { typeof(Foo) }, constructorArguments: null); + var yetAnotherSubstitute = NSubstitute.Substitute.For(constructorArguments: null, typesToProxy: new [] { typeof(Foo) }); + } + } +}"; + await VerifyNoDiagnostic(source); + } + [Fact] public override async Task ReportsDiagnostic_WhenPassedParametersCount_GreaterThanCtorParametersCount() { diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/SubstituteAnalyzerTests/ForPartsOfMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/SubstituteAnalyzerTests/ForPartsOfMethodTests.cs index 04448137..4aa27f45 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/SubstituteAnalyzerTests/ForPartsOfMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/SubstituteAnalyzerTests/ForPartsOfMethodTests.cs @@ -98,7 +98,7 @@ public void Test() await VerifyDiagnostic(source, SubstituteForWithoutAccessibleConstructorDescriptor, "Could not find accessible constructor. Make sure that type MyNamespace.Foo exposes public or protected constructors."); } - public override async Task ReportsDiagnostic_WhenUsedForClassWithProtectedInternalConstructor_AndInternalsVisibleToNotApplied() + public override async Task ReportsNoDiagnostics_WhenUsedForClassWithProtectedInternalConstructor_AndInternalsVisibleToNotApplied() { var source = @"using NSubstitute; @@ -115,11 +115,11 @@ public class FooTests { public void Test() { - var substitute = [|NSubstitute.Substitute.ForPartsOf()|]; + var substitute = NSubstitute.Substitute.ForPartsOf(); } } }"; - await VerifyDiagnostic(source, SubstituteForWithoutAccessibleConstructorDescriptor, "Could not find accessible constructor. Make sure that type MyNamespace.Foo exposes public or protected constructors."); + await VerifyNoDiagnostic(source); } public override async Task ReportsNoDiagnostic_WhenUsedForClassWithInternalConstructor_AndInternalsVisibleToApplied() @@ -176,6 +176,30 @@ public void Test() await VerifyNoDiagnostic(source); } + public override async Task ReportsNoDiagnostic_WhenUsedForClassWithProtectedConstructor() + { + var source = @"using NSubstitute; + +namespace MyNamespace +{ + public class Foo + { + protected Foo() + { + } + } + + public class FooTests + { + public void Test() + { + var substitute = NSubstitute.Substitute.ForPartsOf(); + } + } +}"; + await VerifyNoDiagnostic(source); + } + [Fact] public override async Task ReportsDiagnostic_WhenPassedParametersCount_GreaterThanCtorParametersCount() { diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/SubstituteAnalyzerTests/SubstituteDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/SubstituteAnalyzerTests/SubstituteDiagnosticVerifier.cs index 740741bf..dc0d5885 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/SubstituteAnalyzerTests/SubstituteDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/SubstituteAnalyzerTests/SubstituteDiagnosticVerifier.cs @@ -37,7 +37,7 @@ public abstract class SubstituteDiagnosticVerifier : CSharpDiagnosticVerifier, I public abstract Task ReportsDiagnostic_WhenUsedForClassWithInternalConstructor_AndInternalsVisibleToNotApplied(); [Fact] - public abstract Task ReportsDiagnostic_WhenUsedForClassWithProtectedInternalConstructor_AndInternalsVisibleToNotApplied(); + public abstract Task ReportsNoDiagnostics_WhenUsedForClassWithProtectedInternalConstructor_AndInternalsVisibleToNotApplied(); [Fact] public abstract Task ReportsNoDiagnostic_WhenUsedForClassWithInternalConstructor_AndInternalsVisibleToApplied(); @@ -45,6 +45,9 @@ public abstract class SubstituteDiagnosticVerifier : CSharpDiagnosticVerifier, I [Fact] public abstract Task ReportsNoDiagnostic_WhenUsedForClassWithProtectedInternalConstructor_AndInternalsVisibleToApplied(); + [Fact] + public abstract Task ReportsNoDiagnostic_WhenUsedForClassWithProtectedConstructor(); + [Fact] public abstract Task ReportsDiagnostic_WhenPassedParametersCount_GreaterThanCtorParametersCount(); diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/SubstituteAnalyzerTests/SubstituteFactoryCreateMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/SubstituteAnalyzerTests/SubstituteFactoryCreateMethodTests.cs index 352bec88..2fa73fc0 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/SubstituteAnalyzerTests/SubstituteFactoryCreateMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/SubstituteAnalyzerTests/SubstituteFactoryCreateMethodTests.cs @@ -323,7 +323,7 @@ public void Test() await VerifyDiagnostic(source, SubstituteForWithoutAccessibleConstructorDescriptor, "Could not find accessible constructor. Make sure that type MyNamespace.Foo exposes public or protected constructors."); } - public override async Task ReportsDiagnostic_WhenUsedForClassWithProtectedInternalConstructor_AndInternalsVisibleToNotApplied() + public override async Task ReportsNoDiagnostics_WhenUsedForClassWithProtectedInternalConstructor_AndInternalsVisibleToNotApplied() { var source = @"using NSubstitute.Core; @@ -340,13 +340,13 @@ public class FooTests { public void Test() { - var substitute = [|SubstitutionContext.Current.SubstituteFactory.Create(new[] {typeof(Foo)}, null)|]; - var otherSubstitute = [|SubstitutionContext.Current.SubstituteFactory.Create(typesToProxy: new[] {typeof(Foo)}, constructorArguments: null)|]; - var yetAnotherSubstitute = [|SubstitutionContext.Current.SubstituteFactory.Create(constructorArguments: null, typesToProxy: new[] {typeof(Foo)})|]; + var substitute = SubstitutionContext.Current.SubstituteFactory.Create(new[] {typeof(Foo)}, null); + var otherSubstitute = SubstitutionContext.Current.SubstituteFactory.Create(typesToProxy: new[] {typeof(Foo)}, constructorArguments: null); + var yetAnotherSubstitute = SubstitutionContext.Current.SubstituteFactory.Create(constructorArguments: null, typesToProxy: new[] {typeof(Foo)}); } } }"; - await VerifyDiagnostic(source, SubstituteForWithoutAccessibleConstructorDescriptor, "Could not find accessible constructor. Make sure that type MyNamespace.Foo exposes public or protected constructors."); + await VerifyNoDiagnostic(source); } public override async Task ReportsNoDiagnostic_WhenUsedForClassWithInternalConstructor_AndInternalsVisibleToApplied() @@ -405,6 +405,32 @@ public void Test() await VerifyNoDiagnostic(source); } + public override async Task ReportsNoDiagnostic_WhenUsedForClassWithProtectedConstructor() + { + var source = @"using NSubstitute.Core; + +namespace MyNamespace +{ + public class Foo + { + protected Foo() + { + } + } + + public class FooTests + { + public void Test() + { + var substitute = SubstitutionContext.Current.SubstituteFactory.Create(new[] {typeof(Foo)}, null); + var otherSubstitute = SubstitutionContext.Current.SubstituteFactory.Create(typesToProxy: new[] {typeof(Foo)}, constructorArguments: null); + var yetAnotherSubstitute = SubstitutionContext.Current.SubstituteFactory.Create(constructorArguments: null, typesToProxy: new[] {typeof(Foo)}); + } + } +}"; + await VerifyNoDiagnostic(source); + } + [Fact] public override async Task ReportsDiagnostic_WhenPassedParametersCount_GreaterThanCtorParametersCount() { diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/SubstituteAnalyzerTests/SubstituteFactoryCreatePartialMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/SubstituteAnalyzerTests/SubstituteFactoryCreatePartialMethodTests.cs index eb047de1..523e0a27 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/SubstituteAnalyzerTests/SubstituteFactoryCreatePartialMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/SubstituteAnalyzerTests/SubstituteFactoryCreatePartialMethodTests.cs @@ -132,7 +132,7 @@ public void Test() await VerifyDiagnostic(source, SubstituteForWithoutAccessibleConstructorDescriptor, "Could not find accessible constructor. Make sure that type MyNamespace.Foo exposes public or protected constructors."); } - public override async Task ReportsDiagnostic_WhenUsedForClassWithProtectedInternalConstructor_AndInternalsVisibleToNotApplied() + public override async Task ReportsNoDiagnostics_WhenUsedForClassWithProtectedInternalConstructor_AndInternalsVisibleToNotApplied() { var source = @"using System; using NSubstitute.Core; @@ -150,13 +150,13 @@ public class FooTests { public void Test() { - var substitute = [|SubstitutionContext.Current.SubstituteFactory.CreatePartial(new Type[] { typeof(Foo)}, null)|]; - var otherSubstitute = [|SubstitutionContext.Current.SubstituteFactory.CreatePartial(typesToProxy: new Type[] { typeof(Foo)}, constructorArguments: null)|]; - var yetAnotherSubstitute = [|SubstitutionContext.Current.SubstituteFactory.CreatePartial(constructorArguments: null, typesToProxy: new Type[] { typeof(Foo)})|]; + var substitute = SubstitutionContext.Current.SubstituteFactory.CreatePartial(new Type[] { typeof(Foo)}, null); + var otherSubstitute = SubstitutionContext.Current.SubstituteFactory.CreatePartial(typesToProxy: new Type[] { typeof(Foo)}, constructorArguments: null); + var yetAnotherSubstitute = SubstitutionContext.Current.SubstituteFactory.CreatePartial(constructorArguments: null, typesToProxy: new Type[] { typeof(Foo)}); } } }"; - await VerifyDiagnostic(source, SubstituteForWithoutAccessibleConstructorDescriptor, "Could not find accessible constructor. Make sure that type MyNamespace.Foo exposes public or protected constructors."); + await VerifyNoDiagnostic(source); } public override async Task ReportsNoDiagnostic_WhenUsedForClassWithInternalConstructor_AndInternalsVisibleToApplied() @@ -217,6 +217,33 @@ public void Test() await VerifyNoDiagnostic(source); } + public override async Task ReportsNoDiagnostic_WhenUsedForClassWithProtectedConstructor() + { + var source = @"using System; +using NSubstitute.Core; + +namespace MyNamespace +{ + public class Foo + { + protected Foo() + { + } + } + + public class FooTests + { + public void Test() + { + var substitute = SubstitutionContext.Current.SubstituteFactory.CreatePartial(new Type[] { typeof(Foo)}, null); + var otherSubstitute = SubstitutionContext.Current.SubstituteFactory.CreatePartial(typesToProxy: new Type[] { typeof(Foo)}, constructorArguments: null); + var yetAnotherSubstitute = SubstitutionContext.Current.SubstituteFactory.CreatePartial(constructorArguments: null, typesToProxy: new Type[] { typeof(Foo)}); + } + } +}"; + await VerifyNoDiagnostic(source); + } + [Fact] public override async Task ReportsDiagnostic_WhenPassedParametersCount_GreaterThanCtorParametersCount() { diff --git a/tests/NSubstitute.Analyzers.Tests.Shared/DiagnosticAnalyzers/ISubstituteAnalyzerVerifier.cs b/tests/NSubstitute.Analyzers.Tests.Shared/DiagnosticAnalyzers/ISubstituteAnalyzerVerifier.cs index aaa47603..273c160d 100644 --- a/tests/NSubstitute.Analyzers.Tests.Shared/DiagnosticAnalyzers/ISubstituteAnalyzerVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.Shared/DiagnosticAnalyzers/ISubstituteAnalyzerVerifier.cs @@ -8,12 +8,14 @@ public interface ISubstituteAnalyzerVerifier Task ReportsDiagnostic_WhenUsedForClassWithInternalConstructor_AndInternalsVisibleToNotApplied(); - Task ReportsDiagnostic_WhenUsedForClassWithProtectedInternalConstructor_AndInternalsVisibleToNotApplied(); + Task ReportsNoDiagnostics_WhenUsedForClassWithProtectedInternalConstructor_AndInternalsVisibleToNotApplied(); Task ReportsNoDiagnostic_WhenUsedForClassWithInternalConstructor_AndInternalsVisibleToApplied(); Task ReportsNoDiagnostic_WhenUsedForClassWithProtectedInternalConstructor_AndInternalsVisibleToApplied(); + Task ReportsNoDiagnostic_WhenUsedForClassWithProtectedConstructor(); + Task ReportsDiagnostic_WhenPassedParametersCount_GreaterThanCtorParametersCount(); Task ReportsDiagnostic_WhenPassedParametersCount_LessThanCtorParametersCount(); diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/SubstituteAnalyzerTests/ForAsGenericMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/SubstituteAnalyzerTests/ForAsGenericMethodTests.cs index 53e3d2a7..b0dccf1e 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/SubstituteAnalyzerTests/ForAsGenericMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/SubstituteAnalyzerTests/ForAsGenericMethodTests.cs @@ -233,7 +233,7 @@ End Namespace await VerifyDiagnostic(source, SubstituteForWithoutAccessibleConstructorDescriptor, "Could not find accessible constructor. Make sure that type MyNamespace.Foo exposes public or protected constructors."); } - public override async Task ReportsDiagnostic_WhenUsedForClassWithProtectedInternalConstructor_AndInternalsVisibleToNotApplied() + public override async Task ReportsNoDiagnostics_WhenUsedForClassWithProtectedInternalConstructor_AndInternalsVisibleToNotApplied() { var source = @"Imports NSubstitute @@ -245,12 +245,12 @@ End Class Public Class FooTests Public Sub Test() - Dim substitute = [|NSubstitute.Substitute.[For](Of Foo)()|] + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() End Sub End Class End Namespace "; - await VerifyDiagnostic(source, SubstituteForWithoutAccessibleConstructorDescriptor, "Could not find accessible constructor. Make sure that type MyNamespace.Foo exposes public or protected constructors."); + await VerifyNoDiagnostic(source); } public override async Task ReportsNoDiagnostic_WhenUsedForClassWithInternalConstructor_AndInternalsVisibleToApplied() @@ -297,6 +297,26 @@ End Namespace await VerifyNoDiagnostic(source); } + public override async Task ReportsNoDiagnostic_WhenUsedForClassWithProtectedConstructor() + { + var source = @"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Protected Sub New() + End Sub + End Class + + Public Class FooTests + Public Sub Test() + Dim substitute = NSubstitute.Substitute.[For](Of Foo)() + End Sub + End Class +End Namespace +"; + await VerifyNoDiagnostic(source); + } + public override async Task ReportsDiagnostic_WhenPassedParametersCount_GreaterThanCtorParametersCount() { var source = @"Imports NSubstitute diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/SubstituteAnalyzerTests/ForAsNonGenericMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/SubstituteAnalyzerTests/ForAsNonGenericMethodTests.cs index a7cc53f0..f5886b46 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/SubstituteAnalyzerTests/ForAsNonGenericMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/SubstituteAnalyzerTests/ForAsNonGenericMethodTests.cs @@ -326,7 +326,7 @@ End Namespace await VerifyDiagnostic(source, SubstituteForWithoutAccessibleConstructorDescriptor, "Could not find accessible constructor. Make sure that type MyNamespace.Foo exposes public or protected constructors."); } - public override async Task ReportsDiagnostic_WhenUsedForClassWithProtectedInternalConstructor_AndInternalsVisibleToNotApplied() + public override async Task ReportsNoDiagnostics_WhenUsedForClassWithProtectedInternalConstructor_AndInternalsVisibleToNotApplied() { var source = @"Imports NSubstitute @@ -338,14 +338,14 @@ End Class Public Class FooTests Public Sub Test() - Dim substitute = [|NSubstitute.Substitute.[For]({GetType(Foo)}, Nothing)|] - Dim otherSubstitute = [|NSubstitute.Substitute.[For](typesToProxy:= {GetType(Foo)}, constructorArguments:= Nothing)|] - Dim yetAnotherSubstitute = [|NSubstitute.Substitute.[For](constructorArguments:= Nothing, typesToProxy:= {GetType(Foo)})|] + Dim substitute = NSubstitute.Substitute.[For]({GetType(Foo)}, Nothing) + Dim otherSubstitute = NSubstitute.Substitute.[For](typesToProxy:= {GetType(Foo)}, constructorArguments:= Nothing) + Dim yetAnotherSubstitute = NSubstitute.Substitute.[For](constructorArguments:= Nothing, typesToProxy:= {GetType(Foo)}) End Sub End Class End Namespace "; - await VerifyDiagnostic(source, SubstituteForWithoutAccessibleConstructorDescriptor, "Could not find accessible constructor. Make sure that type MyNamespace.Foo exposes public or protected constructors."); + await VerifyNoDiagnostic(source); } public override async Task ReportsNoDiagnostic_WhenUsedForClassWithInternalConstructor_AndInternalsVisibleToApplied() @@ -396,6 +396,28 @@ End Namespace await VerifyNoDiagnostic(source); } + public override async Task ReportsNoDiagnostic_WhenUsedForClassWithProtectedConstructor() + { + var source = @"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Protected Sub New() + End Sub + End Class + + Public Class FooTests + Public Sub Test() + Dim substitute = NSubstitute.Substitute.[For]({GetType(Foo)}, Nothing) + Dim otherSubstitute = NSubstitute.Substitute.[For](typesToProxy:= {GetType(Foo)}, constructorArguments:= Nothing) + Dim yetAnotherSubstitute = NSubstitute.Substitute.[For](constructorArguments:= Nothing, typesToProxy:= {GetType(Foo)}) + End Sub + End Class +End Namespace +"; + await VerifyNoDiagnostic(source); + } + public override async Task ReportsDiagnostic_WhenPassedParametersCount_GreaterThanCtorParametersCount() { var source = @"Imports NSubstitute diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/SubstituteAnalyzerTests/ForPartsOfMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/SubstituteAnalyzerTests/ForPartsOfMethodTests.cs index 2ff7937d..137bfed2 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/SubstituteAnalyzerTests/ForPartsOfMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/SubstituteAnalyzerTests/ForPartsOfMethodTests.cs @@ -84,7 +84,7 @@ End Namespace await VerifyDiagnostic(source, SubstituteForWithoutAccessibleConstructorDescriptor, "Could not find accessible constructor. Make sure that type MyNamespace.Foo exposes public or protected constructors."); } - public override async Task ReportsDiagnostic_WhenUsedForClassWithProtectedInternalConstructor_AndInternalsVisibleToNotApplied() + public override async Task ReportsNoDiagnostics_WhenUsedForClassWithProtectedInternalConstructor_AndInternalsVisibleToNotApplied() { var source = @"Imports NSubstitute @@ -96,12 +96,12 @@ End Class Public Class FooTests Public Sub Test() - Dim substitute = [|NSubstitute.Substitute.ForPartsOf(Of Foo)()|] + Dim substitute = NSubstitute.Substitute.ForPartsOf(Of Foo)() End Sub End Class End Namespace "; - await VerifyDiagnostic(source, SubstituteForWithoutAccessibleConstructorDescriptor, "Could not find accessible constructor. Make sure that type MyNamespace.Foo exposes public or protected constructors."); + await VerifyNoDiagnostic(source); } public override async Task ReportsNoDiagnostic_WhenUsedForClassWithInternalConstructor_AndInternalsVisibleToApplied() @@ -148,6 +148,26 @@ End Namespace await VerifyNoDiagnostic(source); } + public override async Task ReportsNoDiagnostic_WhenUsedForClassWithProtectedConstructor() + { + var source = @"Imports NSubstitute + +Namespace MyNamespace + Public Class Foo + Protected Sub New() + End Sub + End Class + + Public Class FooTests + Public Sub Test() + Dim substitute = NSubstitute.Substitute.ForPartsOf(Of Foo)() + End Sub + End Class +End Namespace +"; + await VerifyNoDiagnostic(source); + } + public override async Task ReportsDiagnostic_WhenPassedParametersCount_GreaterThanCtorParametersCount() { var source = @"Imports NSubstitute diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/SubstituteAnalyzerTests/SubstituteDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/SubstituteAnalyzerTests/SubstituteDiagnosticVerifier.cs index 5afce42f..a2e1df1a 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/SubstituteAnalyzerTests/SubstituteDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/SubstituteAnalyzerTests/SubstituteDiagnosticVerifier.cs @@ -37,7 +37,7 @@ public abstract class SubstituteDiagnosticVerifier : VisualBasicDiagnosticVerifi public abstract Task ReportsDiagnostic_WhenUsedForClassWithInternalConstructor_AndInternalsVisibleToNotApplied(); [Fact] - public abstract Task ReportsDiagnostic_WhenUsedForClassWithProtectedInternalConstructor_AndInternalsVisibleToNotApplied(); + public abstract Task ReportsNoDiagnostics_WhenUsedForClassWithProtectedInternalConstructor_AndInternalsVisibleToNotApplied(); [Fact] public abstract Task ReportsNoDiagnostic_WhenUsedForClassWithInternalConstructor_AndInternalsVisibleToApplied(); @@ -45,6 +45,9 @@ public abstract class SubstituteDiagnosticVerifier : VisualBasicDiagnosticVerifi [Fact] public abstract Task ReportsNoDiagnostic_WhenUsedForClassWithProtectedInternalConstructor_AndInternalsVisibleToApplied(); + [Fact] + public abstract Task ReportsNoDiagnostic_WhenUsedForClassWithProtectedConstructor(); + [Fact] public abstract Task ReportsDiagnostic_WhenPassedParametersCount_GreaterThanCtorParametersCount(); diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/SubstituteAnalyzerTests/SubstituteFactoryCreateMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/SubstituteAnalyzerTests/SubstituteFactoryCreateMethodTests.cs index f3a76505..c419b93e 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/SubstituteAnalyzerTests/SubstituteFactoryCreateMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/SubstituteAnalyzerTests/SubstituteFactoryCreateMethodTests.cs @@ -281,7 +281,7 @@ End Namespace await VerifyDiagnostic(source, SubstituteForWithoutAccessibleConstructorDescriptor, "Could not find accessible constructor. Make sure that type MyNamespace.Foo exposes public or protected constructors."); } - public override async Task ReportsDiagnostic_WhenUsedForClassWithProtectedInternalConstructor_AndInternalsVisibleToNotApplied() + public override async Task ReportsNoDiagnostics_WhenUsedForClassWithProtectedInternalConstructor_AndInternalsVisibleToNotApplied() { var source = @"Imports NSubstitute.Core @@ -293,14 +293,14 @@ End Class Public Class FooTests Public Sub Test() - Dim substitute = [|SubstitutionContext.Current.SubstituteFactory.Create({GetType(Foo)}, Nothing)|] - Dim otherSubstitute = [|SubstitutionContext.Current.SubstituteFactory.Create(typesToProxy:= {GetType(Foo)}, constructorArguments:= Nothing)|] - Dim yetAnotherSubstitute = [|SubstitutionContext.Current.SubstituteFactory.Create(constructorArguments:= Nothing, typesToProxy:= {GetType(Foo)})|] + Dim substitute = SubstitutionContext.Current.SubstituteFactory.Create({GetType(Foo)}, Nothing) + Dim otherSubstitute = SubstitutionContext.Current.SubstituteFactory.Create(typesToProxy:= {GetType(Foo)}, constructorArguments:= Nothing) + Dim yetAnotherSubstitute = SubstitutionContext.Current.SubstituteFactory.Create(constructorArguments:= Nothing, typesToProxy:= {GetType(Foo)}) End Sub End Class End Namespace "; - await VerifyDiagnostic(source, SubstituteForWithoutAccessibleConstructorDescriptor, "Could not find accessible constructor. Make sure that type MyNamespace.Foo exposes public or protected constructors."); + await VerifyNoDiagnostic(source); } public override async Task ReportsNoDiagnostic_WhenUsedForClassWithInternalConstructor_AndInternalsVisibleToApplied() @@ -351,6 +351,28 @@ End Namespace await VerifyNoDiagnostic(source); } + public override async Task ReportsNoDiagnostic_WhenUsedForClassWithProtectedConstructor() + { + var source = @"Imports NSubstitute.Core + +Namespace MyNamespace + Public Class Foo + Protected Sub New() + End Sub + End Class + + Public Class FooTests + Public Sub Test() + Dim substitute = SubstitutionContext.Current.SubstituteFactory.Create({GetType(Foo)}, Nothing) + Dim otherSubstitute = SubstitutionContext.Current.SubstituteFactory.Create(typesToProxy:= {GetType(Foo)}, constructorArguments:= Nothing) + Dim yetAnotherSubstitute = SubstitutionContext.Current.SubstituteFactory.Create(constructorArguments:= Nothing, typesToProxy:= {GetType(Foo)}) + End Sub + End Class +End Namespace +"; + await VerifyNoDiagnostic(source); + } + [Fact] public override async Task ReportsDiagnostic_WhenPassedParametersCount_GreaterThanCtorParametersCount() { diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/SubstituteAnalyzerTests/SubstituteFactoryCreatePartialMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/SubstituteAnalyzerTests/SubstituteFactoryCreatePartialMethodTests.cs index 923ee2ac..4e59d1fc 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/SubstituteAnalyzerTests/SubstituteFactoryCreatePartialMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/SubstituteAnalyzerTests/SubstituteFactoryCreatePartialMethodTests.cs @@ -118,7 +118,7 @@ End Namespace await VerifyDiagnostic(source, SubstituteForWithoutAccessibleConstructorDescriptor, "Could not find accessible constructor. Make sure that type MyNamespace.Foo exposes public or protected constructors."); } - public override async Task ReportsDiagnostic_WhenUsedForClassWithProtectedInternalConstructor_AndInternalsVisibleToNotApplied() + public override async Task ReportsNoDiagnostics_WhenUsedForClassWithProtectedInternalConstructor_AndInternalsVisibleToNotApplied() { var source = @"Imports System Imports NSubstitute.Core @@ -131,14 +131,14 @@ End Class Public Class FooTests Public Sub Test() - Dim substitute = [|SubstitutionContext.Current.SubstituteFactory.CreatePartial(New Type() {GetType(Foo)}, Nothing)|] - Dim otherSubstitute = [|SubstitutionContext.Current.SubstituteFactory.CreatePartial(typesToProxy:= New Type() {GetType(Foo)}, constructorArguments:= Nothing)|] - Dim yetAnotherSubstitute = [|SubstitutionContext.Current.SubstituteFactory.CreatePartial(constructorArguments:= Nothing, typesToProxy:= New Type() {GetType(Foo)})|] + Dim substitute = SubstitutionContext.Current.SubstituteFactory.CreatePartial(New Type() {GetType(Foo)}, Nothing) + Dim otherSubstitute = SubstitutionContext.Current.SubstituteFactory.CreatePartial(typesToProxy:= New Type() {GetType(Foo)}, constructorArguments:= Nothing) + Dim yetAnotherSubstitute = SubstitutionContext.Current.SubstituteFactory.CreatePartial(constructorArguments:= Nothing, typesToProxy:= New Type() {GetType(Foo)}) End Sub End Class End Namespace "; - await VerifyDiagnostic(source, SubstituteForWithoutAccessibleConstructorDescriptor, "Could not find accessible constructor. Make sure that type MyNamespace.Foo exposes public or protected constructors."); + await VerifyNoDiagnostic(source); } public override async Task ReportsNoDiagnostic_WhenUsedForClassWithInternalConstructor_AndInternalsVisibleToApplied() @@ -191,6 +191,29 @@ End Namespace await VerifyNoDiagnostic(source); } + public override async Task ReportsNoDiagnostic_WhenUsedForClassWithProtectedConstructor() + { + var source = @"Imports System +Imports NSubstitute.Core + +Namespace MyNamespace + Public Class Foo + Protected Sub New() + End Sub + End Class + + Public Class FooTests + Public Sub Test() + Dim substitute = SubstitutionContext.Current.SubstituteFactory.CreatePartial(New Type() {GetType(Foo)}, Nothing) + Dim otherSubstitute = SubstitutionContext.Current.SubstituteFactory.CreatePartial(typesToProxy:= New Type() {GetType(Foo)}, constructorArguments:= Nothing) + Dim yetAnotherSubstitute = SubstitutionContext.Current.SubstituteFactory.CreatePartial(constructorArguments:= Nothing, typesToProxy:= New Type() {GetType(Foo)}) + End Sub + End Class +End Namespace +"; + await VerifyNoDiagnostic(source); + } + [Fact] public override async Task ReportsDiagnostic_WhenPassedParametersCount_GreaterThanCtorParametersCount() {