All.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using Xunit;
  10. namespace Tests
  11. {
  12. public class All : AsyncEnumerableTests
  13. {
  14. [Fact]
  15. public async Task All_Null()
  16. {
  17. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.All<int>(default, x => true));
  18. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.All<int>(Return42, default(Func<int, bool>)));
  19. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.All<int>(default, x => true, CancellationToken.None));
  20. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.All<int>(Return42, default(Func<int, bool>), CancellationToken.None));
  21. }
  22. [Fact]
  23. public void All1()
  24. {
  25. var res = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().All(x => x % 2 == 0);
  26. Assert.False(res.Result);
  27. }
  28. [Fact]
  29. public void All2()
  30. {
  31. var res = new[] { 2, 8, 4 }.ToAsyncEnumerable().All(x => x % 2 == 0);
  32. Assert.True(res.Result);
  33. }
  34. [Fact]
  35. public void All3()
  36. {
  37. var ex = new Exception("Bang!");
  38. var res = Throw<int>(ex).All(x => x % 2 == 0);
  39. AssertThrows<Exception>(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  40. }
  41. [Fact]
  42. public void All4()
  43. {
  44. var ex = new Exception("Bang!");
  45. var res = new[] { 2, 8, 4 }.ToAsyncEnumerable().All(new Func<int, bool>(x => { throw ex; }));
  46. AssertThrows<Exception>(() => res.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  47. }
  48. }
  49. }