Single.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 Single : AsyncEnumerableTests
  12. {
  13. [Fact]
  14. public async Task Single_Null()
  15. {
  16. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.SingleAsync<int>(default));
  17. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.SingleAsync<int>(default, x => true));
  18. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.SingleAsync(Return42, default(Func<int, bool>)));
  19. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.SingleAsync<int>(default, CancellationToken.None));
  20. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.SingleAsync<int>(default, x => true, CancellationToken.None));
  21. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.SingleAsync(Return42, default(Func<int, bool>), CancellationToken.None));
  22. }
  23. [Fact]
  24. public async Task Single1Async()
  25. {
  26. var res = AsyncEnumerable.Empty<int>().SingleAsync();
  27. await AssertThrowsAsync<InvalidOperationException>(res);
  28. }
  29. [Fact]
  30. public async Task Single2Async()
  31. {
  32. var res = AsyncEnumerable.Empty<int>().SingleAsync(x => true);
  33. await AssertThrowsAsync<InvalidOperationException>(res);
  34. }
  35. [Fact]
  36. public async Task Single3Async()
  37. {
  38. var res = Return42.SingleAsync(x => x % 2 != 0);
  39. await AssertThrowsAsync<InvalidOperationException>(res);
  40. }
  41. [Fact]
  42. public async Task Single4Async()
  43. {
  44. var res = Return42.SingleAsync();
  45. Assert.Equal(42, await res);
  46. }
  47. [Fact]
  48. public async Task Single5Async()
  49. {
  50. var res = Return42.SingleAsync(x => x % 2 == 0);
  51. Assert.Equal(42, await res);
  52. }
  53. [Fact]
  54. public async Task Single6Async()
  55. {
  56. var ex = new Exception("Bang!");
  57. var res = Throw<int>(ex).SingleAsync();
  58. await AssertThrowsAsync(res, ex);
  59. }
  60. [Fact]
  61. public async Task Single7Async()
  62. {
  63. var ex = new Exception("Bang!");
  64. var res = Throw<int>(ex).SingleAsync(x => true);
  65. await AssertThrowsAsync(res, ex);
  66. }
  67. [Fact]
  68. public async Task Single8Async()
  69. {
  70. var res = new[] { 42, 45, 90 }.ToAsyncEnumerable().SingleAsync();
  71. await AssertThrowsAsync<InvalidOperationException>(res);
  72. }
  73. [Fact]
  74. public async Task Single9Async()
  75. {
  76. var res = new[] { 42, 45, 90 }.ToAsyncEnumerable().SingleAsync(x => x % 2 != 0);
  77. Assert.Equal(45, await res);
  78. }
  79. [Fact]
  80. public async Task Single10Async()
  81. {
  82. var res = new[] { 42, 23, 45, 90 }.ToAsyncEnumerable().SingleAsync(x => x % 2 != 0);
  83. await AssertThrowsAsync<InvalidOperationException>(res);
  84. }
  85. [Fact]
  86. public async Task Single11Async()
  87. {
  88. var res = new int[0].ToAsyncEnumerable().SingleAsync();
  89. await AssertThrowsAsync<InvalidOperationException>(res);
  90. }
  91. }
  92. }