From ee75ee2547bdb044803c3bdce2e40152fca202bc Mon Sep 17 00:00:00 2001 From: tomasz-podolak Date: Thu, 21 Jul 2022 17:17:59 +0200 Subject: [PATCH] GH-186 - NonSubstitableMemberAnalyzer support for ThrowsAsync like methods --- .../ThrowsAsExtensionMethodTests.cs | 230 +++++++++------- ...sionMethodWithGenericTypeSpecifiedTests.cs | 230 +++++++++------- .../ThrowsAsOrdinaryMethodTests.cs | 238 ++++++++++------- ...naryMethodWithGenericTypeSpecifiedTests.cs | 238 ++++++++++------- .../ThrowsAsExtensionMethodTests.cs | 245 ++++++++++-------- .../ThrowsAsOrdinaryMethodTests.cs | 6 +- ...naryMethodWithGenericTypeSpecifiedTests.cs | 6 +- 7 files changed, 709 insertions(+), 484 deletions(-) diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsExtensionMethodTests.cs index 837c1edb..315b1e5f 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsExtensionMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsExtensionMethodTests.cs @@ -6,12 +6,13 @@ namespace NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.NonSubstitutableMemberAnalyzerTests; -[CombinatoryData("Throws", "ThrowsForAnyArgs")] +[CombinatoryData("Throws", "ThrowsAsync", "ThrowsForAnyArgs", "ThrowsAsyncForAnyArgs")] public class ThrowsAsExtensionMethodTests : NonSubstitutableMemberDiagnosticVerifier { public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -19,9 +20,9 @@ namespace MyNamespace {{ public class Foo {{ - public int Bar() + public Task Bar() {{ - return 2; + return Task.FromResult(1); }} }} @@ -61,6 +62,7 @@ public void Test() public override async Task ReportsDiagnostics_WhenSettingValueForStaticMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -68,9 +70,9 @@ namespace MyNamespace {{ public class Foo {{ - public static int Bar() + public static Task Bar() {{ - return 2; + return Task.FromResult(2); }} }} @@ -89,6 +91,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForVirtualMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -96,9 +99,9 @@ namespace MyNamespace {{ public class Foo {{ - public virtual int Bar() + public virtual Task Bar() {{ - return 2; + return Task.FromResult(2); }} }} @@ -117,6 +120,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForNonSealedOverrideMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -124,15 +128,15 @@ namespace MyNamespace {{ public class Foo {{ - public virtual int Bar() + public virtual Task Bar() {{ - return 2; + return Task.FromResult(2); }} }} public class Foo2 : Foo {{ - public override int Bar() => 1; + public override Task Bar() => Task.FromResult(1); }} public class FooTests @@ -150,6 +154,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenDataFlowAnalysisIsRequired(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -157,9 +162,9 @@ namespace MyNamespace {{ public class Foo {{ - public virtual int Bar() + public virtual Task Bar() {{ - return 2; + return Task.FromResult(2); }} }} @@ -179,6 +184,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForDelegate(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; using System; @@ -189,7 +195,7 @@ public class FooTests {{ public void Test() {{ - var substitute = Substitute.For>(); + var substitute = Substitute.For>>(); substitute().{method}(new Exception()); }} }} @@ -200,6 +206,7 @@ public void Test() public override async Task ReportsDiagnostics_WhenSettingValueForSealedOverrideMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -207,15 +214,15 @@ namespace MyNamespace {{ public class Foo {{ - public virtual int Bar() + public virtual Task Bar() {{ - return 2; + return Task.FromResult(2); }} }} public class Foo2 : Foo {{ - public sealed override int Bar() => 1; + public sealed override Task Bar() => Task.FromResult(1); }} public class FooTests @@ -234,6 +241,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForAbstractMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -241,7 +249,7 @@ namespace MyNamespace {{ public abstract class Foo {{ - public abstract int Bar(); + public abstract Task Bar(); }} public class FooTests @@ -260,6 +268,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForInterfaceMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -267,7 +276,7 @@ namespace MyNamespace {{ public interface IFoo {{ - int Bar(); + Task Bar(); }} public class FooTests @@ -285,6 +294,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForInterfaceProperty(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -292,7 +302,7 @@ namespace MyNamespace {{ public interface IFoo {{ - int Bar {{ get; }} + Task Bar {{ get; }} }} public class FooTests @@ -310,6 +320,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForGenericInterfaceMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -317,7 +328,7 @@ namespace MyNamespace {{ public interface IFoo {{ - int Bar(); + Task Bar(); }} public class FooTests @@ -335,6 +346,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForAbstractProperty(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -342,7 +354,7 @@ namespace MyNamespace {{ public abstract class Foo {{ - public abstract int Bar {{ get; }} + public abstract Task Bar {{ get; }} }} public class FooTests @@ -361,6 +373,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForInterfaceIndexer(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -368,7 +381,7 @@ namespace MyNamespace {{ public interface IFoo {{ - int this[int i] {{ get; }} + Task this[int i] {{ get; }} }} public class FooTests @@ -386,6 +399,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForVirtualProperty(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -393,7 +407,7 @@ namespace MyNamespace {{ public class Foo {{ - public virtual int Bar {{ get; }} + public virtual Task Bar {{ get; }} }} public class FooTests @@ -412,6 +426,7 @@ public void Test() public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualProperty(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -419,7 +434,7 @@ namespace MyNamespace {{ public class Foo {{ - public int Bar {{ get; }} + public Task Bar {{ get; }} }} public class FooTests @@ -438,6 +453,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForVirtualIndexer(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -445,7 +461,7 @@ namespace MyNamespace {{ public class Foo {{ - public virtual int this[int x] => 0; + public virtual Task this[int x] => Task.FromResult(0); }} public class FooTests @@ -463,6 +479,7 @@ public void Test() public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualIndexer(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -470,7 +487,7 @@ namespace MyNamespace {{ public class Foo {{ - public int this[int x] => 0; + public Task this[int x] => Task.FromResult(0); }} public class FooTests @@ -490,13 +507,14 @@ public override async Task ReportsNoDiagnostics_WhenUsingUnfortunatelyNamedMetho { var source = $@" using System; +using System.Threading.Tasks; namespace NSubstitute {{ public class Foo {{ - public int Bar() + public Task Bar() {{ - return 1; + return Task.FromResult(1); }} }} @@ -507,10 +525,20 @@ public static T Throws(this object value, T ex) where T: Exception return default(T); }} + public static T ThrowsAsync(this Task value, T ex) where T: Exception + {{ + return default(T); + }} + public static T ThrowsForAnyArgs(this object value, T ex) where T: Exception {{ return default(T); }} + + public static T ThrowsAsyncForAnyArgs(this Task value, T ex) where T: Exception + {{ + return default(T); + }} }} public class FooTests @@ -530,6 +558,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("P:MyNamespace.Foo.Bar", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -537,9 +566,9 @@ namespace MyNamespace {{ public class Foo {{ - public int Bar {{ get; }} + public Task Bar {{ get; }} - public int FooBar {{ get; }} + public Task FooBar {{ get; }} }} public class FooTests @@ -561,23 +590,24 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("P:MyNamespace.Foo`1.Bar", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; namespace MyNamespace {{ - public class Foo + public class Foo where T : Task {{ public T Bar {{ get; }} - public int FooBar {{ get; }} + public Task FooBar {{ get; }} }} public class FooTests {{ public void Test() {{ - var substitute = NSubstitute.Substitute.For>(); + var substitute = NSubstitute.Substitute.For>(); substitute.Bar.{method}(new Exception()); [|substitute.FooBar|].{method}(new Exception()); }} @@ -592,6 +622,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("M:MyNamespace.Foo.Bar(System.Int32,System.Int32)", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -599,14 +630,14 @@ namespace MyNamespace {{ public class Foo {{ - public int Bar(int x) + public Task Bar(int x) {{ - return 1; + return Task.FromResult(1); }} - public int Bar(int x, int y) + public Task Bar(int x, int y) {{ - return 2; + return Task.FromResult(2); }} }} @@ -629,6 +660,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("M:MyNamespace.Foo.Bar``1(``0,``0)", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -636,14 +668,14 @@ namespace MyNamespace {{ public class Foo {{ - public int Bar(int x) + public Task Bar(int x) {{ - return 1; + return Task.FromResult(1); }} - public int Bar(T x, T y) + public Task Bar(T x, T y) {{ - return 2; + return Task.FromResult(2); }} }} @@ -666,6 +698,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("P:MyNamespace.Foo.Item(System.Int32,System.Int32)", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -673,8 +706,8 @@ namespace MyNamespace {{ public class Foo {{ - public int this[int x] => 0; - public int this[int x, int y] => 0; + public Task this[int x] => Task.FromResult(0); + public Task this[int x, int y] => Task.FromResult(0); }} public class FooTests @@ -696,6 +729,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("P:MyNamespace.Foo`1.Item(`0,`0)", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -703,8 +737,8 @@ namespace MyNamespace {{ public class Foo {{ - public int this[T x] => 0; - public int this[T x, T y] => 0; + public Task this[T x] => Task.FromResult(0); + public Task this[T x, T y] => Task.FromResult(0); }} public class FooTests @@ -726,6 +760,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("T:MyNamespace.Foo", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -733,21 +768,21 @@ namespace MyNamespace {{ public class Foo {{ - public int Bar {{ get; set; }} - public int this[int x] => 0; - public int FooBar() + public Task Bar {{ get; set; }} + public Task this[int x] => Task.FromResult(0); + public Task FooBar() {{ - return 1; + return Task.FromResult(1); }} }} public class FooBarBar {{ - public int Bar {{ get;set; }} - public int this[int x] => 0; - public int FooBar() + public Task Bar {{ get;set; }} + public Task this[int x] => Task.FromResult(0); + public Task FooBar() {{ - return 1; + return Task.FromResult(1); }} }} @@ -787,6 +822,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("T:MyNamespace.Foo`1", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -794,21 +830,21 @@ namespace MyNamespace {{ public class Foo {{ - public int Bar {{ get; set; }} - public int this[int x] => 0; - public int FooBar() + public Task Bar {{ get; set; }} + public Task this[int x] => Task.FromResult(0); + public Task FooBar() {{ - return 1; + return Task.FromResult(1); }} }} public class FooBarBar {{ - public int Bar {{ get;set; }} - public int this[int x] => 0; - public int FooBar() + public Task Bar {{ get;set; }} + public Task this[int x] => Task.FromResult(0); + public Task FooBar() {{ - return 1; + return Task.FromResult(1); }} }} @@ -848,6 +884,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("N:MyNamespace", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -855,11 +892,11 @@ namespace MyOtherNamespace {{ public class FooBarBar {{ - public int Bar {{ get; set; }} - public int this[int x] => 0; - public int FooBar() + public Task Bar {{ get; set; }} + public Task this[int x] => Task.FromResult(0); + public Task FooBar() {{ - return 1; + return Task.FromResult(1); }} }} }} @@ -869,11 +906,11 @@ namespace MyNamespace using MyOtherNamespace; public class Foo {{ - public int Bar {{ get; set; }} - public int this[int x] => 0; - public int FooBar() + public Task Bar {{ get; set; }} + public Task this[int x] => Task.FromResult(0); + public Task FooBar() {{ - return 1; + return Task.FromResult(1); }} }} @@ -910,9 +947,10 @@ public void Test() public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressingExtensionMethod(string method) { - Settings = AnalyzersSettings.CreateWithSuppressions("M:MyNamespace.MyExtensions.GetBar(System.Object)~System.Int32", NonVirtualSetupSpecificationDescriptor.Id); + Settings = AnalyzersSettings.CreateWithSuppressions("M:MyNamespace.MyExtensions.GetBar(System.Object)~System.Threading.Tasks.Task{System.Int32}", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -933,20 +971,20 @@ public static class MyExtensions {{ public static IBar Bar {{ get; set; }} - public static int GetBar(this object @object) + public static Task GetBar(this object @object) {{ return Bar.Foo(@object); }} - public static int GetFooBar(this object @object) + public static Task GetFooBar(this object @object) {{ - return 1; + return Task.FromResult(1); }} }} public interface IBar {{ - int Foo(object @obj); + Task Foo(object @obj); }} }}"; @@ -956,6 +994,7 @@ public interface IBar public override async Task ReportsDiagnostics_WhenSettingValueForInternalVirtualMember_AndInternalsVisibleToNotApplied(string method, string call, string message) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -963,16 +1002,16 @@ namespace MyNamespace {{ public class Foo {{ - internal virtual int Bar {{ get; }} + internal virtual Task Bar {{ get; }} - internal virtual int FooBar() + internal virtual Task FooBar() {{ - return 1; + return Task.FromResult(1); }} - internal virtual int this[int x] + internal virtual Task this[int x] {{ - get {{ return 1; }} + get {{ return Task.FromResult(1); }} }} }} @@ -992,6 +1031,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForInternalVirtualMember_AndInternalsVisibleToApplied(string method, string call) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; using System.Runtime.CompilerServices; @@ -1003,16 +1043,16 @@ namespace MyNamespace {{ public class Foo {{ - internal virtual int Bar {{ get; }} + internal virtual Task Bar {{ get; }} - internal virtual int FooBar() + internal virtual Task FooBar() {{ - return 1; + return Task.FromResult(1); }} - internal virtual int this[int x] + internal virtual Task this[int x] {{ - get {{ return 1; }} + get {{ return Task.FromResult(1); }} }} }} @@ -1032,6 +1072,7 @@ public void Test() public override async Task ReportsDiagnostics_WhenSettingValueForInternalVirtualMember_AndInternalsVisibleToAppliedToWrongAssembly(string method, string call, string message) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; using System.Runtime.CompilerServices; @@ -1041,16 +1082,16 @@ namespace MyNamespace {{ public class Foo {{ - internal virtual int Bar {{ get; }} + internal virtual Task Bar {{ get; }} - internal virtual int FooBar() + internal virtual Task FooBar() {{ - return 1; + return Task.FromResult(1); }} - internal virtual int this[int x] + internal virtual Task this[int x] {{ - get {{ return 1; }} + get {{ return Task.FromResult(1); }} }} }} @@ -1070,6 +1111,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForProtectedInternalVirtualMember(string method, string call) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -1077,16 +1119,16 @@ namespace MyNamespace {{ public class Foo {{ - protected internal virtual int Bar {{ get; }} + protected internal virtual Task Bar {{ get; }} - protected internal virtual int FooBar() + protected internal virtual Task FooBar() {{ - return 1; + return Task.FromResult(1); }} - protected internal virtual int this[int x] + protected internal virtual Task this[int x] {{ - get {{ return 1; }} + get {{ return Task.FromResult(1); }} }} }} diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsExtensionMethodWithGenericTypeSpecifiedTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsExtensionMethodWithGenericTypeSpecifiedTests.cs index d7dbe5ee..018b8112 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsExtensionMethodWithGenericTypeSpecifiedTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsExtensionMethodWithGenericTypeSpecifiedTests.cs @@ -6,12 +6,13 @@ namespace NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.NonSubstitutableMemberAnalyzerTests; -[CombinatoryData("Throws", "ThrowsForAnyArgs")] +[CombinatoryData("Throws", "ThrowsAsync", "ThrowsForAnyArgs", "ThrowsAsyncForAnyArgs")] public class ThrowsAsExtensionMethodWithGenericTypeSpecifiedTests : NonSubstitutableMemberDiagnosticVerifier { public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -19,9 +20,9 @@ namespace MyNamespace {{ public class Foo {{ - public int Bar() + public Task Bar() {{ - return 2; + return Task.FromResult(1); }} }} @@ -61,6 +62,7 @@ public void Test() public override async Task ReportsDiagnostics_WhenSettingValueForStaticMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -68,9 +70,9 @@ namespace MyNamespace {{ public class Foo {{ - public static int Bar() + public static Task Bar() {{ - return 2; + return Task.FromResult(2); }} }} @@ -89,6 +91,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForVirtualMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -96,9 +99,9 @@ namespace MyNamespace {{ public class Foo {{ - public virtual int Bar() + public virtual Task Bar() {{ - return 2; + return Task.FromResult(2); }} }} @@ -117,6 +120,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForNonSealedOverrideMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -124,15 +128,15 @@ namespace MyNamespace {{ public class Foo {{ - public virtual int Bar() + public virtual Task Bar() {{ - return 2; + return Task.FromResult(2); }} }} public class Foo2 : Foo {{ - public override int Bar() => 1; + public override Task Bar() => Task.FromResult(1); }} public class FooTests @@ -150,6 +154,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenDataFlowAnalysisIsRequired(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -157,9 +162,9 @@ namespace MyNamespace {{ public class Foo {{ - public virtual int Bar() + public virtual Task Bar() {{ - return 2; + return Task.FromResult(2); }} }} @@ -179,6 +184,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForDelegate(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; using System; @@ -189,7 +195,7 @@ public class FooTests {{ public void Test() {{ - var substitute = Substitute.For>(); + var substitute = Substitute.For>>(); substitute().{method}(); }} }} @@ -200,6 +206,7 @@ public void Test() public override async Task ReportsDiagnostics_WhenSettingValueForSealedOverrideMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -207,15 +214,15 @@ namespace MyNamespace {{ public class Foo {{ - public virtual int Bar() + public virtual Task Bar() {{ - return 2; + return Task.FromResult(2); }} }} public class Foo2 : Foo {{ - public sealed override int Bar() => 1; + public sealed override Task Bar() => Task.FromResult(1); }} public class FooTests @@ -234,6 +241,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForAbstractMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -241,7 +249,7 @@ namespace MyNamespace {{ public abstract class Foo {{ - public abstract int Bar(); + public abstract Task Bar(); }} public class FooTests @@ -260,6 +268,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForInterfaceMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -267,7 +276,7 @@ namespace MyNamespace {{ public interface IFoo {{ - int Bar(); + Task Bar(); }} public class FooTests @@ -285,6 +294,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForInterfaceProperty(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -292,7 +302,7 @@ namespace MyNamespace {{ public interface IFoo {{ - int Bar {{ get; }} + Task Bar {{ get; }} }} public class FooTests @@ -310,6 +320,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForGenericInterfaceMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -317,7 +328,7 @@ namespace MyNamespace {{ public interface IFoo {{ - int Bar(); + Task Bar(); }} public class FooTests @@ -335,6 +346,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForAbstractProperty(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -342,7 +354,7 @@ namespace MyNamespace {{ public abstract class Foo {{ - public abstract int Bar {{ get; }} + public abstract Task Bar {{ get; }} }} public class FooTests @@ -361,6 +373,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForInterfaceIndexer(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -368,7 +381,7 @@ namespace MyNamespace {{ public interface IFoo {{ - int this[int i] {{ get; }} + Task this[int i] {{ get; }} }} public class FooTests @@ -386,6 +399,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForVirtualProperty(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -393,7 +407,7 @@ namespace MyNamespace {{ public class Foo {{ - public virtual int Bar {{ get; }} + public virtual Task Bar {{ get; }} }} public class FooTests @@ -412,6 +426,7 @@ public void Test() public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualProperty(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -419,7 +434,7 @@ namespace MyNamespace {{ public class Foo {{ - public int Bar {{ get; }} + public Task Bar {{ get; }} }} public class FooTests @@ -438,6 +453,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForVirtualIndexer(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -445,7 +461,7 @@ namespace MyNamespace {{ public class Foo {{ - public virtual int this[int x] => 0; + public virtual Task this[int x] => Task.FromResult(0); }} public class FooTests @@ -463,6 +479,7 @@ public void Test() public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualIndexer(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -470,7 +487,7 @@ namespace MyNamespace {{ public class Foo {{ - public int this[int x] => 0; + public Task this[int x] => Task.FromResult(0); }} public class FooTests @@ -490,13 +507,14 @@ public override async Task ReportsNoDiagnostics_WhenUsingUnfortunatelyNamedMetho { var source = $@" using System; +using System.Threading.Tasks; namespace NSubstitute {{ public class Foo {{ - public int Bar() + public Task Bar() {{ - return 1; + return Task.FromResult(1); }} }} @@ -507,10 +525,20 @@ public static T Throws(this object value) where T: Exception return default(T); }} + public static T ThrowsAsync(this Task value) where T: Exception + {{ + return default(T); + }} + public static T ThrowsForAnyArgs(this object value) where T: Exception {{ return default(T); }} + + public static T ThrowsAsyncForAnyArgs(this Task value) where T: Exception + {{ + return default(T); + }} }} public class FooTests @@ -530,6 +558,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("P:MyNamespace.Foo.Bar", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -537,9 +566,9 @@ namespace MyNamespace {{ public class Foo {{ - public int Bar {{ get; }} + public Task Bar {{ get; }} - public int FooBar {{ get; }} + public Task FooBar {{ get; }} }} public class FooTests @@ -561,23 +590,24 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("P:MyNamespace.Foo`1.Bar", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; namespace MyNamespace {{ - public class Foo + public class Foo where T : Task {{ public T Bar {{ get; }} - public int FooBar {{ get; }} + public Task FooBar {{ get; }} }} public class FooTests {{ public void Test() {{ - var substitute = NSubstitute.Substitute.For>(); + var substitute = NSubstitute.Substitute.For>>(); substitute.Bar.{method}(); [|substitute.FooBar|].{method}(); }} @@ -592,6 +622,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("M:MyNamespace.Foo.Bar(System.Int32,System.Int32)", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -599,14 +630,14 @@ namespace MyNamespace {{ public class Foo {{ - public int Bar(int x) + public Task Bar(int x) {{ - return 1; + return Task.FromResult(1); }} - public int Bar(int x, int y) + public Task Bar(int x, int y) {{ - return 2; + return Task.FromResult(2); }} }} @@ -629,6 +660,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("M:MyNamespace.Foo.Bar``1(``0,``0)", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -636,14 +668,14 @@ namespace MyNamespace {{ public class Foo {{ - public int Bar(int x) + public Task Bar(int x) {{ - return 1; + return Task.FromResult(1); }} - public int Bar(T x, T y) + public Task Bar(T x, T y) {{ - return 2; + return Task.FromResult(2); }} }} @@ -666,6 +698,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("P:MyNamespace.Foo.Item(System.Int32,System.Int32)", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -673,8 +706,8 @@ namespace MyNamespace {{ public class Foo {{ - public int this[int x] => 0; - public int this[int x, int y] => 0; + public Task this[int x] => Task.FromResult(0); + public Task this[int x, int y] => Task.FromResult(0); }} public class FooTests @@ -696,6 +729,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("P:MyNamespace.Foo`1.Item(`0,`0)", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -703,8 +737,8 @@ namespace MyNamespace {{ public class Foo {{ - public int this[T x] => 0; - public int this[T x, T y] => 0; + public Task this[T x] => Task.FromResult(0); + public Task this[T x, T y] => Task.FromResult(0); }} public class FooTests @@ -726,6 +760,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("T:MyNamespace.Foo", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -733,21 +768,21 @@ namespace MyNamespace {{ public class Foo {{ - public int Bar {{ get; set; }} - public int this[int x] => 0; - public int FooBar() + public Task Bar {{ get; set; }} + public Task this[int x] => Task.FromResult(0); + public Task FooBar() {{ - return 1; + return Task.FromResult(1); }} }} public class FooBarBar {{ - public int Bar {{ get;set; }} - public int this[int x] => 0; - public int FooBar() + public Task Bar {{ get;set; }} + public Task this[int x] => Task.FromResult(0); + public Task FooBar() {{ - return 1; + return Task.FromResult(1); }} }} @@ -787,6 +822,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("T:MyNamespace.Foo`1", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -794,21 +830,21 @@ namespace MyNamespace {{ public class Foo {{ - public int Bar {{ get; set; }} - public int this[int x] => 0; - public int FooBar() + public Task Bar {{ get; set; }} + public Task this[int x] => Task.FromResult(0); + public Task FooBar() {{ - return 1; + return Task.FromResult(1); }} }} public class FooBarBar {{ - public int Bar {{ get;set; }} - public int this[int x] => 0; - public int FooBar() + public Task Bar {{ get;set; }} + public Task this[int x] => Task.FromResult(0); + public Task FooBar() {{ - return 1; + return Task.FromResult(1); }} }} @@ -848,6 +884,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("N:MyNamespace", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -855,11 +892,11 @@ namespace MyOtherNamespace {{ public class FooBarBar {{ - public int Bar {{ get; set; }} - public int this[int x] => 0; - public int FooBar() + public Task Bar {{ get; set; }} + public Task this[int x] => Task.FromResult(0); + public Task FooBar() {{ - return 1; + return Task.FromResult(1); }} }} }} @@ -869,11 +906,11 @@ namespace MyNamespace using MyOtherNamespace; public class Foo {{ - public int Bar {{ get; set; }} - public int this[int x] => 0; - public int FooBar() + public Task Bar {{ get; set; }} + public Task this[int x] => Task.FromResult(0); + public Task FooBar() {{ - return 1; + return Task.FromResult(1); }} }} @@ -910,9 +947,10 @@ public void Test() public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressingExtensionMethod(string method) { - Settings = AnalyzersSettings.CreateWithSuppressions("M:MyNamespace.MyExtensions.GetBar(System.Object)~System.Int32", NonVirtualSetupSpecificationDescriptor.Id); + Settings = AnalyzersSettings.CreateWithSuppressions("M:MyNamespace.MyExtensions.GetBar(System.Object)~System.Threading.Tasks.Task{System.Int32}", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -933,20 +971,20 @@ public static class MyExtensions {{ public static IBar Bar {{ get; set; }} - public static int GetBar(this object @object) + public static Task GetBar(this object @object) {{ return Bar.Foo(@object); }} - public static int GetFooBar(this object @object) + public static Task GetFooBar(this object @object) {{ - return 1; + return Task.FromResult(1); }} }} public interface IBar {{ - int Foo(object @obj); + Task Foo(object @obj); }} }}"; @@ -956,6 +994,7 @@ public interface IBar public override async Task ReportsDiagnostics_WhenSettingValueForInternalVirtualMember_AndInternalsVisibleToNotApplied(string method, string call, string message) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -963,16 +1002,16 @@ namespace MyNamespace {{ public class Foo {{ - internal virtual int Bar {{ get; }} + internal virtual Task Bar {{ get; }} - internal virtual int FooBar() + internal virtual Task FooBar() {{ - return 1; + return Task.FromResult(1); }} - internal virtual int this[int x] + internal virtual Task this[int x] {{ - get {{ return 1; }} + get {{ return Task.FromResult(1); }} }} }} @@ -992,6 +1031,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForInternalVirtualMember_AndInternalsVisibleToApplied(string method, string call) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; using System.Runtime.CompilerServices; @@ -1003,16 +1043,16 @@ namespace MyNamespace {{ public class Foo {{ - internal virtual int Bar {{ get; }} + internal virtual Task Bar {{ get; }} - internal virtual int FooBar() + internal virtual Task FooBar() {{ - return 1; + return Task.FromResult(1); }} - internal virtual int this[int x] + internal virtual Task this[int x] {{ - get {{ return 1; }} + get {{ return Task.FromResult(1); }} }} }} @@ -1032,6 +1072,7 @@ public void Test() public override async Task ReportsDiagnostics_WhenSettingValueForInternalVirtualMember_AndInternalsVisibleToAppliedToWrongAssembly(string method, string call, string message) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; using System.Runtime.CompilerServices; @@ -1041,16 +1082,16 @@ namespace MyNamespace {{ public class Foo {{ - internal virtual int Bar {{ get; }} + internal virtual Task Bar {{ get; }} - internal virtual int FooBar() + internal virtual Task FooBar() {{ - return 1; + return Task.FromResult(1); }} - internal virtual int this[int x] + internal virtual Task this[int x] {{ - get {{ return 1; }} + get {{ return Task.FromResult(1); }} }} }} @@ -1070,6 +1111,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForProtectedInternalVirtualMember(string method, string call) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -1077,16 +1119,16 @@ namespace MyNamespace {{ public class Foo {{ - protected internal virtual int Bar {{ get; }} + protected internal virtual Task Bar {{ get; }} - protected internal virtual int FooBar() + protected internal virtual Task FooBar() {{ - return 1; + return Task.FromResult(1); }} - protected internal virtual int this[int x] + protected internal virtual Task this[int x] {{ - get {{ return 1; }} + get {{ return Task.FromResult(1); }} }} }} diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodTests.cs index f25e455b..54b9a02f 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodTests.cs @@ -1,4 +1,4 @@ -using System.Linq; +using System.Linq; using System.Threading.Tasks; using NSubstitute.Analyzers.Shared.Settings; using NSubstitute.Analyzers.Tests.Shared.Extensibility; @@ -6,12 +6,17 @@ namespace NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.NonSubstitutableMemberAnalyzerTests; -[CombinatoryData("ExceptionExtensions.Throws", "ExceptionExtensions.ThrowsForAnyArgs")] +[CombinatoryData( + "ExceptionExtensions.Throws", + "ExceptionExtensions.ThrowsAsync", + "ExceptionExtensions.ThrowsForAnyArgs", + "ExceptionExtensions.ThrowsAsyncForAnyArgs")] public class ThrowsAsOrdinaryMethodTests : NonSubstitutableMemberDiagnosticVerifier { public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -19,9 +24,9 @@ namespace MyNamespace {{ public class Foo {{ - public int Bar() + public Task Bar() {{ - return 2; + return Task.FromResult(1); }} }} @@ -64,6 +69,7 @@ public void Test() public override async Task ReportsDiagnostics_WhenSettingValueForStaticMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -71,9 +77,9 @@ namespace MyNamespace {{ public class Foo {{ - public static int Bar() + public static Task Bar() {{ - return 2; + return Task.FromResult(2); }} }} @@ -94,6 +100,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForVirtualMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -101,9 +108,9 @@ namespace MyNamespace {{ public class Foo {{ - public virtual int Bar() + public virtual Task Bar() {{ - return 2; + return Task.FromResult(2); }} }} @@ -124,6 +131,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForNonSealedOverrideMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -131,15 +139,15 @@ namespace MyNamespace {{ public class Foo {{ - public virtual int Bar() + public virtual Task Bar() {{ - return 2; + return Task.FromResult(2); }} }} public class Foo2 : Foo {{ - public override int Bar() => 1; + public override Task Bar() => Task.FromResult(1); }} public class FooTests @@ -159,6 +167,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenDataFlowAnalysisIsRequired(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -166,9 +175,9 @@ namespace MyNamespace {{ public class Foo {{ - public virtual int Bar() + public virtual Task Bar() {{ - return 2; + return Task.FromResult(2); }} }} @@ -190,6 +199,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForDelegate(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; using System; @@ -200,7 +210,7 @@ public class FooTests {{ public void Test() {{ - var substitute = Substitute.For>(); + var substitute = Substitute.For>>(); {method}(substitute(), new Exception()); {method}(value: substitute(), ex: new Exception()); {method}(ex: new Exception(), value: substitute()); @@ -213,6 +223,7 @@ public void Test() public override async Task ReportsDiagnostics_WhenSettingValueForSealedOverrideMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -220,15 +231,15 @@ namespace MyNamespace {{ public class Foo {{ - public virtual int Bar() + public virtual Task Bar() {{ - return 2; + return Task.FromResult(2); }} }} public class Foo2 : Foo {{ - public sealed override int Bar() => 1; + public sealed override Task Bar() => Task.FromResult(1); }} public class FooTests @@ -249,6 +260,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForAbstractMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -256,7 +268,7 @@ namespace MyNamespace {{ public abstract class Foo {{ - public abstract int Bar(); + public abstract Task Bar(); }} public class FooTests @@ -277,6 +289,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForInterfaceMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -284,7 +297,7 @@ namespace MyNamespace {{ public interface IFoo {{ - int Bar(); + Task Bar(); }} public class FooTests @@ -304,6 +317,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForInterfaceProperty(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -311,7 +325,7 @@ namespace MyNamespace {{ public interface IFoo {{ - int Bar {{ get; }} + Task Bar {{ get; }} }} public class FooTests @@ -331,6 +345,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForGenericInterfaceMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -338,7 +353,7 @@ namespace MyNamespace {{ public interface IFoo {{ - int Bar(); + Task Bar(); }} public class FooTests @@ -358,6 +373,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForAbstractProperty(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -365,7 +381,7 @@ namespace MyNamespace {{ public abstract class Foo {{ - public abstract int Bar {{ get; }} + public abstract Task Bar {{ get; }} }} public class FooTests @@ -386,6 +402,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForInterfaceIndexer(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -393,7 +410,7 @@ namespace MyNamespace {{ public interface IFoo {{ - int this[int i] {{ get; }} + Task this[int i] {{ get; }} }} public class FooTests @@ -413,6 +430,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForVirtualProperty(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -420,7 +438,7 @@ namespace MyNamespace {{ public class Foo {{ - public virtual int Bar {{ get; }} + public virtual Task Bar {{ get; }} }} public class FooTests @@ -441,6 +459,7 @@ public void Test() public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualProperty(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -448,7 +467,7 @@ namespace MyNamespace {{ public class Foo {{ - public int Bar {{ get; }} + public Task Bar {{ get; }} }} public class FooTests @@ -469,6 +488,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForVirtualIndexer(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -476,7 +496,7 @@ namespace MyNamespace {{ public class Foo {{ - public virtual int this[int x] => 0; + public virtual Task this[int x] => Task.FromResult(0); }} public class FooTests @@ -496,6 +516,7 @@ public void Test() public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualIndexer(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -503,7 +524,7 @@ namespace MyNamespace {{ public class Foo {{ - public int this[int x] => 0; + public Task this[int x] => Task.FromResult(0); }} public class FooTests @@ -525,13 +546,14 @@ public override async Task ReportsNoDiagnostics_WhenUsingUnfortunatelyNamedMetho { var source = $@" using System; +using System.Threading.Tasks; namespace NSubstitute {{ public class Foo {{ - public int Bar() + public Task Bar() {{ - return 1; + return Task.FromResult(1); }} }} @@ -542,10 +564,20 @@ public static T Throws(this object value, T ex) where T: Exception return default(T); }} + public static T ThrowsAsync(this Task value, T ex) where T: Exception + {{ + return default(T); + }} + public static T ThrowsForAnyArgs(this object value, T ex) where T: Exception {{ return default(T); }} + + public static T ThrowsAsyncForAnyArgs(this Task value, T ex) where T: Exception + {{ + return default(T); + }} }} public class FooTests @@ -567,6 +599,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("P:MyNamespace.Foo.Bar", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -574,9 +607,9 @@ namespace MyNamespace {{ public class Foo {{ - public int Bar {{ get; }} + public Task Bar {{ get; }} - public int FooBar {{ get; }} + public Task FooBar {{ get; }} }} public class FooTests @@ -598,23 +631,24 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("P:MyNamespace.Foo`1.Bar", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; namespace MyNamespace {{ - public class Foo + public class Foo where T : Task {{ public T Bar {{ get; }} - public int FooBar {{ get; }} + public Task FooBar {{ get; }} }} public class FooTests {{ public void Test() {{ - var substitute = NSubstitute.Substitute.For>(); + var substitute = NSubstitute.Substitute.For>>(); {method}(substitute.Bar, new Exception()); {method}([|substitute.FooBar|], new Exception()); }} @@ -629,6 +663,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("M:MyNamespace.Foo.Bar(System.Int32,System.Int32)", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -636,14 +671,14 @@ namespace MyNamespace {{ public class Foo {{ - public int Bar(int x) + public Task Bar(int x) {{ - return 1; + return Task.FromResult(1); }} - public int Bar(int x, int y) + public Task Bar(int x, int y) {{ - return 2; + return Task.FromResult(2); }} }} @@ -666,6 +701,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("M:MyNamespace.Foo.Bar``1(``0,``0)", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -673,14 +709,14 @@ namespace MyNamespace {{ public class Foo {{ - public int Bar(int x) + public Task Bar(int x) {{ - return 1; + return Task.FromResult(1); }} - public int Bar(T x, T y) + public Task Bar(T x, T y) {{ - return 2; + return Task.FromResult(2); }} }} @@ -703,6 +739,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("P:MyNamespace.Foo.Item(System.Int32,System.Int32)", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -710,8 +747,8 @@ namespace MyNamespace {{ public class Foo {{ - public int this[int x] => 0; - public int this[int x, int y] => 0; + public Task this[int x] => Task.FromResult(0); + public Task this[int x, int y] => Task.FromResult(0); }} public class FooTests @@ -733,6 +770,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("P:MyNamespace.Foo`1.Item(`0,`0)", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -740,8 +778,8 @@ namespace MyNamespace {{ public class Foo {{ - public int this[T x] => 0; - public int this[T x, T y] => 0; + public Task this[T x] => Task.FromResult(0); + public Task this[T x, T y] => Task.FromResult(0); }} public class FooTests @@ -763,6 +801,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("T:MyNamespace.Foo", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -770,21 +809,21 @@ namespace MyNamespace {{ public class Foo {{ - public int Bar {{ get; set; }} - public int this[int x] => 0; - public int FooBar() + public Task Bar {{ get; set; }} + public Task this[int x] => Task.FromResult(0); + public Task FooBar() {{ - return 1; + return Task.FromResult(1); }} }} public class FooBarBar {{ - public int Bar {{ get;set; }} - public int this[int x] => 0; - public int FooBar() + public Task Bar {{ get;set; }} + public Task this[int x] => Task.FromResult(0); + public Task FooBar() {{ - return 1; + return Task.FromResult(1); }} }} @@ -824,6 +863,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("T:MyNamespace.Foo`1", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -831,21 +871,21 @@ namespace MyNamespace {{ public class Foo {{ - public int Bar {{ get; set; }} - public int this[int x] => 0; - public int FooBar() + public Task Bar {{ get; set; }} + public Task this[int x] => Task.FromResult(0); + public Task FooBar() {{ - return 1; + return Task.FromResult(1); }} }} public class FooBarBar {{ - public int Bar {{ get;set; }} - public int this[int x] => 0; - public int FooBar() + public Task Bar {{ get;set; }} + public Task this[int x] => Task.FromResult(0); + public Task FooBar() {{ - return 1; + return Task.FromResult(1); }} }} @@ -885,6 +925,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("N:MyNamespace", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -892,11 +933,11 @@ namespace MyOtherNamespace {{ public class FooBarBar {{ - public int Bar {{ get; set; }} - public int this[int x] => 0; - public int FooBar() + public Task Bar {{ get; set; }} + public Task this[int x] => Task.FromResult(0); + public Task FooBar() {{ - return 1; + return Task.FromResult(1); }} }} }} @@ -906,11 +947,11 @@ namespace MyNamespace using MyOtherNamespace; public class Foo {{ - public int Bar {{ get; set; }} - public int this[int x] => 0; - public int FooBar() + public Task Bar {{ get; set; }} + public Task this[int x] => Task.FromResult(0); + public Task FooBar() {{ - return 1; + return Task.FromResult(1); }} }} @@ -947,9 +988,10 @@ public void Test() public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressingExtensionMethod(string method) { - Settings = AnalyzersSettings.CreateWithSuppressions("M:MyNamespace.MyExtensions.GetBar(System.Object)~System.Int32", NonVirtualSetupSpecificationDescriptor.Id); + Settings = AnalyzersSettings.CreateWithSuppressions("M:MyNamespace.MyExtensions.GetBar(System.Object)~System.Threading.Tasks.Task{System.Int32}", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -970,20 +1012,20 @@ public static class MyExtensions {{ public static IBar Bar {{ get; set; }} - public static int GetBar(this object @object) + public static Task GetBar(this object @object) {{ return Bar.Foo(@object); }} - public static int GetFooBar(this object @object) + public static Task GetFooBar(this object @object) {{ - return 1; + return Task.FromResult(1); }} }} public interface IBar {{ - int Foo(object @obj); + Task Foo(object @obj); }} }}"; @@ -993,6 +1035,7 @@ public interface IBar public override async Task ReportsDiagnostics_WhenSettingValueForInternalVirtualMember_AndInternalsVisibleToNotApplied(string method, string call, string message) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -1000,16 +1043,16 @@ namespace MyNamespace {{ public class Foo {{ - internal virtual object Bar {{ get; }} + internal virtual Task Bar {{ get; }} - internal virtual object FooBar() + internal virtual Task FooBar() {{ - return 1; + return Task.FromResult(1); }} - internal virtual object this[int x] + internal virtual Task this[int x] {{ - get {{ return 1; }} + get {{ return Task.FromResult(1); }} }} }} @@ -1031,6 +1074,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForInternalVirtualMember_AndInternalsVisibleToApplied(string method, string call) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; using System.Runtime.CompilerServices; @@ -1042,16 +1086,16 @@ namespace MyNamespace {{ public class Foo {{ - internal virtual object Bar {{ get; }} + internal virtual Task Bar {{ get; }} - internal virtual object FooBar() + internal virtual Task FooBar() {{ - return 1; + return Task.FromResult(1); }} - internal virtual object this[int x] + internal virtual Task this[int x] {{ - get {{ return 1; }} + get {{ return Task.FromResult(1); }} }} }} @@ -1073,25 +1117,26 @@ public void Test() public override async Task ReportsDiagnostics_WhenSettingValueForInternalVirtualMember_AndInternalsVisibleToAppliedToWrongAssembly(string method, string call, string message) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; -using System.Runtime.CompilerServices; using NSubstitute.ExceptionExtensions; +using System.Runtime.CompilerServices; [assembly: InternalsVisibleTo(""OtherAssembly"")] namespace MyNamespace {{ public class Foo {{ - internal virtual object Bar {{ get; }} + internal virtual Task Bar {{ get; }} - internal virtual object FooBar() + internal virtual Task FooBar() {{ - return 1; + return Task.FromResult(1); }} - internal virtual object this[int x] + internal virtual Task this[int x] {{ - get {{ return 1; }} + get {{ return Task.FromResult(1); }} }} }} @@ -1113,6 +1158,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForProtectedInternalVirtualMember(string method, string call) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -1120,16 +1166,16 @@ namespace MyNamespace {{ public class Foo {{ - protected internal virtual object Bar {{ get; }} + protected internal virtual Task Bar {{ get; }} - protected internal virtual object FooBar() + protected internal virtual Task FooBar() {{ - return 1; + return Task.FromResult(1); }} - protected internal virtual object this[int x] + protected internal virtual Task this[int x] {{ - get {{ return 1; }} + get {{ return Task.FromResult(1); }} }} }} diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodWithGenericTypeSpecifiedTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodWithGenericTypeSpecifiedTests.cs index de11388e..9e274f86 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodWithGenericTypeSpecifiedTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodWithGenericTypeSpecifiedTests.cs @@ -1,4 +1,4 @@ -using System.Linq; +using System.Linq; using System.Threading.Tasks; using NSubstitute.Analyzers.Shared.Settings; using NSubstitute.Analyzers.Tests.Shared.Extensibility; @@ -6,12 +6,17 @@ namespace NSubstitute.Analyzers.Tests.CSharp.DiagnosticAnalyzerTests.NonSubstitutableMemberAnalyzerTests; -[CombinatoryData("ExceptionExtensions.Throws", "ExceptionExtensions.ThrowsForAnyArgs")] +[CombinatoryData( + "ExceptionExtensions.Throws", + "ExceptionExtensions.ThrowsAsync", + "ExceptionExtensions.ThrowsForAnyArgs", + "ExceptionExtensions.ThrowsAsyncForAnyArgs")] public class ThrowsAsOrdinaryMethodWithGenericTypeSpecifiedTests : NonSubstitutableMemberDiagnosticVerifier { public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -19,9 +24,9 @@ namespace MyNamespace {{ public class Foo {{ - public int Bar() + public Task Bar() {{ - return 2; + return Task.FromResult(1); }} }} @@ -61,6 +66,7 @@ public void Test() public override async Task ReportsDiagnostics_WhenSettingValueForStaticMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -68,9 +74,9 @@ namespace MyNamespace {{ public class Foo {{ - public static int Bar() + public static Task Bar() {{ - return 2; + return Task.FromResult(2); }} }} @@ -89,6 +95,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForVirtualMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -96,9 +103,9 @@ namespace MyNamespace {{ public class Foo {{ - public virtual int Bar() + public virtual Task Bar() {{ - return 2; + return Task.FromResult(2); }} }} @@ -117,6 +124,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForNonSealedOverrideMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -124,15 +132,15 @@ namespace MyNamespace {{ public class Foo {{ - public virtual int Bar() + public virtual Task Bar() {{ - return 2; + return Task.FromResult(2); }} }} public class Foo2 : Foo {{ - public override int Bar() => 1; + public override Task Bar() => Task.FromResult(1); }} public class FooTests @@ -150,6 +158,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenDataFlowAnalysisIsRequired(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -157,9 +166,9 @@ namespace MyNamespace {{ public class Foo {{ - public virtual int Bar() + public virtual Task Bar() {{ - return 2; + return Task.FromResult(2); }} }} @@ -179,6 +188,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForDelegate(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; using System; @@ -189,7 +199,7 @@ public class FooTests {{ public void Test() {{ - var substitute = Substitute.For>(); + var substitute = Substitute.For>>(); {method}(substitute()); }} }} @@ -200,6 +210,7 @@ public void Test() public override async Task ReportsDiagnostics_WhenSettingValueForSealedOverrideMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -207,15 +218,15 @@ namespace MyNamespace {{ public class Foo {{ - public virtual int Bar() + public virtual Task Bar() {{ - return 2; + return Task.FromResult(2); }} }} public class Foo2 : Foo {{ - public sealed override int Bar() => 1; + public sealed override Task Bar() => Task.FromResult(1); }} public class FooTests @@ -234,6 +245,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForAbstractMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -241,7 +253,7 @@ namespace MyNamespace {{ public abstract class Foo {{ - public abstract int Bar(); + public abstract Task Bar(); }} public class FooTests @@ -260,6 +272,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForInterfaceMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -267,7 +280,7 @@ namespace MyNamespace {{ public interface IFoo {{ - int Bar(); + Task Bar(); }} public class FooTests @@ -285,6 +298,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForInterfaceProperty(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -292,7 +306,7 @@ namespace MyNamespace {{ public interface IFoo {{ - int Bar {{ get; }} + Task Bar {{ get; }} }} public class FooTests @@ -310,6 +324,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForGenericInterfaceMethod(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -317,7 +332,7 @@ namespace MyNamespace {{ public interface IFoo {{ - int Bar(); + Task Bar(); }} public class FooTests @@ -335,6 +350,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForAbstractProperty(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -342,7 +358,7 @@ namespace MyNamespace {{ public abstract class Foo {{ - public abstract int Bar {{ get; }} + public abstract Task Bar {{ get; }} }} public class FooTests @@ -361,6 +377,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForInterfaceIndexer(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -368,7 +385,7 @@ namespace MyNamespace {{ public interface IFoo {{ - int this[int i] {{ get; }} + Task this[int i] {{ get; }} }} public class FooTests @@ -386,6 +403,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForVirtualProperty(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -393,7 +411,7 @@ namespace MyNamespace {{ public class Foo {{ - public virtual int Bar {{ get; }} + public virtual Task Bar {{ get; }} }} public class FooTests @@ -412,6 +430,7 @@ public void Test() public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualProperty(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -419,7 +438,7 @@ namespace MyNamespace {{ public class Foo {{ - public int Bar {{ get; }} + public Task Bar {{ get; }} }} public class FooTests @@ -438,6 +457,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForVirtualIndexer(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -445,7 +465,7 @@ namespace MyNamespace {{ public class Foo {{ - public virtual int this[int x] => 0; + public virtual Task this[int x] => Task.FromResult(0); }} public class FooTests @@ -463,6 +483,7 @@ public void Test() public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualIndexer(string method) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -470,7 +491,7 @@ namespace MyNamespace {{ public class Foo {{ - public int this[int x] => 0; + public Task this[int x] => Task.FromResult(0); }} public class FooTests @@ -490,13 +511,14 @@ public override async Task ReportsNoDiagnostics_WhenUsingUnfortunatelyNamedMetho { var source = $@" using System; +using System.Threading.Tasks; namespace NSubstitute {{ public class Foo {{ - public int Bar() + public Task Bar() {{ - return 1; + return Task.FromResult(1); }} }} @@ -507,10 +529,20 @@ public static T Throws(this object value) where T: Exception return default(T); }} + public static T ThrowsAsync(this Task value) where T: Exception + {{ + return default(T); + }} + public static T ThrowsForAnyArgs(this object value) where T: Exception {{ return default(T); }} + + public static T ThrowsAsyncForAnyArgs(this Task value) where T: Exception + {{ + return default(T); + }} }} public class FooTests @@ -530,6 +562,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("P:MyNamespace.Foo.Bar", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -537,9 +570,9 @@ namespace MyNamespace {{ public class Foo {{ - public int Bar {{ get; }} + public Task Bar {{ get; }} - public int FooBar {{ get; }} + public Task FooBar {{ get; }} }} public class FooTests @@ -561,23 +594,24 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("P:MyNamespace.Foo`1.Bar", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; namespace MyNamespace {{ - public class Foo + public class Foo where T : Task {{ public T Bar {{ get; }} - public int FooBar {{ get; }} + public Task FooBar {{ get; }} }} public class FooTests {{ public void Test() {{ - var substitute = NSubstitute.Substitute.For>(); + var substitute = NSubstitute.Substitute.For>>(); {method}(substitute.Bar); {method}([|substitute.FooBar|]); }} @@ -592,6 +626,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("M:MyNamespace.Foo.Bar(System.Int32,System.Int32)", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -599,14 +634,14 @@ namespace MyNamespace {{ public class Foo {{ - public int Bar(int x) + public Task Bar(int x) {{ - return 1; + return Task.FromResult(1); }} - public int Bar(int x, int y) + public Task Bar(int x, int y) {{ - return 2; + return Task.FromResult(2); }} }} @@ -629,6 +664,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("M:MyNamespace.Foo.Bar``1(``0,``0)", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -636,14 +672,14 @@ namespace MyNamespace {{ public class Foo {{ - public int Bar(int x) + public Task Bar(int x) {{ - return 1; + return Task.FromResult(1); }} - public int Bar(T x, T y) + public Task Bar(T x, T y) {{ - return 2; + return Task.FromResult(2); }} }} @@ -666,6 +702,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("P:MyNamespace.Foo.Item(System.Int32,System.Int32)", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -673,8 +710,8 @@ namespace MyNamespace {{ public class Foo {{ - public int this[int x] => 0; - public int this[int x, int y] => 0; + public Task this[int x] => Task.FromResult(0); + public Task this[int x, int y] => Task.FromResult(0); }} public class FooTests @@ -696,6 +733,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("P:MyNamespace.Foo`1.Item(`0,`0)", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -703,8 +741,8 @@ namespace MyNamespace {{ public class Foo {{ - public int this[T x] => 0; - public int this[T x, T y] => 0; + public Task this[T x] => Task.FromResult(0); + public Task this[T x, T y] => Task.FromResult(0); }} public class FooTests @@ -726,6 +764,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("T:MyNamespace.Foo", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -733,21 +772,21 @@ namespace MyNamespace {{ public class Foo {{ - public int Bar {{ get; set; }} - public int this[int x] => 0; - public int FooBar() + public Task Bar {{ get; set; }} + public Task this[int x] => Task.FromResult(0); + public Task FooBar() {{ - return 1; + return Task.FromResult(1); }} }} public class FooBarBar {{ - public int Bar {{ get;set; }} - public int this[int x] => 0; - public int FooBar() + public Task Bar {{ get;set; }} + public Task this[int x] => Task.FromResult(0); + public Task FooBar() {{ - return 1; + return Task.FromResult(1); }} }} @@ -787,6 +826,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("T:MyNamespace.Foo`1", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -794,21 +834,21 @@ namespace MyNamespace {{ public class Foo {{ - public int Bar {{ get; set; }} - public int this[int x] => 0; - public int FooBar() + public Task Bar {{ get; set; }} + public Task this[int x] => Task.FromResult(0); + public Task FooBar() {{ - return 1; + return Task.FromResult(1); }} }} public class FooBarBar {{ - public int Bar {{ get;set; }} - public int this[int x] => 0; - public int FooBar() + public Task Bar {{ get;set; }} + public Task this[int x] => Task.FromResult(0); + public Task FooBar() {{ - return 1; + return Task.FromResult(1); }} }} @@ -848,6 +888,7 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("N:MyNamespace", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -855,11 +896,11 @@ namespace MyOtherNamespace {{ public class FooBarBar {{ - public int Bar {{ get; set; }} - public int this[int x] => 0; - public int FooBar() + public Task Bar {{ get; set; }} + public Task this[int x] => Task.FromResult(0); + public Task FooBar() {{ - return 1; + return Task.FromResult(1); }} }} }} @@ -869,11 +910,11 @@ namespace MyNamespace using MyOtherNamespace; public class Foo {{ - public int Bar {{ get; set; }} - public int this[int x] => 0; - public int FooBar() + public Task Bar {{ get; set; }} + public Task this[int x] => Task.FromResult(0); + public Task FooBar() {{ - return 1; + return Task.FromResult(1); }} }} @@ -910,9 +951,10 @@ public void Test() public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressingExtensionMethod(string method) { - Settings = AnalyzersSettings.CreateWithSuppressions("M:MyNamespace.MyExtensions.GetBar(System.Object)~System.Int32", NonVirtualSetupSpecificationDescriptor.Id); + Settings = AnalyzersSettings.CreateWithSuppressions("M:MyNamespace.MyExtensions.GetBar(System.Object)~System.Threading.Tasks.Task{System.Int32}", NonVirtualSetupSpecificationDescriptor.Id); var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -933,20 +975,20 @@ public static class MyExtensions {{ public static IBar Bar {{ get; set; }} - public static int GetBar(this object @object) + public static Task GetBar(this object @object) {{ return Bar.Foo(@object); }} - public static int GetFooBar(this object @object) + public static Task GetFooBar(this object @object) {{ - return 1; + return Task.FromResult(1); }} }} public interface IBar {{ - int Foo(object @obj); + Task Foo(object @obj); }} }}"; @@ -956,6 +998,7 @@ public interface IBar public override async Task ReportsDiagnostics_WhenSettingValueForInternalVirtualMember_AndInternalsVisibleToNotApplied(string method, string call, string message) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -963,16 +1006,16 @@ namespace MyNamespace {{ public class Foo {{ - internal virtual object Bar {{ get; }} + internal virtual Task Bar {{ get; }} - internal virtual object FooBar() + internal virtual Task FooBar() {{ - return 1; + return Task.FromResult(1); }} - internal virtual object this[int x] + internal virtual Task this[int x] {{ - get {{ return 1; }} + get {{ return Task.FromResult(1); }} }} }} @@ -992,6 +1035,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForInternalVirtualMember_AndInternalsVisibleToApplied(string method, string call) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; using System.Runtime.CompilerServices; @@ -1003,16 +1047,16 @@ namespace MyNamespace {{ public class Foo {{ - internal virtual object Bar {{ get; }} + internal virtual Task Bar {{ get; }} - internal virtual object FooBar() + internal virtual Task FooBar() {{ - return 1; + return Task.FromResult(1); }} - internal virtual object this[int x] + internal virtual Task this[int x] {{ - get {{ return 1; }} + get {{ return Task.FromResult(1); }} }} }} @@ -1032,25 +1076,26 @@ public void Test() public override async Task ReportsDiagnostics_WhenSettingValueForInternalVirtualMember_AndInternalsVisibleToAppliedToWrongAssembly(string method, string call, string message) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; -using System.Runtime.CompilerServices; using NSubstitute.ExceptionExtensions; +using System.Runtime.CompilerServices; [assembly: InternalsVisibleTo(""OtherAssembly"")] namespace MyNamespace {{ public class Foo {{ - internal virtual object Bar {{ get; }} + internal virtual Task Bar {{ get; }} - internal virtual object FooBar() + internal virtual Task FooBar() {{ - return 1; + return Task.FromResult(1); }} - internal virtual object this[int x] + internal virtual Task this[int x] {{ - get {{ return 1; }} + get {{ return Task.FromResult(1); }} }} }} @@ -1070,6 +1115,7 @@ public void Test() public override async Task ReportsNoDiagnostics_WhenSettingValueForProtectedInternalVirtualMember(string method, string call) { var source = $@"using System; +using System.Threading.Tasks; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -1077,16 +1123,16 @@ namespace MyNamespace {{ public class Foo {{ - protected internal virtual object Bar {{ get; }} + protected internal virtual Task Bar {{ get; }} - protected internal virtual object FooBar() + protected internal virtual Task FooBar() {{ - return 1; + return Task.FromResult(1); }} - protected internal virtual object this[int x] + protected internal virtual Task this[int x] {{ - get {{ return 1; }} + get {{ return Task.FromResult(1); }} }} }} diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsExtensionMethodTests.cs index 18835b30..2e0c3f8d 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsExtensionMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsExtensionMethodTests.cs @@ -7,21 +7,21 @@ namespace NSubstitute.Analyzers.Tests.VisualBasic.DiagnosticAnalyzersTests.NonSubstitutableMemberAnalyzerTests; -[CombinatoryData("Throws", "ThrowsForAnyArgs")] +[CombinatoryData("Throws", "ThrowsAsync", "ThrowsForAnyArgs", "ThrowsAsyncForAnyArgs")] public class ThrowsAsExtensionMethodTests : NonSubstitutableMemberDiagnosticVerifier { public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualMethod(string method) { var source = $@"Imports System +Imports System.Threading.Tasks Imports NSubstitute Imports NSubstitute.ExceptionExtensions Namespace MyNamespace Public Class Foo - - Public Function Bar() As Integer - Return 2 + Public Function Bar() As Task(Of Integer) + Return Task.FromResult(2) End Function End Class @@ -59,6 +59,7 @@ End Namespace public override async Task ReportsDiagnostics_WhenSettingValueForStaticMethod(string method) { var source = $@"Imports System +Imports System.Threading.Tasks Imports NSubstitute Imports NSubstitute.ExceptionExtensions @@ -66,8 +67,8 @@ Namespace MyNamespace Public Class Foo - Public Shared Function Bar() As Integer - Return 2 + Public Shared Function Bar() As Task(Of Integer) + Return Task.FromResult(2) End Function End Class @@ -85,6 +86,7 @@ End Namespace public override async Task ReportsNoDiagnostics_WhenSettingValueForVirtualMethod(string method) { var source = $@"Imports System +Imports System.Threading.Tasks Imports NSubstitute Imports NSubstitute.ExceptionExtensions @@ -92,8 +94,8 @@ Namespace MyNamespace Public Class Foo - Public Overridable Function Bar() As Integer - Return 2 + Public Overridable Function Bar() As Task(Of Integer) + Return Task.FromResult(2) End Function End Class @@ -112,6 +114,7 @@ End Namespace public override async Task ReportsNoDiagnostics_WhenSettingValueForNonSealedOverrideMethod(string method) { var source = $@"Imports System +Imports System.Threading.Tasks Imports NSubstitute Imports NSubstitute.ExceptionExtensions @@ -119,16 +122,16 @@ Namespace MyNamespace Public Class Foo - Public Overridable Function Bar() As Integer - Return 2 + Public Overridable Function Bar() As Task(Of Integer) + Return Task.FromResult(2) End Function End Class Public Class Foo2 Inherits Foo - Public Overrides Function Bar() As Integer - Return 1 + Public Overrides Function Bar() As Task(Of Integer) + Return Task.FromResult(1) End Function End Class @@ -147,6 +150,7 @@ End Namespace public override async Task ReportsNoDiagnostics_WhenDataFlowAnalysisIsRequired(string method) { var source = $@"Imports System +Imports System.Threading.Tasks Imports NSubstitute Imports NSubstitute.ExceptionExtensions @@ -154,8 +158,8 @@ Namespace MyNamespace Public Class Foo - Public Overridable Function Bar() As Integer - Return 2 + Public Overridable Function Bar() As Task(Of Integer) + Return Task.FromResult(2) End Function End Class @@ -175,6 +179,7 @@ End Namespace public override async Task ReportsNoDiagnostics_WhenSettingValueForDelegate(string method) { var source = $@"Imports System +Imports System.Threading.Tasks Imports NSubstitute Imports NSubstitute.ExceptionExtensions @@ -183,7 +188,7 @@ Namespace MyNamespace Public Class FooTests Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Func(Of Integer))() + Dim substitute = NSubstitute.Substitute.[For](Of Func(Of Task(Of Integer)))() substitute().{method}(New Exception()) End Sub End Class @@ -195,6 +200,7 @@ End Namespace public override async Task ReportsDiagnostics_WhenSettingValueForSealedOverrideMethod(string method) { var source = $@"Imports System +Imports System.Threading.Tasks Imports NSubstitute Imports NSubstitute.ExceptionExtensions @@ -202,16 +208,16 @@ Namespace MyNamespace Public Class Foo - Public Overridable Function Bar() As Integer - Return 2 + Public Overridable Function Bar() As Task(Of Integer) + Return Task.FromResult(2) End Function End Class Public Class Foo2 Inherits Foo - Public NotOverridable Overrides Function Bar() As Integer - Return 1 + Public NotOverridable Overrides Function Bar() As Task(Of Integer) + Return Task.FromResult(1) End Function End Class @@ -230,6 +236,7 @@ End Namespace public override async Task ReportsNoDiagnostics_WhenSettingValueForAbstractMethod(string method) { var source = $@"Imports System +Imports System.Threading.Tasks Imports NSubstitute Imports NSubstitute.ExceptionExtensions @@ -237,7 +244,7 @@ Namespace MyNamespace Public MustInherit Class Foo - Public MustOverride Function Bar() As Integer + Public MustOverride Function Bar() As Task(Of Integer) End Class Public Class FooTests @@ -256,6 +263,7 @@ End Namespace public override async Task ReportsNoDiagnostics_WhenSettingValueForInterfaceMethod(string method) { var source = $@"Imports System +Imports System.Threading.Tasks Imports NSubstitute Imports NSubstitute.ExceptionExtensions @@ -263,7 +271,7 @@ Namespace MyNamespace Interface IFoo - Function Bar() As Integer + Function Bar() As Task(Of Integer) End Interface @@ -282,6 +290,7 @@ End Namespace public override async Task ReportsNoDiagnostics_WhenSettingValueForInterfaceProperty(string method) { var source = $@"Imports System +Imports System.Threading.Tasks Imports NSubstitute Imports NSubstitute.ExceptionExtensions @@ -289,7 +298,7 @@ Namespace MyNamespace Interface IFoo - Property Bar As Integer + Property Bar As Task(Of Integer) End Interface @@ -308,14 +317,14 @@ End Namespace public override async Task ReportsNoDiagnostics_WhenSettingValueForGenericInterfaceMethod(string method) { var source = $@"Imports System +Imports System.Threading.Tasks Imports NSubstitute Imports NSubstitute.ExceptionExtensions Namespace MyNamespace Public Interface IFoo(Of T) - - Function Bar(Of T)() As Integer + Function Bar(Of T)()As Task(Of Integer) End Interface Public Class FooTests @@ -333,6 +342,7 @@ End Class public override async Task ReportsNoDiagnostics_WhenSettingValueForAbstractProperty(string method) { var source = $@"Imports System +Imports System.Threading.Tasks Imports NSubstitute Imports NSubstitute.ExceptionExtensions @@ -340,7 +350,7 @@ Namespace MyNamespace Public MustInherit Class Foo - Public MustOverride ReadOnly Property Bar As Integer + Public MustOverride ReadOnly Property Bar As Task(Of Integer) End Class Public Class FooTests @@ -358,6 +368,7 @@ End Class public override async Task ReportsNoDiagnostics_WhenSettingValueForInterfaceIndexer(string method) { var source = $@"Imports System +Imports System.Threading.Tasks Imports NSubstitute Imports NSubstitute.ExceptionExtensions @@ -365,7 +376,7 @@ Namespace MyNamespace Public Interface IFoo - Default Property Item(ByVal i As Integer) As Integer + Default Property Item(ByVal i As Integer) As Task(Of Integer) End Interface Public Class FooTests @@ -382,6 +393,7 @@ End Class public override async Task ReportsNoDiagnostics_WhenSettingValueForVirtualProperty(string method) { var source = $@"Imports System +Imports System.Threading.Tasks Imports NSubstitute Imports NSubstitute.ExceptionExtensions @@ -389,7 +401,7 @@ Namespace MyNamespace Public Class Foo - Public Overridable ReadOnly Property Bar As Integer + Public Overridable ReadOnly Property Bar As Task(Of Integer) Get End Get End Property @@ -410,6 +422,7 @@ End Class public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualProperty(string method) { var source = $@"Imports System +Imports System.Threading.Tasks Imports NSubstitute Imports NSubstitute.ExceptionExtensions @@ -417,8 +430,9 @@ Namespace MyNamespace Public Class Foo - Public ReadOnly Property Bar As Integer + Public ReadOnly Property Bar As Task(Of Integer) Get + Return Nothing End Get End Property End Class @@ -438,6 +452,7 @@ End Class public override async Task ReportsNoDiagnostics_WhenSettingValueForVirtualIndexer(string method) { var source = $@"Imports System +Imports System.Threading.Tasks Imports NSubstitute Imports NSubstitute.ExceptionExtensions @@ -445,7 +460,7 @@ Namespace MyNamespace Public Class Foo - Public Overridable Default Property Item(ByVal x As Integer) As Integer + Public Overridable Default Property Item(ByVal x As Integer) As Task(Of Integer) Set Throw New NotImplementedException End Set @@ -471,6 +486,7 @@ End Class public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualIndexer(string method) { var source = $@"Imports System +Imports System.Threading.Tasks Imports NSubstitute Imports NSubstitute.ExceptionExtensions @@ -478,7 +494,7 @@ Namespace MyNamespace Public Class Foo - Public Default ReadOnly Property Item(ByVal x As Integer) As Integer + Public Default ReadOnly Property Item(ByVal x As Integer) As Task(Of Integer) Get Throw New NotImplementedException End Get @@ -501,10 +517,12 @@ public override async Task ReportsNoDiagnostics_WhenUsingUnfortunatelyNamedMetho { var source = $@"Imports System.Runtime.CompilerServices Imports System +Imports System.Threading.Tasks + Namespace NSubstitute Public Class Foo - Public Function Bar() As Integer - Return 1 + Public Function Bar() As Task(Of Integer) + Return Task.FromResult(1) End Function End Class @@ -514,10 +532,20 @@ Function Throws(Of T)(ByVal returnValue As T, ex As Exception) As T Return Nothing End Function + + Function ThrowsAsync(Of T)(ByVal returnValue As Task(Of T), ex As Exception) As T + Return Nothing + End Function + Function ThrowsForAnyArgs(Of T)(ByVal returnValue As T, ex As Exception) As T Return Nothing End Function + + + Function ThrowsAsyncForAnyArgs(Of T)(ByVal returnValue As Task(Of T), ex As Exception) As T + Return Nothing + End Function End Module Public Class FooTests @@ -536,13 +564,14 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("P:MyNamespace.Foo.Bar", DiagnosticIdentifiers.NonVirtualSetupSpecification); var source = $@"Imports System +Imports System.Threading.Tasks Imports NSubstitute Imports NSubstitute.ExceptionExtensions Namespace MyNamespace Public Class Foo - Public ReadOnly Property Bar As Integer - Public ReadOnly Property FooBar As Integer + Public ReadOnly Property Bar As Task(Of Integer) + Public ReadOnly Property FooBar As Task(Of Integer) End Class Public Class FooTests @@ -563,18 +592,19 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("P:MyNamespace.Foo`1.Bar", DiagnosticIdentifiers.NonVirtualSetupSpecification); var source = $@"Imports System +Imports System.Threading.Tasks Imports NSubstitute Imports NSubstitute.ExceptionExtensions Namespace MyNamespace - Public Class Foo(Of T) + Public Class Foo(Of T As Task) Public ReadOnly Property Bar As T - Public ReadOnly Property FooBar As Integer + Public ReadOnly Property FooBar As Task(Of Integer) End Class Public Class FooTests Public Sub Test() - Dim substitute = NSubstitute.Substitute.[For](Of Foo(Of Integer))() + Dim substitute = NSubstitute.Substitute.[For](Of Foo(Of Task))() substitute.Bar.{method}(New Exception()) [|substitute.FooBar|].{method}(New Exception()) End Sub @@ -590,17 +620,18 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("M:MyNamespace.Foo.Bar(System.Int32,System.Int32)", DiagnosticIdentifiers.NonVirtualSetupSpecification); var source = $@"Imports System +Imports System.Threading.Tasks Imports NSubstitute Imports NSubstitute.ExceptionExtensions Namespace MyNamespace Public Class Foo - Public Function Bar(ByVal x As Integer) As Integer - Return 1 + Public Function Bar(ByVal x As Integer) As Task(Of Integer) + Return Task.FromResult(0) End Function - Public Function Bar(ByVal x As Integer, ByVal y As Integer) As Integer - Return 2 + Public Function Bar(ByVal x As Integer, ByVal y As Integer) As Task(Of Integer) + Return Task.FromResult(0) End Function End Class @@ -622,17 +653,18 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("M:MyNamespace.Foo.Bar``1(``0,``0)", DiagnosticIdentifiers.NonVirtualSetupSpecification); var source = $@"Imports System +Imports System.Threading.Tasks Imports NSubstitute Imports NSubstitute.ExceptionExtensions Namespace MyNamespace Public Class Foo - Public Function Bar(ByVal x As Integer) As Integer - Return 1 + Public Function Bar(ByVal x As Integer) As Task(Of Integer) + Return Task.FromResult(0) End Function - Public Function Bar(Of T)(ByVal x As T, ByVal y As T) As Integer - Return 2 + Public Function Bar(Of T)(ByVal x As T, ByVal y As T) As Task(Of Integer) + Return Task.FromResult(2) End Function End Class @@ -654,20 +686,21 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("P:MyNamespace.Foo.Item(System.Int32,System.Int32)", DiagnosticIdentifiers.NonVirtualSetupSpecification); var source = $@"Imports System +Imports System.Threading.Tasks Imports NSubstitute Imports NSubstitute.ExceptionExtensions Namespace MyNamespace Public Class Foo - Default Public ReadOnly Property Item(ByVal x As Integer) As Integer + Default Public ReadOnly Property Item(ByVal x As Integer) As Task(Of Integer) Get - Return 0 + Return Task.FromResult(0) End Get End Property - Default Public ReadOnly Property Item(ByVal x As Integer, ByVal y As Integer) As Integer + Default Public ReadOnly Property Item(ByVal x As Integer, ByVal y As Integer) As Task(Of Integer) Get - Return 0 + Return Task.FromResult(0) End Get End Property End Class @@ -690,20 +723,21 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("P:MyNamespace.Foo`1.Item(`0,`0)", DiagnosticIdentifiers.NonVirtualSetupSpecification); var source = $@"Imports System +Imports System.Threading.Tasks Imports NSubstitute Imports NSubstitute.ExceptionExtensions Namespace MyNamespace Public Class Foo(Of T) - Default Public ReadOnly Property Item(ByVal x As T) As Integer + Default Public ReadOnly Property Item(ByVal x As T) As Task(Of Integer) Get - Return 0 + Return Task.FromResult(0) End Get End Property - Default Public ReadOnly Property Item(ByVal x As T, ByVal y As T) As Integer + Default Public ReadOnly Property Item(ByVal x As T, ByVal y As T) As Task(Of Integer) Get - Return 0 + Return Task.FromResult(0) End Get End Property End Class @@ -726,35 +760,36 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("T:MyNamespace.Foo", DiagnosticIdentifiers.NonVirtualSetupSpecification); var source = $@"Imports System +Imports System.Threading.Tasks Imports NSubstitute Imports NSubstitute.ExceptionExtensions Namespace MyNamespace Public Class Foo - Public Property Bar As Integer + Public Property Bar As Task(Of Integer) - Default Public ReadOnly Property Item(ByVal x As Integer) As Integer + Default Public ReadOnly Property Item(ByVal x As Integer) As Task(Of Integer) Get - Return 0 + Return Task.FromResult(0) End Get End Property - Public Function FooBar() As Integer - Return 1 + Public Function FooBar() As Task(Of Integer) + Return Task.FromResult(1) End Function End Class Public Class FooBarBar - Public Property Bar As Integer + Public Property Bar As Task(Of Integer) - Default Public ReadOnly Property Item(ByVal x As Integer) As Integer + Default Public ReadOnly Property Item(ByVal x As Integer) As Task(Of Integer) Get - Return 0 + Return Task.FromResult(0) End Get End Property - Public Function FooBar() As Integer - Return 1 + Public Function FooBar() As Task(Of Integer) + Return Task.FromResult(1) End Function End Class @@ -792,35 +827,36 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("T:MyNamespace.Foo`1", DiagnosticIdentifiers.NonVirtualSetupSpecification); var source = $@"Imports System +Imports System.Threading.Tasks Imports NSubstitute Imports NSubstitute.ExceptionExtensions Namespace MyNamespace Public Class Foo(Of T) - Public Property Bar As Integer + Public Property Bar As Task(Of Integer) - Default Public ReadOnly Property Item(ByVal x As Integer) As Integer + Default Public ReadOnly Property Item(ByVal x As Integer) As Task(Of Integer) Get - Return 0 + Return Task.FromResult(0) End Get End Property - Public Function FooBar() As Integer - Return 1 + Public Function FooBar() As Task(Of Integer) + Return Task.FromResult(1) End Function End Class Public Class FooBarBar(Of T) - Public Property Bar As Integer + Public Property Bar As Task(Of Integer) - Default Public ReadOnly Property Item(ByVal x As Integer) As Integer + Default Public ReadOnly Property Item(ByVal x As Integer) As Task(Of Integer) Get - Return 0 + Return Task.FromResult(0) End Get End Property - Public Function FooBar() As Integer - Return 1 + Public Function FooBar() As Task(Of Integer) + Return Task.FromResult(1) End Function End Class @@ -858,38 +894,39 @@ public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressi Settings = AnalyzersSettings.CreateWithSuppressions("N:MyNamespace", DiagnosticIdentifiers.NonVirtualSetupSpecification); var source = $@"Imports System +Imports System.Threading.Tasks Imports NSubstitute Imports NSubstitute.ExceptionExtensions Imports MyOtherNamespace Namespace MyOtherNamespace Public Class FooBarBar - Public Property Bar As Integer + Public Property Bar As Task(Of Integer) - Default Public ReadOnly Property Item(ByVal x As Integer) As Integer + Default Public ReadOnly Property Item(ByVal x As Integer) As Task(Of Integer) Get - Return 0 + Return Task.FromResult(0) End Get End Property - Public Function FooBar() As Integer - Return 1 + Public Function FooBar() As Task(Of Integer) + Return Task.FromResult(1) End Function End Class End Namespace Namespace MyNamespace Public Class Foo - Public Property Bar As Integer + Public Property Bar As Task(Of Integer) - Default Public ReadOnly Property Item(ByVal x As Integer) As Integer + Default Public ReadOnly Property Item(ByVal x As Integer) As Task(Of Integer) Get - Return 0 + Return Task.FromResult(0) End Get End Property - Public Function FooBar() As Integer - Return 1 + Public Function FooBar() As Task(Of Integer) + Return Task.FromResult(1) End Function End Class @@ -924,9 +961,10 @@ End Namespace public override async Task ReportsNoDiagnosticsForSuppressedMember_WhenSuppressingExtensionMethod(string method) { - Settings = AnalyzersSettings.CreateWithSuppressions("M:MyNamespace.MyExtensions.GetBar(MyNamespace.IFoo)~System.Int32", DiagnosticIdentifiers.NonVirtualSetupSpecification); + Settings = AnalyzersSettings.CreateWithSuppressions("M:MyNamespace.MyExtensions.GetBar(MyNamespace.IFoo)~System.Threading.Tasks.Task{System.Int32}", DiagnosticIdentifiers.NonVirtualSetupSpecification); var source = $@"Imports System +Imports System.Threading.Tasks Imports NSubstitute Imports NSubstitute.ExceptionExtensions Imports System.Runtime.CompilerServices @@ -945,23 +983,22 @@ Module MyExtensions Public Property Bar As IBar - Function GetBar(ByVal foo As IFoo) As Integer + Function GetBar(ByVal foo As IFoo) As Task(Of Integer) Return Bar.Foo() - Return 1 End Function - Function GetFooBar(ByVal foo As IFoo) As Integer - Return 1 + Function GetFooBar(ByVal foo As IFoo) As Task(Of Integer) + Return Task.FromResult(1) End Function End Module Interface IBar - Function Foo() As Integer + Function Foo() As Task(Of Integer) End Interface Interface IFoo - Function Bar() As Integer + Function Bar() As Task(Of Integer) End Interface End Namespace"; @@ -971,18 +1008,19 @@ End Interface public override async Task ReportsDiagnostics_WhenSettingValueForInternalVirtualMember_AndInternalsVisibleToNotApplied(string method, string call, string message) { var source = $@"Imports System +Imports System.Threading.Tasks Imports NSubstitute Imports NSubstitute.ExceptionExtensions Namespace MyNamespace Public Class Foo - Friend Overridable ReadOnly Property Bar As Foo + Friend Overridable ReadOnly Property Bar As Task(Of Foo) - Friend Overridable Function FooBar() As Foo + Friend Overridable Function FooBar() As Task(Of Foo) Return Nothing End Function - Default Friend Overridable ReadOnly Property Item(ByVal x As Integer) As Foo + Default Friend Overridable ReadOnly Property Item(ByVal x As Integer) As Task(Of Foo) Get Return Nothing End Get @@ -1003,6 +1041,7 @@ End Class public override async Task ReportsNoDiagnostics_WhenSettingValueForInternalVirtualMember_AndInternalsVisibleToApplied(string method, string call) { var source = $@"Imports System +Imports System.Threading.Tasks Imports NSubstitute Imports NSubstitute.ExceptionExtensions @@ -1012,13 +1051,13 @@ Imports NSubstitute.ExceptionExtensions Namespace MyNamespace Public Class Foo - Friend Overridable ReadOnly Property Bar As Foo + Friend Overridable ReadOnly Property Bar As Task(Of Foo) - Friend Overridable Function FooBar() As Foo + Friend Overridable Function FooBar() As Task(Of Foo) Return Nothing End Function - Default Friend Overridable ReadOnly Property Item(ByVal x As Integer) As Foo + Default Friend Overridable ReadOnly Property Item(ByVal x As Integer) As Task(Of Foo) Get Return Nothing End Get @@ -1039,6 +1078,7 @@ End Class public override async Task ReportsDiagnostics_WhenSettingValueForInternalVirtualMember_AndInternalsVisibleToAppliedToWrongAssembly(string method, string call, string message) { var source = $@"Imports System +Imports System.Threading.Tasks Imports NSubstitute Imports NSubstitute.ExceptionExtensions @@ -1046,13 +1086,13 @@ Imports NSubstitute.ExceptionExtensions Namespace MyNamespace Public Class Foo - Friend Overridable ReadOnly Property Bar As Foo + Friend Overridable ReadOnly Property Bar As Task(Of Foo) - Friend Overridable Function FooBar() As Foo + Friend Overridable Function FooBar() As Task(Of Foo) Return Nothing End Function - Default Friend Overridable ReadOnly Property Item(ByVal x As Integer) As Foo + Default Friend Overridable ReadOnly Property Item(ByVal x As Integer) As Task(Of Foo) Get Return Nothing End Get @@ -1073,18 +1113,19 @@ End Class public override async Task ReportsNoDiagnostics_WhenSettingValueForProtectedInternalVirtualMember(string method, string call) { var source = $@"Imports System +Imports System.Threading.Tasks Imports NSubstitute Imports NSubstitute.ExceptionExtensions Namespace MyNamespace Public Class Foo - Protected Friend Overridable ReadOnly Property Bar As Foo + Protected Friend Overridable ReadOnly Property Bar As Task(Of Foo) - Protected Friend Overridable Function FooBar() As Foo + Protected Friend Overridable Function FooBar() As Task(Of Foo) Return Nothing End Function - Default Protected Friend Overridable ReadOnly Property Item(ByVal x As Integer) As Foo + Default Protected Friend Overridable ReadOnly Property Item(ByVal x As Integer) As Task(Of Foo) Get Return Nothing End Get diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodTests.cs index 02721017..b9e3654a 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodTests.cs @@ -7,7 +7,11 @@ namespace NSubstitute.Analyzers.Tests.VisualBasic.DiagnosticAnalyzersTests.NonSubstitutableMemberAnalyzerTests; -[CombinatoryData("ExceptionExtensions.Throws", "ExceptionExtensions.ThrowsForAnyArgs")] +[CombinatoryData( + "ExceptionExtensions.Throws", + "ExceptionExtensions.ThrowsAsync", + "ExceptionExtensions.ThrowsForAnyArgs", + "ExceptionExtensions.ThrowsAsyncForAnyArgs")] public class ThrowsAsOrdinaryMethodTests : NonSubstitutableMemberDiagnosticVerifier { public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualMethod(string method) diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodWithGenericTypeSpecifiedTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodWithGenericTypeSpecifiedTests.cs index 254e3974..23ce1c5a 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodWithGenericTypeSpecifiedTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/NonSubstitutableMemberAnalyzerTests/ThrowsAsOrdinaryMethodWithGenericTypeSpecifiedTests.cs @@ -7,7 +7,11 @@ namespace NSubstitute.Analyzers.Tests.VisualBasic.DiagnosticAnalyzersTests.NonSubstitutableMemberAnalyzerTests; -[CombinatoryData("ExceptionExtensions.Throws(Of Exception)", "ExceptionExtensions.ThrowsForAnyArgs(Of Exception)")] +[CombinatoryData( + "ExceptionExtensions.Throws(Of Exception)", + "ExceptionExtensions.ThrowsAsync(Of Exception)", + "ExceptionExtensions.ThrowsForAnyArgs(Of Exception)", + "ExceptionExtensions.ThrowsAsyncForAnyArgs(Of Exception)")] public class ThrowsAsOrdinaryMethodWithGenericTypeSpecifiedTests : NonSubstitutableMemberDiagnosticVerifier { public override async Task ReportsDiagnostics_WhenSettingValueForNonVirtualMethod(string method)