AsyncTests.Creation.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 Throw_Null()
  28. {
  29. AssertThrows<ArgumentNullException>(() => AsyncEnumerable.Throw<int>(null));
  30. }
  31. [Fact]
  32. public void Throw()
  33. {
  34. var ex = new Exception("Bang");
  35. var xs = AsyncEnumerable.Throw<int>(ex);
  36. var e = xs.GetAsyncEnumerator();
  37. AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), (Exception ex_) => ((AggregateException)ex_).InnerExceptions.Single() == ex);
  38. AssertThrows<InvalidOperationException>(() => Nop(e.Current));
  39. }
  40. private void Nop(object o)
  41. {
  42. }
  43. }
  44. }