Skip to content

Commit 45e3632

Browse files
committed
Fix nullable formatting
1 parent 6c748e3 commit 45e3632

File tree

3 files changed

+28
-31
lines changed

3 files changed

+28
-31
lines changed

src/SharpAssert.Demo/demo.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -934,8 +934,8 @@ Assert(value == 42);
934934
**Output:**
935935
```
936936
Assertion failed: value == 42 at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/11_NullableTypeDemos.cs:13
937-
Left: HasValue: false
938-
Right: HasValue: true, Value: 42
937+
Left: null
938+
Right: 42
939939
```
940940

941941
---
@@ -952,8 +952,8 @@ Assert(value == 100);
952952
**Output:**
953953
```
954954
Assertion failed: value == 100 at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/11_NullableTypeDemos.cs:22
955-
Left: HasValue: true, Value: 42
956-
Right: HasValue: true, Value: 100
955+
Left: 42
956+
Right: 100
957957
```
958958

959959
---
@@ -970,8 +970,8 @@ Assert(value == true);
970970
**Output:**
971971
```
972972
Assertion failed: value == true at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/11_NullableTypeDemos.cs:31
973-
Left: HasValue: true, Value: False
974-
Right: HasValue: true, Value: True
973+
Left: False
974+
Right: True
975975
```
976976

977977
---
@@ -989,8 +989,8 @@ Assert(value == expected);
989989
**Output:**
990990
```
991991
Assertion failed: value == expected at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/11_NullableTypeDemos.cs:41
992-
Left: HasValue: false
993-
Right: HasValue: true, Value: 10/8/2025
992+
Left: null
993+
Right: 10/8/2025
994994
```
995995

996996
---
@@ -1026,8 +1026,8 @@ Assert(value == null);
10261026
**Output:**
10271027
```
10281028
Assertion failed: value == null at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/11_NullableTypeDemos.cs:60
1029-
Left: HasValue: true, Value: 42
1030-
Right: HasValue: false
1029+
Left: 42
1030+
Right: null
10311031
```
10321032

10331033
---

src/SharpAssert.Runtime/NullableComparisonFormatter.cs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ static string FormatWithType(object? value, Type expressionType)
4444
if (!IsNullableType(expressionType))
4545
return FormatActualValue(value);
4646

47-
return value == null
48-
? "HasValue: false"
49-
: $"HasValue: true, Value: {FormatActualValue(value)}";
47+
return value == null ? "null" : $"{FormatActualValue(value)}";
5048
}
5149

5250
static string FormatRuntimeValue(object? value)
@@ -55,22 +53,21 @@ static string FormatRuntimeValue(object? value)
5553
return "null";
5654

5755
var type = value.GetType();
58-
if (IsNullableType(type))
59-
{
60-
var hasValueProperty = type.GetProperty("HasValue");
61-
var valueProperty = type.GetProperty("Value");
62-
63-
if (hasValueProperty?.GetValue(value) is bool hasValue)
64-
{
65-
if (!hasValue)
66-
return "HasValue: false";
67-
68-
var actualValue = valueProperty?.GetValue(value);
69-
return $"HasValue: true, Value: {FormatActualValue(actualValue)}";
70-
}
71-
}
72-
73-
return FormatActualValue(value);
56+
if (!IsNullableType(type))
57+
return FormatActualValue(value);
58+
59+
var hasValueProperty = type.GetProperty("HasValue");
60+
var valueProperty = type.GetProperty("Value");
61+
62+
if (hasValueProperty?.GetValue(value) is not bool hasValue)
63+
return FormatActualValue(value);
64+
65+
if (!hasValue)
66+
return "null";
67+
68+
var actualValue = valueProperty?.GetValue(value);
69+
return $"{FormatActualValue(actualValue)}";
70+
7471
}
7572

7673
static bool IsNullableType(Type? type)

src/SharpAssert.Tests/NullableTypeFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public void Should_show_HasValue_information_in_detailed_diagnostics()
132132
Expression<Func<bool>> expr = () => nullableWithoutValue == nullableWithValue;
133133

134134
AssertExpressionThrows(expr, "nullableWithoutValue == nullableWithValue", "TestFile.cs", 123,
135-
"*HasValue: false*HasValue: true, Value: 42*");
135+
"*null*42*");
136136
}
137137

138138
[Test]
@@ -142,6 +142,6 @@ public void Should_handle_null_comparison_edge_cases()
142142
Expression<Func<bool>> expr = () => nullable == null;
143143

144144
AssertExpressionThrows(expr, "nullable == null", "TestFile.cs", 123,
145-
"*HasValue: true, Value: 42*HasValue: false*");
145+
"*42*null*");
146146
}
147147
}

0 commit comments

Comments
 (0)