Single.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 Single : AsyncEnumerableTests
  13. {
  14. [Fact]
  15. public async Task Single_Null()
  16. {
  17. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.Single<int>(default));
  18. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.Single<int>(default, x => true));
  19. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.Single<int>(Return42, default(Func<int, bool>)));
  20. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.Single<int>(default, CancellationToken.None));
  21. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.Single<int>(default, x => true, CancellationToken.None));
  22. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.Single<int>(Return42, default(Func<int, bool>), CancellationToken.None));
  23. }
  24. [Fact]
  25. public void Single1()
  26. {
  27. var res = AsyncEnumerable.Empty<int>().Single();
  28. AssertThrows<Exception>(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException);
  29. }
  30. [Fact]
  31. public void Single2()
  32. {
  33. var res = AsyncEnumerable.Empty<int>().Single(x => true);
  34. AssertThrows<Exception>(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException);
  35. }
  36. [Fact]
  37. public void Single3()
  38. {
  39. var res = Return42.Single(x => x % 2 != 0);
  40. AssertThrows<Exception>(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException);
  41. }
  42. [Fact]
  43. public void Single4()
  44. {
  45. var res = Return42.Single();
  46. Assert.Equal(42, res.Result);
  47. }
  48. [Fact]
  49. public void Single5()
  50. {
  51. var res = Return42.Single(x => x % 2 == 0);
  52. Assert.Equal(42, res.Result);
  53. }
  54. [Fact]
  55. public void Single6()
  56. {
  57. var ex = new Exception("Bang!");
  58. var res = Throw<int>(ex).Single();
  59. AssertThrows<Exception>(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  60. }
  61. [Fact]
  62. public void Single7()
  63. {
  64. var ex = new Exception("Bang!");
  65. var res = Throw<int>(ex).Single(x => true);
  66. AssertThrows<Exception>(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  67. }
  68. [Fact]
  69. public void Single8()
  70. {
  71. var res = new[] { 42, 45, 90 }.ToAsyncEnumerable().Single();
  72. AssertThrows<Exception>(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException);
  73. }
  74. [Fact]
  75. public void Single9()
  76. {
  77. var res = new[] { 42, 45, 90 }.ToAsyncEnumerable().Single(x => x % 2 != 0);
  78. Assert.Equal(45, res.Result);
  79. }
  80. [Fact]
  81. public void Single10()
  82. {
  83. var res = new[] { 42, 23, 45, 90 }.ToAsyncEnumerable().Single(x => x % 2 != 0);
  84. AssertThrows<Exception>(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException);
  85. }
  86. [Fact]
  87. public void Single11()
  88. {
  89. var res = new int[0].ToAsyncEnumerable().Single();
  90. AssertThrows<Exception>(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException);
  91. }
  92. }
  93. }