Adding unit test to cover NullReferenceException when passing null to a collection...
authorDavid Cantu <jozkyy@gmail.com>
Thu, 20 Jun 2019 20:12:18 +0000 (13:12 -0700)
committerGitHub <noreply@github.com>
Thu, 20 Jun 2019 20:12:18 +0000 (13:12 -0700)
* Adding unit test to cover NullReferenceException when passing null to a collaction (issue dotnet/corefx#37078)

* Adding Peer Review suggestions.

* Splitting referece type and value type test scenarios into separate methods.

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

src/libraries/System.Text.Json/tests/Serialization/Stream.ReadTests.cs

index 4b1a353..04c09e7 100644 (file)
@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
+using System.Collections.Generic;
 using System.IO;
 using System.Threading.Tasks;
 using Xunit;
@@ -79,5 +80,25 @@ namespace System.Text.Json.Serialization.Tests
                 Assert.Equal(1, i);
             }
         }
+
+        [Fact]
+        public static async Task ReadReferenceTypeCollectionPassingNullValueAsync()
+        {
+            using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes("null")))
+            {
+                IList<object> referenceTypeCollection  = await JsonSerializer.ReadAsync<IList<object>>(stream);
+                Assert.Null(referenceTypeCollection);
+            }
+        }
+
+        [Fact]
+        public static async Task ReadValueTypeCollectionPassingNullValueAsync()
+        {
+            using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes("null")))
+            {
+                IList<int> valueTypeCollection = await JsonSerializer.ReadAsync<IList<int>>(stream);
+                Assert.Null(valueTypeCollection);
+            }
+        }
     }
 }