Skip to content

Commit fee92b9

Browse files
committed
RepeatAsync
1 parent 83bf70b commit fee92b9

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Initial version.
1717
- Add IntersectByAsync
1818
- Add JoinAsync
1919
- Add OrderAsync
20+
- Add RepeatAsync
2021
- Add SkipAsync
2122
- Add SkipLastAsync
2223
- Add SkipWhileAsync
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
namespace AsyncLinqR.Tests;
2+
3+
public class RepeatAsyncTest
4+
{
5+
[Fact]
6+
public async Task RepeatAsync_should_work_as_expected()
7+
{
8+
var result = await AsyncLinq.RepeatAsync("Patin", 2).ToListAsync();
9+
var expected = new List<string> { "Patin", "Patin" };
10+
11+
result.Should().BeEquivalentTo(expected);
12+
}
13+
14+
[Fact]
15+
public async Task RepeatAsync_should_be_empty_when_count_is_zero()
16+
{
17+
Assert.Empty(await AsyncLinq.RepeatAsync("Patin", 0).ToListAsync());
18+
}
19+
20+
[Fact]
21+
public async Task RepeatAsync_should_cancel_with_its_cancellation_token()
22+
{
23+
var token = new CancellationTokenSource();
24+
await token.CancelAsync();
25+
26+
var sut = async () => await AsyncLinq.RepeatAsync("Patin", 2, token.Token).ToListAsync();
27+
await sut.Should().ThrowAsync<OperationCanceledException>();
28+
}
29+
30+
[Fact]
31+
public async Task RepeatAsync_should_receive_cancellation_token_and_cancel()
32+
{
33+
var token = new CancellationTokenSource();
34+
await token.CancelAsync();
35+
36+
var sut = async () => await AsyncLinq.RepeatAsync("Patin", 2, token.Token).ToListAsync(token.Token);
37+
await sut.Should().ThrowAsync<OperationCanceledException>();
38+
}
39+
}

src/AsyncLinqR/RepeatAsync.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace AsyncLinqR;
2+
3+
public static partial class AsyncLinq
4+
{
5+
public static async IAsyncEnumerable<T> RepeatAsync<T>(T element, int count, [EnumeratorCancellation] CancellationToken cancellationToken = default)
6+
{
7+
await Task.Yield();
8+
cancellationToken.ThrowIfCancellationRequested();
9+
while (count-- > 0)
10+
{
11+
cancellationToken.ThrowIfCancellationRequested();
12+
yield return element;
13+
}
14+
}
15+
}

src/AsyncLinqR/_Todo.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
// TODO : Créer les méthodes ci-dessous
22

3-
// RepeatAsync
4-
53
// Nouveautés .Net 9
64
// Enumerable.CountBy, AggregateBy

0 commit comments

Comments
 (0)