|
1 | 1 | using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Reflection; |
| 4 | +using System.Threading.Tasks; |
2 | 5 | using NSubstitute.Core; |
3 | 6 |
|
4 | 7 | // Disable nullability for client API, so it does not affect clients. |
@@ -67,5 +70,197 @@ public static ConfiguredCall ThrowsForAnyArgs<TException>(this object value) |
67 | 70 | /// <returns></returns> |
68 | 71 | public static ConfiguredCall ThrowsForAnyArgs(this object value, Func<CallInfo, Exception> createException) => |
69 | 72 | value.ReturnsForAnyArgs(ci => throw createException(ci)); |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Throw an exception for this call. |
| 76 | + /// </summary> |
| 77 | + /// <param name="value"></param> |
| 78 | + /// <param name="ex">Exception to throw</param> |
| 79 | + /// <returns></returns> |
| 80 | + public static ConfiguredCall ThrowsAsync(this Task value, Exception ex) => |
| 81 | + value.Returns(_ => TaskFromException(ex)); |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Throw an exception for this call. |
| 85 | + /// </summary> |
| 86 | + /// <param name="value"></param> |
| 87 | + /// <param name="ex">Exception to throw</param> |
| 88 | + /// <returns></returns> |
| 89 | + public static ConfiguredCall ThrowsAsync<T>(this Task<T> value, Exception ex) => |
| 90 | + value.Returns(_ => TaskFromException<T>(ex)); |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Throw an exception of the given type for this call. |
| 94 | + /// </summary> |
| 95 | + /// <typeparam name="TException">Type of exception to throw</typeparam> |
| 96 | + /// <param name="value"></param> |
| 97 | + /// <returns></returns> |
| 98 | + public static ConfiguredCall ThrowsAsync<TException>(this Task value) |
| 99 | + where TException : notnull, Exception, new() |
| 100 | + { |
| 101 | + return value.Returns(_ => FromException(value, new TException())); |
| 102 | + } |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// Throw an exception for this call, as generated by the specified function. |
| 106 | + /// </summary> |
| 107 | + /// <param name="value"></param> |
| 108 | + /// <param name="createException">Func creating exception object</param> |
| 109 | + /// <returns></returns> |
| 110 | + public static ConfiguredCall ThrowsAsync(this Task value, Func<CallInfo, Exception> createException) => |
| 111 | + value.Returns(ci => TaskFromException(createException(ci))); |
| 112 | + |
| 113 | + /// <summary> |
| 114 | + /// Throw an exception for this call, as generated by the specified function. |
| 115 | + /// </summary> |
| 116 | + /// <param name="value"></param> |
| 117 | + /// <param name="createException">Func creating exception object</param> |
| 118 | + /// <returns></returns> |
| 119 | + public static ConfiguredCall ThrowsAsync<T>(this Task<T> value, Func<CallInfo, Exception> createException) => |
| 120 | + value.Returns(ci => TaskFromException<T>(createException(ci))); |
| 121 | + |
| 122 | + /// <summary> |
| 123 | + /// Throw an exception for this call made with any arguments. |
| 124 | + /// </summary> |
| 125 | + /// <param name="value"></param> |
| 126 | + /// <param name="ex">Exception to throw</param> |
| 127 | + /// <returns></returns> |
| 128 | + public static ConfiguredCall ThrowsAsyncForAnyArgs(this Task value, Exception ex) => |
| 129 | + value.ReturnsForAnyArgs(_ => TaskFromException(ex)); |
| 130 | + |
| 131 | + /// <summary> |
| 132 | + /// Throw an exception for this call made with any arguments. |
| 133 | + /// </summary> |
| 134 | + /// <param name="value"></param> |
| 135 | + /// <param name="ex">Exception to throw</param> |
| 136 | + /// <returns></returns> |
| 137 | + public static ConfiguredCall ThrowsAsyncForAnyArgs<T>(this Task<T> value, Exception ex) => |
| 138 | + value.ReturnsForAnyArgs(_ => TaskFromException<T>(ex)); |
| 139 | + |
| 140 | + /// <summary> |
| 141 | + /// Throws an exception of the given type for this call made with any arguments. |
| 142 | + /// </summary> |
| 143 | + /// <typeparam name="TException">Type of exception to throw</typeparam> |
| 144 | + /// <param name="value"></param> |
| 145 | + /// <returns></returns> |
| 146 | + public static ConfiguredCall ThrowsAsyncForAnyArgs<TException>(this Task value) |
| 147 | + where TException : notnull, Exception, new() |
| 148 | + { |
| 149 | + return value.ReturnsForAnyArgs(_ => FromException(value, new TException())); |
| 150 | + } |
| 151 | + |
| 152 | + /// <summary> |
| 153 | + /// Throws an exception for this call made with any arguments, as generated by the specified function. |
| 154 | + /// </summary> |
| 155 | + /// <param name="value"></param> |
| 156 | + /// <param name="createException">Func creating exception object</param> |
| 157 | + /// <returns></returns> |
| 158 | + public static ConfiguredCall ThrowsAsyncForAnyArgs(this Task value, Func<CallInfo, Exception> createException) => |
| 159 | + value.ReturnsForAnyArgs(ci => TaskFromException(createException(ci))); |
| 160 | + |
| 161 | + /// <summary> |
| 162 | + /// Throws an exception for this call made with any arguments, as generated by the specified function. |
| 163 | + /// </summary> |
| 164 | + /// <param name="value"></param> |
| 165 | + /// <param name="createException">Func creating exception object</param> |
| 166 | + /// <returns></returns> |
| 167 | + public static ConfiguredCall ThrowsAsyncForAnyArgs<T>(this Task<T> value, Func<CallInfo, Exception> createException) => |
| 168 | + value.ReturnsForAnyArgs(ci => TaskFromException<T>(createException(ci))); |
| 169 | + |
| 170 | +#if NET5_0_OR_GREATER |
| 171 | + /// <summary> |
| 172 | + /// Throw an exception for this call. |
| 173 | + /// </summary> |
| 174 | + /// <param name="value"></param> |
| 175 | + /// <param name="ex">Exception to throw</param> |
| 176 | + /// <returns></returns> |
| 177 | + public static ConfiguredCall ThrowsAsync<T>(this ValueTask<T> value, Exception ex) => |
| 178 | + value.Returns(_ => ValueTask.FromException<T>(ex)); |
| 179 | + |
| 180 | + /// <summary> |
| 181 | + /// Throw an exception of the given type for this call. |
| 182 | + /// </summary> |
| 183 | + /// <typeparam name="TResult">Type of exception to throw</typeparam> |
| 184 | + /// <typeparam name="TException">Type of exception to throw</typeparam> |
| 185 | + /// <param name="value"></param> |
| 186 | + /// <returns></returns> |
| 187 | + public static ConfiguredCall ThrowsAsync<TResult, TException>(this ValueTask<TResult> value) |
| 188 | + where TException : notnull, Exception, new() |
| 189 | + { |
| 190 | + return value.Returns(_ => ValueTask.FromException<TResult>(new TException())); |
| 191 | + } |
| 192 | + |
| 193 | + /// <summary> |
| 194 | + /// Throw an exception for this call, as generated by the specified function. |
| 195 | + /// </summary> |
| 196 | + /// <param name="value"></param> |
| 197 | + /// <param name="createException">Func creating exception object</param> |
| 198 | + /// <returns></returns> |
| 199 | + public static ConfiguredCall ThrowsAsync<T>(this ValueTask<T> value, Func<CallInfo, Exception> createException) => |
| 200 | + value.Returns(ci => ValueTask.FromException<T>(createException(ci))); |
| 201 | + |
| 202 | + /// <summary> |
| 203 | + /// Throws an exception of the given type for this call made with any arguments. |
| 204 | + /// </summary> |
| 205 | + /// <typeparam name="T"></typeparam> |
| 206 | + /// <typeparam name="TException">Type of exception to throw</typeparam> |
| 207 | + /// <param name="value"></param> |
| 208 | + /// <returns></returns> |
| 209 | + public static ConfiguredCall ThrowsAsyncForAnyArgs<T, TException>(this ValueTask<T> value) |
| 210 | + where TException : notnull, Exception, new() |
| 211 | + { |
| 212 | + return value.ReturnsForAnyArgs(_ => ValueTask.FromException<T>(new TException())); |
| 213 | + } |
| 214 | + |
| 215 | + /// <summary> |
| 216 | + /// Throw an exception for this call made with any arguments. |
| 217 | + /// </summary> |
| 218 | + /// <param name="value"></param> |
| 219 | + /// <param name="ex">Exception to throw</param> |
| 220 | + /// <returns></returns> |
| 221 | + public static ConfiguredCall ThrowsAsyncForAnyArgs<T>(this ValueTask<T> value, Exception ex) => |
| 222 | + value.ReturnsForAnyArgs(_ => ValueTask.FromException<T>(ex)); |
| 223 | + |
| 224 | + /// <summary> |
| 225 | + /// Throws an exception for this call made with any arguments, as generated by the specified function. |
| 226 | + /// </summary> |
| 227 | + /// <param name="value"></param> |
| 228 | + /// <param name="createException">Func creating exception object</param> |
| 229 | + /// <returns></returns> |
| 230 | + public static ConfiguredCall ThrowsAsyncForAnyArgs<T>(this ValueTask<T> value, Func<CallInfo, Exception> createException) => |
| 231 | + value.ReturnsForAnyArgs(ci => ValueTask.FromException<T>(createException(ci))); |
| 232 | +#endif |
| 233 | + |
| 234 | + private static object FromException(object value, Exception exception) |
| 235 | + { |
| 236 | + // Handle Task<T> |
| 237 | + var valueType = value.GetType(); |
| 238 | + if (valueType.IsConstructedGenericType) |
| 239 | + { |
| 240 | + var fromExceptionMethodInfo = typeof(Task).GetMethods(BindingFlags.Static | BindingFlags.Public).Single(m => m.Name == "FromException" && m.ContainsGenericParameters); |
| 241 | + var specificFromExceptionMethod = fromExceptionMethodInfo.MakeGenericMethod(valueType.GenericTypeArguments); |
| 242 | + return specificFromExceptionMethod.Invoke(null, new object[] {exception}); |
| 243 | + } |
| 244 | + |
| 245 | + return TaskFromException(exception); |
| 246 | + } |
| 247 | + |
| 248 | + private static Task TaskFromException(Exception ex) { |
| 249 | +#if NET45 |
| 250 | + return new Task(() => throw ex); |
| 251 | +#else |
| 252 | + return Task.FromException(ex); |
| 253 | +#endif |
| 254 | + } |
| 255 | + |
| 256 | + private static Task<T> TaskFromException<T>(Exception ex) |
| 257 | + { |
| 258 | +#if NET45 |
| 259 | + return new Task<T>(() => throw ex); |
| 260 | +#else |
| 261 | + return Task<T>.FromException<T>(ex); |
| 262 | +#endif |
| 263 | + } |
| 264 | + |
70 | 265 | } |
71 | 266 | } |
0 commit comments