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); }} }} }}