Defer.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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 Xunit;
  8. namespace Tests
  9. {
  10. public class Defer : AsyncEnumerableExTests
  11. {
  12. [Fact]
  13. public void Defer_Null()
  14. {
  15. Assert.Throws<ArgumentNullException>(() => AsyncEnumerableEx.Defer<int>(default(Func<IAsyncEnumerable<int>>)));
  16. }
  17. [Fact]
  18. public async System.Threading.Tasks.Task Defer1Async()
  19. {
  20. var x = 0;
  21. var xs = AsyncEnumerableEx.Defer<int>(() => new[] { x }.ToAsyncEnumerable());
  22. {
  23. var e = xs.GetAsyncEnumerator();
  24. await HasNextAsync(e, 0);
  25. await NoNextAsync(e);
  26. }
  27. {
  28. x++;
  29. var e = xs.GetAsyncEnumerator();
  30. await HasNextAsync(e, 1);
  31. await NoNextAsync(e);
  32. }
  33. }
  34. }
  35. }