public async Task DemistifiesMethodWithAsyncLambda() { Exception dex = null; try { await MethodWithAsyncLambda(); } catch (Exception e) { dex = e.Demystify(); } // Assert var stackTrace = dex.ToString(); stackTrace = LineEndingsHelper.RemoveLineEndings(stackTrace); var trace = string.Join(string.Empty, stackTrace.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)); var expected = string.Join(string.Empty, "System.ArgumentException: Value does not fall within the expected range.", " at async Task Ben.Demystifier.Test.MethodTests.MethodWithAsyncLambda()+(?) => { }", " at async Task Ben.Demystifier.Test.MethodTests.MethodWithAsyncLambda()", " at async Task Ben.Demystifier.Test.MethodTests.DemistifiesMethodWithAsyncLambda()"); Assert.Equal(expected, trace); }
public void DemistifiesMethodWithParams() { Exception dex = null; try { MethodWithParams(1, 2, 3); } catch (Exception e) { dex = e.Demystify(); } // Assert var stackTrace = dex.ToString(); stackTrace = LineEndingsHelper.RemoveLineEndings(stackTrace); var trace = string.Join(string.Empty, stackTrace.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)); var expected = string.Join(string.Empty, new[] { "System.ArgumentException: Value does not fall within the expected range.", " at bool Ben.Demystifier.Test.ParameterParamTests.MethodWithParams(params int[] numbers)", " at void Ben.Demystifier.Test.ParameterParamTests.DemistifiesMethodWithParams()" }); Assert.Equal(expected, trace); }
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); }
public async Task DemystifiesAsyncEnumerable() { Exception demystifiedException = null; try { await foreach (var val in Start(CancellationToken.None)) { _ = val; } } 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) .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<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); }
public async Task DoesNotPreventThrowStackTrace() { // Arrange Exception innerException = null; try { await Task.Run(() => throw new Exception()).ConfigureAwait(false); } catch (Exception ex) { innerException = ex; } // Act var demystifiedException = new Exception(innerException.Message, innerException).Demystify(); // Assert var stackTrace = demystifiedException.ToString(); stackTrace = LineEndingsHelper.RemoveLineEndings(stackTrace); var trace = stackTrace.Split(new[] { Environment.NewLine }, StringSplitOptions.None); Assert.Equal( new[] { "System.Exception: Exception of type 'System.Exception' was thrown. ---> System.Exception: Exception of type 'System.Exception' was thrown.", " at Task Ben.Demystifier.Test.NonThrownException.DoesNotPreventThrowStackTrace()+() => { }", " at async Task Ben.Demystifier.Test.NonThrownException.DoesNotPreventThrowStackTrace()", " --- End of inner exception stack trace ---" }, trace); // Act try { throw demystifiedException; } catch (Exception ex) { demystifiedException = ex.Demystify(); } // Assert stackTrace = demystifiedException.ToString(); stackTrace = LineEndingsHelper.RemoveLineEndings(stackTrace); trace = stackTrace.Split(new[] { Environment.NewLine }, StringSplitOptions.None); Assert.Equal( new[] { "System.Exception: Exception of type 'System.Exception' was thrown. ---> System.Exception: Exception of type 'System.Exception' was thrown.", " at Task Ben.Demystifier.Test.NonThrownException.DoesNotPreventThrowStackTrace()+() => { }", " at async Task Ben.Demystifier.Test.NonThrownException.DoesNotPreventThrowStackTrace()", " --- End of inner exception stack trace ---", " at async Task Ben.Demystifier.Test.NonThrownException.DoesNotPreventThrowStackTrace()" }, trace); }
public void DemystifiesAggregateExceptions() { Exception demystifiedException = null; try { var tasks = new List <Task> { Task.Run(async() => await Throw1()), Task.Run(async() => await Throw2()), Task.Run(async() => await Throw3()) }; Task.WaitAll(tasks.ToArray()); } 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) // Remove items that vary between test runners .Where(s => s != " at Task Ben.Demystifier.Test.DynamicCompilation.DoesNotPreventStackTrace()+() => { }" && !s.Contains("System.Threading.Tasks.Task.WaitAll") ) .Skip(1) .ToArray()) // Remove Full framework back arrow .Replace("<---", ""); var expected = string.Join("", new[] { " at async Task Ben.Demystifier.Test.AggregateException.Throw1()", " at async void Ben.Demystifier.Test.AggregateException.DemystifiesAggregateExceptions()+(?) => { }", " --- End of inner exception stack trace ---", " at void Ben.Demystifier.Test.AggregateException.DemystifiesAggregateExceptions()", "---> (Inner Exception #0) System.ArgumentException: Value does not fall within the expected range.", " at async Task Ben.Demystifier.Test.AggregateException.Throw1()", " at async void Ben.Demystifier.Test.AggregateException.DemystifiesAggregateExceptions()+(?) => { }", "---> (Inner Exception #1) System.NullReferenceException: Object reference not set to an instance of an object.", " at async Task Ben.Demystifier.Test.AggregateException.Throw2()", " at async void Ben.Demystifier.Test.AggregateException.DemystifiesAggregateExceptions()+(?) => { }", "---> (Inner Exception #2) System.InvalidOperationException: Operation is not valid due to the current state of the object.", " at async Task Ben.Demystifier.Test.AggregateException.Throw3()", " at async void Ben.Demystifier.Test.AggregateException.DemystifiesAggregateExceptions()+(?) => { }" }); Assert.Equal(expected, trace); }
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 = LineEndingsHelper.RemoveLineEndings(stackTrace); var trace = stackTrace.Split(new[] { Environment.NewLine }, StringSplitOptions.None) // 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 Ben.Demystifier.Test.DynamicCompilation.DoesNotPreventStackTrace()+() => { }" ) .Select(s => Regex.Replace(s, "lambda_method[0-9]+\\(", "lambda_method(")) .ToArray(); Assert.Equal( new[] { "System.ArgumentException: Message", " at void lambda_method(Closure)", " at async Task Ben.Demystifier.Test.DynamicCompilation.DoesNotPreventStackTrace()" }, trace); }
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); }
public async Task Current() { // Arrange EnhancedStackTrace est = null; // Act await Task.Run(() => est = EnhancedStackTrace.Current()).ConfigureAwait(false); // Assert var stackTrace = est.ToString(); 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") && !s.StartsWith(" at void System.Threading.Tasks.Task.System.Thre")); Assert.Equal( new[] { " at bool System.Threading.ThreadPoolWorkQueue.Dispatch()" }, trace); }