Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json.Tests\Linq\JTokenTests.cs

Symbol Coverage: 99.34% (302 of 304)

Branch Coverage: 82.35% (28 of 34)

Cyclomatic Complexity Avg: 1.15 Max:5

Code Lines: 420


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.Linq;
29
using System.Text;
30
using Newtonsoft.Json.Converters;
31
using NUnit.Framework;
32
using Newtonsoft.Json.Linq;
33
using System.IO;
34

  
35
namespace Newtonsoft.Json.Tests.Linq
36
{
37
  public class JTokenTests : TestFixtureBase
38
  {
39
    [Test]
40
    public void ReadFrom()
41
    {
42
 1
      JObject o = (JObject)JToken.ReadFrom(new JsonTextReader(new StringReader("{'pie':true}")));
43
 1
      Assert.AreEqual(true, (bool)o["pie"]);
44

  
45
 1
      JArray a = (JArray)JToken.ReadFrom(new JsonTextReader(new StringReader("[1,2,3]")));
46
 1
      Assert.AreEqual(1, (int)a[0]);
47
 1
      Assert.AreEqual(2, (int)a[1]);
48
 1
      Assert.AreEqual(3, (int)a[2]);
49

  
50
 1
      JsonReader reader = new JsonTextReader(new StringReader("{'pie':true}"));
51
 1
      reader.Read();
52
 1
      reader.Read();
53

  
54
 1
      JProperty p = (JProperty)JToken.ReadFrom(reader);
55
 1
      Assert.AreEqual("pie", p.Name);
56
 1
      Assert.AreEqual(true, (bool)p.Value);
57

  
58
 1
      JConstructor c = (JConstructor)JToken.ReadFrom(new JsonTextReader(new StringReader("new Date(1)")));
59
 1
      Assert.AreEqual("Date", c.Name);
60
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue(1), c.Values().ElementAt(0)));
61

  
62
      JValue v;
63
      
64
 1
      v = (JValue)JToken.ReadFrom(new JsonTextReader(new StringReader(@"""stringvalue""")));
65
 1
      Assert.AreEqual("stringvalue", (string)v);
66

  
67
 1
      v = (JValue)JToken.ReadFrom(new JsonTextReader(new StringReader(@"1")));
68
 1
      Assert.AreEqual(1, (int)v);
69

  
70
 1
      v = (JValue)JToken.ReadFrom(new JsonTextReader(new StringReader(@"1.1")));
71
 1
      Assert.AreEqual(1.1, (double)v);
72
 1
    }
73

  
74
    [Test]
75
    public void Parent()
76
    {
77
 1
      JArray v = new JArray(new JConstructor("TestConstructor"), new JValue(new DateTime(2000, 12, 20)));
78

  
79
 1
      Assert.AreEqual(null, v.Parent);
80

  
81
 1
      JObject o =
82
 1
        new JObject(
83
 1
          new JProperty("Test1", v),
84
 1
          new JProperty("Test2", "Test2Value"),
85
 1
          new JProperty("Test3", "Test3Value"),
86
 1
          new JProperty("Test4", null)
87
 1
        );
88

  
89
 1
      Assert.AreEqual(o.Property("Test1"), v.Parent);
90

  
91
 1
      JProperty p = new JProperty("NewProperty", v);
92

  
93
      // existing value should still have same parent
94
 1
      Assert.AreEqual(o.Property("Test1"), v.Parent);
95

  
96
      // new value should be cloned
97
 1
      Assert.AreNotSame(p.Value, v);
98

  
99
 1
      Assert.AreEqual((DateTime)((JValue)p.Value[1]).Value, (DateTime)((JValue)v[1]).Value);
100

  
101
 1
      Assert.AreEqual(v, o["Test1"]);
102

  
103
 1
      Assert.AreEqual(null, o.Parent);
104
 1
      JProperty o1 = new JProperty("O1", o);
105
 1
      Assert.AreEqual(o, o1.Value);
106

  
107
 1
      Assert.AreNotEqual(null, o.Parent);
108
 1
      JProperty o2 = new JProperty("O2", o);
109

  
110
 1
      Assert.AreNotSame(o1.Value, o2.Value);
111
 1
      Assert.AreEqual(o1.Value.Children().Count(), o2.Value.Children().Count());
112
 1
      Assert.AreEqual(false, JToken.DeepEquals(o1, o2));
113
 1
      Assert.AreEqual(true, JToken.DeepEquals(o1.Value, o2.Value));
114
 1
    }
