Skip to content

Commit

Permalink
resolves #163 🔨🔬✨
Browse files Browse the repository at this point in the history
  • Loading branch information
BryanWilhite committed Jan 29, 2024
1 parent a4110c2 commit 437cde2
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Songhay.Models;

namespace Songhay.Tests.Extensions;

public class MenuDisplayItemModelExtensionsTests
{
// ReSharper disable once InconsistentNaming
public static IEnumerable<object[]> GetAllByData =
new[]
{
new object[]
{
new MenuDisplayItemModel
{
GroupId = "g-42",
ChildItems =
new []
{
new MenuDisplayItemModel(),
new MenuDisplayItemModel { GroupId = "g-42" },
new MenuDisplayItemModel
{
ChildItems = new []
{
new MenuDisplayItemModel { GroupId = "g-42" },
}
}
}
},
(MenuDisplayItemModel i) => i.HasGroupId("g-42"), // predicate
3 // expectedNumberOfItems
}
};

[Theory]
[MemberData(nameof(GetAllByData))]
public void GetAllBy_Test(MenuDisplayItemModel data, Func<MenuDisplayItemModel, bool> predicate, int expectedNumberOfItems)
{
var actual = data.GetAllBy(predicate).ToArray();
Assert.NotEmpty(actual);
Assert.Equal(expectedNumberOfItems, actual.Length);
}
}
27 changes: 24 additions & 3 deletions SonghayCore/Extensions/MenuDisplayItemModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,34 @@
/// </summary>
public static class MenuDisplayItemModelExtensions
{
/// <summary>
/// Gets all <see cref="MenuDisplayItemModel"/>
/// by the specified predicate.
/// </summary>
/// <param name="data">the <see cref="MenuDisplayItemModel"/></param>
/// <param name="predicate">the predicate</param>
public static IEnumerable<MenuDisplayItemModel> GetAllBy(this MenuDisplayItemModel? data,
Func<MenuDisplayItemModel, bool> predicate)
{
var items1 = data != null ?
new[] {data}.Where(predicate)
:
Enumerable.Empty<MenuDisplayItemModel>();
var items2 = data?
.ChildItems.SelectMany(i => i.GetAllBy(predicate))
??
Enumerable.Empty<MenuDisplayItemModel>();

return items1.Union(items2);
}

/// <summary>
/// Returns <c>true</c> when the grouping has the specified identifier.
/// </summary>
/// <param name="data"></param>
/// <param name="groupId"></param>
/// <param name="data">the <see cref="MenuDisplayItemModel"/></param>
/// <param name="groupId">the <see cref="MenuDisplayItemModel.GroupId"/></param>
public static bool HasGroupId(this MenuDisplayItemModel? data, string groupId) => data != null &&
(!string.IsNullOrWhiteSpace(groupId) && data.GroupId.EqualsInvariant(groupId));
!string.IsNullOrWhiteSpace(groupId) && data.GroupId.EqualsInvariant(groupId);

/// <summary>
/// Returns the Default Selection
Expand Down
12 changes: 10 additions & 2 deletions SonghayCore/SonghayCore.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 437cde2

Please sign in to comment.