Take.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 Xunit;
  7. namespace Tests
  8. {
  9. public class Take : AsyncEnumerableTests
  10. {
  11. [Fact]
  12. public void Take_Null()
  13. {
  14. AssertThrows<ArgumentNullException>(() => AsyncEnumerable.Take<int>(null, 5));
  15. }
  16. [Fact]
  17. public void Take0()
  18. {
  19. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  20. var ys = xs.Take(-2);
  21. var e = ys.GetAsyncEnumerator();
  22. NoNext(e);
  23. }
  24. [Fact]
  25. public void Take1()
  26. {
  27. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  28. var ys = xs.Take(2);
  29. var e = ys.GetAsyncEnumerator();
  30. HasNext(e, 1);
  31. HasNext(e, 2);
  32. NoNext(e);
  33. }
  34. [Fact]
  35. public void Take2()
  36. {
  37. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  38. var ys = xs.Take(10);
  39. var e = ys.GetAsyncEnumerator();
  40. HasNext(e, 1);
  41. HasNext(e, 2);
  42. HasNext(e, 3);
  43. HasNext(e, 4);
  44. NoNext(e);
  45. }
  46. [Fact]
  47. public void Take3()
  48. {
  49. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  50. var ys = xs.Take(0);
  51. var e = ys.GetAsyncEnumerator();
  52. NoNext(e);
  53. }
  54. [Fact]
  55. public void Take4()
  56. {
  57. var ex = new Exception("Bang");
  58. var xs = AsyncEnumerable.Throw<int>(ex);
  59. var ys = xs.Take(2);
  60. var e = ys.GetAsyncEnumerator();
  61. AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), (Exception ex_) => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  62. }
  63. [Fact]
  64. public async Task Take5()
  65. {
  66. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  67. var ys = xs.Take(2);
  68. await SequenceIdentity(ys);
  69. }
  70. }
  71. }