Added tests to cover all branches of JsonNodeOptions (#436)
authorAnton Landor <55381413+AntonLandor@users.noreply.github.com>
Wed, 11 Dec 2019 07:59:32 +0000 (08:59 +0100)
committerAhson Khan <ahson_ahmedk@yahoo.com>
Wed, 11 Dec 2019 07:59:32 +0000 (23:59 -0800)
* Added tests to cover all branches of JsonNodeOptions.

* Created a test class for JsonNodeOptions and moved relevant tests to it. Also implemented tests for CommentHandling and AllowTrailingCommas options.

* Added tests for default values. Moved JsonNodeOptionsTests to the correct item group.

* Added test to make sure default JsonNodeOptions have equal values to JsonNodeOptions created from default constructor.

* Added missed options arguments.

* Added assertions to make sure default JsonNodeOptions values are as expected.

src/libraries/System.Text.Json/tests/JsonNode.TraversalTests.cs
src/libraries/System.Text.Json/tests/JsonNodeOptionsTests.cs [new file with mode: 0644]
src/libraries/System.Text.Json/tests/System.Text.Json.Tests.csproj

index e909124..e02f474 100644 (file)
@@ -110,23 +110,6 @@ namespace System.Text.Json.Tests
         }
 
         [Fact]
-        public static void TestParseFailsWhenExceedsMaxDepth()
-        {
-            var builder = new StringBuilder();
-            for (int i = 0; i < 100; i++)
-            {
-                builder.Append("[");
-            }
-
-            for (int i = 0; i < 100; i++)
-            {
-                builder.Append("]");
-            }
-
-            Assert.ThrowsAny<JsonException>(() => JsonNode.Parse(builder.ToString()));
-        }
-
-        [Fact]
         public static void TestDeepCopyDoesNotStackOverflow()
         {
             var builder = new StringBuilder();
diff --git a/src/libraries/System.Text.Json/tests/JsonNodeOptionsTests.cs b/src/libraries/System.Text.Json/tests/JsonNodeOptionsTests.cs
new file mode 100644 (file)
index 0000000..f6ae389
--- /dev/null
@@ -0,0 +1,125 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using Xunit;
+
+namespace System.Text.Json.Tests
+{
+    public static partial class JsonNodeOptionsTests
+    {
+        [Fact]
+        public static void TestParseFailsWhenExceedsMaxDepth()
+        {
+            var builder = new StringBuilder();
+            for (int i = 0; i < 100; i++)
+            {
+                builder.Append("[");
+            }
+
+            for (int i = 0; i < 100; i++)
+            {
+                builder.Append("]");
+            }
+
+            // Test for default MaxDepth
+            Assert.ThrowsAny<JsonException>(() => JsonNode.Parse(builder.ToString()));
+            JsonNodeOptions options = new JsonNodeOptions { MaxDepth = default };
+            Assert.ThrowsAny<JsonException>(() => JsonNode.Parse(builder.ToString(), options));
+
+            // Test for MaxDepth of 5
+            options = new JsonNodeOptions { MaxDepth = 5 };
+            Assert.ThrowsAny<JsonException>(() => JsonNode.Parse(builder.ToString(), options));
+        }
+
+        [Fact]
+        public static void TestJsonCommentHandling()
+        {
+            string jsonStringWithComments = @"
+            {
+                // First comment
+                ""firstProperty"": ""first value"", //Second comment
+                ""secondProperty"" : ""second value""// Third comment
+                // Last comment
+            }";
+
+            JsonNodeOptions options = new JsonNodeOptions { CommentHandling = default };
+            Assert.ThrowsAny<JsonException>(() => JsonNode.Parse(jsonStringWithComments, options));
+
+            options = new JsonNodeOptions { CommentHandling = JsonCommentHandling.Disallow };
+            Assert.ThrowsAny<JsonException>(() => JsonNode.Parse(jsonStringWithComments, options));
+
+            options = new JsonNodeOptions { CommentHandling = JsonCommentHandling.Skip };
+            JsonObject jsonObject = (JsonObject)JsonNode.Parse(jsonStringWithComments, options);
+
+            Assert.Equal("first value", jsonObject["firstProperty"]);
+            Assert.Equal("second value", jsonObject["secondProperty"]);
+
+            Assert.Equal(2, jsonObject.GetPropertyNames().Count);
+            Assert.Equal(2, jsonObject.GetPropertyValues().Count);
+        }
+
+        [Fact]
+        public static void TestTrailingCommas()
+        {
+            string jsonStringWithTrailingCommas = @"
+            {
+                ""firstProperty"": ""first value"",
+                ""secondProperty"" : ""second value"",
+            }";
+
+            JsonNodeOptions options = new JsonNodeOptions { AllowTrailingCommas = default };
+            Assert.ThrowsAny<JsonException>(() => JsonNode.Parse(jsonStringWithTrailingCommas, options));
+
+            options = new JsonNodeOptions { AllowTrailingCommas = false };
+            Assert.ThrowsAny<JsonException>(() => JsonNode.Parse(jsonStringWithTrailingCommas, options));
+
+            options = new JsonNodeOptions { AllowTrailingCommas = true };
+            JsonObject jsonObject = (JsonObject) JsonNode.Parse(jsonStringWithTrailingCommas, options);
+
+            Assert.Equal("first value", jsonObject["firstProperty"]);
+            Assert.Equal("second value", jsonObject["secondProperty"]);
+
+            Assert.Equal(2, jsonObject.GetPropertyNames().Count);
+            Assert.Equal(2, jsonObject.GetPropertyValues().Count);
+        }
+
+        [Fact]
+        public static void TestInvalidJsonNodeOptions()
+        {
+            Assert.Throws<ArgumentOutOfRangeException>(() => new JsonNodeOptions()
+            {
+                CommentHandling = (JsonCommentHandling)Enum.GetNames(typeof(JsonCommentHandling)).Length
+            });
+            Assert.Throws<ArgumentOutOfRangeException>(() => new JsonNodeOptions()
+            {
+                MaxDepth = -1
+            });
+            Assert.Throws<ArgumentOutOfRangeException>(() => new JsonNodeOptions()
+            {
+                DuplicatePropertyNameHandling = (DuplicatePropertyNameHandlingStrategy)Enum.GetNames(typeof(DuplicatePropertyNameHandlingStrategy)).Length
+            });
+            Assert.Throws<ArgumentOutOfRangeException>(() => new JsonNodeOptions()
+            {
+                DuplicatePropertyNameHandling = (DuplicatePropertyNameHandlingStrategy)(-1)
+            });
+        }
+
+        [Fact]
+        public static void TestDefaultJsonNodeOptions()
+        {
+            JsonNodeOptions defaultOptions = default;
+            JsonNodeOptions newOptions = new JsonNodeOptions();
+
+            Assert.Equal(defaultOptions.AllowTrailingCommas, newOptions.AllowTrailingCommas);
+            Assert.Equal(defaultOptions.CommentHandling, newOptions.CommentHandling);
+            Assert.Equal(defaultOptions.DuplicatePropertyNameHandling, newOptions.DuplicatePropertyNameHandling);
+            Assert.Equal(defaultOptions.MaxDepth, newOptions.MaxDepth);
+
+            Assert.False(defaultOptions.AllowTrailingCommas);
+            Assert.Equal(JsonCommentHandling.Disallow, defaultOptions.CommentHandling);
+            Assert.Equal(DuplicatePropertyNameHandlingStrategy.Replace, defaultOptions.DuplicatePropertyNameHandling);
+            Assert.Equal(0, defaultOptions.MaxDepth);
+        }
+    }
+}
index 27c04eb..3a4b6ab 100644 (file)
     <Compile Include="JsonNode.AsJsonElementTests.cs" />
     <Compile Include="JsonNode.CloneTests.cs" />
     <Compile Include="JsonNode.TraversalTests.cs" />
+    <Compile Include="JsonNodeOptionsTests.cs" />
     <Compile Include="JsonNullTests.cs" />
     <Compile Include="JsonNumberTests.cs" />
     <Compile Include="JsonObjectTests.cs" />