Skip to content

Commit 0d86986

Browse files
committed
Add tests for JsonObject
Fix a bug where renaming a property to the same name would remove the property.
1 parent 1803720 commit 0d86986

3 files changed

Lines changed: 121 additions & 0 deletions

File tree

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+
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/StyleCop.Analyzers.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@
235235
<Compile Include="LayoutRules\SA1518UnitTests.cs" />
236236
<Compile Include="LayoutRules\SA1519UnitTests.cs" />
237237
<Compile Include="LayoutRules\SA1520UnitTests.cs" />
238+
<Compile Include="LightJson\JsonObjectTests.cs" />
238239
<Compile Include="LightJson\JsonReaderTests.cs" />
239240
<Compile Include="LightJson\JsonValueTests.cs" />
240241
<Compile Include="Lightup\AccessorDeclarationSyntaxExtensionsTests.cs" />

StyleCop.Analyzers/StyleCop.Analyzers/LightJson/JsonObject.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace LightJson
55
{
66
using System.Collections.Generic;
77
using System.Diagnostics;
8+
using System.Diagnostics.CodeAnalysis;
89

910
/// <summary>
1011
/// Represents a key-value pair collection of JsonValue objects.
@@ -121,6 +122,12 @@ public JsonObject Clear()
121122
/// <returns>Returns this JsonObject.</returns>
122123
public JsonObject Rename(string oldKey, string newKey)
123124
{
125+
if (oldKey == newKey)
126+
{
127+
// Renaming to the same name just does nothing
128+
return this;
129+
}
130+
124131
JsonValue value;
125132

126133
if (this.properties.TryGetValue(oldKey, out value))
@@ -179,6 +186,7 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
179186
return this.GetEnumerator();
180187
}
181188

189+
[ExcludeFromCodeCoverage]
182190
private class JsonObjectDebugView
183191
{
184192
private JsonObject jsonObject;

0 commit comments

Comments
 (0)