Any.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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;
  7. using System.Threading.Tasks;
  8. using Xunit;
  9. namespace Tests
  10. {
  11. public class Any : AsyncEnumerableTests
  12. {
  13. [Fact]
  14. public async Task AnyAsync_Null()
  15. {
  16. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AnyAsync<int>(default).AsTask());
  17. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AnyAsync<int>(default, CancellationToken.None).AsTask());
  18. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AnyAsync<int>(default, x => true).AsTask());
  19. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AnyAsync(Return42, default(Func<int, bool>)).AsTask());
  20. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AnyAsync<int>(default, x => true, CancellationToken.None).AsTask());
  21. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AnyAsync(Return42, default, CancellationToken.None).AsTask());
  22. }
  23. [Fact]
  24. public async Task AnyAsync_Simple_True()
  25. {
  26. var res = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().AnyAsync(x => x % 2 == 0);
  27. Assert.True(await res);
  28. }
  29. [Fact]
  30. public async Task AnyAsync_Simple_False()
  31. {
  32. var res = new[] { 2, 8, 4 }.ToAsyncEnumerable().AnyAsync(x => x % 2 != 0);
  33. Assert.False(await res);
  34. }
  35. [Fact]
  36. public async Task AnyAsync_Throw_Source()
  37. {
  38. var ex = new Exception("Bang!");
  39. var res = Throw<int>(ex).AnyAsync(x => x % 2 == 0);
  40. await AssertThrowsAsync(res, ex);
  41. }
  42. [Fact]
  43. public async Task AnyAsync_Throw_Selector()
  44. {
  45. var ex = new Exception("Bang!");
  46. var res = new[] { 2, 8, 4 }.ToAsyncEnumerable().AnyAsync(new Func<int, bool>(x => { throw ex; }));
  47. await AssertThrowsAsync(res, ex);
  48. }
  49. [Fact]
  50. public async Task AnyAsync_NoSelector_NonEmpty()
  51. {
  52. var res = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().AnyAsync();
  53. Assert.True(await res);
  54. }
  55. [Fact]
  56. public async Task AnyAsync_NoSelector_Empty()
  57. {
  58. var res = new int[0].ToAsyncEnumerable().AnyAsync();
  59. Assert.False(await res);
  60. }
  61. [Fact]
  62. public async Task AnyAwaitAsync_Null()
  63. {
  64. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AnyAwaitAsync<int>(default, x => new ValueTask<bool>(true)).AsTask());
  65. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AnyAwaitAsync(Return42, default).AsTask());
  66. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AnyAwaitAsync<int>(default, x => new ValueTask<bool>(true), CancellationToken.None).AsTask());
  67. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AnyAwaitAsync(Return42, default, CancellationToken.None).AsTask());
  68. }
  69. [Fact]
  70. public async Task AnyAwaitAsync_Simple_True()
  71. {
  72. var res = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().AnyAwaitAsync(x => new ValueTask<bool>(x % 2 == 0));
  73. Assert.True(await res);
  74. }
  75. [Fact]
  76. public async Task AnyAwaitAsync_Simple_False()
  77. {
  78. var res = new[] { 2, 8, 4 }.ToAsyncEnumerable().AnyAwaitAsync(x => new ValueTask<bool>(x % 2 != 0));
  79. Assert.False(await res);
  80. }
  81. [Fact]
  82. public async Task AnyAwaitAsync_Throw_Source()
  83. {
  84. var ex = new Exception("Bang!");
  85. var res = Throw<int>(ex).AnyAwaitAsync(x => new ValueTask<bool>(x % 2 == 0));
  86. await AssertThrowsAsync(res, ex);
  87. }
  88. [Fact]
  89. public async Task AnyAwaitAsync_Throw_Selector()
  90. {
  91. var ex = new Exception("Bang!");
  92. var res = new[] { 2, 8, 4 }.ToAsyncEnumerable().AnyAwaitAsync(new Func<int, ValueTask<bool>>(x => { throw ex; }));
  93. await AssertThrowsAsync(res, ex);
  94. }
  95. #if !NO_DEEP_CANCELLATION
  96. [Fact]
  97. public async Task AnyAwaitWithCancellationAsync_Null()
  98. {
  99. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AnyAwaitWithCancellationAsync<int>(default, (x, ct) => new ValueTask<bool>(true)).AsTask());
  100. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AnyAwaitWithCancellationAsync(Return42, default).AsTask());
  101. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AnyAwaitWithCancellationAsync<int>(default, (x, ct) => new ValueTask<bool>(true), CancellationToken.None).AsTask());
  102. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AnyAwaitWithCancellationAsync(Return42, default, CancellationToken.None).AsTask());
  103. }
  104. [Fact]
  105. public async Task AnyAwaitWithCancellationAsync_Simple_True()
  106. {
  107. var res = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().AnyAwaitWithCancellationAsync((x, ct) => new ValueTask<bool>(x % 2 == 0));
  108. Assert.True(await res);
  109. }
  110. [Fact]
  111. public async Task AnyAwaitWithCancellationAsync_Simple_False()
  112. {
  113. var res = new[] { 2, 8, 4 }.ToAsyncEnumerable().AnyAwaitWithCancellationAsync((x, ct) => new ValueTask<bool>(x % 2 != 0));
  114. Assert.False(await res);
  115. }
  116. [Fact]
  117. public async Task AnyAwaitWithCancellationAsync_Throw_Source()
  118. {
  119. var ex = new Exception("Bang!");
  120. var res = Throw<int>(ex).AnyAwaitWithCancellationAsync((x, ct) => new ValueTask<bool>(x % 2 == 0));
  121. await AssertThrowsAsync(res, ex);
  122. }
  123. [Fact]
  124. public async Task AnyAwaitWithCancellationAsync_Throw_Selector()
  125. {
  126. var ex = new Exception("Bang!");
  127. var res = new[] { 2, 8, 4 }.ToAsyncEnumerable().AnyAwaitWithCancellationAsync(new Func<int, CancellationToken, ValueTask<bool>>((x, ct) => { throw ex; }));
  128. await AssertThrowsAsync(res, ex);
  129. }
  130. #endif
  131. }
  132. }