feat: Adds AsyncPaginationExtensions (#2516)

This commit is contained in:
Stefan
2022-08-08 17:27:46 +02:00
committed by GitHub
parent 651d9818c7
commit eaef1eee26
8 changed files with 1109 additions and 0 deletions
@@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NSubstitute;
using Octokit.AsyncPaginationExtension;
using Xunit;
namespace Octokit.Tests
{
public class AsyncEnumerableExtensionTests
{
public class ThePaginatedList
{
[Fact]
public void RejectsInvalidValues()
{
var client = Substitute.For<IRepositoriesClient>();
Assert.ThrowsAsync<ArgumentOutOfRangeException>(() => client.GetAllForOrgAsync("octokit")[-1]);
}
[Fact]
public async Task ReturnsNullOnExceedingTotalSize()
{
var client = Substitute.For<IRepositoriesClient>();
Assert.Null(await client.GetAllForOrgAsync("octokit")[int.MaxValue]);
}
[Fact]
public async Task EnumeratesCorrectValues()
{
var client = Substitute.For<IRepositoriesClient>();
var list = new List<Repository>();
var enumerator = client.GetAllForOrgAsync("octokit").GetAsyncEnumerator();
while (await enumerator.MoveNextAsync())
{
list.Add(enumerator.Current);
}
Assert.Equal(await client.GetAllForOrg("octokit"), list);
}
[Fact]
public async Task HandlesZeroCorrectly()
{
var client = Substitute.For<IRepositoriesClient>();
client.GetAllForOrg(Arg.Any<string>(), Arg.Any<ApiOptions>())
.Returns(Enumerable.Repeat(new Repository(), 24).ToList());
Assert.NotNull(await client.GetAllForOrgAsync("octokit")[0]);
}
[Fact]
public async Task HandlesPageEdgesCorrectly()
{
var client = Substitute.For<IRepositoriesClient>();
client.GetAllForOrg(Arg.Any<string>(), Arg.Any<ApiOptions>())
.Returns(Enumerable.Repeat(new Repository(), 5).ToList());
var repos = client.GetAllForOrgAsync("octokit", 5);
Assert.NotNull(await repos[4]);
Assert.NotNull(await repos[5]);
}
}
public class ThePaginationOverloads
{
[Fact]
public void RejectInvalidValues()
{
var client = Substitute.For<IRepositoriesClient>();
Assert.Throws<ArgumentOutOfRangeException>(() => client.GetAllForUserAsync("fake", -1));
Assert.Throws<ArgumentOutOfRangeException>(() => client.GetAllForUserAsync("fake", 0));
}
}
}
}