コード例 #1
0
        public static async Task <List <T> > ToList <T>(this IAsyncEnumerator <T> asyncEnumerator)
        {
            List <T> itemList = new List <T>();
            await asyncEnumerator.ForEach(itemList, (item, list) => list.Add(item)).ConfigureAwait(false);

            return(itemList);
        }
コード例 #2
0
        public void ItemExceptionIsPropagated()
        {
            IAsyncEnumerator <int> enumerator = TaskAsyncEnumerator.Create
                                                (
                Task.Delay(100).ContinueWith(_ => 64),
                Task.FromException <int>(new InvalidOperationException())
                                                );

            Func <Task> act = () => enumerator.ForEach(_ => { });

            act.ShouldThrow <InvalidOperationException>();
        }
コード例 #3
0
        public async Task LambdaIsCalledForEachItem()
        {
            List <int> callArguments = new List <int>();

            Task <int>[] itemTasks = new Task <int>[]
            {
                Task.FromResult(42),
                Task.Delay(100).ContinueWith(_ => 64),
                Task.FromResult(128)
            };

            IAsyncEnumerator <int> enumerator = TaskAsyncEnumerator.Create(itemTasks);
            await enumerator.ForEach(item => callArguments.Add(item)).ConfigureAwait(true);

            int[] expectedItems = await Task.WhenAll(itemTasks).ConfigureAwait(true);

            callArguments.Should().HaveEquivalentItems(expectedItems);
        }
コード例 #4
0
        public void LambdaExceptionIsPropagated()
        {
            const int dangerousItem           = 42;
            IAsyncEnumerator <int> enumerator = TaskAsyncEnumerator.Create
                                                (
                Task.Delay(100).ContinueWith(_ => 64),
                Task.FromResult(dangerousItem)
                                                );

            Func <Task> act
                = () =>
                  enumerator.ForEach
                  (
                      value =>
            {
                if (value == dangerousItem)
                {
                    throw new InvalidOperationException();
                }
            }
                  );

            act.ShouldThrow <InvalidOperationException>();
        }
コード例 #5
0
 public static Task ForEach <T>(this IAsyncEnumerator <T> asyncEnumerator, Action <T> action)
 => asyncEnumerator.ForEach(action, (item, actionArg) => actionArg(item));