-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from Geta/feature/enumerable-extensions
Add content ref enumerable extensions from categories package
- Loading branch information
Showing
3 changed files
with
287 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} | ||
} |
227 changes: 227 additions & 0 deletions
227
test/Geta.Optimizely.Extensions.Tests/Extensions/EnumerableExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |