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 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 ToList : AsyncEnumerableTests
  12. {
  13. [Fact]
  14. public async Task ToList_Null()
  15. {
  16. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToList<int>(null));
  17. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.ToList<int>(null, CancellationToken.None));
  18. }
  19. [Fact]
  20. public void ToList1()
  21. {
  22. var xs = new[] { 42, 25, 39 };
  23. var res = xs.ToAsyncEnumerable().ToList();
  24. Assert.True(res.Result.SequenceEqual(xs));
  25. }
  26. [Fact]
  27. public void ToList2()
  28. {
  29. var xs = AsyncEnumerable.Empty<int>();
  30. var res = xs.ToList();
  31. Assert.True(res.Result.Count == 0);
  32. }
  33. [Fact]
  34. public void ToList3()
  35. {
  36. var ex = new Exception("Bang!");
  37. var res = AsyncEnumerable.Throw<int>(ex).ToList();
  38. AssertThrows<Exception>(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  39. }
  40. }
  41. }