Skip to content

Commit f24aa5d

Browse files
author
Oren Novotny
committed
Update tests to work with ValueTask
1 parent 0ae1af3 commit f24aa5d

File tree

9 files changed

+140
-31
lines changed

9 files changed

+140
-31
lines changed

Ix.NET/Source/System.Interactive.Async.Tests/AsyncTests.Bugs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public async Task DisposesUponError()
6363
var ys = xs.Select(x => { if (x == 1) throw ex; return x; });
6464

6565
var e = ys.GetAsyncEnumerator();
66-
await Assert.ThrowsAsync<Exception>(() => e.MoveNextAsync());
66+
await AssertX.ThrowsAsync<Exception>(() => e.MoveNextAsync());
6767

6868
var result = await disposed.Task;
6969
Assert.True(result);

Ix.NET/Source/System.Interactive.Async.Tests/System.Interactive.Async.Tests.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727

2828
</ItemGroup>
2929

30+
<ItemGroup>
31+
<Compile Include="..\System.Linq.Async.Tests\ValueTaskHelpers.cs" />
32+
<Compile Include="..\System.Linq.Async.Tests\TaskExt.cs" />
33+
</ItemGroup>
34+
3035
<ItemGroup>
3136
<None Update="AsyncQueryableExTests.Generated.tt">
3237
<LastGenOutput>AsyncQueryableExTests.Generated.cs</LastGenOutput>

Ix.NET/Source/System.Linq.Async.Tests/System/Linq/AsyncEnumerableTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ protected static IAsyncEnumerable<TValue> Throw<TValue>(Exception exception)
8888
#if NO_TASK_FROMEXCEPTION
8989
var tcs = new TaskCompletionSource<bool>();
9090
tcs.TrySetException(exception);
91-
var moveNextThrows = tcs.Task;
91+
var moveNextThrows = new ValueTask<bool>(tcs.Task);
9292
#else
93-
var moveNextThrows = Task.FromException<bool>(exception);
93+
var moveNextThrows = new ValueTask<bool>(Task.FromException<bool>(exception));
9494
#endif
9595

9696
return AsyncEnumerable.CreateEnumerable(

Ix.NET/Source/System.Linq.Async.Tests/System/Linq/AsyncEnumeratorTests.cs

Lines changed: 0 additions & 21 deletions
This file was deleted.

Ix.NET/Source/System.Linq.Async.Tests/System/Linq/Operators/CreateEnumerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ public class CreateEnumerator : AsyncEnumerableTests
1515
[Fact]
1616
public void CreateEnumerator_Null()
1717
{
18-
AssertThrows<ArgumentNullException>(() => AsyncEnumerable.CreateEnumerator<int>(default(Func<Task<bool>>), () => 3, () => Task.FromResult(true)));
18+
AssertThrows<ArgumentNullException>(() => AsyncEnumerable.CreateEnumerator<int>(default(Func<ValueTask<bool>>), () => 3, () => TaskExt.CompletedTask));
1919
}
2020

2121
[Fact]
2222
public void CreateEnumerator_Throws()
2323
{
24-
var iter = AsyncEnumerable.CreateEnumerator<int>(() => Task.FromResult(true), () => 3, () => Task.FromResult(true));
24+
var iter = AsyncEnumerable.CreateEnumerator<int>(() => TaskExt.True, () => 3, () => TaskExt.CompletedTask);
2525

2626
var enu = (IAsyncEnumerable<int>)iter;
2727

Ix.NET/Source/System.Linq.Async.Tests/System/Linq/Operators/ToEnumerable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void ToEnumerable3()
3535
{
3636
var ex = new Exception("Bang");
3737
var xs = Throw<int>(ex).ToEnumerable();
38-
AssertThrows<Exception>(() => xs.GetEnumerator().MoveNext(), SingleInnerExceptionMatches(ex));
38+
Assert.Throws<Exception>(() => xs.GetEnumerator().MoveNext());
3939
}
4040
}
4141
}

Ix.NET/Source/System.Linq.Async.Tests/System/Linq/Operators/ToObservable.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ public void ToObservable_DisposesEnumeratorOnCompletion()
143143

144144
var ae = AsyncEnumerable.CreateEnumerable(
145145
() => AsyncEnumerable.CreateEnumerator<int>(
146-
() => Task.FromResult(false),
146+
() => TaskExt.False,
147147
() => { throw new InvalidOperationException(); },
148-
() => { evt.Set(); return Task.FromResult(true); }));
148+
() => { evt.Set(); return TaskExt.CompletedTask; }));
149149

