Generate.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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.Linq;
  6. using System.Threading.Tasks;
  7. using Xunit;
  8. namespace Tests
  9. {
  10. public class Generate : AsyncEnumerableExTests
  11. {
  12. [Fact]
  13. public void Generate_Null()
  14. {
  15. AssertThrows<ArgumentNullException>(() => AsyncEnumerableEx.Generate<int, int>(0, default, x => x, x => x));
  16. AssertThrows<ArgumentNullException>(() => AsyncEnumerableEx.Generate<int, int>(0, x => true, default, x => x));
  17. AssertThrows<ArgumentNullException>(() => AsyncEnumerableEx.Generate<int, int>(0, x => true, x => x, default));
  18. }
  19. [Fact]
  20. public async Task Generate1()
  21. {
  22. var xs = AsyncEnumerableEx.Generate(0, x => x < 5, x => x + 1, x => x * x);
  23. var e = xs.GetAsyncEnumerator();
  24. HasNext(e, 0);
  25. HasNext(e, 1);
  26. HasNext(e, 4);
  27. HasNext(e, 9);
  28. HasNext(e, 16);
  29. NoNext(e);
  30. await e.DisposeAsync();
  31. }
  32. [Fact]
  33. public void Generate2()
  34. {
  35. var ex = new Exception("Bang!");
  36. var xs = AsyncEnumerableEx.Generate(0, x => { throw ex; }, x => x + 1, x => x * x);
  37. var e = xs.GetAsyncEnumerator();
  38. AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), (Exception ex_) => ((AggregateException)ex_).InnerExceptions.Single() == ex);
  39. }
  40. [Fact]
  41. public void Generate3()
  42. {
  43. var ex = new Exception("Bang!");
  44. var xs = AsyncEnumerableEx.Generate(0, x => true, x => x + 1, x => { if (x == 1) throw ex; return x * x; });
  45. var e = xs.GetAsyncEnumerator();
  46. HasNext(e, 0);
  47. AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), (Exception ex_) => ((AggregateException)ex_).InnerExceptions.Single() == ex);
  48. }
  49. [Fact]
  50. public void Generate4()
  51. {
  52. var ex = new Exception("Bang!");
  53. var xs = AsyncEnumerableEx.Generate(0, x => true, x => { throw ex; }, x => x * x);
  54. var e = xs.GetAsyncEnumerator();
  55. HasNext(e, 0);
  56. AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), (Exception ex_) => ((AggregateException)ex_).InnerExceptions.Single() == ex);
  57. }
  58. [Fact]
  59. public async Task Generate5()
  60. {
  61. var xs = AsyncEnumerableEx.Generate(0, x => x < 5, x => x + 1, x => x * x);
  62. await SequenceIdentity(xs);
  63. }
  64. }
  65. }