Take.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 Take : AsyncEnumerableTests
  11. {
  12. [Fact]
  13. public void Take_Null()
  14. {
  15. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Take<int>(default, 5));
  16. }
  17. [Fact]
  18. public async Task Take0Async()
  19. {
  20. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  21. var ys = xs.Take(-2);
  22. var e = ys.GetAsyncEnumerator();
  23. await NoNextAsync(e);
  24. }
  25. [Fact]
  26. public async Task Take1Async()
  27. {
  28. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  29. var ys = xs.Take(2);
  30. var e = ys.GetAsyncEnumerator();
  31. await HasNextAsync(e, 1);
  32. await HasNextAsync(e, 2);
  33. await NoNextAsync(e);
  34. }
  35. [Fact]
  36. public async Task Take2Async()
  37. {
  38. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  39. var ys = xs.Take(10);
  40. var e = ys.GetAsyncEnumerator();
  41. await HasNextAsync(e, 1);
  42. await HasNextAsync(e, 2);
  43. await HasNextAsync(e, 3);
  44. await HasNextAsync(e, 4);
  45. await NoNextAsync(e);
  46. }
  47. [Fact]
  48. public async Task Take3Async()
  49. {
  50. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  51. var ys = xs.Take(0);
  52. var e = ys.GetAsyncEnumerator();
  53. await NoNextAsync(e);
  54. }
  55. [Fact]
  56. public async Task Take4Async()
  57. {
  58. var ex = new Exception("Bang");
  59. var xs = Throw<int>(ex);
  60. var ys = xs.Take(2);
  61. var e = ys.GetAsyncEnumerator();
  62. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  63. }
  64. [Fact]
  65. public async Task Take5()
  66. {
  67. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  68. var ys = xs.Take(2);
  69. await SequenceIdentity(ys);
  70. }
  71. }
  72. }