DTLib.Demystifier/test/Ben.Demystifier.Test/DynamicCompilation.cs
2017-11-12 12:06:26 +00:00

60 lines
1.9 KiB
C#

using System;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Xunit;
namespace Demystify
{
public class DynamicCompilation
{
[Fact]
public async Task DoesNotPreventStackTrace()
{
// Arrange
var expression = Expression.Throw(
Expression.New(
typeof(ArgumentException).GetConstructor(
new Type[] {typeof(string)}),
Expression.Constant( "Message")));
var lambda = Expression.Lambda<Action>(expression);
var action = lambda.Compile();
// Act
Exception demystifiedException = null;
try
{
await Task.Run(() => action()).ConfigureAwait(false);
}
catch(Exception ex)
{
demystifiedException = ex.Demystify();
}
// Assert
var stackTrace = demystifiedException.ToString();
stackTrace = ReplaceLineEndings.Replace(stackTrace, "");
var trace = stackTrace.Split(Environment.NewLine)
// Remove items that vary between test runners
.Where(s =>
s != " at void System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, object state)" &&
s != " at Task Demystify.DynamicCompilation.DoesNotPreventStackTrace()+()=>{}"
)
.ToArray();
Assert.Equal(
new[] {
"System.ArgumentException: Message",
" at void lambda_method(Closure)",
" at async Task Demystify.DynamicCompilation.DoesNotPreventStackTrace()"},
trace);
}
private Regex ReplaceLineEndings = new Regex(" in [^\n\r]+");
}
}