StartWith.cs 1.7 KB

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