-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonTests.cs
149 lines (136 loc) · 4.63 KB
/
JsonTests.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MiniJson;
namespace UnitTests
{
[TestClass]
public class JsonTests
{
[TestMethod]
public void TestParseString()
{
Assert.AreEqual("asdf\ubeef\u0005\r\n", Json.Parse("\"asdf\\ubeef\\u0005\\r\\n\""));
Assert.AreEqual("\"", Json.Parse("\"\\\"\" "));
Assert.AreEqual("", Json.Parse("\"\""));
}
[TestMethod]
public void TestParseDouble()
{
Assert.AreEqual(0.12345, (double)Json.Parse("0.12345"));
Assert.AreEqual(3.0, (double)Json.Parse(" 3 "));
Assert.AreEqual(0.001, (double)Json.Parse(" 1e-3"));
Assert.AreEqual(1000.0, (double)Json.Parse("1e+3 "));
Assert.AreEqual(1000.0, (double)Json.Parse("1e3"));
Assert.AreEqual(0.0, (double)Json.Parse("0"));
}
[TestMethod]
public void TestParseArray()
{
Assert.AreEqual(0, ((List<object>)Json.Parse("[]")).Count);
Assert.IsTrue(new List<object> { 1.0, true, 3.0 }.SequenceEqual((List<object>)Json.Parse("[1 , true,3]")));
}
[TestMethod]
public void TestParseObject()
{
Assert.AreEqual(0, ((Dictionary<string, object>)Json.Parse("{}")).Count);
var target = new Dictionary<string, object>
{
{"asdf", 0.0},
{"qwer", "ooo"},
};
var result = (Dictionary<string, object>)Json.Parse("{\"asdf\": 0,\t\"qwer\"\t\t:\"ooo\"}");
Assert.AreEqual(target.Count, result.Count);
foreach (var kv in target)
{
Assert.AreEqual(kv.Value, result[kv.Key]);
}
}
[TestMethod]
public void TestParseTrueFalseNull()
{
Assert.AreEqual(true, Json.Parse("true"));
Assert.AreEqual(false, Json.Parse("false"));
Assert.AreEqual(null, Json.Parse("null"));
}
[TestMethod]
public void TestStringifyString()
{
Assert.AreEqual("\"\\\"\"", Json.Stringify("\""));
Assert.AreEqual("\"asdf\\r\\n\"", Json.Stringify("asdf\r\n"));
Assert.AreEqual("\"\\u0005\"", Json.Stringify("\u0005"));
}
private void AssertStringifyFails(object obj)
{
try
{
Assert.Fail(Json.Stringify(obj));
}
catch (ArgumentException)
{
Assert.IsTrue(true);
}
}
[TestMethod]
public void TestStringifyDouble()
{
Assert.AreEqual("3", Json.Stringify(3.0));
Assert.AreEqual("0.001", Json.Stringify(0.001));
Assert.AreEqual("1000", Json.Stringify(1000.0));
AssertStringifyFails(double.NaN);
AssertStringifyFails(double.PositiveInfinity);
AssertStringifyFails(double.NegativeInfinity);
}
[TestMethod]
public void TestStringifyArray()
{
Assert.AreEqual("[1,2,3]", Json.Stringify(new List<object> { 1.0, 2.0, 3.0 }));
}
[TestMethod]
public void TestStringifyObject()
{
var result = Json.Stringify(new Dictionary<string, object>
{
{"asdf", 4.0},
{"qwer", "ooo"},
});
Assert.IsTrue(result == "{\"asdf\":4,\"qwer\":\"ooo\"}" || result == "{\"qwer\":\"ooo\",\"asdf\":4}");
}
[TestMethod]
public void TestStringifyTrueFalseNull()
{
Assert.AreEqual("true", Json.Stringify(true));
Assert.AreEqual("false", Json.Stringify(false));
Assert.AreEqual("null", Json.Stringify(null));
}
[TestMethod]
public void TestNested()
{
Assert.AreEqual("{\"\":[[],[1,{\"a\":2},[3,4]]]}", Json.Stringify(new Dictionary<string, object>{
{"", new List<object>{
new List<object>(),
new List<object>{
1.0,
new Dictionary<string, object>{
{"a", 2.0}
},
new List<object>{ 3.0, 4.0 }
},
}}
}));
}
[TestMethod]
public void TestEmpty()
{
try
{
Assert.Fail(Json.Parse("").ToString());
}
catch (FormatException)
{
Assert.IsTrue(true);
}
}
}
}