mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-07-09 03:04:20 -04:00
Adding tests for enumerables
This commit is contained in:
parent
6763b2c0d1
commit
d6630f29ea
@ -18,13 +18,13 @@ namespace Kyoo
|
||||
/// <param name="mapper">The function that will map each items</param>
|
||||
/// <typeparam name="T">The type of items in <see cref="self"/></typeparam>
|
||||
/// <typeparam name="T2">The type of items in the returned list</typeparam>
|
||||
/// <returns>The list mapped or null if the input map was null.</returns>
|
||||
/// <exception cref="ArgumentNullException">mapper can't be null</exception>
|
||||
public static IEnumerable<T2> Map<T, T2>([CanBeNull] this IEnumerable<T> self,
|
||||
/// <returns>The list mapped.</returns>
|
||||
/// <exception cref="ArgumentNullException">The list or the mapper can't be null</exception>
|
||||
public static IEnumerable<T2> Map<T, T2>([NotNull] this IEnumerable<T> self,
|
||||
[NotNull] Func<T, int, T2> mapper)
|
||||
{
|
||||
if (self == null)
|
||||
return null;
|
||||
throw new ArgumentNullException(nameof(self));
|
||||
if (mapper == null)
|
||||
throw new ArgumentNullException(nameof(mapper));
|
||||
|
||||
@ -46,20 +46,22 @@ namespace Kyoo
|
||||
/// A map where the mapping function is asynchronous.
|
||||
/// Note: <see cref="SelectAsync{T,T2}"/> might interest you.
|
||||
/// </summary>
|
||||
/// <param name="self">The IEnumerable to map. If self is null, an empty list is returned</param>
|
||||
/// <param name="self">The IEnumerable to map.</param>
|
||||
/// <param name="mapper">The asynchronous function that will map each items</param>
|
||||
/// <typeparam name="T">The type of items in <see cref="self"/></typeparam>
|
||||
/// <typeparam name="T2">The type of items in the returned list</typeparam>
|
||||
/// <returns>The list mapped as an AsyncEnumerable</returns>
|
||||
/// <exception cref="ArgumentNullException">mapper can't be null</exception>
|
||||
public static async IAsyncEnumerable<T2> MapAsync<T, T2>([CanBeNull] this IEnumerable<T> self,
|
||||
/// <exception cref="ArgumentNullException">The list or the mapper can't be null</exception>
|
||||
public static IAsyncEnumerable<T2> MapAsync<T, T2>([NotNull] this IEnumerable<T> self,
|
||||
[NotNull] Func<T, int, Task<T2>> mapper)
|
||||
{
|
||||
if (self == null)
|
||||
yield break;
|
||||
throw new ArgumentNullException(nameof(self));
|
||||
if (mapper == null)
|
||||
throw new ArgumentNullException(nameof(mapper));
|
||||
|
||||
static async IAsyncEnumerable<T2> Generator(IEnumerable<T> self, Func<T, int, Task<T2>> mapper)
|
||||
{
|
||||
using IEnumerator<T> enumerator = self.GetEnumerator();
|
||||
int index = 0;
|
||||
|
||||
@ -70,6 +72,9 @@ namespace Kyoo
|
||||
}
|
||||
}
|
||||
|
||||
return Generator(self, mapper);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An asynchronous version of Select.
|
||||
/// </summary>
|
||||
@ -78,21 +83,26 @@ namespace Kyoo
|
||||
/// <typeparam name="T">The type of items in <see cref="self"/></typeparam>
|
||||
/// <typeparam name="T2">The type of items in the returned list</typeparam>
|
||||
/// <returns>The list mapped as an AsyncEnumerable</returns>
|
||||
/// <exception cref="ArgumentNullException">mapper can't be null</exception>
|
||||
public static async IAsyncEnumerable<T2> SelectAsync<T, T2>([CanBeNull] this IEnumerable<T> self,
|
||||
/// <exception cref="ArgumentNullException">The list or the mapper can't be null</exception>
|
||||
public static IAsyncEnumerable<T2> SelectAsync<T, T2>([NotNull] this IEnumerable<T> self,
|
||||
[NotNull] Func<T, Task<T2>> mapper)
|
||||
{
|
||||
if (self == null)
|
||||
yield break;
|
||||
throw new ArgumentNullException(nameof(self));
|
||||
if (mapper == null)
|
||||
throw new ArgumentNullException(nameof(mapper));
|
||||
|
||||
static async IAsyncEnumerable<T2> Generator(IEnumerable<T> self, Func<T, Task<T2>> mapper)
|
||||
{
|
||||
using IEnumerator<T> enumerator = self.GetEnumerator();
|
||||
|
||||
while (enumerator.MoveNext())
|
||||
yield return await mapper(enumerator.Current);
|
||||
}
|
||||
|
||||
return Generator(self, mapper);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert an AsyncEnumerable to a List by waiting for every item.
|
||||
/// </summary>
|
||||
@ -100,26 +110,38 @@ namespace Kyoo
|
||||
/// <typeparam name="T">The type of items in the async list and in the returned list.</typeparam>
|
||||
/// <returns>A task that will return a simple list</returns>
|
||||
/// <exception cref="ArgumentNullException">The list can't be null</exception>
|
||||
public static async Task<List<T>> ToListAsync<T>([NotNull] this IAsyncEnumerable<T> self)
|
||||
public static Task<List<T>> ToListAsync<T>([NotNull] this IAsyncEnumerable<T> self)
|
||||
{
|
||||
if (self == null)
|
||||
throw new ArgumentNullException(nameof(self));
|
||||
|
||||
static async Task<List<T>> ToList(IAsyncEnumerable<T> self)
|
||||
{
|
||||
List<T> ret = new();
|
||||
|
||||
await foreach (T i in self)
|
||||
ret.Add(i);
|
||||
return ret;
|
||||
}
|
||||
|
||||
return ToList(self);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// If the enumerable is empty, execute an action.
|
||||
/// </summary>
|
||||
/// <param name="self">The enumerable to check</param>
|
||||
/// <param name="action">The action to execute is the list is empty</param>
|
||||
/// <typeparam name="T">The type of items inside the list</typeparam>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<T> IfEmpty<T>(this IEnumerable<T> self, Action action)
|
||||
/// <exception cref="ArgumentNullException">The iterable and the action can't be null.</exception>
|
||||
/// <returns>The iterator proxied, there is no dual iterations.</returns>
|
||||
public static IEnumerable<T> IfEmpty<T>([NotNull] this IEnumerable<T> self, [NotNull] Action action)
|
||||
{
|
||||
if (self == null)
|
||||
throw new ArgumentNullException(nameof(self));
|
||||
if (action == null)
|
||||
throw new ArgumentNullException(nameof(action));
|
||||
|
||||
static IEnumerable<T> Generator(IEnumerable<T> self, Action action)
|
||||
{
|
||||
using IEnumerator<T> enumerator = self.GetEnumerator();
|
||||
|
||||
@ -136,6 +158,9 @@ namespace Kyoo
|
||||
while (enumerator.MoveNext());
|
||||
}
|
||||
|
||||
return Generator(self, action);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A foreach used as a function with a little specificity: the list can be null.
|
||||
/// </summary>
|
||||
|
@ -1,14 +1,41 @@
|
||||
using System.Reflection;
|
||||
using Xunit;
|
||||
using Xunit.Sdk;
|
||||
|
||||
namespace Kyoo.Tests
|
||||
{
|
||||
/// <summary>
|
||||
/// Custom assertions used by Kyoo's tests.
|
||||
/// </summary>
|
||||
public static class KAssert
|
||||
{
|
||||
/// <summary>
|
||||
/// Check if every property of the item is equal to the other's object.
|
||||
/// </summary>
|
||||
/// <param name="expected">The value to check against</param>
|
||||
/// <param name="value">The value to check</param>
|
||||
/// <typeparam name="T">The type to check</typeparam>
|
||||
public static void DeepEqual<T>(T expected, T value)
|
||||
{
|
||||
foreach (PropertyInfo property in typeof(T).GetProperties())
|
||||
Assert.Equal(property.GetValue(expected), property.GetValue(value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Explicitly fail a test.
|
||||
/// </summary>
|
||||
public static void Fail()
|
||||
{
|
||||
throw new XunitException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Explicitly fail a test.
|
||||
/// </summary>
|
||||
/// <param name="message">The message that will be seen in the test report</param>
|
||||
public static void Fail(string message)
|
||||
{
|
||||
throw new XunitException(message);
|
||||
}
|
||||
}
|
||||
}
|
71
Kyoo.Tests/Utility/EnumerableTests.cs
Normal file
71
Kyoo.Tests/Utility/EnumerableTests.cs
Normal file
@ -0,0 +1,71 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace Kyoo.Tests
|
||||
{
|
||||
public class EnumerableTests
|
||||
{
|
||||
[Fact]
|
||||
public void MapTest()
|
||||
{
|
||||
int[] list = {1, 2, 3, 4};
|
||||
Assert.All(list.Map((x, i) => (x, i)), x => Assert.Equal(x.x - 1, x.i));
|
||||
Assert.Throws<ArgumentNullException>(() => list.Map(((Func<int, int, int>)null)!));
|
||||
list = null;
|
||||
Assert.Throws<ArgumentNullException>(() => list!.Map((x, _) => x + 1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task MapAsyncTest()
|
||||
{
|
||||
int[] list = {1, 2, 3, 4};
|
||||
await foreach((int x, int i) in list.MapAsync((x, i) => Task.FromResult((x, i))))
|
||||
{
|
||||
Assert.Equal(x - 1, i);
|
||||
}
|
||||
Assert.Throws<ArgumentNullException>(() => list.MapAsync(((Func<int, int, Task<int>>)null)!));
|
||||
list = null;
|
||||
Assert.Throws<ArgumentNullException>(() => list!.MapAsync((x, _) => Task.FromResult(x + 1)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SelectAsyncTest()
|
||||
{
|
||||
int[] list = {1, 2, 3, 4};
|
||||
int i = 2;
|
||||
await foreach(int x in list.SelectAsync(x => Task.FromResult(x + 1)))
|
||||
{
|
||||
Assert.Equal(i++, x);
|
||||
}
|
||||
Assert.Throws<ArgumentNullException>(() => list.SelectAsync(((Func<int, Task<int>>)null)!));
|
||||
list = null;
|
||||
Assert.Throws<ArgumentNullException>(() => list!.SelectAsync(x => Task.FromResult(x + 1)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ToListAsyncTest()
|
||||
{
|
||||
int[] expected = {1, 2, 3, 4};
|
||||
IAsyncEnumerable<int> list = expected.SelectAsync(Task.FromResult);
|
||||
Assert.Equal(expected, await list.ToListAsync());
|
||||
list = null;
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(() => list!.ToListAsync());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IfEmptyTest()
|
||||
{
|
||||
int[] list = {1, 2, 3, 4};
|
||||
list = list.IfEmpty(() => KAssert.Fail("Empty action should not be triggered.")).ToArray();
|
||||
Assert.Throws<ArgumentNullException>(() => list.IfEmpty(null!).ToList());
|
||||
list = null;
|
||||
Assert.Throws<ArgumentNullException>(() => list!.IfEmpty(() => {}).ToList());
|
||||
list = Array.Empty<int>();
|
||||
Assert.Throws<ArgumentException>(() => list.IfEmpty(() => throw new ArgumentException()).ToList());
|
||||
Assert.Empty(list.IfEmpty(() => {}));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user