Skip to content

Commit 535ae9e

Browse files
dtchepakSocolin
authored andcommitted
Simplify conditional compilation
1 parent dbd1228 commit 535ae9e

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

src/NSubstitute/Extensions/ExceptionExtensions.cs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using System;
22
using System.Linq;
33
using System.Reflection;
4-
#if !NET45
54
using System.Threading.Tasks;
6-
#endif
75
using NSubstitute.Core;
86

97
// Disable nullability for client API, so it does not affect clients.
@@ -73,15 +71,14 @@ public static ConfiguredCall ThrowsForAnyArgs<TException>(this object value)
7371
public static ConfiguredCall ThrowsForAnyArgs(this object value, Func<CallInfo, Exception> createException) =>
7472
value.ReturnsForAnyArgs(ci => throw createException(ci));
7573

76-
#if !NET45
7774
/// <summary>
7875
/// Throw an exception for this call.
7976
/// </summary>
8077
/// <param name="value"></param>
8178
/// <param name="ex">Exception to throw</param>
8279
/// <returns></returns>
8380
public static ConfiguredCall ThrowsAsync(this Task value, Exception ex) =>
84-
value.Returns(_ => Task.FromException(ex));
81+
value.Returns(_ => TaskFromException(ex));
8582

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

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

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

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

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

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

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

173170
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
174171
/// <summary>
@@ -245,8 +242,23 @@ private static object FromException(object value, Exception exception)
245242
return specificFromExceptionMethod.Invoke(null, new object[] {exception});
246243
}
247244

248-
return Task.FromException(exception);
245+
return TaskFromException(exception);
246+
}
247+
248+
private static Task TaskFromException(Exception ex) {
249+
#if NET45
250+
return new Task(() => throw ex);
251+
#endif
252+
return Task.FromException(ex);
249253
}
254+
255+
private static Task<T> TaskFromException<T>(Exception ex)
256+
{
257+
#if NET45
258+
return new Task<T>(() => throw ex);
250259
#endif
260+
return Task<T>.FromException<T>(ex);
261+
}
262+
251263
}
252264
}

0 commit comments

Comments
 (0)