PageRenderTime 54ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Newtonsoft.Json.Tests/Linq/JArrayTests.cs

https://github.com/ayoung/Newtonsoft.Json
C# | 440 lines | 360 code | 80 blank | 0 comment | 2 complexity | 434c90e9de7b095976fc74411b056aac MD5 | raw file
  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. namespace Newtonsoft.Json.Tests.Linq
  9. {
  10. public class JArrayTests : TestFixtureBase
  11. {
  12. [Test]
  13. public void Clear()
  14. {
  15. JArray a = new JArray { 1 };
  16. Assert.AreEqual(1, a.Count);
  17. a.Clear();
  18. Assert.AreEqual(0, a.Count);
  19. }
  20. [Test]
  21. public void Contains()
  22. {
  23. JValue v = new JValue(1);
  24. JArray a = new JArray { v };
  25. Assert.AreEqual(false, a.Contains(new JValue(2)));
  26. Assert.AreEqual(false, a.Contains(new JValue(1)));
  27. Assert.AreEqual(false, a.Contains(null));
  28. Assert.AreEqual(true, a.Contains(v));
  29. }
  30. [Test]
  31. public void GenericCollectionCopyTo()
  32. {
  33. JArray j = new JArray();
  34. j.Add(new JValue(1));
  35. j.Add(new JValue(2));
  36. j.Add(new JValue(3));
  37. Assert.AreEqual(3, j.Count);
  38. JToken[] a = new JToken[5];
  39. ((ICollection<JToken>)j).CopyTo(a, 1);
  40. Assert.AreEqual(null, a[0]);
  41. Assert.AreEqual(1, (int)a[1]);
  42. Assert.AreEqual(2, (int)a[2]);
  43. Assert.AreEqual(3, (int)a[3]);
  44. Assert.AreEqual(null, a[4]);
  45. }
  46. [Test]
  47. [ExpectedException(typeof(ArgumentNullException), ExpectedMessage = @"Value cannot be null.
  48. Parameter name: array")]
  49. public void GenericCollectionCopyToNullArrayShouldThrow()
  50. {
  51. JArray j = new JArray();
  52. ((ICollection<JToken>)j).CopyTo(null, 0);
  53. }
  54. [Test]
  55. [ExpectedException(typeof(ArgumentOutOfRangeException), ExpectedMessage = @"arrayIndex is less than 0.
  56. Parameter name: arrayIndex")]
  57. public void GenericCollectionCopyToNegativeArrayIndexShouldThrow()
  58. {
  59. JArray j = new JArray();
  60. ((ICollection<JToken>)j).CopyTo(new JToken[1], -1);
  61. }
  62. [Test]
  63. [ExpectedException(typeof(ArgumentException), ExpectedMessage = @"arrayIndex is equal to or greater than the length of array.")]
  64. public void GenericCollectionCopyToArrayIndexEqualGreaterToArrayLengthShouldThrow()
  65. {
  66. JArray j = new JArray();
  67. ((ICollection<JToken>)j).CopyTo(new JToken[1], 1);
  68. }
  69. [Test]
  70. [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.")]
  71. public void GenericCollectionCopyToInsufficientArrayCapacity()
  72. {
  73. JArray j = new JArray();
  74. j.Add(new JValue(1));
  75. j.Add(new JValue(2));
  76. j.Add(new JValue(3));
  77. ((ICollection<JToken>)j).CopyTo(new JToken[3], 1);
  78. }
  79. [Test]
  80. public void Remove()
  81. {
  82. JValue v = new JValue(1);
  83. JArray j = new JArray();
  84. j.Add(v);
  85. Assert.AreEqual(1, j.Count);
  86. Assert.AreEqual(false, j.Remove(new JValue(1)));
  87. Assert.AreEqual(false, j.Remove(null));
  88. Assert.AreEqual(true, j.Remove(v));
  89. Assert.AreEqual(false, j.Remove(v));
  90. Assert.AreEqual(0, j.Count);
  91. }
  92. [Test]
  93. public void IndexOf()
  94. {
  95. JValue v1 = new JValue(1);
  96. JValue v2 = new JValue(1);
  97. JValue v3 = new JValue(1);
  98. JArray j = new JArray();
  99. j.Add(v1);
  100. Assert.AreEqual(0, j.IndexOf(v1));
  101. j.Add(v2);
  102. Assert.AreEqual(0, j.IndexOf(v1));
  103. Assert.AreEqual(1, j.IndexOf(v2));
  104. j.AddFirst(v3);
  105. Assert.AreEqual(1, j.IndexOf(v1));
  106. Assert.AreEqual(2, j.IndexOf(v2));
  107. Assert.AreEqual(0, j.IndexOf(v3));
  108. v3.Remove();
  109. Assert.AreEqual(0, j.IndexOf(v1));
  110. Assert.AreEqual(1, j.IndexOf(v2));
  111. Assert.AreEqual(-1, j.IndexOf(v3));
  112. }
  113. [Test]
  114. public void RemoveAt()
  115. {
  116. JValue v1 = new JValue(1);
  117. JValue v2 = new JValue(1);
  118. JValue v3 = new JValue(1);
  119. JArray j = new JArray();
  120. j.Add(v1);
  121. j.Add(v2);
  122. j.Add(v3);
  123. Assert.AreEqual(true, j.Contains(v1));
  124. j.RemoveAt(0);
  125. Assert.AreEqual(false, j.Contains(v1));
  126. Assert.AreEqual(true, j.Contains(v3));
  127. j.RemoveAt(1);
  128. Assert.AreEqual(false, j.Contains(v3));
  129. Assert.AreEqual(1, j.Count);
  130. }
  131. [Test]
  132. [ExpectedException(typeof(ArgumentOutOfRangeException), ExpectedMessage = @"Index is equal to or greater than Count.
  133. Parameter name: index")]
  134. public void RemoveAtOutOfRangeIndexShouldError()
  135. {
  136. JArray j = new JArray();
  137. j.RemoveAt(0);
  138. }
  139. [Test]
  140. [ExpectedException(typeof(ArgumentOutOfRangeException), ExpectedMessage = @"Index is less than 0.
  141. Parameter name: index")]
  142. public void RemoveAtNegativeIndexShouldError()
  143. {
  144. JArray j = new JArray();
  145. j.RemoveAt(-1);
  146. }
  147. [Test]
  148. public void Insert()
  149. {
  150. JValue v1 = new JValue(1);
  151. JValue v2 = new JValue(2);
  152. JValue v3 = new JValue(3);
  153. JValue v4 = new JValue(4);
  154. JArray j = new JArray();
  155. j.Add(v1);
  156. j.Add(v2);
  157. j.Add(v3);
  158. j.Insert(1, v4);
  159. Assert.AreEqual(0, j.IndexOf(v1));
  160. Assert.AreEqual(1, j.IndexOf(v4));
  161. Assert.AreEqual(2, j.IndexOf(v2));
  162. Assert.AreEqual(3, j.IndexOf(v3));
  163. }
  164. [Test]
  165. public void AddFirstAddedTokenShouldBeFirst()
  166. {
  167. JValue v1 = new JValue(1);
  168. JValue v2 = new JValue(2);
  169. JValue v3 = new JValue(3);
  170. JArray j = new JArray();
  171. Assert.AreEqual(null, j.First);
  172. Assert.AreEqual(null, j.Last);
  173. j.AddFirst(v1);
  174. Assert.AreEqual(v1, j.First);
  175. Assert.AreEqual(v1, j.Last);
  176. j.AddFirst(v2);
  177. Assert.AreEqual(v2, j.First);
  178. Assert.AreEqual(v1, j.Last);
  179. j.AddFirst(v3);
  180. Assert.AreEqual(v3, j.First);
  181. Assert.AreEqual(v1, j.Last);
  182. }
  183. [Test]
  184. public void InsertShouldInsertAtZeroIndex()
  185. {
  186. JValue v1 = new JValue(1);
  187. JValue v2 = new JValue(2);
  188. JArray j = new JArray();
  189. j.Insert(0, v1);
  190. Assert.AreEqual(0, j.IndexOf(v1));
  191. j.Insert(0, v2);
  192. Assert.AreEqual(1, j.IndexOf(v1));
  193. Assert.AreEqual(0, j.IndexOf(v2));
  194. }
  195. [Test]
  196. public void InsertNull()
  197. {
  198. JArray j = new JArray();
  199. j.Insert(0, null);
  200. Assert.AreEqual(null, ((JValue)j[0]).Value);
  201. }
  202. [Test]
  203. [ExpectedException(typeof(ArgumentOutOfRangeException), ExpectedMessage = @"Index was out of range. Must be non-negative and less than the size of the collection.
  204. Parameter name: index")]
  205. public void InsertNegativeIndexShouldThrow()
  206. {
  207. JArray j = new JArray();
  208. j.Insert(-1, new JValue(1));
  209. }
  210. [Test]
  211. [ExpectedException(typeof(ArgumentOutOfRangeException), ExpectedMessage = @"Index must be within the bounds of the List.
  212. Parameter name: index")]
  213. public void InsertOutOfRangeIndexShouldThrow()
  214. {
  215. JArray j = new JArray();
  216. j.Insert(2, new JValue(1));
  217. }
  218. [Test]
  219. public void Item()
  220. {
  221. JValue v1 = new JValue(1);
  222. JValue v2 = new JValue(2);
  223. JValue v3 = new JValue(3);
  224. JValue v4 = new JValue(4);
  225. JArray j = new JArray();
  226. j.Add(v1);
  227. j.Add(v2);
  228. j.Add(v3);
  229. j[1] = v4;
  230. Assert.AreEqual(null, v2.Parent);
  231. Assert.AreEqual(-1, j.IndexOf(v2));
  232. Assert.AreEqual(j, v4.Parent);
  233. Assert.AreEqual(1, j.IndexOf(v4));
  234. }
  235. [Test]
  236. [ExpectedException(typeof(Exception), ExpectedMessage = "Error reading JArray from JsonReader. Current JsonReader item is not an array: StartObject")]
  237. public void Parse_ShouldThrowOnUnexpectedToken()
  238. {
  239. string json = @"{""prop"":""value""}";
  240. JArray.Parse(json);
  241. }
  242. public class ListItemFields
  243. {
  244. public string ListItemText { get; set; }
  245. public object ListItemValue { get; set; }
  246. }
  247. [Test]
  248. public void ArrayOrder()
  249. {
  250. string itemZeroText = "Zero text";
  251. IEnumerable<ListItemFields> t = new List<ListItemFields>
  252. {
  253. new ListItemFields { ListItemText = "First", ListItemValue = 1 },
  254. new ListItemFields { ListItemText = "Second", ListItemValue = 2 },
  255. new ListItemFields { ListItemText = "Third", ListItemValue = 3 }
  256. };
  257. JObject optionValues =
  258. new JObject(
  259. new JProperty("options",
  260. new JArray(
  261. new JObject(
  262. new JProperty("text", itemZeroText),
  263. new JProperty("value", "0")),
  264. from r in t
  265. orderby r.ListItemValue
  266. select new JObject(
  267. new JProperty("text", r.ListItemText),
  268. new JProperty("value", r.ListItemValue.ToString())))));
  269. string result = "myOptions = " + optionValues.ToString();
  270. Assert.AreEqual(@"myOptions = {
  271. ""options"": [
  272. {
  273. ""text"": ""Zero text"",
  274. ""value"": ""0""
  275. },
  276. {
  277. ""text"": ""First"",
  278. ""value"": ""1""
  279. },
  280. {
  281. ""text"": ""Second"",
  282. ""value"": ""2""
  283. },
  284. {
  285. ""text"": ""Third"",
  286. ""value"": ""3""
  287. }
  288. ]
  289. }", result);
  290. }
  291. [Test]
  292. public void Iterate()
  293. {
  294. JArray a = new JArray(1, 2, 3, 4, 5);
  295. int i = 1;
  296. foreach (JToken token in a)
  297. {
  298. Assert.AreEqual(i, (int)token);
  299. i++;
  300. }
  301. }
  302. #if !(SILVERLIGHT || MONOTOUCH || MONODROID)
  303. [Test]
  304. public void ITypedListGetItemProperties()
  305. {
  306. JProperty p1 = new JProperty("Test1", 1);
  307. JProperty p2 = new JProperty("Test2", "Two");
  308. ITypedList a = new JArray(new JObject(p1, p2));
  309. PropertyDescriptorCollection propertyDescriptors = a.GetItemProperties(null);
  310. Assert.IsNotNull(propertyDescriptors);
  311. Assert.AreEqual(2, propertyDescriptors.Count);
  312. Assert.AreEqual("Test1", propertyDescriptors[0].Name);
  313. Assert.AreEqual("Test2", propertyDescriptors[1].Name);
  314. }
  315. #endif
  316. [Test]
  317. public void AddArrayToSelf()
  318. {
  319. JArray a = new JArray(1, 2);
  320. a.Add(a);
  321. Assert.AreEqual(3, a.Count);
  322. Assert.AreEqual(1, (int)a[0]);
  323. Assert.AreEqual(2, (int)a[1]);
  324. Assert.AreNotSame(a, a[2]);
  325. }
  326. [Test]
  327. [ExpectedException(typeof(ArgumentException), ExpectedMessage = @"Set JArray values with invalid key value: ""badvalue"". Array position index expected.")]
  328. public void SetValueWithInvalidIndex()
  329. {
  330. JArray a = new JArray();
  331. a["badvalue"] = new JValue(3);
  332. }
  333. [Test]
  334. public void SetValue()
  335. {
  336. object key = 0;
  337. JArray a = new JArray((object)null);
  338. a[key] = new JValue(3);
  339. Assert.AreEqual(3, (int)a[key]);
  340. }
  341. [Test]
  342. public void ReplaceAll()
  343. {
  344. JArray a = new JArray(new [] { 1, 2, 3 });
  345. Assert.AreEqual(3, a.Count);
  346. Assert.AreEqual(1, (int)a[0]);
  347. Assert.AreEqual(2, (int)a[1]);
  348. Assert.AreEqual(3, (int)a[2]);
  349. a.ReplaceAll(1);
  350. Assert.AreEqual(1, a.Count);
  351. Assert.AreEqual(1, (int)a[0]);
  352. }
  353. [Test]
  354. [ExpectedException(typeof(Exception), ExpectedMessage = "Unexpected end of content while loading JArray.")]
  355. public void ParseIncomplete()
  356. {
  357. JArray.Parse("[1");
  358. }
  359. }
  360. }