~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.Text;
 
29
using Newtonsoft.Json.Converters;
 
30
#if !NETFX_CORE
 
31
using NUnit.Framework;
 
32
#else
 
33
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
 
34
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
 
35
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
 
36
#endif
 
37
using Newtonsoft.Json.Linq;
 
38
using System.IO;
 
39
#if NET20
 
40
using Newtonsoft.Json.Utilities.LinqBridge;
 
41
#else
 
42
using System.Linq;
 
43
#endif
 
44
 
 
45
namespace Newtonsoft.Json.Tests.Linq
 
46
{
 
47
  [TestFixture]
 
48
  public class JTokenTests : TestFixtureBase
 
49
  {
 
50
    [Test]
 
51
    public void ReadFrom()
 
52
    {
 
53
      JObject o = (JObject)JToken.ReadFrom(new JsonTextReader(new StringReader("{'pie':true}")));
 
54
      Assert.AreEqual(true, (bool)o["pie"]);
 
55
 
 
56
      JArray a = (JArray)JToken.ReadFrom(new JsonTextReader(new StringReader("[1,2,3]")));
 
57
      Assert.AreEqual(1, (int)a[0]);
 
58
      Assert.AreEqual(2, (int)a[1]);
 
59
      Assert.AreEqual(3, (int)a[2]);
 
60
 
 
61
      JsonReader reader = new JsonTextReader(new StringReader("{'pie':true}"));
 
62
      reader.Read();
 
63
      reader.Read();
 
64
 
 
65
      JProperty p = (JProperty)JToken.ReadFrom(reader);
 
66
      Assert.AreEqual("pie", p.Name);
 
67
      Assert.AreEqual(true, (bool)p.Value);
 
68
 
 
69
      JConstructor c = (JConstructor)JToken.ReadFrom(new JsonTextReader(new StringReader("new Date(1)")));
 
70
      Assert.AreEqual("Date", c.Name);
 
71
      Assert.IsTrue(JToken.DeepEquals(new JValue(1), c.Values().ElementAt(0)));
 
72
 
 
73
      JValue v;
 
74
 
 
75
      v = (JValue)JToken.ReadFrom(new JsonTextReader(new StringReader(@"""stringvalue""")));
 
76
      Assert.AreEqual("stringvalue", (string)v);
 
77
 
 
78
      v = (JValue)JToken.ReadFrom(new JsonTextReader(new StringReader(@"1")));
 
79
      Assert.AreEqual(1, (int)v);
 
80
 
 
81
      v = (JValue)JToken.ReadFrom(new JsonTextReader(new StringReader(@"1.1")));
 
82
      Assert.AreEqual(1.1, (double)v);
 
83
 
 
84
#if !NET20
 
85
      v = (JValue)JToken.ReadFrom(new JsonTextReader(new StringReader(@"""1970-01-01T00:00:00+12:31"""))
 
86
        {
 
87
          DateParseHandling = DateParseHandling.DateTimeOffset
 
88
        });
 
89
      Assert.AreEqual(typeof(DateTimeOffset), v.Value.GetType());
 
90
      Assert.AreEqual(new DateTimeOffset(JsonConvert.InitialJavaScriptDateTicks, new TimeSpan(12, 31, 0)), v.Value);
 
91
#endif
 
92
    }
 
93
 
 
94
    [Test]
 
95
    public void Load()
 
96
    {
 
97
      JObject o = (JObject)JToken.Load(new JsonTextReader(new StringReader("{'pie':true}")));
 
98
      Assert.AreEqual(true, (bool)o["pie"]);
 
99
    }
 
100
 
 
101
    [Test]
 
102
    public void Parse()
 
103
    {
 
104
      JObject o = (JObject)JToken.Parse("{'pie':true}");
 
105
      Assert.AreEqual(true, (bool)o["pie"]);
 
106
    }
 
107
 
 
108
    [Test]
 
109
    public void Parent()
 
110
    {
 
111
      JArray v = new JArray(new JConstructor("TestConstructor"), new JValue(new DateTime(2000, 12, 20)));
 
112
 
 
113
      Assert.AreEqual(null, v.Parent);
 
114
 
 
115
      JObject o =
 
116
        new JObject(
 
117
          new JProperty("Test1", v),
 
118
          new JProperty("Test2", "Test2Value"),
 
119
          new JProperty("Test3", "Test3Value"),
 
120
          new JProperty("Test4", null)
 
121
        );
 
122
 
 
123
      Assert.AreEqual(o.Property("Test1"), v.Parent);
 
124
 
 
125
      JProperty p = new JProperty("NewProperty", v);
 
126
 
 
127
      // existing value should still have same parent
 
128
      Assert.AreEqual(o.Property("Test1"), v.Parent);
 
129
 
 
130
      // new value should be cloned
 
131
      Assert.AreNotSame(p.Value, v);
 
132
 
 
133
      Assert.AreEqual((DateTime)((JValue)p.Value[1]).Value, (DateTime)((JValue)v[1]).Value);
 
134
 
 
135
      Assert.AreEqual(v, o["Test1"]);
 
136
 
 
137
      Assert.AreEqual(null, o.Parent);
 
138
      JProperty o1 = new JProperty("O1", o);
 
139
      Assert.AreEqual(o, o1.Value);
 
140
 
 
141
      Assert.AreNotEqual(null, o.Parent);
 
142
      JProperty o2 = new JProperty("O2", o);
 
143
 
 
144
      Assert.AreNotSame(o1.Value, o2.Value);
 
145
      Assert.AreEqual(o1.Value.Children().Count(), o2.Value.Children().Count());
 
146
      Assert.AreEqual(false, JToken.DeepEquals(o1, o2));
 
147
      Assert.AreEqual(true, JToken.DeepEquals(o1.Value, o2.Value));
 
148
    }
 
149
 
 
150
    [Test]
 
151
    public void Next()
 
152
    {
 
153
      JArray a =
 
154
        new JArray(
 
155
          5,
 
156
          6,
 
157
          new JArray(7, 8),
 
158
          new JArray(9, 10)
 
159
        );
 
160
 
 
161
      JToken next = a[0].Next;
 
162
      Assert.AreEqual(6, (int)next);
 
163
 
 
164
      next = next.Next;
 
165
      Assert.IsTrue(JToken.DeepEquals(new JArray(7, 8), next));
 
166
 
 
167
      next = next.Next;
 
168
      Assert.IsTrue(JToken.DeepEquals(new JArray(9, 10), next));
 
169
 
 
170
      next = next.Next;
 
171
      Assert.IsNull(next);
 
172
    }
 
173
 
 
174
    [Test]
 
175
    public void Previous()
 
176
    {
 
177
      JArray a =
 
178
        new JArray(
 
179
          5,
 
180
          6,
 
181
          new JArray(7, 8),
 
182
          new JArray(9, 10)
 
183
        );
 
184
 
 
185
      JToken previous = a[3].Previous;
 
186
      Assert.IsTrue(JToken.DeepEquals(new JArray(7, 8), previous));
 
187
 
 
188
      previous = previous.Previous;
 
189
      Assert.AreEqual(6, (int)previous);
 
190
 
 
191
      previous = previous.Previous;
 
192
      Assert.AreEqual(5, (int)previous);
 
193
 
 
194
      previous = previous.Previous;
 
195
      Assert.IsNull(previous);
 
196
    }
 
197
 
 
198
    [Test]
 
199
    public void Children()
 
200
    {
 
201
      JArray a =
 
202
        new JArray(
 
203
          5,
 
204
          new JArray(1),
 
205
          new JArray(1, 2),
 
206
          new JArray(1, 2, 3)
 
207
        );
 
208
 
 
209
      Assert.AreEqual(4, a.Count());
 
210
      Assert.AreEqual(3, a.Children<JArray>().Count());
 
211
    }
 
212
 
 
213
    [Test]
 
214
    public void BeforeAfter()
 
215
    {
 
216
      JArray a =
 
217
        new JArray(
 
218
          5,
 
219
          new JArray(1, 2, 3),
 
220
          new JArray(1, 2, 3),
 
221
          new JArray(1, 2, 3)
 
222
        );
 
223
 
 
224
      Assert.AreEqual(5, (int)a[1].Previous);
 
225
      Assert.AreEqual(2, a[2].BeforeSelf().Count());
 
226
      //Assert.AreEqual(2, a[2].AfterSelf().Count());
 
227
    }
 
228
 
 
229
    [Test]
 
230
    public void Casting()
 
231
    {
 
232
      Assert.AreEqual(1L, (long)(new JValue(1)));
 
233
      Assert.AreEqual(2L, (long)new JArray(1, 2, 3)[1]);
 
234
 
 
235
      Assert.AreEqual(new DateTime(2000, 12, 20), (DateTime)new JValue(new DateTime(2000, 12, 20)));
 
236
#if !PocketPC && !NET20
 
237
      Assert.AreEqual(new DateTimeOffset(2000, 12, 20, 0, 0, 0, TimeSpan.Zero), (DateTimeOffset)new JValue(new DateTime(2000, 12, 20, 0, 0, 0, DateTimeKind.Utc)));
 
238
      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)));
 
