~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/JValueTests.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
#if !NETFX_CORE
 
30
using NUnit.Framework;
 
31
#else
 
32
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
 
33
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
 
34
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
 
35
#endif
 
36
using Newtonsoft.Json.Linq;
 
37
using System.Globalization;
 
38
#if NET20
 
39
using Newtonsoft.Json.Utilities.LinqBridge;
 
40
#else
 
41
using System.Linq;
 
42
#endif
 
43
 
 
44
namespace Newtonsoft.Json.Tests.Linq
 
45
{
 
46
  [TestFixture]
 
47
  public class JValueTests : TestFixtureBase
 
48
  {
 
49
    [Test]
 
50
    public void ChangeValue()
 
51
    {
 
52
      JValue v = new JValue(true);
 
53
      Assert.AreEqual(true, v.Value);
 
54
      Assert.AreEqual(JTokenType.Boolean, v.Type);
 
55
 
 
56
      v.Value = "Pie";
 
57
      Assert.AreEqual("Pie", v.Value);
 
58
      Assert.AreEqual(JTokenType.String, v.Type);
 
59
 
 
60
      v.Value = null;
 
61
      Assert.AreEqual(null, v.Value);
 
62
      Assert.AreEqual(JTokenType.Null, v.Type);
 
63
 
 
64
      v.Value = (int?) null;
 
65
      Assert.AreEqual(null, v.Value);
 
66
      Assert.AreEqual(JTokenType.Null, v.Type);
 
67
 
 
68
      v.Value = "Pie";
 
69
      Assert.AreEqual("Pie", v.Value);
 
70
      Assert.AreEqual(JTokenType.String, v.Type);
 
71
 
 
72
#if !(NETFX_CORE || PORTABLE)
 
73
      v.Value = DBNull.Value;
 
74
      Assert.AreEqual(DBNull.Value, v.Value);
 
75
      Assert.AreEqual(JTokenType.Null, v.Type);
 
76
#endif
 
77
 
 
78
      byte[] data = new byte[0];
 
79
      v.Value = data;
 
80
 
 
81
      Assert.AreEqual(data, v.Value);
 
82
      Assert.AreEqual(JTokenType.Bytes, v.Type);
 
83
 
 
84
      v.Value = StringComparison.OrdinalIgnoreCase;
 
85
      Assert.AreEqual(StringComparison.OrdinalIgnoreCase, v.Value);
 
86
      Assert.AreEqual(JTokenType.Integer, v.Type);
 
87
 
 
88
      v.Value = new Uri("http://json.codeplex.com/");
 
89
      Assert.AreEqual(new Uri("http://json.codeplex.com/"), v.Value);
 
90
      Assert.AreEqual(JTokenType.Uri, v.Type);
 
91
 
 
92
      v.Value = TimeSpan.FromDays(1);
 
93
      Assert.AreEqual(TimeSpan.FromDays(1), v.Value);
 
94
      Assert.AreEqual(JTokenType.TimeSpan, v.Type);
 
95
 
 
96
      Guid g = Guid.NewGuid();
 
97
      v.Value = g;
 
98
      Assert.AreEqual(g, v.Value);
 
99
      Assert.AreEqual(JTokenType.Guid, v.Type);
 
100
    }
 
101
 
 
102
    [Test]
 
103
    public void CreateComment()
 
104
    {
 
105
      JValue commentValue = JValue.CreateComment(null);
 
106
      Assert.AreEqual(null, commentValue.Value);
 
107
      Assert.AreEqual(JTokenType.Comment, commentValue.Type);
 
108
 
 
109
      commentValue.Value = "Comment";
 
110
      Assert.AreEqual("Comment", commentValue.Value);
 
111
      Assert.AreEqual(JTokenType.Comment, commentValue.Type);
 
112
    }
 
113
 
 
114
    [Test]
 
115
    public void CreateString()
 
116
    {
 
117
      JValue stringValue = JValue.CreateString(null);
 
118
      Assert.AreEqual(null, stringValue.Value);
 
119
      Assert.AreEqual(JTokenType.String, stringValue.Type);
 
120
    }
 
121
 
 
122
    [Test]
 
123
    public void JValueToString()
 
124
    {
 
125
      JValue v;
 
126
 
 
127
      v = new JValue(true);
 
128
      Assert.AreEqual("True", v.ToString());
 
129
 
 
130
      v = new JValue(Encoding.UTF8.GetBytes("Blah"));
 
131
      Assert.AreEqual("System.Byte[]", v.ToString(null, CultureInfo.InvariantCulture));
 
132
 
 
133
      v = new JValue("I am a string!");
 
134
      Assert.AreEqual("I am a string!", v.ToString());
 
135
 
 
136
      v = new JValue(null, JTokenType.Null);
 
137
      Assert.AreEqual("", v.ToString());
 
138
 
 
139
      v = new JValue(null, JTokenType.Null);
 
140
      Assert.AreEqual("", v.ToString(null, CultureInfo.InvariantCulture));
 
141
 
 
142
      v = new JValue(new DateTime(2000, 12, 12, 20, 59, 59, DateTimeKind.Utc), JTokenType.Date);
 
143
      Assert.AreEqual("12/12/2000 20:59:59", v.ToString(null, CultureInfo.InvariantCulture));
 
144
 
 
145
      v = new JValue(new Uri("http://json.codeplex.com/"));
 
146
      Assert.AreEqual("http://json.codeplex.com/", v.ToString(null, CultureInfo.InvariantCulture));
 
147
 
 
148
      v = new JValue(TimeSpan.FromDays(1));
 
149
      Assert.AreEqual("1.00:00:00", v.ToString(null, CultureInfo.InvariantCulture));
 
150
 
 
151
      v = new JValue(new Guid("B282ADE7-C520-496C-A448-4084F6803DE5"));
 
152
      Assert.AreEqual("b282ade7-c520-496c-a448-4084f6803de5", v.ToString(null, CultureInfo.InvariantCulture));
 
153
    }
 
154
 
 
155
    [Test]
 
156
    public void Last()
 
157
    {
 
158
      ExceptionAssert.Throws<InvalidOperationException>("Cannot access child value on Newtonsoft.Json.Linq.JValue.",
 
159
        () =>
 
160
          {
 
161
            JValue v = new JValue(true);
 
162
            JToken last = v.Last;
 
163
          });
 
164
    }
 
165
 
 
166
    [Test]
 
167
    public void Children()
 
168
    {
 
169
      JValue v = new JValue(true);
 
170
      var c = v.Children();
 
171
      Assert.AreEqual(JEnumerable<JToken>.Empty, c);
 
172
    }
 
173
 
 
174
    [Test]
 
175
    public void First()
 
176
    {
 
177
      ExceptionAssert.Throws<InvalidOperationException>("Cannot access child value on Newtonsoft.Json.Linq.JValue.",
 
178
        () =>
 
179
          {
 
180
            JValue v = new JValue(true);
 
181
            JToken first = v.First;
 
182
          });
 
183
    }
 
184
 
 
185
    [Test]
 
186
    public void Item()
 
187
    {
 
188
      ExceptionAssert.Throws<InvalidOperationException>("Cannot access child value on Newtonsoft.Json.Linq.JValue.",
 
189
        () =>
 
190
          {
 
191
            JValue v = new JValue(true);
 
192
            JToken first = v[0];
 
193
          });
 
194
    }
 
195
 
 
196
    [Test]
 
197
    public void Values()
 
198
    {
 
199
      ExceptionAssert.Throws<InvalidOperationException>("Cannot access child value on Newtonsoft.Json.Linq.JValue.",
 
200
        () =>
 
201
          {
 
202
            JValue v = new JValue(true);
 
203
            v.Values<int>();
 
204
          });
 
205
    }
 
206
 
 
207
    [Test]
 
208
    public void RemoveParentNull()
 
209
    {
 
210
      ExceptionAssert.Throws<InvalidOperationException>("The parent is missing.",
 
211
        () =>
 
212
          {
 
213
            JValue v = new JValue(true);
 
214
            v.Remove();
 
215
          });
 
216
    }
 
217
 
 
218
    [Test]
 
219
    public void Root()
 
220
    {
 
221
      JValue v = new JValue(true);
 
222
      Assert.AreEqual(v, v.Root);
 
223
    }
 
224
 
 
225
    [Test]
 
226
    public void Previous()
 
227
    {
 
228
      JValue v = new JValue(true);
 
229
      Assert.IsNull(v.Previous);
 
230
    }
 
231
 
 
232
    [Test]
 
233
    public void Next()
 
234
    {
 
235
      JValue v = new JValue(true);
 
236
      Assert.IsNull(v.Next);
 
237
    }
 
238
 
 
239
    [Test]
 
240
    public void DeepEquals()
 
241
    {
 
242
      Assert.IsTrue(JToken.DeepEquals(new JValue(5L), new JValue(5)));
 
243
      Assert.IsFalse(JToken.DeepEquals(new JValue(5M), new JValue(5)));
 
244
      Assert.IsTrue(JToken.DeepEquals(new JValue((ulong) long.MaxValue), new JValue(long.MaxValue)));
 
245
    }
 
246
 
 
247
    [Test]
 
248
    public void HasValues()
 
249
    {
 
250
      Assert.IsFalse((new JValue(5L)).HasValues);
 
251
    }
 
252
 
 
253
    [Test]
 
254
    public void SetValue()
 
255
    {
 
256
      ExceptionAssert.Throws<InvalidOperationException>("Cannot set child value on Newtonsoft.Json.Linq.JValue.",
 
257
        () =>
 
258
          {
 
259
            JToken t = new JValue(5L);
 
260
            t[0] = new JValue(3);
 
261
          });
 
262
    }
 
263
 
 
264
    [Test]
 
265
    public void CastNullValueToNonNullable()
 
266
    {
 
267
      ExceptionAssert.Throws<ArgumentException>("Can not convert Null to Int32.",
 
268
        () =>
 
269
          {
 
270
            JValue v = new JValue((object) null);
 
271
            int i = (int) v;
 
272
          });
 
273
    }
 
274
 
 
275
    [Test]
 
276
    public void ConvertValueToCompatibleType()
 
277
    {
 
278
      IComparable c = (new JValue(1).Value<IComparable>());
 
279
      Assert.AreEqual(1L, c);
 
280
    }
 
281
 
 
282
    [Test]
 
283
    public void ConvertValueToFormattableType()
 
284
    {
 
285
      IFormattable f = (new JValue(1).Value<IFormattable>());
 
286
      Assert.AreEqual(1L, f);
 
287
 
 
288
      Assert.AreEqual("01", f.ToString("00", CultureInfo.InvariantCulture));
 
289
    }
 
290
 
 
291
    [Test]
 
292
    public void Ordering()
 
293
    {
 
294
      JObject o = new JObject(
 
295
        new JProperty("Integer", new JValue(1)),
 
296
        new JProperty("Float", new JValue(1.2d)),
 
297
        new JProperty("Decimal", new JValue(1.1m))
 
298
        );
 
299
 
 
300
      IList<object> orderedValues = o.Values().Cast<JValue>().OrderBy(v => v).Select(v => v.Value).ToList();
 
301
 
 
302
      Assert.AreEqual(1L, orderedValues[0]);
 
303
      Assert.AreEqual(1.1m, orderedValues[1]);
 
304
      Assert.AreEqual(1.2d, orderedValues[2]);
 
305
    }
 
306
 
 
307
    [Test]
 
308
    public void WriteSingle()
 
309
    {
 
310
      float f = 5.2f;
 
311
      JValue value = new JValue(f);
 
312
 
 
313
      string json = value.ToString(Formatting.None);
 
314
 
 
315
      Assert.AreEqual("5.2", json);
 
316
    }
 
317
 
 
318
    public class Rate
 
319
    {
 
320
      public decimal Compoundings { get; set; }
 
321
    }
 
322
 
 
323
    private readonly Rate rate = new Rate {Compoundings = 12.166666666666666666666666667m};
 
324
 
 
325
    [Test]
 
326
    public void WriteFullDecimalPrecision()
 
327
    {
 
328
      var jTokenWriter = new JTokenWriter();
 
329
      new JsonSerializer().Serialize(jTokenWriter, rate);
 
330
      string json = jTokenWriter.Token.ToString();
 
331
      Assert.AreEqual(@"{
 
332
  ""Compoundings"": 12.166666666666666666666666667
 
333
}", json);
 
334
    }
 
335
 
 
336
    [Test]
 
337
    public void RoundTripDecimal()
 
338
    {
 
339
      var jTokenWriter = new JTokenWriter();
 
340
      new JsonSerializer().Serialize(jTokenWriter, rate);
 
341
      var rate2 = new JsonSerializer().Deserialize<Rate>(new JTokenReader(jTokenWriter.Token));
 
342
 
 
343
      Assert.AreEqual(rate.Compoundings, rate2.Compoundings);
 
344
    }
 
345
 
 
346
#if !NET20
 
347
    public class ObjectWithDateTimeOffset
 
348
    {
 
349
      public DateTimeOffset DateTimeOffset { get; set; }
 
350
    }
 
351
 
 
352
    [Test]
 
353
    public void SetDateTimeOffsetProperty()
 
354
    {
 
355
      var dateTimeOffset = new DateTimeOffset(new DateTime(2000, 1, 1), TimeSpan.FromHours(3));
 
356
      var json = JsonConvert.SerializeObject(
 
357
        new ObjectWithDateTimeOffset
 
358
          {
 
359
            DateTimeOffset = dateTimeOffset
 
360
          });
 
361
 
 
362
      var o = JObject.Parse(json);
 
363
      o.Property("DateTimeOffset").Value = dateTimeOffset;
 
364
    }
 
365
#endif
 
366
  }
 
367
}
 
 
b'\\ No newline at end of file'