Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwilson committed Aug 27, 2022
1 parent 58013a9 commit 1cacd92
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions ExceptionAsserts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,13 @@ static Exception ThrowsAny(Type exceptionType, Exception exception)
/// <param name="testCode">A delegate to the code to be tested</param>
/// <returns>The exception that was thrown, when successful</returns>
/// <exception cref="ThrowsException">Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown</exception>
#if XUNIT_NULLABLE
public static T Throws<T>(string? paramName, Action testCode)
#else
public static T Throws<T>(string paramName, Action testCode)
#endif
where T : ArgumentException
{
GuardArgumentNotNull(nameof(paramName), paramName);

var ex = Throws<T>(testCode);
Equal(paramName, ex.ParamName);
return ex;
Expand All @@ -275,14 +277,12 @@ public static T Throws<T>(string paramName, Action testCode)
/// <returns>The exception that was thrown, when successful</returns>
/// <exception cref="ThrowsException">Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown</exception>
#if XUNIT_NULLABLE
public static T Throws<T>(string paramName, Func<object?> testCode)
public static T Throws<T>(string? paramName, Func<object?> testCode)
#else
public static T Throws<T>(string paramName, Func<object> testCode)
#endif
where T : ArgumentException
{
GuardArgumentNotNull(nameof(paramName), paramName);

var ex = Throws<T>(testCode);
Equal(paramName, ex.ParamName);
return ex;
Expand All @@ -291,13 +291,21 @@ public static T Throws<T>(string paramName, Func<object> testCode)
/// <summary/>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("You must call Assert.ThrowsAsync<T> (and await the result) when testing async code.", true)]
#if XUNIT_NULLABLE
public static T Throws<T>(string? paramName, Func<Task> testCode) where T : ArgumentException { throw new NotImplementedException(); }
#else
public static T Throws<T>(string paramName, Func<Task> testCode) where T : ArgumentException { throw new NotImplementedException(); }
#endif

#if XUNIT_VALUETASK
/// <summary/>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("You must call Assert.ThrowsAsync<T> (and await the result) when testing async code.", true)]
#if XUNIT_NULLABLE
public static T Throws<T>(string? paramName, Func<ValueTask> testCode) where T : ArgumentException { throw new NotImplementedException(); }
#else
public static T Throws<T>(string paramName, Func<ValueTask> testCode) where T : ArgumentException { throw new NotImplementedException(); }
#endif
#endif

/// <summary>
Expand All @@ -308,7 +316,11 @@ public static T Throws<T>(string paramName, Func<object> testCode)
/// <param name="testCode">A delegate to the task to be tested</param>
/// <returns>The exception that was thrown, when successful</returns>
/// <exception cref="ThrowsException">Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown</exception>
#if XUNIT_NULLABLE
public static async Task<T> ThrowsAsync<T>(string? paramName, Func<Task> testCode)
#else
public static async Task<T> ThrowsAsync<T>(string paramName, Func<Task> testCode)
#endif
where T : ArgumentException
{
var ex = await ThrowsAsync<T>(testCode);
Expand All @@ -325,7 +337,11 @@ public static async Task<T> ThrowsAsync<T>(string paramName, Func<Task> testCode
/// <param name="testCode">A delegate to the task to be tested</param>
/// <returns>The exception that was thrown, when successful</returns>
/// <exception cref="ThrowsException">Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown</exception>
#if XUNIT_NULLABLE
public static async ValueTask<T> ThrowsAsync<T>(string? paramName, Func<ValueTask> testCode)
#else
public static async ValueTask<T> ThrowsAsync<T>(string paramName, Func<ValueTask> testCode)
#endif
where T : ArgumentException
{
var ex = await ThrowsAsync<T>(testCode);
Expand Down

0 comments on commit 1cacd92

Please sign in to comment.