From 111c6e25607643291c11ad162f7b84893fcabd08 Mon Sep 17 00:00:00 2001 From: buyaa-n Date: Wed, 3 Jul 2019 13:35:51 -0700 Subject: [PATCH] Code coverage JsonDocument, JsonElement (dotnet/corefx#39103) Improve coverage, remove unreachable section Commit migrated from https://github.com/dotnet/corefx/commit/b00b4a5110c209b522fc711e48cd5243b3fb34ec --- .../Text/Json/Document/JsonDocument.Parse.cs | 13 +--- .../System.Text.Json/tests/JsonDocumentTests.cs | 75 ++++++++++++++++++++++ 2 files changed, 78 insertions(+), 10 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.Parse.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.Parse.cs index 4cf6d52..8f82bd2 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.Parse.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.Parse.cs @@ -497,16 +497,9 @@ namespace System.Text.Json { if (shouldThrow) { - byte displayByte; - - if (reader.HasValueSequence) - { - displayByte = reader.ValueSequence.First.Span[0]; - } - else - { - displayByte = reader.ValueSpan[0]; - } + // Default case would only hit if TokenType equals JsonTokenType.EndObject or JsonTokenType.EndArray in which case it would never be sequence + Debug.Assert(!reader.HasValueSequence); + byte displayByte = reader.ValueSpan[0]; ThrowHelper.ThrowJsonReaderException( ref reader, diff --git a/src/libraries/System.Text.Json/tests/JsonDocumentTests.cs b/src/libraries/System.Text.Json/tests/JsonDocumentTests.cs index 2ba3006..44665cf 100644 --- a/src/libraries/System.Text.Json/tests/JsonDocumentTests.cs +++ b/src/libraries/System.Text.Json/tests/JsonDocumentTests.cs @@ -411,6 +411,42 @@ namespace System.Text.Json.Tests GetAwaiter().GetResult()); } + [Fact] + public static void ParseJson_Stream_ClearRentedBuffer_WhenThrow_CodeCoverage() + { + using (Stream stream = new ThrowOnReadStream(new byte[] { 1 })) + { + Assert.Throws(() => JsonDocument.Parse(stream)); + } + } + + [Fact] + public static void ParseJson_Stream_Async_ClearRentedBuffer_WhenThrow_CodeCoverage() + { + using (Stream stream = new ThrowOnReadStream(new byte[] { 1 })) + { + Assert.ThrowsAsync(async () => await JsonDocument.ParseAsync(stream)); + } + } + + [Fact] + public static void ParseJson_Stream_ThrowsOn_ArrayPoolRent_CodeCoverage() + { + using (Stream stream = new ThrowOnCanSeekStream (new byte[] { 1 })) + { + Assert.Throws(() => JsonDocument.Parse(stream)); + } + } + + [Fact] + public static void ParseJson_Stream_Async_ThrowsOn_ArrayPoolRent_CodeCoverage() + { + using (Stream stream = new ThrowOnCanSeekStream (new byte[] { 1 })) + { + Assert.ThrowsAsync(async () => await JsonDocument.ParseAsync(stream)); + } + } + [Theory] [MemberData(nameof(BadBOMCases))] public static void ParseJson_SeekableStream_BadBOM(string json) @@ -2571,6 +2607,14 @@ namespace System.Text.Json.Tests test++; } + structEnumerator.Reset(); + + Assert.True(structEnumerator.MoveNext()); + Assert.Equal(0, structEnumerator.Current.GetInt32()); + + Assert.True(structEnumerator.MoveNext()); + Assert.Equal(1, structEnumerator.Current.GetInt32()); + Assert.True(structEnumerator.MoveNext()); Assert.Equal(2, structEnumerator.Current.GetInt32()); @@ -2737,6 +2781,16 @@ namespace System.Text.Json.Tests test++; } + structEnumerator.Reset(); + + Assert.True(structEnumerator.MoveNext()); + Assert.Equal("name0", structEnumerator.Current.Name); + Assert.Equal(0, structEnumerator.Current.Value.GetInt32()); + + Assert.True(structEnumerator.MoveNext()); + Assert.Equal("name1", structEnumerator.Current.Name); + Assert.Equal(1, structEnumerator.Current.Value.GetInt32()); + Assert.True(structEnumerator.MoveNext()); Assert.Equal("name2", structEnumerator.Current.Name); Assert.Equal(2, structEnumerator.Current.Value.GetInt32()); @@ -3687,4 +3741,25 @@ namespace System.Text.Json.Tests return s_compactJson[testCaseType] = existing; } } + + public class ThrowOnReadStream : MemoryStream + { + public ThrowOnReadStream(byte[] bytes) : base(bytes) + { + } + + public override int Read(byte[] buffer, int offset, int count) + { + throw new EndOfStreamException(); + } + } + + public class ThrowOnCanSeekStream : MemoryStream + { + public ThrowOnCanSeekStream (byte[] bytes) : base(bytes) + { + } + + public override bool CanSeek => throw new InsufficientMemoryException(); + } } -- 2.7.4