239
      Assert.AreEqual(null, (DateTimeOffset?)new JValue((DateTimeOffset?)null));
 
240
      Assert.AreEqual(null, (DateTimeOffset?)(JValue)null);
 
241
#endif
 
242
      Assert.AreEqual(true, (bool)new JValue(true));
 
243
      Assert.AreEqual(true, (bool?)new JValue(true));
 
244
      Assert.AreEqual(null, (bool?)((JValue)null));
 
245
      Assert.AreEqual(null, (bool?)new JValue((object)null));
 
246
      Assert.AreEqual(10, (long)new JValue(10));
 
247
      Assert.AreEqual(null, (long?)new JValue((long?)null));
 
248
      Assert.AreEqual(null, (long?)(JValue)null);
 
249
      Assert.AreEqual(null, (int?)new JValue((int?)null));
 
250
      Assert.AreEqual(null, (int?)(JValue)null);
 
251
      Assert.AreEqual(null, (DateTime?)new JValue((DateTime?)null));
 
252
      Assert.AreEqual(null, (DateTime?)(JValue)null);
 
253
      Assert.AreEqual(null, (short?)new JValue((short?)null));
 
254
      Assert.AreEqual(null, (short?)(JValue)null);
 
255
      Assert.AreEqual(null, (float?)new JValue((float?)null));
 
256
      Assert.AreEqual(null, (float?)(JValue)null);
 
257
      Assert.AreEqual(null, (double?)new JValue((double?)null));
 
258
      Assert.AreEqual(null, (double?)(JValue)null);
 
259
      Assert.AreEqual(null, (decimal?)new JValue((decimal?)null));
 
260
      Assert.AreEqual(null, (decimal?)(JValue)null);
 
261
      Assert.AreEqual(null, (uint?)new JValue((uint?)null));
 
262
      Assert.AreEqual(null, (uint?)(JValue)null);
 
263
      Assert.AreEqual(null, (sbyte?)new JValue((sbyte?)null));
 
264
      Assert.AreEqual(null, (sbyte?)(JValue)null);
 
265
      Assert.AreEqual(null, (byte?)new JValue((byte?)null));
 
266
      Assert.AreEqual(null, (byte?)(JValue)null);
 
267
      Assert.AreEqual(null, (ulong?)new JValue((ulong?)null));
 
268
      Assert.AreEqual(null, (ulong?)(JValue)null);
 
269
      Assert.AreEqual(null, (uint?)new JValue((uint?)null));
 
270
      Assert.AreEqual(null, (uint?)(JValue)null);
 
271
      Assert.AreEqual(11.1f, (float)new JValue(11.1));
 
272
      Assert.AreEqual(float.MinValue, (float)new JValue(float.MinValue));
 
273
      Assert.AreEqual(1.1, (double)new JValue(1.1));
 
274
      Assert.AreEqual(uint.MaxValue, (uint)new JValue(uint.MaxValue));
 
275
      Assert.AreEqual(ulong.MaxValue, (ulong)new JValue(ulong.MaxValue));
 
276
      Assert.AreEqual(ulong.MaxValue, (ulong)new JProperty("Test", new JValue(ulong.MaxValue)));
 
277
      Assert.AreEqual(null, (string)new JValue((string)null));
 
278
      Assert.AreEqual(5m, (decimal)(new JValue(5L)));
 
279
      Assert.AreEqual(5m, (decimal?)(new JValue(5L)));
 
280
      Assert.AreEqual(5f, (float)(new JValue(5L)));
 
281
      Assert.AreEqual(5f, (float)(new JValue(5m)));
 
282
      Assert.AreEqual(5f, (float?)(new JValue(5m)));
 
283
      Assert.AreEqual(5, (byte)(new JValue(5)));
 
284
 
 
285
      Assert.AreEqual("1", (string)(new JValue(1)));
 
286
      Assert.AreEqual("1", (string)(new JValue(1.0)));
 
287
      Assert.AreEqual("1.0", (string)(new JValue(1.0m)));
 
288
      Assert.AreEqual("True", (string)(new JValue(true)));
 
289
      Assert.AreEqual(null, (string)(new JValue((object)null)));
 
