Take.cs 2.1 KB

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