defaultifempty

master
neuecc 2020-05-10 22:52:12 +09:00
parent 93dd82e3d4
commit 3f082f1923
3 changed files with 38 additions and 5 deletions

View File

@ -73,11 +73,16 @@ namespace Cysharp.Threading.Tasks.Linq
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
completionSource.Reset(); completionSource.Reset();
if (iteratingState == IteratingState.Empty) if (iteratingState == IteratingState.Completed)
{ {
return CompletedTasks.False; return CompletedTasks.False;
} }
if (enumerator == null)
{
enumerator = source.GetAsyncEnumerator(cancellationToken);
}
awaiter = enumerator.MoveNextAsync().GetAwaiter(); awaiter = enumerator.MoveNextAsync().GetAwaiter();
if (awaiter.IsCompleted) if (awaiter.IsCompleted)

View File

@ -112,5 +112,33 @@ namespace NetCoreTests.Linq
await Assert.ThrowsAsync<UniTaskTestException>(async () => await zs); await Assert.ThrowsAsync<UniTaskTestException>(async () => await zs);
} }
} }
[Fact]
public async Task DefaultIfEmpty()
{
{
var xs = await Enumerable.Range(1, 0).ToUniTaskAsyncEnumerable().DefaultIfEmpty(99).ToArrayAsync();
var ys = Enumerable.Range(1, 0).DefaultIfEmpty(99).ToArray();
xs.Should().BeEquivalentTo(ys);
}
{
var xs = await Enumerable.Range(1, 1).ToUniTaskAsyncEnumerable().DefaultIfEmpty(99).ToArrayAsync();
var ys = Enumerable.Range(1, 1).DefaultIfEmpty(99).ToArray();
xs.Should().BeEquivalentTo(ys);
}
{
var xs = await Enumerable.Range(1, 10).ToUniTaskAsyncEnumerable().DefaultIfEmpty(99).ToArrayAsync();
var ys = Enumerable.Range(1, 10).DefaultIfEmpty(99).ToArray();
xs.Should().BeEquivalentTo(ys);
}
// Throw
{
foreach (var item in UniTaskTestException.Throws())
{
var xs = item.DefaultIfEmpty().ToArrayAsync();
await Assert.ThrowsAsync<UniTaskTestException>(async () => await xs);
}
}
}
} }
} }

View File

@ -23,14 +23,14 @@ namespace NetCoreTests.Linq
} }
public static IEnumerable<IUniTaskAsyncEnumerable<int>> Throws() public static IEnumerable<IUniTaskAsyncEnumerable<int>> Throws(int count = 3)
{ {
yield return ThrowImmediate(); yield return ThrowImmediate();
yield return ThrowAfter(); yield return ThrowAfter();
yield return ThrowInMoveNext(); yield return ThrowInMoveNext();
yield return UniTaskAsyncEnumerable.Range(1, 3).Concat(ThrowImmediate()); yield return UniTaskAsyncEnumerable.Range(1, count).Concat(ThrowImmediate());
yield return UniTaskAsyncEnumerable.Range(1, 3).Concat(ThrowAfter()); yield return UniTaskAsyncEnumerable.Range(1, count).Concat(ThrowAfter());
yield return UniTaskAsyncEnumerable.Range(1, 3).Concat(ThrowInMoveNext()); yield return UniTaskAsyncEnumerable.Range(1, count).Concat(ThrowInMoveNext());
} }
} }