Skip to content

Commit c5f8422

Browse files
authored
Make StackTraceHelper tests runtime-agnostic (#64007)
1 parent e91b768 commit c5f8422

File tree

1 file changed

+40
-4
lines changed

1 file changed

+40
-4
lines changed

src/Shared/test/Shared.Tests/StackTraceHelperTest.cs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ namespace Microsoft.Extensions.Internal;
1515

1616
public class StackTraceHelperTest
1717
{
18+
private static bool IsMono => Type.GetType("Mono.RuntimeStructs") != null;
19+
1820
[Fact]
1921
public void StackTraceHelper_IncludesLineNumbersForFiles()
2022
{
@@ -179,7 +181,30 @@ public void StackTraceHelper_ProducesReadableOutput()
179181
var methodNames = stackFrames.Select(stackFrame => stackFrame.MethodDisplayInfo.ToString()).ToArray();
180182

181183
// Assert
182-
Assert.Equal(expectedCallStack, methodNames);
184+
Assert.Equal(expectedCallStack.Count, methodNames.Length);
185+
186+
if (IsMono)
187+
{
188+
// On Mono, verify key components are present but allow for runtime-specific formatting
189+
Assert.Contains("Iterator()+MoveNext()", methodNames[0]);
190+
Assert.Contains("string.Join", methodNames[1]);
191+
Assert.Contains("GenericClass<T>.GenericMethod<V>", methodNames[2]);
192+
Assert.Contains("MethodAsync(int value)", methodNames[3]);
193+
194+
// For async generic method on Mono, check for either resolved form or state machine form
195+
var asyncGenericFrame = methodNames[4];
196+
Assert.True(
197+
asyncGenericFrame.Contains("MethodAsync<TValue>(TValue value)") || // Resolved form
198+
asyncGenericFrame.Contains("MethodAsync") && asyncGenericFrame.Contains("TValue"), // State machine form
199+
$"Expected async generic method info in: {asyncGenericFrame}");
200+
201+
Assert.Contains("Method(string value)", methodNames[5]);
202+
Assert.Contains("StackTraceHelper_ProducesReadableOutput()", methodNames[6]);
203+
}
204+
else
205+
{
206+
Assert.Equal(expectedCallStack, methodNames);
207+
}
183208
}
184209

185210
[Fact]
@@ -242,9 +267,20 @@ public void GetFrames_DoesNotFailForDynamicallyGeneratedAssemblies()
242267
// Assert
243268
var frame = frames[0];
244269
Assert.Null(frame.FilePath);
245-
// lambda_method{RandomNumber}(Closure )
246-
Assert.StartsWith("lambda_method", frame.MethodDisplayInfo.ToString());
247-
Assert.EndsWith("(Closure )", frame.MethodDisplayInfo.ToString());
270+
var methodDisplay = frame.MethodDisplayInfo.ToString();
271+
272+
if (IsMono)
273+
{
274+
// On Mono, lambda methods may include declaring type prefix (e.g., "object.lambda_method34")
275+
Assert.Contains("lambda_method", methodDisplay);
276+
Assert.EndsWith("(Closure )", methodDisplay);
277+
}
278+
else
279+
{
280+
// On CoreCLR, maintain strict check to prevent regressions
281+
Assert.StartsWith("lambda_method", methodDisplay);
282+
Assert.EndsWith("(Closure )", methodDisplay);
283+
}
248284
}
249285

250286
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]

0 commit comments

Comments
 (0)