115

  
116
    [Test]
117
    public void Next()
118
    {
119
 1
      JArray a =
120
 1
        new JArray(
121
 1
          5,
122
 1
          6,
123
 1
          new JArray(7, 8),
124
 1
          new JArray(9, 10)
125
 1
        );
126

  
127
 1
      JToken next = a[0].Next;
128
 1
      Assert.AreEqual(6, (int)next);
129

  
130
 1
      next = next.Next;
131
 1
      Assert.IsTrue(JToken.DeepEquals(new JArray(7, 8), next));
132
 
133
 1
      next = next.Next;
134
 1
      Assert.IsTrue(JToken.DeepEquals(new JArray(9, 10), next));
135

  
136
 1
      next = next.Next;
137
 1
      Assert.IsNull(next);
138
 1
    }
139

  
140
    [Test]
141
    public void Previous()
142
    {
143
 1
      JArray a =
144
 1
        new JArray(
145
 1
          5,
146
 1
          6,
147
 1
          new JArray(7, 8),
148
 1
          new JArray(9, 10)
149
 1
        );
150

  
151
 1
      JToken previous = a[3].Previous;
152
 1
      Assert.IsTrue(JToken.DeepEquals(new JArray(7, 8), previous));
153

  
154
 1
      previous = previous.Previous;
155
 1
      Assert.AreEqual(6, (int)previous);
156

  
157
 1
      previous = previous.Previous;
158
 1
      Assert.AreEqual(5, (int)previous);
159

  
160
 1
      previous = previous.Previous;
161
 1
      Assert.IsNull(previous);
162
 1
    }
163

  
164
    [Test]
165
    public void Children()
166
    {
167
 1
      JArray a =
168
 1
        new JArray(
169
 1
          5,
170
 1
          new JArray(1),
171
 1
          new JArray(1, 2),
172
 1
          new JArray(1, 2, 3)
173
 1
        );
174

  
175
 1
      Assert.AreEqual(4, a.Count());
176
 1
      Assert.AreEqual(3, a.Children<JArray>().Count());
177
 1
    }
178

  
179
    [Test]
180
    public void BeforeAfter()
181
    {
182
 1
      JArray a =
183
 1
        new JArray(
184
 1
          5,
185
 1
          new JArray(1, 2, 3),
186
 1
          new JArray(1, 2, 3),
187
 1
          new JArray(1, 2, 3)
188
 1
        );
189

  
190
 1
      Assert.AreEqual(5, (int)a[1].Previous);
191
 1
      Assert.AreEqual(2, a[2].BeforeSelf().Count());
192
      //Assert.AreEqual(2, a[2].AfterSelf().Count());
193
 1
    }
194

  
195
    [Test]
196
    public void Casting()