150150
ae
151151
.ToObservable()
@@ -186,7 +186,7 @@ public void ToObservable_DisposesEnumeratorWhenSubscriptionIsDisposed()
186186
() =>
187187
{
188188
evt.Set();
189-
return Task.FromResult(true);
189+
return TaskExt.CompletedTask;
190190
}));
191191

192192
subscription = ae
@@ -234,7 +234,7 @@ public void ToObservable_DesNotCallMoveNextAgainWhenSubscriptionIsDisposed()
234234
() =>
235235
{
236236
evt.Set();
237-
return Task.FromResult(true);
237+
return TaskExt.CompletedTask;
238238
}));
239239

240240
subscription = ae
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information.
4+
5+
namespace System.Threading.Tasks
6+
{
7+
internal static class TaskExt
8+
{
9+
public static readonly ValueTask<bool> True = new ValueTask<bool>(true);
10+
public static readonly ValueTask<bool> False = new ValueTask<bool>(false);
11+
public static readonly ValueTask CompletedTask = new ValueTask(Task.FromResult(true));
12+
public static readonly ValueTask<bool> Never = new ValueTask<bool>(new TaskCompletionSource<bool>().Task);
13+
}
14+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using System.Threading.Tasks;
5+
using Xunit.Sdk;
6+
7+
namespace System.Linq
8+
{
9+
static class ValueTaskHelpers
10+
{
11+
public static void Wait<T>(this ValueTask<T> task, int timeOut)
12+
{
13+
task.AsTask().Wait(timeOut);
14+
}
15+
}
16+
}
17+
18+
namespace Xunit
19+
{
20+
static class AssertX
21+
{
22+
/// <summary>
23+
/// Verifies that the exact exception is thrown (and not a derived exception type).
24+
/// </summary>
25+
/// <typeparam name="T">The type of the exception expected to be thrown</typeparam>
26+
/// <param name="testCode">A delegate to the task to be tested</param>
27+
/// <returns>The exception that was thrown, when successful</returns>
28+
/// <exception cref="ThrowsException">Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown</exception>
29+
public static async Task<T> ThrowsAsync<T>(Func<ValueTask> testCode)
30+
where T : Exception
31+
{
32+
return (T)Throws(typeof(T), await RecordExceptionAsync(testCode));
33+
}
34+
35+
/// <summary>
36+
/// Verifies that the exact exception is thrown (and not a derived exception type).
37+
/// </summary>
38+
/// <typeparam name="T">The type of the exception expected to be thrown</typeparam>
39+
/// <param name="testCode">A delegate to the task to be tested</param>
40+
/// <returns>The exception that was thrown, when successful</returns>
41+
/// <exception cref="ThrowsException">Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown</exception>
42+
public static async Task<T> ThrowsAsync<T>(Func<ValueTask<bool>> testCode)
43+
where T : Exception
44+
{
45+
return (T)Throws(typeof(T), await RecordExceptionAsync(testCode));
46+
}
47+
48+
/// <summary>
49+
/// Records any exception which is thrown by the given task.
50+
/// </summary>
51+
/// <param name="testCode">The task which may thrown an exception.</param>
52+
/// <returns>Returns the exception that was thrown by the code; null, otherwise.</returns>
53+
static async Task<Exception> RecordExceptionAsync(Func<ValueTask> testCode)
54+
{
55+
if (testCode == null)
56+
{
57+
throw new ArgumentNullException(nameof(testCode));
58+
}
59+
60+
try
61+
{
62+
await testCode();
63+
return null;
64+
}
65+
catch (Exception ex)
66+
{
67+
return ex;
68+
}
69+
}
70+
71+
/// <summary>
72+
/// Records any exception which is thrown by the given task.
73+
/// </summary>
74+
/// <param name="testCode">The task which may thrown an exception.</param>
75+
/// <returns>Returns the exception that was thrown by the code; null, otherwise.</returns>
76+
static async Task<Exception> RecordExceptionAsync<T>(Func<ValueTask<T>> testCode)
77+
{
78+
if (testCode == null)
79+
{
80+
throw new ArgumentNullException(nameof(testCode));
81+
}
82+
83+
try
84+
{
85+
await testCode();
86+
return null;
87+
}
88+
catch (Exception ex)
89+
{
90+
return ex;
91+
}
92+
}
93+
94+
static Exception Throws(Type exceptionType, Exception exception)
95+
{
96+
if (exceptionType == null)
97+
{
98+
throw new ArgumentNullException(nameof(exceptionType));
99+
}
100+
101+
if (exception == null)
102+
throw new ThrowsException(exceptionType);
103+
104+
if (!exceptionType.Equals(exception.GetType()))
105+
throw new ThrowsException(exceptionType, exception);
106+
107+
return exception;
108+
}
109+
}
110+
111+
}

0 commit comments

Comments
 (0)