StartWith.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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.Tasks;
  7. using Xunit;
  8. namespace Tests
  9. {
  10. public class StartWith : AsyncEnumerableExTests
  11. {
  12. [Fact]
  13. public void StartWith_Null()
  14. {
  15. Assert.Throws<ArgumentNullException>(() => AsyncEnumerableEx.StartWith(default, new[] { 1 }));
  16. Assert.Throws<ArgumentNullException>(() => AsyncEnumerableEx.StartWith(Return42, default));
  17. }
  18. [Fact]
  19. public async Task StartWith1Async()
  20. {
  21. var xs = AsyncEnumerable.Empty<int>().StartWith(1, 2);
  22. var e = xs.GetAsyncEnumerator();
  23. await HasNextAsync(e, 1);
  24. await HasNextAsync(e, 2);
  25. await NoNextAsync(e);
  26. }
  27. [Fact]
  28. public async Task StartWith2Async()
  29. {
  30. var xs = Return42.StartWith(40, 41);
  31. var e = xs.GetAsyncEnumerator();
  32. await HasNextAsync(e, 40);
  33. await HasNextAsync(e, 41);
  34. await HasNextAsync(e, 42);
  35. await NoNextAsync(e);
  36. }
  37. [Fact]
  38. public async Task StartWith3Async()
  39. {
  40. var ex = new Exception("Bang!");
  41. var xs = Throw<int>(ex).StartWith(1, 2);
  42. var e = xs.GetAsyncEnumerator();
  43. await HasNextAsync(e, 1);
  44. await HasNextAsync(e, 2);
  45. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  46. }
  47. }
  48. }