Skip to content

Commit 1ae9a47

Browse files
committed
Fix multiline string formatting
1 parent 45e3632 commit 1ae9a47

File tree

3 files changed

+63
-43
lines changed

3 files changed

+63
-43
lines changed

src/SharpAssert.Demo/demo.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -317,14 +317,19 @@ Assert(actual == expected);
317317
**Output:**
318318
```
319319
Assertion failed: actual == expected at /Users/yb/work/oss/SharpAssert/src/SharpAssert.Demo/Demos/04_StringComparisonDemos.cs:32
320-
Left: "Line 1: Introduction
320+
Left:
321+
Line 1: Introduction
321322
Line 2: Body content
322-
Line 3: Conclusion"
323-
Right: "Line 1: Introduction
323+
Line 3: Conclusion
324+
Right:
325+
Line 1: Introduction
324326
Line 2: Different content
325-
Line 3: Conclusion"
326-
- Line 2: Body content
327-
+ Line 2: Different content
327+
Line 3: Conclusion
328+
Diff:
329+
Line 1: Introduction
330+
- Line 2: Body content
331+
+ Line 2: Different content
332+
Line 3: Conclusion
328333
```
329334

330335
---

src/SharpAssert.Runtime/StringDiffer.cs

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ public static string FormatDiff(string? left, string? right)
1717
var leftTruncated = TruncateString(left);
1818
var rightTruncated = TruncateString(right);
1919

20-
var basic = $" Left: {FormatStringValue(leftTruncated)}\n Right: {FormatStringValue(rightTruncated)}";
21-
2220
if (IsMultiline(left) || IsMultiline(right))
23-
return basic + "\n" + GenerateMultilineDiff(leftTruncated, rightTruncated);
21+
return FormatMultilineComparison(leftTruncated, rightTruncated);
2422

23+
var basic = $" Left: {FormatStringValue(leftTruncated)}\n Right: {FormatStringValue(rightTruncated)}";
2524
return basic + "\n" + GenerateInlineDiff(leftTruncated, rightTruncated);
2625
}
2726

@@ -31,7 +30,22 @@ static string FormatNullComparison(string? left, string? right)
3130
if (left == null) return $" Left: null\n Right: {FormatStringValue(right)}";
3231
return $" Left: {FormatStringValue(left)}\n Right: null";
3332
}
34-
33+
34+
static string FormatMultilineComparison(string left, string right)
35+
{
36+
var result = new System.Text.StringBuilder();
37+
result.AppendLine(" Left:");
38+
foreach (var line in left.Split('\n'))
39+
result.AppendLine(line);
40+
41+
result.AppendLine(" Right:");
42+
foreach (var line in right.Split('\n'))
43+
result.AppendLine(line);
44+
45+
result.Append(GenerateMultilineDiff(left, right));
46+
return result.ToString();
47+
}
48+
3549
static string FormatStringValue(string? value)
3650
{
3751
if (value == null) return "null";
@@ -97,46 +111,38 @@ static void AppendRemainingText(System.Text.StringBuilder builder, string source
97111

98112
static string GenerateMultilineDiff(string left, string right)
99113
{
100-
var differ = new Differ();
101-
var diffResult = differ.CreateLineDiffs(left, right, ignoreWhitespace: false);
114+
var diffBuilder = new InlineDiffBuilder(new Differ());
115+
var diff = diffBuilder.BuildDiffModel(left, right, ignoreWhitespace: false);
102116

103117
var result = new List<string>();
104-
var leftLines = left.Split('\n');
105-
var rightLines = right.Split('\n');
118+
result.Add(" Diff:");
106119

107-
foreach (var block in diffResult.DiffBlocks)
120+
foreach (var line in diff.Lines)
108121
{
109-
AppendDeletedLines(result, leftLines, block.DeleteStartA, block.DeleteCountA);
110-
AppendInsertedLines(result, rightLines, block.InsertStartB, block.InsertCountB);
122+
switch (line.Type)
123+
{
124+
case ChangeType.Unchanged:
125+
result.Add(line.Text);
126+
break;
127+
case ChangeType.Deleted:
128+
result.Add($"- {line.Text}");
129+
break;
130+
case ChangeType.Inserted:
131+
result.Add($"+ {line.Text}");
132+
break;
133+
case ChangeType.Modified:
134+
result.Add($"~ {line.Text}");
135+
break;
136+
}
111137
}
112138

113-
if (result.Count == 0)
114-
result.Add(" (No specific line differences found)");
139+
if (result.Count == 1)
140+
result.Add("(No specific line differences found)");
115141

116142
TruncateResultIfNeeded(result);
117143
return string.Join("\n", result);
118144
}
119145

120-
static void AppendDeletedLines(List<string> output, string[] lines, int start, int count)
121-
{
122-
for (var i = 0; i < count; i++)
123-
{
124-
var lineIndex = start + i;
125-
if (lineIndex < lines.Length)
126-
output.Add($" - {lines[lineIndex]}");
127-
}
128-
}
129-
130-
static void AppendInsertedLines(List<string> output, string[] lines, int start, int count)
131-
{
132-
for (var i = 0; i < count; i++)
133-
{
134-
var lineIndex = start + i;
135-
if (lineIndex < lines.Length)
136-
output.Add($" + {lines[lineIndex]}");
137-
}
138-
}
139-
140146
static void TruncateResultIfNeeded(List<string> result)
141147
{
142148
if (result.Count <= MaxDiffLines) return;

src/SharpAssert.Tests/StringComparisonFixture.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,26 @@ public void Should_handle_multiline_strings()
2525
{
2626
var actual = "line1\nline2\nline3";
2727
var expected = "line1\nMODIFIED\nline3";
28-
28+
2929
AssertExpressionThrows(
3030
() => actual == expected,
3131
"actual == expected",
3232
"StringComparisonFixture.cs",
3333
30,
3434
"*Assertion failed: actual == expected*" +
35-
"*Left: \"line1*line2*line3\"*" +
36-
"*Right: \"line1*MODIFIED*line3\"*" +
35+
"*Left:*" +
36+
"*line1*" +
37+
"*line2*" +
38+
"*line3*" +
39+
"*Right:*" +
40+
"*line1*" +
41+
"*MODIFIED*" +
42+
"*line3*" +
43+
"*Diff:*" +
44+
"*line1*" +
3745
"*- line2*" +
38-
"*+ MODIFIED*");
46+
"*+ MODIFIED*" +
47+
"*line3*");
3948
}
4049

4150
[Test]

0 commit comments

Comments
 (0)