Skip to content

Commit

Permalink
Merge pull request #22 from Geta/feature/enumerable-extensions
Browse files Browse the repository at this point in the history
Add content ref enumerable extensions from categories package
  • Loading branch information
brianweet committed Jul 1, 2022
2 parents cfbbe31 + b55b699 commit 85ed604
Show file tree
Hide file tree
Showing 3 changed files with 287 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,17 @@ If you need to check if the CategoryList has that category you can use the Conta

bool hasBikes = CurrentPage.Category.Contains("bikes");

### Enumerable extensions

You can easily check if content references are part of a list using `MemberOf`, `MemberOfAny` or `MemberOfAll`, for example to check if a page has any of the supplied categories:

```
IEnumerable<ContentReference> categories = ...;
var pagesWithCat = _contentLoader
.GetChildren<ICategorizableContent>(contentLink, loaderOptions)
.Where(x => x.Categories.MemberOfAny(categories));
```

### External/friendly URL

This can be useful when used together with sharing widgets.
Expand Down
49 changes: 49 additions & 0 deletions src/Geta.Optimizely.Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Collections.Generic;
using System.Linq;
using EPiServer.Core;

namespace Geta.Optimizely.Extensions
{
public static class EnumerableExtensions
{
public static bool MemberOf(this IEnumerable<ContentReference> contentLinks, ContentReference contentReference)
{
if (contentLinks == null)
{
return false;
}

return contentLinks.Any(x => x.CompareToIgnoreWorkID(contentReference));
}

public static bool MemberOfAny(this IEnumerable<ContentReference> contentLinks, IEnumerable<ContentReference> otherContentLinks)
{
if (otherContentLinks == null || !otherContentLinks.Any())
{
return true;
}

if (contentLinks == null)
{
return false;
}

return otherContentLinks.Any(x => contentLinks.Any(y => y.CompareToIgnoreWorkID(x)));
}

public static bool MemberOfAll(this IEnumerable<ContentReference> contentLinks, IEnumerable<ContentReference> otherContentLinks)
{
if (otherContentLinks == null || !otherContentLinks.Any())
{
return true;
}

if (contentLinks == null)
{
return false;
}

return otherContentLinks.All(x => contentLinks.Any(y => y.CompareToIgnoreWorkID(x)));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
using System;
using System.Collections.Generic;
using System.Linq;
using EPiServer.Core;
using Xunit;

namespace Geta.Optimizely.Extensions.Tests.Extensions
{
public class EnumerableExtensionsTests
{
[Fact]
public void MemberOf_SourceListIsNull_ThenReturnFalse()
{
IEnumerable<ContentReference> sourceList = null;

var result = sourceList.MemberOf(new ContentReference(1));

Assert.False(result);
}

[Fact]
public void MemberOf_SourceListDoesNotContainRef_ThenReturnFalse()
{
IEnumerable<ContentReference> sourceList = new List<ContentReference>() {
new ContentReference(1),
new ContentReference(2)
};

var result = sourceList.MemberOf(new ContentReference(3));

Assert.False(result);
}

[Fact]
public void MemberOf_SourceListContainsRef_ThenReturnTrue()
{
IEnumerable<ContentReference> sourceList = new List<ContentReference>() {
new ContentReference(1),
new ContentReference(2)
};

var result = sourceList.MemberOf(new ContentReference(2));

Assert.True(result);
}

[Fact]
public void MemberOf_SourceListContainsRefWithDifferentWorkingVersion_ThenReturnTrue()
{
IEnumerable<ContentReference> sourceList = new List<ContentReference>() {
new ContentReference(1),
new ContentReference(2, 3)
};

var result = sourceList.MemberOf(new ContentReference(2, 2));

Assert.True(result);
}

[Fact]
public void MemberOfAny_TargetListIsNull_ThenReturnTrue()
{
IEnumerable<ContentReference> sourceList = new List<ContentReference>() {
new ContentReference(1),
new ContentReference(2)
};
IEnumerable<ContentReference> targetList = null;

var result = sourceList.MemberOfAny(targetList);

Assert.True(result);
}

[Fact]
public void MemberOfAny_TargetListIsEmpty_ThenReturnTrue()
{
IEnumerable<ContentReference> sourceList = new List<ContentReference>() {
new ContentReference(1),
new ContentReference(2)
};
IEnumerable<ContentReference> targetList = new List<ContentReference>() {};

var result = sourceList.MemberOfAny(targetList);

Assert.True(result);
}

[Fact]
public void MemberOfAny_SourceListIsNull_ThenReturnFalse()
{
IEnumerable<ContentReference> sourceList = null;
IEnumerable<ContentReference> targetList = new List<ContentReference>() { new ContentReference(2) };

var result = sourceList.MemberOfAny(targetList);

Assert.False(result);
}

[Fact]
public void MemberOfAny_SourceListContainsRefWithDifferentWorkingVersion_ThenReturnTrue()
{
IEnumerable<ContentReference> sourceList = new List<ContentReference>() {
new ContentReference(1),
new ContentReference(2, 3)
};
IEnumerable<ContentReference> targetList = new List<ContentReference>() { new ContentReference(2) };

var result = sourceList.MemberOfAny(targetList);

Assert.True(result);
}

[Fact]
public void MemberOfAny_SourceListContainsAnyMatches_ThenReturnTrue()
{
IEnumerable<ContentReference> sourceList = new List<ContentReference>() {
new ContentReference(1),
new ContentReference(2)
};
IEnumerable<ContentReference> targetList = new List<ContentReference>() {
new ContentReference(2),
new ContentReference(3)
};

var result = sourceList.MemberOfAny(targetList);

Assert.True(result);
}

[Fact]
public void MemberOfAny_SourceListDoesNotContainAnyMatches_ThenReturnFalse()
{
IEnumerable<ContentReference> sourceList = new List<ContentReference>() {
new ContentReference(1),
new ContentReference(2)
};
IEnumerable<ContentReference> targetList = new List<ContentReference>() {
new ContentReference(3),
new ContentReference(4)
};

var result = sourceList.MemberOfAny(targetList);

Assert.False(result);
}

[Fact]
public void MemberOfAll_TargetListIsNull_ThenReturnTrue()
{
IEnumerable<ContentReference> sourceList = new List<ContentReference>() {
new ContentReference(1),
new ContentReference(2)
};
IEnumerable<ContentReference> targetList = null;

var result = sourceList.MemberOfAll(targetList);

Assert.True(result);
}

[Fact]
public void MemberOfAll_TargetListIsEmpty_ThenReturnTrue()
{
IEnumerable<ContentReference> sourceList = new List<ContentReference>() {
new ContentReference(1),
new ContentReference(2)
};
IEnumerable<ContentReference> targetList = new List<ContentReference>() { };

var result = sourceList.MemberOfAll(targetList);

Assert.True(result);
}

[Fact]
public void MemberOfAll_SourceListIsNull_ThenReturnFalse()
{
IEnumerable<ContentReference> sourceList = null;
IEnumerable<ContentReference> targetList = new List<ContentReference>() { new ContentReference(2) };

var result = sourceList.MemberOfAll(targetList);

Assert.False(result);
}

[Fact]
public void MemberOfAll_SourceListContainsRefWithDifferentWorkingVersion_ThenReturnTrue()
{
IEnumerable<ContentReference> sourceList = new List<ContentReference>() {
new ContentReference(1),
new ContentReference(2, 3)
};
IEnumerable<ContentReference> targetList = new List<ContentReference>() { new ContentReference(2) };

var result = sourceList.MemberOfAll(targetList);

Assert.True(result);
}

[Theory]
[InlineData(new[] { 1, 2 }, new[] { 3 })]
[InlineData(new[] { 1, 2 }, new[] { 1, 2, 3 })]
public void xMemberOfAll_SourceListDoesNotContainAllTargets_ThenReturnFalse(int[] source, int[] target)
{
var sourceList = source.Select(x => new ContentReference(x));
var targetList = target.Select(x => new ContentReference(x));

var result = sourceList.MemberOfAll(targetList);

Assert.False(result);
}

[Theory]
[InlineData(new[] { 1 }, new[] { 1 })]
[InlineData(new[] { 1, 2 }, new[] { 1, 2 })]
[InlineData(new[] { 1, 2 }, new[] { 1 })]
public void xMemberOfAll_SourceListIsNull_ThenReturnTrue(int[] source, int[] target)
{
var sourceList = source.Select(x => new ContentReference(x));
var targetList = target.Select(x => new ContentReference(x));

var result = sourceList.MemberOfAll(targetList);

Assert.True(result);
}
}
}

0 comments on commit 85ed604

Please sign in to comment.