Code Coverage Statistics for Source File
Newtonsoft.Json.Tests\Schema\JsonSchemaTests.cs
Symbol Coverage: 100.00% (80 of 80)
Branch Coverage: 100.00% (9 of 9)
Cyclomatic Complexity Avg: 1.00 Max:1
Code Lines: 232
Symbol Coverage Trend
View:
L | V | Source |
---|---|---|
1 |
#region License |
|
2 |
// Copyright (c) 2007 James Newton-King |
|
3 |
// |
|
4 |
// Permission is hereby granted, free of charge, to any person |
|
5 |
// obtaining a copy of this software and associated documentation |
|
6 |
// files (the "Software"), to deal in the Software without |
|
7 |
// restriction, including without limitation the rights to use, |
|
8 |
// copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
9 |
// copies of the Software, and to permit persons to whom the |
|
10 |
// Software is furnished to do so, subject to the following |
|
11 |
// conditions: |
|
12 |
// |
|
13 |
// The above copyright notice and this permission notice shall be |
|
14 |
// included in all copies or substantial portions of the Software. |
|
15 |
// |
|
16 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
17 |
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|
18 |
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
19 |
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
20 |
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|
21 |
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
22 |
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
23 |
// OTHER DEALINGS IN THE SOFTWARE. |
|
24 |
#endregion |
|
25 |
||
26 |
using System; |
|
27 |
using System.Collections.Generic; |
|
28 |
using System.IO; |
|
29 |
using System.Linq; |
|
30 |
using System.Text; |
|
31 |
using Newtonsoft.Json.Schema; |
|
32 |
using NUnit.Framework; |
|
33 |
||
34 |
namespace Newtonsoft.Json.Tests.Schema |
|
35 |
{ |
|
36 |
public class JsonSchemaTests : TestFixtureBase |
|
37 |
{ |
|
38 |
[Test] |
|
39 |
public void Extends() |
|
40 |
{ |
|
41 |
string json; |
|
42 |
1 |
JsonSchemaResolver resolver = new JsonSchemaResolver();
|
43 |
||
44 |
1 |
json = @"{ |
45 |
1 |
""id"":""first"",
|
46 |
1 |
""type"":""object"",
|
47 |
1 |
""additionalProperties"":{}
|
48 |
1 |
}";
|
49 |
||
50 |
1 |
JsonSchema first = JsonSchema.Parse(json, resolver);
|
51 |
||
52 |
1 |
json = |
53 |
1 |
@"{
|
54 |
1 |
""id"":""second"",
|
55 |
1 |
""type"":""object"",
|
56 |
1 |
""extends"":{""$ref"":""first""},
|
57 |
1 |
""additionalProperties"":{""type"":""string""}
|
58 |
1 |
}";
|
59 |
||
60 |
1 |
JsonSchema second = JsonSchema.Parse(json, resolver);
|
61 |
1 |
Assert.AreEqual(first, second.Extends);
|
62 |
||
63 |
1 |
json = |
64 |
1 |
@"{
|
65 |
1 |
""id"":""third"",
|
66 |
1 |
""type"":""object"",
|
67 |
1 |
""extends"":{""$ref"":""second""},
|
68 |
1 |
""additionalProperties"":false
|
69 |
1 |
}";
|
70 |
||
71 |
1 |
JsonSchema third = JsonSchema.Parse(json, resolver);
|
72 |
1 |
Assert.AreEqual(second, third.Extends);
|
73 |
1 |
Assert.AreEqual(first, third.Extends.Extends);
|
74 |
||
75 |
1 |
StringWriter writer = new StringWriter();
|
76 |
1 |
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
77 |
1 |
jsonWriter.Formatting = Formatting.Indented;
|
78 |
||
79 |
1 |
third.WriteTo(jsonWriter, resolver);
|
80 |
||
81 |
1 |
string writtenJson = writer.ToString();
|
82 |
1 |
Assert.AreEqual(@"{
|
83 |
1 |
""id"": ""third"",
|
84 |
1 |
""type"": ""object"",
|
85 |
1 |
""additionalProperties"": false,
|
86 |
1 |
""extends"": {
|
87 |
1 |
""$ref"": ""second""
|
88 |
1 |
}
|
89 |
1 |
}", writtenJson);
|
90 |
||
91 |
1 |
StringWriter writer1 = new StringWriter();
|
92 |
1 |
JsonTextWriter jsonWriter1 = new JsonTextWriter(writer1);
|
93 |
1 |
jsonWriter1.Formatting = Formatting.Indented;
|
94 |
||
95 |
1 |
third.WriteTo(jsonWriter1);
|
96 |
||
97 |
1 |
writtenJson = writer1.ToString();
|
98 |
1 |
Assert.AreEqual(@"{
|
99 |
1 |
""id"": ""third"",
|
100 |
1 |
""type"": ""object"",
|
101 |
1 |
""additionalProperties"": false,
|
102 |
1 |
""extends"": {
|
103 |
1 |
""id"": ""second"",
|
104 |
1 |
""type"": ""object"",
|
105 |
1 |
""additionalProperties"": {
|
106 |
1 |
""type"": ""string""
|
107 |
1 |
},
|
108 |
1 |
""extends"": {
|
109 |
1 |
""id"": ""first"",
|
110 |
1 |
""type"": ""object"",
|
111 |
1 |
""additionalProperties"": {}
|
112 |
1 |
}
|
113 |
1 |
}
|
114 |
1 |
}", writtenJson);
|
115 |
1 |
}
|
116 |
[Test] |
|
117 |
public void WriteTo_AdditionalProperties() |
|
118 |
{ |
|
119 |
1 |
StringWriter writer = new StringWriter();
|
120 |
1 |
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
121 |
1 |
jsonWriter.Formatting = Formatting.Indented;
|
122 |
||
123 |
1 |
JsonSchema schema = JsonSchema.Parse(@"{ |
124 |
1 |
""description"":""AdditionalProperties"",
|
125 |
1 |
""type"":[""string"", ""integer""],
|
126 |
1 |
""additionalProperties"":{""type"":[""object"", ""boolean""]}
|
127 |
1 |
}");
|
128 |
||
129 |
1 |
schema.WriteTo(jsonWriter);
|
130 |
||
131 |
1 |
string json = writer.ToString();
|
132 |
||
133 |
1 |
Assert.AreEqual(@"{
|
134 |
1 |
""description"": ""AdditionalProperties"",
|
135 |
1 |
""type"": [
|
136 |
1 |
""string"",
|
137 |
1 |
""integer""
|
138 |
1 |
],
|
139 |
1 |
""additionalProperties"": {
|
140 |
1 |
""type"": [
|
141 |
1 |
""boolean"",
|
142 |
1 |
""object""
|
143 |
1 |
]
|
144 |
1 |
}
|
145 |
1 |
}", json);
|
146 |
1 |
}
|
147 |
||
148 |
[Test] |
|
149 |
public void WriteTo_Properties() |
|
150 |
{ |
|
151 |
1 |
JsonSchema schema = JsonSchema.Parse(@"{ |
152 |
1 |
""description"":""A person"",
|
153 |
1 |
""type"":""object"",
|
154 |
1 |
""properties"":
|
155 |
1 |
{
|
156 |
1 |
""name"":{""type"":""string""},
|
157 |
1 |
""hobbies"":
|
158 |
1 |
{
|
159 |
1 |
""type"":""array"",
|
160 |
1 |
""items"": {""type"":""string""}
|
161 |
1 |
}
|
162 |
1 |
}
|
163 |
1 |
}");
|
164 |
||
165 |
1 |
StringWriter writer = new StringWriter();
|
166 |
1 |
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
167 |
1 |
jsonWriter.Formatting = Formatting.Indented;
|
168 |
||
169 |
1 |
schema.WriteTo(jsonWriter);
|
170 |
||
171 |
1 |
string json = writer.ToString();
|
172 |
||
173 |
1 |
Assert.AreEqual(@"{
|
174 |
1 |
""description"": ""A person"",
|
175 |
1 |
""type"": ""object"",
|
176 |
1 |
""properties"": {
|
177 |
1 |
""name"": {
|
178 |
1 |
""type"": ""string""
|
179 |
1 |
},
|
180 |
1 |
""hobbies"": {
|
181 |
1 |
""type"": ""array"",
|
182 |
1 |
""items"": {
|
183 |
1 |
""type"": ""string""
|
184 |
1 |
}
|
185 |
1 |
}
|
186 |
1 |
}
|
187 |
1 |
}", json);
|
188 |
||
189 |
1 |
}
|
190 |
||
191 |
[Test] |
|
192 |
public void WriteTo_Enum() |
|
193 |
{ |
|
194 |
1 |
JsonSchema schema = JsonSchema.Parse(@"{ |
195 |
1 |
""description"":""Type"",
|
196 |
1 |
""type"":[""string"",""array""],
|
197 |
1 |
""items"":{},
|
198 |
1 |
""enum"":[""string"",""object"",""array"",""boolean"",""number"",""integer"",""null"",""any""]
|
199 |
1 |
}");
|
200 |
||
201 |
1 |
StringWriter writer = new StringWriter();
|
202 |
1 |
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
203 |
1 |
jsonWriter.Formatting = Formatting.Indented;
|
204 |
||
205 |
1 |
schema.WriteTo(jsonWriter);
|
206 |
||
207 |
1 |
string json = writer.ToString();
|
208 |
||
209 |
1 |
Assert.AreEqual(@"{
|
210 |
1 |
""description"": ""Type"",
|
211 |
1 |
""type"": [
|
212 |
1 |
""string"",
|
213 |
1 |
""array""
|
214 |
1 |
],
|
215 |
1 |
""items"": {},
|
216 |
1 |
""enum"": [
|
217 |
1 |
""string"",
|
218 |
1 |
""object"",
|
219 |
1 |
""array"",
|
220 |
1 |
""boolean"",
|
221 |
1 |
""number"",
|
222 |
1 |
""integer"",
|
223 |
1 |
""null"",
|
224 |
1 |
""any""
|
225 |
1 |
]
|
226 |
1 |
}", json);
|
227 |
1 |
}
|
228 |
||
229 |
[Test] |
|
230 |
public void WriteTo_CircularReference() |
|
231 |
{ |
|
232 |
1 |
string json = @"{ |
233 |
1 |
""id"":""CircularReferenceArray"",
|
234 |
1 |
""description"":""CircularReference"",
|
235 |
1 |
""type"":[""array""],
|
236 |
1 |
""items"":{""$ref"":""CircularReferenceArray""}
|
237 |
1 |
}";
|
238 |
||
239 |
1 |
JsonSchema schema = JsonSchema.Parse(json);
|
240 |
||
241 |
1 |
StringWriter writer = new StringWriter();
|
242 |
1 |
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
243 |
1 |
jsonWriter.Formatting = Formatting.Indented;
|
244 |
||
245 |
1 |
schema.WriteTo(jsonWriter);
|
246 |
||
247 |
1 |
string writtenJson = writer.ToString();
|
248 |
||
249 |
1 |
Assert.AreEqual(@"{
|
250 |
1 |
""id"": ""CircularReferenceArray"",
|
251 |
1 |
""description"": ""CircularReference"",
|
252 |
1 |
""type"": ""array"",
|
253 |
1 |
""items"": {
|
254 |
1 |
""$ref"": ""CircularReferenceArray""
|
255 |
1 |
}
|
256 |
1 |
}", writtenJson);
|
257 |
1 |
}
|
258 |
||
259 |
[Test] |
|
260 |
public void WriteTo_DisallowMultiple() |
|
261 |
{ |
|
262 |
1 |
JsonSchema schema = JsonSchema.Parse(@"{ |
263 |
1 |
""description"":""Type"",
|
264 |
1 |
""type"":[""string"",""array""],
|
265 |
1 |
""items"":{},
|
266 |
1 |
""disallow"":[""string"",""object"",""array""]
|
267 |
1 |
}");
|
268 |
||
269 |
1 |
StringWriter writer = new StringWriter();
|
270 |
1 |
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
271 |
1 |
jsonWriter.Formatting = Formatting.Indented;
|
272 |
||
273 |
1 |
schema.WriteTo(jsonWriter);
|
274 |
||
275 |
1 |
string json = writer.ToString();
|
276 |
||
277 |
1 |
Assert.AreEqual(@"{
|
278 |
1 |
""description"": ""Type"",
|
279 |
1 |
""type"": [
|
280 |
1 |
""string"",
|
281 |
1 |
""array""
|
282 |
1 |
],
|
283 |
1 |
""items"": {},
|
284 |
1 |
""disallow"": [
|
285 |
1 |
""string"",
|
286 |
1 |
""object"",
|
287 |
1 |
""array""
|
288 |
1 |
]
|
289 |
1 |
}", json);
|
290 |
1 |
}
|
291 |
||
292 |
[Test] |
|
293 |
public void WriteTo_DisallowSingle() |
|
294 |
{ |
|
295 |
1 |
JsonSchema schema = JsonSchema.Parse(@"{ |
296 |
1 |
""description"":""Type"",
|
297 |
1 |
""type"":[""string"",""array""],
|
298 |
1 |
""items"":{},
|
299 |
1 |
""disallow"":""any""
|
300 |
1 |
}");
|
301 |
||
302 |
1 |
StringWriter writer = new StringWriter();
|
303 |
1 |
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
304 |
1 |
jsonWriter.Formatting = Formatting.Indented;
|
305 |
||
306 |
1 |
schema.WriteTo(jsonWriter);
|
307 |
||
308 |
1 |
string json = writer.ToString();
|
309 |
||
310 |
1 |
Assert.AreEqual(@"{
|
311 |
1 |
""description"": ""Type"",
|
312 |
1 |
""type"": [
|
313 |
1 |
""string"",
|
314 |
1 |
""array""
|
315 |
1 |
],
|
316 |
1 |
""items"": {},
|
317 |
1 |
""disallow"": ""any""
|
318 |
1 |
}", json);
|
319 |
1 |
}
|
320 |
||
321 |
[Test] |
|
322 |
public void WriteTo_MultipleItems() |
|
323 |
{ |
|
324 |
1 |
JsonSchema schema = JsonSchema.Parse(@"{ |
325 |
1 |
""items"":[{},{}]
|
326 |
1 |
}");
|
327 |
||
328 |
1 |
StringWriter writer = new StringWriter();
|
329 |
1 |
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
330 |
1 |
jsonWriter.Formatting = Formatting.Indented;
|
331 |
||
332 |
1 |
schema.WriteTo(jsonWriter);
|
333 |
||
334 |
1 |
string json = writer.ToString();
|
335 |
||
336 |
1 |
Assert.AreEqual(@"{
|
337 |
1 |
""items"": [
|
338 |
1 |
{},
|
339 |
1 |
{}
|
340 |
1 |
]
|
341 |
1 |
}", json);
|
342 |
1 |
}
|
343 |
} |
|
344 |
} |