Code Coverage Statistics for Source File
Newtonsoft.Json.Tests\Linq\JArrayTests.cs
Symbol Coverage: 95.10% (194 of 204)
Branch Coverage: 69.70% (23 of 33)
Cyclomatic Complexity Avg: 1.13 Max:3
Code Lines: 235
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 NUnit.Framework; |
|
7 |
using Newtonsoft.Json.Linq; |
|
8 |
||
9 |
namespace Newtonsoft.Json.Tests.Linq |
|
10 |
{ |
|
11 |
public class JArrayTests : TestFixtureBase |
|
12 |
{ |
|
13 |
[Test] |
|
14 |
public void Clear() |
|
15 |
{ |
|
16 |
1 |
JArray a = new JArray { 1 };
|
17 |
1 |
Assert.AreEqual(1, a.Count);
|
18 |
||
19 |
1 |
a.Clear();
|
20 |
1 |
Assert.AreEqual(0, a.Count);
|
21 |
1 |
}
|
22 |
||
23 |
[Test] |
|
24 |
public void Contains() |
|
25 |
{ |
|
26 |
1 |
JValue v = new JValue(1);
|
27 |
||
28 |
1 |
JArray a = new JArray { v };
|
29 |
||
30 |
1 |
Assert.AreEqual(false, a.Contains(new JValue(2)));
|
31 |
1 |
Assert.AreEqual(false, a.Contains(new JValue(1)));
|
32 |
1 |
Assert.AreEqual(false, a.Contains(null));
|
33 |
1 |
Assert.AreEqual(true, a.Contains(v));
|
34 |
1 |
}
|
35 |
||
36 |
[Test] |
|
37 |
public void GenericCollectionCopyTo() |
|
38 |
{ |
|
39 |
1 |
JArray j = new JArray();
|
40 |
1 |
j.Add(new JValue(1));
|
41 |
1 |
j.Add(new JValue(2));
|
42 |
1 |
j.Add(new JValue(3));
|
43 |
1 |
Assert.AreEqual(3, j.Count);
|
44 |
||
45 |
1 |
JToken[] a = new JToken[5];
|
46 |
||
47 |
1 |
((ICollection<JToken>)j).CopyTo(a, 1);
|
48 |
||
49 |
1 |
Assert.AreEqual(null, a[0]);
|
50 |
||
51 |
1 |
Assert.AreEqual(1, (int)a[1]);
|
52 |
||
53 |
1 |
Assert.AreEqual(2, (int)a[2]);
|
54 |
||
55 |
1 |
Assert.AreEqual(3, (int)a[3]);
|
56 |
||
57 |
1 |
Assert.AreEqual(null, a[4]);
|
58 |
||
59 |
1 |
}
|
60 |
||
61 |
[Test] |
|
62 |
[ExpectedException(typeof(ArgumentNullException), ExpectedMessage = @"Value cannot be null. |
|
63 |
Parameter name: array")] |
|
64 |
public void GenericCollectionCopyToNullArrayShouldThrow() |
|
65 |
{ |
|
66 |
1 |
JArray j = new JArray();
|
67 |
1 |
((ICollection<JToken>)j).CopyTo(null, 0);
|
68 |
0 |
}
|
69 |
||
70 |
[Test] |
|
71 |
[ExpectedException(typeof(ArgumentOutOfRangeException), ExpectedMessage = @"arrayIndex is less than 0. |
|
72 |
Parameter name: arrayIndex")] |
|
73 |
public void GenericCollectionCopyToNegativeArrayIndexShouldThrow() |
|
74 |
{ |
|
75 |
1 |
JArray j = new JArray();
|
76 |
1 |
((ICollection<JToken>)j).CopyTo(new JToken[1], -1);
|
77 |
0 |
}
|
78 |
||
79 |
[Test] |
|
80 |
[ExpectedException(typeof(ArgumentException), ExpectedMessage = @"arrayIndex is equal to or greater than the length of array.")] |
|
81 |
public void GenericCollectionCopyToArrayIndexEqualGreaterToArrayLengthShouldThrow() |
|
82 |
{ |
|
83 |
1 |
JArray j = new JArray();
|
84 |
1 |
((ICollection<JToken>)j).CopyTo(new JToken[1], 1);
|
85 |
0 |
}
|
86 |
||
87 |
[Test] |
|
88 |
[ExpectedException(typeof(ArgumentException), ExpectedMessage = @"The number of elements in the source JObject is greater than the available space from arrayIndex to the end of the destination array.")] |
|
89 |
public void GenericCollectionCopyToInsufficientArrayCapacity() |
|
90 |
{ |
|
91 |
1 |
JArray j = new JArray();
|
92 |
1 |
j.Add(new JValue(1));
|
93 |
1 |
j.Add(new JValue(2));
|
94 |
1 |
j.Add(new JValue(3));
|
95 |
||
96 |
1 |
((ICollection<JToken>)j).CopyTo(new JToken[3], 1);
|
97 |
0 |
}
|
98 |
||
99 |
[Test] |
|
100 |
public void Remove() |
|
101 |
{ |
|
102 |
1 |
JValue v = new JValue(1);
|
103 |
1 |
JArray j = new JArray();
|
104 |
1 |
j.Add(v);
|
105 |
||
106 |
1 |
Assert.AreEqual(1, j.Count);
|
107 |
||
108 |
1 |
Assert.AreEqual(false, j.Remove(new JValue(1)));
|
109 |
1 |
Assert.AreEqual(false, j.Remove(null));
|
110 |
1 |
Assert.AreEqual(true, j.Remove(v));
|
111 |
1 |
Assert.AreEqual(false, j.Remove(v));
|
112 |
||
113 |
1 |
Assert.AreEqual(0, j.Count);
|
114 |
1 |
}
|
115 |
||
116 |
[Test] |
|
117 |
public void IndexOf() |
|
118 |
{ |
|
119 |
1 |
JValue v1 = new JValue(1);
|
120 |
1 |
JValue v2 = new JValue(1);
|
121 |
1 |
JValue v3 = new JValue(1);
|
122 |
||
123 |
1 |
JArray j = new JArray();
|
124 |
||
125 |
1 |
j.Add(v1);
|
126 |
1 |
Assert.AreEqual(0, j.IndexOf(v1));
|
127 |
||
128 |
1 |
j.Add(v2);
|
129 |
1 |
Assert.AreEqual(0, j.IndexOf(v1));
|
130 |
1 |
Assert.AreEqual(1, j.IndexOf(v2));
|
131 |
||
132 |
1 |
j.AddFirst(v3);
|
133 |
1 |
Assert.AreEqual(1, j.IndexOf(v1));
|
134 |
1 |
Assert.AreEqual(2, j.IndexOf(v2));
|
135 |
1 |
Assert.AreEqual(0, j.IndexOf(v3));
|
136 |
||
137 |
1 |
v3.Remove();
|
138 |
1 |
Assert.AreEqual(0, j.IndexOf(v1));
|
139 |
1 |
Assert.AreEqual(1, j.IndexOf(v2));
|
140 |
1 |
Assert.AreEqual(-1, j.IndexOf(v3));
|
141 |
1 |
}
|
142 |
||
143 |
[Test] |
|
144 |
public void RemoveAt() |
|
145 |
{ |
|
146 |
1 |
JValue v1 = new JValue(1);
|
147 |
1 |
JValue v2 = new JValue(1);
|
148 |
1 |
JValue v3 = new JValue(1);
|
149 |
||
150 |
1 |
JArray j = new JArray();
|
151 |
||
152 |
1 |
j.Add(v1);
|
153 |
1 |
j.Add(v2);
|
154 |
1 |
j.Add(v3);
|
155 |
||
156 |
1 |
Assert.AreEqual(true, j.Contains(v1));
|
157 |
1 |
j.RemoveAt(0);
|
158 |
1 |
Assert.AreEqual(false, j.Contains(v1));
|
159 |
||
160 |
1 |
Assert.AreEqual(true, j.Contains(v3));
|
161 |
1 |
j.RemoveAt(1);
|
162 |
1 |
Assert.AreEqual(false, j.Contains(v3));
|
163 |
||
164 |
1 |
Assert.AreEqual(1, j.Count);
|
165 |
1 |
}
|
166 |
||
167 |
[Test] |
|
168 |
[ExpectedException(typeof(ArgumentOutOfRangeException), ExpectedMessage = @"index is equal to or greater than Count. |
|
169 |
Parameter name: index")] |
|
170 |
public void RemoveAtOutOfRangeIndexShouldError() |
|
171 |
{ |
|
172 |
1 |
JArray j = new JArray();
|
173 |
1 |
j.RemoveAt(0);
|
174 |
0 |
}
|
175 |
||
176 |
[Test] |
|
177 |
[ExpectedException(typeof(ArgumentOutOfRangeException), ExpectedMessage = @"index is less than 0. |
|
178 |
Parameter name: index")] |
|
179 |
public void RemoveAtNegativeIndexShouldError() |
|
180 |
{ |
|
181 |
1 |
JArray j = new JArray();
|
182 |
1 |
j.RemoveAt(-1);
|
183 |
0 |
}
|
184 |
||
185 |
[Test] |
|
186 |
public void Insert() |
|
187 |
{ |
|
188 |
1 |
JValue v1 = new JValue(1);
|
189 |
1 |
JValue v2 = new JValue(2);
|
190 |
1 |
JValue v3 = new JValue(3);
|
191 |
1 |
JValue v4 = new JValue(4);
|
192 |
||
193 |
1 |
JArray j = new JArray();
|
194 |
||
195 |
1 |
j.Add(v1);
|
196 |
1 |
j.Add(v2);
|
197 |
1 |
j.Add(v3);
|
198 |
1 |
j.Insert(1, v4);
|
199 |
||
200 |
1 |
Assert.AreEqual(0, j.IndexOf(v1));
|
201 |
1 |
Assert.AreEqual(1, j.IndexOf(v4));
|
202 |
1 |
Assert.AreEqual(2, j.IndexOf(v2));
|
203 |
1 |
Assert.AreEqual(3, j.IndexOf(v3));
|
204 |
1 |
}
|
205 |
||
206 |
[Test] |
|
207 |
public void AddFirstAddedTokenShouldBeFirst() |
|
208 |
{ |
|
209 |
1 |
JValue v1 = new JValue(1);
|
210 |
1 |
JValue v2 = new JValue(2);
|
211 |
1 |
JValue v3 = new JValue(3);
|
212 |
||
213 |
1 |
JArray j = new JArray();
|
214 |
1 |
Assert.AreEqual(null, j.First);
|
215 |
1 |
Assert.AreEqual(null, j.Last);
|
216 |
||
217 |
1 |
j.AddFirst(v1);
|
218 |
1 |
Assert.AreEqual(v1, j.First);
|
219 |
1 |
Assert.AreEqual(v1, j.Last);
|
220 |
||
221 |
1 |
j.AddFirst(v2);
|
222 |
1 |
Assert.AreEqual(v2, j.First);
|
223 |
1 |
Assert.AreEqual(v1, j.Last);
|
224 |
||
225 |
1 |
j.AddFirst(v3);
|
226 |
1 |
Assert.AreEqual(v3, j.First);
|
227 |
1 |
Assert.AreEqual(v1, j.Last);
|
228 |
1 |
}
|
229 |
||
230 |
[Test] |
|
231 |
public void InsertShouldInsertAtZeroIndex() |
|
232 |
{ |
|
233 |
1 |
JValue v1 = new JValue(1);
|
234 |
1 |
JValue v2 = new JValue(2);
|
235 |
||
236 |
1 |
JArray j = new JArray();
|
237 |
||
238 |
1 |
j.Insert(0, v1);
|
239 |
1 |
Assert.AreEqual(0, j.IndexOf(v1));
|
240 |
||
241 |
1 |
j.Insert(0, v2);
|
242 |
1 |
Assert.AreEqual(1, j.IndexOf(v1));
|
243 |
1 |
Assert.AreEqual(0, j.IndexOf(v2));
|
244 |
1 |
}
|
245 |
||
246 |
[Test] |
|
247 |
public void InsertNull() |
|
248 |
{ |
|
249 |
1 |
JArray j = new JArray();
|
250 |
1 |
j.Insert(0, null);
|
251 |
||
252 |
1 |
Assert.AreEqual(null, ((JValue)j[0]).Value);
|
253 |
1 |
}
|
254 |
||
255 |
[Test] |
|
256 |
[ExpectedException(typeof(ArgumentOutOfRangeException), ExpectedMessage = @"Specified argument was out of the range of valid values. |
|
257 |
Parameter name: index")] |
|
258 |
public void InsertNegativeIndexShouldThrow() |
|
259 |
{ |
|
260 |
1 |
JArray j = new JArray();
|
261 |
1 |
j.Insert(-1, new JValue(1));
|
262 |
0 |
}
|
263 |
||
264 |
[Test] |
|
265 |
[ExpectedException(typeof(ArgumentOutOfRangeException), ExpectedMessage = @"Specified argument was out of the range of valid values. |
|
266 |
Parameter name: index")] |
|
267 |
public void InsertOutOfRangeIndexShouldThrow() |
|
268 |
{ |
|
269 |
1 |
JArray j = new JArray();
|
270 |
1 |
j.Insert(2, new JValue(1));
|
271 |
0 |
}
|
272 |
||
273 |
[Test] |
|
274 |
public void Item() |
|
275 |
{ |
|
276 |
1 |
JValue v1 = new JValue(1);
|
277 |
1 |
JValue v2 = new JValue(2);
|
278 |
1 |
JValue v3 = new JValue(3);
|
279 |
1 |
JValue v4 = new JValue(4);
|
280 |
||
281 |
1 |
JArray j = new JArray();
|
282 |
||
283 |
1 |
j.Add(v1);
|
284 |
1 |
j.Add(v2);
|
285 |
1 |
j.Add(v3);
|
286 |
||
287 |
1 |
j[1] = v4;
|
288 |
||
289 |
1 |
Assert.AreEqual(null, v2.Parent);
|
290 |
1 |
Assert.AreEqual(-1, j.IndexOf(v2));
|
291 |
1 |
Assert.AreEqual(j, v4.Parent);
|
292 |
1 |
Assert.AreEqual(1, j.IndexOf(v4));
|
293 |
1 |
}
|
294 |
||
295 |
[Test] |
|
296 |
[ExpectedException(typeof(Exception), ExpectedMessage = "Error reading JArray from JsonReader. Current JsonReader item is not an array: StartObject")] |
|
297 |
public void Parse_ShouldThrowOnUnexpectedToken() |
|
298 |
{ |
|
299 |
1 |
string json = @"{""prop"":""value""}";
|
300 |
1 |
JArray.Parse(json);
|
301 |
0 |
}
|
302 |
||
303 |
public class ListItemFields |
|
304 |
{ |
|
305 |
public string ListItemText { get; set; } |
|
306 |
public object ListItemValue { get; set; } |
|
307 |
} |
|
308 |
||
309 |
[Test] |
|
310 |
public void ArrayOrder() |
|
311 |
{ |
|
312 |
1 |
string itemZeroText = "Zero text";
|
313 |
||
314 |
1 |
IEnumerable<ListItemFields> t = new List<ListItemFields>
|
315 |
1 |
{
|
316 |
1 |
new ListItemFields { ListItemText = "First", ListItemValue = 1 },
|
317 |
1 |
new ListItemFields { ListItemText = "Second", ListItemValue = 2 },
|
318 |
1 |
new ListItemFields { ListItemText = "Third", ListItemValue = 3 }
|
319 |
1 |
};
|
320 |
||
321 |
1 |
JObject optionValues =
|
322 |
1 |
new JObject(
|
323 |
1 |
new JProperty("options",
|
324 |
1 |
new JArray(
|
325 |
1 |
new JObject(
|
326 |
1 |
new JProperty("text", itemZeroText),
|
327 |
1 |
new JProperty("value", "0")),
|
328 |
1 |
from r in t
|
329 |
1 |
orderby r.ListItemValue
|
330 |
1 |
select new JObject(
|
331 |
1 |
new JProperty("text", r.ListItemText),
|
332 |
1 |
new JProperty("value", r.ListItemValue.ToString())))));
|
333 |
||
334 |
1 |
string result = "myOptions = " + optionValues.ToString();
|
335 |
||
336 |
1 |
Assert.AreEqual(@"myOptions = {
|
337 |
1 |
""options"": [
|
338 |
1 |
{
|
339 |
1 |
""text"": ""Zero text"",
|
340 |
1 |
""value"": ""0""
|
341 |
1 |
},
|
342 |
1 |
{
|
343 |
1 |
""text"": ""First"",
|
344 |
1 |
""value"": ""1""
|
345 |
1 |
},
|
346 |
1 |
{
|
347 |
1 |
""text"": ""Second"",
|
348 |
1 |
""value"": ""2""
|
349 |
1 |
},
|
350 |
1 |
{
|
351 |
1 |
""text"": ""Third"",
|
352 |
1 |
""value"": ""3""
|
353 |
1 |
}
|
354 |
1 |
]
|
355 |
1 |
}", result);
|
356 |
1 |
}
|
357 |
||
358 |
[Test] |
|
359 |
public void Iterate() |
|
360 |
{ |
|
361 |
1 |
JArray a = new JArray(1, 2, 3, 4, 5);
|
362 |
||
363 |
1 |
int i = 1;
|
364 |
1 |
foreach (JToken token in a)
|
365 |
{ |
|
366 |
5 |
Assert.AreEqual(i, (int)token);
|
367 |
5 |
i++;
|
368 |
} |
|
369 |
1 |
}
|
370 |
||
371 |
|
|
372 |
#if !SILVERLIGHT |
|
373 |
[Test] |
|
374 |
public void ITypedListGetItemProperties() |
|
375 |
{ |
|
376 |
1 |
JProperty p1 = new JProperty("Test1", 1);
|
377 |
1 |
JProperty p2 = new JProperty("Test2", "Two");
|
378 |
1 |
ITypedList a = new JArray(new JObject(p1, p2));
|
379 |
||
380 |
1 |
PropertyDescriptorCollection propertyDescriptors = a.GetItemProperties(null);
|
381 |
1 |
Assert.IsNotNull(propertyDescriptors);
|
382 |
1 |
Assert.AreEqual(2, propertyDescriptors.Count);
|
383 |
1 |
Assert.AreEqual("Test1", propertyDescriptors[0].Name);
|
384 |
1 |
Assert.AreEqual("Test2", propertyDescriptors[1].Name);
|
385 |
1 |
}
|
386 |
#endif |
|
387 |
||
388 |
[Test] |
|
389 |
public void AddArrayToSelf() |
|
390 |
{ |
|
391 |
1 |
JArray a = new JArray(1, 2);
|
392 |
1 |
a.Add(a);
|
393 |
||
394 |
1 |
Assert.AreEqual(3, a.Count);
|
395 |
1 |
Assert.AreEqual(1, (int)a[0]);
|
396 |
1 |
Assert.AreEqual(2, (int)a[1]);
|
397 |
1 |
Assert.AreNotSame(a, a[2]);
|
398 |
1 |
}
|
399 |
||
400 |
[Test] |
|
401 |
[ExpectedException(typeof(ArgumentException), ExpectedMessage = @"Set JArray values with invalid key value: ""badvalue"". Array position index expected.")] |
|
402 |
public void SetValueWithInvalidIndex() |
|
403 |
{ |
|
404 |
1 |
JArray a = new JArray();
|
405 |
1 |
a["badvalue"] = new JValue(3);
|
406 |
0 |
}
|
407 |
||
408 |
[Test] |
|
409 |
public void SetValue() |
|
410 |
{ |
|
411 |
1 |
object key = 0;
|
412 |
||
413 |
1 |
JArray a = new JArray((object)null);
|
414 |
1 |
a[key] = new JValue(3);
|
415 |
||
416 |
1 |
Assert.AreEqual(3, (int)a[key]);
|
417 |
1 |
}
|
418 |
||
419 |
[Test] |
|
420 |
public void ReplaceAll() |
|
421 |
{ |
|
422 |
1 |
JArray a = new JArray(new [] { 1, 2, 3 });
|
423 |
1 |
Assert.AreEqual(3, a.Count);
|
424 |
1 |
Assert.AreEqual(1, (int)a[0]);
|
425 |
1 |
Assert.AreEqual(2, (int)a[1]);
|
426 |
1 |
Assert.AreEqual(3, (int)a[2]);
|
427 |
||
428 |
1 |
a.ReplaceAll(1);
|
429 |
1 |
Assert.AreEqual(1, a.Count);
|
430 |
1 |
Assert.AreEqual(1, (int)a[0]);
|
431 |
1 |
}
|
432 |
} |
|
433 |
} |