|
1 |
| -using System; |
| 1 | +using System; |
2 | 2 | using System.IO;
|
| 3 | +using System.Linq; |
3 | 4 | using Funcky.Monads;
|
4 | 5 | using Xunit;
|
5 | 6 | using static Funcky.Functional;
|
@@ -99,5 +100,49 @@ public static TheoryData<Result<int>, bool> GetResults()
|
99 | 100 | { Result<int>.Ok(42), true },
|
100 | 101 | { Result<int>.Error(new InvalidCastException()), false },
|
101 | 102 | };
|
| 103 | + |
| 104 | + [Fact] |
| 105 | + public void GivenAResultWithAnExceptionWeGetAStackTrace() |
| 106 | + { |
| 107 | + var arbitrayNumberOfStackFrames = 3; |
| 108 | + |
| 109 | + InterestingStackTrace(arbitrayNumberOfStackFrames) |
| 110 | + .Match( |
| 111 | + ok: v => FunctionalAssert.Unmatched("ok"), |
| 112 | + error: e => Assert.NotNull(e.StackTrace)); |
| 113 | + } |
| 114 | + |
| 115 | + [Fact] |
| 116 | + public void GivenAResultWithAnExceptionTheStackTraceStartsInCreationMethod() |
| 117 | + { |
| 118 | + var arbitrayNumberOfStackFrames = 0; |
| 119 | + |
| 120 | + InterestingStackTrace(arbitrayNumberOfStackFrames) |
| 121 | + .Match( |
| 122 | + ok: v => FunctionalAssert.Unmatched("ok"), |
| 123 | + error: IsInterestingStackTraceFirst); |
| 124 | + } |
| 125 | + |
| 126 | + private void IsInterestingStackTraceFirst(Exception exception) |
| 127 | + { |
| 128 | + if (exception.StackTrace is { }) |
| 129 | + { |
| 130 | + var lines = exception.StackTrace.Split(new[] { Environment.NewLine }, StringSplitOptions.None); |
| 131 | + |
| 132 | + Assert.StartsWith(" at Funcky.Test.ResultTest.InterestingStackTrace(Int32 n)", lines.First()); |
| 133 | + } |
| 134 | + else |
| 135 | + { |
| 136 | + FunctionalAssert.Unmatched("else"); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + private Result<int> InterestingStackTrace(int n) |
| 141 | + => n == 0 |
| 142 | + ? Result<int>.Error(new InvalidCastException()) |
| 143 | + : Indirection(n - 1); |
| 144 | + |
| 145 | + private Result<int> Indirection(int n) |
| 146 | + => InterestingStackTrace(n); |
102 | 147 | }
|
103 | 148 | }
|
0 commit comments