Skip to content

Commit

Permalink
Using UTF8Encoding instead of Encoding.Utf for reading as string
Browse files Browse the repository at this point in the history
  • Loading branch information
buyaa-n committed Apr 26, 2020
1 parent d2eb4c0 commit 2f04d5d
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/libraries/System.Private.Xml/tests/XmlWriter/DisposeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,21 +142,23 @@ public static async Task AsyncWriter_DisposeAsync_ShouldCall_FlushAsyncWriteAsyn
[Fact]
public static async Task XmlWriter_AsyncSyncResult_ShouldBeSame_AfterDispose_StreamWriter()
{
var settings = new XmlWriterSettings() { Async = true, Encoding = new UTF8Encoding(false) };
using (var stream1 = new MemoryStream())
using (var stream2 = new MemoryStream())
using (var stream3 = new MemoryStream())
{
using (var asyncWriter = XmlWriter.Create(stream1, new XmlWriterSettings() { Async = true }))
using (var asyncWriter = XmlWriter.Create(stream1, settings))
{
await asyncWriter.WriteStartDocumentAsync();
await asyncWriter.WriteStartElementAsync(string.Empty, "root", null);
await asyncWriter.WriteStartElementAsync(null, "test", null);
await asyncWriter.WriteAttributeStringAsync(string.Empty, "abc", string.Empty, "1");
await asyncWriter.WriteEndElementAsync();
await asyncWriter.WriteEndElementAsync();
Console.WriteLine(asyncWriter.Settings.Encoding);
}

await using (var asyncWriter = XmlWriter.Create(stream2, new XmlWriterSettings() { Async = true }))
await using (var asyncWriter = XmlWriter.Create(stream2, settings))
{
await asyncWriter.WriteStartDocumentAsync();
await asyncWriter.WriteStartElementAsync(string.Empty, "root", null);
Expand All @@ -166,7 +168,8 @@ public static async Task XmlWriter_AsyncSyncResult_ShouldBeSame_AfterDispose_Str
await asyncWriter.WriteEndElementAsync();
}

using (var syncWriter = XmlWriter.Create(stream3, new XmlWriterSettings() { Async = false }))
settings.Async = false;
using (var syncWriter = XmlWriter.Create(stream3, settings))
{
syncWriter.WriteStartDocument();
syncWriter.WriteStartElement(string.Empty, "root", null);
Expand All @@ -176,15 +179,17 @@ public static async Task XmlWriter_AsyncSyncResult_ShouldBeSame_AfterDispose_Str
syncWriter.WriteEndElement();
}

Assert.Equal(@"?<?xml version=""1.0"" encoding=""utf - 8""?><root><test abc=""1"" /></root>", Encoding.UTF8.GetString(stream1.GetBuffer()));
Assert.Equal(Encoding.UTF8.GetString(stream1.GetBuffer()), Encoding.UTF8.GetString(stream2.GetBuffer()));
Assert.Equal(Encoding.UTF8.GetString(stream2.GetBuffer()), Encoding.UTF8.GetString(stream3.GetBuffer()));

Assert.Equal(stream1.GetBuffer(), stream2.GetBuffer());
Assert.Equal(stream2.GetBuffer(), stream3.GetBuffer());

Assert.Equal(@"<?xml version=""1.0"" encoding=""utf-8""?><root><test abc=""1"" /></root>", readAsString(stream1.GetBuffer(), stream1.Length));
Assert.Equal(@"<?xml version=""1.0"" encoding=""utf-8""?><root><test abc=""1"" /></root>", readAsString(stream2.GetBuffer(), stream1.Length));
Assert.Equal(@"<?xml version=""1.0"" encoding=""utf-8""?><root><test abc=""1"" /></root>", readAsString(stream3.GetBuffer(), stream1.Length));
}
}

private static string readAsString(byte[] bytes, long length) => new UTF8Encoding().GetString(bytes, 0, (int)length);

[Fact]
public static async Task AsyncWriterDispose_ShouldCall_FlushAsyncWriteAsyncOnly_TextWriter()
{
Expand Down

0 comments on commit 2f04d5d

Please sign in to comment.