First.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using Xunit;
  10. namespace Tests
  11. {
  12. public class First : AsyncEnumerableTests
  13. {
  14. [Fact]
  15. public async Task First_Null()
  16. {
  17. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.First<int>(default(IAsyncEnumerable<int>)));
  18. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.First<int>(default(IAsyncEnumerable<int>), x => true));
  19. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.First<int>(Return42, default(Func<int, bool>)));
  20. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.First<int>(default(IAsyncEnumerable<int>), CancellationToken.None));
  21. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.First<int>(default(IAsyncEnumerable<int>), x => true, CancellationToken.None));
  22. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.First<int>(Return42, default(Func<int, bool>), CancellationToken.None));
  23. }
  24. [Fact]
  25. public void First1()
  26. {
  27. var res = AsyncEnumerable.Empty<int>().First();
  28. AssertThrows<Exception>(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException);
  29. }
  30. [Fact]
  31. public void First2()
  32. {
  33. var res = AsyncEnumerable.Empty<int>().First(x => true);
  34. AssertThrows<Exception>(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException);
  35. }
  36. [Fact]
  37. public void First3()
  38. {
  39. var res = Return42.First(x => x % 2 != 0);
  40. AssertThrows<Exception>(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException);
  41. }
  42. [Fact]
  43. public void First4()
  44. {
  45. var res = Return42.First();
  46. Assert.Equal(42, res.Result);
  47. }
  48. [Fact]
  49. public void First5()
  50. {
  51. var res = Return42.First(x => x % 2 == 0);
  52. Assert.Equal(42, res.Result);
  53. }
  54. [Fact]
  55. public void First6()
  56. {
  57. var ex = new Exception("Bang!");
  58. var res = AsyncEnumerable.Throw<int>(ex).First();
  59. AssertThrows<Exception>(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  60. }
  61. [Fact]
  62. public void First7()
  63. {
  64. var ex = new Exception("Bang!");
  65. var res = AsyncEnumerable.Throw<int>(ex).First(x => true);
  66. AssertThrows<Exception>(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  67. }
  68. [Fact]
  69. public void First8()
  70. {
  71. var res = new[] { 42, 45, 90 }.ToAsyncEnumerable().First();
  72. Assert.Equal(42, res.Result);
  73. }
  74. [Fact]
  75. public void First9()
  76. {
  77. var res = new[] { 42, 45, 90 }.ToAsyncEnumerable().First(x => x % 2 != 0);
  78. Assert.Equal(45, res.Result);
  79. }
  80. }
  81. }