Skip to content

Commit 03e2566

Browse files
committed
Bring back max depth feature and add tests
1 parent 150d0dc commit 03e2566

File tree

4 files changed

+176
-52
lines changed

4 files changed

+176
-52
lines changed

Editor/JSONObjectAsyncTests.cs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,45 @@ public void SubStringInputMatchesOutput() {
7575
}
7676

7777
var result = parser.Current;
78-
Assert.That(result.offset, Is.EqualTo(end));
78+
Assert.That(result.offset, Is.EqualTo(end + 1));
7979
ValidateJsonObject(result.result, substring);
8080
}
8181
}
8282

83+
[Test]
84+
public void MaxDepthWithoutExcessLevels() {
85+
using (var parser = JSONObject.CreateAsync(TestStrings.JsonString, maxDepth: 2, storeExcessLevels: false).GetEnumerator()) {
86+
while (parser.MoveNext()) { }
87+
88+
var jsonObject = parser.Current.result;
89+
var testObject = jsonObject["TestObject"];
90+
var someObject = testObject["SomeObject"];
91+
Assert.That(someObject.type, Is.EqualTo(JSONObject.Type.Null));
92+
93+
var nestedArray = testObject["NestedArray"];
94+
Assert.That(nestedArray.type, Is.EqualTo(JSONObject.Type.Null));
95+
}
96+
}
97+
98+
[Test]
99+
public void MaxDepthWithExcessLevels() {
100+
using (var parser = JSONObject.CreateAsync(TestStrings.JsonString, maxDepth: 2, storeExcessLevels: true).GetEnumerator()) {
101+
while (parser.MoveNext()) { }
102+
103+
var jsonObject = parser.Current.result;
104+
var testObject = jsonObject["TestObject"];
105+
var someObject = testObject["SomeObject"];
106+
Assert.That(someObject.type, Is.EqualTo(JSONObject.Type.Baked));
107+
Assert.That(someObject.stringValue, Is.EqualTo(TestStrings.SomeObject));
108+
109+
var nestedArray = testObject["NestedArray"];
110+
Assert.That(nestedArray.type, Is.EqualTo(JSONObject.Type.Baked));
111+
Assert.That(nestedArray.stringValue, Is.EqualTo(TestStrings.NestedArray));
112+
113+
ValidateJsonObject(jsonObject, TestStrings.JsonString);
114+
}
115+
}
116+
83117
[Test]
84118
public void PrettyInputMatchesPrettyOutput() {
85119
ValidateJsonString(TestStrings.PrettyJsonString, TestStrings.PrettyJsonString, true);

Editor/JSONObjectTestStrings.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ THE SOFTWARE.
2424

2525
namespace Defective.JSON.Tests {
2626
static class JSONObjectTestStrings {
27-
public const string JsonString = "{\"TestObject\":{\"SomeText\":\"Blah\",\"SomeObject\":{\"SomeNumber\":42,\"SomeFloat\":13.37,\"SomeBool\":true,\"SomeNull\":null},\"SomeEmptyObject\":{},\"SomeEmptyArray\":[],\"BasicArray\":[0,1,2],\"NestedArray\":[[0,1,[0,1]],{\"field\":42}],\"EmbeddedObject\":\"{\\\"field\\\":\\\"Value with \\\\\\\"escaped quotes\\\\\\\"\\\"}\"}}";
27+
public const string SomeObject = "{\"SomeNumber\":42,\"SomeFloat\":13.37,\"SomeBool\":true,\"SomeNull\":null}";
28+
public const string NestedArray = "[[0,1,[0,1]],{\"field\":42}]";
29+
public const string JsonString = "{\"TestObject\":{\"SomeText\":\"Blah\",\"SomeObject\":" + SomeObject + ",\"SomeEmptyObject\":{},\"SomeEmptyArray\":[],\"BasicArray\":[0,1,2],\"NestedArray\":" + NestedArray + ",\"EmbeddedObject\":\"{\\\"field\\\":\\\"Value with \\\\\\\"escaped quotes\\\\\\\"\\\"}\"}}";
2830
public const string PrettyJsonString = "{\r\n\t\"TestObject\":{\r\n\t\t\"SomeText\":\"Blah\",\r\n\t\t\"SomeObject\":{\r\n\t\t\t\"SomeNumber\":42,\r\n\t\t\t\"SomeFloat\":13.37,\r\n\t\t\t\"SomeBool\":true,\r\n\t\t\t\"SomeNull\":null\r\n\t\t},\r\n\t\t\"SomeEmptyObject\":{},\r\n\t\t\"SomeEmptyArray\":[],\r\n\t\t\"EmbeddedObject\":\"{\\\"field\\\":\\\"Value with \\\\\\\"escaped quotes\\\\\\\"\\\"}\"\r\n\t}\r\n}";
2931
public const string FieldName = "TestField";
3032
public const string JsonFormat = "{{\"" + FieldName + "\":{0}}}";

Editor/JSONObjectTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,32 @@ public void SubStringInputMatchesOutput() {
5555
ValidateJsonObject(JSONObject.Create(TestStrings.JsonString, start, end), substring);
5656
}
5757

58+
[Test]
59+
public void MaxDepthWithoutExcessLevels() {
60+
var jsonObject = JSONObject.Create(TestStrings.JsonString, maxDepth: 2, storeExcessLevels: false);
61+
var testObject = jsonObject["TestObject"];
62+
var someObject = testObject["SomeObject"];
63+
Assert.That(someObject.type, Is.EqualTo(JSONObject.Type.Null));
64+
65+
var nestedArray = testObject["NestedArray"];
66+
Assert.That(nestedArray.type, Is.EqualTo(JSONObject.Type.Null));
67+
}
68+
69+
[Test]
70+
public void MaxDepthWithExcessLevels() {
71+
var jsonObject = JSONObject.Create(TestStrings.JsonString, maxDepth: 2, storeExcessLevels: true);
72+
var testObject = jsonObject["TestObject"];
73+
var someObject = testObject["SomeObject"];
74+
Assert.That(someObject.type, Is.EqualTo(JSONObject.Type.Baked));
75+
Assert.That(someObject.stringValue, Is.EqualTo(TestStrings.SomeObject));
76+
77+
var nestedArray = testObject["NestedArray"];
78+
Assert.That(nestedArray.type, Is.EqualTo(JSONObject.Type.Baked));
79+
Assert.That(nestedArray.stringValue, Is.EqualTo(TestStrings.NestedArray));
80+
81+
ValidateJsonObject(jsonObject, TestStrings.JsonString);
82+
}
83+
5884
[TestCase(long.MaxValue)]
5985
[TestCase(long.MinValue)]
6086
[TestCase(0)]

0 commit comments

Comments
 (0)