-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Edward Miller
committed
Apr 22, 2024
1 parent
be17057
commit 1b8f3e5
Showing
1 changed file
with
114 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,114 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Xunit; | ||
|
||
namespace Microsoft.Maui.UnitTests | ||
{ | ||
[Category(TestCategory.Core)] | ||
public partial class EnumerableExtensionTests | ||
{ | ||
public static TheoryData<IEnumerable<int>, int, int> TestData() | ||
{ | ||
var data = new TheoryData<IEnumerable<int>, int, int>(); | ||
|
||
var list = Enumerable.Range(0, 10).ToList(); | ||
var array = Enumerable.Range(0, 10).ToArray(); | ||
var hashSet = Enumerable.Range(0, 10).ToHashSet(); | ||
|
||
var collections = new List<IEnumerable<int>> { list, array, hashSet }; | ||
|
||
foreach (var collection in collections) | ||
{ | ||
data.Add(collection, 99, -1); | ||
data.Add(collection, 2, 2); | ||
} | ||
|
||
return data; | ||
} | ||
|
||
public static TheoryData<IEnumerable<string>, Func<string, int>, IDictionary<int, List<string>>> GroupToDictionaryData() | ||
{ | ||
var data = new TheoryData<IEnumerable<string>, Func<string, int>, IDictionary<int, List<string>>>(); | ||
|
||
var list = new List<string> { "apple", "banana", "cherry", "date", "elderberry" }; | ||
var array = new string[] { "apple", "banana", "cherry", "date", "elderberry" }; | ||
var hashSet = new HashSet<string> { "apple", "banana", "cherry", "date", "elderberry" }; | ||
|
||
var collections = new List<IEnumerable<string>> { list, array, hashSet }; | ||
|
||
Func<string, int> func = s => s.Length; | ||
|
||
foreach (var collection in collections) | ||
{ | ||
var expected = collection.GroupBy(func).ToDictionary(g => g.Key, g => g.ToList()); | ||
data.Add(collection, func, expected); | ||
} | ||
|
||
return data; | ||
} | ||
|
||
[Fact] | ||
public void ForEachDoesLoop() | ||
{ | ||
int count = 0; | ||
|
||
int[] array = Enumerable.Range(0, 10).ToArray(); | ||
|
||
EnumerableExtensions.ForEach(array, i => count++); | ||
|
||
Assert.Equal(count, array.Length); | ||
} | ||
|
||
[Theory] | ||
[Obsolete] | ||
[MemberData(nameof(GroupToDictionaryData))] | ||
public void GroupToDictionaryTest(IEnumerable<string> collection, Func<string, int> func, IDictionary<int, List<string>> expected) | ||
{ | ||
var result = EnumerableExtensions.GroupToDictionary(collection, func); | ||
|
||
Assert.Equal(expected, result); | ||
} | ||
|
||
[Theory] | ||
[Obsolete] | ||
[MemberData(nameof(TestData))] | ||
public void IndexOfPredicateSucceeds(IEnumerable<int> collection, int value, int expectedIndex) | ||
{ | ||
var index = EnumerableExtensions.IndexOf(collection, i => i == value); | ||
|
||
Assert.Equal(expectedIndex, index); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(TestData))] | ||
public void IndexOfGenericSucceeds(IEnumerable<int> collection, int value, int expectedIndex) | ||
{ | ||
var index = EnumerableExtensions.IndexOf(collection, value); | ||
|
||
Assert.Equal(expectedIndex, index); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(TestData))] | ||
public void IndexOfSucceeds(IEnumerable<int> collection, int value, int expectedIndex) | ||
{ | ||
var enumerable = collection as IEnumerable; | ||
|
||
var index = EnumerableExtensions.IndexOf(enumerable, value); | ||
|
||
Assert.Equal(expectedIndex, index); | ||
} | ||
|
||
[Fact] | ||
public void LastSucceeds() | ||
{ | ||
var array = Enumerable.Range(0, 10).ToArray(); | ||
|
||
var index = EnumerableExtensions.Last(array); | ||
|
||
Assert.Equal(10, index); | ||
} | ||
} | ||
} |