Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UsingTestingSequence to enforce simpler & correct usage #475

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
39 changes: 22 additions & 17 deletions MoreLinq.Test/RankTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace MoreLinq.Test
{
using System;
using System.Collections.Generic;
using NUnit.Framework;

/// <summary>
Expand Down Expand Up @@ -34,7 +35,8 @@ public void TestRankByIsLazy()
public void TestRankNullComparer()
{
var sequence = Enumerable.Repeat(1, 10);
sequence.AsTestingSequence().Rank(null).AssertSequenceEqual(sequence);
sequence.UsingTestingSequence(ts => ts.Rank(null))
.AssertSequenceEqual(sequence);
}

/// <summary>
Expand All @@ -44,7 +46,8 @@ public void TestRankNullComparer()
public void TestRankByNullComparer()
{
var sequence = Enumerable.Repeat(1, 10);
sequence.AsTestingSequence().RankBy(x => x, null).AssertSequenceEqual(sequence);
sequence.UsingTestingSequence(ts => ts.RankBy(x => x, null))
.AssertSequenceEqual(sequence);
}

/// <summary>
Expand All @@ -56,11 +59,11 @@ public void TestRankDescendingSequence()
{
const int count = 100;
var sequence = Enumerable.Range(456, count).Reverse();
var result = sequence.AsTestingSequence().Rank().ToArray();
var expectedResult = Enumerable.Range(1, count);

var result = sequence.UsingTestingSequence(ts => ts.Rank());

Assert.AreEqual(count, result.Length);
Assert.IsTrue(result.SequenceEqual(expectedResult));
Assert.IsTrue(result.SequenceEqual(Enumerable.Range(1, count)));
}

/// <summary>
Expand All @@ -72,11 +75,11 @@ public void TestRankByAscendingSeries()
{
const int count = 100;
var sequence = Enumerable.Range(456, count);
var result = sequence.AsTestingSequence().Rank().ToArray();
var expectedResult = Enumerable.Range(1, count).Reverse();

var result = sequence.UsingTestingSequence(ts => ts.Rank());

Assert.AreEqual(count, result.Length);
Assert.IsTrue(result.SequenceEqual(expectedResult));
Assert.IsTrue(result.SequenceEqual(Enumerable.Range(1, count).Reverse()));
}

/// <summary>
Expand All @@ -87,7 +90,7 @@ public void TestRankEquivalentItems()
{
const int count = 100;
var sequence = Enumerable.Repeat(1234, count);
var result = sequence.AsTestingSequence().Rank().ToArray();
var result = sequence.UsingTestingSequence(ts => ts.Rank());

Assert.AreEqual(count, result.Length);
Assert.IsTrue(result.SequenceEqual(Enumerable.Repeat(1, count)));
Expand All @@ -103,7 +106,8 @@ public void TestRankGroupedItems()
var sequence = Enumerable.Range(0, count)
.Concat(Enumerable.Range(0, count))
.Concat(Enumerable.Range(0, count));
var result = sequence.AsTestingSequence().Rank();

var result = sequence.UsingTestingSequence(ts => ts.Rank());

Assert.AreEqual(count, result.Distinct().Count());
Assert.IsTrue(result.SequenceEqual(sequence.Reverse().Select(x => x + 1)));
Expand All @@ -115,11 +119,11 @@ public void TestRankGroupedItems()
[Test]
public void TestRankOfHighestItemIsOne()
{
const int count = 10;
var sequence = Enumerable.Range(1, count);
var result = sequence.AsTestingSequence().Rank();
var result =
Enumerable.Range(1, 10).UsingTestingSequence(ts =>
ts.Rank().OrderBy(x => x).First());

Assert.AreEqual(1, result.OrderBy(x => x).First());
Assert.AreEqual(1, result);
}

/// <summary>
Expand All @@ -139,7 +143,8 @@ public void TestRankByKeySelector()
new { Name = "Jim", Age = 74, ExpectedRank = 1 },
new { Name = "Jes", Age = 11, ExpectedRank = 8 },
};
var result = sequence.AsTestingSequence().RankBy(x => x.Age).ToArray();
var result = sequence.UsingTestingSequence(ts =>
ts.RankBy(x => x.Age));

Assert.AreEqual(sequence.Length, result.Length);
Assert.IsTrue(result.SequenceEqual(sequence.Select(x => x.ExpectedRank)));
Expand All @@ -155,8 +160,8 @@ public void TestRankCustomComparer()
var ordinals = Enumerable.Range(1, count);
var sequence = ordinals.Select( x => new DateTime(2010,x,20-x) );
// invert the CompareTo operation to Rank in reverse order (ascening to descending)
var resultA = sequence.AsTestingSequence().Rank(Comparer.Create<DateTime>((a, b) => -a.CompareTo(b)));
var resultB = sequence.AsTestingSequence().RankBy(x => x.Day, Comparer.Create<int>((a, b) => -a.CompareTo(b)));
var resultA = sequence.UsingTestingSequence(ts => ts.Rank(Comparer.Create<DateTime>((a, b) => -a.CompareTo(b))));
var resultB = sequence.UsingTestingSequence(ts => ts.RankBy(x => x.Day, Comparer.Create<int>((a, b) => -a.CompareTo(b))));

Assert.IsTrue(resultA.SequenceEqual(ordinals));
Assert.IsTrue(resultB.SequenceEqual(ordinals.Reverse()));
Expand Down
15 changes: 15 additions & 0 deletions MoreLinq.Test/TestingSequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,25 @@ static class TestingSequence
internal static TestingSequence<T> Of<T>(params T[] elements) =>
new TestingSequence<T>(elements);

[Obsolete("Use " + nameof(UsingTestingSequence) + " instead.")]
internal static TestingSequence<T> AsTestingSequence<T>(this IEnumerable<T> source) =>
source != null
? new TestingSequence<T>(source)
: throw new ArgumentNullException(nameof(source));

public static TResult UsingTestingSequence<T, TResult>(this IEnumerable<T> source,
Func<TestingSequence<T>, TResult> user)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (user == null) throw new ArgumentNullException(nameof(user));

using (var ts = source.AsTestingSequence())
return user(ts);
}

public static TResult[] UsingTestingSequence<T, TResult>(this IEnumerable<T> source,
Func<TestingSequence<T>, IEnumerable<TResult>> user) =>
source.UsingTestingSequence(ts => user(ts).ToArray());
}

/// <summary>
Expand Down