123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- // 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;
- using System.Threading.Tasks;
- using Xunit;
- namespace Tests
- {
- public class Single : AsyncEnumerableTests
- {
- [Fact]
- public async Task Single_Null()
- {
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.SingleAsync<int>(default));
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.SingleAsync<int>(default, x => true));
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.SingleAsync(Return42, default(Func<int, bool>)));
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.SingleAsync<int>(default, CancellationToken.None));
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.SingleAsync<int>(default, x => true, CancellationToken.None));
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.SingleAsync(Return42, default(Func<int, bool>), CancellationToken.None));
- }
- [Fact]
- public async Task Single1Async()
- {
- var res = AsyncEnumerable.Empty<int>().SingleAsync();
- await AssertThrowsAsync<InvalidOperationException>(res);
- }
- [Fact]
- public async Task Single2Async()
- {
- var res = AsyncEnumerable.Empty<int>().SingleAsync(x => true);
- await AssertThrowsAsync<InvalidOperationException>(res);
- }
- [Fact]
- public async Task Single3Async()
- {
- var res = Return42.SingleAsync(x => x % 2 != 0);
- await AssertThrowsAsync<InvalidOperationException>(res);
- }
- [Fact]
- public async Task Single4Async()
- {
- var res = Return42.SingleAsync();
- Assert.Equal(42, await res);
- }
- [Fact]
- public async Task Single5Async()
- {
- var res = Return42.SingleAsync(x => x % 2 == 0);
- Assert.Equal(42, await res);
- }
- [Fact]
- public async Task Single6Async()
- {
- var ex = new Exception("Bang!");
- var res = Throw<int>(ex).SingleAsync();
- await AssertThrowsAsync(res, ex);
- }
- [Fact]
- public async Task Single7Async()
- {
- var ex = new Exception("Bang!");
- var res = Throw<int>(ex).SingleAsync(x => true);
- await AssertThrowsAsync(res, ex);
- }
- [Fact]
- public async Task Single8Async()
- {
- var res = new[] { 42, 45, 90 }.ToAsyncEnumerable().SingleAsync();
- await AssertThrowsAsync<InvalidOperationException>(res);
- }
- [Fact]
- public async Task Single9Async()
- {
- var res = new[] { 42, 45, 90 }.ToAsyncEnumerable().SingleAsync(x => x % 2 != 0);
- Assert.Equal(45, await res);
- }
- [Fact]
- public async Task Single10Async()
- {
- var res = new[] { 42, 23, 45, 90 }.ToAsyncEnumerable().SingleAsync(x => x % 2 != 0);
- await AssertThrowsAsync<InvalidOperationException>(res);
- }
- [Fact]
- public async Task Single11Async()
- {
- var res = new int[0].ToAsyncEnumerable().SingleAsync();
- await AssertThrowsAsync<InvalidOperationException>(res);
- }
- }
- }
|