FirstOrDefault.cs 3.3 KB

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