< Summary

Class:Azure.Core.PageableHelpers
Assembly:Azure.ResourceManager.Network
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\Shared\AutoRest\PageableHelpers.cs
Covered lines:32
Uncovered lines:0
Coverable lines:32
Total lines:85
Line coverage:100% (32 of 32)
Covered branches:19
Total branches:26
Branch coverage:73% (19 of 26)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
CreateEnumerable(...)-100%100%
CreateAsyncEnumerable(...)-100%100%
.ctor(...)-100%100%
AsPages()-100%68.75%
.ctor(...)-100%100%
AsPages()-100%66.67%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\Shared\AutoRest\PageableHelpers.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4#nullable enable
 5
 6using System;
 7using System.Collections.Generic;
 8using System.Threading.Tasks;
 9
 10namespace Azure.Core
 11{
 12    internal static class PageableHelpers
 13    {
 14        public static Pageable<T> CreateEnumerable<T>(Func<int?, Page<T>> firstPageFunc, Func<string?, int?, Page<T>>? n
 15        {
 40216            PageFunc<T> first = (continuationToken, pageSizeHint) => firstPageFunc(pageSizeHint);
 20217            PageFunc<T>? next = nextPageFunc != null ? new PageFunc<T>(nextPageFunc) : null;
 20218            return new FuncPageable<T>(first, next, pageSize);
 19        }
 20
 21        public static AsyncPageable<T> CreateAsyncEnumerable<T>(Func<int?, Task<Page<T>>> firstPageFunc, Func<string?, i
 22        {
 40223            AsyncPageFunc<T> first = (continuationToken, pageSizeHint) => firstPageFunc(pageSizeHint);
 20224            AsyncPageFunc<T>? next = nextPageFunc != null ? new AsyncPageFunc<T>(nextPageFunc) : null;
 20225            return new FuncAsyncPageable<T>(first, next, pageSize);
 26        }
 27
 28        internal delegate Task<Page<T>> AsyncPageFunc<T>(string? continuationToken = default, int? pageSizeHint = defaul
 29        internal delegate Page<T> PageFunc<T>(string? continuationToken = default, int? pageSizeHint = default);
 30
 31        internal class FuncAsyncPageable<T> : AsyncPageable<T> where T : notnull
 32        {
 33            private readonly AsyncPageFunc<T> _firstPageFunc;
 34            private readonly AsyncPageFunc<T>? _nextPageFunc;
 35            private readonly int? _defaultPageSize;
 36
 20237            public FuncAsyncPageable(AsyncPageFunc<T> firstPageFunc, AsyncPageFunc<T>? nextPageFunc, int? defaultPageSiz
 38            {
 20239                _firstPageFunc = firstPageFunc;
 20240                _nextPageFunc = nextPageFunc;
 20241                _defaultPageSize = defaultPageSize;
 20242            }
 43
 44            public override async IAsyncEnumerable<Page<T>> AsPages(string? continuationToken = default, int? pageSizeHi
 45            {
 20046                AsyncPageFunc<T>? pageFunc = _firstPageFunc;
 20047                int? pageSize = pageSizeHint ?? _defaultPageSize;
 48                do
 49                {
 20050                    Page<T> pageResponse = await pageFunc(continuationToken, pageSize).ConfigureAwait(false);
 20051                    yield return pageResponse;
 20052                    continuationToken = pageResponse.ContinuationToken;
 20053                    pageFunc = _nextPageFunc;
 40054                } while (!string.IsNullOrEmpty(continuationToken) && pageFunc != null);
 20055            }
 56        }
 57
 58        internal class FuncPageable<T> : Pageable<T> where T : notnull
 59        {
 60            private readonly PageFunc<T> _firstPageFunc;
 61            private readonly PageFunc<T>? _nextPageFunc;
 62            private readonly int? _defaultPageSize;
 63
 20264            public FuncPageable(PageFunc<T> firstPageFunc, PageFunc<T>? nextPageFunc, int? defaultPageSize = default)
 65            {
 20266                _firstPageFunc = firstPageFunc;
 20267                _nextPageFunc = nextPageFunc;
 20268                _defaultPageSize = defaultPageSize;
 20269            }
 70
 71            public override IEnumerable<Page<T>> AsPages(string? continuationToken = default, int? pageSizeHint = defaul
 72            {
 20073                PageFunc<T>? pageFunc = _firstPageFunc;
 20074                int? pageSize = pageSizeHint ?? _defaultPageSize;
 75                do
 76                {
 20077                    Page<T> pageResponse = pageFunc(continuationToken, pageSize);
 20078                    yield return pageResponse;
 20079                    continuationToken = pageResponse.ContinuationToken;
 20080                    pageFunc = _nextPageFunc;
 40081                } while (!string.IsNullOrEmpty(continuationToken) && pageFunc != null);
 20082            }
 83        }
 84    }
 85}