1
0

All.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 All : AsyncEnumerableTests
  12. {
  13. [Fact]
  14. public async Task AllAsync_Null()
  15. {
  16. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AllAsync<int>(default, x => true).AsTask());
  17. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AllAsync(Return42, default).AsTask());
  18. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AllAsync<int>(default, x => true, CancellationToken.None).AsTask());
  19. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AllAsync(Return42, default, CancellationToken.None).AsTask());
  20. }
  21. [Fact]
  22. public async Task AllAsync_Simple_False()
  23. {
  24. var res = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().AllAsync(x => x % 2 == 0);
  25. Assert.False(await res);
  26. }
  27. [Fact]
  28. public async Task AllAsync_Simple_True()
  29. {
  30. var res = new[] { 2, 8, 4 }.ToAsyncEnumerable().AllAsync(x => x % 2 == 0);
  31. Assert.True(await res);
  32. }
  33. [Fact]
  34. public async Task AllAsync_Throw_Source()
  35. {
  36. var ex = new Exception("Bang!");
  37. var res = Throw<int>(ex).AllAsync(x => x % 2 == 0);
  38. await AssertThrowsAsync(res, ex);
  39. }
  40. [Fact]
  41. public async Task AllAsync_Throw_Selector()
  42. {
  43. var ex = new Exception("Bang!");
  44. var res = new[] { 2, 8, 4 }.ToAsyncEnumerable().AllAsync(new Func<int, bool>(x => { throw ex; }));
  45. await AssertThrowsAsync(res, ex);
  46. }
  47. [Fact]
  48. public async Task AllAwaitAsync_Null()
  49. {
  50. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AllAwaitAsync<int>(default, x => new ValueTask<bool>(true)).AsTask());
  51. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AllAwaitAsync(Return42, default).AsTask());
  52. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AllAwaitAsync<int>(default, x => new ValueTask<bool>(true), CancellationToken.None).AsTask());
  53. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AllAwaitAsync(Return42, default, CancellationToken.None).AsTask());
  54. }
  55. [Fact]
  56. public async Task AllAwaitAsync_Simple_False()
  57. {
  58. var res = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().AllAwaitAsync(x => new ValueTask<bool>(x % 2 == 0));
  59. Assert.False(await res);
  60. }
  61. [Fact]
  62. public async Task AllAwaitAsync_Simple_True()
  63. {
  64. var res = new[] { 2, 8, 4 }.ToAsyncEnumerable().AllAwaitAsync(x => new ValueTask<bool>(x % 2 == 0));
  65. Assert.True(await res);
  66. }
  67. [Fact]
  68. public async Task AllAwaitAsync_Throw_Source()
  69. {
  70. var ex = new Exception("Bang!");
  71. var res = Throw<int>(ex).AllAwaitAsync(x => new ValueTask<bool>(x % 2 == 0));
  72. await AssertThrowsAsync(res, ex);
  73. }
  74. [Fact]
  75. public async Task AllAwaitAsync_Throw_Selector()
  76. {
  77. var ex = new Exception("Bang!");
  78. var res = new[] { 2, 8, 4 }.ToAsyncEnumerable().AllAwaitAsync(new Func<int, ValueTask<bool>>(x => { throw ex; }));
  79. await AssertThrowsAsync(res, ex);
  80. }
  81. #if !NO_DEEP_CANCELLATION
  82. [Fact]
  83. public async Task AllAwaitWithCancellationAsync_Null()
  84. {
  85. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AllAwaitWithCancellationAsync<int>(default, (x, ct) => new ValueTask<bool>(true)).AsTask());
  86. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AllAwaitWithCancellationAsync(Return42, default).AsTask());
  87. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AllAwaitWithCancellationAsync<int>(default, (x, ct) => new ValueTask<bool>(true), CancellationToken.None).AsTask());
  88. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AllAwaitWithCancellationAsync(Return42, default, CancellationToken.None).AsTask());
  89. }
  90. [Fact]
  91. public async Task AllAwaitWithCancellationAsync_Simple_False()
  92. {
  93. var res = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().AllAwaitWithCancellationAsync((x, ct) => new ValueTask<bool>(x % 2 == 0));
  94. Assert.False(await res);
  95. }
  96. [Fact]
  97. public async Task AllAwaitWithCancellationAsync_Simple_True()
  98. {
  99. var res = new[] { 2, 8, 4 }.ToAsyncEnumerable().AllAwaitWithCancellationAsync((x, ct) => new ValueTask<bool>(x % 2 == 0));
  100. Assert.True(await res);
  101. }
  102. [Fact]
  103. public async Task AllAwaitWithCancellationAsync_Throw_Source()
  104. {
  105. var ex = new Exception("Bang!");
  106. var res = Throw<int>(ex).AllAwaitWithCancellationAsync((x, ct) => new ValueTask<bool>(x % 2 == 0));
  107. await AssertThrowsAsync(res, ex);
  108. }
  109. [Fact]
  110. public async Task AllAwaitWithCancellationAsync_Throw_Selector()
  111. {
  112. var ex = new Exception("Bang!");
  113. var res = new[] { 2, 8, 4 }.ToAsyncEnumerable().AllAwaitWithCancellationAsync(new Func<int, CancellationToken, ValueTask<bool>>((x, ct) => { throw ex; }));
  114. await AssertThrowsAsync(res, ex);
  115. }
  116. #endif
  117. }
  118. }