forked from ShittyLabs/ShittyLINQ
-
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.
ThenBy extension method and unit test implementation.
- Loading branch information
1 parent
b9c5eca
commit 5d33197
Showing
2 changed files
with
87 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace ShittyLINQ | ||
{ | ||
public static partial class Extensions | ||
{ | ||
public static IOrderedEnumerable<TSource> ThenBy<TSource, TKey>(this IOrderedEnumerable<TSource> source, Func<TSource, TKey> keySelector) | ||
{ | ||
if (source == null) throw new ArgumentNullException("source"); | ||
return source.CreateOrderedEnumerable<TKey>(keySelector, null, false); | ||
} | ||
|
||
public static IOrderedEnumerable<TSource> ThenBy<TSource, TKey>(this IOrderedEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer) | ||
{ | ||
if (source == null) throw new ArgumentNullException("source"); | ||
return source.CreateOrderedEnumerable<TKey>(keySelector, comparer, false); | ||
} | ||
} | ||
} |
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,66 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using ShittyLINQ; | ||
using System; | ||
|
||
namespace ShittyTests | ||
{ | ||
[TestClass] | ||
public class ThenByTests | ||
{ | ||
|
||
[TestMethod] | ||
public void ThenBy_WithoutComparer() | ||
{ | ||
// Arrange | ||
var items = System.Linq.Enumerable.OrderBy(new (string prop1, int prop2)[] | ||
{ | ||
("test", 3), | ||
("test", 2), | ||
("test", 1), | ||
}, i => i.prop1); | ||
|
||
// Act | ||
var result = items.ThenBy(i => i.prop2); | ||
|
||
// Assert | ||
Assert.IsNotNull(result); | ||
using (var enumerator = result.GetEnumerator()) | ||
{ | ||
enumerator.MoveNext(); | ||
Assert.AreEqual(1, enumerator.Current.prop2); | ||
enumerator.MoveNext(); | ||
Assert.AreEqual(2, enumerator.Current.prop2); | ||
enumerator.MoveNext(); | ||
Assert.AreEqual(3, enumerator.Current.prop2); | ||
} | ||
} | ||
|
||
|
||
[TestMethod] | ||
public void ThenBy_WithComparer() | ||
{ | ||
// Arrange | ||
var items = System.Linq.Enumerable.OrderBy(new (string prop1, string prop2)[] | ||
{ | ||
("test", "c"), | ||
("test", "B"), | ||
("test", "a"), | ||
}, i => i.prop1); | ||
|
||
// Act | ||
var result = items.ThenBy(i => i.prop2, StringComparer.OrdinalIgnoreCase); | ||
|
||
// Assert | ||
Assert.IsNotNull(result); | ||
using (var enumerator = result.GetEnumerator()) | ||
{ | ||
enumerator.MoveNext(); | ||
Assert.AreEqual("a", enumerator.Current.prop2); | ||
enumerator.MoveNext(); | ||
Assert.AreEqual("B", enumerator.Current.prop2); | ||
enumerator.MoveNext(); | ||
Assert.AreEqual("c", enumerator.Current.prop2); | ||
} | ||
} | ||
} | ||
} |