From 7b290ad595dab3c479bd07e6a5bb0cc913acb075 Mon Sep 17 00:00:00 2001 From: Edward Miller Date: Mon, 22 Apr 2024 11:00:47 -0500 Subject: [PATCH] add EnumerableTests --- .../UnitTests/EnumerableExtensionTests.cs | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 src/Core/tests/UnitTests/EnumerableExtensionTests.cs diff --git a/src/Core/tests/UnitTests/EnumerableExtensionTests.cs b/src/Core/tests/UnitTests/EnumerableExtensionTests.cs new file mode 100644 index 000000000000..7c2e8ab8d333 --- /dev/null +++ b/src/Core/tests/UnitTests/EnumerableExtensionTests.cs @@ -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, int, int> TestData() + { + var data = new TheoryData, 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> { list, array, hashSet }; + + foreach (var collection in collections) + { + data.Add(collection, 99, -1); + data.Add(collection, 2, 2); + } + + return data; + } + + public static TheoryData, Func, IDictionary>> GroupToDictionaryData() + { + var data = new TheoryData, Func, IDictionary>>(); + + var list = new List { "apple", "banana", "cherry", "date", "elderberry" }; + var array = new string[] { "apple", "banana", "cherry", "date", "elderberry" }; + var hashSet = new HashSet { "apple", "banana", "cherry", "date", "elderberry" }; + + var collections = new List> { list, array, hashSet }; + + Func 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 collection, Func func, IDictionary> expected) + { + var result = EnumerableExtensions.GroupToDictionary(collection, func); + + Assert.Equal(expected, result); + } + + [Theory] + [Obsolete] + [MemberData(nameof(TestData))] + public void IndexOfPredicateSucceeds(IEnumerable 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 collection, int value, int expectedIndex) + { + var index = EnumerableExtensions.IndexOf(collection, value); + + Assert.Equal(expectedIndex, index); + } + + [Theory] + [MemberData(nameof(TestData))] + public void IndexOfSucceeds(IEnumerable 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); + } + } +} \ No newline at end of file