Any.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 Any_Null()
  15. {
  16. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AnyAsync<int>(default));
  17. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AnyAsync<int>(default, x => true));
  18. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AnyAsync(Return42, default(Func<int, bool>)));
  19. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AnyAsync<int>(default, CancellationToken.None));
  20. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AnyAsync<int>(default, x => true, CancellationToken.None));
  21. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AnyAsync(Return42, default(Func<int, bool>), CancellationToken.None));
  22. }
  23. [Fact]
  24. public async Task Any1Async()
  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 Any2Async()
  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 Any3Async()
  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 Any4Async()
  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 Any5Async()
  51. {
  52. var res = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().AnyAsync();
  53. Assert.True(await res);
  54. }
  55. [Fact]
  56. public async Task Any6Async()
  57. {
  58. var res = new int[0].ToAsyncEnumerable().AnyAsync();
  59. Assert.False(await res);
  60. }
  61. }
  62. }