From 0311766a56d45bc02bbb2053531651e1bd85da7a Mon Sep 17 00:00:00 2001 From: Maneet Arvind Mehta Date: Wed, 14 Oct 2026 01:39:35 +0800 Subject: [PATCH] Add ToString override to DiffPiece Shows position, sub-piece count, change type and text, so a DiffPaneModel's Lines are readable in the debugger without expanding each piece. Null-safe for Position, SubPieces and Text. Fixes #84 --- DiffPlex/DiffBuilder/Model/DiffPiece.cs | 7 +++++ Facts.DiffPlex/DiffPieceFacts.cs | 38 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 Facts.DiffPlex/DiffPieceFacts.cs diff --git a/DiffPlex/DiffBuilder/Model/DiffPiece.cs b/DiffPlex/DiffBuilder/Model/DiffPiece.cs index 5dc5f4f9..39dac7aa 100644 --- a/DiffPlex/DiffBuilder/Model/DiffPiece.cs +++ b/DiffPlex/DiffBuilder/Model/DiffPiece.cs @@ -55,6 +55,13 @@ public override int GetHashCode() return hashCode; } + public override string ToString() + { + var position = Position?.ToString() ?? "-"; + var subPieceCount = SubPieces?.Count ?? 0; + return $"Pos:{position,-6} Subs:{subPieceCount,-6} {Type,-16} {Text}"; + } + private bool SubPiecesEqual(DiffPiece other) { if (SubPieces is null) diff --git a/Facts.DiffPlex/DiffPieceFacts.cs b/Facts.DiffPlex/DiffPieceFacts.cs new file mode 100644 index 00000000..1604c7b6 --- /dev/null +++ b/Facts.DiffPlex/DiffPieceFacts.cs @@ -0,0 +1,38 @@ +using System.Collections.Generic; +using DiffPlex.DiffBuilder.Model; +using Xunit; + +namespace Facts.DiffPlex +{ + public class DiffPieceFacts + { + public class ToStringMethod + { + [Fact] + public void Will_include_position_sub_piece_count_type_and_text() + { + var piece = new DiffPiece("hello", ChangeType.Modified, 3); + piece.SubPieces.Add(new DiffPiece("h", ChangeType.Unchanged, 1)); + piece.SubPieces.Add(new DiffPiece("ello", ChangeType.Inserted, 2)); + + Assert.Equal("Pos:3 Subs:2 Modified hello", piece.ToString()); + } + + [Fact] + public void Will_show_dash_when_position_is_null() + { + var piece = new DiffPiece("text", ChangeType.Inserted); + + Assert.Equal("Pos:- Subs:0 Inserted text", piece.ToString()); + } + + [Fact] + public void Will_not_throw_when_sub_pieces_and_text_are_null() + { + var piece = new DiffPiece { SubPieces = null }; + + Assert.Equal("Pos:- Subs:0 Imaginary ", piece.ToString()); + } + } + } +}