290
      Assert.AreEqual(null, (string)(JValue)null);
 
291
      Assert.AreEqual("12/12/2000 12:12:12", (string)(new JValue(new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Utc))));
 
292
#if !NET20
 
293
      Assert.AreEqual("12/12/2000 12:12:12 +00:00", (string)(new JValue(new DateTimeOffset(2000, 12, 12, 12, 12, 12, TimeSpan.Zero))));
 
294
#endif
 
295
      Assert.AreEqual(true, (bool)(new JValue(1)));
 
296
      Assert.AreEqual(true, (bool)(new JValue(1.0)));
 
297
      Assert.AreEqual(true, (bool)(new JValue("true")));
 
298
      Assert.AreEqual(true, (bool)(new JValue(true)));
 
299
      Assert.AreEqual(1, (int)(new JValue(1)));
 
300
      Assert.AreEqual(1, (int)(new JValue(1.0)));
 
301
      Assert.AreEqual(1, (int)(new JValue("1")));
 
302
      Assert.AreEqual(1, (int)(new JValue(true)));
 
303
      Assert.AreEqual(1m, (decimal)(new JValue(1)));
 
304
      Assert.AreEqual(1m, (decimal)(new JValue(1.0)));
 
305
      Assert.AreEqual(1m, (decimal)(new JValue("1")));
 
306
      Assert.AreEqual(1m, (decimal)(new JValue(true)));
 
307
      Assert.AreEqual(TimeSpan.FromMinutes(1), (TimeSpan)(new JValue(TimeSpan.FromMinutes(1))));
 
308
      Assert.AreEqual("00:01:00", (string)(new JValue(TimeSpan.FromMinutes(1))));
 
309
      Assert.AreEqual(TimeSpan.FromMinutes(1), (TimeSpan)(new JValue("00:01:00")));
 
310
      Assert.AreEqual("46efe013-b56a-4e83-99e4-4dce7678a5bc", (string)(new JValue(new Guid("46EFE013-B56A-4E83-99E4-4DCE7678A5BC"))));
 
311
      Assert.AreEqual("http://www.google.com/", (string)(new JValue(new Uri("http://www.google.com"))));
 
312
      Assert.AreEqual(new Guid("46EFE013-B56A-4E83-99E4-4DCE7678A5BC"), (Guid)(new JValue("46EFE013-B56A-4E83-99E4-4DCE7678A5BC")));
 
313
      Assert.AreEqual(new Guid("46EFE013-B56A-4E83-99E4-4DCE7678A5BC"), (Guid)(new JValue(new Guid("46EFE013-B56A-4E83-99E4-4DCE7678A5BC"))));
 
314
      Assert.AreEqual(new Uri("http://www.google.com"), (Uri)(new JValue("http://www.google.com")));
 
315
      Assert.AreEqual(new Uri("http://www.google.com"), (Uri)(new JValue(new Uri("http://www.google.com"))));
 
316
      Assert.AreEqual(null, (Uri)(new JValue((object)null)));
 
317
      Assert.AreEqual(Convert.ToBase64String(Encoding.UTF8.GetBytes("hi")), (string)(new JValue(Encoding.UTF8.GetBytes("hi"))));
 
318
      Assert.AreEqual(Encoding.UTF8.GetBytes("hi"), (byte[])(new JValue(Convert.ToBase64String(Encoding.UTF8.GetBytes("hi")))));
 
319
 
 
320
      Assert.AreEqual(null, (Uri)(JValue)null);
 
321
      Assert.AreEqual(null, (int?)(JValue)null);
 
322
      Assert.AreEqual(null, (uint?)(JValue)null);
 
323
      Assert.AreEqual(null, (Guid?)(JValue)null);
 
324
      Assert.AreEqual(null, (TimeSpan?)(JValue)null);
 
325
      Assert.AreEqual(null, (byte[])(JValue)null);
 
326
      Assert.AreEqual(null, (bool?)(JValue)null);
 
327
      Assert.AreEqual(null, (char?)(JValue)null);
 
328
      Assert.AreEqual(null, (DateTime?)(JValue)null);
 
329
#if !NET20
 
330
      Assert.AreEqual(null, (DateTimeOffset?)(JValue)null);
 
331
#endif
 
332
      Assert.AreEqual(null, (short?)(JValue)null);
 
333
      Assert.AreEqual(null, (ushort?)(JValue)null);
 
334
      Assert.AreEqual(null, (byte?)(JValue)null);
 
335
      Assert.AreEqual(null, (byte?)(JValue)null);
 
336
      Assert.AreEqual(null, (sbyte?)(JValue)null);
 
337
      Assert.AreEqual(null, (sbyte?)(JValue)null);
 
338
      Assert.AreEqual(null, (long?)(JValue)null);
 
339
      Assert.AreEqual(null, (ulong?)(JValue)null);
 
340
      Assert.AreEqual(null, (double?)(JValue)null);
 
341
      Assert.AreEqual(null, (float?)(JValue)null);
 
342
 
 
343
      byte[] data = new byte[0];
 
344
      Assert.AreEqual(data, (byte[])(new JValue(data)));
 
345
 
 
346
      Assert.AreEqual(5, (int)(new JValue(StringComparison.OrdinalIgnoreCase)));
 
347
    }
 
348
 
 
349
    [Test]
 
350
    public void FailedCasting()
 