197
    {
198
 1
      Assert.AreEqual(new DateTime(2000, 12, 20), (DateTime)new JValue(new DateTime(2000, 12, 20)));
199
#if !PocketPC && !NET20
200
 1
      Assert.AreEqual(new DateTimeOffset(2000, 12, 20, 23, 50, 10, TimeSpan.Zero), (DateTimeOffset)new JValue(new DateTimeOffset(2000, 12, 20, 23, 50, 10, TimeSpan.Zero)));
201
 1
      Assert.AreEqual(null, (DateTimeOffset?)new JValue((DateTimeOffset?)null));
202
 1
      Assert.AreEqual(null, (DateTimeOffset?)(JValue)null);
203
#endif
204
 1
      Assert.AreEqual(true, (bool)new JValue(true));
205
 1
      Assert.AreEqual(true, (bool?)new JValue(true));
206
 1
      Assert.AreEqual(null, (bool?)((JValue)null));
207
 1
      Assert.AreEqual(null, (bool?)new JValue((object)null));
208
 1
      Assert.AreEqual(10, (long)new JValue(10));
209
 1
      Assert.AreEqual(null, (long?)new JValue((long?)null));
210
 1
      Assert.AreEqual(null, (long?)(JValue)null);
211
 1
      Assert.AreEqual(null, (int?)new JValue((int?)null));
212
 1
      Assert.AreEqual(null, (int?)(JValue)null);
213
 1
      Assert.AreEqual(null, (DateTime?)new JValue((DateTime?)null));
214
 1
      Assert.AreEqual(null, (DateTime?)(JValue)null);
215
 1
      Assert.AreEqual(null, (short?)new JValue((short?)null));
216
 1
      Assert.AreEqual(null, (short?)(JValue)null);
217
 1
      Assert.AreEqual(null, (float?)new JValue((float?)null));
218
 1
      Assert.AreEqual(null, (float?)(JValue)null);
219
 1
      Assert.AreEqual(null, (double?)new JValue((double?)null));
220
 1
      Assert.AreEqual(null, (double?)(JValue)null);
221
 1
      Assert.AreEqual(null, (decimal?)new JValue((decimal?)null));
222
 1
      Assert.AreEqual(null, (decimal?)(JValue)null);
223
 1
      Assert.AreEqual(null, (uint?)new JValue((uint?)null));
224
 1
      Assert.AreEqual(null, (uint?)(JValue)null);
225
 1
      Assert.AreEqual(null, (sbyte?)new JValue((sbyte?)null));
226
 1
      Assert.AreEqual(null, (sbyte?)(JValue)null);
227
 1
      Assert.AreEqual(null, (ulong?)new JValue((ulong?)null));
228
 1
      Assert.AreEqual(null, (ulong?)(JValue)null);
229
 1
      Assert.AreEqual(null, (uint?)new JValue((uint?)null));
230
 1
      Assert.AreEqual(null, (uint?)(JValue)null);
231
 1
      Assert.AreEqual(11.1f, (float)new JValue(11.1));
232
 1
      Assert.AreEqual(float.MinValue, (float)new JValue(float.MinValue));
233
 1
      Assert.AreEqual(1.1, (double)new JValue(1.1));
234
 1
      Assert.AreEqual(uint.MaxValue, (uint)new JValue(uint.MaxValue));
235
 1
      Assert.AreEqual(ulong.MaxValue, (ulong)new JValue(ulong.MaxValue));
236
 1
      Assert.AreEqual(ulong.MaxValue, (ulong)new JProperty("Test", new JValue(ulong.MaxValue)));
237
 1
      Assert.AreEqual(null, (string)new JValue((string)null));
238
 1
      Assert.AreEqual(5m, (decimal)(new JValue(5L)));
239
 1
      Assert.AreEqual(5m, (decimal?)(new JValue(5L)));
240
 1
      Assert.AreEqual(5f, (float)(new JValue(5L)));
241
 1
      Assert.AreEqual(5f, (float)(new JValue(5m)));
242
 1
      Assert.AreEqual(5f, (float?)(new JValue(5m)));
243

  
244
 1
      byte[] data = new byte[0];
245
 1
      Assert.AreEqual(data, (byte[])(new JValue(data)));
246

  
247
 1
      Assert.AreEqual(5, (int)(new JValue(StringComparison.OrdinalIgnoreCase)));
248
 1
    }
249

  
250
    [Test]
251
    public void ImplicitCastingTo()
