-
Notifications
You must be signed in to change notification settings - Fork 509
Expand file tree
/
Copy pathJsonObjectTests.cs
More file actions
149 lines (122 loc) · 4.75 KB
/
JsonObjectTests.cs
File metadata and controls
149 lines (122 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
#nullable disable
namespace StyleCop.Analyzers.Test.LightJson
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using global::LightJson;
using Xunit;
using IEnumerable = System.Collections.IEnumerable;
public class JsonObjectTests
{
[Fact]
public void TestCount()
{
var obj = new JsonObject();
Assert.Equal(0, obj.Count);
obj["x"] = "value";
Assert.Equal(1, obj.Count);
obj["y"] = "value";
Assert.Equal(2, obj.Count);
obj["x"] = "value2";
Assert.Equal(2, obj.Count);
Assert.True(obj.Remove("x"));
Assert.Equal(1, obj.Count);
Assert.False(obj.Remove("x"));
Assert.Equal(1, obj.Count);
obj["z"] = "value3";
Assert.Equal(2, obj.Count);
Assert.Same(obj, obj.Clear());
Assert.Equal(0, obj.Count);
}
[Fact]
public void TestAdd()
{
var obj = new JsonObject();
Assert.Equal(JsonValue.Null, obj["x"]);
Assert.False(obj.ContainsKey("x"));
Assert.Same(obj, obj.Add("x"));
Assert.Equal(JsonValue.Null, obj["x"]);
Assert.True(obj.ContainsKey("x"));
}
[Fact]
public void TestEnumerator()
{
var obj = new JsonObject();
obj["x"] = "x1";
obj["y"] = "y1";
foreach (var value in obj)
{
Assert.Equal(typeof(KeyValuePair<string, JsonValue>), StaticType(value));
Assert.Equal(value.Value, obj[value.Key]);
}
IEnumerable<JsonValue> genericEnumerable = obj;
foreach (var value in genericEnumerable)
{
Assert.True(obj.Contains(value));
}
IEnumerable legacyEnumerable = obj;
foreach (var value in legacyEnumerable)
{
Assert.IsType<KeyValuePair<string, JsonValue>>(value);
Assert.True(obj.Contains(((KeyValuePair<string, JsonValue>)value).Value));
}
}
[Fact]
public void TestRename()
{
var obj = new JsonObject { ["x"] = "value1", ["y"] = "value2" };
Assert.Equal(2, obj.Count);
var value = obj["x"].AsString;
Assert.False(obj.ContainsKey("z"));
Assert.Same(obj, obj.Rename("x", "z"));
Assert.Same(value, obj["z"].AsString);
Assert.False(obj.ContainsKey("x"));
Assert.Equal(2, obj.Count);
// Renaming can overwrite a value
Assert.Same(obj, obj.Rename("z", "y"));
Assert.Same(value, obj["y"].AsString);
Assert.Equal(1, obj.Count);
// Renaming to the same name does nothing
Assert.Same(obj, obj.Rename("y", "y"));
Assert.Same(value, obj["y"].AsString);
Assert.Equal(1, obj.Count);
// Renaming a non-existent element is not a problem, and does not overwrite the target
Assert.Same(obj, obj.Rename("bogus", "y"));
Assert.Equal(1, obj.Count);
Assert.Same(value, obj["y"].AsString);
}
[Fact]
public void TestDebugView()
{
var obj = new JsonObject
{
["x"] = "string value",
["y"] = new JsonObject(),
["z"] = new JsonArray("a", "b", "c"),
};
var proxyAttribute = obj.GetType().GetCustomAttribute<DebuggerTypeProxyAttribute>();
Assert.NotNull(proxyAttribute);
var proxyType = Type.GetType(proxyAttribute.ProxyTypeName);
Assert.NotNull(proxyType);
var proxy = Activator.CreateInstance(proxyType, obj);
Assert.NotNull(proxy);
var keys = proxyType.GetTypeInfo().GetDeclaredProperty("Keys").GetValue(proxy);
var keysArray = Assert.IsAssignableFrom<Array>(keys);
Assert.NotEmpty(keysArray);
foreach (var key in keysArray)
{
var view = key.GetType().GetTypeInfo().GetDeclaredProperty("View").GetValue(key);
Assert.NotNull(view);
}
}
#pragma warning disable IDE0079 // Remove unnecessary suppression
#pragma warning disable IDE0060 // Remove unused parameter
private static Type StaticType<T>(T value) => typeof(T);
#pragma warning restore IDE0060 // Remove unused parameter
#pragma warning restore IDE0079 // Remove unnecessary suppression
}
}