// 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; using System.Threading.Tasks; using Xunit; namespace Tests { public class All : AsyncEnumerableTests { [Fact] public async Task All_Null() { await Assert.ThrowsAsync(() => AsyncEnumerable.All(default, x => true)); await Assert.ThrowsAsync(() => AsyncEnumerable.All(Return42, default(Func))); await Assert.ThrowsAsync(() => AsyncEnumerable.All(default, x => true, CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerable.All(Return42, default(Func), CancellationToken.None)); } [Fact] public void All1() { var res = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().All(x => x % 2 == 0); Assert.False(res.Result); } [Fact] public void All2() { var res = new[] { 2, 8, 4 }.ToAsyncEnumerable().All(x => x % 2 == 0); Assert.True(res.Result); } [Fact] public void All3() { var ex = new Exception("Bang!"); var res = Throw(ex).All(x => x % 2 == 0); AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] public void All4() { var ex = new Exception("Bang!"); var res = new[] { 2, 8, 4 }.ToAsyncEnumerable().All(new Func(x => { throw ex; })); AssertThrows(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } } }