AsyncEnumerableExTests.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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 AsyncEnumerableExTests
  13. {
  14. protected static readonly IAsyncEnumerable<int> Return42 = AsyncEnumerableEx.Return(42);
  15. protected static IAsyncEnumerable<T> Throw<T>(Exception exception) => AsyncEnumerableEx.Throw<T>(exception);
  16. protected async Task AssertThrowsAsync<TException>(Task t)
  17. where TException : Exception
  18. {
  19. await Assert.ThrowsAsync<TException>(() => t);
  20. }
  21. protected async Task AssertThrowsAsync(Task t, Exception e)
  22. {
  23. try
  24. {
  25. await t;
  26. }
  27. catch (Exception ex)
  28. {
  29. Assert.Same(e, ex);
  30. }
  31. }
  32. protected Task AssertThrowsAsync<T>(ValueTask<T> t, Exception e)
  33. {
  34. return AssertThrowsAsync(t.AsTask(), e);
  35. }
  36. protected async Task NoNextAsync<T>(IAsyncEnumerator<T> e)
  37. {
  38. Assert.False(await e.MoveNextAsync());
  39. }
  40. protected async Task HasNextAsync<T>(IAsyncEnumerator<T> e, T value)
  41. {
  42. Assert.True(await e.MoveNextAsync());
  43. Assert.Equal(value, e.Current);
  44. }
  45. protected async Task SequenceIdentity<T>(IAsyncEnumerable<T> enumerable)
  46. {
  47. var en1 = enumerable.GetAsyncEnumerator();
  48. var en2 = enumerable.GetAsyncEnumerator();
  49. Assert.Equal(en1.GetType(), en2.GetType());
  50. await en1.DisposeAsync();
  51. await en2.DisposeAsync();
  52. var res1 = await enumerable.ToListAsync();
  53. var res2 = await enumerable.ToListAsync();
  54. res1.Should().BeEquivalentTo(res2);
  55. }
  56. }
  57. }