123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the Apache 2.0 License.
- // See the LICENSE file in the project root for more information.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Xunit;
- namespace Tests
- {
- public class ToAsyncEnumerable : AsyncEnumerableTests
- {
- [Fact]
- public void ToAsyncEnumerable_Null()
- {
- Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.ToAsyncEnumerable(default(IEnumerable<int>)));
- Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.ToAsyncEnumerable(default(IObservable<int>)));
- Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.ToAsyncEnumerable(default(Task<int>)));
- }
- [Fact]
- public async Task ToAsyncEnumerable1Async()
- {
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
- var e = xs.GetAsyncEnumerator();
- await HasNextAsync(e, 1);
- await HasNextAsync(e, 2);
- await HasNextAsync(e, 3);
- await HasNextAsync(e, 4);
- await NoNextAsync(e);
- }
- [Fact]
- public async Task ToAsyncEnumerable2Async()
- {
- var ex = new Exception("Bang");
- var xs = ToAsyncEnumerable_Sequence(ex).ToAsyncEnumerable();
- var e = xs.GetAsyncEnumerator();
- await HasNextAsync(e, 42);
- await AssertThrowsAsync(e.MoveNextAsync(), ex);
- }
- private IEnumerable<int> ToAsyncEnumerable_Sequence(Exception e)
- {
- yield return 42;
- throw e;
- }
- [Fact]
- public async Task ToAsyncEnumerable3Async()
- {
- var subscribed = false;
- var xs = new MyObservable<int>(obs =>
- {
- subscribed = true;
- obs.OnNext(42);
- obs.OnCompleted();
- return new MyDisposable(() => { });
- }).ToAsyncEnumerable();
- Assert.False(subscribed);
- var e = xs.GetAsyncEnumerator();
- Assert.True(subscribed);
- await HasNextAsync(e, 42);
- await NoNextAsync(e);
- }
- [Fact]
- public async Task ToAsyncEnumerable4Async()
- {
- var ex = new Exception("Bang!");
- var subscribed = false;
- var xs = new MyObservable<int>(obs =>
- {
- subscribed = true;
- obs.OnError(ex);
- return new MyDisposable(() => { });
- }).ToAsyncEnumerable();
- Assert.False(subscribed);
- var e = xs.GetAsyncEnumerator();
- Assert.True(subscribed);
- await AssertThrowsAsync(e.MoveNextAsync(), ex);
- }
- [Fact]
- public async Task ToAsyncEnumerable5Async()
- {
- var set = new HashSet<int>(new[] { 1, 2, 3, 4 });
- var xs = set.ToAsyncEnumerable();
- var e = xs.GetAsyncEnumerator();
- await HasNextAsync(e, 1);
- await HasNextAsync(e, 2);
- await HasNextAsync(e, 3);
- await HasNextAsync(e, 4);
- await NoNextAsync(e);
- }
- [Fact]
- public async Task ToAsyncEnumerable6()
- {
- var set = new HashSet<int>(new[] { 1, 2, 3, 4, 5, 6, 7, 8 });
- var xs = set.ToAsyncEnumerable();
- var arr = await xs.ToArrayAsync();
- Assert.True(set.SetEquals(arr));
- }
- [Fact]
- public async Task ToAsyncEnumerable7()
- {
- var set = new HashSet<int>(new[] { 1, 2, 3, 4 });
- var xs = set.ToAsyncEnumerable();
- var arr = await xs.ToListAsync();
- Assert.True(set.SetEquals(arr));
- }
- [Fact]
- public async Task ToAsyncEnumerable8()
- {
- var set = new HashSet<int>(new[] { 1, 2, 3, 4 });
- var xs = set.ToAsyncEnumerable();
- var c = await xs.CountAsync();
- Assert.Equal(set.Count, c);
- }
- [Fact]
- public async Task ToAsyncEnumerable9()
- {
- var set = new HashSet<int>(new[] { 1, 2, 3, 4 });
- var xs = set.ToAsyncEnumerable();
- await SequenceIdentity(xs);
- }
- [Fact]
- public async Task ToAsyncEnumerable10()
- {
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
- await SequenceIdentity(xs);
- }
- [Fact]
- public void ToAsyncEnumerable11()
- {
- var set = new HashSet<int>(new[] { 1, 2, 3, 4 });
- var xs = set.ToAsyncEnumerable();
- var xc = xs as ICollection<int>;
- Assert.NotNull(xc);
- Assert.False(xc.IsReadOnly);
- xc.Add(5);
- Assert.True(xc.Contains(5));
- Assert.True(xc.Remove(5));
- var arr = new int[4];
- xc.CopyTo(arr, 0);
- Assert.True(arr.SequenceEqual(xc));
- xc.Clear();
- Assert.Equal(0, xc.Count);
- }
- [Fact]
- public void ToAsyncEnumerable12()
- {
- var set = new List<int> { 1, 2, 3, 4 };
- var xs = set.ToAsyncEnumerable();
- var xl = xs as IList<int>;
- Assert.NotNull(xl);
- Assert.False(xl.IsReadOnly);
- xl.Add(5);
- Assert.True(xl.Contains(5));
- Assert.True(xl.Remove(5));
- xl.Insert(2, 10);
- Assert.Equal(2, xl.IndexOf(10));
- xl.RemoveAt(2);
- xl[0] = 7;
- Assert.Equal(7, xl[0]);
- var arr = new int[4];
- xl.CopyTo(arr, 0);
- Assert.True(arr.SequenceEqual(xl));
- xl.Clear();
- Assert.Equal(0, xl.Count);
- }
- [Fact]
- public async Task ToAsyncEnumerable_With_Completed_TaskAsync()
- {
- var task = Task.Factory.StartNew(() => 36);
- var xs = task.ToAsyncEnumerable();
- var e = xs.GetAsyncEnumerator();
- Assert.True(await e.MoveNextAsync());
- Assert.Equal(36, e.Current);
- Assert.False(await e.MoveNextAsync());
- }
- [Fact]
- public async Task ToAsyncEnumerable_With_Faulted_TaskAsync()
- {
- var ex = new InvalidOperationException();
- var tcs = new TaskCompletionSource<int>();
- tcs.SetException(ex);
- var xs = tcs.Task.ToAsyncEnumerable();
- var e = xs.GetAsyncEnumerator();
- await AssertThrowsAsync(e.MoveNextAsync(), ex);
- }
- [Fact]
- public async Task ToAsyncEnumerable_With_Canceled_TaskAsync()
- {
- var tcs = new TaskCompletionSource<int>();
- tcs.SetCanceled();
- var xs = tcs.Task.ToAsyncEnumerable();
- var e = xs.GetAsyncEnumerator();
- await AssertThrowsAsync<TaskCanceledException>(e.MoveNextAsync().AsTask());
- }
- private sealed class MyObservable<T> : IObservable<T>
- {
- private readonly Func<IObserver<T>, IDisposable> _subscribe;
- public MyObservable(Func<IObserver<T>, IDisposable> subscribe)
- {
- _subscribe = subscribe;
- }
- public IDisposable Subscribe(IObserver<T> observer)
- {
- return _subscribe(observer);
- }
- }
- private sealed class MyDisposable : IDisposable
- {
- private readonly Action _dispose;
- public MyDisposable(Action dispose)
- {
- _dispose = dispose;
- }
- public void Dispose()
- {
- _dispose();
- }
- }
- }
- }
|