Defer.cs 1.1 KB

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