All.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 All_Null()
  15. {
  16. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AllAsync<int>(default, x => true));
  17. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AllAsync(Return42, default(Func<int, bool>)));
  18. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AllAsync<int>(default, x => true, CancellationToken.None));
  19. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AllAsync(Return42, default(Func<int, bool>), CancellationToken.None));
  20. }
  21. [Fact]
  22. public async Task All1Async()
  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 All2Async()
  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 All3Async()
  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 All4Async()
  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. }
  48. }