From 612bab324979f963449016c07f1f13df4bd72b48 Mon Sep 17 00:00:00 2001 From: buyaa-n Date: Thu, 18 Jul 2019 10:59:35 -0700 Subject: [PATCH] Code coverage for JsonWriterHelper JsonWriter (dotnet/corefx#39407) * Code coverage for JsonWriterHelper JsonWriter * Applying feedback * Some updates was not saved for previous commit * Remove not needed row, formatting * Removing tests not needed anymore * Applying feedback Commit migrated from https://github.com/dotnet/corefx/commit/4cbad86c4e6aeaa135b9a569dbde9151180c40f5 --- .../tests/JsonElementWriteTests.cs | 48 ++++++++- .../System.Text.Json/tests/Utf8JsonWriterTests.cs | 110 +++++++-------------- 2 files changed, 81 insertions(+), 77 deletions(-) diff --git a/src/libraries/System.Text.Json/tests/JsonElementWriteTests.cs b/src/libraries/System.Text.Json/tests/JsonElementWriteTests.cs index 4533079..0bb5674 100644 --- a/src/libraries/System.Text.Json/tests/JsonElementWriteTests.cs +++ b/src/libraries/System.Text.Json/tests/JsonElementWriteTests.cs @@ -4,6 +4,7 @@ using Xunit; using System.Buffers; +using Newtonsoft.Json; using System.IO; namespace System.Text.Json.Tests @@ -41,6 +42,7 @@ namespace System.Text.Json.Tests } [Theory] + [InlineData("12E-3", false)] [InlineData("1e6", false)] [InlineData("1e6", true)] [InlineData("1e+6", false)] @@ -59,6 +61,7 @@ namespace System.Text.Json.Tests } [Theory] + [InlineData("1.2E+3", false)] [InlineData("5.012e-20", false)] [InlineData("5.012e-20", true)] [InlineData("5.012e20", false)] @@ -523,6 +526,41 @@ null, "{\"m\\u00eal\\u00e9e\":1e6}"); } + [Fact] + public static void WriteValueSurrogatesEscapeString() + { + string unicodeString = "\uD800\uDC00\uD803\uDE6D \uD834\uDD1E\uDBFF\uDFFF"; + string json = $"[\"{unicodeString}\"]"; + var buffer = new ArrayBufferWriter(1024); + string expectedStr = GetEscapedExpectedString(unicodeString, StringEscapeHandling.EscapeNonAscii); + + using (JsonDocument doc = JsonDocument.Parse(json, s_options)) + { + JsonElement target = doc.RootElement[0]; + + using (var writer = new Utf8JsonWriter(buffer)) + { + target.WriteTo(writer); + writer.Flush(); + } + AssertContents(expectedStr, buffer); + } + } + + private static string GetEscapedExpectedString(string value, StringEscapeHandling escaping) + { + using (TextWriter stringWriter = new StringWriter()) + using (var json = new JsonTextWriter(stringWriter) + { + StringEscapeHandling = escaping + }) + { + json.WriteValue(value); + json.Flush(); + return stringWriter.ToString(); + } + } + [Theory] [InlineData(false)] [InlineData(true)] @@ -1060,11 +1098,11 @@ null, Indented = indented, }; - var writer = new Utf8JsonWriter(buffer, options); - - target.WriteTo(writer); - writer.Flush(); - + using (var writer = new Utf8JsonWriter(buffer, options)) + { + target.WriteTo(writer); + writer.Flush(); + } AssertContents(jsonOut ?? jsonIn, buffer); } } diff --git a/src/libraries/System.Text.Json/tests/Utf8JsonWriterTests.cs b/src/libraries/System.Text.Json/tests/Utf8JsonWriterTests.cs index c6ab641..294ab2c 100644 --- a/src/libraries/System.Text.Json/tests/Utf8JsonWriterTests.cs +++ b/src/libraries/System.Text.Json/tests/Utf8JsonWriterTests.cs @@ -2204,64 +2204,21 @@ namespace System.Text.Json.Tests } [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public void WriteBase64String(bool formatted, bool skipValidation) - { - string propertyName = "message"; - byte[] value = { 1, 2, 3, 4, 5 }; - string expectedStr = GetBase64ExpectedString(prettyPrint: formatted, propertyName, value); - - JsonEncodedText encodedPropertyName = JsonEncodedText.Encode(propertyName); - - byte[] utf8PropertyName = Encoding.UTF8.GetBytes("message"); - - var options = new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation }; - - for (int i = 0; i < 4; i++) - { - var output = new ArrayBufferWriter(32); - var jsonUtf8 = new Utf8JsonWriter(output, options); - - jsonUtf8.WriteStartObject(); - - switch (i) - { - case 0: - jsonUtf8.WriteBase64String(propertyName, value); - jsonUtf8.WriteBase64String(propertyName, value); - break; - case 1: - jsonUtf8.WriteBase64String(propertyName.AsSpan(), value); - jsonUtf8.WriteBase64String(propertyName.AsSpan(), value); - break; - case 2: - jsonUtf8.WriteBase64String(utf8PropertyName, value); - jsonUtf8.WriteBase64String(utf8PropertyName, value); - break; - case 3: - jsonUtf8.WriteBase64String(encodedPropertyName, value); - jsonUtf8.WriteBase64String(encodedPropertyName, value); - break; - } - - jsonUtf8.WriteEndObject(); - jsonUtf8.Flush(); - - AssertContents(expectedStr, output); - } - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public void WriteBase64StringEscaped(bool formatted, bool skipValidation) - { - string propertyName = "mess>(12); + using var jsonUtf8 = new Utf8JsonWriter(output); + + Assert.Throws(() => jsonUtf8.WriteString(propertyName, value)); + } + private const string InvalidUtf8 = "\"\\uFFFD(\""; private const string ValidUtf8 = "\"\\u00F1\""; @@ -5471,23 +5439,21 @@ namespace System.Text.Json.Tests private static string GetEscapedExpectedString(bool prettyPrint, string propertyName, string value, StringEscapeHandling escaping, bool escape = true) { - var ms = new MemoryStream(); - TextWriter streamWriter = new StreamWriter(ms, new UTF8Encoding(false), 1024, true); - - var json = new JsonTextWriter(streamWriter) + using (TextWriter stringWriter = new StringWriter()) + using (var json = new JsonTextWriter(stringWriter) { Formatting = prettyPrint ? Formatting.Indented : Formatting.None, StringEscapeHandling = escaping - }; - - json.WriteStartObject(); - json.WritePropertyName(propertyName, escape); - json.WriteValue(value); - json.WriteEnd(); - - json.Flush(); + }) + { + json.WriteStartObject(); + json.WritePropertyName(propertyName, escape); + json.WriteValue(value); + json.WriteEnd(); - return Encoding.UTF8.GetString(ms.ToArray()); + json.Flush(); + return stringWriter.ToString(); + } } private static string GetCustomExpectedString(bool prettyPrint) -- 2.7.4