351
    {
 
352
      ExceptionAssert.Throws<ArgumentException>("Can not convert Boolean to DateTime.", () => { var i = (DateTime)new JValue(true); });
 
353
      ExceptionAssert.Throws<ArgumentException>("Can not convert Integer to DateTime.", () => { var i = (DateTime)new JValue(1); });
 
354
      ExceptionAssert.Throws<ArgumentException>("Can not convert Float to DateTime.", () => { var i = (DateTime)new JValue(1.1); });
 
355
      ExceptionAssert.Throws<ArgumentException>("Can not convert Float to DateTime.", () => { var i = (DateTime)new JValue(1.1m); });
 
356
      ExceptionAssert.Throws<ArgumentException>("Can not convert TimeSpan to DateTime.", () => { var i = (DateTime)new JValue(TimeSpan.Zero); });
 
357
      ExceptionAssert.Throws<ArgumentException>("Can not convert Uri to DateTime.", () => { var i = (DateTime)new JValue(new Uri("http://www.google.com")); });
 
358
      ExceptionAssert.Throws<ArgumentException>("Can not convert Null to DateTime.", () => { var i = (DateTime)new JValue((object)null); });
 
359
      ExceptionAssert.Throws<ArgumentException>("Can not convert Guid to DateTime.", () => { var i = (DateTime)new JValue(Guid.NewGuid()); });
 
360
 
 
361
      ExceptionAssert.Throws<ArgumentException>("Can not convert Boolean to Uri.", () => { var i = (Uri)new JValue(true); });
 
362
      ExceptionAssert.Throws<ArgumentException>("Can not convert Integer to Uri.", () => { var i = (Uri)new JValue(1); });
 
363
      ExceptionAssert.Throws<ArgumentException>("Can not convert Float to Uri.", () => { var i = (Uri)new JValue(1.1); });
 
364
      ExceptionAssert.Throws<ArgumentException>("Can not convert Float to Uri.", () => { var i = (Uri)new JValue(1.1m); });
 
365
      ExceptionAssert.Throws<ArgumentException>("Can not convert TimeSpan to Uri.", () => { var i = (Uri)new JValue(TimeSpan.Zero); });
 
366
      ExceptionAssert.Throws<ArgumentException>("Can not convert Guid to Uri.", () => { var i = (Uri)new JValue(Guid.NewGuid()); });
 
367
      ExceptionAssert.Throws<ArgumentException>("Can not convert Date to Uri.", () => { var i = (Uri)new JValue(DateTime.Now); });
 
368
#if !NET20
 
369
      ExceptionAssert.Throws<ArgumentException>("Can not convert Date to Uri.", () => { var i = (Uri)new JValue(DateTimeOffset.Now); });
 
370
#endif
 
371
 
 
372
      ExceptionAssert.Throws<ArgumentException>("Can not convert Boolean to TimeSpan.", () => { var i = (TimeSpan)new JValue(true); });
 
373
      ExceptionAssert.Throws<ArgumentException>("Can not convert Integer to TimeSpan.", () => { var i = (TimeSpan)new JValue(1); });
 
374
      ExceptionAssert.Throws<ArgumentException>("Can not convert Float to TimeSpan.", () => { var i = (TimeSpan)new JValue(1.1); });
 
375
      ExceptionAssert.Throws<ArgumentException>("Can not convert Float to TimeSpan.", () => { var i = (TimeSpan)new JValue(1.1m); });
 
376
      ExceptionAssert.Throws<ArgumentException>("Can not convert Null to TimeSpan.", () => { var i = (TimeSpan)new JValue((object)null); });
 
377
      ExceptionAssert.Throws<ArgumentException>("Can not convert Guid to TimeSpan.", () => { var i = (TimeSpan)new JValue(Guid.NewGuid()); });
 
378
      ExceptionAssert.Throws<ArgumentException>("Can not convert Date to TimeSpan.", () => { var i = (TimeSpan)new JValue(DateTime.Now); });
 
379
#if !NET20
 
380
      ExceptionAssert.Throws<ArgumentException>("Can not convert Date to TimeSpan.", () => { var i = (TimeSpan)new JValue(DateTimeOffset.Now); });
 
381
#endif
 
382
      ExceptionAssert.Throws<ArgumentException>("Can not convert Uri to TimeSpan.", () => { var i = (TimeSpan)new JValue(new Uri("http://www.google.com")); });
 
383
 
 
384
      ExceptionAssert.Throws<ArgumentException>("Can not convert Boolean to Guid.", () => { var i = (Guid)new JValue(true); });
 
385
      ExceptionAssert.Throws<ArgumentException>("Can not convert Integer to Guid.", () => { var i = (Guid)new JValue(1); });
 
386
      ExceptionAssert.Throws<ArgumentException>("Can not convert Float to Guid.", () => { var i = (Guid)new JValue(1.1); });
 
387
      ExceptionAssert.Throws<ArgumentException>("Can not convert Float to Guid.", () => { var i = (Guid)new JValue(1.1m); });
 
388
      ExceptionAssert.Throws<ArgumentException>("Can not convert Null to Guid.", () => { var i = (Guid)new JValue((object)null); });
 
389
      ExceptionAssert.Throws<ArgumentException>("Can not convert Date to Guid.", () => { var i = (Guid)new JValue(DateTime.Now); });
 
390
#if !NET20
 
391
      ExceptionAssert.Throws<ArgumentException>("Can not convert Date to Guid.", () => { var i = (Guid)new JValue(DateTimeOffset.Now); });
 
392
#endif
 
393
      ExceptionAssert.Throws<ArgumentException>("Can not convert TimeSpan to Guid.", () => { var i = (Guid)new JValue(TimeSpan.FromMinutes(1)); });
 
394
      ExceptionAssert.Throws<ArgumentException>("Can not convert Uri to Guid.", () => { var i = (Guid)new JValue(new Uri("http://www.google.com")); });
 
395
 
 
396
#if !NET20
 
397
      ExceptionAssert.Throws<Exception>("Can not convert Boolean to DateTimeOffset.", () => { var i = (DateTimeOffset)new JValue(true); });
 
398
#endif
 
399
      ExceptionAssert.Throws<Exception>("Can not convert Boolean to Uri.", () => { var i = (Uri)new JValue(true); });
 
400
    }
 
401
 
 
402
    [Test]
 
403
    public void ToObject()
 
