Add tests for TextFormatter StripCRLF and ReplaceCRLFWithSpace

Also made the helper methods internal so they can be accessed from the test project.
This commit is contained in:
Tonttu
2025-03-14 21:29:56 +02:00
committed by Tig
parent 384cefcdf1
commit c4502b0741
2 changed files with 75 additions and 2 deletions

View File

@@ -1185,7 +1185,7 @@ public class TextFormatter
}
// TODO: Move to StringExtensions?
private static string StripCRLF (string str, bool keepNewLine = false)
internal static string StripCRLF (string str, bool keepNewLine = false)
{
List<Rune> runes = str.ToRuneList ();
@@ -1229,7 +1229,7 @@ public class TextFormatter
}
// TODO: Move to StringExtensions?
private static string ReplaceCRLFWithSpace (string str)
internal static string ReplaceCRLFWithSpace (string str)
{
List<Rune> runes = str.ToRuneList ();

View File

@@ -2886,4 +2886,77 @@ public class TextFormatterTests
Assert.Equal (resultLines, wrappedLines);
}
[Theory]
[InlineData ("No crlf", "No crlf")]
// CRLF
[InlineData ("\r\nThis has crlf in the beginning", "This has crlf in the beginning")]
[InlineData ("This has crlf\r\nin the middle", "This has crlfin the middle")]
[InlineData ("This has crlf in the end\r\n", "This has crlf in the end")]
// LFCR
[InlineData ("\n\rThis has lfcr in the beginning", "\rThis has lfcr in the beginning")]
[InlineData ("This has lfcr\n\rin the middle", "This has lfcr\rin the middle")]
[InlineData ("This has lfcr in the end\n\r", "This has lfcr in the end\r")]
// CR
[InlineData ("\rThis has cr in the beginning", "This has cr in the beginning")]
[InlineData ("This has cr\rin the middle", "This has crin the middle")]
[InlineData ("This has cr in the end\r", "This has cr in the end")]
// LF
[InlineData ("\nThis has lf in the beginning", "This has lf in the beginning")]
[InlineData ("This has lf\nin the middle", "This has lfin the middle")]
[InlineData ("This has lf in the end\n", "This has lf in the end")]
public void StripCRLF_RemovesCrLf (string input, string expected)
{
string actual = TextFormatter.StripCRLF(input, keepNewLine: false);
Assert.Equal (expected, actual);
}
[Theory]
[InlineData ("No crlf", "No crlf")]
// CRLF
[InlineData ("\r\nThis has crlf in the beginning", "\nThis has crlf in the beginning")]
[InlineData ("This has crlf\r\nin the middle", "This has crlf\nin the middle")]
[InlineData ("This has crlf in the end\r\n", "This has crlf in the end\n")]
// LFCR
[InlineData ("\n\rThis has lfcr in the beginning", "\n\rThis has lfcr in the beginning")]
[InlineData ("This has lfcr\n\rin the middle", "This has lfcr\n\rin the middle")]
[InlineData ("This has lfcr in the end\n\r", "This has lfcr in the end\n\r")]
// CR
[InlineData ("\rThis has cr in the beginning", "\rThis has cr in the beginning")]
[InlineData ("This has cr\rin the middle", "This has cr\rin the middle")]
[InlineData ("This has cr in the end\r", "This has cr in the end\r")]
// LF
[InlineData ("\nThis has lf in the beginning", "\nThis has lf in the beginning")]
[InlineData ("This has lf\nin the middle", "This has lf\nin the middle")]
[InlineData ("This has lf in the end\n", "This has lf in the end\n")]
public void StripCRLF_KeepNewLine_RemovesCarriageReturnFromCrLf (string input, string expected)
{
string actual = TextFormatter.StripCRLF(input, keepNewLine: true);
Assert.Equal (expected, actual);
}
[Theory]
[InlineData ("No crlf", "No crlf")]
// CRLF
[InlineData ("\r\nThis has crlf in the beginning", " This has crlf in the beginning")]
[InlineData ("This has crlf\r\nin the middle", "This has crlf in the middle")]
[InlineData ("This has crlf in the end\r\n", "This has crlf in the end ")]
// LFCR
[InlineData ("\n\rThis has lfcr in the beginning", " This has lfcr in the beginning")]
[InlineData ("This has lfcr\n\rin the middle", "This has lfcr in the middle")]
[InlineData ("This has lfcr in the end\n\r", "This has lfcr in the end ")]
// CR
[InlineData ("\rThis has cr in the beginning", " This has cr in the beginning")]
[InlineData ("This has cr\rin the middle", "This has cr in the middle")]
[InlineData ("This has cr in the end\r", "This has cr in the end ")]
// LF
[InlineData ("\nThis has lf in the beginning", " This has lf in the beginning")]
[InlineData ("This has lf\nin the middle", "This has lf in the middle")]
[InlineData ("This has lf in the end\n", "This has lf in the end ")]
public void ReplaceCRLFWithSpace_ReplacesCrLfWithSpace (string input, string expected)
{
string actual = TextFormatter.ReplaceCRLFWithSpace(input);
Assert.Equal (expected, actual);
}
}