1
0

AsyncEnumerableExTests.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 AsyncEnumerableExTests
  13. {
  14. protected const int WaitTimeoutMs = 5000;
  15. #pragma warning disable xUnit1013 // Public method should be marked as test
  16. public void AssertThrows<E>(Action a)
  17. where E : Exception
  18. {
  19. Assert.Throws<E>(a);
  20. }
  21. [Obsolete("Don't use this, use Assert.ThrowsAsync and await it", true)]
  22. public Task AssertThrows<E>(Func<Task> func)
  23. where E : Exception
  24. {
  25. return Assert.ThrowsAsync<E>(func);
  26. }
  27. public void AssertThrows<E>(Action a, Func<E, bool> assert)
  28. where E : Exception
  29. {
  30. var hasFailed = false;
  31. try
  32. {
  33. a();
  34. }
  35. catch (E e)
  36. {
  37. Assert.True(assert(e));
  38. hasFailed = true;
  39. }
  40. if (!hasFailed)
  41. {
  42. Assert.True(false);
  43. }
  44. }
  45. public void NoNext<T>(IAsyncEnumerator<T> e)
  46. {
  47. Assert.False(e.MoveNextAsync().Result);
  48. }
  49. public void HasNext<T>(IAsyncEnumerator<T> e, T value)
  50. {
  51. Assert.True(e.MoveNextAsync().Result);
  52. Assert.Equal(value, e.Current);
  53. }
  54. public async Task SequenceIdentity<T>(IAsyncEnumerable<T> enumerable)
  55. {
  56. var en1 = enumerable.GetAsyncEnumerator();
  57. var en2 = enumerable.GetAsyncEnumerator();
  58. Assert.Equal(en1.GetType(), en2.GetType());
  59. await en1.DisposeAsync();
  60. await en2.DisposeAsync();
  61. var e1t = enumerable.ToList();
  62. var e2t = enumerable.ToList();
  63. await Task.WhenAll(e1t, e2t);
  64. var e1Result = e1t.Result;
  65. var e2Result = e2t.Result;
  66. e1Result.ShouldAllBeEquivalentTo(e2Result);
  67. }
  68. #pragma warning restore xUnit1013 // Public method should be marked as test
  69. }
  70. }