~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/JPathTests.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
#if !NETFX_CORE
 
29
using NUnit.Framework;
 
30
#else
 
31
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
 
32
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
 
33
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
 
34
#endif
 
35
using Newtonsoft.Json.Linq;
 
36
#if NET20
 
37
using Newtonsoft.Json.Utilities.LinqBridge;
 
38
#else
 
39
using System.Linq;
 
40
#endif
 
41
 
 
42
namespace Newtonsoft.Json.Tests.Linq
 
43
{
 
44
  [TestFixture]
 
45
  public class JPathTests : TestFixtureBase
 
46
  {
 
47
    [Test]
 
48
    public void SingleProperty()
 
49
    {
 
50
      JPath path = new JPath("Blah");
 
51
      Assert.AreEqual(1, path.Parts.Count);
 
52
      Assert.AreEqual("Blah", path.Parts[0]);
 
53
    }
 
54
 
 
55
    [Test]
 
56
    public void TwoProperties()
 
57
    {
 
58
      JPath path = new JPath("Blah.Two");
 
59
      Assert.AreEqual(2, path.Parts.Count);
 
60
      Assert.AreEqual("Blah", path.Parts[0]);
 
61
      Assert.AreEqual("Two", path.Parts[1]);
 
62
    }
 
63
 
 
64
    [Test]
 
65
    public void SinglePropertyAndIndexer()
 
66
    {
 
67
      JPath path = new JPath("Blah[0]");
 
68
      Assert.AreEqual(2, path.Parts.Count);
 
69
      Assert.AreEqual("Blah", path.Parts[0]);
 
70
      Assert.AreEqual(0, path.Parts[1]);
 
71
    }
 
72
 
 
73
    [Test]
 
74
    public void MultiplePropertiesAndIndexers()
 
75
    {
 
76
      JPath path = new JPath("Blah[0].Two.Three[1].Four");
 
77
      Assert.AreEqual(6, path.Parts.Count);
 
78
      Assert.AreEqual("Blah", path.Parts[0]);
 
79
      Assert.AreEqual(0, path.Parts[1]);
 
80
      Assert.AreEqual("Two", path.Parts[2]);
 
81
      Assert.AreEqual("Three", path.Parts[3]);
 
82
      Assert.AreEqual(1, path.Parts[4]);
 
83
      Assert.AreEqual("Four", path.Parts[5]);
 
84
    }
 
85
 
 
86
    [Test]
 
87
    public void BadCharactersInIndexer()
 
88
    {
 
89
      ExceptionAssert.Throws<JsonException>(
 
90
        @"Unexpected character while parsing path indexer: [",
 
91
        () =>
 
92
        {
 
93
          new JPath("Blah[[0]].Two.Three[1].Four");
 
94
        });
 
95
    }
 
96
 
 
97
    [Test]
 
98
    public void UnclosedIndexer()
 
99
    {
 
100
      ExceptionAssert.Throws<JsonException>(
 
101
        @"Path ended with open indexer. Expected ]",
 
102
        () =>
 
103
        {
 
104
          new JPath("Blah[0");
 
105
        });
 
106
    }
 
107
 
 
108
    [Test]
 
109
    public void AdditionalDots()
 
110
    {
 
111
      JPath path = new JPath(".Blah..[0]..Two.Three....[1].Four.");
 
112
      Assert.AreEqual(6, path.Parts.Count);
 
113
      Assert.AreEqual("Blah", path.Parts[0]);
 
114
      Assert.AreEqual(0, path.Parts[1]);
 
115
      Assert.AreEqual("Two", path.Parts[2]);
 
116
      Assert.AreEqual("Three", path.Parts[3]);
 
117
      Assert.AreEqual(1, path.Parts[4]);
 
118
      Assert.AreEqual("Four", path.Parts[5]);
 
119
    }
 
120
 
 
121
    [Test]
 
122
    public void IndexerOnly()
 
123
    {
 
124
      JPath path = new JPath("[111119990]");
 
125
      Assert.AreEqual(1, path.Parts.Count);
 
126
      Assert.AreEqual(111119990, path.Parts[0]);
 
127
    }
 
128
 
 
129
    [Test]
 
130
    public void EmptyIndexer()
 
131
    {
 
132
      ExceptionAssert.Throws<JsonException>(
 
133
        "Empty path indexer.",
 
134
        () =>
 
135
        {
 
136
          new JPath("[]");
 
137
        });
 
138
    }
 
139
 
 
140
    [Test]
 
141
    public void IndexerCloseInProperty()
 
142
    {
 
143
      ExceptionAssert.Throws<JsonException>(
 
144
        "Unexpected character while parsing path: ]",
 
145
        () =>
 
146
        {
 
147
          new JPath("]");
 
148
        });
 
149
    }
 
150
 
 
151
    [Test]
 
152
    public void AdjacentIndexers()
 
153
    {
 
154
      JPath path = new JPath("[1][0][0][" + int.MaxValue + "]");
 
155
      Assert.AreEqual(4, path.Parts.Count);
 
156
      Assert.AreEqual(1, path.Parts[0]);
 
157
      Assert.AreEqual(0, path.Parts[1]);
 
158
      Assert.AreEqual(0, path.Parts[2]);
 
159
      Assert.AreEqual(int.MaxValue, path.Parts[3]);
 
160
    }
 
161
 
 
162
    [Test]
 
163
    public void MissingDotAfterIndexer()
 
164
    {
 
165
      ExceptionAssert.Throws<JsonException>(
 
166
        "Unexpected character following indexer: B",
 
167
        () =>
 
168
        {
 
169
          new JPath("[1]Blah");
 
170
        });
 
171
    }
 
172
 
 
173
    [Test]
 
174
    public void EvaluateSingleProperty()
 
175
    {
 
176
      JObject o = new JObject(
 
177
        new JProperty("Blah", 1));
 
178
 
 
179
      JToken t = o.SelectToken("Blah");
 
180
      Assert.IsNotNull(t);
 
181
      Assert.AreEqual(JTokenType.Integer, t.Type);
 
182
      Assert.AreEqual(1, (int)t);
 
183
    }
 
184
 
 
185
    [Test]
 
186
    public void EvaluateMissingProperty()
 
187
    {
 
188
      JObject o = new JObject(
 
189
        new JProperty("Blah", 1));
 
190
 
 
191
      JToken t = o.SelectToken("Missing[1]");
 
192
      Assert.IsNull(t);
 
193
    }
 
194
 
 
195
    [Test]
 
196
    public void EvaluateIndexerOnObject()
 
197
    {
 
198
      JObject o = new JObject(
 
199
        new JProperty("Blah", 1));
 
200
 
 
201
      JToken t = o.SelectToken("[1]");
 
202
      Assert.IsNull(t);
 
203
    }
 
204
 
 
205
    [Test]
 
206
    public void EvaluateIndexerOnObjectWithError()
 
207
    {
 
208
      JObject o = new JObject(
 
209
        new JProperty("Blah", 1));
 
210
 
 
211
      ExceptionAssert.Throws<JsonException>(
 
212
        @"Index 1 not valid on JObject.",
 
213
        () =>
 
214
        {
 
215
          o.SelectToken("[1]", true);
 
216
        });
 
217
    }
 
218
 
 
219
    [Test]
 
220
    public void EvaluatePropertyOnArray()
 
221
    {
 
222
      JArray a = new JArray(1, 2, 3, 4, 5);
 
223
 
 
224
      JToken t = a.SelectToken("BlahBlah");
 
225
      Assert.IsNull(t);
 
226
    }
 
227
 
 
228
    [Test]
 
229
    public void EvaluatePropertyOnArrayWithError()
 
230
    {
 
231
      JArray a = new JArray(1, 2, 3, 4, 5);
 
232
 
 
233
      ExceptionAssert.Throws<JsonException>(
 
234
        @"Property 'BlahBlah' not valid on JArray.",
 
235
        () =>
 
236
        {
 
237
          a.SelectToken("BlahBlah", true);
 
238
        });
 
239
    }
 
240
 
 
241
    [Test]
 
242
    public void EvaluateConstructorOutOfBoundsIndxerWithError()
 
243
    {
 
244
      JConstructor c = new JConstructor("Blah");
 
245
 
 
246
      ExceptionAssert.Throws<IndexOutOfRangeException>(
 
247
        @"Index 1 outside the bounds of JConstructor.",
 
248
        () =>
 
249
        {
 
250
          c.SelectToken("[1]", true);
 
251
        });
 
252
    }
 
253
 
 
254
    [Test]
 
255
    public void EvaluateMissingPropertyWithError()
 
256
    {
 
257
      JObject o = new JObject(
 
258
        new JProperty("Blah", 1));
 
259
 
 
260
      ExceptionAssert.Throws<JsonException>(
 
261
        "Property 'Missing' does not exist on JObject.",
 
262
        () =>
 
263
        {
 
264
          o.SelectToken("Missing", true);
 
265
        });
 
266
    }
 
267
 
 
268
    [Test]
 
269
    public void EvaluateOutOfBoundsIndxer()
 
270
    {
 
271
      JArray a = new JArray(1, 2, 3, 4, 5);
 
272
 
 
273
      JToken t = a.SelectToken("[1000].Ha");
 
274
      Assert.IsNull(t);
 
275
    }
 
276
 
 
277
    [Test]
 
278
    public void EvaluateArrayOutOfBoundsIndxerWithError()
 
279
    {
 
280
      JArray a = new JArray(1, 2, 3, 4, 5);
 
281
 
 
282
      ExceptionAssert.Throws<IndexOutOfRangeException>(
 
283
        "Index 1000 outside the bounds of JArray.",
 
284
        () =>
 
285
        {
 
286
          a.SelectToken("[1000].Ha", true);
 
287
        });
 
288
    }
 
289
 
 
290
    [Test]
 
291
    public void EvaluateArray()
 
292
    {
 
293
      JArray a = new JArray(1, 2, 3, 4);
 
294
 
 
295
      JToken t = a.SelectToken("[1]");
 
296
      Assert.IsNotNull(t);
 
297
      Assert.AreEqual(JTokenType.Integer, t.Type);
 
298
      Assert.AreEqual(2, (int)t);
 
299
    }
 
300
 
 
301
    [Test]
 
302
    public void EvaluateSinglePropertyReturningArray()
 
303
    {
 
304
      JObject o = new JObject(
 
305
        new JProperty("Blah", new [] { 1, 2, 3 }));
 
306
 
 
307
      JToken t = o.SelectToken("Blah");
 
308
      Assert.IsNotNull(t);
 
309
      Assert.AreEqual(JTokenType.Array, t.Type);
 
310
 
 
311
      t = o.SelectToken("Blah[2]");
 
312
      Assert.AreEqual(JTokenType.Integer, t.Type);
 
313
      Assert.AreEqual(3, (int)t);
 
314
    }
 
315
 
 
316
    [Test]
 
317
    public void EvaluateLastSingleCharacterProperty()
 
318
    {
 
319
      JObject o2 = JObject.Parse("{'People':[{'N':'Jeff'}]}");
 
320
      string a2 = (string)o2.SelectToken("People[0].N");
 
321
 
 
322
      Assert.AreEqual("Jeff", a2);
 
323
    }
 
324
 
 
325
    [Test]
 
326
    public void PathWithConstructor()
 
327
    {
 
328
      JArray a = JArray.Parse(@"[
 
329
  {
 
330
    ""Property1"": [
 
331
      1,
 
332
      [
 
333
        [
 
334
          []
 
335
        ]
 
336
      ]
 
337
    ]
 
338
  },
 
339
  {
 
340
    ""Property2"": new Constructor1(
 
341
      null,
 
342
      [
 
343
        1
 
344
      ]
 
345
    )
 
346
  }
 
347
]");
 
348
 
 
349
      JValue v = (JValue)a.SelectToken("[1].Property2[1][0]");
 
350
      Assert.AreEqual(1L, v.Value);
 
351
    }
 
352
 
 
353
 
 
354
    [Test]
 
355
    public void Example()
 
356
    {
 
357
      JObject o = JObject.Parse(@"{
 
358
        ""Stores"": [
 
359
          ""Lambton Quay"",
 
360
          ""Willis Street""
 
361
        ],
 
362
        ""Manufacturers"": [
 
363
          {
 
364
            ""Name"": ""Acme Co"",
 
365
            ""Products"": [
 
366
              {
 
367
                ""Name"": ""Anvil"",
 
368
                ""Price"": 50
 
369
              }
 
370
            ]
 
371
          },
 
372
          {
 
373
            ""Name"": ""Contoso"",
 
374
            ""Products"": [
 
375
              {
 
376
                ""Name"": ""Elbow Grease"",
 
377
                ""Price"": 99.95
 
378
              },
 
379
              {
 
380
                ""Name"": ""Headlight Fluid"",
 
381
                ""Price"": 4
 
382
              }
 
383
            ]
 
384
          }
 
385
        ]
 
386
      }");
 
387
 
 
388
      string name = (string)o.SelectToken("Manufacturers[0].Name");
 
389
      // Acme Co
 
390
 
 
391
      decimal productPrice = (decimal)o.SelectToken("Manufacturers[0].Products[0].Price");
 
392
      // 50
 
393
 
 
394
      string productName = (string)o.SelectToken("Manufacturers[1].Products[0].Name");
 
395
      // Elbow Grease
 
396
 
 
397
      Assert.AreEqual("Acme Co", name);
 
398
      Assert.AreEqual(50m, productPrice);
 
399
      Assert.AreEqual("Elbow Grease", productName);
 
400
 
 
401
      IList<string> storeNames = o.SelectToken("Stores").Select(s => (string)s).ToList();
 
402
      // Lambton Quay
 
403
      // Willis Street
 
404
 
 
405
      IList<string> firstProductNames = o["Manufacturers"].Select(m => (string)m.SelectToken("Products[1].Name")).ToList();
 
406
      // null
 
407
      // Headlight Fluid
 
408
 
 
409
      decimal totalPrice = o["Manufacturers"].Sum(m => (decimal)m.SelectToken("Products[0].Price"));
 
410
      // 149.95
 
411
 
 
412
      Assert.AreEqual(2, storeNames.Count);
 
413
      Assert.AreEqual("Lambton Quay", storeNames[0]);
 
414
      Assert.AreEqual("Willis Street", storeNames[1]);
 
415
      Assert.AreEqual(2, firstProductNames.Count);
 
416
      Assert.AreEqual(null, firstProductNames[0]);
 
417
      Assert.AreEqual("Headlight Fluid", firstProductNames[1]);
 
418
      Assert.AreEqual(149.95m, totalPrice);
 
419
    }
 
420
  }
 
421
}
 
 
b'\\ No newline at end of file'