Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json.Tests\Linq\JObjectTests.cs

Symbol Coverage: 96.66% (666 of 689)

Branch Coverage: 76.77% (76 of 99)

Cyclomatic Complexity Avg: 1.08 Max:3

Code Lines: 812


L V Source
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Linq;
5
using System.Text;
6
using Newtonsoft.Json.Tests.TestObjects;
7
using NUnit.Framework;
8
using Newtonsoft.Json.Linq;
9
using Newtonsoft.Json.Converters;
10
using System.IO;
11
using System.Collections;
12
using System.Collections.Specialized;
13
#if !PocketPC && !SILVERLIGHT
14
using System.Web.UI;
15
#endif
16

  
17
namespace Newtonsoft.Json.Tests.Linq
18
{
19
  public class JObjectTests : TestFixtureBase
20
  {
21
    [Test]
22
    public void TryGetValue()
23
    {
24
 1
      JObject o = new JObject();
25
 1
      o.Add("PropertyNameValue", new JValue(1));
26
 1
      Assert.AreEqual(1, o.Children().Count());
27

  
28
      JToken t;
29
 1
      Assert.AreEqual(false, o.TryGetValue("sdf", out t));
30
 1
      Assert.AreEqual(null, t);
31

  
32
 1
      Assert.AreEqual(false, o.TryGetValue(null, out t));
33
 1
      Assert.AreEqual(null, t);
34

  
35
 1
      Assert.AreEqual(true, o.TryGetValue("PropertyNameValue", out t));
36
 1
      Assert.AreEqual(true, JToken.DeepEquals(new JValue(1), t));
37
 1
    }
38

  
39
    [Test]
40
    public void DictionaryItemShouldSet()
41
    {
42
 1
      JObject o = new JObject();
43
 1
      o["PropertyNameValue"] = new JValue(1);
44
 1
      Assert.AreEqual(1, o.Children().Count());
45

  
46
      JToken t;
47
 1
      Assert.AreEqual(true, o.TryGetValue("PropertyNameValue", out t));
48
 1
      Assert.AreEqual(true, JToken.DeepEquals(new JValue(1), t));
49

  
50
 1
      o["PropertyNameValue"] = new JValue(2);
51
 1
      Assert.AreEqual(1, o.Children().Count());
52

  
53
 1
      Assert.AreEqual(true, o.TryGetValue("PropertyNameValue", out t));
54
 1
      Assert.AreEqual(true, JToken.DeepEquals(new JValue(2), t));
55

  
56
 1
      o["PropertyNameValue"] = null;
57
 1
      Assert.AreEqual(1, o.Children().Count());
58

  
59
 1
      Assert.AreEqual(true, o.TryGetValue("PropertyNameValue", out t));
60
 1
      Assert.AreEqual(true, JToken.DeepEquals(new JValue((object)null), t));
61
 1
    }
62

  
63
    [Test]
64
    public void Remove()
65
    {
66
 1
      JObject o = new JObject();
67
 1
      o.Add("PropertyNameValue", new JValue(1));
68
 1
      Assert.AreEqual(1, o.Children().Count());
69

  
70
 1
      Assert.AreEqual(false, o.Remove("sdf"));
71
 1
      Assert.AreEqual(false, o.Remove(null));
72
 1
      Assert.AreEqual(true, o.Remove("PropertyNameValue"));
73

  
74
 1
      Assert.AreEqual(0, o.Children().Count());
75
 1
    }
76

  
77
    [Test]
78
    public void GenericCollectionRemove()
79
    {
80
 1
      JValue v = new JValue(1);
81
 1
      JObject o = new JObject();
82
 1
      o.Add("PropertyNameValue", v);
83
 1
      Assert.AreEqual(1, o.Children().Count());
84

  
85
 1
      Assert.AreEqual(false, ((ICollection<KeyValuePair<string, JToken>>)o).Remove(new KeyValuePair<string, JToken>("PropertyNameValue1", new JValue(1))));
86
 1
      Assert.AreEqual(false, ((ICollection<KeyValuePair<string, JToken>>)o).Remove(new KeyValuePair<string, JToken>("PropertyNameValue", new JValue(2))));
87
 1
      Assert.AreEqual(false, ((ICollection<KeyValuePair<string, JToken>>)o).Remove(new KeyValuePair<string, JToken>("PropertyNameValue", new JValue(1))));
88
 1
      Assert.AreEqual(true, ((ICollection<KeyValuePair<string, JToken>>)o).Remove(new KeyValuePair<string, JToken>("PropertyNameValue", v)));
89

  
90
 1
      Assert.AreEqual(0, o.Children().Count());
91
 1
    }
92

  
93
    [Test]
94
    [ExpectedException(typeof(ArgumentException), ExpectedMessage = "Can not add property PropertyNameValue to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object.")]
95
    public void DuplicatePropertyNameShouldThrow()
96
    {
97
 1
      JObject o = new JObject();
98
 1
      o.Add("PropertyNameValue", null);
99
 1
      o.Add("PropertyNameValue", null);
100
 0
    }
101

  
102
    [Test]
103
    public void GenericDictionaryAdd()
104
    {
105
 1
      JObject o = new JObject();
106

  
107
 1
      o.Add("PropertyNameValue", new JValue(1));
108
 1
      Assert.AreEqual(1, (int)o["PropertyNameValue"]);
109

  
110
 1
      o.Add("PropertyNameValue1", null);
111
 1
      Assert.AreEqual(null, ((JValue)o["PropertyNameValue1"]).Value);
112

  
113
 1
      Assert.AreEqual(2, o.Children().Count());
114
 1
    }
115

  
116
    [Test]
117
    public void GenericCollectionAdd()
118
    {
119
 1
      JObject o = new JObject();
120
 1
      ((ICollection<KeyValuePair<string,JToken>>)o).Add(new KeyValuePair<string,JToken>("PropertyNameValue", new JValue(1)));
121

  
122
 1
      Assert.AreEqual(1, (int)o["PropertyNameValue"]);
123
 1
      Assert.AreEqual(1, o.Children().Count());
124
 1
    }
125

  
126
    [Test]
127
    public void GenericCollectionClear()
128
    {
129
 1
      JObject o = new JObject();
130
 1
      o.Add("PropertyNameValue", new JValue(1));
131
 1
      Assert.AreEqual(1, o.Children().Count());
132

  
133
 1
      JProperty p = (JProperty)o.Children().ElementAt(0);
134

  
135
 1
      ((ICollection<KeyValuePair<string, JToken>>)o).Clear();
136
 1
      Assert.AreEqual(0, o.Children().Count());
137

  
138
 1
      Assert.AreEqual(null, p.Parent);
139
 1
    }
140

  
141
    [Test]
142
    public void GenericCollectionContains()
143
    {
144
 1
      JValue v = new JValue(1);
145
 1
      JObject o = new JObject();
146
 1
      o.Add("PropertyNameValue", v);
147
 1
      Assert.AreEqual(1, o.Children().Count());
148

  
149
 1
      bool contains = ((ICollection<KeyValuePair<string, JToken>>)o).Contains(new KeyValuePair<string, JToken>("PropertyNameValue", new JValue(1)));
150
 1
      Assert.AreEqual(false, contains);
151

  
152
 1
      contains = ((ICollection<KeyValuePair<string, JToken>>)o).Contains(new KeyValuePair<string, JToken>("PropertyNameValue", v));
153
 1
      Assert.AreEqual(true, contains);
154

  
155
 1
      contains = ((ICollection<KeyValuePair<string, JToken>>)o).Contains(new KeyValuePair<string, JToken>("PropertyNameValue", new JValue(2)));
156
 1
      Assert.AreEqual(false, contains);
157

  
158
 1
      contains = ((ICollection<KeyValuePair<string, JToken>>)o).Contains(new KeyValuePair<string, JToken>("PropertyNameValue1", new JValue(1)));
159
 1
      Assert.AreEqual(false, contains);
160

  
161
 1
      contains = ((ICollection<KeyValuePair<string, JToken>>)o).Contains(default(KeyValuePair<string, JToken>));
162
 1
      Assert.AreEqual(false, contains);
163
 1
    }
164

  
165
    [Test]
166
    public void GenericDictionaryContains()
167
    {
168
 1
      JObject o = new JObject();
169
 1
      o.Add("PropertyNameValue", new JValue(1));
170
 1
      Assert.AreEqual(1, o.Children().Count());
171

  
172
 1
      bool contains = ((IDictionary<string, JToken>)o).ContainsKey("PropertyNameValue");
173
 1
      Assert.AreEqual(true, contains);
174
 1
    }
175

  
176
    [Test]
177
    public void GenericCollectionCopyTo()
178
    {
179
 1
      JObject o = new JObject();
180
 1
      o.Add("PropertyNameValue", new JValue(1));
181
 1
      o.Add("PropertyNameValue2", new JValue(2));
182
 1
      o.Add("PropertyNameValue3", new JValue(3));
183
 1
      Assert.AreEqual(3, o.Children().Count());
184

  
185
 1
      KeyValuePair<string, JToken>[] a = new KeyValuePair<string,JToken>[5];
186

  
187
 1
      ((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(a, 1);
188

  
189
 1
      Assert.AreEqual(default(KeyValuePair<string,JToken>), a[0]);
190
      
191
 1
      Assert.AreEqual("PropertyNameValue", a[1].Key);
192
 1
      Assert.AreEqual(1, (int)a[1].Value);
193

  
194
 1
      Assert.AreEqual("PropertyNameValue2", a[2].Key);
195
 1
      Assert.AreEqual(2, (int)a[2].Value);
196

  
197
 1
      Assert.AreEqual("PropertyNameValue3", a[3].Key);
198
 1
      Assert.AreEqual(3, (int)a[3].Value);
199

  
200
 1
      Assert.AreEqual(default(KeyValuePair<string, JToken>), a[4]);
201
 1
    }
202

  
203
    [Test]
204
    [ExpectedException(typeof(ArgumentNullException), ExpectedMessage = @"Value cannot be null.
205
Parameter name: array")]
206
    public void GenericCollectionCopyToNullArrayShouldThrow()
207
    {
208
 1
      JObject o = new JObject();
209
 1
      ((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(null, 0);
210
 0
    }
211

  
212
    [Test]
213
    [ExpectedException(typeof(ArgumentOutOfRangeException), ExpectedMessage = @"arrayIndex is less than 0.
214
Parameter name: arrayIndex")]
215
    public void GenericCollectionCopyToNegativeArrayIndexShouldThrow()
216
    {
217
 1
      JObject o = new JObject();
218
 1
      ((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(new KeyValuePair<string, JToken>[1], -1);
219
 0
    }
220

  
221
    [Test]
222
    [ExpectedException(typeof(ArgumentException), ExpectedMessage = @"arrayIndex is equal to or greater than the length of array.")]
223
    public void GenericCollectionCopyToArrayIndexEqualGreaterToArrayLengthShouldThrow()
224
    {
225
 1
      JObject o = new JObject();
226
 1
      ((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(new KeyValuePair<string, JToken>[1], 1);
227
 0
    }
228

  
229
    [Test]
230
    [ExpectedException(typeof(ArgumentException), ExpectedMessage = @"The number of elements in the source JObject is greater than the available space from arrayIndex to the end of the destination array.")]
231
    public void GenericCollectionCopyToInsufficientArrayCapacity()
232
    {
233
 1
      JObject o = new JObject();
234
 1
      o.Add("PropertyNameValue", new JValue(1));
235
 1
      o.Add("PropertyNameValue2", new JValue(2));
236
 1
      o.Add("PropertyNameValue3", new JValue(3));
237

  
238
 1
      ((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(new KeyValuePair<string, JToken>[3], 1);
239
 0
    }
240

  
241
    [Test]
242
    public void FromObjectRaw()
243
    {
244
 1
      PersonRaw raw = new PersonRaw
245
 1
      {
246
 1
        FirstName = "FirstNameValue",
247
 1
        RawContent = new JRaw("[1,2,3,4,5]"),
248
 1
        LastName = "LastNameValue"
249
 1
      };
250

  
251
 1
      JObject o = JObject.FromObject(raw);
252

  
253
 1
      Assert.AreEqual("FirstNameValue", (string)o["first_name"]);
254
 1
      Assert.AreEqual(JTokenType.Raw, ((JValue)o["RawContent"]).Type);
255
 1
      Assert.AreEqual("[1,2,3,4,5]", (string)o["RawContent"]);
256
 1
      Assert.AreEqual("LastNameValue", (string)o["last_name"]);
257
 1
    }
258

  
259
    [Test]
260
    public void JTokenReader()
261
    {
262
 1
      PersonRaw raw = new PersonRaw
263
 1
      {
264
 1
        FirstName = "FirstNameValue",
265
 1
        RawContent = new JRaw("[1,2,3,4,5]"),
266
 1
        LastName = "LastNameValue"
267
 1
      };
268

  
269
 1
      JObject o = JObject.FromObject(raw);
270

  
271
 1
      JsonReader reader = new JTokenReader(o);
272

  
273
 1
      Assert.IsTrue(reader.Read());
274
 1
      Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
275

  
276
 1
      Assert.IsTrue(reader.Read());
277
 1
      Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
278

  
279
 1
      Assert.IsTrue(reader.Read());
280
 1
      Assert.AreEqual(JsonToken.String, reader.TokenType);
281

  
282
 1
      Assert.IsTrue(reader.Read());
283
 1
      Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
284

  
285
 1
      Assert.IsTrue(reader.Read());
286
 1
      Assert.AreEqual(JsonToken.Raw, reader.TokenType);
287

  
288
 1
      Assert.IsTrue(reader.Read());
289
 1
      Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
290

  
291
 1
      Assert.IsTrue(reader.Read());
292
 1
      Assert.AreEqual(JsonToken.String, reader.TokenType);
293

  
294
 1
      Assert.IsTrue(reader.Read());
295
 1
      Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
296

  
297
 1
      Assert.IsFalse(reader.Read());
298
 1
    }
299

  
300
    [Test]
301
    public void DeserializeFromRaw()
302
    {
303
 1
      PersonRaw raw = new PersonRaw
304
 1
      {
305
 1
        FirstName = "FirstNameValue",
306
 1
        RawContent = new JRaw("[1,2,3,4,5]"),
307
 1
        LastName = "LastNameValue"
308
 1
      };
309

  
310
 1
      JObject o = JObject.FromObject(raw);
311

  
312
 1
      JsonReader reader = new JTokenReader(o);
313
 1
      JsonSerializer serializer = new JsonSerializer();
314
 1
      raw = (PersonRaw)serializer.Deserialize(reader, typeof(PersonRaw));
315

  
316
 1
      Assert.AreEqual("FirstNameValue", raw.FirstName);
317
 1
      Assert.AreEqual("LastNameValue", raw.LastName);
318
 1
      Assert.AreEqual("[1,2,3,4,5]", raw.RawContent.Value);
319
 1
    }
320

  
321
    [Test]
322
    [ExpectedException(typeof(Exception), ExpectedMessage = "Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray")]
323
    public void Parse_ShouldThrowOnUnexpectedToken()
324
    {
325
 1
      string json = @"[""prop""]";
326
 1
      JObject.Parse(json);
327
 0
    }
328

  
329
    [Test]
330
    public void ParseJavaScriptDate()
331
    {
332
 1
      string json = @"[new Date(1207285200000)]";
333

  
334
 1
      JArray a = (JArray)JsonConvert.DeserializeObject(json, null);
335
 1
      JValue v = (JValue)a[0];
336

  
337
 1
      Assert.AreEqual(JsonConvert.ConvertJavaScriptTicksToDateTime(1207285200000), (DateTime)v);
338
 1
    }
339

  
340
    [Test]
341
    public void GenericValueCast()
342
    {
343
 1
      string json = @"{""foo"":true}";
344
 1
      JObject o = (JObject)JsonConvert.DeserializeObject(json);
345
 1
      bool? value = o.Value<bool?>("foo");
346
 1
      Assert.AreEqual(true, value);
347

  
348
 1
      json = @"{""foo"":null}"; 
349
 1
      o = (JObject)JsonConvert.DeserializeObject(json);
350
 1
      value = o.Value<bool?>("foo");
351
 1
      Assert.AreEqual(null, value);
352
 1
    }
353

  
354
    [Test]
355
    [ExpectedException(typeof(JsonReaderException), ExpectedMessage = "Invalid property identifier character: ]. Line 3, position 9.")]
356
    public void Blog()
357
    {
358
 1
      JObject person = JObject.Parse(@"{
359
 1
        ""name"": ""James"",
360
 1
        ]!#$THIS IS: BAD JSON![{}}}}]
361
 1
      }");
362

  
363
      // Invalid property identifier character: ]. Line 3, position 9.
364
 0
    }
365

  
366
    [Test]
367
    public void RawChildValues()
368
    {
369
 1
      JObject o = new JObject();
370
 1
      o["val1"] = new JRaw("1");
371
 1
      o["val2"] = new JRaw("1");
372

  
373
 1
      string json = o.ToString();
374

  
375
 1
      Assert.AreEqual(@"{
376
 1
  ""val1"": 1,
377
 1
  ""val2"": 1
378
 1
}", json);
379
 1
    }
380

  
381
    [Test]
382
    public void Iterate()
383
    {
384
 1
      JObject o = new JObject();
385
 1
      o.Add("PropertyNameValue1", new JValue(1));
386
 1
      o.Add("PropertyNameValue2", new JValue(2));
387

  
388
 1
      JToken t = o;
389

  
390
 1
      int i = 1;
391
 1
      foreach (JProperty property in t)
392
      {
393
 2
        Assert.AreEqual("PropertyNameValue" + i, property.Name);
394
 2
        Assert.AreEqual(i, (int)property.Value);
395

  
396
 2
        i++;
397
      }
398
 1
    }
399

  
400
    [Test]
401
    public void KeyValuePairIterate()
402
    {
403
 1
      JObject o = new JObject();
404
 1
      o.Add("PropertyNameValue1", new JValue(1));
405
 1
      o.Add("PropertyNameValue2", new JValue(2));
406

  
407
 1
      int i = 1;
408
 1
      foreach (KeyValuePair<string, JToken> pair in o)
409
      {
410
 2
        Assert.AreEqual("PropertyNameValue" + i, pair.Key);
411
 2
        Assert.AreEqual(i, (int)pair.Value);
412

  
413
 2
        i++;
414
      }
415
 1
    }
416

  
417
    [Test]
418
    public void WriteObjectNullStringValue()
419
    {
420
 1
      string s = null;
421
 1
      JValue v = new JValue(s);
422
 1
      Assert.AreEqual(null, v.Value);
423
 1
      Assert.AreEqual(JTokenType.String, v.Type);
424

  
425
 1
      JObject o = new JObject();
426
 1
      o["title"] = v;
427

  
428
 1
      string output = o.ToString();
429
      
430
 1
      Assert.AreEqual(@"{
431
 1
  ""title"": null
432
 1
}", output);
433
 1
    }
434

  
435
    [Test]
436
    public void Example()
437
    {
438
 1
      string json = @"{
439
 1
        ""Name"": ""Apple"",
440
 1
        ""Expiry"": new Date(1230422400000),
441
 1
        ""Price"": 3.99,
442
 1
        ""Sizes"": [
443
 1
          ""Small"",
444
 1
          ""Medium"",
445
 1
          ""Large""
446
 1
        ]
447
 1
      }";
448

  
449
 1
      JObject o = JObject.Parse(json);
450

  
451
 1
      string name = (string)o["Name"];
452
      // Apple
453

  
454
 1
      JArray sizes = (JArray)o["Sizes"];
455

  
456
 1
      string smallest = (string)sizes[0];
457
      // Small
458

  
459
 1
      Console.WriteLine(name);
460
 1
      Console.WriteLine(smallest);
461
 1
    }
462

  
463
    [Test]
464
    public void DeserializeClassManually()
465
    {
466
 1
      string jsonText = @"{
467
 1
	      ""short"":{
468
 1
		      ""original"":""http://www.foo.com/"",
469
 1
		      ""short"":""krehqk"",
470
 1
		      ""error"":{
471
 1
			      ""code"":0,
472
 1
			      ""msg"":""No action taken""}
473
 1
		  }";
474

  
475
 1
      JObject json = JObject.Parse(jsonText);
476

  
477
 1
      Shortie shortie = new Shortie
478
 1
                        {
479
 1
                          Original = (string)json["short"]["original"],
480
 1
                          Short = (string)json["short"]["short"],
481
 1
                          Error = new ShortieException
482
 1
                                  {
483
 1
                                    Code = (int)json["short"]["error"]["code"],
484
 1
                                    ErrorMessage = (string)json["short"]["error"]["msg"]
485
 1
                                  }
486
 1
                        };
487

  
488
 1
      Console.WriteLine(shortie.Original);
489
      // http://www.foo.com/
490

  
491
 1
      Console.WriteLine(shortie.Error.ErrorMessage);
492
      // No action taken
493

  
494
 1
      Assert.AreEqual("http://www.foo.com/", shortie.Original);
495
 1
      Assert.AreEqual("krehqk", shortie.Short);
496
 1
      Assert.AreEqual(null, shortie.Shortened);
497
 1
      Assert.AreEqual(0, shortie.Error.Code);
498
 1
      Assert.AreEqual("No action taken", shortie.Error.ErrorMessage);
499
 1
    }
500

  
501
    [Test]
502
    public void JObjectContainingHtml()
503
    {
504
 1
      JObject o = new JObject();
505
 1
      o["rc"] = new JValue(200);
506
 1
      o["m"] = new JValue("");
507
 1
      o["o"] = new JValue(@"<div class='s1'>
508
 1
    <div class='avatar'>                    
509
 1
        <a href='asdf'>asdf</a><br />
510
 1
        <strong>0</strong>
511
 1
    </div>
512
 1
    <div class='sl'>
513
 1
        <p>
514
 1
            444444444
515
 1
        </p>
516
 1
    </div>
517
 1
    <div class='clear'>
518
 1
    </div>                        
519
 1
</div>");
520

  
521
 1
      Assert.AreEqual(@"{
522
 1
  ""rc"": 200,
523
 1
  ""m"": """",
524
 1
  ""o"": ""<div class='s1'>\r\n    <div class='avatar'>                    \r\n        <a href='asdf'>asdf</a><br />\r\n        <strong>0</strong>\r\n    </div>\r\n    <div class='sl'>\r\n        <p>\r\n            444444444\r\n        </p>\r\n    </div>\r\n    <div class='clear'>\r\n    </div>                        \r\n</div>""
525
 1
}", o.ToString());
526
 1
    }
527

  
528
    [Test]
529
    public void ImplicitValueConversions()
530
    {
531
 1
      JObject moss = new JObject();
532
 1
      moss["FirstName"] = new JValue("Maurice");
533
 1
      moss["LastName"] = new JValue("Moss");
534
 1
      moss["BirthDate"] = new JValue(new DateTime(1977, 12, 30));
535
 1
      moss["Department"] = new JValue("IT");
536
 1
      moss["JobTitle"] = new JValue("Support");
537

  
538
 1
      Console.WriteLine(moss.ToString());
539
      //{
540
      //  "FirstName": "Maurice",
541
      //  "LastName": "Moss",
542
      //  "BirthDate": "\/Date(252241200000+1300)\/",
543
      //  "Department": "IT",
544
      //  "JobTitle": "Support"
545
      //}
546

  
547

  
548
 1
      JObject jen = new JObject();
549
 1
      jen["FirstName"] = "Jen";
550
 1
      jen["LastName"] = "Barber";
551
 1
      jen["BirthDate"] = new DateTime(1978, 3, 15);
552
 1
      jen["Department"] = "IT";
553
 1
      jen["JobTitle"] = "Manager";
554

  
555
 1
      Console.WriteLine(jen.ToString());
556
      //{
557
      //  "FirstName": "Jen",
558
      //  "LastName": "Barber",
559
      //  "BirthDate": "\/Date(258721200000+1300)\/",
560
      //  "Department": "IT",
561
      //  "JobTitle": "Manager"
562
      //}
563
 1
    }
564

  
565
    [Test]
566
    public void ReplaceJPropertyWithJPropertyWithSameName()
567
    {
568
 1
      JProperty p1 = new JProperty("Test1", 1);
569
 1
      JProperty p2 = new JProperty("Test2", "Two");
570

  
571
 1
      JObject o = new JObject(p1, p2);
572
 1
      IList l = o;
573
 1
      Assert.AreEqual(p1, l[0]);
574
 1
      Assert.AreEqual(p2, l[1]);
575

  
576
 1
      JProperty p3 = new JProperty("Test1", "III");
577

  
578
 1
      p1.Replace(p3);
579
 1
      Assert.AreEqual(null, p1.Parent);
580
 1
      Assert.AreEqual(l, p3.Parent);
581

  
582
 1
      Assert.AreEqual(p3, l[0]);
583
 1
      Assert.AreEqual(p2, l[1]);
584

  
585
 1
      Assert.AreEqual(2, l.Count);
586
 1
      Assert.AreEqual(2, o.Properties().Count());
587

  
588
 1
      JProperty p4 = new JProperty("Test4", "IV");
589

  
590
 1
      p2.Replace(p4);
591
 1
      Assert.AreEqual(null, p2.Parent);
592
 1
      Assert.AreEqual(l, p4.Parent);
593

  
594
 1
      Assert.AreEqual(p3, l[0]);
595
 1
      Assert.AreEqual(p4, l[1]);
596
 1
    }
597

  
598
#if !PocketPC && !SILVERLIGHT && !NET20
599
    [Test]
600
    public void PropertyChanging()
601
    {
602
 1
      object changing = null;
603
 1
      object changed = null;
604
 1
      int changingCount = 0;
605
 1
      int changedCount = 0;
606

  
607
 1
      JObject o = new JObject();
608
 1
      o.PropertyChanging += (sender, args) =>
609
 1
        {
610
 1
          JObject s = (JObject) sender;
611
 1
          changing = (s[args.PropertyName] != null) ? ((JValue)s[args.PropertyName]).Value : null;
612
 1
          changingCount++;
613
 1
        };
614
 1
      o.PropertyChanged += (sender, args) =>
615
 1
      {
616
 1
        JObject s = (JObject)sender;
617
 1
        changed = (s[args.PropertyName] != null) ? ((JValue)s[args.PropertyName]).Value : null;
618
 1
        changedCount++;
619
 1
      };
620

  
621
 1
      o["StringValue"] = "value1";
622
 1
      Assert.AreEqual(null, changing);
623
 1
      Assert.AreEqual("value1", changed);
624
 1
      Assert.AreEqual("value1", (string)o["StringValue"]);
625
 1
      Assert.AreEqual(1, changingCount);
626
 1
      Assert.AreEqual(1, changedCount);
627

  
628
 1
      o["StringValue"] = "value1";
629
 1
      Assert.AreEqual(1, changingCount);
630
 1
      Assert.AreEqual(1, changedCount);
631

  
632
 1
      o["StringValue"] = "value2";
633
 1
      Assert.AreEqual("value1", changing);
634
 1
      Assert.AreEqual("value2", changed);
635
 1
      Assert.AreEqual("value2", (string)o["StringValue"]);
636
 1
      Assert.AreEqual(2, changingCount);
637
 1
      Assert.AreEqual(2, changedCount);
638

  
639
 1
      o["StringValue"] = null;
640
 1
      Assert.AreEqual("value2", changing);
641
 1
      Assert.AreEqual(null, changed);
642
 1
      Assert.AreEqual(null, (string)o["StringValue"]);
643
 1
      Assert.AreEqual(3, changingCount);
644
 1
      Assert.AreEqual(3, changedCount);
645

  
646
 1
      o["NullValue"] = null;
647
 1
      Assert.AreEqual(null, changing);
648
 1
      Assert.AreEqual(null, changed);
649
 1
      Assert.AreEqual(new JValue((object)null), o["NullValue"]);
650
 1
      Assert.AreEqual(4, changingCount);
651
 1
      Assert.AreEqual(4, changedCount);
652

  
653
 1
      o["NullValue"] = null;
654
 1
      Assert.AreEqual(4, changingCount);
655
 1
      Assert.AreEqual(4, changedCount);
656
 1
    }
657
#endif
658

  
659
    [Test]
660
    public void PropertyChanged()
661
    {
662
 1
      object changed = null;
663
 1
      int changedCount = 0;
664

  
665
 1
      JObject o = new JObject();
666
 1
      o.PropertyChanged += (sender, args) =>
667
 1
      {
668
 1
        JObject s = (JObject)sender;
669
 1
        changed = (s[args.PropertyName] != null) ? ((JValue)s[args.PropertyName]).Value : null;
670
 1
        changedCount++;
671
 1
      };
672

  
673
 1
      o["StringValue"] = "value1";
674
 1
      Assert.AreEqual("value1", changed);
675
 1
      Assert.AreEqual("value1", (string)o["StringValue"]);
676
 1
      Assert.AreEqual(1, changedCount);
677

  
678
 1
      o["StringValue"] = "value1";
679
 1
      Assert.AreEqual(1, changedCount);
680

  
681
 1
      o["StringValue"] = "value2";
682
 1
      Assert.AreEqual("value2", changed);
683
 1
      Assert.AreEqual("value2", (string)o["StringValue"]);
684
 1
      Assert.AreEqual(2, changedCount);
685

  
686
 1
      o["StringValue"] = null;
687
 1
      Assert.AreEqual(null, changed);
688
 1
      Assert.AreEqual(null, (string)o["StringValue"]);
689
 1
      Assert.AreEqual(3, changedCount);
690

  
691
 1
      o["NullValue"] = null;
692
 1
      Assert.AreEqual(null, changed);
693
 1
      Assert.AreEqual(new JValue((object)null), o["NullValue"]);
694
 1
      Assert.AreEqual(4, changedCount);
695

  
696
 1
      o["NullValue"] = null;
697
 1
      Assert.AreEqual(4, changedCount);
698
 1
    }
699

  
700
    [Test]
701
    public void IListContains()
702
    {
703
 1
      JProperty p = new JProperty("Test", 1);
704
 1
      IList l = new JObject(p);
705

  
706
 1
      Assert.IsTrue(l.Contains(p));
707
 1
      Assert.IsFalse(l.Contains(new JProperty("Test", 1)));
708
 1
    }
709

  
710
    [Test]
711
    public void IListIndexOf()
712
    {
713
 1
      JProperty p = new JProperty("Test", 1);
714
 1
      IList l = new JObject(p);
715

  
716
 1
      Assert.AreEqual(0, l.IndexOf(p));
717
 1
      Assert.AreEqual(-1, l.IndexOf(new JProperty("Test", 1)));
718
 1
    }
719

  
720
    [Test]
721
    public void IListClear()
722
    {
723
 1
      JProperty p = new JProperty("Test", 1);
724
 1
      IList l = new JObject(p);
725

  
726
 1
      Assert.AreEqual(1, l.Count);
727

  
728
 1
      l.Clear();
729

  
730
 1
      Assert.AreEqual(0, l.Count);
731
 1
    }
732

  
733
    [Test]
734
    public void IListCopyTo()
735
    {
736
 1
      JProperty p1 = new JProperty("Test1", 1);
737
 1
      JProperty p2 = new JProperty("Test2", "Two");
738
 1
      IList l = new JObject(p1, p2);
739

  
740
 1
      object[] a = new object[l.Count];
741

  
742
 1
      l.CopyTo(a, 0);
743

  
744
 1
      Assert.AreEqual(p1, a[0]);
745
 1
      Assert.AreEqual(p2, a[1]);
746
 1
    }
747

  
748
    [Test]
749
    public void IListAdd()
750
    {
751
 1
      JProperty p1 = new JProperty("Test1", 1);
752
 1
      JProperty p2 = new JProperty("Test2", "Two");
753
 1
      IList l = new JObject(p1, p2);
754

  
755
 1
      JProperty p3 = new JProperty("Test3", "III");
756

  
757
 1
      l.Add(p3);
758

  
759
 1
      Assert.AreEqual(3, l.Count);
760
 1
      Assert.AreEqual(p3, l[2]);
761
 1
    }
762

  
763
    [Test]
764
    [ExpectedException(typeof(ArgumentException), ExpectedMessage = "Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject.")]
765
    public void IListAddBadToken()
766
    {
767
 1
      JProperty p1 = new JProperty("Test1", 1);
768
 1
      JProperty p2 = new JProperty("Test2", "Two");
769
 1
      IList l = new JObject(p1, p2);
770

  
771
 1
      l.Add(new JValue("Bad!"));
772
 0
    }
773

  
774
    [Test]
775
    [ExpectedException(typeof(ArgumentException), ExpectedMessage = "Argument is not a JToken.")]
776
    public void IListAddBadValue()
777
    {
778
 1
      JProperty p1 = new JProperty("Test1", 1);
779
 1
      JProperty p2 = new JProperty("Test2", "Two");
780
 1
      IList l = new JObject(p1, p2);
781

  
782
 1
      l.Add("Bad!");
783
 0
    }
784

  
785
    [Test]
786
    [ExpectedException(typeof(ArgumentException), ExpectedMessage = "Can not add property Test2 to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object.")]
787
    public void IListAddPropertyWithExistingName()
788
    {
789
 1
      JProperty p1 = new JProperty("Test1", 1);
790
 1
      JProperty p2 = new JProperty("Test2", "Two");
791
 1
      IList l = new JObject(p1, p2);
792

  
793
 1
      JProperty p3 = new JProperty("Test2", "II");
794

  
795
 1
      l.Add(p3);
796
 0
    }
797

  
798
    [Test]
799
    public void IListRemove()
800
    {
801
 1
      JProperty p1 = new JProperty("Test1", 1);
802
 1
      JProperty p2 = new JProperty("Test2", "Two");
803
 1
      IList l = new JObject(p1, p2);
804

  
805
 1
      JProperty p3 = new JProperty("Test3", "III");
806

  
807
      // won't do anything
808
 1
      l.Remove(p3);
809
 1
      Assert.AreEqual(2, l.Count);
810

  
811
 1
      l.Remove(p1);
812
 1
      Assert.AreEqual(1, l.Count);
813
 1
      Assert.IsFalse(l.Contains(p1));
814
 1
      Assert.IsTrue(l.Contains(p2));
815

  
816
 1
      l.Remove(p2);
817
 1
      Assert.AreEqual(0, l.Count);
818
 1
      Assert.IsFalse(l.Contains(p2));
819
 1
      Assert.AreEqual(null, p2.Parent);
820
 1
    }
821

  
822
    [Test]
823
    public void IListRemoveAt()
824
    {
825
 1
      JProperty p1 = new JProperty("Test1", 1);
826
 1
      JProperty p2 = new JProperty("Test2", "Two");
827
 1
      IList l = new JObject(p1, p2);
828

  
829
      // won't do anything
830
 1
      l.RemoveAt(0);
831

  
832
 1
      l.Remove(p1);
833
 1
      Assert.AreEqual(1, l.Count);
834

  
835
 1
      l.Remove(p2);
836
 1
      Assert.AreEqual(0, l.Count);
837
 1
    }
838

  
839
    [Test]
840
    public void IListInsert()
841
    {
842
 1
      JProperty p1 = new JProperty("Test1", 1);
843
 1
      JProperty p2 = new JProperty("Test2", "Two");
844
 1
      IList l = new JObject(p1, p2);
845

  
846
 1
      JProperty p3 = new JProperty("Test3", "III");
847

  
848
 1
      l.Insert(1, p3);
849
 1
      Assert.AreEqual(l, p3.Parent);
850

  
851
 1
      Assert.AreEqual(p1, l[0]);
852
 1
      Assert.AreEqual(p3, l[1]);
853
 1
      Assert.AreEqual(p2, l[2]);
854
 1
    }
855

  
856
    [Test]
857
    public void IListIsReadOnly()
858
    {
859
 1
      IList l = new JObject();
860
 1
      Assert.IsFalse(l.IsReadOnly);
861
 1
    }
862

  
863
    [Test]
864
    public void IListIsFixedSize()
865
    {
866
 1
      IList l = new JObject();
867
 1
      Assert.IsFalse(l.IsFixedSize);
868
 1
    }
869

  
870
    [Test]
871
    public void IListSetItem()
872
    {
873
 1
      JProperty p1 = new JProperty("Test1", 1);
874
 1
      JProperty p2 = new JProperty("Test2", "Two");
875
 1
      IList l = new JObject(p1, p2);
876

  
877
 1
      JProperty p3 = new JProperty("Test3", "III");
878

  
879
 1
      l[0] = p3;
880

  
881
 1
      Assert.AreEqual(p3, l[0]);
882
 1
      Assert.AreEqual(p2, l[1]);
883
 1
    }
884

  
885
    [Test]
886
    [ExpectedException(typeof(ArgumentException), ExpectedMessage = "Can not add property Test3 to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object.")]
887
    public void IListSetItemAlreadyExists()
888
    {
889
 1
      JProperty p1 = new JProperty("Test1", 1);
890
 1
      JProperty p2 = new JProperty("Test2", "Two");
891
 1
      IList l = new JObject(p1, p2);
892

  
893
 1
      JProperty p3 = new JProperty("Test3", "III");
894

  
895
 1
      l[0] = p3;
896
 1
      l[1] = p3;
897
 0
    }
898

  
899
    [Test]
900
    [ExpectedException(typeof(ArgumentException), ExpectedMessage = @"Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject.")]
901
    public void IListSetItemInvalid()
902
    {
903
 1
      JProperty p1 = new JProperty("Test1", 1);
904
 1
      JProperty p2 = new JProperty("Test2", "Two");
905
 1
      IList l = new JObject(p1, p2);
906

  
907
 1
      l[0] = new JValue(true);
908
 0
    }
909

  
910
    [Test]
911
    public void IListSyncRoot()
912
    {
913
 1
      JProperty p1 = new JProperty("Test1", 1);
914
 1
      JProperty p2 = new JProperty("Test2", "Two");
915
 1
      IList l = new JObject(p1, p2);
916

  
917
 1
      Assert.IsNotNull(l.SyncRoot);
918
 1
    }
919

  
920
    [Test]
921
    public void IListIsSynchronized()
922
    {
923
 1
      JProperty p1 = new JProperty("Test1", 1);
924
 1
      JProperty p2 = new JProperty("Test2", "Two");
925
 1
      IList l = new JObject(p1, p2);
926

  
927
 1
      Assert.IsFalse(l.IsSynchronized);
928
 1
    }
929

  
930
    [Test]
931
    public void GenericListJTokenContains()
932
    {
933
 1
      JProperty p = new JProperty("Test", 1);
934
 1
      IList<JToken> l = new JObject(p);
935

  
936
 1
      Assert.IsTrue(l.Contains(p));
937
 1
      Assert.IsFalse(l.Contains(new JProperty("Test", 1)));
938
 1
    }
939

  
940
    [Test]
941
    public void GenericListJTokenIndexOf()
942
    {
943
 1
      JProperty p = new JProperty("Test", 1);
944
 1
      IList<JToken> l = new JObject(p);
945

  
946
 1
      Assert.AreEqual(0, l.IndexOf(p));
947
 1
      Assert.AreEqual(-1, l.IndexOf(new JProperty("Test", 1)));
948
 1
    }
949

  
950
    [Test]
951
    public void GenericListJTokenClear()
952
    {
953
 1
      JProperty p = new JProperty("Test", 1);
954
 1
      IList<JToken> l = new JObject(p);
955

  
956
 1
      Assert.AreEqual(1, l.Count);
957

  
958
 1
      l.Clear();
959

  
960
 1
      Assert.AreEqual(0, l.Count);
961
 1
    }
962

  
963
    [Test]
964
    public void GenericListJTokenCopyTo()
965
    {
966
 1
      JProperty p1 = new JProperty("Test1", 1);
967
 1
      JProperty p2 = new JProperty("Test2", "Two");
968
 1
      IList<JToken> l = new JObject(p1, p2);
969

  
970
 1
      JToken[] a = new JToken[l.Count];
971

  
972
 1
      l.CopyTo(a, 0);
973

  
974
 1
      Assert.AreEqual(p1, a[0]);
975
 1
      Assert.AreEqual(p2, a[1]);
976
 1
    }
977

  
978
    [Test]
979
    public void GenericListJTokenAdd()
980
    {
981
 1
      JProperty p1 = new JProperty("Test1", 1);
982
 1
      JProperty p2 = new JProperty("Test2", "Two");
983
 1
      IList<JToken> l = new JObject(p1, p2);
984

  
985
 1
      JProperty p3 = new JProperty("Test3", "III");
986

  
987
 1
      l.Add(p3);
988

  
989
 1
      Assert.AreEqual(3, l.Count);
990
 1
      Assert.AreEqual(p3, l[2]);
991
 1
    }
992

  
993
    [Test]
994
    [ExpectedException(typeof(ArgumentException), ExpectedMessage = "Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject.")]
995
    public void GenericListJTokenAddBadToken()
996
    {
997
 1
      JProperty p1 = new JProperty("Test1", 1);
998
 1
      JProperty p2 = new JProperty("Test2", "Two");
999
 1
      IList<JToken> l = new JObject(p1, p2);
1000

  
1001
 1
      l.Add(new JValue("Bad!"));
1002
 0
    }
1003

  
1004
    [Test]
1005
    [ExpectedException(typeof(ArgumentException), ExpectedMessage = "Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject.")]
1006
    public void GenericListJTokenAddBadValue()
1007
    {
1008
 1
      JProperty p1 = new JProperty("Test1", 1);
1009
 1
      JProperty p2 = new JProperty("Test2", "Two");
1010
 1
      IList<JToken> l = new JObject(p1, p2);
1011

  
1012
      // string is implicitly converted to JValue
1013
 1
      l.Add("Bad!");
1014
 0
    }
1015

  
1016
    [Test]
1017
    [ExpectedException(typeof(ArgumentException), ExpectedMessage = "Can not add property Test2 to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object.")]
1018
    public void GenericListJTokenAddPropertyWithExistingName()
1019
    {
1020
 1
      JProperty p1 = new JProperty("Test1", 1);
1021
 1
      JProperty p2 = new JProperty("Test2", "Two");
1022
 1
      IList<JToken> l = new JObject(p1, p2);
1023

  
1024
 1
      JProperty p3 = new JProperty("Test2", "II");
1025

  
1026
 1
      l.Add(p3);
1027
 0
    }
1028

  
1029
    [Test]
1030
    public void GenericListJTokenRemove()
1031
    {
1032
 1
      JProperty p1 = new JProperty("Test1", 1);
1033
 1
      JProperty p2 = new JProperty("Test2", "Two");
1034
 1
      IList<JToken> l = new JObject(p1, p2);
1035

  
1036
 1
      JProperty p3 = new JProperty("Test3", "III");
1037

  
1038
      // won't do anything
1039
 1
      Assert.IsFalse(l.Remove(p3));
1040
 1
      Assert.AreEqual(2, l.Count);
1041

  
1042
 1
      Assert.IsTrue(l.Remove(p1));
1043
 1
      Assert.AreEqual(1, l.Count);
1044
 1
      Assert.IsFalse(l.Contains(p1));
1045
 1
      Assert.IsTrue(l.Contains(p2));
1046

  
1047
 1
      Assert.IsTrue(l.Remove(p2));
1048
 1
      Assert.AreEqual(0, l.Count);
1049
 1
      Assert.IsFalse(l.Contains(p2));
1050
 1
      Assert.AreEqual(null, p2.Parent);
1051
 1
    }
1052

  
1053
    [Test]
1054
    public void GenericListJTokenRemoveAt()
1055
    {
1056
 1
      JProperty p1 = new JProperty("Test1", 1);
1057
 1
      JProperty p2 = new JProperty("Test2", "Two");
1058
 1
      IList<JToken> l = new JObject(p1, p2);
1059

  
1060
      // won't do anything
1061
 1
      l.RemoveAt(0);
1062

  
1063
 1
      l.Remove(p1);
1064
 1
      Assert.AreEqual(1, l.Count);
1065

  
1066
 1
      l.Remove(p2);
1067
 1
      Assert.AreEqual(0, l.Count);
1068
 1
    }
1069

  
1070
    [Test]
1071
    public void GenericListJTokenInsert()
1072
    {
1073
 1
      JProperty p1 = new JProperty("Test1", 1);
1074
 1
      JProperty p2 = new JProperty("Test2", "Two");
1075
 1
      IList<JToken> l = new JObject(p1, p2);
1076

  
1077
 1
      JProperty p3 = new JProperty("Test3", "III");
1078

  
1079
 1
      l.Insert(1, p3);
1080
 1
      Assert.AreEqual(l, p3.Parent);
1081

  
1082
 1
      Assert.AreEqual(p1, l[0]);
1083
 1
      Assert.AreEqual(p3, l[1]);
1084
 1
      Assert.AreEqual(p2, l[2]);
1085
 1
    }
1086

  
1087
    [Test]
1088
    public void GenericListJTokenIsReadOnly()
1089
    {
1090
 1
      IList<JToken> l = new JObject();
1091
 1
      Assert.IsFalse(l.IsReadOnly);
1092
 1
    }
1093

  
1094
    [Test]
1095
    public void GenericListJTokenSetItem()
1096
    {
1097
 1
      JProperty p1 = new JProperty("Test1", 1);
1098
 1
      JProperty p2 = new JProperty("Test2", "Two");
1099
 1
      IList<JToken> l = new JObject(p1, p2);
1100

  
1101
 1
      JProperty p3 = new JProperty("Test3", "III");
1102

  
1103
 1
      l[0] = p3;
1104

  
1105
 1
      Assert.AreEqual(p3, l[0]);
1106
 1
      Assert.AreEqual(p2, l[1]);
1107
 1
    }
1108

  
1109
    [Test]
1110
    [ExpectedException(typeof(ArgumentException), ExpectedMessage = "Can not add property Test3 to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object.")]
1111
    public void GenericListJTokenSetItemAlreadyExists()
1112
    {
1113
 1
      JProperty p1 = new JProperty("Test1", 1);
1114
 1
      JProperty p2 = new JProperty("Test2", "Two");
1115
 1
      IList<JToken> l = new JObject(p1, p2);
1116

  
1117
 1
      JProperty p3 = new JProperty("Test3", "III");
1118

  
1119
 1
      l[0] = p3;
1120
 1
      l[1] = p3;
1121
 0
    }
1122

  
1123
#if !SILVERLIGHT
1124
    [Test]
1125
    public void IBindingListSortDirection()
1126
    {
1127
 1
      IBindingList l = new JObject();
1128
 1
      Assert.AreEqual(ListSortDirection.Ascending, l.SortDirection);
1129
 1
    }
1130

  
1131
    [Test]
1132
    public void IBindingListSortProperty()
1133
    {
1134
 1
      IBindingList l = new JObject();
1135
 1
      Assert.AreEqual(null, l.SortProperty);
1136
 1
    }
1137

  
1138
    [Test]
1139
    public void IBindingListSupportsChangeNotification()
1140
    {
1141
 1
      IBindingList l = new JObject();
1142
 1
      Assert.AreEqual(true, l.SupportsChangeNotification);
1143
 1
    }
1144

  
1145
    [Test]
1146
    public void IBindingListSupportsSearching()
1147
    {
1148
 1
      IBindingList l = new JObject();
1149
 1
      Assert.AreEqual(false, l.SupportsSearching);
1150
 1
    }
1151

  
1152
    [Test]
1153
    public void IBindingListSupportsSorting()
1154
    {
1155
 1
      IBindingList l = new JObject();
1156
 1
      Assert.AreEqual(false, l.SupportsSorting);
1157
 1
    }
1158

  
1159
    [Test]
1160
    public void IBindingListAllowEdit()
1161
    {
1162
 1
      IBindingList l = new JObject();
1163
 1
      Assert.AreEqual(true, l.AllowEdit);
1164
 1
    }
1165

  
1166
    [Test]
1167
    public void IBindingListAllowNew()
1168
    {
1169
 1
      IBindingList l = new JObject();
1170
 1
      Assert.AreEqual(true, l.AllowNew);
1171
 1
    }
1172

  
1173
    [Test]
1174
    public void IBindingListAllowRemove()
1175
    {
1176
 1
      IBindingList l = new JObject();
1177
 1
      Assert.AreEqual(true, l.AllowRemove);
1178
 1
    }
1179

  
1180
    [Test]
1181
    public void IBindingListAddIndex()
1182
    {
1183
 1
      IBindingList l = new JObject();
1184
      // do nothing
1185
 1
      l.AddIndex(null);
1186
 1
    }
1187

  
1188
    [Test]
1189
    [ExpectedException(typeof(NotSupportedException))]
1190
    public void IBindingListApplySort()
1191
    {
1192
 1
      IBindingList l = new JObject();
1193
 1
      l.ApplySort(null, ListSortDirection.Ascending);
1194
 0
    }
1195

  
1196
    [Test]
1197
    [ExpectedException(typeof(NotSupportedException))]
1198
    public void IBindingListRemoveSort()
1199
    {
1200
 1
      IBindingList l = new JObject();
1201
 1
      l.RemoveSort();
1202
 0
    }
1203

  
1204
    [Test]
1205
    public void IBindingListRemoveIndex()
1206
    {
1207
 1
      IBindingList l = new JObject();
1208
      // do nothing
1209
 1
      l.RemoveIndex(null);
1210
 1
    }
1211

  
1212
    [Test]
1213
    [ExpectedException(typeof(NotSupportedException))]
1214
    public void IBindingListFind()
1215
    {
1216
 1
      IBindingList l = new JObject();
1217
 1
      l.Find(null, null);
1218
 0
    }
1219

  
1220
    [Test]
1221
    public void IBindingListIsSorted()
1222
    {
1223
 1
      IBindingList l = new JObject();
1224
 1
      Assert.AreEqual(false, l.IsSorted);
1225
 1
    }
1226

  
1227
    [Test]
1228
    [ExpectedException(typeof(Exception), ExpectedMessage = "Could not determine new value to add to 'Newtonsoft.Json.Linq.JObject'.")]
1229
    public void IBindingListAddNew()
1230
    {
1231
 1
      IBindingList l = new JObject();
1232
 1
      l.AddNew();
1233
 0
    }
1234

  
1235
    [Test]
1236
    public void IBindingListAddNewWithEvent()
1237
    {
1238
 1
      JObject o = new JObject();
1239
 1
      o.AddingNew += (s, e) => e.NewObject = new JProperty("Property!");
1240

  
1241
 1
      IBindingList l = o;
1242
 1
      object newObject = l.AddNew();
1243
 1
      Assert.IsNotNull(newObject);
1244

  
1245
 1
      JProperty p = (JProperty) newObject;
1246
 1
      Assert.AreEqual("Property!", p.Name);
1247
 1
      Assert.AreEqual(o, p.Parent);
1248
 1
    }
1249

  
1250
    [Test]
1251
    public void ITypedListGetListName()
1252
    {
1253
 1
      JProperty p1 = new JProperty("Test1", 1);
1254
 1
      JProperty p2 = new JProperty("Test2", "Two");
1255
 1
      ITypedList l = new JObject(p1, p2);
1256

  
1257
 1
      Assert.AreEqual(string.Empty, l.GetListName(null));
1258
 1
    }
1259

  
1260
    [Test]
1261
    public void ITypedListGetItemProperties()
1262
    {
1263
 1
      JProperty p1 = new JProperty("Test1", 1);
1264
 1
      JProperty p2 = new JProperty("Test2", "Two");
1265
 1
      ITypedList l = new JObject(p1, p2);
1266

  
1267
 1
      PropertyDescriptorCollection propertyDescriptors = l.GetItemProperties(null);
1268
 1
      Assert.IsNull(propertyDescriptors);
1269
 1
    }
1270

  
1271
    [Test]
1272
    public void ListChanged()
1273
    {
1274
 1
      JProperty p1 = new JProperty("Test1", 1);
1275
 1
      JProperty p2 = new JProperty("Test2", "Two");
1276
 1
      JObject o = new JObject(p1, p2);
1277

  
1278
 1
      ListChangedType? changedType = null;
1279
 1
      int? index = null;
1280
      
1281
 1
      o.ListChanged += (s, a) =>
1282
 1
        {
1283
 1
          changedType = a.ListChangedType;
1284
 1
          index = a.NewIndex;
1285
 1
        };
1286

  
1287
 1
      JProperty p3 = new JProperty("Test3", "III");
1288

  
1289
 1
      o.Add(p3);
1290
 1
      Assert.AreEqual(changedType, ListChangedType.ItemAdded);
1291
 1
      Assert.AreEqual(index, 2);
1292
 1
      Assert.AreEqual(p3, ((IList<JToken>)o)[index.Value]);
1293

  
1294
 1
      JProperty p4 = new JProperty("Test4", "IV");
1295

  
1296
 1
      ((IList<JToken>) o)[index.Value] = p4;
1297
 1
      Assert.AreEqual(changedType, ListChangedType.ItemChanged);
1298
 1
      Assert.AreEqual(index, 2);
1299
 1
      Assert.AreEqual(p4, ((IList<JToken>)o)[index.Value]);
1300
 1
      Assert.IsFalse(((IList<JToken>)o).Contains(p3));
1301
 1
      Assert.IsTrue(((IList<JToken>)o).Contains(p4));
1302

  
1303
 1
      o["Test1"] = 2;
1304
 1
      Assert.AreEqual(changedType, ListChangedType.ItemChanged);
1305
 1
      Assert.AreEqual(index, 0);
1306
 1
      Assert.AreEqual(2, (int)o["Test1"]);
1307
 1
    }
1308
#else
1309
    [Test]
1310
    public void ListChanged()
1311
    {
1312
      JProperty p1 = new JProperty("Test1", 1);
1313
      JProperty p2 = new JProperty("Test2", "Two");
1314
      JObject o = new JObject(p1, p2);
1315

  
1316
      NotifyCollectionChangedAction? changedType = null;
1317
      int? index = null;
1318

  
1319
      o.CollectionChanged += (s, a) =>
1320
      {
1321
        changedType = a.Action;
1322
        index = a.NewStartingIndex;
1323
      };
1324

  
1325
      JProperty p3 = new JProperty("Test3", "III");
1326

  
1327
      o.Add(p3);
1328
      Assert.AreEqual(changedType, NotifyCollectionChangedAction.Add);
1329
      Assert.AreEqual(index, 2);
1330
      Assert.AreEqual(p3, ((IList<JToken>)o)[index.Value]);
1331

  
1332
      JProperty p4 = new JProperty("Test4", "IV");
1333

  
1334
      ((IList<JToken>)o)[index.Value] = p4;
1335
      Assert.AreEqual(changedType, NotifyCollectionChangedAction.Replace);
1336
      Assert.AreEqual(index, 2);
1337
      Assert.AreEqual(p4, ((IList<JToken>)o)[index.Value]);
1338
      Assert.IsFalse(((IList<JToken>)o).Contains(p3));
1339
      Assert.IsTrue(((IList<JToken>)o).Contains(p4));
1340

  
1341
      o["Test1"] = 2;
1342
      Assert.AreEqual(changedType, NotifyCollectionChangedAction.Replace);
1343
      Assert.AreEqual(index, 0);
1344
      Assert.AreEqual(2, (int)o["Test1"]);
1345
    }
1346
#endif
1347

  
1348
    [Test]
1349
    public void GetGeocodeAddress()
1350
    {
1351
 1
      string json = @"{
1352
 1
  ""name"": ""Address: 435 North Mulford Road Rockford, IL 61107"",
1353
 1
  ""Status"": {
1354
 1
    ""code"": 200,
1355
 1
    ""request"": ""geocode""
1356
 1
  },
1357
 1
  ""Placemark"": [ {
1358
 1
    ""id"": ""p1"",
1359
 1
    ""address"": ""435 N Mulford Rd, Rockford, IL 61107, USA"",
1360
 1
    ""AddressDetails"": {
1361
 1
   ""Accuracy"" : 8,
1362
 1
   ""Country"" : {
1363
 1
      ""AdministrativeArea"" : {
1364
 1
         ""AdministrativeAreaName"" : ""IL"",
1365
 1
         ""SubAdministrativeArea"" : {
1366
 1
            ""Locality"" : {
1367
 1
               ""LocalityName"" : ""Rockford"",
1368
 1
               ""PostalCode"" : {
1369
 1
                  ""PostalCodeNumber"" : ""61107""
1370
 1
               },
1371
 1
               ""Thoroughfare"" : {
1372
 1
                  ""ThoroughfareName"" : ""435 N Mulford Rd""
1373
 1
               }
1374
 1
            },
1375
 1
            ""SubAdministrativeAreaName"" : ""Winnebago""
1376
 1
         }
1377
 1
      },
1378
 1
      ""CountryName"" : ""USA"",
1379
 1
      ""CountryNameCode"" : ""US""
1380
 1
   }
1381
 1
},
1382
 1
    ""ExtendedData"": {
1383
 1
      ""LatLonBox"": {
1384
 1
        ""north"": 42.2753076,
1385
 1
        ""south"": 42.2690124,
1386
 1
        ""east"": -88.9964645,
1387
 1
        ""west"": -89.0027597
1388
 1
      }
1389
 1
    },
1390
 1
    ""Point"": {
1391
 1
      ""coordinates"": [ -88.9995886, 42.2721596, 0 ]
1392
 1
    }
1393
 1
  } ]
1394
 1
}";
1395

  
1396
 1
      JObject o = JObject.Parse(json);
1397

  
1398
 1
      string searchAddress = (string)o["Placemark"][0]["AddressDetails"]["Country"]["AdministrativeArea"]["SubAdministrativeArea"]["Locality"]["Thoroughfare"]["ThoroughfareName"];
1399
 1
      Assert.AreEqual("435 N Mulford Rd", searchAddress);
1400
 1
    }
1401

  
1402
    [Test]
1403
    [ExpectedException(typeof(ArgumentException), ExpectedMessage = "Set JObject values with invalid key value: 0. Object property name expected.")]
1404
    public void SetValueWithInvalidPropertyName()
1405
    {
1406
 1
      JObject o = new JObject();
1407
 1
      o[0] = new JValue(3);
1408
 0
    }
1409

  
1410
    [Test]
1411
    public void SetValue()
1412
    {
1413
 1
      object key = "TestKey";
1414

  
1415
 1
      JObject o = new JObject();
1416
 1
      o[key] = new JValue(3);
1417

  
1418
 1
      Assert.AreEqual(3, (int)o[key]);
1419
 1
    }
1420

  
1421
    [Test]
1422
    public void ParseMultipleProperties()
1423
    {
1424
 1
      string json = @"{
1425
 1
        ""Name"": ""Name1"",
1426
 1
        ""Name"": ""Name2""
1427
 1
      }";
1428

  
1429
 1
      JObject o = JObject.Parse(json);
1430
 1
      string value = (string)o["Name"];
1431

  
1432
 1
      Assert.AreEqual("Name2", value);
1433
 1
    }
1434

  
1435
    [Test]
1436
    public void WriteObjectNullDBNullValue()
1437
    {
1438
 1
      DBNull dbNull = DBNull.Value;
1439
 1
      JValue v = new JValue(dbNull);
1440
 1
      Assert.AreEqual(DBNull.Value, v.Value);
1441
 1
      Assert.AreEqual(JTokenType.Null, v.Type);
1442

  
1443
 1
      JObject o = new JObject();
1444
 1
      o["title"] = v;
1445

  
1446
 1
      string output = o.ToString();
1447
      
1448
 1
      Assert.AreEqual(@"{
1449
 1
  ""title"": null
1450
 1
}", output);
1451
 1
    }
1452

  
1453
    [Test]
1454
    [ExpectedException(typeof(ArgumentException), ExpectedMessage = "Can not convert Object to String.")]
1455
    public void InvalidValueCastExceptionMessage()
1456
    {
1457
 1
      string json = @"{
1458
 1
  ""responseData"": {}, 
1459
 1
  ""responseDetails"": null, 
1460
 1
  ""responseStatus"": 200
1461
 1
}";
1462

  
1463
 1
      JObject o = JObject.Parse(json);
1464

  
1465
 1
      string name = (string)o["responseData"];
1466
 0
    }
1467

  
1468
    [Test]
1469
    [ExpectedException(typeof(ArgumentException), ExpectedMessage = "Can not convert Object to String.")]
1470
    public void InvalidPropertyValueCastExceptionMessage()
1471
    {
1472
 1
      string json = @"{
1473
 1
  ""responseData"": {}, 
1474
 1
  ""responseDetails"": null, 
1475
 1
  ""responseStatus"": 200
1476
 1
}";
1477

  
1478
 1
      JObject o = JObject.Parse(json);
1479

  
1480
 1
      string name = (string)o.Property("responseData");
1481
 0
    }
1482
  }
1483
}