252
    {
253
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue(new DateTime(2000, 12, 20)), (JValue)new DateTime(2000, 12, 20)));
254
#if !PocketPC && !NET20
255
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue(new DateTimeOffset(2000, 12, 20, 23, 50, 10, TimeSpan.Zero)), (JValue)new DateTimeOffset(2000, 12, 20, 23, 50, 10, TimeSpan.Zero)));
256
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue((DateTimeOffset?)null), (JValue)(DateTimeOffset?)null));
257
#endif
258

  
259
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue(true), (JValue)true));
260
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue(true), (JValue)(bool?)true));
261
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue((bool?)null), (JValue)(bool?)null));
262
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue(10), (JValue)10));
263
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue((long?)null), (JValue)(long?)null));
264
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue((DateTime?)null), (JValue)(DateTime?)null));
265
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue(long.MaxValue), (JValue)long.MaxValue));
266
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue((int?)null), (JValue)(int?)null));
267
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue((short?)null), (JValue)(short?)null));
268
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue((double?)null), (JValue)(double?)null));
269
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue((uint?)null), (JValue)(uint?)null));
270
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue((decimal?)null), (JValue)(decimal?)null));
271
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue((ulong?)null), (JValue)(ulong?)null));
272
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue((sbyte?)null), (JValue)(sbyte?)null));
273
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue((ushort?)null), (JValue)(ushort?)null));
274
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue(ushort.MaxValue), (JValue)ushort.MaxValue));
275
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue(11.1f), (JValue)11.1f));
276
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue(float.MinValue), (JValue)float.MinValue));
277
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue(double.MinValue), (JValue)double.MinValue));
278
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue(uint.MaxValue), (JValue)uint.MaxValue));
279
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue(ulong.MaxValue), (JValue)ulong.MaxValue));
280
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue(ulong.MinValue), (JValue)ulong.MinValue));
281
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue((string)null), (JValue)(string)null));
282
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue((DateTime?)null), (JValue)(DateTime?)null));
283
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue(decimal.MaxValue), (JValue)decimal.MaxValue));
284
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue(decimal.MaxValue), (JValue)(decimal?)decimal.MaxValue));
285
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue(decimal.MinValue), (JValue)decimal.MinValue));
286
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue(float.MaxValue), (JValue)(float?)float.MaxValue));
287
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue(double.MaxValue), (JValue)(double?)double.MaxValue));
288
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue((object)null), (JValue)(double?)null));
289

  
290
 1
      Assert.IsFalse(JToken.DeepEquals(new JValue(true), (JValue)(bool?)null));
291
 1
      Assert.IsFalse(JToken.DeepEquals(new JValue((object)null), (JValue)(object)null));
292

  
293
 1
      byte[] emptyData = new byte[0];
294
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue(emptyData), (JValue)emptyData));
295
 1
      Assert.IsFalse(JToken.DeepEquals(new JValue(emptyData), (JValue)new byte[1]));
296
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue(Encoding.UTF8.GetBytes("Hi")), (JValue)Encoding.UTF8.GetBytes("Hi")));
297
 1
    }
298

  
299
    [Test]
300
    public void Root()
301
    {
302
 1
      JArray a =
303
 1
        new JArray(
304
 1
          5,
305
 1
          6,
306
 1
          new JArray(7, 8),
307
 1
          new JArray(9, 10)
308
 1
        );
309

  
310
 1
      Assert.AreEqual(a, a.Root);
311
 1
      Assert.AreEqual(a, a[0].Root);
312
 1
      Assert.AreEqual(a, ((JArray)a[2])[0].Root);
313
 1
    }
314

  
315
    [Test]
316
    public void Remove()
317
    {
318
      JToken t;
319
 1
      JArray a =
320
 1
        new JArray(
321
 1
          5,
322
 1
          6,
323
 1
          new JArray(7, 8),
324
 1
          new JArray(9, 10)
325
 1
        );
326

  
327
 1
      a[0].Remove();
328

  
329
 1
      Assert.AreEqual(6, (int)a[0]);
330

  
331
 1
      a[1].Remove();
332

  
333
 1
      Assert.AreEqual(6, (int)a[0]);
334
 1
      Assert.IsTrue(JToken.DeepEquals(new JArray(9, 10), a[1]));
335
 1
      Assert.AreEqual(2, a.Count());
336

  
337
 1
      t = a[1];
338
 1
      t.Remove();
339
 1
      Assert.AreEqual(6, (int)a[0]);
340
 1
      Assert.IsNull(t.Next);
341
 1
      Assert.IsNull(t.Previous);
342
 1
      Assert.IsNull(t.Parent);
343

  
344
 1
      t = a[0];
345
 1
      t.Remove();
346
 1
      Assert.AreEqual(0, a.Count());
347

  
348
 1
      Assert.IsNull(t.Next);
349
 1
      Assert.IsNull(t.Previous);
350
 1
      Assert.IsNull(t.Parent);
351
 1
    }
