| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 | // 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.Linq;using System.Threading.Tasks;using Xunit;namespace Tests{    public class Generate : AsyncEnumerableExTests    {        [Fact]        public void Generate_Null()        {            AssertThrows<ArgumentNullException>(() => AsyncEnumerableEx.Generate<int, int>(0, default(Func<int, bool>), x => x, x => x));            AssertThrows<ArgumentNullException>(() => AsyncEnumerableEx.Generate<int, int>(0, x => true, default(Func<int, int>), x => x));            AssertThrows<ArgumentNullException>(() => AsyncEnumerableEx.Generate<int, int>(0, x => true, x => x, default(Func<int, int>)));        }        [Fact]        public async Task Generate1()        {            var xs = AsyncEnumerableEx.Generate(0, x => x < 5, x => x + 1, x => x * x);            var e = xs.GetAsyncEnumerator();            HasNext(e, 0);            HasNext(e, 1);            HasNext(e, 4);            HasNext(e, 9);            HasNext(e, 16);            NoNext(e);            await e.DisposeAsync();        }        [Fact]        public void Generate2()        {            var ex = new Exception("Bang!");            var xs = AsyncEnumerableEx.Generate(0, x => { throw ex; }, x => x + 1, x => x * x);            var e = xs.GetAsyncEnumerator();            AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), (Exception ex_) => ((AggregateException)ex_).InnerExceptions.Single() == ex);        }        [Fact]        public void Generate3()        {            var ex = new Exception("Bang!");            var xs = AsyncEnumerableEx.Generate(0, x => true, x => x + 1, x => { if (x == 1) throw ex; return x * x; });            var e = xs.GetAsyncEnumerator();            HasNext(e, 0);            AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), (Exception ex_) => ((AggregateException)ex_).InnerExceptions.Single() == ex);        }        [Fact]        public void Generate4()        {            var ex = new Exception("Bang!");            var xs = AsyncEnumerableEx.Generate(0, x => true, x => { throw ex; }, x => x * x);            var e = xs.GetAsyncEnumerator();            HasNext(e, 0);            AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), (Exception ex_) => ((AggregateException)ex_).InnerExceptions.Single() == ex);        }        [Fact]        public async Task Generate5()        {            var xs = AsyncEnumerableEx.Generate(0, x => x < 5, x => x + 1, x => x * x);            await SequenceIdentity(xs);        }    }}
 |