Where.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 Where : AsyncEnumerableTests
  11. {
  12. [Fact]
  13. public void Where_Null()
  14. {
  15. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Where<int>(default, x => true));
  16. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Where<int>(default, (x, i) => true));
  17. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Where(Return42, default(Func<int, bool>)));
  18. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Where(Return42, default(Func<int, int, bool>)));
  19. }
  20. [Fact]
  21. public async Task Where1()
  22. {
  23. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  24. var ys = xs.Where(x => x % 2 == 0);
  25. var e = ys.GetAsyncEnumerator();
  26. await HasNextAsync(e, 8);
  27. await HasNextAsync(e, 4);
  28. await HasNextAsync(e, 6);
  29. await HasNextAsync(e, 2);
  30. await HasNextAsync(e, 0);
  31. await NoNextAsync(e);
  32. }
  33. [Fact]
  34. public async Task Where2()
  35. {
  36. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  37. var ys = xs.Where((x, i) => i % 2 == 0);
  38. var e = ys.GetAsyncEnumerator();
  39. await HasNextAsync(e, 8);
  40. await HasNextAsync(e, 7);
  41. await HasNextAsync(e, 6);
  42. await HasNextAsync(e, 2);
  43. await HasNextAsync(e, 0);
  44. await NoNextAsync(e);
  45. }
  46. [Fact]
  47. public async Task Where3()
  48. {
  49. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  50. var ex = new Exception("Bang");
  51. var ys = xs.Where(x => { if (x == 4) throw ex; return true; });
  52. var e = ys.GetAsyncEnumerator();
  53. await HasNextAsync(e, 8);
  54. await HasNextAsync(e, 5);
  55. await HasNextAsync(e, 7);
  56. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  57. }
  58. [Fact]
  59. public async Task Where4()
  60. {
  61. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  62. var ex = new Exception("Bang");
  63. var ys = xs.Where((x, i) => { if (i == 3) throw ex; return true; });
  64. var e = ys.GetAsyncEnumerator();
  65. await HasNextAsync(e, 8);
  66. await HasNextAsync(e, 5);
  67. await HasNextAsync(e, 7);
  68. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  69. }
  70. [Fact]
  71. public async Task Where5Async()
  72. {
  73. var ex = new Exception("Bang");
  74. var xs = Throw<int>(ex);
  75. var ys = xs.Where(x => true);
  76. var e = ys.GetAsyncEnumerator();
  77. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  78. }
  79. [Fact]
  80. public async Task Where6Async()
  81. {
  82. var ex = new Exception("Bang");
  83. var xs = Throw<int>(ex);
  84. var ys = xs.Where((x, i) => true);
  85. var e = ys.GetAsyncEnumerator();
  86. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  87. }
  88. [Fact]
  89. public async Task Where7()
  90. {
  91. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  92. var ys = xs.Where(x => x % 2 == 0).Where(x => x > 5);
  93. var e = ys.GetAsyncEnumerator();
  94. await HasNextAsync(e, 8);
  95. await HasNextAsync(e, 6);
  96. await NoNextAsync(e);
  97. }
  98. [Fact]
  99. public async Task Where8()
  100. {
  101. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  102. var ys = xs.Where(x => x % 2 == 0);
  103. await SequenceIdentity(ys);
  104. }
  105. [Fact]
  106. public async Task Where9()
  107. {
  108. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  109. var ys = xs.Where((x, i) => i % 2 == 0);
  110. await SequenceIdentity(ys);
  111. }
  112. }
  113. }