Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format NSubstitute.csproj layout + remove unusable code for net4.5 #761

Merged
merged 3 commits into from
Dec 25, 2023
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
41 changes: 11 additions & 30 deletions src/NSubstitute/Extensions/ExceptionExtensions.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -78,7 +78,7 @@ public static ConfiguredCall ThrowsForAnyArgs(this object value, Func<CallInfo,
/// <param name="ex">Exception to throw</param>
/// <returns></returns>
public static ConfiguredCall ThrowsAsync(this Task value, Exception ex) =>
value.Returns(_ => TaskFromException(ex));
value.Returns(_ => Task.FromException(ex));

/// <summary>
/// Throw an exception for this call.
Expand All @@ -87,7 +87,7 @@ public static ConfiguredCall ThrowsAsync(this Task value, Exception ex) =>
/// <param name="ex">Exception to throw</param>
/// <returns></returns>
public static ConfiguredCall ThrowsAsync<T>(this Task<T> value, Exception ex) =>
value.Returns(_ => TaskFromException<T>(ex));
value.Returns(_ => Task.FromException<T>(ex));

/// <summary>
/// Throw an exception of the given type for this call.
Expand All @@ -108,7 +108,7 @@ public static ConfiguredCall ThrowsAsync<TException>(this Task value)
/// <param name="createException">Func creating exception object</param>
/// <returns></returns>
public static ConfiguredCall ThrowsAsync(this Task value, Func<CallInfo, Exception> createException) =>
value.Returns(ci => TaskFromException(createException(ci)));
value.Returns(ci => Task.FromException(createException(ci)));

/// <summary>
/// Throw an exception for this call, as generated by the specified function.
Expand All @@ -117,7 +117,7 @@ public static ConfiguredCall ThrowsAsync(this Task value, Func<CallInfo, Excepti
/// <param name="createException">Func creating exception object</param>
/// <returns></returns>
public static ConfiguredCall ThrowsAsync<T>(this Task<T> value, Func<CallInfo, Exception> createException) =>
value.Returns(ci => TaskFromException<T>(createException(ci)));
value.Returns(ci => Task.FromException<T>(createException(ci)));

/// <summary>
/// Throw an exception for this call made with any arguments.
Expand All @@ -126,7 +126,7 @@ public static ConfiguredCall ThrowsAsync<T>(this Task<T> value, Func<CallInfo, E
/// <param name="ex">Exception to throw</param>
/// <returns></returns>
public static ConfiguredCall ThrowsAsyncForAnyArgs(this Task value, Exception ex) =>
value.ReturnsForAnyArgs(_ => TaskFromException(ex));
value.ReturnsForAnyArgs(_ => Task.FromException(ex));

/// <summary>
/// Throw an exception for this call made with any arguments.
Expand All @@ -135,7 +135,7 @@ public static ConfiguredCall ThrowsAsyncForAnyArgs(this Task value, Exception ex
/// <param name="ex">Exception to throw</param>
/// <returns></returns>
public static ConfiguredCall ThrowsAsyncForAnyArgs<T>(this Task<T> value, Exception ex) =>
value.ReturnsForAnyArgs(_ => TaskFromException<T>(ex));
value.ReturnsForAnyArgs(_ => Task.FromException<T>(ex));

/// <summary>
/// Throws an exception of the given type for this call made with any arguments.
Expand All @@ -156,7 +156,7 @@ public static ConfiguredCall ThrowsAsyncForAnyArgs<TException>(this Task value)
/// <param name="createException">Func creating exception object</param>
/// <returns></returns>
public static ConfiguredCall ThrowsAsyncForAnyArgs(this Task value, Func<CallInfo, Exception> createException) =>
value.ReturnsForAnyArgs(ci => TaskFromException(createException(ci)));
value.ReturnsForAnyArgs(ci => Task.FromException(createException(ci)));

/// <summary>
/// Throws an exception for this call made with any arguments, as generated by the specified function.
Expand All @@ -165,7 +165,7 @@ public static ConfiguredCall ThrowsAsyncForAnyArgs(this Task value, Func<CallInf
/// <param name="createException">Func creating exception object</param>
/// <returns></returns>
public static ConfiguredCall ThrowsAsyncForAnyArgs<T>(this Task<T> value, Func<CallInfo, Exception> createException) =>
value.ReturnsForAnyArgs(ci => TaskFromException<T>(createException(ci)));
value.ReturnsForAnyArgs(ci => Task.FromException<T>(createException(ci)));

#if NET5_0_OR_GREATER
/// <summary>
Expand Down Expand Up @@ -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<T> TaskFromException<T>(Exception ex)
{
#if NET45
return new Task<T>(() => throw ex);
#else
return Task<T>.FromException<T>(ex);
#endif
}

}
}
64 changes: 28 additions & 36 deletions src/NSubstitute/NSubstitute.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;netstandard2.0;net462</TargetFrameworks>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<SignAssembly>true</SignAssembly>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
<AssemblyOriginatorKeyFile>nsubstitute.snk</AssemblyOriginatorKeyFile>
<DelaySign>false</DelaySign>
<DocumentationFile>..\..\bin\$(Configuration)\NSubstitute\$(TargetFramework)\NSubstitute.xml</DocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>

<PropertyGroup>
<Description>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.</Description>
<Version>3.0.0</Version>
<Version>5.0.0</Version>
<Authors>Anthony Egerton;David Tchepak;Alexandr Nikitin;Oleksandr Povar</Authors>
<AssemblyName>NSubstitute</AssemblyName>
<PackageId>NSubstitute</PackageId>
Expand All @@ -14,44 +25,15 @@
<RepositoryType>git</RepositoryType>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\docs\images\nsubstitute-100x100.png" Pack="true" PackagePath="icon.png" Visible="false" />
</ItemGroup>

<PropertyGroup>
<TargetFrameworks>net6.0;netstandard2.0;net462</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Castle.Core" Version="5.1.1-*" />
</ItemGroup>

<ItemGroup Condition="'$(TargetIsNet5OrNewer)' != 'true'">
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.3.0-*" />
</ItemGroup>

<PropertyGroup>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<SignAssembly>true</SignAssembly>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
<AssemblyOriginatorKeyFile>nsubstitute.snk</AssemblyOriginatorKeyFile>
<DelaySign>false</DelaySign>
</PropertyGroup>

<PropertyGroup>
<DocumentationFile>..\..\bin\$(Configuration)\NSubstitute\$(TargetFramework)\NSubstitute.xml</DocumentationFile>
<NoWarn>$(NoWarn);1701;1702;1705;1591</NoWarn>
<PropertyGroup Condition="'$(TargetIsNet5OrNewer)' == 'true'">
<DefineConstants>$(DefineConstants);SYSTEM_DIAGNOSTICS_CODEANALYSIS_NULLABILITY</DefineConstants>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup>
<Nullable Condition="'$(TargetIsNet5OrNewer)' == 'true'">enable</Nullable>
<PropertyGroup Condition="'$(TargetIsNet5OrNewer)' != 'true'">
<!-- Nullability does not work nicely for older versions of .NET, so just disable nullability for those versions. -->
<!-- CS8632 - The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. -->
<NoWarn Condition="'$(TargetIsNet5OrNewer)' != 'true'">$(NoWarn);CS8632</NoWarn>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetIsNet5OrNewer)' == 'true'">
<DefineConstants>$(DefineConstants);SYSTEM_DIAGNOSTICS_CODEANALYSIS_NULLABILITY</DefineConstants>
<NoWarn>$(NoWarn);CS8632</NoWarn>
</PropertyGroup>

<PropertyGroup Condition="'$(CI)' == 'True'">
Expand All @@ -61,7 +43,17 @@
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All"/>
<None Include="..\..\docs\images\nsubstitute-100x100.png" Pack="true" PackagePath="icon.png" Visible="false" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Castle.Core" Version="5.1.1-*" />
</ItemGroup>

<ItemGroup Condition="'$(TargetIsNet5OrNewer)' != 'true'">
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.3.0-*" />
</ItemGroup>

</Project>
10 changes: 4 additions & 6 deletions tests/NSubstitute.Acceptance.Specs/ThrowingAsyncExceptions.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -338,5 +337,4 @@ public static TException AssertFaultedTaskException<TException>(Func<Task> act)
return actual.Exception!.InnerExceptions.First() as TException;
}
}
}
#endif
}
Loading