Generate.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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.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. Assert.Throws<ArgumentNullException>(() => AsyncEnumerableEx.Generate<int, int>(0, default, x => x, x => x));
  16. Assert.Throws<ArgumentNullException>(() => AsyncEnumerableEx.Generate<int, int>(0, x => true, default, x => x));
  17. Assert.Throws<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. await HasNextAsync(e, 0);
  25. await HasNextAsync(e, 1);
  26. await HasNextAsync(e, 4);
  27. await HasNextAsync(e, 9);
  28. await HasNextAsync(e, 16);
  29. await NoNextAsync(e);
  30. await e.DisposeAsync();
  31. }
  32. [Fact]
  33. public async Task Generate2Async()
  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. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  39. }
  40. [Fact]
  41. public async Task Generate3Async()
  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. await HasNextAsync(e, 0);
  47. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  48. }
  49. [Fact]
  50. public async Task Generate4Async()
  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. await HasNextAsync(e, 0);
  56. await AssertThrowsAsync(e.MoveNextAsync(), 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. }