From 510a10bf3c904ff0885d0b1062b035430eee2998 Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Wed, 13 Dec 2023 20:49:14 -0600 Subject: [PATCH] Fixing bug with string differ and simplifying the logic for ending with a single line. (#1070) --- .../Utilities/StringDifferTests.cs | 95 +++++++++++++------ Src/CSharpier/Utilities/StringDiffer.cs | 24 ++--- 2 files changed, 71 insertions(+), 48 deletions(-) diff --git a/Src/CSharpier.Tests/Utilities/StringDifferTests.cs b/Src/CSharpier.Tests/Utilities/StringDifferTests.cs index 483a7939b..39c945349 100644 --- a/Src/CSharpier.Tests/Utilities/StringDifferTests.cs +++ b/Src/CSharpier.Tests/Utilities/StringDifferTests.cs @@ -11,7 +11,7 @@ public class StringDifferTests [Test] public void PrintDifference_Should_Not_Print_Anything_If_Values_Are_Identical() { - var result = PrintDifference("value", "value"); + var result = AddNewLinesAndFindDifference("value", "value"); result.Should().BeNullOrEmpty(); } @@ -19,7 +19,7 @@ public void PrintDifference_Should_Not_Print_Anything_If_Values_Are_Identical() [Test] public void PrintDifference_Should_Print_Visible_Spaces() { - var result = PrintDifference("value", "value "); + var result = AddNewLinesAndFindDifference("value", "value "); result .Should() @@ -35,7 +35,7 @@ public void PrintDifference_Should_Print_Visible_Spaces() [Test] public void PrintDifference_Should_Print_Visible_Tabs() { - var result = PrintDifference("value", "value\t"); + var result = AddNewLinesAndFindDifference("value", "value\t"); result .Should() @@ -51,7 +51,17 @@ public void PrintDifference_Should_Print_Visible_Tabs() [Test] public void PrintDifference_Should_Print_LineEnding_Message() { - var result = PrintDifference("lineEndings\r\ndiffer", "lineEndings\ndiffer"); + var result = AddNewLinesAndFindDifference("lineEndings\r\ndiffer", "lineEndings\ndiffer"); + + result + .Should() + .Be("The file contained different line endings than formatting it would result in."); + } + + [Test] + public void PrintDifference_Should_Print_LineEnding_Message2() + { + var result = PrintDifference("lineEndings\r\ndiffer\r\n", "lineEndings\ndiffer\n"); result .Should() @@ -61,7 +71,7 @@ public void PrintDifference_Should_Print_LineEnding_Message() [Test] public void PrintDifference_Should_Print_Single_Line_Difference() { - var result = PrintDifference("one", "two"); + var result = AddNewLinesAndFindDifference("one", "two"); result .Should() @@ -80,11 +90,13 @@ public void PrintDifference_Should_Print_Multi_Line_Difference() var result = PrintDifference( @"one two -four", +four +", @"one two three -four" +four +" ); result @@ -110,13 +122,15 @@ public void PrintDifference_Should_Show_Extra_Whitespace_On_Empty_Line() private string field1; private string field2; -}", +} +", @"public class ClassName { private string field1; private string field2; -}" +} +" ); result @@ -141,11 +155,13 @@ public void PrintDifference_Should_Show_Extra_Whitespace_At_End_Of_Line() @"public class ClassName { private string field1; -}", +} +", @"public class ClassName { private string field1; -}" +} +" ); result @@ -166,7 +182,10 @@ public void PrintDifference_Should_Show_Extra_Whitespace_At_End_Of_Line() [Test] public void PrintDifference_Should_Not_Show_Whitespace_In_Middle_Of_Line() { - var result = PrintDifference("public class ClassName { }", "public class ClassName { }"); + var result = AddNewLinesAndFindDifference( + "public class ClassName { }", + "public class ClassName { }" + ); result .Should() @@ -182,7 +201,10 @@ public class ClassName { } [Test] public void PrintDifference_Should_Show_Extra_Whitespace_After_Line() { - var result = PrintDifference("public class ClassName { }", "public class ClassName { } "); + var result = AddNewLinesAndFindDifference( + "public class ClassName { }", + "public class ClassName { } " + ); result .Should() @@ -201,11 +223,13 @@ public void PrintDifference_Should_Print_Extra_Line_Difference() var result = PrintDifference( @"one two -three", +three +", @"one two three -four" +four +" ); result @@ -221,32 +245,41 @@ public void PrintDifference_Should_Print_Extra_Line_Difference() ); } - [Test] - public void PrintDifference_Should_Make_Extra_New_Line_Obvious() + [TestCase("\r\n")] + [TestCase("\n")] + public void PrintDifference_Should_Make_Extra_New_Line_Obvious(string lineEnding) { - var result = PrintDifference( - @"} -", - @"} - -" - ); + var result = PrintDifference($";{lineEnding}", $";{lineEnding}{lineEnding}"); result.Should().Be("The file did not end with a single newline."); } - [Test] - public void PrintDifference_Should_Make_Missing_New_Line_Obvious() + [TestCase("\r\n")] + [TestCase("\n")] + public void PrintDifference_Should_Make_Missing_New_Line_Obvious(string lineEnding) { - var result = PrintDifference( - @"} -", - @"}" - ); + var result = PrintDifference($";{lineEnding}", ";"); result.Should().Be("The file did not end with a single newline."); } + [TestCase("\r\n")] + [TestCase("\n")] + public void PrintDifference_Should_Pass_With_Proper_Line_Ending(string lineEnding) + { + var result = PrintDifference($";{lineEnding}", $";{lineEnding}"); + + result.Should().BeNullOrEmpty(); + } + + private static string AddNewLinesAndFindDifference(string expected, string actual) + { + return StringDiffer.PrintFirstDifference( + expected + Environment.NewLine, + actual + Environment.NewLine + ); + } + private static string PrintDifference(string expected, string actual) { return StringDiffer.PrintFirstDifference(expected, actual); diff --git a/Src/CSharpier/Utilities/StringDiffer.cs b/Src/CSharpier/Utilities/StringDiffer.cs index 0e48ccc42..fdac93629 100644 --- a/Src/CSharpier/Utilities/StringDiffer.cs +++ b/Src/CSharpier/Utilities/StringDiffer.cs @@ -15,24 +15,8 @@ public static string PrintFirstDifference(string expected, string actual) return string.Empty; } - var x = 1; - while (x <= expected.Length && x <= actual.Length) + if (!EndsWithExactlyOneNewline(actual)) { - var endOfExpected = expected[^x]; - var endOfActual = actual[^x]; - var expectedIsNewLine = endOfExpected is '\r' or '\n'; - var actualIsNewLine = endOfActual is '\r' or '\n'; - if (expectedIsNewLine && actualIsNewLine) - { - x++; - continue; - } - - if (!expectedIsNewLine && !actualIsNewLine) - { - break; - } - return "The file did not end with a single newline."; } @@ -112,6 +96,12 @@ public static string PrintFirstDifference(string expected, string actual) return "The file contained different line endings than formatting it would result in."; } + private static bool EndsWithExactlyOneNewline(string value) + { + return (!value.EndsWith("\r\n") && value.EndsWith("\n") && !value.EndsWith("\n\n")) + || (value.EndsWith("\r\n") && !value.EndsWith("\r\n\r\n")); + } + private static int FindIndexOfNonWhitespace(string input) { for (var x = input.Length - 1; x >= 0; x--)