Skip to content

Commit

Permalink
refactor: Use IReadOnlyList from the start
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Feb 19, 2024
1 parent 30f187f commit 96cb0d8
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 13 deletions.
12 changes: 4 additions & 8 deletions src/LinkDotNet.Blog.Infrastructure/PagedList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace LinkDotNet.Blog.Infrastructure;

Expand All @@ -14,16 +13,13 @@ public sealed class PagedList<T> : IPagedList<T>
private readonly IReadOnlyList<T> subset;
private readonly int totalPages;

public PagedList(IEnumerable<T> items, int pageNumber, int pageSize)
: this(items, items.Count(), pageNumber, pageSize)
public PagedList(IReadOnlyList<T> items, int totalCount, int pageNumber, int pageSize)
{
}
ArgumentNullException.ThrowIfNull(items);

public PagedList(IEnumerable<T> items, int count, int pageNumber, int pageSize)
{
PageNumber = pageNumber;
totalPages = (int)Math.Ceiling(count / (double)pageSize);
subset = items as IReadOnlyList<T> ?? items.ToArray();
totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
subset = items;
}

public int PageNumber { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private static void SetupGetAll(
Arg.Any<bool>(),
Arg.Any<int>(),
Arg.Any<int>())
.Returns(new PagedList<ProfileInformationEntry>(entries, 1, 100));
.Returns(new PagedList<ProfileInformationEntry>(entries, entries.Length, 1, 100));
}

private (IRepository<ProfileInformationEntry> repoMock, ISortOrderCalculator calcMock) RegisterServices()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,6 @@ private void SetupRepository()
Arg.Any<Expression<Func<BlogPost, object>>>(),
Arg.Any<bool>(),
Arg.Any<int>(),
Arg.Any<int>()).Returns(new PagedList<BlogPost>(new[] { blogPost }, 1, 1));
Arg.Any<int>()).Returns(new PagedList<BlogPost>([blogPost], 1, 1, 1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static void RegisterRepositoryWithEmptyReturn<TEntity>(this IServiceColle
Arg.Any<bool>(),
Arg.Any<int>(),
Arg.Any<int>())
.Returns(new PagedList<TEntity>(Array.Empty<TEntity>(), 1, 1));
.Returns(new PagedList<TEntity>([], 0, 1, 1));

collection.AddScoped(_ => repositoryMock);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,13 @@ public async Task ShouldCreateSitemap()
.WithTags("tag2")
.Build();
bp2.Id = "id2";
var blogPosts = new[] { bp1, bp2 };
repositoryMock.GetAllAsync(
Arg.Any<Expression<Func<BlogPost, bool>>>(),
Arg.Any<Expression<Func<BlogPost, object>>>(),
true,
Arg.Any<int>(),
Arg.Any<int>())
.Returns(new PagedList<BlogPost>(blogPosts, 1, 10));
.Returns(new PagedList<BlogPost>([bp1, bp2], 2, 1, 10));

var sitemap = await sut.CreateSitemapAsync();

Expand Down

0 comments on commit 96cb0d8

Please sign in to comment.