First.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 First : AsyncEnumerableTests
  12. {
  13. [Fact]
  14. public async Task First_Null()
  15. {
  16. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.FirstAsync<int>(default));
  17. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.FirstAsync<int>(default, x => true));
  18. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.FirstAsync(Return42, default(Func<int, bool>)));
  19. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.FirstAsync<int>(default, CancellationToken.None));
  20. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.FirstAsync<int>(default, x => true, CancellationToken.None));
  21. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.FirstAsync(Return42, default(Func<int, bool>), CancellationToken.None));
  22. }
  23. [Fact]
  24. public async Task First1Async()
  25. {
  26. var res = AsyncEnumerable.Empty<int>().FirstAsync();
  27. await AssertThrowsAsync<InvalidOperationException>(res);
  28. }
  29. [Fact]
  30. public async Task First2Async()
  31. {
  32. var res = AsyncEnumerable.Empty<int>().FirstAsync(x => true);
  33. await AssertThrowsAsync<InvalidOperationException>(res);
  34. }
  35. [Fact]
  36. public async Task First3Async()
  37. {
  38. var res = Return42.FirstAsync(x => x % 2 != 0);
  39. await AssertThrowsAsync<InvalidOperationException>(res);
  40. }
  41. [Fact]
  42. public async Task First4Async()
  43. {
  44. var res = Return42.FirstAsync();
  45. Assert.Equal(42, await res);
  46. }
  47. [Fact]
  48. public async Task First5Async()
  49. {
  50. var res = Return42.FirstAsync(x => x % 2 == 0);
  51. Assert.Equal(42, await res);
  52. }
  53. [Fact]
  54. public async Task First6Async()
  55. {
  56. var ex = new Exception("Bang!");
  57. var res = Throw<int>(ex).FirstAsync();
  58. await AssertThrowsAsync(res, ex);
  59. }
  60. [Fact]
  61. public async Task First7Async()
  62. {
  63. var ex = new Exception("Bang!");
  64. var res = Throw<int>(ex).FirstAsync(x => true);
  65. await AssertThrowsAsync(res, ex);
  66. }
  67. [Fact]
  68. public async Task First8Async()
  69. {
  70. var res = new[] { 42, 45, 90 }.ToAsyncEnumerable().FirstAsync();
  71. Assert.Equal(42, await res);
  72. }
  73. [Fact]
  74. public async Task First9Async()
  75. {
  76. var res = new[] { 42, 45, 90 }.ToAsyncEnumerable().FirstAsync(x => x % 2 != 0);
  77. Assert.Equal(45, await res);
  78. }
  79. }
  80. }