From 4d262d3b85003bf7bedda9584b5547a6bf8659eb Mon Sep 17 00:00:00 2001 From: Romfos Date: Mon, 25 Dec 2023 04:07:49 +0300 Subject: [PATCH] Format NSubstitute.csproj layout + remove unusable code for net4.5 (#761) * Format NSubstitute.csproj layout + remove unusable code * Remove Microsoft.SourceLink.GitHub * Code review --- .../Extensions/ExceptionExtensions.cs | 41 ++++-------- src/NSubstitute/NSubstitute.csproj | 64 ++++++++----------- .../ThrowingAsyncExceptions.cs | 10 ++- 3 files changed, 43 insertions(+), 72 deletions(-) diff --git a/src/NSubstitute/Extensions/ExceptionExtensions.cs b/src/NSubstitute/Extensions/ExceptionExtensions.cs index 030f67fc..30f4f4ed 100644 --- a/src/NSubstitute/Extensions/ExceptionExtensions.cs +++ b/src/NSubstitute/Extensions/ExceptionExtensions.cs @@ -1,8 +1,8 @@ -using System; +using NSubstitute.Core; +using System; using System.Linq; using System.Reflection; using System.Threading.Tasks; -using NSubstitute.Core; // Disable nullability for client API, so it does not affect clients. #nullable disable annotations @@ -78,7 +78,7 @@ public static ConfiguredCall ThrowsForAnyArgs(this object value, FuncException to throw /// public static ConfiguredCall ThrowsAsync(this Task value, Exception ex) => - value.Returns(_ => TaskFromException(ex)); + value.Returns(_ => Task.FromException(ex)); /// /// Throw an exception for this call. @@ -87,7 +87,7 @@ public static ConfiguredCall ThrowsAsync(this Task value, Exception ex) => /// Exception to throw /// public static ConfiguredCall ThrowsAsync(this Task value, Exception ex) => - value.Returns(_ => TaskFromException(ex)); + value.Returns(_ => Task.FromException(ex)); /// /// Throw an exception of the given type for this call. @@ -108,7 +108,7 @@ public static ConfiguredCall ThrowsAsync(this Task value) /// Func creating exception object /// public static ConfiguredCall ThrowsAsync(this Task value, Func createException) => - value.Returns(ci => TaskFromException(createException(ci))); + value.Returns(ci => Task.FromException(createException(ci))); /// /// Throw an exception for this call, as generated by the specified function. @@ -117,7 +117,7 @@ public static ConfiguredCall ThrowsAsync(this Task value, FuncFunc creating exception object /// public static ConfiguredCall ThrowsAsync(this Task value, Func createException) => - value.Returns(ci => TaskFromException(createException(ci))); + value.Returns(ci => Task.FromException(createException(ci))); /// /// Throw an exception for this call made with any arguments. @@ -126,7 +126,7 @@ public static ConfiguredCall ThrowsAsync(this Task value, FuncException to throw /// public static ConfiguredCall ThrowsAsyncForAnyArgs(this Task value, Exception ex) => - value.ReturnsForAnyArgs(_ => TaskFromException(ex)); + value.ReturnsForAnyArgs(_ => Task.FromException(ex)); /// /// Throw an exception for this call made with any arguments. @@ -135,7 +135,7 @@ public static ConfiguredCall ThrowsAsyncForAnyArgs(this Task value, Exception ex /// Exception to throw /// public static ConfiguredCall ThrowsAsyncForAnyArgs(this Task value, Exception ex) => - value.ReturnsForAnyArgs(_ => TaskFromException(ex)); + value.ReturnsForAnyArgs(_ => Task.FromException(ex)); /// /// Throws an exception of the given type for this call made with any arguments. @@ -156,7 +156,7 @@ public static ConfiguredCall ThrowsAsyncForAnyArgs(this Task value) /// Func creating exception object /// public static ConfiguredCall ThrowsAsyncForAnyArgs(this Task value, Func createException) => - value.ReturnsForAnyArgs(ci => TaskFromException(createException(ci))); + value.ReturnsForAnyArgs(ci => Task.FromException(createException(ci))); /// /// Throws an exception for this call made with any arguments, as generated by the specified function. @@ -165,7 +165,7 @@ public static ConfiguredCall ThrowsAsyncForAnyArgs(this Task value, FuncFunc creating exception object /// public static ConfiguredCall ThrowsAsyncForAnyArgs(this Task value, Func createException) => - value.ReturnsForAnyArgs(ci => TaskFromException(createException(ci))); + value.ReturnsForAnyArgs(ci => Task.FromException(createException(ci))); #if NET5_0_OR_GREATER /// @@ -242,26 +242,7 @@ private static object FromException(object value, Exception exception) return specificFromExceptionMethod.Invoke(null, new object[] { exception }); } - return TaskFromException(exception); + return Task.FromException(exception); } - - private static Task TaskFromException(Exception ex) - { -#if NET45 - return new Task(() => throw ex); -#else - return Task.FromException(ex); -#endif - } - - private static Task TaskFromException(Exception ex) - { -#if NET45 - return new Task(() => throw ex); -#else - return Task.FromException(ex); -#endif - } - } } diff --git a/src/NSubstitute/NSubstitute.csproj b/src/NSubstitute/NSubstitute.csproj index f6ffa0d3..39749dde 100644 --- a/src/NSubstitute/NSubstitute.csproj +++ b/src/NSubstitute/NSubstitute.csproj @@ -1,8 +1,19 @@  + + net6.0;netstandard2.0;net462 + true + true + true + nsubstitute.snk + false + ..\..\bin\$(Configuration)\NSubstitute\$(TargetFramework)\NSubstitute.xml + $(NoWarn);1591 + + NSubstitute is a friendly substitute for .NET mocking libraries. It has a simple, succinct syntax to help developers write clearer tests. NSubstitute is designed for Arrange-Act-Assert (AAA) testing and with Test Driven Development (TDD) in mind. - 3.0.0 + 5.0.0 Anthony Egerton;David Tchepak;Alexandr Nikitin;Oleksandr Povar NSubstitute NSubstitute @@ -14,44 +25,15 @@ git - - - - - - net6.0;netstandard2.0;net462 - - - - - - - - - - - - true - true - true - nsubstitute.snk - false - - - - ..\..\bin\$(Configuration)\NSubstitute\$(TargetFramework)\NSubstitute.xml - $(NoWarn);1701;1702;1705;1591 + + $(DefineConstants);SYSTEM_DIAGNOSTICS_CODEANALYSIS_NULLABILITY + enable - - enable + - $(NoWarn);CS8632 - - - - $(DefineConstants);SYSTEM_DIAGNOSTICS_CODEANALYSIS_NULLABILITY + $(NoWarn);CS8632 @@ -61,7 +43,17 @@ true snupkg + - + + + + + + + + + + diff --git a/tests/NSubstitute.Acceptance.Specs/ThrowingAsyncExceptions.cs b/tests/NSubstitute.Acceptance.Specs/ThrowingAsyncExceptions.cs index 12be33c8..792cd3c3 100644 --- a/tests/NSubstitute.Acceptance.Specs/ThrowingAsyncExceptions.cs +++ b/tests/NSubstitute.Acceptance.Specs/ThrowingAsyncExceptions.cs @@ -1,10 +1,9 @@ -#if !NET45 +using NSubstitute.Acceptance.Specs.Infrastructure; +using NSubstitute.ExceptionExtensions; +using NUnit.Framework; using System; using System.Linq; using System.Threading.Tasks; -using NSubstitute.Acceptance.Specs.Infrastructure; -using NSubstitute.ExceptionExtensions; -using NUnit.Framework; namespace NSubstitute.Acceptance.Specs { @@ -338,5 +337,4 @@ public static TException AssertFaultedTaskException(Func act) return actual.Exception!.InnerExceptions.First() as TException; } } -} -#endif \ No newline at end of file +} \ No newline at end of file