ElementAt.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 ElementAt : AsyncEnumerableTests
  12. {
  13. [Fact]
  14. public async Task ElementAt_Null()
  15. {
  16. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ElementAt<int>(default, 0));
  17. await Assert.ThrowsAsync<ArgumentOutOfRangeException>(() => AsyncEnumerable.ElementAt(Return42, -1));
  18. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ElementAt<int>(default, 0, CancellationToken.None));
  19. await Assert.ThrowsAsync<ArgumentOutOfRangeException>(() => AsyncEnumerable.ElementAt(Return42, -1, CancellationToken.None));
  20. }
  21. [Fact]
  22. public async Task ElementAt1Async()
  23. {
  24. var res = AsyncEnumerable.Empty<int>().ElementAt(0);
  25. await AssertThrowsAsync<ArgumentOutOfRangeException>(res);
  26. }
  27. [Fact]
  28. public async Task ElementAt2Async()
  29. {
  30. var res = Return42.ElementAt(0);
  31. Assert.Equal(42, await res);
  32. }
  33. [Fact]
  34. public async Task ElementAt3Async()
  35. {
  36. var res = Return42.ElementAt(1);
  37. await AssertThrowsAsync<ArgumentOutOfRangeException>(res);
  38. }
  39. [Fact]
  40. public async Task ElementAt4Async()
  41. {
  42. var res = new[] { 1, 42, 3 }.ToAsyncEnumerable().ElementAt(1);
  43. Assert.Equal(42, await res);
  44. }
  45. [Fact]
  46. public async Task ElementAt5Async()
  47. {
  48. var res = new[] { 1, 42, 3 }.ToAsyncEnumerable().ElementAt(7);
  49. await AssertThrowsAsync<ArgumentOutOfRangeException>(res);
  50. }
  51. [Fact]
  52. public async Task ElementAt6Async()
  53. {
  54. var ex = new Exception("Bang!");
  55. var res = Throw<int>(ex).ElementAt(15);
  56. await AssertThrowsAsync(res, ex);
  57. }
  58. }
  59. }