Code coverage JsonDocument, JsonElement (dotnet/corefx#39103)
authorbuyaa-n <bunamnan@microsoft.com>
Wed, 3 Jul 2019 20:35:51 +0000 (13:35 -0700)
committerGitHub <noreply@github.com>
Wed, 3 Jul 2019 20:35:51 +0000 (13:35 -0700)
 Improve coverage, remove unreachable section

Commit migrated from https://github.com/dotnet/corefx/commit/b00b4a5110c209b522fc711e48cd5243b3fb34ec

src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.Parse.cs
src/libraries/System.Text.Json/tests/JsonDocumentTests.cs

index 4cf6d52..8f82bd2 100644 (file)
@@ -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,
index 2ba3006..44665cf 100644 (file)
@@ -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<EndOfStreamException>(() => JsonDocument.Parse(stream));
+            }
+        }
+
+        [Fact]
+        public static void ParseJson_Stream_Async_ClearRentedBuffer_WhenThrow_CodeCoverage()
+        {
+            using (Stream stream = new ThrowOnReadStream(new byte[] { 1 }))
+            {
+                Assert.ThrowsAsync<EndOfStreamException>(async () => await JsonDocument.ParseAsync(stream));
+            }
+        }
+
+        [Fact]
+        public static void ParseJson_Stream_ThrowsOn_ArrayPoolRent_CodeCoverage()
+        {
+            using (Stream stream = new ThrowOnCanSeekStream (new byte[] { 1 }))
+            {
+                Assert.Throws<InsufficientMemoryException>(() => JsonDocument.Parse(stream));
+            }
+        }
+
+        [Fact]
+        public static void ParseJson_Stream_Async_ThrowsOn_ArrayPoolRent_CodeCoverage()
+        {
+            using (Stream stream = new ThrowOnCanSeekStream (new byte[] { 1 }))
+            {
+                Assert.ThrowsAsync<InsufficientMemoryException>(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();
+    }
 }