ValueTaskHelpers.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. using Xunit.Sdk;
  6. namespace System.Linq
  7. {
  8. internal static class ValueTaskHelpers
  9. {
  10. public static void Wait<T>(this ValueTask<T> task, int timeOut)
  11. {
  12. task.AsTask().Wait(timeOut);
  13. }
  14. }
  15. }
  16. namespace Xunit
  17. {
  18. internal static class AssertX
  19. {
  20. /// <summary>
  21. /// Verifies that the exact exception is thrown (and not a derived exception type).
  22. /// </summary>
  23. /// <typeparam name="T">The type of the exception expected to be thrown</typeparam>
  24. /// <param name="testCode">A delegate to the task to be tested</param>
  25. /// <returns>The exception that was thrown, when successful</returns>
  26. /// <exception cref="ThrowsException">Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown</exception>
  27. public static async Task<T> ThrowsAsync<T>(Func<ValueTask> testCode)
  28. where T : Exception
  29. {
  30. return (T)Throws(typeof(T), await RecordExceptionAsync(testCode));
  31. }
  32. /// <summary>
  33. /// Verifies that the exact exception is thrown (and not a derived exception type).
  34. /// </summary>
  35. /// <typeparam name="T">The type of the exception expected to be thrown</typeparam>
  36. /// <param name="testCode">A delegate to the task to be tested</param>
  37. /// <returns>The exception that was thrown, when successful</returns>
  38. /// <exception cref="ThrowsException">Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown</exception>
  39. public static async Task<T> ThrowsAsync<T>(Func<ValueTask<bool>> testCode)
  40. where T : Exception
  41. {
  42. return (T)Throws(typeof(T), await RecordExceptionAsync(testCode));
  43. }
  44. /// <summary>
  45. /// Records any exception which is thrown by the given task.
  46. /// </summary>
  47. /// <param name="testCode">The task which may thrown an exception.</param>
  48. /// <returns>Returns the exception that was thrown by the code; null, otherwise.</returns>
  49. private static async Task<Exception> RecordExceptionAsync(Func<ValueTask> testCode)
  50. {
  51. if (testCode == null)
  52. {
  53. throw new ArgumentNullException(nameof(testCode));
  54. }
  55. try
  56. {
  57. await testCode();
  58. return null;
  59. }
  60. catch (Exception ex)
  61. {
  62. return ex;
  63. }
  64. }
  65. /// <summary>
  66. /// Records any exception which is thrown by the given task.
  67. /// </summary>
  68. /// <param name="testCode">The task which may thrown an exception.</param>
  69. /// <returns>Returns the exception that was thrown by the code; null, otherwise.</returns>
  70. private static async Task<Exception> RecordExceptionAsync<T>(Func<ValueTask<T>> testCode)
  71. {
  72. if (testCode == null)
  73. {
  74. throw new ArgumentNullException(nameof(testCode));
  75. }
  76. try
  77. {
  78. await testCode();
  79. return null;
  80. }
  81. catch (Exception ex)
  82. {
  83. return ex;
  84. }
  85. }
  86. private static Exception Throws(Type exceptionType, Exception exception)
  87. {
  88. if (exceptionType == null)
  89. {
  90. throw new ArgumentNullException(nameof(exceptionType));
  91. }
  92. if (exception == null)
  93. throw new ThrowsException(exceptionType);
  94. if (!exceptionType.Equals(exception.GetType()))
  95. throw new ThrowsException(exceptionType, exception);
  96. return exception;
  97. }
  98. }
  99. }