Skip to content

Commit

Permalink
Merge pull request #1649 from Miepee/ubnit
Browse files Browse the repository at this point in the history
Add unit tests for UTStrings toString
  • Loading branch information
BenjaminUrquhart authored May 1, 2024
2 parents c7904ef + ad57eba commit caaa765
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions UndertaleModLib/Models/UndertaleString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public string ToString(bool isGMS2)
return "\"" + Content.Replace("\\", "\\\\").Replace("\r", "\\r").Replace("\n", "\\n").Replace("\"", "\\\"") + "\"";

// Handle GM:S 1's lack of escaping
// Yes, in GM:S 1 you cannot escape quotation marks. You are expected to concatenate
// single-quote strings with double-quote strings.
string res = Content;
bool front, back;
if (res.StartsWith('"'))
Expand Down
49 changes: 49 additions & 0 deletions UndertaleModLibTests/Models/StringTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,53 @@ public void TestUnserialize(byte[] input, string expected)

Assert.Equal(utString.Content, expected);
}

[Fact]
public void TestToStringWithNull()
{
var s = new UndertaleString();
var result = s.ToString();
Assert.Equal("\"null\"", result);
}

[Theory]
[InlineData("", "\"\"")]
[InlineData("\"", "\"\\\"\"")]
[InlineData("Hi", "\"Hi\"")]
[InlineData("This is a string", "\"This is a string\"")]
[InlineData("\"A quote\"", "\"\\\"A quote\\\"\"")]
[InlineData("This string has \"quotes\"", "\"This string has \\\"quotes\\\"\"")]
[InlineData("This string has \n newline", "\"This string has \\n newline\"")]
[InlineData("This string has \r also newline", "\"This string has \\r also newline\"")]
[InlineData("This string has \\ backslashes", "\"This string has \\\\ backslashes\"")]
[InlineData("This \"string\" has \n all \r kinds of \\ stuff", "\"This \\\"string\\\" has \\n all \\r kinds of \\\\ stuff\"")]
[InlineData("Some cool characters: { } = () %$! ß? ´'`@", "\"Some cool characters: { } = () %$! ß? ´'`@\"")]
public void TestToStringWithGMS2(string content, string expected)
{
var s = new UndertaleString(content);
var result = s.ToString(true);
Assert.Equal(expected, result);
}

[Theory]
[InlineData("", "\"\"")]
[InlineData("\"", "'\"'")]
[InlineData("\"This starts with quotes", "'\"' + \"This starts with quotes\"")]
[InlineData("This ends with quotes\"", "\"This ends with quotes\" + '\"'")]
[InlineData("\"This has quotes in start and end\"", "'\"' + \"This has quotes in start and end\" + '\"'")]
[InlineData("This has quotes \" in middle", "\"This has quotes \" + '\"' + \" in middle\"")]
[InlineData("\"This starts and has \" quotes in middle", "'\"' + \"This starts and has \" + '\"' + \" quotes in middle\"")]
[InlineData("This ends and has \" quotes in middle\"", "\"This ends and has \" + '\"' + \" quotes in middle\" + '\"'")]
[InlineData("\"This starts, has \" quotes in middle and ends\"", "'\"' + \"This starts, has \" + '\"' + \" quotes in middle and ends\" + '\"'")]
[InlineData("Hi", "\"Hi\"")]
[InlineData("This is a string", "\"This is a string\"")]
[InlineData("This string has \n newline", "\"This string has \n newline\"")]
[InlineData("This \"string\" has \n all \r kinds of \\ stuff", "\"This \" + '\"' + \"string\" + '\"' + \" has \n all \r kinds of \\ stuff\"")]
[InlineData("Some cool characters: { } = () %$! ß? ´'`@", "\"Some cool characters: { } = () %$! ß? ´'`@\"")]
public void TestToStringWithGMS1(string content, string expected)
{
var s = new UndertaleString(content);
var result = s.ToString(false);
Assert.Equal(expected, result);
}
}

0 comments on commit caaa765

Please sign in to comment.