-
Notifications
You must be signed in to change notification settings - Fork 792
Expand file tree
/
Copy pathAsyncEnumerableTests.cs
More file actions
83 lines (68 loc) · 2.57 KB
/
Copy pathAsyncEnumerableTests.cs
File metadata and controls
83 lines (68 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Xunit;
namespace Tests
{
public class AsyncEnumerableTests
{
protected static readonly IAsyncEnumerable<int> Return42 = new[] { 42 }.ToAsyncEnumerable();
protected async Task<TException> AssertThrowsAsync<TException>(Task t)
where TException : Exception
{
return await Assert.ThrowsAsync<TException>(() => t);
}
protected async Task AssertThrowsAsync(Task t, Exception e)
{
var ex = await Assert.ThrowsAnyAsync<Exception>(() => t);
Assert.Same(e, ex);
}
protected Task AssertThrowsAsync<T>(ValueTask<T> t, Exception e)
{
return AssertThrowsAsync(t.AsTask(), e);
}
protected async Task NoNextAsync<T>(IAsyncEnumerator<T> e)
{
Assert.False(await e.MoveNextAsync());
}
protected async Task HasNextAsync<T>(IAsyncEnumerator<T> e, T value)
{
Assert.True(await e.MoveNextAsync());
Assert.Equal(value, e.Current);
}
protected async Task SequenceIdentity<T>(IAsyncEnumerable<T> enumerable)
{
var en1 = enumerable.GetAsyncEnumerator();
var en2 = enumerable.GetAsyncEnumerator();
Assert.Equal(en1.GetType(), en2.GetType());
await en1.DisposeAsync();
await en2.DisposeAsync();
var res1 = await enumerable.ToListAsync();
var res2 = await enumerable.ToListAsync();
res1.Should().BeEquivalentTo(res2);
}
protected static IAsyncEnumerable<TValue> Throw<TValue>(Exception exception)
{
if (exception == null)
throw new ArgumentNullException(nameof(exception));
#if NO_TASK_FROMEXCEPTION
var tcs = new TaskCompletionSource<bool>();
tcs.TrySetException(exception);
var moveNextThrows = new ValueTask<bool>(tcs.Task);
#else
var moveNextThrows = new ValueTask<bool>(Task.FromException<bool>(exception));
#endif
return AsyncEnumerable.Create(
_ => AsyncEnumerator.Create<TValue>(
() => moveNextThrows,
getCurrent: null,
disposeAsync: null)
);
}
}
}