-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathExceptionExtensionsTests.cs
More file actions
76 lines (63 loc) · 2.87 KB
/
ExceptionExtensionsTests.cs
File metadata and controls
76 lines (63 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using Xunit;
namespace Platform.Exceptions.Tests
{
public static class ExceptionExtensionsTests
{
[Fact]
public static void ToStringWithAllInnerExceptions_WithSingleException_FormatsProperly()
{
// Arrange
var exception = new ArgumentException("Test message");
// Act
var result = exception.ToStringWithAllInnerExceptions();
// Assert
Assert.Contains("Test message", result);
Assert.Contains(ExceptionExtensions.ExceptionContentsSeparator, result);
}
[Fact]
public static void ToStringWithAllInnerExceptions_WithNestedExceptions_FormatsProperly()
{
// Arrange
var innermost = new ArgumentException("Innermost exception");
var middle = new InvalidOperationException("Middle exception", innermost);
var outer = new Exception("Outer exception", middle);
// Act
var result = outer.ToStringWithAllInnerExceptions();
// Assert
Assert.Contains("Outer exception", result);
Assert.Contains("Middle exception", result);
Assert.Contains("Innermost exception", result);
Assert.Contains("Inner exception: ", result);
}
[Fact]
public static void ToStringWithAllInnerExceptions_WithNullException_HandlesGracefully()
{
// This test ensures our iterative implementation handles edge cases
Exception nullException = null;
// This should not be called on null, but let's test with a valid exception with null inner
var exception = new ArgumentException("Test");
// Act & Assert (should not throw)
var result = exception.ToStringWithAllInnerExceptions();
Assert.NotNull(result);
Assert.Contains("Test", result);
}
[Fact]
public static void ToStringWithAllInnerExceptions_WithDeeplyNestedExceptions_WorksWithoutStackOverflow()
{
// Arrange - Create a deep chain of exceptions to test non-recursive behavior
Exception current = new ArgumentException("Level 0");
// Create 100 levels of nested exceptions
for (int i = 1; i < 100; i++)
{
current = new InvalidOperationException($"Level {i}", current);
}
// Act & Assert (should not throw stack overflow)
var result = current.ToStringWithAllInnerExceptions();
Assert.NotNull(result);
Assert.Contains("Level 0", result);
Assert.Contains("Level 99", result);
Assert.NotEqual(ExceptionExtensions.ExceptionStringBuildingFailed, result);
}
}
}