404
    {
 
405
      Assert.AreEqual((ushort)1, (new JValue(1).ToObject(typeof(ushort))));
 
406
      Assert.AreEqual((ushort)1, (new JValue(1).ToObject(typeof(ushort?))));
 
407
      Assert.AreEqual((uint)1L, (new JValue(1).ToObject(typeof(uint))));
 
408
      Assert.AreEqual((uint)1L, (new JValue(1).ToObject(typeof(uint?))));
 
409
      Assert.AreEqual((ulong)1L, (new JValue(1).ToObject(typeof(ulong))));
 
410
      Assert.AreEqual((ulong)1L, (new JValue(1).ToObject(typeof(ulong?))));
 
411
      Assert.AreEqual((sbyte)1L, (new JValue(1).ToObject(typeof(sbyte))));
 
412
      Assert.AreEqual((sbyte)1L, (new JValue(1).ToObject(typeof(sbyte?))));
 
413
      Assert.AreEqual((byte)1L, (new JValue(1).ToObject(typeof(byte))));
 
414
      Assert.AreEqual((byte)1L, (new JValue(1).ToObject(typeof(byte?))));
 
415
      Assert.AreEqual((short)1L, (new JValue(1).ToObject(typeof(short))));
 
416
      Assert.AreEqual((short)1L, (new JValue(1).ToObject(typeof(short?))));
 
417
      Assert.AreEqual(1, (new JValue(1).ToObject(typeof(int))));
 
418
      Assert.AreEqual(1, (new JValue(1).ToObject(typeof(int?))));
 
419
      Assert.AreEqual(1L, (new JValue(1).ToObject(typeof(long))));
 
420
      Assert.AreEqual(1L, (new JValue(1).ToObject(typeof(long?))));
 
421
      Assert.AreEqual((float)1, (new JValue(1.0).ToObject(typeof(float))));
 
422
      Assert.AreEqual((float)1, (new JValue(1.0).ToObject(typeof(float?))));
 
423
      Assert.AreEqual((double)1, (new JValue(1.0).ToObject(typeof(double))));
 
424
      Assert.AreEqual((double)1, (new JValue(1.0).ToObject(typeof(double?))));
 
425
      Assert.AreEqual(1m, (new JValue(1).ToObject(typeof(decimal))));
 
426
      Assert.AreEqual(1m, (new JValue(1).ToObject(typeof(decimal?))));
 
427
      Assert.AreEqual(true, (new JValue(true).ToObject(typeof(bool))));
 
428
      Assert.AreEqual(true, (new JValue(true).ToObject(typeof(bool?))));
 
429
      Assert.AreEqual('b', (new JValue('b').ToObject(typeof(char))));
 
430
      Assert.AreEqual('b', (new JValue('b').ToObject(typeof(char?))));
 
431
      Assert.AreEqual(TimeSpan.MaxValue, (new JValue(TimeSpan.MaxValue).ToObject(typeof(TimeSpan))));
 
432
      Assert.AreEqual(TimeSpan.MaxValue, (new JValue(TimeSpan.MaxValue).ToObject(typeof(TimeSpan?))));
 
433
      Assert.AreEqual(DateTime.MaxValue, (new JValue(DateTime.MaxValue).ToObject(typeof(DateTime))));
 
434
      Assert.AreEqual(DateTime.MaxValue, (new JValue(DateTime.MaxValue).ToObject(typeof(DateTime?))));
 
435
#if !NET20
 
436
      Assert.AreEqual(DateTimeOffset.MaxValue, (new JValue(DateTimeOffset.MaxValue).ToObject(typeof(DateTimeOffset))));
 
437
      Assert.AreEqual(DateTimeOffset.MaxValue, (new JValue(DateTimeOffset.MaxValue).ToObject(typeof(DateTimeOffset?))));
 
438
#endif
 
439
      Assert.AreEqual("b", (new JValue("b").ToObject(typeof(string))));
 
440
      Assert.AreEqual(new Guid("A34B2080-B5F0-488E-834D-45D44ECB9E5C"), (new JValue(new Guid("A34B2080-B5F0-488E-834D-45D44ECB9E5C")).ToObject(typeof(Guid))));
 
441
      Assert.AreEqual(new Guid("A34B2080-B5F0-488E-834D-45D44ECB9E5C"), (new JValue(new Guid("A34B2080-B5F0-488E-834D-45D44ECB9E5C")).ToObject(typeof(Guid?))));
 
442
      Assert.AreEqual(new Uri("http://www.google.com/"), (new JValue(new Uri("http://www.google.com/")).ToObject(typeof(Uri))));
 
443
    }
 
444
 
 
445
    [Test]
 
446
    public void ImplicitCastingTo()
 
447
    {
 
448
      Assert.IsTrue(JToken.DeepEquals(new JValue(new DateTime(2000, 12, 20)), (JValue)new DateTime(2000, 12, 20)));
 
449
#if !PocketPC && !NET20
 
450
      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)));
 
451
      Assert.IsTrue(JToken.DeepEquals(new JValue((DateTimeOffset?)null), (JValue)(DateTimeOffset?)null));
 
452
#endif
 
453
 
 
454
      Assert.IsTrue(JToken.DeepEquals(new JValue(true), (JValue)true));
 
455
      Assert.IsTrue(JToken.DeepEquals(new JValue(true), (JValue)(bool?)true));
 
456
      Assert.IsTrue(JToken.DeepEquals(new JValue((bool?)null), (JValue)(bool?)null));
 
457
      Assert.IsTrue(JToken.DeepEquals(new JValue(10), (JValue)10));
 
458
      Assert.IsTrue(JToken.DeepEquals(new JValue((long?)null), (JValue)(long?)null));
 
459
      Assert.IsTrue(JToken.DeepEquals(new JValue((DateTime?)null), (JValue)(DateTime?)null));
 
460
      Assert.IsTrue(JToken.DeepEquals(new JValue(long.MaxValue), (JValue)long.MaxValue));
 
461
      Assert.IsTrue(JToken.DeepEquals(new JValue((int?)null), (JValue)(int?)null));
 
462
      Assert.IsTrue(JToken.DeepEquals(new JValue((short?)null), (JValue)(short?)null));
 
463
      Assert.IsTrue(JToken.DeepEquals(new JValue((double?)null), (JValue)(double?)null));
 
464
      Assert.IsTrue(JToken.DeepEquals(new JValue((uint?)null), (JValue)(uint?)null));
 
465
      Assert.IsTrue(JToken.DeepEquals(new JValue((decimal?)null), (JValue)(decimal?)null));
 
466
      Assert.IsTrue(JToken.DeepEquals(new JValue((ulong?)null), (JValue)(ulong?)null));
 
467
      Assert.IsTrue(JToken.DeepEquals(new JValue((sbyte?)null), (JValue)(sbyte?)null));
 
468
      Assert.IsTrue(JToken.DeepEquals(new JValue((ushort?)null), (JValue)(ushort?)null));
 
469
      Assert.IsTrue(JToken.DeepEquals(new JValue(short.MaxValue), (JValue)short.MaxValue));
 
470
      Assert.IsTrue(JToken.DeepEquals(new JValue(ushort.MaxValue), (JValue)ushort.MaxValue));
 
471
      Assert.IsTrue(JToken.DeepEquals(new JValue(11.1f), (JValue)11.1f));
 
472
      Assert.IsTrue(JToken.DeepEquals(new JValue(float.MinValue), (JValue)float.MinValue));
 
473
      Assert.IsTrue(JToken.DeepEquals(new JValue(double.MinValue), (JValue)double.MinValue));
 
474
      Assert.IsTrue(JToken.DeepEquals(new JValue(uint.MaxValue), (JValue)uint.MaxValue));
 
475
      Assert.IsTrue(JToken.DeepEquals(new JValue(ulong.MaxValue), (JValue)ulong.MaxValue));
 
476
      Assert.IsTrue(JToken.DeepEquals(new JValue(ulong.MinValue), (JValue)ulong.MinValue));
 
477
      Assert.IsTrue(JToken.DeepEquals(new JValue((string)null), (JValue)(string)null));
 
478
      Assert.IsTrue(JToken.DeepEquals(new JValue((DateTime?)null), (JValue)(DateTime?)null));
 
