Last.cs 3.3 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 Last : AsyncEnumerableTests
  13. {
  14. [Fact]
  15. public async Task Last_Null()
  16. {
  17. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.Last<int>(default));
  18. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.Last<int>(default, x => true));
  19. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.Last<int>(Return42, default(Func<int, bool>)));
  20. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.Last<int>(default, CancellationToken.None));
  21. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.Last<int>(default, x => true, CancellationToken.None));
  22. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.Last<int>(Return42, default(Func<int, bool>), CancellationToken.None));
  23. }
  24. [Fact]
  25. public void Last1()
  26. {
  27. var res = AsyncEnumerable.Empty<int>().Last();
  28. AssertThrows<Exception>(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException);
  29. }
  30. [Fact]
  31. public void Last2()
  32. {
  33. var res = AsyncEnumerable.Empty<int>().Last(x => true);
  34. AssertThrows<Exception>(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException);
  35. }
  36. [Fact]
  37. public void Last3()
  38. {
  39. var res = Return42.Last(x => x % 2 != 0);
  40. AssertThrows<Exception>(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException);
  41. }
  42. [Fact]
  43. public void Last4()
  44. {
  45. var res = Return42.Last();
  46. Assert.Equal(42, res.Result);
  47. }
  48. [Fact]
  49. public void Last5()
  50. {
  51. var res = Return42.Last(x => x % 2 == 0);
  52. Assert.Equal(42, res.Result);
  53. }
  54. [Fact]
  55. public void Last6()
  56. {
  57. var ex = new Exception("Bang!");
  58. var res = Throw<int>(ex).Last();
  59. AssertThrows<Exception>(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  60. }
  61. [Fact]
  62. public void Last7()
  63. {
  64. var ex = new Exception("Bang!");
  65. var res = Throw<int>(ex).Last(x => true);
  66. AssertThrows<Exception>(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  67. }
  68. [Fact]
  69. public void Last8()
  70. {
  71. var res = new[] { 42, 45, 90 }.ToAsyncEnumerable().Last();
  72. Assert.Equal(90, res.Result);
  73. }
  74. [Fact]
  75. public void Last9()
  76. {
  77. var res = new[] { 42, 23, 45, 90 }.ToAsyncEnumerable().Last(x => x % 2 != 0);
  78. Assert.Equal(45, res.Result);
  79. }
  80. }
  81. }