Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle unicode and e-notation differences between S.T.J and J.N tests #35042

Merged
merged 10 commits into from
Nov 17, 2020
20 changes: 10 additions & 10 deletions src/libraries/System.Text.Json/tests/JsonTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ public static string GetCompactString(string jsonString)
}
}

public static void AssertContents(string expectedValue, ArrayBufferWriter<byte> buffer)
public static void AssertContents(string expectedValue, ArrayBufferWriter<byte> buffer, bool skipSpecialRules = false)
{
string value = Encoding.UTF8.GetString(
buffer.WrittenSpan
Expand All @@ -717,17 +717,17 @@ public static void AssertContents(string expectedValue, ArrayBufferWriter<byte>
#endif
);

AssertContentsAgainstJsonNet(expectedValue, value);
AssertContentsAgainstJsonNet(expectedValue, value, skipSpecialRules);
}

public static void AssertContents(string expectedValue, MemoryStream stream)
public static void AssertContents(string expectedValue, MemoryStream stream, bool skipSpecialRules = false)
{
string value = Encoding.UTF8.GetString(stream.ToArray());

AssertContentsAgainstJsonNet(expectedValue, value);
AssertContentsAgainstJsonNet(expectedValue, value, skipSpecialRules);
}

public static void AssertContentsNotEqual(string expectedValue, ArrayBufferWriter<byte> buffer)
public static void AssertContentsNotEqual(string expectedValue, ArrayBufferWriter<byte> buffer, bool skipSpecialRules = false)
{
string value = Encoding.UTF8.GetString(
buffer.WrittenSpan
Expand All @@ -736,17 +736,17 @@ public static void AssertContentsNotEqual(string expectedValue, ArrayBufferWrite
#endif
);

AssertContentsNotEqualAgainstJsonNet(expectedValue, value);
AssertContentsNotEqualAgainstJsonNet(expectedValue, value, skipSpecialRules);
}

public static void AssertContentsAgainstJsonNet(string expectedValue, string value)
public static void AssertContentsAgainstJsonNet(string expectedValue, string value, bool skipSpecialRules)
{
Assert.Equal(expectedValue.NormalizeToJsonNetFormat(), value.NormalizeToJsonNetFormat());
Assert.Equal(expectedValue.NormalizeToJsonNetFormat(skipSpecialRules), value.NormalizeToJsonNetFormat(skipSpecialRules));
}

public static void AssertContentsNotEqualAgainstJsonNet(string expectedValue, string value)
public static void AssertContentsNotEqualAgainstJsonNet(string expectedValue, string value, bool skipSpecialRules)
{
Assert.NotEqual(expectedValue.NormalizeToJsonNetFormat(), value.NormalizeToJsonNetFormat());
Assert.NotEqual(expectedValue.NormalizeToJsonNetFormat(skipSpecialRules), value.NormalizeToJsonNetFormat(skipSpecialRules));
}

public delegate void AssertThrowsActionUtf8JsonReader(Utf8JsonReader json);
Expand Down
68 changes: 52 additions & 16 deletions src/libraries/System.Text.Json/tests/Utf8JsonWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5368,6 +5368,7 @@ public void WriteDoubleValue(bool formatted, bool skipValidation, double value)
}

[Theory]
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
[InlineData(true, true, "message")]
[InlineData(true, false, "message")]
[InlineData(false, true, "message")]
Expand Down Expand Up @@ -5589,8 +5590,7 @@ public void WriteNumbers(bool formatted, bool skipValidation, string keyString)
jsonUtf8.WriteEndObject();
jsonUtf8.Flush();

// TODO: https://github.com/dotnet/runtime/issues/32350
// JsonTestHelper.AssertContents(expectedStr, output);
JsonTestHelper.AssertContents(expectedStr, output);
}
}

Expand Down Expand Up @@ -5873,7 +5873,7 @@ private static void WriteStringHelper(JsonWriterOptions options, string keyStrin
}
else
{
JsonTestHelper.AssertContentsNotEqual(expectedStr, output);
JsonTestHelper.AssertContentsNotEqual(expectedStr, output, skipSpecialRules: true);
}
}
}
Expand Down Expand Up @@ -7690,33 +7690,69 @@ public static IEnumerable<object[]> JsonEncodedTextStrings
public static class WriterHelpers
{
// Normalize comparisons against Json.NET.
// Includes uppercasing the \u escaped hex characters and escaping forward slash to "\/" instead of "\u002f".
public static string NormalizeToJsonNetFormat(this string json)
// The following is performed unless skipSpecialRules is true:
// * Uppercases the \u escaped hex characters.
// * Escapes forward slash, greater than, and less than.
// * Ignores ".0" for decimal values.
public static string NormalizeToJsonNetFormat(this string json, bool skipSpecialRules)
{
var sb = new StringBuilder(json.Length);
int i = 0;
while (i < json.Length)
{
if (json[i] == '\\')
if (!skipSpecialRules)
{
sb.Append(json[i++]);

if (i < json.Length - 1 && json[i] == 'u')
if (json[i] == '\\')
{
sb.Append(json[i++]);

if (i < json.Length - 4)
if (i < json.Length - 1 && json[i] == 'u')
{
sb.Append(json[i++]);

if (i < json.Length - 4)
{
string temp = json.Substring(i, 4).ToLowerInvariant();
sb.Append(temp);
i += 4;
}
}
if (i < json.Length - 1 && json[i] == '/')
{
string temp = json.Substring(i, 4).ToLowerInvariant();
sb.Append(temp);
i += 4;
// Convert / to u002f
i++;
sb.Append("u002f");
}
}
if (i < json.Length - 1 && json[i] == '/')
// Convert > to \u003e
else if (json[i] == '>')
{
// Convert / to u002f
i++;
sb.Append("u002f");
sb.Append("\\u003e");
}
// Convert < to \u003c
else if (json[i] == '<')
{
i++;
sb.Append("\\u003c");
}
// Remove .0
else if (json[i] == '.' && json[i + 1] == '0')
{
// Verify that token after .0 is a delimiter.
if (json[i + 2] == ',' || json[i + 2] == ']' || json[i + 2] == '}' ||
json[i + 2] == ' ' || json[i + 2] == '\r' || json[i + 2] == '\n')
{
i += 2;
}
else
{
sb.Append(json[i++]);
}
}
else
{
sb.Append(json[i++]);
}
}
else
Expand Down