Skip to content

Commit 36d8fab

Browse files
authored
Merge pull request #2406 from sharwell/lightjson-testing
Initial round of testing for LightJson
2 parents b328def + 1212761 commit 36d8fab

16 files changed

Lines changed: 1090 additions & 67 deletions

StyleCop.Analyzers/StyleCop.Analyzers.Test/Helpers/DiagnosticVerifier.Helper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ protected virtual Solution CreateSolution(ProjectId projectId, string language)
166166
}
167167
else
168168
{
169-
JsonObject indentationObject = JsonReader.Parse(indentationSettings);
170-
JsonObject settingsObject = JsonReader.Parse(settings);
169+
JsonObject indentationObject = JsonReader.Parse(indentationSettings).AsJsonObject;
170+
JsonObject settingsObject = JsonReader.Parse(settings).AsJsonObject;
171171
JsonObject mergedSettings = MergeJsonObjects(settingsObject, indentationObject);
172172
using (var writer = new JsonWriter(pretty: true))
173173
{
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.LightJson
5+
{
6+
using System;
7+
using global::LightJson;
8+
using Xunit;
9+
using IEnumerable = System.Collections.IEnumerable;
10+
11+
public class JsonArrayTests
12+
{
13+
[Fact]
14+
public void TestConstructor()
15+
{
16+
var obj = new JsonArray();
17+
Assert.Equal(0, obj.Count);
18+
19+
var obj1 = new JsonArray(1, "test1");
20+
Assert.Equal(2, obj1.Count);
21+
Assert.Equal(1, obj1[0].AsInteger);
22+
Assert.Equal("test1", obj1[1].AsString);
23+
24+
var obj2 = new JsonArray { 1, "test2" };
25+
Assert.Equal(2, obj2.Count);
26+
Assert.Equal(1, obj2[0].AsInteger);
27+
Assert.Equal("test2", obj2[1].AsString);
28+
29+
Assert.Throws<ArgumentNullException>("values", () => new JsonArray(default(JsonValue[])));
30+
}
31+
32+
[Fact]
33+
public void TestIndexer()
34+
{
35+
var obj = new JsonArray(1);
36+
Assert.Equal(1, obj.Count);
37+
Assert.Equal(1, obj[0].AsInteger);
38+
Assert.Equal(JsonValue.Null, obj[1]);
39+
Assert.Equal(JsonValue.Null, obj[-1]);
40+
41+
obj[0] = 2;
42+
Assert.Equal(2, obj[0].AsInteger);
43+
44+
Assert.ThrowsAny<ArgumentOutOfRangeException>(() => obj[-1] = 0);
45+
Assert.ThrowsAny<ArgumentException>(() => obj[1] = 0);
46+
}
47+
48+
[Fact]
49+
public void TestInsert()
50+
{
51+
var obj = new JsonArray(1);
52+
Assert.Equal(1, obj.Count);
53+
Assert.Equal(1, obj[0].AsInteger);
54+
55+
// Insert at end
56+
Assert.Same(obj, obj.Insert(obj.Count, 2));
57+
Assert.Equal(2, obj.Count);
58+
Assert.Equal(1, obj[0].AsInteger);
59+
Assert.Equal(2, obj[1].AsInteger);
60+
61+
// Insert at beginning
62+
Assert.Same(obj, obj.Insert(0, 0));
63+
Assert.Equal(3, obj.Count);
64+
Assert.Equal(0, obj[0].AsInteger);
65+
Assert.Equal(1, obj[1].AsInteger);
66+
Assert.Equal(2, obj[2].AsInteger);
67+
68+
Assert.ThrowsAny<ArgumentOutOfRangeException>(() => obj.Insert(-1, 0));
69+
Assert.ThrowsAny<ArgumentException>(() => obj.Insert(obj.Count + 1, 0));
70+
}
71+
72+
[Fact]
73+
public void TestRemove()
74+
{
75+
var obj = new JsonArray(0, 1, 2);
76+
Assert.Equal(3, obj.Count);
77+
78+
Assert.ThrowsAny<ArgumentOutOfRangeException>(() => obj.Remove(-1));
79+
Assert.ThrowsAny<ArgumentException>(() => obj.Remove(obj.Count));
80+
81+
Assert.Same(obj, obj.Remove(1));
82+
Assert.Equal(2, obj.Count);
83+
Assert.Equal(0, obj[0].AsInteger);
84+
Assert.Equal(2, obj[1].AsInteger);
85+
}
86+
87+
[Fact]
88+
public void TestClear()
89+
{
90+
var obj = new JsonArray(0, 1, 2);
91+
Assert.Equal(3, obj.Count);
92+
93+
Assert.Same(obj, obj.Clear());
94+
Assert.Equal(0, obj.Count);
95+
96+
Assert.Same(obj, obj.Clear());
97+
Assert.Equal(0, obj.Count);
98+
}
99+
100+
[Fact]
101+
public void TestContains()
102+
{
103+
var obj = new JsonArray("a", "b", "c");
104+
Assert.True(obj.Contains("b"));
105+
obj.Remove(1);
106+
Assert.False(obj.Contains("b"));
107+
108+
Assert.False(obj.Contains(JsonValue.Null));
109+
}
110+
111+
[Fact]
112+
public void TestIndexOf()
113+
{
114+
var obj = new JsonArray("a", "b", "c");
115+
Assert.Equal(1, obj.IndexOf("b"));
116+
Assert.Equal(2, obj.IndexOf("c"));
117+
obj.Remove(1);
118+
Assert.Equal(-1, obj.IndexOf("b"));
119+
Assert.Equal(1, obj.IndexOf("c"));
120+
121+
Assert.Equal(-1, obj.IndexOf(JsonValue.Null));
122+
}
123+
124+
[Fact]
125+
public void TestEnumerators()
126+
{
127+
var obj = new JsonArray("a", "b", "c");
128+
129+
using (var genericEnumerator = obj.GetEnumerator())
130+
{
131+
var legacyEnumerator = ((IEnumerable)obj).GetEnumerator();
132+
for (int i = 0; i < obj.Count; i++)
133+
{
134+
Assert.True(genericEnumerator.MoveNext());
135+
Assert.True(legacyEnumerator.MoveNext());
136+
Assert.Equal(obj[i], genericEnumerator.Current);
137+
Assert.Equal(obj[i], legacyEnumerator.Current);
138+
Assert.Equal(genericEnumerator.Current, legacyEnumerator.Current);
139+
}
140+
}
141+
}
142+
}
143+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.LightJson
5+
{
6+
using System;
7+
using System.Collections.Generic;
8+
using global::LightJson;
9+
using Xunit;
10+
using IEnumerable = System.Collections.IEnumerable;
11+
12+
public class JsonObjectTests
13+
{
14+
[Fact]
15+
public void TestCount()
16+
{
17+
var obj = new JsonObject();
18+
Assert.Equal(0, obj.Count);
19+
20+
obj["x"] = "value";
21+
Assert.Equal(1, obj.Count);
22+
23+
obj["y"] = "value";
24+
Assert.Equal(2, obj.Count);
25+
26+
obj["x"] = "value2";
27+
Assert.Equal(2, obj.Count);
28+
29+
Assert.True(obj.Remove("x"));
30+
Assert.Equal(1, obj.Count);
31+
32+
Assert.False(obj.Remove("x"));
33+
Assert.Equal(1, obj.Count);
34+
35+
obj["z"] = "value3";
36+
Assert.Equal(2, obj.Count);
37+
38+
Assert.Same(obj, obj.Clear());
39+
Assert.Equal(0, obj.Count);
40+
}
41+
42+
[Fact]
43+
public void TestAdd()
44+
{
45+
var obj = new JsonObject();
46+
Assert.Equal(JsonValue.Null, obj["x"]);
47+
Assert.False(obj.ContainsKey("x"));
48+
49+
Assert.Same(obj, obj.Add("x"));
50+
Assert.Equal(JsonValue.Null, obj["x"]);
51+
Assert.True(obj.ContainsKey("x"));
52+
}
53+
54+
[Fact]
55+
public void TestEnumerator()
56+
{
57+
var obj = new JsonObject();
58+
obj["x"] = "x1";
59+
obj["y"] = "y1";
60+
61+
foreach (var value in obj)
62+
{
63+
Assert.Equal(typeof(KeyValuePair<string, JsonValue>), StaticType(value));
64+
Assert.Equal(value.Value, obj[value.Key]);
65+
}
66+
67+
IEnumerable<JsonValue> genericEnumerable = obj;
68+
foreach (var value in genericEnumerable)
69+
{
70+
Assert.True(obj.Contains(value));
71+
}
72+
73+
IEnumerable legacyEnumerable = obj;
74+
foreach (var value in legacyEnumerable)
75+
{
76+
Assert.IsType<KeyValuePair<string, JsonValue>>(value);
77+
Assert.True(obj.Contains(((KeyValuePair<string, JsonValue>)value).Value));
78+
}
79+
}
80+
81+
[Fact]
82+
public void TestRename()
83+
{
84+
var obj = new JsonObject { ["x"] = "value1", ["y"] = "value2" };
85+
Assert.Equal(2, obj.Count);
86+
87+
var value = obj["x"].AsString;
88+
Assert.False(obj.ContainsKey("z"));
89+
Assert.Same(obj, obj.Rename("x", "z"));
90+
Assert.Same(value, obj["z"].AsString);
91+
Assert.False(obj.ContainsKey("x"));
92+
Assert.Equal(2, obj.Count);
93+
94+
// Renaming can overwrite a value
95+
Assert.Same(obj, obj.Rename("z", "y"));
96+
Assert.Same(value, obj["y"].AsString);
97+
Assert.Equal(1, obj.Count);
98+
99+
// Renaming to the same name does nothing
100+
Assert.Same(obj, obj.Rename("y", "y"));
101+
Assert.Same(value, obj["y"].AsString);
102+
Assert.Equal(1, obj.Count);
103+
104+
// Renaming a non-existent element is not a problem, and does not overwrite the target
105+
Assert.Same(obj, obj.Rename("bogus", "y"));
106+
Assert.Equal(1, obj.Count);
107+
Assert.Same(value, obj["y"].AsString);
108+
}
109+
110+
private static Type StaticType<T>(T value) => typeof(T);
111+
}
112+
}

0 commit comments

Comments
 (0)