479
      Assert.IsTrue(JToken.DeepEquals(new JValue(decimal.MaxValue), (JValue)decimal.MaxValue));
 
480
      Assert.IsTrue(JToken.DeepEquals(new JValue(decimal.MaxValue), (JValue)(decimal?)decimal.MaxValue));
 
481
      Assert.IsTrue(JToken.DeepEquals(new JValue(decimal.MinValue), (JValue)decimal.MinValue));
 
482
      Assert.IsTrue(JToken.DeepEquals(new JValue(float.MaxValue), (JValue)(float?)float.MaxValue));
 
483
      Assert.IsTrue(JToken.DeepEquals(new JValue(double.MaxValue), (JValue)(double?)double.MaxValue));
 
484
      Assert.IsTrue(JToken.DeepEquals(new JValue((object)null), (JValue)(double?)null));
 
485
 
 
486
      Assert.IsFalse(JToken.DeepEquals(new JValue(true), (JValue)(bool?)null));
 
487
      Assert.IsFalse(JToken.DeepEquals(new JValue((object)null), (JValue)(object)null));
 
488
 
 
489
      byte[] emptyData = new byte[0];
 
490
      Assert.IsTrue(JToken.DeepEquals(new JValue(emptyData), (JValue)emptyData));
 
491
      Assert.IsFalse(JToken.DeepEquals(new JValue(emptyData), (JValue)new byte[1]));
 
492
      Assert.IsTrue(JToken.DeepEquals(new JValue(Encoding.UTF8.GetBytes("Hi")), (JValue)Encoding.UTF8.GetBytes("Hi")));
 
493
 
 
494
      Assert.IsTrue(JToken.DeepEquals(new JValue(TimeSpan.FromMinutes(1)), (JValue)TimeSpan.FromMinutes(1)));
 
495
      Assert.IsTrue(JToken.DeepEquals(new JValue((object)null), (JValue)(TimeSpan?)null));
 
496
      Assert.IsTrue(JToken.DeepEquals(new JValue(TimeSpan.FromMinutes(1)), (JValue)(TimeSpan?)TimeSpan.FromMinutes(1)));
 
497
      Assert.IsTrue(JToken.DeepEquals(new JValue(new Guid("46EFE013-B56A-4E83-99E4-4DCE7678A5BC")), (JValue)new Guid("46EFE013-B56A-4E83-99E4-4DCE7678A5BC")));
 
498
      Assert.IsTrue(JToken.DeepEquals(new JValue(new Uri("http://www.google.com")), (JValue)new Uri("http://www.google.com")));
 
499
      Assert.IsTrue(JToken.DeepEquals(new JValue((object)null), (JValue)(Uri)null));
 
500
      Assert.IsTrue(JToken.DeepEquals(new JValue((object)null), (JValue)(Guid?)null));
 
501
    }
 
502
 
 
503
    [Test]
 
504
    public void Root()
 
505
    {
 
506
      JArray a =
 
507
        new JArray(
 
508
          5,
 
509
          6,
 
510
          new JArray(7, 8),
 
511
          new JArray(9, 10)
 
512
        );
 
513
 
 
514
      Assert.AreEqual(a, a.Root);
 
515
      Assert.AreEqual(a, a[0].Root);
 
516
      Assert.AreEqual(a, ((JArray)a[2])[0].Root);
 
517
    }
 
518
 
 
519
    [Test]
 
520
    public void Remove()
 
521
    {
 
522
      JToken t;
 
523
      JArray a =
 
524
        new JArray(
 
525
          5,
 
526
          6,
 
527
          new JArray(7, 8),
 
528
          new JArray(9, 10)
 
529
        );
 
530
 
 
531
      a[0].Remove();
 
532
 
 
533
      Assert.AreEqual(6, (int)a[0]);
 
534
 
 
535
      a[1].Remove();
 
536
 
 
537
      Assert.AreEqual(6, (int)a[0]);
 
538
      Assert.IsTrue(JToken.DeepEquals(new JArray(9, 10), a[1]));
 
539
      Assert.AreEqual(2, a.Count());
 
540
 
 
541
      t = a[1];
 
542
      t.Remove();
 
543
      Assert.AreEqual(6, (int)a[0]);
 
544
      Assert.IsNull(t.Next);
 
545
      Assert.IsNull(t.Previous);
 
546
      Assert.IsNull(t.Parent);
 
547
 
 
548
      t = a[0];
 
549
      t.Remove();
 
550
      Assert.AreEqual(0, a.Count());
 
551
 
 
552
      Assert.IsNull(t.Next);
 
553
      Assert.IsNull(t.Previous);
 
554
      Assert.IsNull(t.Parent);
 
555
    }
 
556
 
 
557
    [Test]
 
558
    public void AfterSelf()
 
559
    {
 
560
      JArray a =
 
561
        new JArray(
 
562
          5,
 
563
          new JArray(1),
 
564
          new JArray(1, 2),
 
565
          new JArray(1, 2, 3)
 
566
        );
 
567
 
 
568
      JToken t = a[1];
 
569
      List<JToken> afterTokens = t.AfterSelf().ToList();
 
570
 
 
571
      Assert.AreEqual(2, afterTokens.Count);
 
572
      Assert.IsTrue(JToken.DeepEquals(new JArray(1, 2), afterTokens[0]));
 
573
      Assert.IsTrue(JToken.DeepEquals(new JArray(1, 2, 3), afterTokens[1]));
 
574
    }
 
575
 
 
576
    [Test]
 
577
    public void BeforeSelf()
 
578
    {
 
579
      JArray a =
 
580
        new JArray(
 
581
          5,
 
582
          new JArray(1),
 
583
          new JArray(1, 2),
 
584
          new JArray(1, 2, 3)
 
585
        );
 
586
 
 
587
      JToken t = a[2];
 
588
      List<JToken> beforeTokens = t.BeforeSelf().ToList();
 
589
 
 
590
      Assert.AreEqual(2, beforeTokens.Count);
 
591
      Assert.IsTrue(JToken.DeepEquals(new JValue(5), beforeTokens[0]));
 
592
      Assert.IsTrue(JToken.DeepEquals(new JArray(1), beforeTokens[1]));
 
593
    }
 
594
 
 
595
    [Test]
 
596
    public void HasValues()
 
597
    {
 
598
      JArray a =
 
599
        new JArray(
 
600
          5,
 
601
          new JArray(1),
 
602
          new JArray(1, 2),
 
603
          new JArray(1, 2, 3)
 
604
        );
 
605
 
 
606
      Assert.IsTrue(a.HasValues);
 
607
    }
 
608
 
 
609
    [Test]
 
610
    public void Ancestors()
 
