Skip to content

Commit

Permalink
Update Materialize tests to account for new optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
bash committed Nov 20, 2023
1 parent d651c4a commit 74385e6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
6 changes: 3 additions & 3 deletions Funcky.Test/Extensions/EnumerableExtensions/AnyOrElseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ public void IsEmptyWhenBothEnumerablesAreEmpty()
[Fact]
public void IsSourceEnumerableWhenNonEmpty()
{
var source = Sequence.Return(1, 2, 3).EraseNonEnumeratedCount();
var source = Sequence.Return(1, 2, 3).PreventLinqOptimizations();
var fallback = Sequence.Return(4, 5, 6);
Assert.Equal(source, source.AnyOrElse(fallback));
}

[Fact]
public void IsFallbackEnumerableWhenSourceIsEmpty()
{
var source = Enumerable.Empty<int>().EraseNonEnumeratedCount();
var source = Enumerable.Empty<int>().PreventLinqOptimizations();
var fallback = Sequence.Return(1, 2, 3);
Assert.Equal(fallback, source.AnyOrElse(fallback));
}
Expand All @@ -36,7 +36,7 @@ public void SourceIsEnumeratedLazily()
[Fact]
public void FallbackIsEnumeratedLazily()
{
var source = Enumerable.Empty<int>().EraseNonEnumeratedCount();
var source = Enumerable.Empty<int>().PreventLinqOptimizations();
_ = source.AnyOrElse(new FailOnEnumerationSequence<int>());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void GetNonEnumeratedCountOrNoneReturnsCountOnEnumerableRange()
[Property]
public Property GetNonEnumeratedCountOrNoneReturnsNoneForInstancesWithoutCount(List<int> list)
{
return list.EraseNonEnumeratedCount().GetNonEnumeratedCountOrNone()
return list.PreventLinqOptimizations().GetNonEnumeratedCountOrNone()
.Match(none: true, some: False)
.ToProperty();
}
Expand Down
15 changes: 13 additions & 2 deletions Funcky.Test/Extensions/EnumerableExtensions/MaterializeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,30 @@ public void MaterializeDoesNotEnumerateCollectionTypes()
[Fact]
public void MaterializeReturnsImmutableCollectionWhenEnumerated()
{
var sequence = Enumerable.Repeat("Hello world!", 3);
var sequence = Enumerable.Repeat("Hello world!", 3).PreventLinqOptimizations();

Assert.IsType<ImmutableList<string>>(sequence.Materialize());
}

[Fact]
public void MaterializeWithMaterializationReturnsCorrectCollectionWhenEnumerate()
{
var sequence = Enumerable.Repeat("Hello world!", 3);
var sequence = Enumerable.Repeat("Hello world!", 3).PreventLinqOptimizations();

Assert.IsType<HashSet<string>>(sequence.Materialize(ToHashSet));
}

#if NET8_0_OR_GREATER
// This is an optimization added in .NET 8
[Fact]
public void MaterializeDoesNotEnumerableEnumerableReturnedByRepeat()
{
var sequence = Enumerable.Repeat("Hello world!", 3);
var materialized = sequence.Materialize<string, IReadOnlyCollection<string>>(_ => throw new FailException("Materialization should never be called"));
Assert.Same(sequence, materialized);
}
#endif

[Fact]
public void MaterializeDoesNotEnumerateCollectionWhichImplementsICollectionOnly()
{
Expand Down
5 changes: 2 additions & 3 deletions Funcky.Test/TestUtils/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ namespace Funcky.Test.TestUtils;

internal static class EnumerableExtensions
{
internal static IEnumerable<TItem> EraseNonEnumeratedCount<TItem>(this IEnumerable<TItem> source)
/// <summary>Prevents LINQ from optimizing by hiding the underlying source enumerable.</summary>
internal static IEnumerable<TItem> PreventLinqOptimizations<TItem>(this IEnumerable<TItem> source)
{
// Having our own state machine erases the non enumerated count
// provided when using LINQ methods such as Select.
foreach (var element in source)
{
yield return element;
Expand Down

0 comments on commit 74385e6

Please sign in to comment.