352

  
353
    [Test]
354
    public void AfterSelf()
355
    {
356
 1
      JArray a =
357
 1
        new JArray(
358
 1
          5,
359
 1
          new JArray(1),
360
 1
          new JArray(1, 2),
361
 1
          new JArray(1, 2, 3)
362
 1
        );
363

  
364
 1
      JToken t = a[1];
365
 1
      List<JToken> afterTokens = t.AfterSelf().ToList();
366

  
367
 1
      Assert.AreEqual(2, afterTokens.Count);
368
 1
      Assert.IsTrue(JToken.DeepEquals(new JArray(1, 2), afterTokens[0]));
369
 1
      Assert.IsTrue(JToken.DeepEquals(new JArray(1, 2, 3), afterTokens[1]));
370
 1
    }
371

  
372
    [Test]
373
    public void BeforeSelf()
374
    {
375
 1
      JArray a =
376
 1
        new JArray(
377
 1
          5,
378
 1
          new JArray(1),
379
 1
          new JArray(1, 2),
380
 1
          new JArray(1, 2, 3)
381
 1
        );
382

  
383
 1
      JToken t = a[2];
384
 1
      List<JToken> beforeTokens = t.BeforeSelf().ToList();
385

  
386
 1
      Assert.AreEqual(2, beforeTokens.Count);
387
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue(5), beforeTokens[0]));
388
 1
      Assert.IsTrue(JToken.DeepEquals(new JArray(1), beforeTokens[1]));
389
 1
    }
390

  
391
    [Test]
392
    public void HasValues()
393
    {
394
 1
      JArray a =
395
 1
        new JArray(
396
 1
          5,
397
 1
          new JArray(1),
398
 1
          new JArray(1, 2),
399
 1
          new JArray(1, 2, 3)
400
 1
        );
401

  
402
 1
      Assert.IsTrue(a.HasValues);
403
 1
    }
404

  
405
    [Test]
406
    public void Ancestors()
407
    {
408
 1
      JArray a =
409
 1
        new JArray(
410
 1
          5,
411
 1
          new JArray(1),
412
 1
          new JArray(1, 2),
413
 1
          new JArray(1, 2, 3)
414
 1
        );
415

  
416
 1
      JToken t = a[1][0];
417
 1
      List<JToken> ancestors = t.Ancestors().ToList();
418
 1
      Assert.AreEqual(2, ancestors.Count());
419
 1
      Assert.AreEqual(a[1], ancestors[0]);
420
 1
      Assert.AreEqual(a, ancestors[1]);
421
 1
    }
422

  
423
    [Test]
424
    public void Descendants()
425
    {
426
 1
      JArray a =
427
 1
        new JArray(
428
 1
          5,
429
 1
          new JArray(1),
430
 1
          new JArray(1, 2),
431
 1
          new JArray(1, 2, 3)
432
 1
        );
433

  
434
 1
      List<JToken> descendants = a.Descendants().ToList();
435
 1
      Assert.AreEqual(10, descendants.Count());
436
 1
      Assert.AreEqual(5, (int)descendants[0]);
437
 1
      Assert.IsTrue(JToken.DeepEquals(new JArray(1, 2, 3), descendants[descendants.Count - 4]));
438
 1
      Assert.AreEqual(1, (int)descendants[descendants.Count - 3]);
439
 1
      Assert.AreEqual(2, (int)descendants[descendants.Count - 2]);
440
 1
      Assert.AreEqual(3, (int)descendants[descendants.Count - 1]);
441
 1
    }
442

  
443
    [Test]
444
    public void CreateWriter()
