Repository navigation
Expand file tree
/
Copy pathJsonUtilityTest.cs
More file actions
108 lines (96 loc) · 3.6 KB
/
Copy pathJsonUtilityTest.cs
File metadata and controls
108 lines (96 loc) · 3.6 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
using Newtonsoft.Json.Linq;
using Segment.Serialization;
using Xunit;
namespace Tests
{
public class JsonUtilityTest
{
private static readonly string s_date = System.DateTime.UtcNow.ToString();
private readonly JsonObject _jsonObject = new JsonObject
{
["int"] = 1,
["float"] = 1f,
["long"] = 1L,
["double"] = 1.0,
["string"] = "1",
["bool"] = true,
["datetime"] = "2015-12-10T04:08:31.905Z",
["date"] = s_date,
["null"] = null,
["object"] = new JsonObject
{
["another object"] = "obj"
},
["array"] = new JsonArray
{
1, 1f, 1L, 1.0, "1", true, new JsonObject
{
["object in array"] = "obj"
}
}
};
private readonly string _jsonStr =
"{\"int\": 1,\"float\": 1,\"long\": 1,\"double\": 1,\"string\": \"1\",\"bool\": true,\"datetime\": \"2015-12-10T04:08:31.905Z\",\"date\": \"" + s_date + "\",\"null\": null,\"object\": {\"another object\": \"obj\"},\"array\": [1,1,1,1,\"1\",true,{\"object in array\": \"obj\"}]}";
[Fact]
public void Test_ToJson()
{
string actual = JsonUtility.ToJson(_jsonObject);
JsonObject expected = JsonUtility.FromJson<JsonObject>(_jsonStr);
Assert.Equal(JsonUtility.ToJson(expected), actual);
}
[Fact]
public void Test_FromJson()
{
JsonObject actual = JsonUtility.FromJson<JsonObject>(_jsonStr);
Assert.Equal(_jsonObject.ToString(), actual.ToString());
}
[Fact]
public void Test_ToJson_Camel_Case()
{
var foo = new Foo();
string actual = JsonUtility.ToJson(foo);
Assert.True(JsonEquals(foo.ToString(), actual));
}
[Fact]
public void Test_ToJson_Polymorphic()
{
var bar = new Bar();
Foo foo = bar;
string actual = JsonUtility.ToJson(foo);
Assert.True(JsonEquals(bar.ToString(), actual));
}
[Fact]
public void Test_ToJson_Only_Property()
{
var foo = new Foo();
string actual = JsonUtility.ToJson(foo);
Assert.Contains(foo.PropertyFoo, actual);
Assert.DoesNotContain("privateFieldFoo", actual);
}
[Fact]
public void Test_FromJson_T()
{
var actual = new Foo();
Foo expected = JsonUtility.FromJson<Foo>(actual.ToString());
Assert.Equal(expected.ToString(), actual.ToString());
}
[Fact]
public void Test_FromJson_Nested_Objects()
{
string settingsStr =
"{\"integrations\":{\"Segment.io\":{\"apiKey\":\"qwerty\"}},\"plan\":{},\"edgeFunctions\":{}}";
Settings settings = JsonUtility.FromJson<Settings>(settingsStr);
Assert.NotNull(settings.Integrations);
Assert.NotNull(settings.Integrations["Segment.io"]);
Assert.Equal("qwerty", settings.Integrations.GetJsonObject("Segment.io").GetString("apiKey"));
Assert.Equal(new JsonObject(), settings.Plan);
Assert.Equal(new JsonObject(), settings.EdgeFunctions);
}
private bool JsonEquals(string expect, string actual)
{
var jsonExpect = JObject.Parse(expect);
var jsonActual = JObject.Parse(actual);
return JToken.DeepEquals(jsonExpect, jsonActual);
}
}
}