Count.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 Count : AsyncEnumerableTests
  12. {
  13. [Fact]
  14. public async Task Count_Null()
  15. {
  16. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.CountAsync<int>(default));
  17. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.CountAsync<int>(default, x => true));
  18. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.CountAsync(Return42, default(Func<int, bool>)));
  19. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.CountAsync<int>(default, CancellationToken.None));
  20. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.CountAsync<int>(default, x => true, CancellationToken.None));
  21. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.CountAsync(Return42, default(Func<int, bool>), CancellationToken.None));
  22. }
  23. [Fact]
  24. public async Task Count1()
  25. {
  26. Assert.Equal(0, await new int[0].ToAsyncEnumerable().CountAsync());
  27. Assert.Equal(3, await new[] { 1, 2, 3 }.ToAsyncEnumerable().CountAsync());
  28. }
  29. [Fact]
  30. public async Task Count2()
  31. {
  32. Assert.Equal(0, await new int[0].ToAsyncEnumerable().CountAsync(x => x < 3));
  33. Assert.Equal(2, await new[] { 1, 2, 3 }.ToAsyncEnumerable().CountAsync(x => x < 3));
  34. }
  35. [Fact]
  36. public async Task Count3Async()
  37. {
  38. var ex = new Exception("Bang!");
  39. var ys = new[] { 1, 2, 3 }.ToAsyncEnumerable().CountAsync(new Func<int, bool>(x => { throw ex; }));
  40. await AssertThrowsAsync(ys, ex);
  41. }
  42. [Fact]
  43. public async Task Count4Async()
  44. {
  45. var ex = new Exception("Bang!");
  46. await AssertThrowsAsync(Throw<int>(ex).CountAsync(), ex);
  47. }
  48. [Fact]
  49. public async Task Count5Async()
  50. {
  51. var ex = new Exception("Bang!");
  52. await AssertThrowsAsync(Throw<int>(ex).CountAsync(x => x < 3), ex);
  53. }
  54. }
  55. }