Cast.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 Cast : AsyncEnumerableTests
  11. {
  12. [Fact]
  13. public void Cast_Null()
  14. {
  15. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Cast<int>(default));
  16. }
  17. [Fact]
  18. public async Task Cast_References()
  19. {
  20. var xs = new object[] { "bar", "foo", "qux" }.ToAsyncEnumerable();
  21. var ys = xs.Cast<string>();
  22. var e = ys.GetAsyncEnumerator();
  23. await HasNextAsync(e, "bar");
  24. await HasNextAsync(e, "foo");
  25. await HasNextAsync(e, "qux");
  26. await NoNextAsync(e);
  27. }
  28. [Fact]
  29. public async Task Cast_Values()
  30. {
  31. var xs = new object[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  32. var ys = xs.Cast<int>();
  33. var e = ys.GetAsyncEnumerator();
  34. await HasNextAsync(e, 1);
  35. await HasNextAsync(e, 2);
  36. await HasNextAsync(e, 3);
  37. await HasNextAsync(e, 4);
  38. await NoNextAsync(e);
  39. }
  40. [Fact]
  41. public async Task Cast_InvalidCast()
  42. {
  43. var xs = new object[] { 42, "foo", 43 }.ToAsyncEnumerable();
  44. var ys = xs.Cast<int>();
  45. var e = ys.GetAsyncEnumerator();
  46. await HasNextAsync(e, 42);
  47. await AssertThrowsAsync<InvalidCastException>(e.MoveNextAsync().AsTask());
  48. }
  49. [Fact]
  50. public void Cast_Aliasing()
  51. {
  52. var xs = new[] { new EventArgs(), new EventArgs(), new EventArgs() }.ToAsyncEnumerable();
  53. var ys = xs.Cast<EventArgs>();
  54. Assert.Same(xs, ys);
  55. }
  56. }
  57. }