Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/release_packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ jobs:
dotnet-version: 9.0.x

- name: Build package
run: dotnet pack src/NSubstitute/NSubstitute.csproj -p:CI=true
run: dotnet pack src/NSubstitute/NSubstitute.csproj -o bin -p:CI=true

- name: Upload packages
uses: actions/upload-artifact@v4
with:
name: packages
path: |
bin/Release/NSubstitute/*.nupkg
bin/Release/NSubstitute/*.snupkg
bin/*.nupkg
bin/*.snupkg
retention-days: 7
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* [UPDATE] Migrate to slnx format for solution file
* [UPDATE] Migrate documentation validation from build.fsproj to Roslyn code generator
* [NEW] Added NuGet Package README file.
* [UPDATE] Make public api and tests the same for all TFMs

### 5.3.0 (October 2024)

Expand Down
48 changes: 47 additions & 1 deletion src/NSubstitute/Extensions/ExceptionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,18 @@ public static ConfiguredCall ThrowsAsyncForAnyArgs(this Task value, Func<CallInf
public static ConfiguredCall ThrowsAsyncForAnyArgs<T>(this Task<T> value, Func<CallInfo, Exception> createException) =>
value.ReturnsForAnyArgs(ci => Task.FromException<T>(createException(ci)));

#if NET5_0_OR_GREATER
/// <summary>
/// Throw an exception for this call.
/// </summary>
/// <param name="value"></param>
/// <param name="ex">Exception to throw</param>
/// <returns></returns>
public static ConfiguredCall ThrowsAsync<T>(this ValueTask<T> value, Exception ex) =>
#if NETSTANDARD2_0
value.Returns(_ => new ValueTask<T>(Task.FromException<T>(ex)));
#else
value.Returns(_ => ValueTask.FromException<T>(ex));
#endif

/// <summary>
/// Throw an exception of the given type for this call.
Expand All @@ -181,7 +184,11 @@ public static ConfiguredCall ThrowsAsync<T>(this ValueTask<T> value, Exception e
public static ConfiguredCall ThrowsAsync<TResult, TException>(this ValueTask<TResult> value)
where TException : notnull, Exception, new()
{
#if NETSTANDARD2_0
return value.Returns(_ => new ValueTask<TResult>(Task.FromException<TResult>(new TException())));
#else
return value.Returns(_ => ValueTask.FromException<TResult>(new TException()));
#endif
}

/// <summary>
Expand All @@ -191,7 +198,11 @@ public static ConfiguredCall ThrowsAsync<TResult, TException>(this ValueTask<TRe
/// <param name="createException">Func creating exception object</param>
/// <returns></returns>
public static ConfiguredCall ThrowsAsync<T>(this ValueTask<T> value, Func<CallInfo, Exception> createException) =>
#if NETSTANDARD2_0
value.Returns(ci => new ValueTask<T>(Task.FromException<T>(createException(ci))));
#else
value.Returns(ci => ValueTask.FromException<T>(createException(ci)));
#endif

/// <summary>
/// Throws an exception of the given type for this call made with any arguments.
Expand All @@ -203,7 +214,11 @@ public static ConfiguredCall ThrowsAsync<T>(this ValueTask<T> value, Func<CallIn
public static ConfiguredCall ThrowsAsyncForAnyArgs<T, TException>(this ValueTask<T> value)
where TException : notnull, Exception, new()
{
#if NETSTANDARD2_0
return value.ReturnsForAnyArgs(_ => new ValueTask<T>(Task.FromException<T>(new TException())));
#else
return value.ReturnsForAnyArgs(_ => ValueTask.FromException<T>(new TException()));
#endif
}

/// <summary>
Expand All @@ -213,7 +228,11 @@ public static ConfiguredCall ThrowsAsyncForAnyArgs<T, TException>(this ValueTask
/// <param name="ex">Exception to throw</param>
/// <returns></returns>
public static ConfiguredCall ThrowsAsyncForAnyArgs<T>(this ValueTask<T> value, Exception ex) =>
#if NETSTANDARD2_0
value.ReturnsForAnyArgs(_ => new ValueTask<T>(Task.FromException<T>(ex)));
#else
value.ReturnsForAnyArgs(_ => ValueTask.FromException<T>(ex));
#endif

/// <summary>
/// Throws an exception for this call made with any arguments, as generated by the specified function.
Expand All @@ -222,7 +241,11 @@ public static ConfiguredCall ThrowsAsyncForAnyArgs<T>(this ValueTask<T> value, E
/// <param name="createException">Func creating exception object</param>
/// <returns></returns>
public static ConfiguredCall ThrowsAsyncForAnyArgs<T>(this ValueTask<T> value, Func<CallInfo, Exception> createException) =>
#if NETSTANDARD2_0
value.ReturnsForAnyArgs(ci => new ValueTask<T>(Task.FromException<T>(createException(ci))));
#else
value.ReturnsForAnyArgs(ci => ValueTask.FromException<T>(createException(ci)));
#endif

/// <summary>
/// Throw an exception for this call.
Expand All @@ -231,7 +254,11 @@ public static ConfiguredCall ThrowsAsyncForAnyArgs<T>(this ValueTask<T> value, F
/// <param name="ex">Exception to throw</param>
/// <returns></returns>
public static ConfiguredCall ThrowsAsync(this ValueTask value, Exception ex) =>
#if NETSTANDARD2_0
value.Returns(_ => new ValueTask(Task.FromException(ex)));
#else
value.Returns(_ => ValueTask.FromException(ex));
#endif

/// <summary>
/// Throw an exception of the given type for this call.
Expand All @@ -242,7 +269,11 @@ public static ConfiguredCall ThrowsAsync(this ValueTask value, Exception ex) =>
public static ConfiguredCall ThrowsAsync<TException>(this ValueTask value)
where TException : notnull, Exception, new()
{
#if NETSTANDARD2_0
return value.Returns(_ => new ValueTask(Task.FromException(new TException())));
#else
return value.Returns(_ => ValueTask.FromException(new TException()));
#endif
}

/// <summary>
Expand All @@ -252,7 +283,11 @@ public static ConfiguredCall ThrowsAsync<TException>(this ValueTask value)
/// <param name="createException">Func creating exception object</param>
/// <returns></returns>
public static ConfiguredCall ThrowsAsync(this ValueTask value, Func<CallInfo, Exception> createException) =>
#if NETSTANDARD2_0
value.Returns(ci => new ValueTask(Task.FromException(createException(ci))));
#else
value.Returns(ci => ValueTask.FromException(createException(ci)));
#endif

/// <summary>
/// Throws an exception of the given type for this call made with any arguments.
Expand All @@ -263,7 +298,11 @@ public static ConfiguredCall ThrowsAsync(this ValueTask value, Func<CallInfo, Ex
public static ConfiguredCall ThrowsAsyncForAnyArgs<TException>(this ValueTask value)
where TException : notnull, Exception, new()
{
#if NETSTANDARD2_0
return value.ReturnsForAnyArgs(_ => new ValueTask(Task.FromException(new TException())));
#else
return value.ReturnsForAnyArgs(_ => ValueTask.FromException(new TException()));
#endif
}

/// <summary>
Expand All @@ -273,7 +312,11 @@ public static ConfiguredCall ThrowsAsyncForAnyArgs<TException>(this ValueTask va
/// <param name="ex">Exception to throw</param>
/// <returns></returns>
public static ConfiguredCall ThrowsAsyncForAnyArgs(this ValueTask value, Exception ex) =>
#if NETSTANDARD2_0
value.ReturnsForAnyArgs(_ => new ValueTask(Task.FromException(ex)));
#else
value.ReturnsForAnyArgs(_ => ValueTask.FromException(ex));
#endif

/// <summary>
/// Throws an exception for this call made with any arguments, as generated by the specified function.
Expand All @@ -282,6 +325,9 @@ public static ConfiguredCall ThrowsAsyncForAnyArgs(this ValueTask value, Excepti
/// <param name="createException">Func creating exception object</param>
/// <returns></returns>
public static ConfiguredCall ThrowsAsyncForAnyArgs(this ValueTask value, Func<CallInfo, Exception> createException) =>
#if NETSTANDARD2_0
value.ReturnsForAnyArgs(ci => new ValueTask(Task.FromException(createException(ci))));
#else
value.ReturnsForAnyArgs(ci => ValueTask.FromException(createException(ci)));
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/NSubstitute/NSubstitute.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.3.0-*" />
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
</ItemGroup>

</Project>
2 changes: 0 additions & 2 deletions tests/NSubstitute.Acceptance.Specs/ThrowingAsyncExceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ public void TearDown()
}
}

#if NET5_0_OR_GREATER
public class ForValueTask
{

Expand Down Expand Up @@ -421,7 +420,6 @@ public static void AssertDoesNotThrow(Func<ValueTask> act)
Assert.That(actual.IsFaulted, Is.False);
}
}
#endif

public static TException AssertFaultedTaskException<TException>(Func<Task> act)
where TException : Exception
Expand Down
Loading