611
    {
 
612
      JArray a =
 
613
        new JArray(
 
614
          5,
 
615
          new JArray(1),
 
616
          new JArray(1, 2),
 
617
          new JArray(1, 2, 3)
 
618
        );
 
619
 
 
620
      JToken t = a[1][0];
 
621
      List<JToken> ancestors = t.Ancestors().ToList();
 
622
      Assert.AreEqual(2, ancestors.Count());
 
623
      Assert.AreEqual(a[1], ancestors[0]);
 
624
      Assert.AreEqual(a, ancestors[1]);
 
625
    }
 
626
 
 
627
    [Test]
 
628
    public void Descendants()
 
629
    {
 
630
      JArray a =
 
631
        new JArray(
 
632
          5,
 
633
          new JArray(1),
 
634
          new JArray(1, 2),
 
635
          new JArray(1, 2, 3)
 
636
        );
 
637
 
 
638
      List<JToken> descendants = a.Descendants().ToList();
 
639
      Assert.AreEqual(10, descendants.Count());
 
640
      Assert.AreEqual(5, (int)descendants[0]);
 
641
      Assert.IsTrue(JToken.DeepEquals(new JArray(1, 2, 3), descendants[descendants.Count - 4]));
 
642
      Assert.AreEqual(1, (int)descendants[descendants.Count - 3]);
 
643
      Assert.AreEqual(2, (int)descendants[descendants.Count - 2]);
 
644
      Assert.AreEqual(3, (int)descendants[descendants.Count - 1]);
 
645
    }
 
646
 
 
647
    [Test]
 
648
    public void CreateWriter()
 
649
    {
 
650
      JArray a =
 
651
        new JArray(
 
652
          5,
 
653
          new JArray(1),
 
654
          new JArray(1, 2),
 
655
          new JArray(1, 2, 3)
 
656
        );
 
657
 
 
658
      JsonWriter writer = a.CreateWriter();
 
659
      Assert.IsNotNull(writer);
 
660
      Assert.AreEqual(4, a.Count());
 
661
 
 
662
      writer.WriteValue("String");
 
663
      Assert.AreEqual(5, a.Count());
 
664
      Assert.AreEqual("String", (string)a[4]);
 
665
 
 
666
      writer.WriteStartObject();
 
667
      writer.WritePropertyName("Property");
 
668
      writer.WriteValue("PropertyValue");
 
669
      writer.WriteEnd();
 
670
 
 
671
      Assert.AreEqual(6, a.Count());
 
672
      Assert.IsTrue(JToken.DeepEquals(new JObject(new JProperty("Property", "PropertyValue")), a[5]));
 
673
    }
 
674
 
 
675
    [Test]
 
676
    public void AddFirst()
 
677
    {
 
678
      JArray a =
 
679
        new JArray(
 
680
          5,
 
681
          new JArray(1),
 
682
          new JArray(1, 2),
 
683
          new JArray(1, 2, 3)
 
684
        );
 
685
 
 
686
      a.AddFirst("First");
 
687
 
 
688
      Assert.AreEqual("First", (string)a[0]);
 
689
      Assert.AreEqual(a, a[0].Parent);
 
690
      Assert.AreEqual(a[1], a[0].Next);
 
691
      Assert.AreEqual(5, a.Count());
 
692
 
 
693
      a.AddFirst("NewFirst");
 
694
      Assert.AreEqual("NewFirst", (string)a[0]);
 
695
      Assert.AreEqual(a, a[0].Parent);
 
696
      Assert.AreEqual(a[1], a[0].Next);
 
697
      Assert.AreEqual(6, a.Count());
 
698
 
 
699
      Assert.AreEqual(a[0], a[0].Next.Previous);
 
700
    }
 
701
 
 
702
    [Test]
 
703
    public void RemoveAll()
 
704
    {
 
705
      JArray a =
 
706
        new JArray(
 
707
          5,
 
708
          new JArray(1),
 
709
          new JArray(1, 2),
 
710
          new JArray(1, 2, 3)
 
711
        );
 
712
 
 
713
      JToken first = a.First;
 
714
      Assert.AreEqual(5, (int)first);
 
715
 
 
716
      a.RemoveAll();
 
717
      Assert.AreEqual(0, a.Count());
 
718
 
 
719
      Assert.IsNull(first.Parent);
 
720
      Assert.IsNull(first.Next);
 
721
    }
 
722
 
 
723
    [Test]
 
724
    public void AddPropertyToArray()
 
725
    {
 
726
      ExceptionAssert.Throws<ArgumentException>("Can not add Newtonsoft.Json.Linq.JProperty to Newtonsoft.Json.Linq.JArray.",
 
727
      () =>
 
728
      {
 
729
        JArray a = new JArray();
 
730
        a.Add(new JProperty("PropertyName"));
 
731
      });
 
732
    }
 
733
 
 
734
    [Test]
 
735
    public void AddValueToObject()
 
736
    {
 
737
      ExceptionAssert.Throws<ArgumentException>(
 
738
        "Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject.",
 
739
        () =>
 
740
        {
 
741
          JObject o = new JObject();
 
742
          o.Add(5);
 
743
        });
 
744
    }
 
745
 
 
746
    [Test]
 
747
    public void Replace()
 
748
    {
 
749
      JArray a =
 
750
        new JArray(
 
751
          5,
 
752
          new JArray(1),
 
753
          new JArray(1, 2),
 
754
          new JArray(1, 2, 3)
 
755
        );
 
756
 
 
757
      a[0].Replace(new JValue(int.MaxValue));
 
758
      Assert.AreEqual(int.MaxValue, (int)a[0]);
 
759
      Assert.AreEqual(4, a.Count());
 
760
 
 
761
      a[1][0].Replace(new JValue("Test"));
 
762
      Assert.AreEqual("Test", (string)a[1][0]);
 
763
 
 
764
      a[2].Replace(new JValue(int.MaxValue));
 
765
      Assert.AreEqual(int.MaxValue, (int)a[2]);
 
766
      Assert.AreEqual(4, a.Count());
 
767
 
 
768
      Assert.IsTrue(JToken.DeepEquals(new JArray(int.MaxValue, new JArray("Test"), int.MaxValue, new JArray(1, 2, 3)), a));
 
769
    }
 
770
 
 
771
    [Test]
 
772
    public void ToStringWithConverters()
 
