diff --git a/Src/CSharpier.Tests/Utilities/StringDifferTests.cs b/Src/CSharpier.Tests/Utilities/StringDifferTests.cs index 0b7e601b0..483a7939b 100644 --- a/Src/CSharpier.Tests/Utilities/StringDifferTests.cs +++ b/Src/CSharpier.Tests/Utilities/StringDifferTests.cs @@ -101,6 +101,100 @@ public void PrintDifference_Should_Print_Multi_Line_Difference() ); } + [Test] + public void PrintDifference_Should_Show_Extra_Whitespace_On_Empty_Line() + { + var result = PrintDifference( + @"public class ClassName +{ + private string field1; + + private string field2; +}", + @"public class ClassName +{ + private string field1; + + private string field2; +}" + ); + + result + .Should() + .Be( + @"----------------------------- Expected: Around Line 4 ----------------------------- + private string field1; + + private string field2; +----------------------------- Actual: Around Line 4 ----------------------------- + private string field1; +···· + private string field2; +" + ); + } + + [Test] + public void PrintDifference_Should_Show_Extra_Whitespace_At_End_Of_Line() + { + var result = PrintDifference( + @"public class ClassName +{ + private string field1; +}", + @"public class ClassName +{ + private string field1; +}" + ); + + result + .Should() + .Be( + @"----------------------------- Expected: Around Line 3 ----------------------------- +{ + private string field1; +} +----------------------------- Actual: Around Line 3 ----------------------------- +{ + private string field1;···· +} +" + ); + } + + [Test] + public void PrintDifference_Should_Not_Show_Whitespace_In_Middle_Of_Line() + { + var result = PrintDifference("public class ClassName { }", "public class ClassName { }"); + + result + .Should() + .Be( + @"----------------------------- Expected: Around Line 1 ----------------------------- +public class ClassName { } +----------------------------- Actual: Around Line 1 ----------------------------- +public class ClassName { } +" + ); + } + + [Test] + public void PrintDifference_Should_Show_Extra_Whitespace_After_Line() + { + var result = PrintDifference("public class ClassName { }", "public class ClassName { } "); + + result + .Should() + .Be( + @"----------------------------- Expected: Around Line 1 ----------------------------- +public class ClassName { } +----------------------------- Actual: Around Line 1 ----------------------------- +public class ClassName { }· +" + ); + } + [Test] public void PrintDifference_Should_Print_Extra_Line_Difference() { diff --git a/Src/CSharpier/Utilities/StringDiffer.cs b/Src/CSharpier/Utilities/StringDiffer.cs index 7f7922855..0e48ccc42 100644 --- a/Src/CSharpier/Utilities/StringDiffer.cs +++ b/Src/CSharpier/Utilities/StringDiffer.cs @@ -62,13 +62,14 @@ public static string PrintFirstDifference(string expected, string actual) ); if (previousExpectedLine != null) { - stringBuilder.AppendLine(MakeWhiteSpaceVisible(previousExpectedLine)); + stringBuilder.AppendLine(previousExpectedLine); } - stringBuilder.AppendLine(MakeWhiteSpaceVisible(expectedLine)); + + stringBuilder.AppendLine(expectedLine); var nextExpectedLine = expectedReader.ReadLine(); if (nextExpectedLine != null) { - stringBuilder.AppendLine(MakeWhiteSpaceVisible(nextExpectedLine)); + stringBuilder.AppendLine(nextExpectedLine); } stringBuilder.AppendLine( @@ -76,13 +77,33 @@ public static string PrintFirstDifference(string expected, string actual) ); if (previousActualLine != null) { - stringBuilder.AppendLine(MakeWhiteSpaceVisible(previousActualLine)); + stringBuilder.AppendLine(previousActualLine); + } + + if (string.IsNullOrWhiteSpace(actualLine)) + { + stringBuilder.AppendLine(MakeWhiteSpaceVisible(actualLine)); } - stringBuilder.AppendLine(MakeWhiteSpaceVisible(actualLine)); + else + { + if (actualLine[^1] is ' ' or '\t') + { + var lastNonWhitespace = FindIndexOfNonWhitespace(actualLine); + stringBuilder.AppendLine( + actualLine[..lastNonWhitespace] + + MakeWhiteSpaceVisible(actualLine[lastNonWhitespace..]) + ); + } + else + { + stringBuilder.AppendLine(actualLine); + } + } + var nextActualLine = actualReader.ReadLine(); if (nextActualLine != null) { - stringBuilder.AppendLine(MakeWhiteSpaceVisible(nextActualLine)); + stringBuilder.AppendLine(nextActualLine); } return stringBuilder.ToString(); @@ -91,6 +112,19 @@ public static string PrintFirstDifference(string expected, string actual) return "The file contained different line endings than formatting it would result in."; } + private static int FindIndexOfNonWhitespace(string input) + { + for (var x = input.Length - 1; x >= 0; x--) + { + if (input[x] is not (' ' or '\t')) + { + return x; + } + } + + return -1; + } + private static string? MakeWhiteSpaceVisible(string? value) { return value?.Replace(' ', '·').Replace('\t', '→');