AsyncTests.Creation.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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.Tasks;
  8. using Xunit;
  9. namespace Tests
  10. {
  11. public partial class AsyncTests
  12. {
  13. [Fact]
  14. public void Create_Null()
  15. {
  16. AssertThrows<ArgumentNullException>(() => AsyncEnumerable.CreateEnumerable<int>(default(Func<IAsyncEnumerator<int>>)));
  17. AssertThrows<ArgumentNullException>(() => AsyncEnumerable.CreateEnumerator<int>(null, () => 3, () => Task.FromResult(true)));
  18. }
  19. [Fact]
  20. public void Create_Iterator_Throws()
  21. {
  22. var iter = AsyncEnumerable.CreateEnumerator<int>(() => Task.FromResult(true), () => 3, () => Task.FromResult(true));
  23. var enu = (IAsyncEnumerable<int>)iter;
  24. AssertThrows<NotSupportedException>(() => enu.GetAsyncEnumerator());
  25. }
  26. [Fact]
  27. public void Return()
  28. {
  29. var xs = AsyncEnumerable.Return(42);
  30. HasNext(xs.GetAsyncEnumerator(), 42);
  31. }
  32. [Fact]
  33. public async Task Never()
  34. {
  35. var xs = AsyncEnumerableEx.Never<int>();
  36. var e = xs.GetAsyncEnumerator();
  37. Assert.False(e.MoveNextAsync().IsCompleted); // Very rudimentary check
  38. AssertThrows<InvalidOperationException>(() => Nop(e.Current));
  39. await e.DisposeAsync();
  40. }
  41. [Fact]
  42. public void Throw_Null()
  43. {
  44. AssertThrows<ArgumentNullException>(() => AsyncEnumerable.Throw<int>(null));
  45. }
  46. [Fact]
  47. public void Throw()
  48. {
  49. var ex = new Exception("Bang");
  50. var xs = AsyncEnumerable.Throw<int>(ex);
  51. var e = xs.GetAsyncEnumerator();
  52. AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), (Exception ex_) => ((AggregateException)ex_).InnerExceptions.Single() == ex);
  53. AssertThrows<InvalidOperationException>(() => Nop(e.Current));
  54. }
  55. private void Nop(object o)
  56. {
  57. }
  58. }
  59. }