FirstOrDefault.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 FirstOrDefault : AsyncEnumerableTests
  12. {
  13. [Fact]
  14. public async Task FirstOrDefault_Null()
  15. {
  16. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.FirstOrDefault<int>(null));
  17. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.FirstOrDefault<int>(null, x => true));
  18. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.FirstOrDefault<int>(AsyncEnumerable.Return(42), default(Func<int, bool>)));
  19. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.FirstOrDefault<int>(null, CancellationToken.None));
  20. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.FirstOrDefault<int>(null, x => true, CancellationToken.None));
  21. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.FirstOrDefault<int>(AsyncEnumerable.Return(42), default(Func<int, bool>), CancellationToken.None));
  22. }
  23. [Fact]
  24. public void FirstOrDefault1()
  25. {
  26. var res = AsyncEnumerable.Empty<int>().FirstOrDefault();
  27. Assert.Equal(0, res.Result);
  28. }
  29. [Fact]
  30. public void FirstOrDefault2()
  31. {
  32. var res = AsyncEnumerable.Empty<int>().FirstOrDefault(x => true);
  33. Assert.Equal(0, res.Result);
  34. }
  35. [Fact]
  36. public void FirstOrDefault3()
  37. {
  38. var res = AsyncEnumerable.Return(42).FirstOrDefault(x => x % 2 != 0);
  39. Assert.Equal(0, res.Result);
  40. }
  41. [Fact]
  42. public void FirstOrDefault4()
  43. {
  44. var res = AsyncEnumerable.Return(42).FirstOrDefault();
  45. Assert.Equal(42, res.Result);
  46. }
  47. [Fact]
  48. public void FirstOrDefault5()
  49. {
  50. var res = AsyncEnumerable.Return(42).FirstOrDefault(x => x % 2 == 0);
  51. Assert.Equal(42, res.Result);
  52. }
  53. [Fact]
  54. public void FirstOrDefault6()
  55. {
  56. var ex = new Exception("Bang!");
  57. var res = AsyncEnumerable.Throw<int>(ex).FirstOrDefault();
  58. AssertThrows<Exception>(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  59. }
  60. [Fact]
  61. public void FirstOrDefault7()
  62. {
  63. var ex = new Exception("Bang!");
  64. var res = AsyncEnumerable.Throw<int>(ex).FirstOrDefault(x => true);
  65. AssertThrows<Exception>(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  66. }
  67. [Fact]
  68. public void FirstOrDefault8()
  69. {
  70. var res = new[] { 42, 45, 90 }.ToAsyncEnumerable().FirstOrDefault();
  71. Assert.Equal(42, res.Result);
  72. }
  73. [Fact]
  74. public void FirstOrDefault9()
  75. {
  76. var res = new[] { 42, 45, 90 }.ToAsyncEnumerable().FirstOrDefault(x => x % 2 != 0);
  77. Assert.Equal(45, res.Result);
  78. }
  79. [Fact]
  80. public void FirstOrDefault10()
  81. {
  82. var res = new[] { 42, 45, 90 }.ToAsyncEnumerable().FirstOrDefault(x => x < 10);
  83. Assert.Equal(0, res.Result);
  84. }
  85. }
  86. }