Skip repeats and IValueTaskSource.GetResult (#132)

* Skip repeats and IValueTaskSource.GetResult

* Add RecursionTests

* Varibale counts

* More flexible
This commit is contained in:
Ben Adams
2021-01-03 21:48:34 +00:00
committed by GitHub
parent c502ee90f6
commit b3e8977885
5 changed files with 140 additions and 17 deletions

View File

@@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
@@ -33,16 +34,13 @@ namespace Ben.Demystifier.Test
var stackTrace = demystifiedException.ToString();
stackTrace = LineEndingsHelper.RemoveLineEndings(stackTrace);
var trace = string.Join("", stackTrace.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
.Select(s => Regex.Replace(s, " x [0-9]+", " x N"))
.Skip(1)
.ToArray());
var expected = string.Join("", new[] {
" at async IAsyncEnumerable<int> Ben.Demystifier.Test.AsyncEnumerableTests.Throw(CancellationToken cancellationToken)+MoveNext()",
" at async IAsyncEnumerable<int> Ben.Demystifier.Test.AsyncEnumerableTests.Throw(CancellationToken cancellationToken)+System.Threading.Tasks.Sources.IValueTaskSource<System.Boolean>.GetResult(short token)",
" at async IAsyncEnumerable<long> Ben.Demystifier.Test.AsyncEnumerableTests.Start(CancellationToken cancellationToken)+MoveNext()",
" at async IAsyncEnumerable<long> Ben.Demystifier.Test.AsyncEnumerableTests.Start(CancellationToken cancellationToken)+MoveNext()",
" at async IAsyncEnumerable<long> Ben.Demystifier.Test.AsyncEnumerableTests.Start(CancellationToken cancellationToken)+System.Threading.Tasks.Sources.IValueTaskSource<System.Boolean>.GetResult(short token)",
" at async Task Ben.Demystifier.Test.AsyncEnumerableTests.DemystifiesAsyncEnumerable()",
" at async Task Ben.Demystifier.Test.AsyncEnumerableTests.DemystifiesAsyncEnumerable()"
" at async IAsyncEnumerable<long> Ben.Demystifier.Test.AsyncEnumerableTests.Start(CancellationToken cancellationToken)+MoveNext() x N",
" at async Task Ben.Demystifier.Test.AsyncEnumerableTests.DemystifiesAsyncEnumerable() x N"
});
Assert.Equal(expected, trace);
}

View File

@@ -0,0 +1,74 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Xunit;
namespace Ben.Demystifier.Test
{
public class RecursionTests
{
[Fact]
public async Task DemystifiesAsyncRecursion()
{
Exception demystifiedException = null;
try
{
await RecurseAsync(10);
}
catch (Exception ex)
{
demystifiedException = ex.Demystify();
}
// Assert
var stackTrace = demystifiedException.ToString();
stackTrace = LineEndingsHelper.RemoveLineEndings(stackTrace);
var traces = stackTrace.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
.Select(s => Regex.Replace(s, " x [0-9]+", " x N"))
.Skip(1)
.ToArray();
Assert.Contains(" at async Task<int> Ben.Demystifier.Test.RecursionTests.RecurseAsync(int depth) x N", traces);
}
[Fact]
public void DemystifiesRecursion()
{
Exception demystifiedException = null;
try
{
Recurse(10);
}
catch (Exception ex)
{
demystifiedException = ex.Demystify();
}
// Assert
var stackTrace = demystifiedException.ToString();
stackTrace = LineEndingsHelper.RemoveLineEndings(stackTrace);
var traces = stackTrace.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
.Select(s => Regex.Replace(s, " x [0-9]+", " x N"))
.Skip(1)
.ToArray();
Assert.Contains(" at int Ben.Demystifier.Test.RecursionTests.Recurse(int depth) x N", traces);
}
async Task<int> RecurseAsync(int depth)
{
if (depth > 0) await RecurseAsync(depth - 1);
throw new InvalidOperationException();
}
int Recurse(int depth)
{
if (depth > 0) Recurse(depth - 1);
throw new InvalidOperationException();
}
}
}