Where.cs 4.4 KB

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