ToList.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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 ToList : AsyncEnumerableTests
  12. {
  13. [Fact]
  14. public async Task ToList_Null()
  15. {
  16. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToListAsync<int>(default).AsTask());
  17. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToListAsync<int>(default, CancellationToken.None).AsTask());
  18. }
  19. [Fact]
  20. public async Task ToList_Simple()
  21. {
  22. var xs = new[] { 42, 25, 39 };
  23. var res = xs.ToAsyncEnumerable().ToListAsync();
  24. Assert.True((await res).SequenceEqual(xs));
  25. }
  26. [Fact]
  27. public async Task ToList_Empty()
  28. {
  29. var xs = AsyncEnumerable.Empty<int>();
  30. var res = xs.ToListAsync();
  31. Assert.True((await res).Count == 0);
  32. }
  33. [Fact]
  34. public async Task ToList_Throw()
  35. {
  36. var ex = new Exception("Bang!");
  37. var res = Throw<int>(ex).ToListAsync();
  38. await AssertThrowsAsync(res, ex);
  39. }
  40. }
  41. }