Skip to content

Commit

Permalink
Fixing bug with string differ and simplifying the logic for ending wi…
Browse files Browse the repository at this point in the history
…th a single line. (#1070)
  • Loading branch information
belav authored Dec 14, 2023
1 parent 182e890 commit 510a10b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 48 deletions.
95 changes: 64 additions & 31 deletions Src/CSharpier.Tests/Utilities/StringDifferTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ 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();
}

[Test]
public void PrintDifference_Should_Print_Visible_Spaces()
{
var result = PrintDifference("value", "value ");
var result = AddNewLinesAndFindDifference("value", "value ");

result
.Should()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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);
Expand Down
24 changes: 7 additions & 17 deletions Src/CSharpier/Utilities/StringDiffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
}

Expand Down Expand Up @@ -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--)
Expand Down

0 comments on commit 510a10b

Please sign in to comment.