Expand.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 Xunit;
  9. namespace Tests
  10. {
  11. public class Expand : AsyncEnumerableExTests
  12. {
  13. [Fact]
  14. public void Expand_Null()
  15. {
  16. Assert.Throws<ArgumentNullException>(() => AsyncEnumerableEx.Expand(default(IAsyncEnumerable<int>), x => default(IAsyncEnumerable<int>)));
  17. Assert.Throws<ArgumentNullException>(() => AsyncEnumerableEx.Expand(Return42, default(Func<int, IAsyncEnumerable<int>>)));
  18. }
  19. [Fact]
  20. public async Task Expand1Async()
  21. {
  22. var xs = new[] { 2, 3 }.ToAsyncEnumerable().Expand(x => AsyncEnumerableEx.Return(x - 1).Repeat(x - 1));
  23. var e = xs.GetAsyncEnumerator();
  24. await HasNextAsync(e, 2);
  25. await HasNextAsync(e, 3);
  26. await HasNextAsync(e, 1);
  27. await HasNextAsync(e, 2);
  28. await HasNextAsync(e, 2);
  29. await HasNextAsync(e, 1);
  30. await HasNextAsync(e, 1);
  31. await NoNextAsync(e);
  32. }
  33. [Fact]
  34. public async Task Expand2()
  35. {
  36. var ex = new Exception("Bang!");
  37. var xs = new[] { 2, 3 }.ToAsyncEnumerable().Expand(new Func<int, IAsyncEnumerable<int>>(x => { throw ex; }));
  38. var e = xs.GetAsyncEnumerator();
  39. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  40. }
  41. [Fact]
  42. public async Task Expand3Async()
  43. {
  44. var xs = new[] { 2, 3 }.ToAsyncEnumerable().Expand(x => default(IAsyncEnumerable<int>));
  45. var e = xs.GetAsyncEnumerator();
  46. await HasNextAsync(e, 2);
  47. await HasNextAsync(e, 3);
  48. await AssertThrowsAsync<NullReferenceException>(e.MoveNextAsync().AsTask());
  49. }
  50. [Fact]
  51. public async Task Expand4()
  52. {
  53. var xs = new[] { 2, 3 }.ToAsyncEnumerable().Expand(x => AsyncEnumerableEx.Return(x - 1).Repeat(x - 1));
  54. await SequenceIdentity(xs);
  55. }
  56. }
  57. }