Code Coverage Statistics for Source File
Newtonsoft.Json.Tests\Linq\JPathTests.cs
Symbol Coverage: 92.00% (115 of 125)
Branch Coverage: 65.52% (19 of 29)
Cyclomatic Complexity Avg: 1.10 Max:4
Code Lines: 157
Symbol Coverage Trend
View:
L | V | Source |
---|---|---|
1 |
using System; |
|
2 |
using System.Collections.Generic; |
|
3 |
using System.ComponentModel; |
|
4 |
using System.Linq; |
|
5 |
using System.Text; |
|
6 |
using Newtonsoft.Json.Tests.TestObjects; |
|
7 |
using NUnit.Framework; |
|
8 |
using Newtonsoft.Json.Linq; |
|
9 |
using Newtonsoft.Json.Converters; |
|
10 |
using System.IO; |
|
11 |
using System.Collections; |
|
12 |
||
13 |
namespace Newtonsoft.Json.Tests.Linq |
|
14 |
{ |
|
15 |
public class JPathTests : TestFixtureBase |
|
16 |
{ |
|
17 |
[Test] |
|
18 |
public void SingleProperty() |
|
19 |
{ |
|
20 |
1 |
JPath path = new JPath("Blah");
|
21 |
1 |
Assert.AreEqual(1, path.Parts.Count);
|
22 |
1 |
Assert.AreEqual("Blah", path.Parts[0]);
|
23 |
1 |
}
|
24 |
||
25 |
[Test] |
|
26 |
public void TwoProperties() |
|
27 |
{ |
|
28 |
1 |
JPath path = new JPath("Blah.Two");
|
29 |
1 |
Assert.AreEqual(2, path.Parts.Count);
|
30 |
1 |
Assert.AreEqual("Blah", path.Parts[0]);
|
31 |
1 |
Assert.AreEqual("Two", path.Parts[1]);
|
32 |
1 |
}
|
33 |
||
34 |
[Test] |
|
35 |
public void SinglePropertyAndIndexer() |
|
36 |
{ |
|
37 |
1 |
JPath path = new JPath("Blah[0]");
|
38 |
1 |
Assert.AreEqual(2, path.Parts.Count);
|
39 |
1 |
Assert.AreEqual("Blah", path.Parts[0]);
|
40 |
1 |
Assert.AreEqual(0, path.Parts[1]);
|
41 |
1 |
}
|
42 |
||
43 |
[Test] |
|
44 |
public void MultiplePropertiesAndIndexers() |
|
45 |
{ |
|
46 |
1 |
JPath path = new JPath("Blah[0].Two.Three[1].Four");
|
47 |
1 |
Assert.AreEqual(6, path.Parts.Count);
|
48 |
1 |
Assert.AreEqual("Blah", path.Parts[0]);
|
49 |
1 |
Assert.AreEqual(0, path.Parts[1]);
|
50 |
1 |
Assert.AreEqual("Two", path.Parts[2]);
|
51 |
1 |
Assert.AreEqual("Three", path.Parts[3]);
|
52 |
1 |
Assert.AreEqual(1, path.Parts[4]);
|
53 |
1 |
Assert.AreEqual("Four", path.Parts[5]);
|
54 |
1 |
}
|
55 |
||
56 |
[Test] |
|
57 |
[ExpectedException(typeof(Exception), ExpectedMessage = @"Unexpected character while parsing path indexer: [")] |
|
58 |
public void BadCharactersInIndexer() |
|
59 |
{ |
|
60 |
1 |
new JPath("Blah[[0]].Two.Three[1].Four");
|
61 |
0 |
}
|
62 |
||
63 |
[Test] |
|
64 |
[ExpectedException(typeof(Exception), ExpectedMessage = @"Path ended with open indexer. Expected ]")] |
|
65 |
public void UnclosedIndexer() |
|
66 |
{ |
|
67 |
1 |
new JPath("Blah[0");
|
68 |
0 |
}
|
69 |
||
70 |
[Test] |
|
71 |
public void AdditionalDots() |
|
72 |
{ |
|
73 |
1 |
JPath path = new JPath(".Blah..[0]..Two.Three....[1].Four.");
|
74 |
1 |
Assert.AreEqual(6, path.Parts.Count);
|
75 |
1 |
Assert.AreEqual("Blah", path.Parts[0]);
|
76 |
1 |
Assert.AreEqual(0, path.Parts[1]);
|
77 |
1 |
Assert.AreEqual("Two", path.Parts[2]);
|
78 |
1 |
Assert.AreEqual("Three", path.Parts[3]);
|
79 |
1 |
Assert.AreEqual(1, path.Parts[4]);
|
80 |
1 |
Assert.AreEqual("Four", path.Parts[5]);
|
81 |
1 |
}
|
82 |
||
83 |
[Test] |
|
84 |
public void IndexerOnly() |
|
85 |
{ |
|
86 |
1 |
JPath path = new JPath("[111119990]");
|
87 |
1 |
Assert.AreEqual(1, path.Parts.Count);
|
88 |
1 |
Assert.AreEqual(111119990, path.Parts[0]);
|
89 |
1 |
}
|
90 |
||
91 |
[Test] |
|
92 |
[ExpectedException(typeof(Exception), ExpectedMessage = "Empty path indexer.")] |
|
93 |
public void EmptyIndexer() |
|
94 |
{ |
|
95 |
1 |
new JPath("[]");
|
96 |
0 |
}
|
97 |
||
98 |
[Test] |
|
99 |
[ExpectedException(typeof(Exception), ExpectedMessage = "Unexpected character while parsing path: ]")] |
|
100 |
public void IndexerCloseInProperty() |
|
101 |
{ |
|
102 |
1 |
new JPath("]");
|
103 |
0 |
}
|
104 |
||
105 |
[Test] |
|
106 |
public void AdjacentIndexers() |
|
107 |
{ |
|
108 |
1 |
JPath path = new JPath("[1][0][0][" + int.MaxValue + "]");
|
109 |
1 |
Assert.AreEqual(4, path.Parts.Count);
|
110 |
1 |
Assert.AreEqual(1, path.Parts[0]);
|
111 |
1 |
Assert.AreEqual(0, path.Parts[1]);
|
112 |
1 |
Assert.AreEqual(0, path.Parts[2]);
|
113 |
1 |
Assert.AreEqual(int.MaxValue, path.Parts[3]);
|
114 |
1 |
}
|
115 |
||
116 |
[Test] |
|
117 |
[ExpectedException(typeof(Exception), ExpectedMessage = "Unexpected character following indexer: B")] |
|
118 |
public void MissingDotAfterIndexer() |
|
119 |
{ |
|
120 |
1 |
new JPath("[1]Blah");
|
121 |
0 |
}
|
122 |
||
123 |
[Test] |
|
124 |
public void EvaluateSingleProperty() |
|
125 |
{ |
|
126 |
1 |
JObject o = new JObject(
|
127 |
1 |
new JProperty("Blah", 1));
|
128 |
||
129 |
1 |
JToken t = o.SelectToken("Blah");
|
130 |
1 |
Assert.IsNotNull(t);
|
131 |
1 |
Assert.AreEqual(JTokenType.Integer, t.Type);
|
132 |
1 |
Assert.AreEqual(1, (int)t);
|
133 |
1 |
}
|
134 |
||
135 |
[Test] |
|
136 |
public void EvaluateMissingProperty() |
|
137 |
{ |
|
138 |
1 |
JObject o = new JObject(
|
139 |
1 |
new JProperty("Blah", 1));
|
140 |
||
141 |
1 |
JToken t = o.SelectToken("Missing[1]");
|
142 |
1 |
Assert.IsNull(t);
|
143 |
1 |
}
|
144 |
||
145 |
[Test] |
|
146 |
public void EvaluateIndexerOnObject() |
|
147 |
{ |
|
148 |
1 |
JObject o = new JObject(
|
149 |
1 |
new JProperty("Blah", 1));
|
150 |
||
151 |
1 |
JToken t = o.SelectToken("[1]");
|
152 |
1 |
Assert.IsNull(t);
|
153 |
1 |
}
|
154 |
||
155 |
[Test] |
|
156 |
[ExpectedException(typeof(Exception), ExpectedMessage = @"Index 1 not valid on JObject.")] |
|
157 |
public void EvaluateIndexerOnObjectWithError() |
|
158 |
{ |
|
159 |
1 |
JObject o = new JObject(
|
160 |
1 |
new JProperty("Blah", 1));
|
161 |
||
162 |
1 |
o.SelectToken("[1]", true);
|
163 |
0 |
}
|
164 |
||
165 |
[Test] |
|
166 |
public void EvaluatePropertyOnArray() |
|
167 |
{ |
|
168 |
1 |
JArray a = new JArray(1, 2, 3, 4, 5);
|
169 |
||
170 |
1 |
JToken t = a.SelectToken("BlahBlah");
|
171 |
1 |
Assert.IsNull(t);
|
172 |
1 |
}
|
173 |
||
174 |
[Test] |
|
175 |
[ExpectedException(typeof(Exception), ExpectedMessage = @"Property 'BlahBlah' not valid on JArray.")] |
|
176 |
public void EvaluatePropertyOnArrayWithError() |
|
177 |
{ |
|
178 |
1 |
JArray a = new JArray(1, 2, 3, 4, 5);
|
179 |
||
180 |
1 |
a.SelectToken("BlahBlah", true);
|
181 |
0 |
}
|
182 |
||
183 |
[Test] |
|
184 |
[ExpectedException(typeof(Exception), ExpectedMessage = @"Index 1 not valid on JConstructor.")] |
|
185 |
public void EvaluateIndexerOnConstructorWithError() |
|
186 |
{ |
|
187 |
1 |
JConstructor c = new JConstructor("Blah");
|
188 |
||
189 |
1 |
c.SelectToken("[1]", true);
|
190 |
0 |
}
|
191 |
||
192 |
[Test] |
|
193 |
[ExpectedException(typeof(Exception), ExpectedMessage = "Property 'Missing' does not exist on JObject.")] |
|
194 |
public void EvaluateMissingPropertyWithError() |
|
195 |
{ |
|
196 |
1 |
JObject o = new JObject(
|
197 |
1 |
new JProperty("Blah", 1));
|
198 |
||
199 |
1 |
o.SelectToken("Missing", true);
|
200 |
0 |
}
|
201 |
||
202 |
[Test] |
|
203 |
public void EvaluateOutOfBoundsIndxer() |
|
204 |
{ |
|
205 |
1 |
JArray a = new JArray(1, 2, 3, 4, 5);
|
206 |
||
207 |
1 |
JToken t = a.SelectToken("[1000].Ha");
|
208 |
1 |
Assert.IsNull(t);
|
209 |
1 |
}
|
210 |
||
211 |
[Test] |
|
212 |
[ExpectedException(typeof(IndexOutOfRangeException), ExpectedMessage = "Index 1000 outside the bounds of JArray.")] |
|
213 |
public void EvaluateOutOfBoundsIndxerWithError() |
|
214 |
{ |
|
215 |
1 |
JArray a = new JArray(1, 2, 3, 4, 5);
|
216 |
||
217 |
1 |
a.SelectToken("[1000].Ha", true);
|
218 |
0 |
}
|
219 |
||
220 |
[Test] |
|
221 |
public void EvaluateArray() |
|
222 |
{ |
|
223 |
1 |
JArray a = new JArray(1, 2, 3, 4);
|
224 |
||
225 |
1 |
JToken t = a.SelectToken("[1]");
|
226 |
1 |
Assert.IsNotNull(t);
|
227 |
1 |
Assert.AreEqual(JTokenType.Integer, t.Type);
|
228 |
1 |
Assert.AreEqual(2, (int)t);
|
229 |
1 |
}
|
230 |
||
231 |
[Test] |
|
232 |
public void EvaluateSinglePropertyReturningArray() |
|
233 |
{ |
|
234 |
1 |
JObject o = new JObject(
|
235 |
1 |
new JProperty("Blah", new [] { 1, 2, 3 }));
|
236 |
||
237 |
1 |
JToken t = o.SelectToken("Blah");
|
238 |
1 |
Assert.IsNotNull(t);
|
239 |
1 |
Assert.AreEqual(JTokenType.Array, t.Type);
|
240 |
||
241 |
1 |
t = o.SelectToken("Blah[2]");
|
242 |
1 |
Assert.AreEqual(JTokenType.Integer, t.Type);
|
243 |
1 |
Assert.AreEqual(3, (int)t);
|
244 |
1 |
}
|
245 |
||
246 |
[Test] |
|
247 |
public void Example() |
|
248 |
{ |
|
249 |
1 |
JObject o = JObject.Parse(@"{
|
250 |
1 |
""Stores"": [
|
251 |
1 |
""Lambton Quay"",
|
252 |
1 |
""Willis Street""
|
253 |
1 |
],
|
254 |
1 |
""Manufacturers"": [
|
255 |
1 |
{
|
256 |
1 |
""Name"": ""Acme Co"",
|
257 |
1 |
""Products"": [
|
258 |
1 |
{
|
259 |
1 |
""Name"": ""Anvil"",
|
260 |
1 |
""Price"": 50
|
261 |
1 |
}
|
262 |
1 |
]
|
263 |
1 |
},
|
264 |
1 |
{
|
265 |
1 |
""Name"": ""Contoso"",
|
266 |
1 |
""Products"": [
|
267 |
1 |
{
|
268 |
1 |
""Name"": ""Elbow Grease"",
|
269 |
1 |
""Price"": 99.95
|
270 |
1 |
},
|
271 |
1 |
{
|
272 |
1 |
""Name"": ""Headlight Fluid"",
|
273 |
1 |
""Price"": 4
|
274 |
1 |
}
|
275 |
1 |
]
|
276 |
1 |
}
|
277 |
1 |
]
|
278 |
1 |
}");
|
279 |
||
280 |
1 |
string name = (string)o.SelectToken("Manufacturers[0].Name");
|
281 |
// Acme Co |
|
282 |
||
283 |
1 |
decimal productPrice = (decimal)o.SelectToken("Manufacturers[0].Products[0].Price");
|
284 |
// 50 |
|
285 |
||
286 |
1 |
string productName = (string)o.SelectToken("Manufacturers[1].Products[0].Name");
|
287 |
// Elbow Grease |
|
288 |
||
289 |
1 |
Assert.AreEqual("Acme Co", name);
|
290 |
1 |
Assert.AreEqual(50m, productPrice);
|
291 |
1 |
Assert.AreEqual("Elbow Grease", productName);
|
292 |
||
293 |
1 |
IList<string> storeNames = o.SelectToken("Stores").Select(s => (string)s).ToList();
|
294 |
// Lambton Quay |
|
295 |
// Willis Street |
|
296 |
||
297 |
1 |
IList<string> firstProductNames = o["Manufacturers"].Select(m => (string)m.SelectToken("Products[1].Name")).ToList();
|
298 |
// null |
|
299 |
// Headlight Fluid |
|
300 |
||
301 |
1 |
decimal totalPrice = o["Manufacturers"].Sum(m => (decimal)m.SelectToken("Products[0].Price"));
|
302 |
// 149.95 |
|
303 |
||
304 |
1 |
Assert.AreEqual(2, storeNames.Count);
|
305 |
1 |
Assert.AreEqual("Lambton Quay", storeNames[0]);
|
306 |
1 |
Assert.AreEqual("Willis Street", storeNames[1]);
|
307 |
1 |
Assert.AreEqual(2, firstProductNames.Count);
|
308 |
1 |
Assert.AreEqual(null, firstProductNames[0]);
|
309 |
1 |
Assert.AreEqual("Headlight Fluid", firstProductNames[1]);
|
310 |
1 |
Assert.AreEqual(149.95m, totalPrice);
|
311 |
1 |
}
|
312 |
} |
|
313 |
} |