diff --git a/readme.md b/readme.md index c0e0eba..3192e71 100644 --- a/readme.md +++ b/readme.md @@ -169,7 +169,7 @@ public Task TestLevelUsage() return Verify(target, settings); } ``` -snippet source | anchor +snippet source | anchor Or Fluently @@ -185,5 +185,5 @@ public Task TestLevelUsageFluent() .UseDiffPlex(); } ``` -snippet source | anchor +snippet source | anchor diff --git a/src/Directory.Build.props b/src/Directory.Build.props index ec7c5a6..b660f4e 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -2,7 +2,7 @@ CS1591;CS0649;NU1608;NU1109 - 3.3.0 + 3.3.1 1.0.0 preview DiffPlex, Verify diff --git a/src/Tests/Tests.CompactEmptyReceived.verified.txt b/src/Tests/Tests.CompactEmptyReceived.verified.txt new file mode 100644 index 0000000..2ad3585 --- /dev/null +++ b/src/Tests/Tests.CompactEmptyReceived.verified.txt @@ -0,0 +1,23 @@ +{ + Type: VerifyException, + Message: +Directory: {ProjectDirectory} +NotEqual: + - Received: Tests.CompactEmptyReceivedFake.received.txt + Verified: Tests.CompactEmptyReceivedFake.verified.txt + +FileContent: + +NotEqual: + +Received: Tests.CompactEmptyReceivedFake.received.txt +Verified: Tests.CompactEmptyReceivedFake.verified.txt +Compare Result: + [BOF] +- First +- Second +- Third + [EOF] + + +} \ No newline at end of file diff --git a/src/Tests/Tests.CompactEmptyReceivedFake.verified.txt b/src/Tests/Tests.CompactEmptyReceivedFake.verified.txt new file mode 100644 index 0000000..56b892c --- /dev/null +++ b/src/Tests/Tests.CompactEmptyReceivedFake.verified.txt @@ -0,0 +1,3 @@ +First +Second +Third \ No newline at end of file diff --git a/src/Tests/Tests.cs b/src/Tests/Tests.cs index 7ce40a1..827ffad 100644 --- a/src/Tests/Tests.cs +++ b/src/Tests/Tests.cs @@ -79,6 +79,28 @@ public Task AtTestLevelCompact() settings)); } + [Test] + public Task CompactEmptyReceived() + { + var settings = new VerifySettings(); + settings.UseMethodName("CompactEmptyReceivedFake"); + settings.DisableDiff(); + settings.UseDiffPlex(OutputType.Compact); + // Scrub every line so the received content is empty. The resulting diff is all + // deletions (every line has a null Position), which used to throw + // ArgumentOutOfRangeException in the Compact formatter. + settings.ScrubLines(_ => true); + + return ThrowsTask(() => + Verify( + """ + First + Second + Third + """, + settings)); + } + [Test] public Task AtTestLevelMinimal() { diff --git a/src/Verify.DiffPlex/VerifyDiffPlex.cs b/src/Verify.DiffPlex/VerifyDiffPlex.cs index ab0f267..be10781 100644 --- a/src/Verify.DiffPlex/VerifyDiffPlex.cs +++ b/src/Verify.DiffPlex/VerifyDiffPlex.cs @@ -101,8 +101,11 @@ static StringBuilder CompactCompare(string received, string verified) var diff = InlineDiffBuilder.Diff(verified, received); var builder = new StringBuilder(); - // ReSharper disable once RedundantSuppressNullableWarningExpression - var prefixLength = diff.Lines.Max(_ => _.Position).ToString()!.Length; + // Position is null for deleted lines. When the received side is empty the diff is + // all deletions, so Max returns null; fall back to 0 (a single-digit width) to avoid + // a negative spacePrefix length. + var maxPosition = diff.Lines.Max(_ => _.Position) ?? 0; + var prefixLength = maxPosition.ToString().Length; var spacePrefix = new string(' ', prefixLength - 1); static bool IsChanged(DiffPiece? line) => line?.Type is ChangeType.Inserted or ChangeType.Deleted;