// 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 TakeWhile : AsyncEnumerableTests { [Fact] public void TakeWhile_Sync_Null() { Assert.Throws(() => AsyncEnumerable.TakeWhile(default, x => true)); Assert.Throws(() => AsyncEnumerable.TakeWhile(default, (x, i) => true)); Assert.Throws(() => AsyncEnumerable.TakeWhile(Return42, default(Func))); Assert.Throws(() => AsyncEnumerable.TakeWhile(Return42, default(Func))); } [Fact] public async Task TakeWhile_Sync_Simple1() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.TakeWhile(x => x < 3); var e = ys.GetAsyncEnumerator(); await HasNextAsync(e, 1); await HasNextAsync(e, 2); await NoNextAsync(e); } [Fact] public async Task TakeWhile_Sync_Simple2() { var xs = new[] { 1, 2, 3, 4, 3, 2, 1 }.ToAsyncEnumerable(); var ys = xs.TakeWhile(x => x < 3); var e = ys.GetAsyncEnumerator(); await HasNextAsync(e, 1); await HasNextAsync(e, 2); await NoNextAsync(e); } [Fact] public async Task TakeWhile_Sync_False() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.TakeWhile(x => false); var e = ys.GetAsyncEnumerator(); await NoNextAsync(e); } [Fact] public async Task TakeWhile_Sync_True() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.TakeWhile(x => true); var e = ys.GetAsyncEnumerator(); await HasNextAsync(e, 1); await HasNextAsync(e, 2); await HasNextAsync(e, 3); await HasNextAsync(e, 4); await NoNextAsync(e); } [Fact] public async Task TakeWhile_Sync_Throws_Predicate() { var ex = new Exception("Bang"); var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.TakeWhile(new Func(x => { throw ex; })); var e = ys.GetAsyncEnumerator(); await AssertThrowsAsync(e.MoveNextAsync(), ex); } [Fact] public async Task TakeWhile_Sync_Indexed() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.TakeWhile((x, i) => i < 2); var e = ys.GetAsyncEnumerator(); await HasNextAsync(e, 1); await HasNextAsync(e, 2); await NoNextAsync(e); } [Fact] public async Task TakeWhile_Sync_Indexed_False() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.TakeWhile((x, i) => false); var e = ys.GetAsyncEnumerator(); await NoNextAsync(e); } [Fact] public async Task TakeWhile_Sync_Indexed_True() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.TakeWhile((x, i) => true); var e = ys.GetAsyncEnumerator(); await HasNextAsync(e, 1); await HasNextAsync(e, 2); await HasNextAsync(e, 3); await HasNextAsync(e, 4); await NoNextAsync(e); } [Fact] public async Task TakeWhile_Sync_Indexed_Throws_Predicate() { var ex = new Exception("Bang"); var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.TakeWhile(new Func((x, i) => { throw ex; })); var e = ys.GetAsyncEnumerator(); await AssertThrowsAsync(e.MoveNextAsync(), ex); } [Fact] public async Task TakeWhile_Sync_SequenceIdentity() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.TakeWhile(x => x < 3); await SequenceIdentity(ys); } [Fact] public async Task TakeWhile_Sync_Indexed_SequenceIdentity() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.TakeWhile((x, i) => i < 2); await SequenceIdentity(ys); } } }