773
    {
 
774
      JArray a =
 
775
        new JArray(
 
776
          new JValue(new DateTime(2009, 2, 15, 0, 0, 0, DateTimeKind.Utc))
 
777
        );
 
778
 
 
779
      string json = a.ToString(Formatting.Indented, new IsoDateTimeConverter());
 
780
 
 
781
      Assert.AreEqual(@"[
 
782
  ""2009-02-15T00:00:00Z""
 
783
]", json);
 
784
 
 
785
      json = JsonConvert.SerializeObject(a, new IsoDateTimeConverter());
 
786
 
 
787
      Assert.AreEqual(@"[""2009-02-15T00:00:00Z""]", json);
 
788
    }
 
789
 
 
790
    [Test]
 
791
    public void ToStringWithNoIndenting()
 
792
    {
 
793
      JArray a =
 
794
        new JArray(
 
795
          new JValue(new DateTime(2009, 2, 15, 0, 0, 0, DateTimeKind.Utc))
 
796
        );
 
797
 
 
798
      string json = a.ToString(Formatting.None, new IsoDateTimeConverter());
 
799
 
 
800
      Assert.AreEqual(@"[""2009-02-15T00:00:00Z""]", json);
 
801
    }
 
802
 
 
803
    [Test]
 
804
    public void AddAfterSelf()
 
805
    {
 
806
      JArray a =
 
807
        new JArray(
 
808
          5,
 
809
          new JArray(1),
 
810
          new JArray(1, 2),
 
811
          new JArray(1, 2, 3)
 
812
        );
 
813
 
 
814
      a[1].AddAfterSelf("pie");
 
815
 
 
816
      Assert.AreEqual(5, (int)a[0]);
 
817
      Assert.AreEqual(1, a[1].Count());
 
818
      Assert.AreEqual("pie", (string)a[2]);
 
819
      Assert.AreEqual(5, a.Count());
 
820
 
 
821
      a[4].AddAfterSelf("lastpie");
 
822
 
 
823
      Assert.AreEqual("lastpie", (string)a[5]);
 
824
      Assert.AreEqual("lastpie", (string)a.Last);
 
825
    }
 
826
 
 
827
    [Test]
 
828
    public void AddBeforeSelf()
 
829
    {
 
830
      JArray a =
 
831
        new JArray(
 
832
          5,
 
833
          new JArray(1),
 
834
          new JArray(1, 2),
 
835
          new JArray(1, 2, 3)
 
836
        );
 
837
 
 
838
      a[1].AddBeforeSelf("pie");
 
839
 
 
840
      Assert.AreEqual(5, (int)a[0]);
 
841
      Assert.AreEqual("pie", (string)a[1]);
 
842
      Assert.AreEqual(a, a[1].Parent);
 
843
      Assert.AreEqual(a[2], a[1].Next);
 
844
      Assert.AreEqual(5, a.Count());
 
845
 
 
846
      a[0].AddBeforeSelf("firstpie");
 
847
 
 
848
      Assert.AreEqual("firstpie", (string)a[0]);
 
849
      Assert.AreEqual(5, (int)a[1]);
 
850
      Assert.AreEqual("pie", (string)a[2]);
 
851
      Assert.AreEqual(a, a[0].Parent);
 
852
      Assert.AreEqual(a[1], a[0].Next);
 
853
      Assert.AreEqual(6, a.Count());
 
854
 
 
855
      a.Last.AddBeforeSelf("secondlastpie");
 
856
 
 
857
      Assert.AreEqual("secondlastpie", (string)a[5]);
 
858
      Assert.AreEqual(7, a.Count());
 
859
    }
 
860
 
 
861
    [Test]
 
862
    public void DeepClone()
 
863
    {
 
864
      JArray a =
 
865
        new JArray(
 
866
          5,
 
867
          new JArray(1),
 
868
          new JArray(1, 2),
 
869
          new JArray(1, 2, 3),
 
870
          new JObject(
 
871
            new JProperty("First", new JValue(Encoding.UTF8.GetBytes("Hi"))),
 
872
            new JProperty("Second", 1),
 
873
            new JProperty("Third", null),
 
874
            new JProperty("Fourth", new JConstructor("Date", 12345)),
 
875
            new JProperty("Fifth", double.PositiveInfinity),
 
876
            new JProperty("Sixth", double.NaN)
 
877
            )
 
878
        );
 
879
 
 
880
      JArray a2 = (JArray)a.DeepClone();
 
881
 
 
882
      Console.WriteLine(a2.ToString(Formatting.Indented));
 
883
 
 
884
      Assert.IsTrue(a.DeepEquals(a2));
 
885
    }
 
886
 
 
887
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
 
888
    [Test]
 
889
    public void Clone()
 
890
    {
 
891
      JArray a =
 
892
        new JArray(
 
893
          5,
 
894
          new JArray(1),
 
895
          new JArray(1, 2),
 
896
          new JArray(1, 2, 3),
 
897
          new JObject(
 
898
            new JProperty("First", new JValue(Encoding.UTF8.GetBytes("Hi"))),
 
899
            new JProperty("Second", 1),
 
900
            new JProperty("Third", null),
 
901
            new JProperty("Fourth", new JConstructor("Date", 12345)),
 
902
            new JProperty("Fifth", double.PositiveInfinity),
 
903
            new JProperty("Sixth", double.NaN)
 
904
            )
 
905
        );
 
906
 
 
907
      ICloneable c = a;
 
908
 
 
909
      JArray a2 = (JArray) c.Clone();
 
910
 
 
911
      Assert.IsTrue(a.DeepEquals(a2));
 
912
    }
 
913
#endif
 
914
 
 
915
    [Test]
 
916
    public void DoubleDeepEquals()
 
917
    {
 
918
      JArray a =
 
919
        new JArray(
 
920
          double.NaN,
 
921
          double.PositiveInfinity,
 
922
          double.NegativeInfinity
 
923
        );
 
924
 
 
925
      JArray a2 = (JArray)a.DeepClone();
 
926
 
 
927
      Assert.IsTrue(a.DeepEquals(a2));
 
928
 
 
929
      double d = 1 + 0.1 + 0.1 + 0.1;
 
930
 
 
931
      JValue v1 = new JValue(d);
 
932
      JValue v2 = new JValue(1.3);
 
933
 
 
934
      Assert.IsTrue(v1.DeepEquals(v2));
 
935
    }
 
936
 
 
937
    [Test]
 
938
    public void ParseAdditionalContent()
 
939
    {
 
940
      ExceptionAssert.Throws<JsonReaderException>("Additional text encountered after finished reading JSON content: ,. Path '', line 5, position 2.",
 
941
        () =>
 
942
        {
 
943
          string json = @"[
 
944
""Small"",
 
945
""Medium"",
 
946
""Large""
 
947
],";
 
948
 
 
949
          JToken.Parse(json);
 
950
        });
 
951
    }
 
952
  }
 
953
}
 
 
b'\\ No newline at end of file'