445
    {
446
 1
      JArray a =
447
 1
        new JArray(
448
 1
          5,
449
 1
          new JArray(1),
450
 1
          new JArray(1, 2),
451
 1
          new JArray(1, 2, 3)
452
 1
        );
453

  
454
 1
      JsonWriter writer = a.CreateWriter();
455
 1
      Assert.IsNotNull(writer);
456
 1
      Assert.AreEqual(4, a.Count());
457

  
458
 1
      writer.WriteValue("String");
459
 1
      Assert.AreEqual(5, a.Count());
460
 1
      Assert.AreEqual("String", (string)a[4]);
461

  
462
 1
      writer.WriteStartObject();
463
 1
      writer.WritePropertyName("Property");
464
 1
      writer.WriteValue("PropertyValue");
465
 1
      writer.WriteEnd();
466

  
467
 1
      Assert.AreEqual(6, a.Count());
468
 1
      Assert.IsTrue(JToken.DeepEquals(new JObject(new JProperty("Property", "PropertyValue")), a[5]));
469
 1
    }
470

  
471
    [Test]
472
    public void AddFirst()
473
    {
474
 1
      JArray a =
475
 1
        new JArray(
476
 1
          5,
477
 1
          new JArray(1),
478
 1
          new JArray(1, 2),
479
 1
          new JArray(1, 2, 3)
480
 1
        );
481

  
482
 1
      a.AddFirst("First");
483

  
484
 1
      Assert.AreEqual("First", (string)a[0]);
485
 1
      Assert.AreEqual(a, a[0].Parent);
486
 1
      Assert.AreEqual(a[1], a[0].Next);
487
 1
      Assert.AreEqual(5, a.Count());
488

  
489
 1
      a.AddFirst("NewFirst");
490
 1
      Assert.AreEqual("NewFirst", (string)a[0]);
491
 1
      Assert.AreEqual(a, a[0].Parent);
492
 1
      Assert.AreEqual(a[1], a[0].Next);
493
 1
      Assert.AreEqual(6, a.Count());
494

  
495
 1
      Assert.AreEqual(a[0], a[0].Next.Previous);
496
 1
    }
497

  
498
    [Test]
499
    public void RemoveAll()
500
    {
501
 1
      JArray a =
502
 1
        new JArray(
503
 1
          5,
504
 1
          new JArray(1),
505
 1
          new JArray(1, 2),
506
 1
          new JArray(1, 2, 3)
507
 1
        );
508

  
509
 1
      JToken first = a.First;
510
 1
      Assert.AreEqual(5, (int)first);
511

  
512
 1
      a.RemoveAll();
513
 1
      Assert.AreEqual(0, a.Count());
514

  
515
 1
      Assert.IsNull(first.Parent);
516
 1
      Assert.IsNull(first.Next);
517
 1
    }
518

  
519
    [Test]
520
    [ExpectedException(typeof(ArgumentException), ExpectedMessage = "Can not add Newtonsoft.Json.Linq.JProperty to Newtonsoft.Json.Linq.JArray.")]
521
    public void AddPropertyToArray()
522
    {
523
 1
      JArray a = new JArray();
524
 1
      a.Add(new JProperty("PropertyName"));
525
 0
    }
526

  
527
    [Test]
528
    [ExpectedException(typeof(ArgumentException), ExpectedMessage = "Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject.")]
529
    public void AddValueToObject()
530
    {
531
 1
      JObject o = new JObject();
532
 1
      o.Add(5);
533
 0
    }
534

  
535
    [Test]
536
    public void Replace()
537
    {
538
 1
      JArray a =
539
 1
        new JArray(
540
 1
          5,
541
 1
          new JArray(1),
542
 1
          new JArray(1, 2),
543
 1
          new JArray(1, 2, 3)
544
 1
        );
545

  
546
 1
      a[0].Replace(new JValue(int.MaxValue));
547
 1
      Assert.AreEqual(int.MaxValue, (int)a[0]);
548
 1
      Assert.AreEqual(4, a.Count());
549

  
550
 1
      a[1][0].Replace(new JValue("Test"));
551
 1
      Assert.AreEqual("Test", (string)a[1][0]);
552

  
553
 1
      a[2].Replace(new JValue(int.MaxValue));
554
 1
      Assert.AreEqual(int.MaxValue, (int)a[2]);
555
 1
      Assert.AreEqual(4, a.Count());
556

  
557
 1
      Assert.IsTrue(JToken.DeepEquals(new JArray(int.MaxValue, new JArray("Test"), int.MaxValue, new JArray(1, 2, 3)), a));
558
 1
    }
