2
// Copyright (c) 2007 James Newton-King
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
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
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.
27
using System.Collections.Generic;
29
using NUnit.Framework;
31
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
32
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
33
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
35
using Newtonsoft.Json.Linq;
37
using Newtonsoft.Json.Utilities.LinqBridge;
42
namespace Newtonsoft.Json.Tests.Linq
45
public class JPathTests : TestFixtureBase
48
public void SingleProperty()
50
JPath path = new JPath("Blah");
51
Assert.AreEqual(1, path.Parts.Count);
52
Assert.AreEqual("Blah", path.Parts[0]);
56
public void TwoProperties()
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]);
65
public void SinglePropertyAndIndexer()
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]);
74
public void MultiplePropertiesAndIndexers()
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]);
87
public void BadCharactersInIndexer()
89
ExceptionAssert.Throws<JsonException>(
90
@"Unexpected character while parsing path indexer: [",
93
new JPath("Blah[[0]].Two.Three[1].Four");
98
public void UnclosedIndexer()
100
ExceptionAssert.Throws<JsonException>(
101
@"Path ended with open indexer. Expected ]",
109
public void AdditionalDots()
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]);
122
public void IndexerOnly()
124
JPath path = new JPath("[111119990]");
125
Assert.AreEqual(1, path.Parts.Count);
126
Assert.AreEqual(111119990, path.Parts[0]);
130
public void EmptyIndexer()
132
ExceptionAssert.Throws<JsonException>(
133
"Empty path indexer.",
141
public void IndexerCloseInProperty()
143
ExceptionAssert.Throws<JsonException>(
144
"Unexpected character while parsing path: ]",
152
public void AdjacentIndexers()
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]);
163
public void MissingDotAfterIndexer()
165
ExceptionAssert.Throws<JsonException>(
166
"Unexpected character following indexer: B",
169
new JPath("[1]Blah");
174
public void EvaluateSingleProperty()
176
JObject o = new JObject(
177
new JProperty("Blah", 1));
179
JToken t = o.SelectToken("Blah");
181
Assert.AreEqual(JTokenType.Integer, t.Type);
182
Assert.AreEqual(1, (int)t);
186
public void EvaluateMissingProperty()
188
JObject o = new JObject(
189
new JProperty("Blah", 1));
191
JToken t = o.SelectToken("Missing[1]");
196
public void EvaluateIndexerOnObject()
198
JObject o = new JObject(
199
new JProperty("Blah", 1));
201
JToken t = o.SelectToken("[1]");
206
public void EvaluateIndexerOnObjectWithError()
208
JObject o = new JObject(
209
new JProperty("Blah", 1));
211
ExceptionAssert.Throws<JsonException>(
212
@"Index 1 not valid on JObject.",
215
o.SelectToken("[1]", true);
220
public void EvaluatePropertyOnArray()
222
JArray a = new JArray(1, 2, 3, 4, 5);
224
JToken t = a.SelectToken("BlahBlah");
229
public void EvaluatePropertyOnArrayWithError()
231
JArray a = new JArray(1, 2, 3, 4, 5);
233
ExceptionAssert.Throws<JsonException>(
234
@"Property 'BlahBlah' not valid on JArray.",
237
a.SelectToken("BlahBlah", true);
242
public void EvaluateConstructorOutOfBoundsIndxerWithError()
244
JConstructor c = new JConstructor("Blah");
246
ExceptionAssert.Throws<IndexOutOfRangeException>(
247
@"Index 1 outside the bounds of JConstructor.",
250
c.SelectToken("[1]", true);
255
public void EvaluateMissingPropertyWithError()
257
JObject o = new JObject(
258
new JProperty("Blah", 1));
260
ExceptionAssert.Throws<JsonException>(
261
"Property 'Missing' does not exist on JObject.",
264
o.SelectToken("Missing", true);
269
public void EvaluateOutOfBoundsIndxer()
271
JArray a = new JArray(1, 2, 3, 4, 5);
273
JToken t = a.SelectToken("[1000].Ha");
278
public void EvaluateArrayOutOfBoundsIndxerWithError()
280
JArray a = new JArray(1, 2, 3, 4, 5);
282
ExceptionAssert.Throws<IndexOutOfRangeException>(
283
"Index 1000 outside the bounds of JArray.",
286
a.SelectToken("[1000].Ha", true);
291
public void EvaluateArray()
293
JArray a = new JArray(1, 2, 3, 4);
295
JToken t = a.SelectToken("[1]");
297
Assert.AreEqual(JTokenType.Integer, t.Type);
298
Assert.AreEqual(2, (int)t);
302
public void EvaluateSinglePropertyReturningArray()
304
JObject o = new JObject(
305
new JProperty("Blah", new [] { 1, 2, 3 }));
307
JToken t = o.SelectToken("Blah");
309
Assert.AreEqual(JTokenType.Array, t.Type);
311
t = o.SelectToken("Blah[2]");
312
Assert.AreEqual(JTokenType.Integer, t.Type);
313
Assert.AreEqual(3, (int)t);
317
public void EvaluateLastSingleCharacterProperty()
319
JObject o2 = JObject.Parse("{'People':[{'N':'Jeff'}]}");
320
string a2 = (string)o2.SelectToken("People[0].N");
322
Assert.AreEqual("Jeff", a2);
326
public void PathWithConstructor()
328
JArray a = JArray.Parse(@"[
340
""Property2"": new Constructor1(
349
JValue v = (JValue)a.SelectToken("[1].Property2[1][0]");
350
Assert.AreEqual(1L, v.Value);
355
public void Example()
357
JObject o = JObject.Parse(@"{
364
""Name"": ""Acme Co"",
373
""Name"": ""Contoso"",
376
""Name"": ""Elbow Grease"",
380
""Name"": ""Headlight Fluid"",
388
string name = (string)o.SelectToken("Manufacturers[0].Name");
391
decimal productPrice = (decimal)o.SelectToken("Manufacturers[0].Products[0].Price");
394
string productName = (string)o.SelectToken("Manufacturers[1].Products[0].Name");
397
Assert.AreEqual("Acme Co", name);
398
Assert.AreEqual(50m, productPrice);
399
Assert.AreEqual("Elbow Grease", productName);
401
IList<string> storeNames = o.SelectToken("Stores").Select(s => (string)s).ToList();
405
IList<string> firstProductNames = o["Manufacturers"].Select(m => (string)m.SelectToken("Products[1].Name")).ToList();
409
decimal totalPrice = o["Manufacturers"].Sum(m => (decimal)m.SelectToken("Products[0].Price"));
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);
b'\\ No newline at end of file'