AsyncEnumerableTests.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using FluentAssertions;
  9. using Xunit;
  10. namespace Tests
  11. {
  12. public class AsyncEnumerableTests
  13. {
  14. protected static readonly IAsyncEnumerable<int> Return42 = new[] { 42 }.ToAsyncEnumerable();
  15. protected async Task AssertThrowsAsync<TException>(Task t)
  16. where TException : Exception
  17. {
  18. await Assert.ThrowsAsync<TException>(() => t);
  19. }
  20. protected async Task AssertThrowsAsync(Task t, Exception e)
  21. {
  22. try
  23. {
  24. await t;
  25. }
  26. catch (Exception ex)
  27. {
  28. Assert.Same(e, ex);
  29. }
  30. }
  31. protected Task AssertThrowsAsync<T>(ValueTask<T> t, Exception e)
  32. {
  33. return AssertThrowsAsync(t.AsTask(), e);
  34. }
  35. protected async Task NoNextAsync<T>(IAsyncEnumerator<T> e)
  36. {
  37. Assert.False(await e.MoveNextAsync());
  38. }
  39. protected async Task HasNextAsync<T>(IAsyncEnumerator<T> e, T value)
  40. {
  41. Assert.True(await e.MoveNextAsync());
  42. Assert.Equal(value, e.Current);
  43. }
  44. protected async Task SequenceIdentity<T>(IAsyncEnumerable<T> enumerable)
  45. {
  46. var en1 = enumerable.GetAsyncEnumerator();
  47. var en2 = enumerable.GetAsyncEnumerator();
  48. Assert.Equal(en1.GetType(), en2.GetType());
  49. await en1.DisposeAsync();
  50. await en2.DisposeAsync();
  51. var res1 = await enumerable.ToList();
  52. var res2 = await enumerable.ToList();
  53. res1.ShouldAllBeEquivalentTo(res2);
  54. }
  55. protected static IAsyncEnumerable<TValue> Throw<TValue>(Exception exception)
  56. {
  57. if (exception == null)
  58. throw new ArgumentNullException(nameof(exception));
  59. #if NO_TASK_FROMEXCEPTION
  60. var tcs = new TaskCompletionSource<bool>();
  61. tcs.TrySetException(exception);
  62. var moveNextThrows = new ValueTask<bool>(tcs.Task);
  63. #else
  64. var moveNextThrows = new ValueTask<bool>(Task.FromException<bool>(exception));
  65. #endif
  66. return AsyncEnumerable.CreateEnumerable(
  67. _ => AsyncEnumerable.CreateEnumerator<TValue>(
  68. () => moveNextThrows,
  69. current: null,
  70. dispose: null)
  71. );
  72. }
  73. private void AssertThrows<E>(Action a, Func<E, bool> assert)
  74. where E : Exception
  75. {
  76. var hasFailed = false;
  77. try
  78. {
  79. a();
  80. }
  81. catch (E e)
  82. {
  83. Assert.True(assert(e));
  84. hasFailed = true;
  85. }
  86. if (!hasFailed)
  87. {
  88. Assert.True(false);
  89. }
  90. }
  91. }
  92. }