559

  
560
    [Test]
561
    public void ToStringWithConverters()
562
    {
563
 1
      JArray a =
564
 1
        new JArray(
565
 1
          new JValue(new DateTime(2009, 2, 15, 0, 0, 0, DateTimeKind.Utc))
566
 1
        );
567

  
568
 1
      string json = a.ToString(Formatting.Indented, new IsoDateTimeConverter());
569

  
570
 1
      Assert.AreEqual(@"[
571
 1
  ""2009-02-15T00:00:00Z""
572
 1
]", json);
573

  
574
 1
      json = JsonConvert.SerializeObject(a, new IsoDateTimeConverter());
575

  
576
 1
      Assert.AreEqual(@"[""2009-02-15T00:00:00Z""]", json);
577
 1
    }
578

  
579
    [Test]
580
    public void ToStringWithNoIndenting()
581
    {
582
 1
      JArray a =
583
 1
        new JArray(
584
 1
          new JValue(new DateTime(2009, 2, 15, 0, 0, 0, DateTimeKind.Utc))
585
 1
        );
586

  
587
 1
      string json = a.ToString(Formatting.None, new IsoDateTimeConverter());
588

  
589
 1
      Assert.AreEqual(@"[""2009-02-15T00:00:00Z""]", json);
590
 1
    }
591

  
592
    [Test]
593
    public void AddAfterSelf()
594
    {
595
 1
      JArray a =
596
 1
        new JArray(
597
 1
          5,
598
 1
          new JArray(1),
599
 1
          new JArray(1, 2),
600
 1
          new JArray(1, 2, 3)
601
 1
        );
602

  
603
 1
      a[1].AddAfterSelf("pie");
604

  
605
 1
      Assert.AreEqual(5, (int)a[0]);
606
 1
      Assert.AreEqual(1, a[1].Count());
607
 1
      Assert.AreEqual("pie", (string)a[2]);
608
 1
      Assert.AreEqual(5, a.Count());
609

  
610
 1
      a[4].AddAfterSelf("lastpie");
611

  
612
 1
      Assert.AreEqual("lastpie", (string)a[5]);
613
 1
      Assert.AreEqual("lastpie", (string)a.Last);
614
 1
    }
615

  
616
    [Test]
617
    public void AddBeforeSelf()
618
    {
619
 1
      JArray a =
620
 1
        new JArray(
621
 1
          5,
622
 1
          new JArray(1),
623
 1
          new JArray(1, 2),
624
 1
          new JArray(1, 2, 3)
625
 1
        );
626

  
627
 1
      a[1].AddBeforeSelf("pie");
628

  
629
 1
      Assert.AreEqual(5, (int)a[0]);
630
 1
      Assert.AreEqual("pie", (string)a[1]);
631
 1
      Assert.AreEqual(a, a[1].Parent);
632
 1
      Assert.AreEqual(a[2], a[1].Next);
633
 1
      Assert.AreEqual(5, a.Count());
634

  
635
 1
      a[0].AddBeforeSelf("firstpie");
636

  
637
 1
      Assert.AreEqual("firstpie", (string)a[0]);
638
 1
      Assert.AreEqual(5, (int)a[1]);
639
 1
      Assert.AreEqual("pie", (string)a[2]);
640
 1
      Assert.AreEqual(a, a[0].Parent);
641
 1
      Assert.AreEqual(a[1], a[0].Next);
642
 1
      Assert.AreEqual(6, a.Count());
643

  
644
 1
      a.Last.AddBeforeSelf("secondlastpie");
645

  
646
 1
      Assert.AreEqual("secondlastpie", (string)a[5]);
647
 1
      Assert.AreEqual(7, a.Count());
648
 1
    }
649
  }
650
}