using System; using System.Threading.Tasks; using Xunit.Sdk; namespace System.Linq { internal static class ValueTaskHelpers { public static void Wait(this ValueTask task, int timeOut) { task.AsTask().Wait(timeOut); } } } namespace Xunit { internal static class AssertX { /// /// Verifies that the exact exception is thrown (and not a derived exception type). /// /// The type of the exception expected to be thrown /// A delegate to the task to be tested /// The exception that was thrown, when successful /// Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown public static async Task ThrowsAsync(Func testCode) where T : Exception { return (T)Throws(typeof(T), await RecordExceptionAsync(testCode)); } /// /// Verifies that the exact exception is thrown (and not a derived exception type). /// /// The type of the exception expected to be thrown /// A delegate to the task to be tested /// The exception that was thrown, when successful /// Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown public static async Task ThrowsAsync(Func> testCode) where T : Exception { return (T)Throws(typeof(T), await RecordExceptionAsync(testCode)); } /// /// Records any exception which is thrown by the given task. /// /// The task which may thrown an exception. /// Returns the exception that was thrown by the code; null, otherwise. private static async Task RecordExceptionAsync(Func testCode) { if (testCode == null) { throw new ArgumentNullException(nameof(testCode)); } try { await testCode(); return null; } catch (Exception ex) { return ex; } } /// /// Records any exception which is thrown by the given task. /// /// The task which may thrown an exception. /// Returns the exception that was thrown by the code; null, otherwise. private static async Task RecordExceptionAsync(Func> testCode) { if (testCode == null) { throw new ArgumentNullException(nameof(testCode)); } try { await testCode(); return null; } catch (Exception ex) { return ex; } } private static Exception Throws(Type exceptionType, Exception exception) { if (exceptionType == null) { throw new ArgumentNullException(nameof(exceptionType)); } if (exception == null) throw ThrowsException.ForNoException(exceptionType); if (!exceptionType.Equals(exception.GetType())) throw ThrowsException.ForIncorrectExceptionType(exceptionType, exception); return exception; } } }