Skip to content

Commit

Permalink
add EnumerableTests (#21978)
Browse files Browse the repository at this point in the history
Co-authored-by: Edward Miller <symbiogenisis@outlook.com>
  • Loading branch information
symbiogenesis and Edward Miller committed Apr 22, 2024
1 parent 86e22dd commit 3f9472b
Showing 1 changed file with 114 additions and 0 deletions.
114 changes: 114 additions & 0 deletions src/Core/tests/UnitTests/EnumerableExtensionTests.cs
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(9, index);
}
}
}

0 comments on commit 3f9472b

Please sign in to comment.