Fix stack trace for tuples type as part of a generic type.

Fix for #57
This commit is contained in:
Sergey Tepliakov
2018-02-20 14:59:05 -08:00
parent 57e2d49ad1
commit 54f4b0c24b
9 changed files with 196 additions and 22 deletions

View File

@@ -33,7 +33,7 @@ namespace Ben.Demystifier.Test
// Assert
var stackTrace = demystifiedException.ToString();
stackTrace = ReplaceLineEndings.Replace(stackTrace, "");
stackTrace = LineEndingsHelper.RemoveLineEndings(stackTrace);
var trace = string.Join("", stackTrace.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
// Remove items that vary between test runners
.Where(s =>
@@ -80,8 +80,5 @@ namespace Ben.Demystifier.Test
await Task.Delay(1).ConfigureAwait(false);
throw new InvalidOperationException();
}
private Regex ReplaceLineEndings = new Regex(" in [^\n\r]+");
}
}

View File

@@ -37,7 +37,7 @@ namespace Ben.Demystifier.Test
// Assert
var stackTrace = demystifiedException.ToString();
stackTrace = ReplaceLineEndings.Replace(stackTrace, "");
stackTrace = LineEndingsHelper.RemoveLineEndings(stackTrace);
var trace = stackTrace.Split(new[] { Environment.NewLine }, StringSplitOptions.None)
// Remove items that vary between test runners
.Where(s =>
@@ -53,7 +53,5 @@ namespace Ben.Demystifier.Test
" at async Task Ben.Demystifier.Test.DynamicCompilation.DoesNotPreventStackTrace()"},
trace);
}
private Regex ReplaceLineEndings = new Regex(" in [^\n\r]+");
}
}

View File

@@ -0,0 +1,14 @@
using System.Text.RegularExpressions;
namespace Ben.Demystifier.Test
{
internal static class LineEndingsHelper
{
private static readonly Regex ReplaceLineEndings = new Regex(" in [^\n\r]+");
public static string RemoveLineEndings(string original)
{
return ReplaceLineEndings.Replace(original, "");
}
}
}

View File

@@ -28,7 +28,7 @@ namespace Ben.Demystifier.Test
// Assert
var stackTrace = demystifiedException.ToString();
stackTrace = ReplaceLineEndings.Replace(stackTrace, "");
stackTrace = LineEndingsHelper.RemoveLineEndings(stackTrace);
var trace = stackTrace.Split(new[]{Environment.NewLine}, StringSplitOptions.None);
Assert.Equal(
@@ -51,7 +51,7 @@ namespace Ben.Demystifier.Test
// Assert
stackTrace = demystifiedException.ToString();
stackTrace = ReplaceLineEndings.Replace(stackTrace, "");
stackTrace = LineEndingsHelper.RemoveLineEndings(stackTrace);
trace = stackTrace.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
Assert.Equal(
@@ -76,7 +76,7 @@ namespace Ben.Demystifier.Test
// Assert
var stackTrace = est.ToString();
stackTrace = ReplaceLineEndings.Replace(stackTrace, "");
stackTrace = LineEndingsHelper.RemoveLineEndings(stackTrace);
var trace = stackTrace.Split(new[] { Environment.NewLine }, StringSplitOptions.None)
// Remove Full framework entries
.Where(s => !s.StartsWith(" at bool System.Threading._ThreadPoolWaitCallbac") &&
@@ -88,7 +88,5 @@ namespace Ben.Demystifier.Test
" at bool System.Threading.ThreadPoolWorkQueue.Dispatch()"},
trace);
}
private Regex ReplaceLineEndings = new Regex(" in [^\n\r]+");
}
}

View File

@@ -0,0 +1,27 @@
using System;
using System.Diagnostics.Internal;
using Xunit;
namespace Ben.Demystifier.Test
{
public class ReflectionHelperTest
{
[Fact]
public void IsValueTupleReturnsTrueForTupleWith1Element()
{
Assert.True(typeof(ValueTuple<int>).IsValueTuple());
}
[Fact]
public void IsValueTupleReturnsTrueForTupleWith1ElementWithOpenedType()
{
Assert.True(typeof(ValueTuple<>).IsValueTuple());
}
[Fact]
public void IsValueTupleReturnsTrueForTupleWith6ElementsWithOpenedType()
{
Assert.True(typeof(ValueTuple<,,,,,>).IsValueTuple());
}
}
}

View File

@@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using Xunit;
namespace Ben.Demystifier.Test
{
public class TuplesTests
{
[Fact]
public void DemistifiesAsyncMethodWithTuples()
{
Exception demystifiedException = null;
try
{
AsyncThatReturnsTuple().GetAwaiter().GetResult();
}
catch (Exception ex)
{
demystifiedException = ex.Demystify();
}
// Assert
var stackTrace = demystifiedException.ToString();
stackTrace = LineEndingsHelper.RemoveLineEndings(stackTrace);
var trace = string.Join("", stackTrace.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries));
var expected = string.Join("", new[] {
"System.ArgumentException: Value does not fall within the expected range.",
" at async Task<(int left, int right)> Ben.Demystifier.Test.TuplesTests.AsyncThatReturnsTuple()",
" at void Ben.Demystifier.Test.TuplesTests.DemistifiesAsyncMethodWithTuples()"});
Assert.Equal(expected, trace);
}
[Fact]
public void DemistifiesListOfTuples()
{
Exception demystifiedException = null;
try
{
ListOfTuples();
}
catch (Exception ex)
{
demystifiedException = ex.Demystify();
}
// Assert
var stackTrace = demystifiedException.ToString();
stackTrace = LineEndingsHelper.RemoveLineEndings(stackTrace);
var trace = string.Join("", stackTrace.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries));
var expected = string.Join("", new[] {
"System.ArgumentException: Value does not fall within the expected range.",
" at List<(int left, int right)> Ben.Demystifier.Test.TuplesTests.ListOfTuples()",
" at void Ben.Demystifier.Test.TuplesTests.DemistifiesListOfTuples()"});
Assert.Equal(expected, trace);
}
async Task<(int left, int right)> AsyncThatReturnsTuple()
{
await Task.Delay(1).ConfigureAwait(false);
throw new ArgumentException();
}
List<(int left, int right)> ListOfTuples()
{
throw new ArgumentException();
}
}
}