All files
[pithos-ms-client] / trunk / Libraries / Json40r2 / Source / Src / Newtonsoft.Json.Tests / Schema / JsonSchemaTests.cs
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       JsonSchemaResolver resolver = new JsonSchemaResolver();
43
44       json = @"{
45   ""id"":""first"",
46   ""type"":""object"",
47   ""additionalProperties"":{}
48 }";
49
50       JsonSchema first = JsonSchema.Parse(json, resolver);
51
52       json =
53         @"{
54   ""id"":""second"",
55   ""type"":""object"",
56   ""extends"":{""$ref"":""first""},
57   ""additionalProperties"":{""type"":""string""}
58 }";
59
60       JsonSchema second = JsonSchema.Parse(json, resolver);
61       Assert.AreEqual(first, second.Extends);
62
63       json =
64         @"{
65   ""id"":""third"",
66   ""type"":""object"",
67   ""extends"":{""$ref"":""second""},
68   ""additionalProperties"":false
69 }";
70
71       JsonSchema third = JsonSchema.Parse(json, resolver);
72       Assert.AreEqual(second, third.Extends);
73       Assert.AreEqual(first, third.Extends.Extends);
74
75       StringWriter writer = new StringWriter();
76       JsonTextWriter jsonWriter = new JsonTextWriter(writer);
77       jsonWriter.Formatting = Formatting.Indented;
78
79       third.WriteTo(jsonWriter, resolver);
80
81       string writtenJson = writer.ToString();
82       Assert.AreEqual(@"{
83   ""id"": ""third"",
84   ""type"": ""object"",
85   ""additionalProperties"": false,
86   ""extends"": {
87     ""$ref"": ""second""
88   }
89 }", writtenJson);
90
91       StringWriter writer1 = new StringWriter();
92       JsonTextWriter jsonWriter1 = new JsonTextWriter(writer1);
93       jsonWriter1.Formatting = Formatting.Indented;
94
95       third.WriteTo(jsonWriter1);
96
97       writtenJson = writer1.ToString();
98       Assert.AreEqual(@"{
99   ""id"": ""third"",
100   ""type"": ""object"",
101   ""additionalProperties"": false,
102   ""extends"": {
103     ""id"": ""second"",
104     ""type"": ""object"",
105     ""additionalProperties"": {
106       ""type"": ""string""
107     },
108     ""extends"": {
109       ""id"": ""first"",
110       ""type"": ""object"",
111       ""additionalProperties"": {}
112     }
113   }
114 }", writtenJson);
115     }
116     [Test]
117     public void WriteTo_AdditionalProperties()
118     {
119       StringWriter writer = new StringWriter();
120       JsonTextWriter jsonWriter = new JsonTextWriter(writer);
121       jsonWriter.Formatting = Formatting.Indented;
122
123       JsonSchema schema = JsonSchema.Parse(@"{
124   ""description"":""AdditionalProperties"",
125   ""type"":[""string"", ""integer""],
126   ""additionalProperties"":{""type"":[""object"", ""boolean""]}
127 }");
128
129       schema.WriteTo(jsonWriter);
130
131       string json = writer.ToString();
132
133       Assert.AreEqual(@"{
134   ""description"": ""AdditionalProperties"",
135   ""type"": [
136     ""string"",
137     ""integer""
138   ],
139   ""additionalProperties"": {
140     ""type"": [
141       ""boolean"",
142       ""object""
143     ]
144   }
145 }", json);
146     }
147
148     [Test]
149     public void WriteTo_Properties()
150     {
151       JsonSchema schema = JsonSchema.Parse(@"{
152   ""description"":""A person"",
153   ""type"":""object"",
154   ""properties"":
155   {
156     ""name"":{""type"":""string""},
157     ""hobbies"":
158     {
159       ""type"":""array"",
160       ""items"": {""type"":""string""}
161     }
162   }
163 }");
164
165       StringWriter writer = new StringWriter();
166       JsonTextWriter jsonWriter = new JsonTextWriter(writer);
167       jsonWriter.Formatting = Formatting.Indented;
168
169       schema.WriteTo(jsonWriter);
170
171       string json = writer.ToString();
172
173       Assert.AreEqual(@"{
174   ""description"": ""A person"",
175   ""type"": ""object"",
176   ""properties"": {
177     ""name"": {
178       ""type"": ""string""
179     },
180     ""hobbies"": {
181       ""type"": ""array"",
182       ""items"": {
183         ""type"": ""string""
184       }
185     }
186   }
187 }", json);
188
189     }
190
191     [Test]
192     public void WriteTo_Enum()
193     {
194       JsonSchema schema = JsonSchema.Parse(@"{
195   ""description"":""Type"",
196   ""type"":[""string"",""array""],
197   ""items"":{},
198   ""enum"":[""string"",""object"",""array"",""boolean"",""number"",""integer"",""null"",""any""]
199 }");
200
201       StringWriter writer = new StringWriter();
202       JsonTextWriter jsonWriter = new JsonTextWriter(writer);
203       jsonWriter.Formatting = Formatting.Indented;
204
205       schema.WriteTo(jsonWriter);
206
207       string json = writer.ToString();
208
209       Assert.AreEqual(@"{
210   ""description"": ""Type"",
211   ""type"": [
212     ""string"",
213     ""array""
214   ],
215   ""items"": {},
216   ""enum"": [
217     ""string"",
218     ""object"",
219     ""array"",
220     ""boolean"",
221     ""number"",
222     ""integer"",
223     ""null"",
224     ""any""
225   ]
226 }", json);
227     }
228
229     [Test]
230     public void WriteTo_CircularReference()
231     {
232       string json = @"{
233   ""id"":""CircularReferenceArray"",
234   ""description"":""CircularReference"",
235   ""type"":[""array""],
236   ""items"":{""$ref"":""CircularReferenceArray""}
237 }";
238
239       JsonSchema schema = JsonSchema.Parse(json);
240
241       StringWriter writer = new StringWriter();
242       JsonTextWriter jsonWriter = new JsonTextWriter(writer);
243       jsonWriter.Formatting = Formatting.Indented;
244
245       schema.WriteTo(jsonWriter);
246
247       string writtenJson = writer.ToString();
248
249       Assert.AreEqual(@"{
250   ""id"": ""CircularReferenceArray"",
251   ""description"": ""CircularReference"",
252   ""type"": ""array"",
253   ""items"": {
254     ""$ref"": ""CircularReferenceArray""
255   }
256 }", writtenJson);
257     }
258
259     [Test]
260     public void WriteTo_DisallowMultiple()
261     {
262       JsonSchema schema = JsonSchema.Parse(@"{
263   ""description"":""Type"",
264   ""type"":[""string"",""array""],
265   ""items"":{},
266   ""disallow"":[""string"",""object"",""array""]
267 }");
268
269       StringWriter writer = new StringWriter();
270       JsonTextWriter jsonWriter = new JsonTextWriter(writer);
271       jsonWriter.Formatting = Formatting.Indented;
272
273       schema.WriteTo(jsonWriter);
274
275       string json = writer.ToString();
276
277       Assert.AreEqual(@"{
278   ""description"": ""Type"",
279   ""type"": [
280     ""string"",
281     ""array""
282   ],
283   ""items"": {},
284   ""disallow"": [
285     ""string"",
286     ""object"",
287     ""array""
288   ]
289 }", json);
290     }
291
292     [Test]
293     public void WriteTo_DisallowSingle()
294     {
295       JsonSchema schema = JsonSchema.Parse(@"{
296   ""description"":""Type"",
297   ""type"":[""string"",""array""],
298   ""items"":{},
299   ""disallow"":""any""
300 }");
301
302       StringWriter writer = new StringWriter();
303       JsonTextWriter jsonWriter = new JsonTextWriter(writer);
304       jsonWriter.Formatting = Formatting.Indented;
305
306       schema.WriteTo(jsonWriter);
307
308       string json = writer.ToString();
309
310       Assert.AreEqual(@"{
311   ""description"": ""Type"",
312   ""type"": [
313     ""string"",
314     ""array""
315   ],
316   ""items"": {},
317   ""disallow"": ""any""
318 }", json);
319     }
320
321     [Test]
322     public void WriteTo_MultipleItems()
323     {
324       JsonSchema schema = JsonSchema.Parse(@"{
325   ""items"":[{},{}]
326 }");
327
328       StringWriter writer = new StringWriter();
329       JsonTextWriter jsonWriter = new JsonTextWriter(writer);
330       jsonWriter.Formatting = Formatting.Indented;
331
332       schema.WriteTo(jsonWriter);
333
334       string json = writer.ToString();
335
336       Assert.AreEqual(@"{
337   ""items"": [
338     {},
339     {}
340   ]
341 }", json);
342     }
343
344     [Test]
345     public void ReadOptions()
346     {
347       JsonSchema schema = JsonSchema.Parse(@"{
348   ""type"": ""object"",
349   ""properties"": {
350     ""x"": {
351       ""type"": ""integer"",
352       ""enum"": [
353         0,
354         1,
355         -1
356       ],
357       ""options"": [
358         {
359           ""value"": 0,
360           ""label"": ""No""
361         },
362         {
363           ""value"": 1,
364           ""label"": ""Asc""
365         },
366         {
367           ""value"": -1,
368           ""label"": ""Desc""
369         }
370       ]
371     }
372   }
373 }");
374
375       Assert.AreEqual(schema.Properties["x"].Options.Count, 3);
376       Assert.AreEqual(schema.Properties["x"].Options[0], "No");
377       Assert.AreEqual(schema.Properties["x"].Options[1], "Asc");
378       Assert.AreEqual(schema.Properties["x"].Options[-1], "Desc");
379     }
380
381     [Test]
382     public void WriteTo_ExclusiveMinimum_ExclusiveMaximum()
383     {
384       JsonSchema schema = new JsonSchema();
385       schema.ExclusiveMinimum = true;
386       schema.ExclusiveMaximum = true;
387
388       StringWriter writer = new StringWriter();
389       JsonTextWriter jsonWriter = new JsonTextWriter(writer);
390       jsonWriter.Formatting = Formatting.Indented;
391
392       schema.WriteTo(jsonWriter);
393
394       string json = writer.ToString();
395
396       Assert.AreEqual(@"{
397   ""exclusiveMinimum"": true,
398   ""exclusiveMaximum"": true
399 }", json);
400     }
401
402     [Test]
403     public void WriteTo_PatternProperties()
404     {
405       JsonSchema schema = new JsonSchema();
406       schema.PatternProperties = new Dictionary<string, JsonSchema>
407         {
408           { "[abc]", new JsonSchema() }
409         };
410
411       StringWriter writer = new StringWriter();
412       JsonTextWriter jsonWriter = new JsonTextWriter(writer);
413       jsonWriter.Formatting = Formatting.Indented;
414
415       schema.WriteTo(jsonWriter);
416
417       string json = writer.ToString();
418
419       Assert.AreEqual(@"{
420   ""patternProperties"": {
421     ""[abc]"": {}
422   }
423 }", json);
424     }
425   }
426 }