Skip to content

Commit b72a970

Browse files
committed
Premières méthodes de tests pour valider que l'inférence de type fonctionne bien à la compilation et que la valeur de retour est comme attendue
Ajout de méthodes dans SelectManyAsync
1 parent bff1976 commit b72a970

2 files changed

Lines changed: 114 additions & 0 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System.Linq;
2+
3+
namespace AsyncLinqR.Tests;
4+
5+
public class SelectManyAsyncTest
6+
{
7+
// TODO Renommer tous les tests pour donner des noms parlants
8+
[Fact]
9+
public async Task Suite()
10+
{
11+
var enumerable = await Fake<IEnumerable<int>>.Create([]).SelectManyAsync(x => x.List).ToListAsync();
12+
enumerable.Should().BeOfType<List<int>>();
13+
14+
var array = await Fake<int[]>.Create([]).SelectManyAsync(x => x.List).ToListAsync();
15+
array.Should().BeOfType<List<int>>();
16+
17+
// TODO Rajouter List<int>
18+
}
19+
20+
[Fact]
21+
public async Task SelectAsync_patin_couffin()
22+
{
23+
var enumerable = await Fake<IEnumerable<int>>.CreateAsync([]).SelectManyAsync(x => x.List).ToListAsync();
24+
enumerable.Should().BeOfType<List<int>>();
25+
26+
var array = await Fake<int[]>.CreateAsync([]).SelectManyAsync(x => x.List).ToListAsync();
27+
array.Should().BeOfType<List<int>>();
28+
29+
// TODO Rajouter List<int>
30+
}
31+
32+
[Fact]
33+
public async Task Selector_and_async_changer_ces_noms_pourris()
34+
{
35+
var enumerable = await Fake<IEnumerable<int>>.CreateAsync([]).SelectManyAsync(x => x.List, (y, i) => i).ToListAsync();
36+
enumerable.Should().BeOfType<List<int>>();
37+
38+
// TODO Rajouter int[]
39+
// TODO Rajouter List<int>
40+
}
41+
42+
[Fact]
43+
public async Task Selector_and_sync_changer_ces_noms_pourris()
44+
{
45+
var enumerable = await Fake<IEnumerable<int>>.Create([]).SelectManyAsync(x => x.List, (y, i) => i).ToListAsync();
46+
enumerable.Should().BeOfType<List<int>>();
47+
48+
// TODO Rajouter int[]
49+
// TODO Rajouter List<int>
50+
}
51+
52+
[Fact]
53+
public async Task Selector_and_async_changer_ces_noms_pourris_toto()
54+
{
55+
var enumerable = await Fake<IEnumerable<int>>.CreateAsync([]).SelectManyAsync(x => x.List, (y, i) => Task<int>.Factory.StartNew(() => i)).ToListAsync();
56+
enumerable.Should().BeOfType<List<int>>();
57+
58+
// TODO Rajouter int[]
59+
// TODO Rajouter List<int>
60+
}
61+
62+
[Fact]
63+
public async Task Selector_and_sync_changer_ces_noms_pourris_toto()
64+
{
65+
var enumerable = await Fake<IEnumerable<int>>.Create([]).SelectManyAsync(x => x.List, (y, i) => Task<int>.Factory.StartNew(() => i)).ToListAsync();
66+
enumerable.Should().BeOfType<List<int>>();
67+
68+
// TODO Rajouter int[]
69+
// TODO Rajouter List<int>
70+
}
71+
}
72+
73+
record Fake<T>(int Value, Task<T> List)
74+
{
75+
public static IEnumerable<Fake<T>> Create(T list)
76+
{
77+
return Enumerable.Range(0, 10).Select(x => new Fake<T>(x, Task<T>.Factory.StartNew(() => list)));
78+
}
79+
80+
public static IAsyncEnumerable<Fake<T>> CreateAsync(T list)
81+
{
82+
return AsyncLinq.RangeAsync(10).SelectAsync(x => new Fake<T>(x, Task<T>.Factory.StartNew(() => list)));
83+
}
84+
}

src/AsyncLinqR/SelectManyAsync.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ public static async IAsyncEnumerable<TResult> SelectManyAsync<T, TResult>(this I
4848
}
4949
}
5050

51+
public static async IAsyncEnumerable<TResult> SelectManyAsync<T, TResult>(this IAsyncEnumerable<T> source, Func<T, Task<TResult[]>> selector, [EnumeratorCancellation] CancellationToken cancellationToken = default)
52+
{
53+
await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
54+
foreach (var result in await selector(item).ConfigureAwait(false))
55+
{
56+
cancellationToken.ThrowIfCancellationRequested();
57+
yield return result;
58+
}
59+
}
60+
5161
public static async IAsyncEnumerable<TResult> SelectManyAsync<T, TResult>(this IEnumerable<T> source, Func<T, IAsyncEnumerable<TResult>> selector, [EnumeratorCancellation] CancellationToken cancellationToken = default)
5262
{
5363
cancellationToken.ThrowIfCancellationRequested();
@@ -73,6 +83,16 @@ public static async IAsyncEnumerable<TResult> SelectManyAsync<T, TCollection, TR
7383
}
7484
}
7585

86+
public static async IAsyncEnumerable<TResult> SelectManyAsync<T, TCollection, TResult>(this IAsyncEnumerable<T> source, Func<T, Task<IEnumerable<TCollection>>> collectionSelector, Func<T, TCollection, TResult> resultSelector, [EnumeratorCancellation] CancellationToken cancellationToken = default)
87+
{
88+
await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
89+
foreach (var result in await collectionSelector(item).ConfigureAwait(false))
90+
{
91+
cancellationToken.ThrowIfCancellationRequested();
92+
yield return resultSelector(item, result);
93+
}
94+
}
95+
7696
public static async IAsyncEnumerable<TResult> SelectManyAsync<T, TCollection, TResult>(this IEnumerable<T> source, Func<T, Task<IEnumerable<TCollection>>> collectionSelector, Func<T, TCollection, TResult> resultSelector, [EnumeratorCancellation] CancellationToken cancellationToken = default)
7797
{
7898
cancellationToken.ThrowIfCancellationRequested();
@@ -120,6 +140,16 @@ public static async IAsyncEnumerable<TResult> SelectManyAsync<T, TCollection, TR
120140
}
121141
}
122142

143+
public static async IAsyncEnumerable<TResult> SelectManyAsync<T, TCollection, TResult>(this IAsyncEnumerable<T> source, Func<T, Task<IEnumerable<TCollection>>> collectionSelector, Func<T, TCollection, Task<TResult>> resultSelector, [EnumeratorCancellation] CancellationToken cancellationToken = default)
144+
{
145+
await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
146+
foreach (var result in await collectionSelector(item).ConfigureAwait(false))
147+
{
148+
cancellationToken.ThrowIfCancellationRequested();
149+
yield return await resultSelector(item, result).ConfigureAwait(false);
150+
}
151+
}
152+
123153
public static async IAsyncEnumerable<TResult> SelectManyAsync<T, TCollection, TResult>(this IEnumerable<T> source, Func<T, Task<IEnumerable<TCollection>>> collectionSelector, Func<T, TCollection, Task<TResult>> resultSelector, [EnumeratorCancellation] CancellationToken cancellationToken = default)
124154
{
125155
cancellationToken.ThrowIfCancellationRequested();

0 commit comments

Comments
 (0)