PageRenderTime 264ms CodeModel.GetById 37ms RepoModel.GetById 7ms app.codeStats 1ms

/mcs/class/System.Xml.Linq/Test/System.Xml.Linq/XElementTest.cs

https://bitbucket.org/steenlund/mono-2.6.7-for-amiga
C# | 1580 lines | 1417 code | 111 blank | 52 comment | 1 complexity | 1f2c876a8a1d8577ac0440734d251460 MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, LGPL-2.1
  1. //
  2. // Authors:
  3. // Atsushi Enomoto
  4. //
  5. // Copyright 2007 Novell (http://www.novell.com)
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining
  8. // a copy of this software and associated documentation files (the
  9. // "Software"), to deal in the Software without restriction, including
  10. // without limitation the rights to use, copy, modify, merge, publish,
  11. // distribute, sublicense, and/or sell copies of the Software, and to
  12. // permit persons to whom the Software is furnished to do so, subject to
  13. // the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be
  16. // included in all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. //
  26. using System;
  27. using System.Collections.Generic;
  28. using System.IO;
  29. using System.Linq;
  30. using System.Xml;
  31. using System.Xml.Linq;
  32. using NUnit.Framework;
  33. namespace MonoTests.System.Xml.Linq
  34. {
  35. [TestFixture]
  36. public class XElementTest
  37. {
  38. [Test] // xml declaration is skipped.
  39. public void LoadWithXmldecl ()
  40. {
  41. string xml = "<?xml version='1.0'?><root />";
  42. XElement.Load (new StringReader (xml));
  43. }
  44. [Test]
  45. public void Load1 ()
  46. {
  47. string xml = "<root><foo/></root>";
  48. XElement el = XElement.Load (new StringReader (xml));
  49. XElement first = el.FirstNode as XElement;
  50. Assert.IsNotNull (first, "#1");
  51. Assert.IsTrue (el.LastNode is XElement, "#2");
  52. Assert.IsNull (el.NextNode, "#3");
  53. Assert.IsNull (el.PreviousNode, "#4");
  54. Assert.AreEqual (1, new List<XNode> (el.Nodes ()).Count, "#5");
  55. Assert.AreEqual (el, first.Parent, "#6");
  56. Assert.AreEqual (first, el.LastNode, "#7");
  57. Assert.AreEqual ("root", el.Name.ToString (), "#8");
  58. Assert.AreEqual ("foo", first.Name.ToString (), "#9");
  59. Assert.IsFalse (el.Attributes ().GetEnumerator ().MoveNext (), "#10");
  60. }
  61. [Test]
  62. [ExpectedException (typeof (InvalidOperationException))]
  63. public void LoadInvalid ()
  64. {
  65. string xml = "text";
  66. XmlReaderSettings s = new XmlReaderSettings ();
  67. s.ConformanceLevel = ConformanceLevel.Fragment;
  68. XElement.Load (XmlReader.Create (new StringReader (xml), s));
  69. }
  70. [Test]
  71. public void PrecedingWhitespaces ()
  72. {
  73. string xml = " <root/>";
  74. XmlReaderSettings s = new XmlReaderSettings ();
  75. s.ConformanceLevel = ConformanceLevel.Fragment;
  76. XElement.Load (XmlReader.Create (new StringReader (xml), s));
  77. }
  78. [Test]
  79. public void PrecedingWhitespaces2 ()
  80. {
  81. string xml = " <root/>";
  82. XmlReaderSettings s = new XmlReaderSettings ();
  83. s.ConformanceLevel = ConformanceLevel.Fragment;
  84. XmlReader r = XmlReader.Create (new StringReader (xml), s);
  85. r.Read (); // at whitespace
  86. XElement.Load (r);
  87. }
  88. [Test]
  89. public void Load2 ()
  90. {
  91. string xml = "<root>foo</root>";
  92. XElement el = XElement.Load (new StringReader (xml));
  93. XText first = el.FirstNode as XText;
  94. Assert.IsNotNull (first, "#1");
  95. Assert.IsTrue (el.LastNode is XText, "#2");
  96. Assert.AreEqual (1, new List<XNode> (el.Nodes ()).Count, "#3");
  97. Assert.AreEqual (el, first.Parent, "#4");
  98. Assert.AreEqual (first, el.LastNode, "#5");
  99. Assert.AreEqual ("foo", first.Value, "#6");
  100. }
  101. [Test]
  102. [ExpectedException (typeof (ArgumentException))]
  103. public void AddDocumentTypeToElement ()
  104. {
  105. XElement el = new XElement (XName.Get ("foo"));
  106. el.Add (new XDocumentType ("foo", null, null, null));
  107. }
  108. [Test]
  109. [ExpectedException (typeof (ArgumentException))]
  110. [Category ("NotDotNet")]
  111. public void AddXDeclarationToElement ()
  112. {
  113. XElement el = new XElement (XName.Get ("foo"));
  114. // LAMESPEC: in .NET, XDeclaration is not treated as
  115. // invalid, and converted to a string without error.
  116. el.Add (new XDeclaration ("1.0", null, null));
  117. }
  118. [Test]
  119. public void SetAttribute ()
  120. {
  121. XElement el = new XElement (XName.Get ("foo"));
  122. el.SetAttributeValue (XName.Get ("a1"), "v1");
  123. XAttribute a = el.FirstAttribute;
  124. Assert.IsNotNull (a, "#1-1");
  125. Assert.AreEqual (el, a.Parent, "#1-2");
  126. Assert.IsNotNull (el.LastAttribute, "#1-3");
  127. Assert.AreEqual (a, el.LastAttribute, "#1-4");
  128. Assert.AreEqual ("a1", a.Name.LocalName, "#1-5");
  129. Assert.AreEqual ("v1", a.Value, "#1-6");
  130. Assert.IsNull (a.PreviousAttribute, "#1-7");
  131. Assert.IsNull (a.NextAttribute, "#1-8");
  132. el.SetAttributeValue (XName.Get ("a2"), "v2");
  133. Assert.IsFalse (el.FirstAttribute == el.LastAttribute, "#2-1");
  134. Assert.AreEqual ("a2", el.LastAttribute.Name.LocalName, "#2-2");
  135. el.SetAttributeValue (XName.Get ("a1"), "v3");
  136. XAttribute b = el.FirstAttribute;
  137. Assert.IsNotNull (b, "#2-3");
  138. Assert.IsNotNull (el.LastAttribute, "#2-4");
  139. Assert.AreEqual ("a1", b.Name.LocalName, "#2-5");
  140. Assert.AreEqual ("v3", b.Value, "#2-6");
  141. Assert.AreEqual (a, b, "#2-7");
  142. XAttribute c = el.LastAttribute;
  143. Assert.AreEqual (a, c.PreviousAttribute, "#2-8");
  144. a.Remove ();
  145. Assert.IsNull (a.Parent, "#3-1");
  146. Assert.IsNull (a.PreviousAttribute, "#3-2");
  147. Assert.IsNull (a.NextAttribute, "#3-3");
  148. Assert.IsNull (c.PreviousAttribute, "#3-4");
  149. Assert.IsNull (c.NextAttribute, "#3-5");
  150. el.RemoveAttributes ();
  151. Assert.IsFalse (el.HasAttributes, "#4-1");
  152. Assert.IsNull (b.Parent, "#4-2");
  153. Assert.IsNull (c.Parent, "#4-3");
  154. Assert.IsNull (el.FirstAttribute, "#4-4");
  155. Assert.IsNull (el.LastAttribute, "#4-5");
  156. }
  157. [Test]
  158. public void AddAfterSelf ()
  159. {
  160. XElement el = XElement.Parse ("<root><foo/><bar/></root>");
  161. el.FirstNode.AddAfterSelf ("text");
  162. XText t = el.FirstNode.NextNode as XText;
  163. Assert.IsNotNull (t, "#1");
  164. Assert.AreEqual ("text", t.Value, "#2");
  165. XElement bar = t.NextNode as XElement;
  166. Assert.IsNotNull (bar, "#3");
  167. Assert.AreEqual ("bar", bar.Name.LocalName, "#4");
  168. }
  169. [Test]
  170. public void AddAfterSelfList ()
  171. {
  172. XElement el = XElement.Parse ("<root><foo/><bar/></root>");
  173. el.FirstNode.AddAfterSelf (new XText [] {
  174. new XText ("t1"),
  175. new XText ("t2"),
  176. new XText ("t3")});
  177. XText t = el.FirstNode.NextNode as XText;
  178. Assert.IsNotNull (t, "#1");
  179. Assert.AreEqual ("t1", t.Value, "#2");
  180. Assert.AreEqual ("t2", ((XText) t.NextNode).Value, "#3");
  181. Assert.AreEqual ("t3", ((XText) t.NextNode.NextNode).Value, "#4");
  182. XElement bar = t.NextNode.NextNode.NextNode as XElement;
  183. Assert.IsNotNull (bar, "#5");
  184. Assert.AreEqual ("bar", bar.Name.LocalName, "#6");
  185. }
  186. [Test]
  187. [ExpectedException (typeof (ArgumentException))]
  188. public void AddAfterSelfAttribute ()
  189. {
  190. var el = new XElement ("root", new XElement ("child"));
  191. var el2 = el.FirstNode as XElement;
  192. el2.AddAfterSelf (new XAttribute ("foo", "bar"));
  193. }
  194. [Test]
  195. [ExpectedException (typeof (ArgumentException))]
  196. public void AddAfterSelfXDocument ()
  197. {
  198. var el = new XElement ("root", new XElement ("child"));
  199. var el2 = el.FirstNode as XElement;
  200. el2.AddAfterSelf (new XDocument ());
  201. }
  202. [Test]
  203. [ExpectedException (typeof (ArgumentException))]
  204. [Category ("NotDotNet")]
  205. [Category ("NotWorking")]
  206. // LAMESPEC: there is no reason to not reject XDeclaration while it rejects XDocument.
  207. public void AddAfterSelfXDeclaration ()
  208. {
  209. var el = new XElement ("root", new XElement ("child"));
  210. var el2 = el.FirstNode as XElement;
  211. el2.AddAfterSelf (new XDeclaration ("1.0", null, null));
  212. }
  213. [Test]
  214. public void AddAfterSelfCollection ()
  215. {
  216. var el = new XElement ("root", new XElement ("child"));
  217. el.FirstNode.AddAfterSelf (new List<XElement> (new XElement [] {new XElement ("foo"), new XElement ("bar")}));
  218. Assert.AreEqual ("<root><child /><foo /><bar /></root>", el.ToString (SaveOptions.DisableFormatting), "#1");
  219. Assert.AreEqual ("bar", (el.LastNode as XElement).Name.LocalName, "#2");
  220. }
  221. [Test]
  222. public void AddAfterSelfJoinsStringAfterText ()
  223. {
  224. var el = XElement.Parse ("<foo>text1</foo>");
  225. el.LastNode.AddAfterSelf ("text2");
  226. el.LastNode.AddAfterSelf (new XText ("text3"));
  227. IEnumerator<XNode> e = el.Nodes ().GetEnumerator ();
  228. Assert.IsTrue (e.MoveNext (), "#1");
  229. Assert.AreEqual ("text1text2", e.Current.ToString (), "#2");
  230. Assert.IsTrue (e.MoveNext (), "#3");
  231. Assert.AreEqual ("text3", e.Current.ToString (), "#4");
  232. Assert.IsFalse (e.MoveNext (), "#5");
  233. }
  234. [Test]
  235. public void AddBeforeSelf ()
  236. {
  237. XElement el = XElement.Parse ("<root><foo/><bar/></root>");
  238. el.FirstNode.AddBeforeSelf ("text");
  239. XText t = el.FirstNode as XText;
  240. Assert.IsNotNull (t, "#1");
  241. Assert.AreEqual ("text", t.Value, "#2");
  242. XElement foo = t.NextNode as XElement;
  243. Assert.IsNotNull (foo, "#3");
  244. Assert.AreEqual ("foo", foo.Name.LocalName, "#4");
  245. }
  246. [Test]
  247. public void AddBeforeSelfList ()
  248. {
  249. XElement el = XElement.Parse ("<root><foo/><bar/></root>");
  250. el.FirstNode.AddBeforeSelf (new XText [] {
  251. new XText ("t1"),
  252. new XText ("t2"),
  253. new XText ("t3")});
  254. XText t = el.FirstNode as XText;
  255. Assert.IsNotNull (t, "#1");
  256. Assert.AreEqual ("t1", t.Value, "#2");
  257. Assert.AreEqual ("t2", ((XText) t.NextNode).Value, "#3");
  258. Assert.AreEqual ("t3", ((XText) t.NextNode.NextNode).Value, "#4");
  259. XElement foo = t.NextNode.NextNode.NextNode as XElement;
  260. Assert.IsNotNull (foo, "#5");
  261. Assert.AreEqual ("foo", foo.Name.LocalName, "#6");
  262. }
  263. [Test]
  264. public void AddBeforeSelfList2 ()
  265. {
  266. XElement el = XElement.Parse ("<root><foo/><bar/></root>");
  267. el.FirstNode.AddBeforeSelf ("t1", "t2", "t3");
  268. XText t = el.FirstNode as XText;
  269. Assert.IsNotNull (t, "#1");
  270. Assert.AreEqual ("t1t2t3", t.Value, "#2");
  271. XElement foo = t.NextNode as XElement;
  272. Assert.IsNotNull (foo, "#3");
  273. Assert.AreEqual ("foo", foo.Name.LocalName, "#4");
  274. }
  275. [Test]
  276. public void AddJoinsStringAfterText ()
  277. {
  278. var el = XElement.Parse ("<foo>text1</foo>");
  279. el.Add ("text2");
  280. el.Add (new XText ("text3"));
  281. IEnumerator<XNode> e = el.Nodes ().GetEnumerator ();
  282. Assert.IsTrue (e.MoveNext (), "#1");
  283. Assert.AreEqual ("text1text2", e.Current.ToString (), "#2");
  284. Assert.IsTrue (e.MoveNext (), "#3");
  285. Assert.AreEqual ("text3", e.Current.ToString (), "#4");
  286. Assert.IsFalse (e.MoveNext (), "#5");
  287. }
  288. [Test]
  289. [ExpectedException (typeof (InvalidOperationException))]
  290. public void AddDuplicateAttribute ()
  291. {
  292. var el = new XElement ("foo",
  293. new XAttribute ("bar", "baz"));
  294. el.Add (new XAttribute ("bar", "baz"));
  295. }
  296. [Test]
  297. public void ReplaceWith ()
  298. {
  299. XElement el = XElement.Parse ("<root><foo/><bar/></root>");
  300. XNode fc = el.FirstNode;
  301. fc.ReplaceWith ("test");
  302. XText t = el.FirstNode as XText;
  303. Assert.IsNotNull (t, "#1");
  304. Assert.AreEqual ("test", t.Value, "#2");
  305. }
  306. [Test]
  307. public void ReplaceAll ()
  308. {
  309. XElement el = XElement.Parse ("<root><foo/><bar/></root>");
  310. el.ReplaceAll ("test");
  311. XText t = el.FirstNode as XText;
  312. Assert.IsNotNull (t, "#1");
  313. Assert.AreEqual ("test", t.Value, "#2");
  314. Assert.AreEqual (1, new List<XNode> (el.Nodes ()).Count, "#3");
  315. }
  316. [Test]
  317. public void ReplaceAllList ()
  318. {
  319. XElement el = XElement.Parse ("<root><foo/><bar/></root>");
  320. el.ReplaceAll (
  321. new XText ("test1"),
  322. new XText ("test2"),
  323. new XText ("test3"));
  324. XText t = el.FirstNode as XText;
  325. Assert.IsNotNull (t, "#1");
  326. Assert.AreEqual ("test1", t.Value, "#2");
  327. t = el.LastNode as XText;
  328. Assert.IsNotNull (t, "#3");
  329. Assert.AreEqual ("test3", t.Value, "#4");
  330. Assert.AreEqual (3, new List<XNode> (el.Nodes ()).Count, "#5");
  331. }
  332. [Test]
  333. public void ReplaceAttributes ()
  334. {
  335. XElement el = XElement.Parse ("<root x='y'><foo a='b'/></root>");
  336. Assert.IsTrue (el.Attributes ().GetEnumerator ().MoveNext (), "#0");
  337. el.ReplaceAttributes ("test");
  338. Assert.IsTrue (el.FirstNode is XElement, "#1");
  339. Assert.IsTrue (el.LastNode is XText, "#2");
  340. Assert.IsFalse (el.Attributes ().GetEnumerator ().MoveNext (), "#3");
  341. }
  342. [Test]
  343. public void GetDefaultNamespace ()
  344. {
  345. XElement el = XElement.Parse ("<root xmlns='urn:foo'><foo><xxx/></foo><x:bar xmlns:x='urn:bar'><yyy/></x:bar><baz xmlns=''><zzz /></baz></root>");
  346. XNamespace ns1 = XNamespace.Get ("urn:foo");
  347. Assert.AreEqual (ns1, el.GetDefaultNamespace (), "#1");
  348. XElement foo = (XElement) el.FirstNode;
  349. Assert.AreEqual (ns1, foo.GetDefaultNamespace (), "#2");
  350. Assert.AreEqual (ns1, ((XElement) foo.FirstNode).GetDefaultNamespace (), "#3");
  351. XElement bar = (XElement) foo.NextNode;
  352. Assert.AreEqual (ns1, bar.GetDefaultNamespace (), "#4");
  353. Assert.AreEqual (ns1, ((XElement) bar.FirstNode).GetDefaultNamespace (), "#5");
  354. XElement baz = (XElement) bar.NextNode;
  355. Assert.AreEqual (XNamespace.Get (String.Empty), baz.GetDefaultNamespace (), "#6");
  356. Assert.AreEqual (XNamespace.Get (String.Empty), ((XElement) baz.FirstNode).GetDefaultNamespace (), "#7");
  357. }
  358. [Test]
  359. public void GetPrefixNamespace ()
  360. {
  361. XElement el = XElement.Parse ("<x:root xmlns:x='urn:foo'><foo><xxx/></foo><x:bar xmlns:x='urn:bar'><yyy/></x:bar><baz xmlns=''><zzz /></baz></x:root>");
  362. XNamespace ns1 = XNamespace.Get ("urn:foo");
  363. XNamespace ns2 = XNamespace.Get ("urn:bar");
  364. Assert.AreEqual (ns1, el.GetNamespaceOfPrefix ("x"), "#1-1");
  365. Assert.AreEqual ("x", el.GetPrefixOfNamespace (ns1), "#1-2");
  366. XElement foo = (XElement) el.FirstNode;
  367. Assert.AreEqual (ns1, foo.GetNamespaceOfPrefix ("x"), "#2-1");
  368. Assert.AreEqual ("x", foo.GetPrefixOfNamespace (ns1), "#2-2");
  369. Assert.AreEqual (ns1, ((XElement) foo.FirstNode).GetNamespaceOfPrefix ("x"), "#3-1");
  370. Assert.AreEqual ("x", ((XElement) foo.FirstNode).GetPrefixOfNamespace (ns1), "#3-2");
  371. XElement bar = (XElement) foo.NextNode;
  372. Assert.AreEqual (ns2, bar.GetNamespaceOfPrefix ("x"), "#4-1");
  373. Assert.AreEqual ("x", bar.GetPrefixOfNamespace (ns2), "#4-2");
  374. Assert.AreEqual (null, bar.GetPrefixOfNamespace (ns1), "#4-3");
  375. Assert.AreEqual (ns2, ((XElement) bar.FirstNode).GetNamespaceOfPrefix ("x"), "#5-1");
  376. Assert.AreEqual ("x", ((XElement) bar.FirstNode).GetPrefixOfNamespace (ns2), "#5-2");
  377. Assert.AreEqual (null, ((XElement) bar.FirstNode).GetPrefixOfNamespace (ns1), "#5-3");
  378. }
  379. #pragma warning disable 219
  380. [Test]
  381. public void CastNulls ()
  382. {
  383. const XElement a = null;
  384. Assert.AreEqual (null, (bool?) a, "bool?");
  385. Assert.AreEqual (null, (DateTime?) a, "DateTime?");
  386. Assert.AreEqual (null, (DateTimeOffset?) a, "DateTimeOffset?");
  387. Assert.AreEqual (null, (decimal?) a, "decimal?");
  388. Assert.AreEqual (null, (double?) a, "double?");
  389. Assert.AreEqual (null, (float?) a, "float?");
  390. Assert.AreEqual (null, (Guid?) a, "Guid?");
  391. Assert.AreEqual (null, (int?) a, "int?");
  392. Assert.AreEqual (null, (long?) a, "long?");
  393. Assert.AreEqual (null, (uint?) a, "uint?");
  394. Assert.AreEqual (null, (ulong?) a, "ulong?");
  395. Assert.AreEqual (null, (TimeSpan?) a, "TimeSpan?");
  396. Assert.AreEqual (null, (string) a, "string");
  397. AssertThrows<ArgumentNullException> (() => { bool z = (bool) a; }, "bool");
  398. AssertThrows<ArgumentNullException> (() => { DateTime z = (DateTime) a; }, "DateTime");
  399. AssertThrows<ArgumentNullException> (() => { DateTimeOffset z = (DateTimeOffset) a; }, "DateTimeOffset");
  400. AssertThrows<ArgumentNullException> (() => { decimal z = (decimal) a; }, "decimal");
  401. AssertThrows<ArgumentNullException> (() => { double z = (double) a; }, "double");
  402. AssertThrows<ArgumentNullException> (() => { float z = (float) a; }, "float");
  403. AssertThrows<ArgumentNullException> (() => { Guid z = (Guid) a; }, "Guid");
  404. AssertThrows<ArgumentNullException> (() => { int z = (int) a; }, "int");
  405. AssertThrows<ArgumentNullException> (() => { long z = (long) a; }, "long");
  406. AssertThrows<ArgumentNullException> (() => { uint z = (uint) a; }, "uint");
  407. AssertThrows<ArgumentNullException> (() => { ulong z = (ulong) a; }, "ulong");
  408. AssertThrows<ArgumentNullException> (() => { TimeSpan z = (TimeSpan) a; }, "TimeSpan");
  409. }
  410. /// <remarks>
  411. /// Provides functionality similar to Assert.Throws that is available on newer versions of NUnit.
  412. /// </remarks>
  413. private static T AssertThrows<T> (Action code, string message, params object[] args) where T : Exception
  414. {
  415. Exception actual = null;
  416. try {
  417. code ();
  418. } catch (Exception exception) {
  419. actual = exception;
  420. }
  421. Assert.That (actual, new NUnit.Framework.Constraints.ExactTypeConstraint (typeof (T)), message, args);
  422. return (T) actual;
  423. }
  424. [Test]
  425. public void CastEmpties ()
  426. {
  427. XElement a = new XElement ("a");
  428. // Verify expected "cloning" and "empty" behaviour as prerequisites
  429. Assert.IsTrue (a.IsEmpty, "#1-1");
  430. Assert.IsTrue (new XElement (a).IsEmpty, "#1-2");
  431. Assert.AreEqual (String.Empty, a.Value, "#2-1");
  432. Assert.AreEqual (String.Empty, new XElement (a).Value, "#2-2");
  433. Assert.AreNotSame (a, new XElement (a), "#3-1");
  434. Assert.AreEqual (a.ToString (), new XElement (a).ToString (), "#3-2");
  435. Assert.AreEqual ("<a />", a.ToString (), "#3-3");
  436. Assert.AreEqual (a.ToString (), new XElement ("a", null).ToString (), "#3-4");
  437. // Execute the primary assertions of this test
  438. AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (a); }, "bool?");
  439. AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (a); }, "DateTime?");
  440. AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (a); }, "DateTimeOffset?");
  441. AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (a); }, "decimal?");
  442. AssertThrows<FormatException> (() => { double? z = (double?) new XElement (a); }, "double?");
  443. AssertThrows<FormatException> (() => { float? z = (float?) new XElement (a); }, "float?");
  444. AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (a); }, "Guid?");
  445. AssertThrows<FormatException> (() => { int? z = (int?) new XElement (a); }, "int?");
  446. AssertThrows<FormatException> (() => { long? z = (long?) new XElement (a); }, "long?");
  447. AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (a); }, "uint?");
  448. AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (a); }, "ulong?");
  449. AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (a); }, "TimeSpan?");
  450. Assert.AreEqual (String.Empty, (string) new XElement (a), "string");
  451. AssertThrows<FormatException> (() => { bool z = (bool) new XElement (a); }, "bool");
  452. AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (a); }, "DateTime");
  453. AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (a); }, "DateTimeOffset");
  454. AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (a); }, "decimal");
  455. AssertThrows<FormatException> (() => { double z = (double) new XElement (a); }, "double");
  456. AssertThrows<FormatException> (() => { float z = (float) new XElement (a); }, "float");
  457. AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (a); }, "Guid");
  458. AssertThrows<FormatException> (() => { int z = (int) new XElement (a); }, "int");
  459. AssertThrows<FormatException> (() => { long z = (long) new XElement (a); }, "long");
  460. AssertThrows<FormatException> (() => { uint z = (uint) new XElement (a); }, "uint");
  461. AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (a); }, "ulong");
  462. AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (a); }, "TimeSpan");
  463. }
  464. [Test]
  465. public void CastBlanks ()
  466. {
  467. XElement a = new XElement ("a", String.Empty);
  468. XElement b = new XElement ("b", new XCData (string.Empty));
  469. // Verify expected "cloning" and "blank" behaviour as prerequisites
  470. Assert.IsFalse (a.IsEmpty, "#1-1a");
  471. Assert.IsFalse (b.IsEmpty, "#1-1b");
  472. Assert.IsFalse (new XElement (a).IsEmpty, "#1-2a");
  473. Assert.IsFalse (new XElement (b).IsEmpty, "#1-2b");
  474. Assert.AreEqual (String.Empty, a.Value, "#2-1a");
  475. Assert.AreEqual (String.Empty, b.Value, "#2-1b");
  476. Assert.AreEqual (String.Empty, new XElement (a).Value, "#2-2a");
  477. Assert.AreEqual (String.Empty, new XElement (b).Value, "#2-2b");
  478. Assert.AreNotSame (a, new XElement (a), "#3-1a");
  479. Assert.AreNotSame (b, new XElement (b), "#3-1b");
  480. Assert.AreEqual (a.ToString (), new XElement (a).ToString (), "#3-2a");
  481. Assert.AreEqual (b.ToString (), new XElement (b).ToString (), "#3-2b");
  482. Assert.AreEqual ("<a></a>", a.ToString (), "#3-3a");
  483. Assert.AreEqual ("<b><![CDATA[]]></b>", b.ToString (), "#3-3b");
  484. Assert.AreEqual (a.ToString (), new XElement ("a", "").ToString (), "#3-4a");
  485. Assert.AreEqual (b.ToString (), new XElement ("b", new XCData ("")).ToString (), "#3-4b");
  486. // Execute the primary assertions of this test
  487. AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (a); }, "a:bool?");
  488. AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (b); }, "b:bool?");
  489. AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (a); }, "a:DateTime?");
  490. AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (b); }, "b:DateTime?");
  491. AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (a); }, "a:DateTimeOffset?");
  492. AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (b); }, "b:DateTimeOffset?");
  493. AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (a); }, "a:decimal?");
  494. AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (b); }, "b:decimal?");
  495. AssertThrows<FormatException> (() => { double? z = (double?) new XElement (a); }, "a:double?");
  496. AssertThrows<FormatException> (() => { double? z = (double?) new XElement (b); }, "b:double?");
  497. AssertThrows<FormatException> (() => { float? z = (float?) new XElement (a); }, "a:float?");
  498. AssertThrows<FormatException> (() => { float? z = (float?) new XElement (b); }, "b:float?");
  499. AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (a); }, "a:Guid?");
  500. AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (b); }, "b:Guid?");
  501. AssertThrows<FormatException> (() => { int? z = (int?) new XElement (a); }, "a:int?");
  502. AssertThrows<FormatException> (() => { int? z = (int?) new XElement (b); }, "b:int?");
  503. AssertThrows<FormatException> (() => { long? z = (long?) new XElement (a); }, "a:long?");
  504. AssertThrows<FormatException> (() => { long? z = (long?) new XElement (b); }, "b:long?");
  505. AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (a); }, "a:uint?");
  506. AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (b); }, "b:uint?");
  507. AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (a); }, "a:ulong?");
  508. AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (b); }, "b:ulong?");
  509. AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (a); }, "a:TimeSpan?");
  510. AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (b); }, "b:TimeSpan?");
  511. Assert.AreEqual (String.Empty, (string) new XElement (a), "a:string");
  512. Assert.AreEqual (String.Empty, (string) new XElement (b), "b:string");
  513. AssertThrows<FormatException> (() => { bool z = (bool) new XElement (a); }, "a:bool");
  514. AssertThrows<FormatException> (() => { bool z = (bool) new XElement (b); }, "b:bool");
  515. AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (a); }, "a:DateTime");
  516. AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (b); }, "b:DateTime");
  517. AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (a); }, "a:DateTimeOffset");
  518. AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (b); }, "b:DateTimeOffset");
  519. AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (a); }, "a:decimal");
  520. AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (b); }, "b:decimal");
  521. AssertThrows<FormatException> (() => { double z = (double) new XElement (a); }, "a:double");
  522. AssertThrows<FormatException> (() => { double z = (double) new XElement (b); }, "b:double");
  523. AssertThrows<FormatException> (() => { float z = (float) new XElement (a); }, "a:float");
  524. AssertThrows<FormatException> (() => { float z = (float) new XElement (b); }, "b:float");
  525. AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (a); }, "a:Guid");
  526. AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (b); }, "b:Guid");
  527. AssertThrows<FormatException> (() => { int z = (int) new XElement (a); }, "a:int");
  528. AssertThrows<FormatException> (() => { int z = (int) new XElement (b); }, "b:int");
  529. AssertThrows<FormatException> (() => { long z = (long) new XElement (a); }, "a:long");
  530. AssertThrows<FormatException> (() => { long z = (long) new XElement (b); }, "b:long");
  531. AssertThrows<FormatException> (() => { uint z = (uint) new XElement (a); }, "a:uint");
  532. AssertThrows<FormatException> (() => { uint z = (uint) new XElement (b); }, "b:uint");
  533. AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (a); }, "a:ulong");
  534. AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (b); }, "b:ulong");
  535. AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (a); }, "a:TimeSpan");
  536. AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (b); }, "b:TimeSpan");
  537. }
  538. [Test]
  539. public void CastSpaces ()
  540. {
  541. XElement a = new XElement ("a", " ");
  542. XElement b = new XElement ("b", new XCData (" "));
  543. // Verify expected "cloning" and "space" behaviour as prerequisites
  544. Assert.IsFalse (a.IsEmpty, "#1-1a");
  545. Assert.IsFalse (b.IsEmpty, "#1-1b");
  546. Assert.IsFalse (new XElement (a).IsEmpty, "#1-2a");
  547. Assert.IsFalse (new XElement (b).IsEmpty, "#1-2b");
  548. Assert.AreEqual (" ", a.Value, "#2-1a");
  549. Assert.AreEqual (" ", b.Value, "#2-1b");
  550. Assert.AreEqual (" ", new XElement (a).Value, "#2-2a");
  551. Assert.AreEqual (" ", new XElement (b).Value, "#2-2b");
  552. Assert.AreNotSame (a, new XElement (a), "#3-1a");
  553. Assert.AreNotSame (b, new XElement (b), "#3-1b");
  554. Assert.AreEqual (a.ToString (), new XElement (a).ToString (), "#3-2a");
  555. Assert.AreEqual (b.ToString (), new XElement (b).ToString (), "#3-2b");
  556. Assert.AreEqual ("<a> </a>", a.ToString (), "#3-3a");
  557. Assert.AreEqual ("<b><![CDATA[ ]]></b>", b.ToString (), "#3-3b");
  558. Assert.AreEqual (a.ToString (), new XElement ("a", ' ').ToString (), "#3-4");
  559. // Execute the primary assertions of this test
  560. AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (a); }, "a:bool?");
  561. AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (b); }, "b:bool?");
  562. AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (a); }, "a:DateTime?");
  563. AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (b); }, "b:DateTime?");
  564. AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (a); }, "a:DateTimeOffset?");
  565. AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (b); }, "b:DateTimeOffset?");
  566. AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (a); }, "a:decimal?");
  567. AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (b); }, "b:decimal?");
  568. AssertThrows<FormatException> (() => { double? z = (double?) new XElement (a); }, "a:double?");
  569. AssertThrows<FormatException> (() => { double? z = (double?) new XElement (b); }, "b:double?");
  570. AssertThrows<FormatException> (() => { float? z = (float?) new XElement (a); }, "a:float?");
  571. AssertThrows<FormatException> (() => { float? z = (float?) new XElement (b); }, "b:float?");
  572. AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (a); }, "a:Guid?");
  573. AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (b); }, "b:Guid?");
  574. AssertThrows<FormatException> (() => { int? z = (int?) new XElement (a); }, "a:int?");
  575. AssertThrows<FormatException> (() => { int? z = (int?) new XElement (b); }, "b:int?");
  576. AssertThrows<FormatException> (() => { long? z = (long?) new XElement (a); }, "a:long?");
  577. AssertThrows<FormatException> (() => { long? z = (long?) new XElement (b); }, "b:long?");
  578. AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (a); }, "a:uint?");
  579. AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (b); }, "b:uint?");
  580. AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (a); }, "a:ulong?");
  581. AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (b); }, "b:ulong?");
  582. AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (a); }, "a:TimeSpan?");
  583. AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (b); }, "b:TimeSpan?");
  584. Assert.AreEqual (" ", (string) new XElement (a), "a:string");
  585. Assert.AreEqual (" ", (string) new XElement (b), "b:string");
  586. AssertThrows<FormatException> (() => { bool z = (bool) new XElement (a); }, "a:bool");
  587. AssertThrows<FormatException> (() => { bool z = (bool) new XElement (b); }, "b:bool");
  588. AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (a); }, "a:DateTime");
  589. AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (b); }, "b:DateTime");
  590. AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (a); }, "a:DateTimeOffset");
  591. AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (b); }, "b:DateTimeOffset");
  592. AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (a); }, "a:decimal");
  593. AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (b); }, "b:decimal");
  594. AssertThrows<FormatException> (() => { double z = (double) new XElement (a); }, "a:double");
  595. AssertThrows<FormatException> (() => { double z = (double) new XElement (b); }, "b:double");
  596. AssertThrows<FormatException> (() => { float z = (float) new XElement (a); }, "a:float");
  597. AssertThrows<FormatException> (() => { float z = (float) new XElement (b); }, "b:float");
  598. AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (a); }, "a:Guid");
  599. AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (b); }, "b:Guid");
  600. AssertThrows<FormatException> (() => { int z = (int) new XElement (a); }, "a:int");
  601. AssertThrows<FormatException> (() => { int z = (int) new XElement (b); }, "b:int");
  602. AssertThrows<FormatException> (() => { long z = (long) new XElement (a); }, "a:long");
  603. AssertThrows<FormatException> (() => { long z = (long) new XElement (b); }, "b:long");
  604. AssertThrows<FormatException> (() => { uint z = (uint) new XElement (a); }, "a:uint");
  605. AssertThrows<FormatException> (() => { uint z = (uint) new XElement (b); }, "b:uint");
  606. AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (a); }, "a:ulong");
  607. AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (b); }, "b:ulong");
  608. AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (a); }, "a:TimeSpan");
  609. AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (b); }, "b:TimeSpan");
  610. }
  611. [Test]
  612. public void CastNumbers ()
  613. {
  614. XElement a = new XElement ("a", "7");
  615. XElement b = new XElement ("b", new XCData (" 42 "));
  616. XElement c = new XElement ("c", " \r\n 13 \t ");
  617. XElement d = new XElement ("d", -101);
  618. XElement o = new XElement ("o", "0");
  619. XElement l = new XElement ("l", "1");
  620. XElement I = new XElement ("I", "INF");
  621. XElement i = new XElement ("i", " Infinity ");
  622. XElement M = new XElement ("M", " -INF ");
  623. XElement m = new XElement ("m", "-Infinity");
  624. XElement n = new XElement ("n", "\t NaN ");
  625. // Verify expected "cloning" and basic conversion behaviour as prerequisites
  626. Assert.IsFalse (a.IsEmpty, "#1-1");
  627. Assert.IsFalse (new XElement (b).IsEmpty, "#1-2");
  628. Assert.AreEqual (" \r\n 13 \t ", c.Value, "#2-1");
  629. Assert.AreEqual ("-101", new XElement (d).Value, "#2-2");
  630. Assert.AreNotSame (o, new XElement (o), "#3-1");
  631. Assert.AreEqual (l.ToString (), new XElement (l).ToString (), "#3-2");
  632. Assert.AreEqual ("<a>7</a>", a.ToString (), "#3-3a");
  633. Assert.AreEqual ("<b><![CDATA[ 42 ]]></b>", b.ToString (), "#3-3b");
  634. Assert.AreEqual ("<c> \r\n 13 \t </c>", c.ToString (), "#3-3c");
  635. Assert.AreEqual ("<d>-101</d>", d.ToString (), "#3-3d");
  636. Assert.AreEqual ("<o>0</o>", new XElement ("o", 0.0).ToString (), "#3-3o");
  637. Assert.AreEqual ("<l>1</l>", new XElement ("l", 1.0f).ToString (), "#3-3l");
  638. Assert.AreEqual ("<n>NaN</n>", new XElement ("n", double.NaN).ToString (), "#3-3n");
  639. Assert.AreEqual (a.ToString (), new XElement ("a", '7').ToString (), "#3-4a");
  640. Assert.AreEqual (d.ToString (), new XElement ("d", "-101").ToString (), "#3-4d");
  641. Assert.AreEqual (o.ToString (), new XElement ("o", 0L).ToString (), "#3-4o");
  642. Assert.AreEqual (l.ToString (), new XElement ("l", 1m).ToString (), "#3-4l");
  643. // Execute the primary assertions of this test
  644. AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (a); }, "a:bool?");
  645. AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (b); }, "b:bool?");
  646. AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (c); }, "c:bool?");
  647. AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (d); }, "d:bool?");
  648. Assert.IsNotNull ((bool?) new XElement (o), "o:bool?:null");
  649. Assert.AreEqual (false, ((bool?) new XElement (o)).Value, "o:bool?:value");
  650. Assert.IsNotNull ((bool?) new XElement (l), "l:bool?:null");
  651. Assert.AreEqual (true, ((bool?) new XElement (l)).Value, "l:bool?:value");
  652. AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (I); }, "I:bool?");
  653. AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (i); }, "i:bool?");
  654. AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (M); }, "M:bool?");
  655. AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (m); }, "m:bool?");
  656. AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (n); }, "n:bool?");
  657. AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (a); }, "a:DateTime?");
  658. AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (b); }, "b:DateTime?");
  659. AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (c); }, "c:DateTime?");
  660. AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (d); }, "d:DateTime?");
  661. AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (o); }, "o:DateTime?");
  662. AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (l); }, "l:DateTime?");
  663. AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (I); }, "I:DateTime?");
  664. AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (i); }, "i:DateTime?");
  665. AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (M); }, "M:DateTime?");
  666. AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (m); }, "m:DateTime?");
  667. AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (n); }, "n:DateTime?");
  668. AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (a); }, "a:DateTimeOffset?");
  669. AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (b); }, "b:DateTimeOffset?");
  670. AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (c); }, "c:DateTimeOffset?");
  671. AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (d); }, "d:DateTimeOffset?");
  672. AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (o); }, "o:DateTimeOffset?");
  673. AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (l); }, "l:DateTimeOffset?");
  674. AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (I); }, "I:DateTimeOffset?");
  675. AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (i); }, "i:DateTimeOffset?");
  676. AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (M); }, "M:DateTimeOffset?");
  677. AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (m); }, "m:DateTimeOffset?");
  678. AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (n); }, "n:DateTimeOffset?");
  679. Assert.IsNotNull ((decimal?) new XElement (a), "a:decimal?:null");
  680. Assert.AreEqual (7m, ((decimal?) new XElement (a)).Value, "a:decimal?:value");
  681. Assert.IsNotNull ((decimal?) new XElement (b), "b:decimal?:null");
  682. Assert.AreEqual (42m, ((decimal?) new XElement (b)).Value, "b:decimal?:value");
  683. Assert.IsNotNull ((decimal?) new XElement (c), "c:decimal?:null");
  684. Assert.AreEqual (13m, ((decimal?) new XElement (c)).Value, "c:decimal?:value");
  685. Assert.IsNotNull ((decimal?) new XElement (d), "d:decimal?:null");
  686. Assert.AreEqual (-101m, ((decimal?) new XElement (d)).Value, "d:decimal?:value");
  687. Assert.IsNotNull ((decimal?) new XElement (o), "o:decimal?:null");
  688. Assert.AreEqual (0m, ((decimal?) new XElement (o)).Value, "o:decimal?:value");
  689. Assert.IsNotNull ((decimal?) new XElement (l), "l:decimal?:null");
  690. Assert.AreEqual (1m, ((decimal?) new XElement (l)).Value, "l:decimal?:value");
  691. AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (I); }, "I:decimal?");
  692. AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (i); }, "i:decimal?");
  693. AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (M); }, "M:decimal?");
  694. AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (m); }, "m:decimal?");
  695. AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (n); }, "n:decimal?");
  696. Assert.IsNotNull ((double?) new XElement (a), "a:double?:null");
  697. Assert.AreEqual (7d, ((double?) new XElement (a)).Value, "a:double?:value");
  698. Assert.IsNotNull ((double?) new XElement (b), "b:double?:null");
  699. Assert.AreEqual (42d, ((double?) new XElement (b)).Value, "b:double?:value");
  700. Assert.IsNotNull ((double?) new XElement (c), "c:double?:null");
  701. Assert.AreEqual (13d, ((double?) new XElement (c)).Value, "c:double?:value");
  702. Assert.IsNotNull ((double?) new XElement (d), "d:double?:null");
  703. Assert.AreEqual (-101d, ((double?) new XElement (d)).Value, "d:double?:value");
  704. Assert.IsNotNull ((double?) new XElement (o), "o:double?:null");
  705. Assert.AreEqual (0d, ((double?) new XElement (o)).Value, "o:double?:value");
  706. Assert.IsNotNull ((double?) new XElement (l), "l:double?:null");
  707. Assert.AreEqual (1d, ((double?) new XElement (l)).Value, "l:double?:value");
  708. Assert.IsNotNull ((double?) new XElement (I), "I:double?:null");
  709. Assert.AreEqual (double.PositiveInfinity, ((double?) new XElement (I)).Value, "I:double?:value");
  710. Assert.IsNotNull ((double?) new XElement (i), "i:double?:null");
  711. Assert.AreEqual (double.PositiveInfinity, ((double?) new XElement (i)).Value, "i:double?:value");
  712. Assert.IsNotNull ((double?) new XElement (M), "M:double?:null");
  713. Assert.AreEqual (double.NegativeInfinity, ((double?) new XElement (M)).Value, "M:double?:value");
  714. Assert.IsNotNull ((double?) new XElement (m), "m:double?:null");
  715. Assert.AreEqual (double.NegativeInfinity, ((double?) new XElement (m)).Value, "m:double?:value");
  716. Assert.IsNotNull ((double?) new XElement (n), "n:double?:null");
  717. Assert.IsNaN (((double?) new XElement (n)).Value, "n:double?:value");
  718. Assert.IsNotNull ((float?) new XElement (a), "a:float?:null");
  719. Assert.AreEqual (7f, ((float?) new XElement (a)).Value, "a:float?:value");
  720. Assert.IsNotNull ((float?) new XElement (b), "b:float?:null");
  721. Assert.AreEqual (42f, ((float?) new XElement (b)).Value, "b:float?:value");
  722. Assert.IsNotNull ((float?) new XElement (c), "c:float?:null");
  723. Assert.AreEqual (13f, ((float?) new XElement (c)).Value, "c:float?:value");
  724. Assert.IsNotNull ((float?) new XElement (d), "d:float?:null");
  725. Assert.AreEqual (-101f, ((float?) new XElement (d)).Value, "d:float?:value");
  726. Assert.IsNotNull ((float?) new XElement (o), "o:float?:null");
  727. Assert.AreEqual (0f, ((float?) new XElement (o)).Value, "o:float?:value");
  728. Assert.IsNotNull ((float?) new XElement (l), "l:float?:null");
  729. Assert.AreEqual (1f, ((float?) new XElement (l)).Value, "l:float?:value");
  730. Assert.IsNotNull ((float?) new XElement (I), "I:float?:null");
  731. Assert.AreEqual (float.PositiveInfinity, ((float?) new XElement (I)).Value, "I:float?:value");
  732. Assert.IsNotNull ((float?) new XElement (i), "i:float?:null");
  733. Assert.AreEqual (float.PositiveInfinity, ((float?) new XElement (i)).Value, "i:float?:value");
  734. Assert.IsNotNull ((float?) new XElement (M), "M:float?:null");
  735. Assert.AreEqual (float.NegativeInfinity, ((float?) new XElement (M)).Value, "M:float?:value");
  736. Assert.IsNotNull ((float?) new XElement (m), "m:float?:null");
  737. Assert.AreEqual (float.NegativeInfinity, ((float?) new XElement (m)).Value, "m:float?:value");
  738. Assert.IsNotNull ((float?) new XElement (n), "n:float?:null");
  739. Assert.IsNaN (((float?) new XElement (n)).Value, "n:float?:value");
  740. AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (a); }, "a:Guid?");
  741. AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (b); }, "b:Guid?");
  742. AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (c); }, "c:Guid?");
  743. AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (d); }, "d:Guid?");
  744. AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (o); }, "o:Guid?");
  745. AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (l); }, "l:Guid?");
  746. AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (I); }, "I:Guid?");
  747. AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (i); }, "i:Guid?");
  748. AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (M); }, "M:Guid?");
  749. AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (m); }, "m:Guid?");
  750. AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (n); }, "n:Guid?");
  751. Assert.IsNotNull ((int?) new XElement (a), "a:int?:null");
  752. Assert.AreEqual (7, ((int?) new XElement (a)).Value, "a:int?:value");
  753. Assert.IsNotNull ((int?) new XElement (b), "b:int?:null");
  754. Assert.AreEqual (42, ((int?) new XElement (b)).Value, "b:int?:value");
  755. Assert.IsNotNull ((int?) new XElement (c), "c:int?:null");
  756. Assert.AreEqual (13, ((int?) new XElement (c)).Value, "c:int?:value");
  757. Assert.IsNotNull ((int?) new XElement (d), "d:int?:null");
  758. Assert.AreEqual (-101, ((int?) new XElement (d)).Value, "d:int?:value");
  759. Assert.IsNotNull ((int?) new XElement (o), "o:int?:null");
  760. Assert.AreEqual (0, ((int?) new XElement (o)).Value, "o:int?:value");
  761. Assert.IsNotNull ((int?) new XElement (l), "l:int?:null");
  762. Assert.AreEqual (1, ((int?) new XElement (l)).Value, "l:int?:value");
  763. AssertThrows<FormatException> (() => { int? z = (int?) new XElement (I); }, "I:int?");
  764. AssertThrows<FormatException> (() => { int? z = (int?) new XElement (i); }, "i:int?");
  765. AssertThrows<FormatException> (() => { int? z = (int?) new XElement (M); }, "M:int?");
  766. AssertThrows<FormatException> (() => { int? z = (int?) new XElement (m); }, "m:int?");
  767. AssertThrows<FormatException> (() => { int? z = (int?) new XElement (n); }, "n:int?");
  768. Assert.IsNotNull ((long?) new XElement (a), "a:long?:null");
  769. Assert.AreEqual (7L, ((long?) new XElement (a)).Value, "a:long?:value");
  770. Assert.IsNotNull ((long?) new XElement (b), "b:long?:null");
  771. Assert.AreEqual (42L, ((long?) new XElement (b)).Value, "b:long?:value");
  772. Assert.IsNotNull ((long?) new XElement (c), "c:long?:null");
  773. Assert.AreEqual (13L, ((long?) new XElement (c)).Value, "c:long?:value");
  774. Assert.IsNotNull ((long?) new XElement (d), "d:long?:null");
  775. Assert.AreEqual (-101L, ((long?) new XElement (d)).Value, "d:long?:value");
  776. Assert.IsNotNull ((long?) new XElement (o), "o:long?:null");
  777. Assert.AreEqual (0L, ((long?) new XElement (o)).Value, "o:long?:value");
  778. Assert.IsNotNull ((long?) new XElement (l), "l:long?:null");
  779. Assert.AreEqual (1L, ((long?) new XElement (l)).Value, "l:long?:value");
  780. AssertThrows<FormatException> (() => { long? z = (long?) new XElement (I); }, "I:long?");
  781. AssertThrows<FormatException> (() => { long? z = (long?) new XElement (i); }, "i:long?");
  782. AssertThrows<FormatException> (() => { long? z = (long?) new XElement (M); }, "M:long?");
  783. AssertThrows<FormatException> (() => { long? z = (long?) new XElement (m); }, "m:long?");
  784. AssertThrows<FormatException> (() => { long? z = (long?) new XElement (n); }, "n:long?");
  785. Assert.IsNotNull ((uint?) new XElement (a), "a:uint?:null");
  786. Assert.AreEqual (7u, ((uint?) new XElement (a)).Value, "a:uint?:value");
  787. Assert.IsNotNull ((uint?) new XElement (b), "b:uint?:null");
  788. Assert.AreEqual (42u, ((uint?) new XElement (b)).Value, "b:uint?:value");
  789. Assert.IsNotNull ((uint?) new XElement (c), "c:uint?:null");
  790. Assert.AreEqual (13u, ((uint?) new XElement (c)).Value, "c:uint?:value");
  791. // LAMESPEC: see XmlConvertTests.ToUInt32().
  792. //AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (d); }, "d:uint?");
  793. Assert.IsNotNull ((uint?) new XElement (o), "o:uint?:null");
  794. Assert.AreEqual (0u, ((uint?) new XElement (o)).Value, "o:uint?:value");
  795. Assert.IsNotNull ((uint?) new XElement (l), "l:uint?:null");
  796. Assert.AreEqual (1u, ((uint?) new XElement (l)).Value, "l:uint?:value");
  797. AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (I); }, "I:uint?");
  798. AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (i); }, "i:uint?");
  799. AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (M); }, "M:uint?");
  800. AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (m); }, "m:uint?");
  801. AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (n); }, "n:uint?");
  802. Assert.IsNotNull ((ulong?) new XElement (a), "a:ulong?:null");
  803. Assert.AreEqual (7UL, ((ulong?) new XElement (a)).Value, "a:ulong?:value");
  804. Assert.IsNotNull ((ulong?) new XElement (b), "b:ulong?:null");
  805. Assert.AreEqual (42UL, ((ulong?) new XElement (b)).Value, "b:ulong?:value");
  806. Assert.IsNotNull ((ulong?) new XElement (c), "c:ulong?:null");
  807. Assert.AreEqual (13UL, ((ulong?) new XElement (c)).Value, "c:ulong?:value");
  808. // LAMESPEC: see XmlConvertTests.ToUInt64().
  809. //AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (d); }, "d:ulong?");
  810. Assert.IsNotNull ((ulong?) new XElement (o), "o:ulong?:null");
  811. Assert.AreEqual (0UL, ((ulong?) new XElement (o)).Value, "o:ulong?:value");
  812. Assert.IsNotNull ((ulong?) new XElement (l), "l:ulong?:null");
  813. Assert.AreEqual (1UL, ((ulong?) new XElement (l)).Value, "l:ulong?:value");
  814. AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (I); }, "I:ulong?");
  815. AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (i); }, "i:ulong?");
  816. AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (M); }, "M:ulong?");
  817. AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (m); }, "m:ulong?");
  818. AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (n); }, "n:ulong?");
  819. AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (a); }, "a:TimeSpan?");
  820. AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (b); }, "b:TimeSpan?");
  821. AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (c); }, "c:TimeSpan?");
  822. AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (d); }, "d:TimeSpan?");
  823. AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (o); }, "o:TimeSpan?");
  824. AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (l); }, "l:TimeSpan?");
  825. AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (I); }, "I:TimeSpan?");
  826. AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (i); }, "i:TimeSpan?");
  827. AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (M); }, "M:TimeSpan?");
  828. AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (m); }, "m:TimeSpan?");
  829. AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (n); }, "n:TimeSpan?");
  830. Assert.AreEqual ("7", (string) new XElement (a), "a:string");
  831. Assert.AreEqual (" 42 ", (string) new XElement (b), "b:string");
  832. Assert.AreEqual (" \r\n 13 \t ", (string) new XElement (c), "c:string");
  833. Assert.AreEqual ("-101", (string) new XElement (d), "d:string");
  834. Assert.AreEqual ("0", (string) new XElement (o), "o:string");
  835. Assert.AreEqual ("1", (string) new XElement (l), "l:string");
  836. Assert.AreEqual ("INF", (string) new XElement (I), "I:string");
  837. Assert.AreEqual (" Infinity ", (string) new XElement (i), "i:string");
  838. Assert.AreEqual (" -INF ", (string) new XElement (M), "M:string");
  839. Assert.AreEqual ("-Infinity", (string) new XElement (m), "m:string");
  840. Assert.AreEqual ("\t NaN ", (string) new XElement (n), "n:string");
  841. AssertThrows<FormatException> (() => { bool z = (bool) new XElement (a); }, "a:bool");
  842. AssertThrows<FormatException> (() => { bool z = (bool) new XElement (b); }, "b:bool");
  843. AssertThrows<FormatException> (() => { bool z = (bool) new XElement (c); }, "c:bool");
  844. AssertThrows<FormatException> (() => { bool z = (bool) new XElement (d); }, "d:bool");
  845. Assert.AreEqual (false, (bool) new XElement (o), "o:bool");
  846. Assert.AreEqual (true, (bool) new XElement (l), "l:bool");
  847. AssertThrows<FormatException> (() => { bool z = (bool) new XElement (I); }, "I:bool");
  848. AssertThrows<FormatException> (() => { bool z = (bool) new XElement (i); }, "i:bool");
  849. AssertThrows<FormatException> (() => { bool z = (bool) new XElement (M); }, "M:bool");
  850. AssertThrows<FormatException> (() => { bool z = (bool) new XElement (m); }, "m:bool");
  851. AssertThrows<FormatException> (() => { bool z = (bool) new XElement (n); }, "n:bool");
  852. AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (a); }, "a:DateTime");
  853. AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (b); }, "b:DateTime");
  854. AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (c); }, "c:DateTime");
  855. AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (d); }, "d:DateTime");
  856. AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (o); }, "o:DateTime");
  857. AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (l); }, "l:DateTime");
  858. AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (I); }, "I:DateTime");
  859. AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (i); }, "i:DateTime");
  860. AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (M); }, "M:DateTime");
  861. AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (m); }, "m:DateTime");
  862. AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (n); }, "n:DateTime");
  863. AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (a); }, "a:DateTimeOffset");
  864. AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (b); }, "b:DateTimeOffset");
  865. AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (c); }, "c:DateTimeOffset");
  866. AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (d); }, "d:DateTimeOffset");
  867. AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (o); }, "o:DateTimeOffset");
  868. AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (l); }, "l:DateTimeOffset");
  869. AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (I); }, "I:DateTimeOffset");
  870. AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (i); }, "i:DateTimeOffset");
  871. AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (M); }, "M:DateTimeOffset");
  872. AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (m); }, "m:DateTimeOffset");
  873. AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (n); }, "n:DateTimeOffset");
  874. Assert.AreEqual (7m, (decimal) new XElement (a), "a:decimal");
  875. Assert.AreEqual (42m, (decimal) new XElement (b), "b:decimal");
  876. Assert.AreEqual (13m, (decimal) new XElement (c), "c:decimal");
  877. Assert.AreEqual (-101m, (decimal) new XElement (d), "d:decimal");
  878. Assert.AreEqual (0m, (decimal) new XElement (o), "o:decimal");
  879. Assert.AreEqual (1m, (decimal) new XElement (l), "l:decimal");
  880. AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (I); }, "I:decimal");
  881. AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (i); }, "i:decimal");
  882. AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (M); }, "M:decimal");
  883. AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (m); }, "m:decimal");
  884. AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (n); }, "n:decimal");
  885. Assert.AreEqual (7d, (double) new XElement (a), "a:double");
  886. Assert.AreEqual (42d, (double) new XElement (b), "b:double");
  887. Assert.AreEqual (13d, (double) new XElement (c), "c:double");
  888. Assert.AreEqual (-101d, (double) new XElement (d), "d:double");
  889. Assert.AreEqual (0d, (double) new XElement (o), "o:double");
  890. Assert.AreEqual (1d, (double) new XElement (l), "l:double");
  891. Assert.AreEqual (double.PositiveInfinity, (double) new XElement (I), "I:double");
  892. Assert.AreEqual (double.PositiveInfinity, (double) new XElement (i), "i:double");
  893. Assert.AreEqual (double.NegativeInfinity, (double) new XElement (M), "M:double");
  894. Assert.AreEqual (double.NegativeInfinity, (double) new XElement (m), "m:double");
  895. Assert.IsNaN (((double) new XElement (n)), "n:double");
  896. Assert.AreEqual (7f, (float) new XElement (a), "a:float");
  897. Assert.AreEqual (42f, (float) new XElement (b), "b:float");
  898. Assert.AreEqual (13f, (float) new XElement (c), "c:float");
  899. Assert.AreEqual (-101f, (float) new XElement (d), "d:float");
  900. Assert.AreEqual (0f, (float) new XElement (o), "o:float");
  901. Assert.AreEqual (1f, (float) new XElement (l), "l:float");
  902. Assert.AreEqual (float.PositiveInfinity, (float) new XElement (I), "I:float");
  903. Assert.AreEqual (float.PositiveInfinity, (float) new XElement (i), "i:float");
  904. Assert.AreEqual (float.NegativeInfinity, (float) new XElement (M), "M:float");
  905. Assert.AreEqual (float.NegativeInfinity, (float) new XElement (m), "m:float");
  906. Assert.IsNaN (((float) new XElement (n)), "n:float");
  907. AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (a); }, "a:Guid");
  908. AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (b); }, "b:Guid");
  909. AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (c); }, "c:Guid");
  910. AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (d); }, "d:Guid");
  911. AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (o); }, "o:Guid");
  912. AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (l); }, "l:Guid");
  913. AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (I); }, "I:Guid");
  914. AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (i); }, "i:Guid");
  915. AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (M); }, "M:Guid");
  916. AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (m); }, "m:Guid");
  917. AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (n); }, "n:Guid");
  918. Assert.AreEqual (7, (int) new XElement (a), "a:int");
  919. Assert.AreEqual (42, (int) new XElement (b), "b:int");
  920. Assert.AreEqual (13, (int) new XElement (c), "c:int");
  921. Assert.AreEqual (-101, (int) new XElement (d), "d:int");
  922. Assert.AreEqual (0, (int) new XElement (o), "o:int");
  923. Assert.AreEqual (1, (int) new XElement (l), "l:int");
  924. AssertThrows<FormatException> (() => { int z = (int) new XElement (I); }, "I:int");
  925. AssertThrows<FormatException> (() => { int z = (int) new XElement (i); }, "i:int");
  926. AssertThrows<FormatException> (() => { int z = (int) new XElement (M); }, "M:int");
  927. AssertThrows<FormatException> (() => { int z = (int) new XElement (m); }, "m:int");
  928. AssertThrows<FormatException> (() => { int z = (int) new XElement (n); }, "n:int");
  929. Assert.AreEqual (7L, (long) new XElement (a), "a:long");
  930. Assert.AreEqual (42L, (long) new XElement (b), "b:long");
  931. Assert.AreEqual (13L, (long) new XElement (c), "c:long");
  932. Assert.AreEqual (-101L, (long) new XElement (d), "d:long");
  933. Assert.AreEqual (0L, (long) new XElement (o), "o:long");
  934. Assert.AreEqual (1L, (long) new XElement (l), "l:long");
  935. AssertThrows<FormatException> (() => { long z = (long) new XElement (I); }, "I:long");
  936. AssertThrows<FormatException> (() => { long z = (long) new XElement (i); }, "i:long");
  937. AssertThrows<FormatException> (() => { long z = (long) new XElement (M); }, "M:long");
  938. AssertThrows<FormatException> (() => { long z = (long) new XElement (m); }, "m:long");
  939. AssertThrows<FormatException> (() => { long z = (long) new XElement (n); }, "n:long");
  940. Assert.AreEqual (7u, (uint) new XElement (a), "a:uint");
  941. Assert.AreEqual (42u, (uint) new XElement (b), "b:uint");
  942. Assert.AreEqual (13u, (uint) new XElement (c), "c:uint");
  943. // LAMESPEC: see XmlConvertTests.ToUInt32().
  944. //AssertThrows<FormatException> (() => { uint z = (uint) new XElement (d); }, "d:uint");
  945. Assert.AreEqual (0u, (uint) new XElement (o), "o:uint");
  946. Assert.AreEqual (1u, (uint) new XElement (l), "l:uint");
  947. AssertThrows<FormatException> (() => { uint z = (uint) new XElement (I); }, "I:uint");
  948. AssertThrows<FormatException> (() => { uint z = (uint) new XElement (i); }, "i:uint");
  949. AssertThrows<FormatException> (() => { uint z = (uint) new XElement (M); }, "M:uint");
  950. AssertThrows<FormatException> (() => { uint z = (uint) new XElement (m); }, "m:uint");
  951. AssertThrows<FormatException> (() => { uint z = (uint) new XElement (n); }, "n:uint");
  952. Assert.AreEqual (7UL, (ulong) new XElement (a), "a:ulong");
  953. Assert.AreEqual (42UL, (ulong) new XElement (b), "b:ulong");
  954. Assert.AreEqual (13UL, (ulong) new XElement (c), "c:ulong");
  955. // LAMESPEC: see XmlConvertTests.ToUInt64().
  956. //AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (d); }, "d:ulong");
  957. Assert.AreEqual (0UL, (ulong) new XElement (o), "o:ulong");
  958. Assert.AreEqual (1UL, (ulong) new XElement (l), "l:ulong");
  959. AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (I); }, "I:ulong");
  960. AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (i); }, "i:ulong");
  961. AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (M); }, "M:ulong");
  962. AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (m); }, "m:ulong");
  963. AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (n); }, "n:ulong");
  964. AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (a); }, "a:TimeSpan");
  965. AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (b); }, "b:TimeSpan");
  966. AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (c); }, "c:TimeSpan");
  967. AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (d); }, "d:TimeSpan");
  968. AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (o); }, "o:TimeSpan");
  969. AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (l); }, "l:TimeSpan");
  970. AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (I); }, "I:TimeSpan");
  971. AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (i); }, "i:TimeSpan");
  972. AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (M); }, "M:TimeSpan");
  973. AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (m); }, "m:TimeSpan");
  974. AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (n); }, "n:TimeSpan");
  975. // Perform some round-trip tests with numbers
  976. XElement x;
  977. const decimal @decimal = -41051609414188012238960097189m;
  978. const double @double = 8.5506609919892972E+307d;
  979. const float @float = -1.70151961E+37f;
  980. const int @int = -1051251773;
  981. const long @long = 4596767133891939716L;
  982. const uint @uint = 4106628142u;
  983. const ulong @ulong = 10713797297298255927UL;
  984. x = new XElement ("x", @decimal);
  985. Assert.IsNotNull ((decimal?) new XElement (x), "x:decimal?:null");
  986. Assert.AreEqual (@decimal, ((decimal?) new XElement (x)).Value, "x:decimal?:value");
  987. Assert.AreEqual (@decimal, (decimal) new XElement (x), "x:decimal");
  988. x = new XElement ("x", @double);
  989. Assert.IsNotNull ((double?) new XElement (x), "x:double?:null");
  990. Assert.AreEqual (@double, ((double?) new XElement (x)).Value, "x:double?:value");
  991. Assert.AreEqual (@double, (double) new XElement (x), "x:double");
  992. x = new XElement ("x", @float);
  993. Assert.IsNotNull ((float?) new XElement (x), "x:float?:null");
  994. Assert.AreEqual (@float, ((float?) new XElement (x)).Value, "x:float?:value");
  995. Assert.AreEqual (@float, (float) new XElement (x), "x:float");
  996. x = new XElement ("x", @int);
  997. Assert.IsNotNull ((int?) new XElement (x), "x:int?:null");
  998. Assert.AreEqual (@int, ((int?) new XElement (x)).Value, "x:int?:value");
  999. Assert.AreEqual (@int, (int) new XElement (x), "x:int");
  1000. x = new XElement ("x", @long);
  1001. Assert.IsNotNull ((long?) new XElement (x), "x:long?:null");
  1002. Assert.AreEqual (@long, ((long?) new XElement (x)).Value, "x:long?:value");
  1003. Assert.AreEqual (@long, (long) new XElement (x), "x:long");
  1004. x = new XElement ("x", @uint);
  1005. Assert.IsNotNull ((uint?) new XElement (x), "x:uint?:null");
  1006. Assert.AreEqual (@uint, ((uint?) new XElement (x)).Value, "x:uint?:value");
  1007. Assert.AreEqual (@uint, (uint) new XElement (x), "x:uint");
  1008. x = new XElement ("x", @ulong);
  1009. Assert.IsNotNull ((ulong?) new XElement (x), "x:ulong?:null");
  1010. Assert.AreEqual (@ulong, ((ulong?) new XElement (x)).Value, "x:ulong?:value");
  1011. Assert.AreEqual (@ulong, (ulong) new XElement (x), "x:ulong");
  1012. x = new XElement ("x", double.NaN);
  1013. Assert.IsNotNull ((double?) new XElement (x), "NaN:double?:null");
  1014. Assert.AreEqual (double.NaN, ((double?) new XElement (x)).Value, "NaN:double?:value");
  1015. Assert.AreEqual (double.NaN, (double) new XElement (x), "NaN:double");
  1016. x = new XElement ("x", float.NaN);
  1017. Assert.IsNotNull ((float?) new XElement (x), "NaN:float?:null");
  1018. Assert.AreEqual (float.NaN, ((float?) new XElement (x)).Value, "NaN:float?:value");
  1019. Assert.AreEqual (float.NaN, (float) new XElement (x), "NaN:float");
  1020. x = new XElement ("x", double.PositiveInfinity);
  1021. Assert.IsNotNull ((double?) new XElement (x), "+Inf:double?:null");
  1022. Assert.AreEqual (double.PositiveInfinity, ((double?) new XElement (x)).Value, "+Inf:double?:value");
  1023. Assert.AreEqual (double.PositiveInfinity, (double) new XElement (x), "+Inf:double");
  1024. x = new XElement ("x", float.PositiveInfinity);
  1025. Assert.IsNotNull ((float?) new XElement (x), "+Inf:float?:null");
  1026. Assert.AreEqual (float.PositiveInfinity, ((float?) new XElement (x)).Value, "+Inf:float?:value");
  1027. Assert.AreEqual (float.PositiveInfinity, (float) new XElement (x), "+Inf:float");
  1028. x = new XElement ("x", double.NegativeInfinity);
  1029. Assert.IsNotNull ((double?) new XElement (x), "-Inf:double?:null");
  1030. Assert.AreEqual (double.NegativeInfinity, ((double?) new XElement (x)).Value, "-Inf:double?:value");
  1031. Assert.AreEqual (double.NegativeInfinity, (double) new XElement (x), "-Inf:double");
  1032. x = new XElement ("x", float.NegativeInfinity);
  1033. Assert.IsNotNull ((float?) new XElement (x), "-Inf:float?:null");
  1034. Assert.AreEqual (float.NegativeInfinity, ((float?) new XElement (x)).Value, "-Inf:float?:value");
  1035. Assert.AreEqual (float.NegativeInfinity, (float) new XElement (x), "-Inf:float");
  1036. // Perform overflow tests with numbers
  1037. AssertThrows<OverflowException> (() => { decimal z = (decimal) new XElement ("z", "91051609414188012238960097189"); }, "z:decimal");
  1038. AssertThrows<OverflowException> (() => { decimal? z = (decimal?) new XElement ("z", "91051609414188012238960097189"); }, "z:decimal?");
  1039. AssertThrows<OverflowException> (() => { double z = (double) new XElement ("z", "8.5506609919892972E+654"); }, "z:double");
  1040. AssertThrows<OverflowException> (() => { double? z = (double?) new XElement ("z", "8.5506609919892972E+654"); }, "z:double?");
  1041. AssertThrows<OverflowException> (() => { float z = (float) new XElement ("z", @double); }, "z:float");
  1042. AssertThrows<OverflowException> (() => { float? z = (float?) new XElement ("z", @double); }, "z:float?");
  1043. AssertThrows<OverflowException> (() => { int z = (int) new XElement ("z", @long); }, "z:int");
  1044. AssertThrows<OverflowException> (() => { int? z = (int?) new XElement ("z", @long); }, "z:int?");
  1045. AssertThrows<OverflowException> (() => { long z = (long) new XElement ("z", @decimal); }, "z:long");
  1046. AssertThrows<OverflowException> (() => { long? z = (long?) new XElement ("z", @decimal); }, "z:long?");
  1047. AssertThrows<OverflowException> (() => { uint z = (uint) new XElement ("z", @ulong); }, "z:uint");
  1048. AssertThrows<OverflowException> (() => { uint? z = (uint?) new XElement ("z", @ulong); }, "z:uint?");
  1049. AssertThrows<OverflowException> (() => { ulong z = (ulong) new XElement ("z", -@decimal); }, "z:ulong");
  1050. AssertThrows<OverflowException> (() => { ulong? z = (ulong?) new XElement ("z", -@decimal); }, "z:ulong?");
  1051. }
  1052. [Test]
  1053. public void CastExtremes ()
  1054. {
  1055. // Test extremes/constants where round-trips should work in specific ways
  1056. Assert.AreEqual (decimal.MaxValue, (decimal) new XElement ("k", decimal.MaxValue), "MaxValue:decimal");
  1057. Assert.AreEqual (decimal.MinValue, (decimal) new XElement ("k", decimal.MinValue), "MinValue:decimal");
  1058. Assert.AreEqual (decimal.MinusOne, (decimal) new XElement ("k", decimal.MinusOne), "MinusOne:decimal");
  1059. Assert.AreEqual (decimal.One, (decimal) new XElement ("k", decimal.One), "One:decimal");
  1060. Assert.AreEqual (decimal.Zero, (decimal) new XElement ("k", decimal.Zero), "Zero:decimal");
  1061. Assert.AreEqual (double.MaxValue, (double) new XElement ("k", double.MaxValue), "MaxValue:double");
  1062. Assert.AreEqual (double.MinValue, (double) new XElement ("k", double.MinValue), "MinValue:double");
  1063. Assert.AreEqual (double.Epsilon, (double) new XElement ("k", double.Epsilon), "Epsilon:double");
  1064. Assert.AreEqual (double.NaN, (double) new XElement ("k", double.NaN), "NaN:double");
  1065. Assert.AreEqual (double.NegativeInfinity, (double) new XElement ("k", double.NegativeInfinity), "-Inf:double");
  1066. Assert.AreEqual (double.PositiveInfinity, (double) new XElement ("k", double.PositiveInfinity), "+Inf:double");
  1067. Assert.AreEqual (float.MaxValue, (float) new XElement ("k", float.MaxValue), "MaxValue:float");
  1068. Assert.AreEqual (float.MinValue, (float) new XElement ("k", float.MinValue), "MinValue:float");
  1069. Assert.AreEqual (float.Epsilon, (float) new XElement ("k", float.Epsilon), "Epsilon:float");
  1070. Assert.AreEqual (float.NaN, (float) new XElement ("k", float.NaN), "NaN:float");
  1071. Assert.AreEqual (float.NegativeInfinity, (float) new XElement ("k", float.NegativeInfinity), "-Inf:float");
  1072. Assert.AreEqual (float.PositiveInfinity, (float) new XElement ("k", float.PositiveInfinity), "+Inf:float");
  1073. Assert.AreEqual (int.MaxValue, (int) new XElement ("k", int.MaxValue), "MaxValue:int");
  1074. Assert.AreEqual (int.MinValue, (int) new XElement ("k", int.MinValue), "MinValue:int");
  1075. Assert.AreEqual (long.MaxValue, (long) new XElement ("k", long.MaxValue), "MaxValue:long");
  1076. Assert.AreEqual (long.MinValue, (long) new XElement ("k", long.MinValue), "MinValue:long");
  1077. Assert.AreEqual (uint.MaxValue, (uint) new XElement ("k", uint.MaxValue), "MaxValue:uint");
  1078. Assert.AreEqual (uint.MinValue, (uint) new XElement ("k", uint.MinValue), "MinValue:uint");
  1079. Assert.AreEqual (ulong.MaxValue, (ulong) new XElement ("k", ulong.MaxValue), "MaxValue:ulong");
  1080. Assert.AreEqual (ulong.MinValue, (ulong) new XElement ("k", ulong.MinValue), "MinValue:ulong");
  1081. Assert.AreEqual (decimal.MaxValue, (decimal?) new XElement ("k", decimal.MaxValue), "MaxValue:decimal?");
  1082. Assert.AreEqual (decimal.MinValue, (decimal?) new XElement ("k", decimal.MinValue), "MinValue:decimal?");
  1083. Assert.AreEqual (decimal.MinusOne, (decimal?) new XElement ("k", decimal.MinusOne), "MinusOne:decimal?");
  1084. Assert.AreEqual (decimal.One, (decimal?) new XElement ("k", decimal.One), "One:decimal?");
  1085. Assert.AreEqual (decimal.Zero, (decimal?) new XElement ("k", decimal.Zero), "Zero:decimal?");
  1086. Assert.AreEqual (double.MaxValue, (double?) new XElement ("k", double.MaxValue), "MaxValue:double?");
  1087. Assert.AreEqual (double.MinValue, (double?) new XElement ("k", double.MinValue), "MinValue:double?");
  1088. Assert.AreEqual (double.Epsilon, (double?) new XElement ("k", double.Epsilon), "Epsilon:double?");
  1089. Assert.AreEqual (double.NaN, (double?) new XElement ("k", double.NaN), "NaN:double?");
  1090. Assert.AreEqual (double.NegativeInfinity, (double?) new XElement ("k", double.NegativeInfinity), "-Inf:double?");
  1091. Assert.AreEqual (double.PositiveInfinity, (double?) new XElement ("k", double.PositiveInfinity), "+Inf:double?");
  1092. Assert.AreEqual (float.MaxValue, (float?) new XElement ("k", float.MaxValue), "MaxValue:float?");
  1093. Assert.AreEqual (float.MinValue, (float?) new XElement ("k", float.MinValue), "MinValue:float?");
  1094. Assert.AreEqual (float.Epsilon, (float?) new XElement ("k", float.Epsilon), "Epsilon:float?");
  1095. Assert.AreEqual (float.NaN, (float?) new XElement ("k", float.NaN), "NaN:float?");
  1096. Assert.AreEqual (float.NegativeInfinity, (float?) new XElement ("k", float.NegativeInfinity), "-Inf:float?");
  1097. Assert.AreEqual (float.PositiveInfinity, (float?) new XElement ("k", float.PositiveInfinity), "+Inf:float?");
  1098. Assert.AreEqual (int.MaxValue, (int?) new XElement ("k", int.MaxValue), "MaxValue:int?");
  1099. Assert.AreEqual (int.MinValue, (int?) new XElement ("k", int.MinValue), "MinValue:int?");
  1100. Assert.AreEqual (long.MaxValue, (long?) new XElement ("k", long.MaxValue), "MaxValue:long?");
  1101. Assert.AreEqual (long.MinValue, (long?) new XElement ("k", long.MinValue), "MinValue:long?");
  1102. Assert.AreEqual (uint.MaxValue, (uint?) new XElement ("k", uint.MaxValue), "MaxValue:uint?");
  1103. Assert.AreEqual (uint.MinValue, (uint?) new XElement ("k", uint.MinValue), "MinValue:uint?");
  1104. Assert.AreEqual (ulong.MaxValue, (ulong?) new XElement ("k", ulong.MaxValue), "MaxValue:ulong?");
  1105. Assert.AreEqual (ulong.MinValue, (ulong?) new XElement ("k", ulong.MinValue), "MinValue:ulong?");
  1106. Assert.AreEqual (DateTime.MaxValue, (DateTime) new XElement ("k", DateTime.MaxValue), "MaxValue:DateTime");
  1107. Assert.AreEqual (DateTime.MinValue, (DateTime) new XElement ("k", DateTime.MinValue), "MinValue:DateTime");
  1108. Assert.AreEqual (DateTime.MaxValue, (DateTime?) new XElement ("k", DateTime.MaxValue), "MaxValue:DateTime?");
  1109. Assert.AreEqual (DateTime.MinValue, (DateTime?) new XElement ("k", DateTime.MinValue), "MinValue:DateTime?");
  1110. Assert.AreEqual (DateTimeOffset.MaxValue, (DateTimeOffset) new XElement ("k", DateTimeOffset.MaxValue), "MaxValue:DateTimeOffset");
  1111. Assert.AreEqual (DateTimeOffset.MinValue, (DateTimeOffset) new XElement ("k", DateTimeOffset.MinValue), "MinValue:DateTimeOffset");
  1112. Assert.AreEqual (DateTimeOffset.MaxValue, (DateTimeOffset?) new XElement ("k", DateTimeOffset.MaxValue), "MaxValue:DateTimeOffset?");
  1113. Assert.AreEqual (DateTimeOffset.MinValue, (DateTimeOffset?) new XElement ("k", DateTimeOffset.MinValue), "MinValue:DateTimeOffset?");
  1114. Assert.AreEqual (TimeSpan.MaxValue, (TimeSpan) new XElement ("k", TimeSpan.MaxValue), "MaxValue:TimeSpan");
  1115. Assert.AreEqual (TimeSpan.MinValue, (TimeSpan) new XElement ("k", TimeSpan.MinValue), "MinValue:TimeSpan");
  1116. Assert.AreEqual (TimeSpan.MaxValue, (TimeSpan?) new XElement ("k", TimeSpan.MaxValue), "MaxValue:TimeSpan?");
  1117. Assert.AreEqual (TimeSpan.MinValue, (TimeSpan?) new XElement ("k", TimeSpan.MinValue), "MinValue:TimeSpan?");
  1118. }
  1119. [Test]
  1120. public void CastBooleans ()
  1121. {
  1122. Assert.IsNotNull ((bool?) new XElement ("fq", "false"), "#1a");
  1123. Assert.AreEqual (false, ((bool?) new XElement ("fq", "false")).Value, "#1b");
  1124. Assert.IsNotNull ((bool?) new XElement ("tq", "true"), "#2a");
  1125. Assert.AreEqual (true, ((bool?) new XElement ("tq", "true")).Value, "#2b");
  1126. Assert.IsNotNull ((bool?) new XElement ("Fq", "False"), "#3a");
  1127. Assert.AreEqual (false, ((bool?) new XElement ("Fq", "False")).Value, "#3b");
  1128. Assert.IsNotNull ((bool?) new XElement ("Tq", "True"), "#4a");
  1129. Assert.AreEqual (true, ((bool?) new XElement ("Tq", "True")).Value, "#4b");
  1130. Assert.IsNotNull ((bool?) new XElement ("Fs", " False \t \r "), "#5a");
  1131. Assert.AreEqual (false, ((bool?) new XElement ("Fs", " False \t \r ")).Value, "#5b");
  1132. Assert.IsNotNull ((bool?) new XElement ("Ts", " \t True \n "), "#6a");
  1133. Assert.AreEqual (true, ((bool?) new XElement ("Ts", " \t True \n ")).Value, "#6b");
  1134. Assert.AreEqual (false, (bool) new XElement ("f", "false"), "#7");
  1135. Assert.AreEqual (true, (bool) new XElement ("t", "true"), "#8");
  1136. Assert.AreEqual (false, (bool) new XElement ("F", "False"), "#9");
  1137. Assert.AreEqual (true, (bool) new XElement ("T", "True"), "#10");
  1138. Assert.AreEqual (false, (bool)new XElement ("fs", " false "), "#11");
  1139. Assert.AreEqual (true, (bool)new XElement ("ts", " true "), "#12");
  1140. Assert.IsNotNull ((bool?) new XElement ("Tc", new XCData (" \t True \n ")), "#13a");
  1141. Assert.AreEqual (true, ((bool?) new XElement ("Tc", new XCData (" \t True \n "))).Value, "#13b");
  1142. Assert.AreEqual (false, (bool)new XElement ("fc", new XCData (" false ")), "#14");
  1143. Assert.IsNotNull ((bool?) new XElement ("x", true), "#15a");
  1144. Assert.IsTrue (((bool?) new XElement ("x", true)).Value, "#15b");
  1145. Assert.IsTrue ((bool) new XElement ("x", true), "#15c");
  1146. Assert.IsNotNull ((bool?) new XElement ("x", false), "#16a");
  1147. Assert.IsFalse (((bool?) new XElement ("x", false)).Value, "#16b");
  1148. Assert.IsFalse ((bool) new XElement ("x", false), "#16c");
  1149. Assert.IsTrue ((bool) new XElement ("x", bool.TrueString), "#17a");
  1150. Assert.IsFalse ((bool) new XElement ("x", bool.FalseString), "#17b");
  1151. Assert.IsTrue ((bool) new XElement ("x", new XCData (bool.TrueString)), "#18a");
  1152. Assert.IsFalse ((bool) new XElement ("x", new XCData (bool.FalseString)), "#18b");
  1153. }
  1154. [Test]
  1155. public void CastGuids ()
  1156. {
  1157. Guid rb = new Guid (new byte[16] { 0x9A, 0xBF, 0xCE, 0x7E, 0x07, 0x29, 0x9C, 0x43, 0x80, 0x7D, 0x48, 0x20, 0xB9, 0x19, 0xEA, 0x57 });
  1158. Guid rd = new Guid (new byte[16] { 0x21, 0x5B, 0x57, 0x26, 0xCD, 0x14, 0x5E, 0x44, 0x8F, 0xFA, 0xE2, 0xBC, 0x24, 0x7B, 0x2E, 0xC9 });
  1159. Guid rn = new Guid (new byte[16] { 0xF9, 0x46, 0x41, 0xA8, 0xA5, 0x03, 0xF1, 0x4A, 0xAD, 0x97, 0x7B, 0xC7, 0x79, 0x57, 0x2B, 0x79 });
  1160. Guid rp = new Guid (new byte[16] { 0x51, 0x6B, 0x8A, 0x17, 0xEF, 0x11, 0xFB, 0x48, 0x83, 0xBD, 0x57, 0xB4, 0x99, 0xF9, 0xC1, 0xE6 });
  1161. Guid rz = Guid.Empty;
  1162. Guid rx = Guid.NewGuid ();
  1163. XElement b = new XElement ("b", " {7ECEBF9A-2907-439c-807D-4820B919EA57}");
  1164. XElement d = new XElement ("d", "26575b21-14cd-445e-8ffa-e2bc247b2ec9");
  1165. XElement n = new XElement ("n", "a84146f903A54af1ad977bC779572b79\r\n");
  1166. XElement p = new XElement ("p", " (178a6b51-11ef-48fb-83bd-57b499f9c1e6) \t ");
  1167. XElement z = new XElement ("z", " \t \n 00000000-0000-0000-0000-000000000000 ");
  1168. XElement x = new XElement ("x", rx);
  1169. Assert.IsNotNull ((Guid?) new XElement (b), "#1a");
  1170. Assert.AreEqual (rb, ((Guid?) new XElement (b)).Value, "#1b");
  1171. Assert.AreEqual (rb, (Guid) new XElement (b), "#1c");
  1172. Assert.AreEqual (rb, (Guid) new XElement ("r", rb), "#1d");
  1173. Assert.IsNotNull ((Guid?) new XElement ("r", rb), "#1e");
  1174. Assert.AreEqual (rb, ((Guid?) new XElement ("r", rb)).Value, "#1f");
  1175. Assert.IsNotNull ((Guid?) new XElement (d), "#2a");
  1176. Assert.AreEqual (rd, ((Guid?) new XElement (d)).Value, "#2b");
  1177. Assert.AreEqual (rd, (Guid) new XElement (d), "#2c");
  1178. Assert.AreEqual (rd, (Guid) new XElement ("r", rd), "#2d");
  1179. Assert.IsNotNull ((Guid?) new XElement ("r", rd), "#2e");
  1180. Assert.AreEqual (rd, ((Guid?) new XElement ("r", rd)).Value, "#2f");
  1181. Assert.IsNotNull ((Guid?) new XElement (n), "#3a");
  1182. Assert.AreEqual (rn, ((Guid?) new XElement (n)).Value, "#3b");
  1183. Assert.AreEqual (rn, (Guid) new XElement (n), "#3c");
  1184. Assert.AreEqual (rn, (Guid) new XElement ("r", rn), "#3d");
  1185. Assert.IsNotNull ((Guid?) new XElement ("r", rn), "#3e");
  1186. Assert.AreEqual (rn, ((Guid?) new XElement ("r", rn)).Value, "#3f");
  1187. Assert.IsNotNull ((Guid?) new XElement (p), "#4a");
  1188. Assert.AreEqual (rp, ((Guid?) new XElement (p)).Value, "#4b");
  1189. Assert.AreEqual (rp, (Guid) new XElement (p), "#4c");
  1190. Assert.AreEqual (rp, (Guid) new XElement ("r", rp), "#4d");
  1191. Assert.IsNotNull ((Guid?) new XElement ("r", rp), "#4e");
  1192. Assert.AreEqual (rp, ((Guid?) new XElement ("r", rp)).Value, "#4f");
  1193. Assert.IsNotNull ((Guid?) new XElement (z), "#5a");
  1194. Assert.AreEqual (rz, ((Guid?) new XElement (z)).Value, "#5b");
  1195. Assert.AreEqual (rz, (Guid) new XElement (z), "#5c");
  1196. Assert.IsNotNull ((Guid?) new XElement (x), "#6a");
  1197. Assert.AreEqual (rx, ((Guid?) new XElement (x)).Value, "#6b");
  1198. Assert.AreEqual (rx, (Guid) new XElement (x), "#6c");
  1199. }
  1200. [Test]
  1201. public void CastDateTimes ()
  1202. {
  1203. DateTime ra = new DateTime (1987, 1, 23, 21, 45, 36, 89, DateTimeKind.Unspecified);
  1204. DateTime rb = new DateTime (2001, 2, 3, 4, 5, 6, 789, DateTimeKind.Local);
  1205. DateTime rc = new DateTime (2010, 1, 2, 0, 0, 0, 0, DateTimeKind.Utc);
  1206. DateTime rd = new DateTime (1956, 11, 2, 0, 34, 0);
  1207. DateTime re = new DateTime (635085111683456297L, DateTimeKind.Utc);
  1208. DateTime rf = re.ToLocalTime ();
  1209. DateTime rx = DateTime.Now;
  1210. DateTime rz = DateTime.UtcNow;
  1211. XElement a = new XElement ("a", "1987-01-23T21:45:36.089");
  1212. XElement b = new XElement ("b", "2001-02-03T04:05:06.789" + DateTime.Now.ToString ("zzz"));
  1213. XElement c = new XElement ("c", "2010-01-02T00:00:00Z");
  1214. XElement d = new XElement ("d", " Nov 2, 1956 12:34 AM \r\n \t");
  1215. XElement e = new XElement ("e", " 2013-07-04T05:06:08.3456297Z "); // UTC, all the way
  1216. XElement f = new XElement ("f", " 2013-07-04T05:06:08.3456297+00:00 "); // UTC initially, but should be converted automatically to local time
  1217. XElement x = new XElement ("x", rx);
  1218. XElement z = new XElement ("z", rz);
  1219. Assert.IsNotNull ((DateTime?) new XElement (a), "#1a");
  1220. Assert.AreEqual (ra, ((DateTime?) new XElement (a)).Value, "#1b");
  1221. Assert.AreEqual (ra, (DateTime) new XElement (a), "#1c");
  1222. Assert.AreEqual (ra, (DateTime) new XElement ("r", ra), "#1d");
  1223. Assert.IsNotNull ((DateTime?) new XElement ("r", ra), "#1e");
  1224. Assert.AreEqual (ra, ((DateTime?) new XElement ("r", ra)).Value, "#1f");
  1225. Assert.IsNotNull ((DateTime?) new XElement (b), "#2a");
  1226. Assert.AreEqual (rb, ((DateTime?) new XElement (b)).Value, "#2b");
  1227. Assert.AreEqual (rb, (DateTime) new XElement (b), "#2c");
  1228. Assert.AreEqual (rb, (DateTime) new XElement ("r", rb), "#2d");
  1229. Assert.IsNotNull ((DateTime?) new XElement ("r", rb), "#2e");
  1230. Assert.AreEqual (rb, ((DateTime?) new XElement ("r", rb)).Value, "#2f");
  1231. Assert.IsNotNull ((DateTime?) new XElement (c), "#3a");
  1232. Assert.AreEqual (rc, ((DateTime?) new XElement (c)).Value, "#3b");
  1233. Assert.AreEqual (rc, (DateTime) new XElement (c), "#3c");
  1234. Assert.AreEqual (rc, (DateTime) new XElement ("r", rc), "#3d");
  1235. Assert.IsNotNull ((DateTime?) new XElement ("r", rc), "#3e");
  1236. Assert.AreEqual (rc, ((DateTime?) new XElement ("r", rc)).Value, "#3f");
  1237. Assert.IsNotNull ((DateTime?) new XElement (d), "#4a");
  1238. Assert.AreEqual (rd, ((DateTime?) new XElement (d)).Value, "#4b");
  1239. Assert.AreEqual (rd, (DateTime) new XElement (d), "#4c");
  1240. Assert.AreEqual (rd, (DateTime) new XElement ("r", rd), "#4d");
  1241. Assert.IsNotNull ((DateTime?) new XElement ("r", rd), "#4e");
  1242. Assert.AreEqual (rd, ((DateTime?) new XElement ("r", rd)).Value, "#4f");
  1243. Assert.IsNotNull ((DateTime?) new XElement (x), "#5a");
  1244. Assert.AreEqual (rx, ((DateTime?) new XElement (x)).Value, "#5b");
  1245. Assert.AreEqual (rx, (DateTime) new XElement (x), "#5c");
  1246. Assert.IsNotNull ((DateTime?) new XElement (z), "#6a");
  1247. Assert.AreEqual (rz, ((DateTime?) new XElement (z)).Value, "#6b");
  1248. Assert.AreEqual (rz, (DateTime) new XElement (z), "#6c");
  1249. Assert.IsNotNull ((DateTime?) new XElement (e), "#7a");
  1250. Assert.AreEqual (re, ((DateTime?) new XElement (e)).Value, "#7b");
  1251. Assert.AreEqual (re, (DateTime) new XElement (e), "#7c");
  1252. Assert.AreEqual (re, (DateTime) new XElement ("r", re), "#7d");
  1253. Assert.IsNotNull ((DateTime?) new XElement ("r", re), "#7e");
  1254. Assert.AreEqual (re, ((DateTime?) new XElement ("r", re)).Value, "#7f");
  1255. Assert.IsNotNull ((DateTime?) new XElement (f), "#8a");
  1256. Assert.AreEqual (rf, ((DateTime?) new XElement (f)).Value, "#8b");
  1257. Assert.AreEqual (rf, (DateTime) new XElement (f), "#8c");
  1258. Assert.AreEqual (rf, (DateTime) new XElement ("r", rf), "#8d");
  1259. Assert.IsNotNull ((DateTime?) new XElement ("r", rf), "#8e");
  1260. Assert.AreEqual (rf, ((DateTime?) new XElement ("r", rf)).Value, "#8f");
  1261. }
  1262. [Test]
  1263. public void CastDateTimeOffsets ()
  1264. {
  1265. DateTimeOffset ra = new DateTimeOffset (1987, 1, 23, 21, 45, 36, 89, TimeSpan.FromHours (+13.75)); // e.g., Chatham Islands (daylight-savings time)
  1266. DateTimeOffset rb = new DateTimeOffset (2001, 2, 3, 4, 5, 6, 789, DateTimeOffset.Now.Offset); // Local time
  1267. DateTimeOffset rc = new DateTimeOffset (2010, 1, 2, 0, 0, 0, 0, TimeSpan.Zero); // UTC
  1268. DateTimeOffset rd = new DateTimeOffset (1956, 11, 2, 12, 34, 10, TimeSpan.FromHours (-3.5));
  1269. DateTimeOffset re = new DateTimeOffset (630646468235678363, TimeSpan.FromHours (-1)); // UTC-1, also with full resolution and a fractional second that might lose a tick on Mono 2.6.1
  1270. DateTimeOffset rx = DateTimeOffset.Now;
  1271. DateTimeOffset rz = DateTimeOffset.UtcNow;
  1272. XElement a = new XElement ("a", "1987-01-23T21:45:36.089+13:45");
  1273. XElement b = new XElement ("b", "2001-02-03T04:05:06.789" + DateTimeOffset.Now.ToString ("zzz"));
  1274. XElement c = new XElement ("c", "2010-01-02T00:00:00Z");
  1275. XElement d = new XElement ("d", " Nov 2, 1956 12:34:10 PM -3:30 \r\n \t");
  1276. XElement e = new XElement ("e", " \t \n 1999-06-10T21:27:03.5678363-01:00 ");
  1277. XElement x = new XElement ("x", rx);
  1278. XElement z = new XElement ("z", rz);
  1279. Assert.IsNotNull ((DateTimeOffset?) new XElement (a), "#1a");
  1280. Assert.AreEqual (ra, ((DateTimeOffset?) new XElement (a)).Value, "#1b");
  1281. Assert.AreEqual (ra, (DateTimeOffset) new XElement (a), "#1c");
  1282. Assert.AreEqual (ra, (DateTimeOffset) new XElement ("r", ra), "#1d");
  1283. Assert.IsNotNull ((DateTimeOffset?) new XElement ("r", ra), "#1e");
  1284. Assert.AreEqual (ra, ((DateTimeOffset?) new XElement ("r", ra)).Value, "#1f");
  1285. Assert.IsNotNull ((DateTimeOffset?) new XElement (b), "#2a");
  1286. Assert.AreEqual (rb, ((DateTimeOffset?) new XElement (b)).Value, "#2b");
  1287. Assert.AreEqual (rb, (DateTimeOffset) new XElement (b), "#2c");
  1288. Assert.AreEqual (rb, (DateTimeOffset) new XElement ("r", rb), "#2d");
  1289. Assert.IsNotNull ((DateTimeOffset?) new XElement ("r", rb), "#2e");
  1290. Assert.AreEqual (rb, ((DateTimeOffset?) new XElement ("r", rb)).Value, "#2f");
  1291. Assert.IsNotNull ((DateTimeOffset?) new XElement (c), "#3a");
  1292. Assert.AreEqual (rc, ((DateTimeOffset?) new XElement (c)).Value, "#3b");
  1293. Assert.AreEqual (rc, (DateTimeOffset) new XElement (c), "#3c");
  1294. Assert.AreEqual (rc, (DateTimeOffset) new XElement ("r", rc), "#3d");
  1295. Assert.IsNotNull ((DateTimeOffset?) new XElement ("r", rc), "#3e");
  1296. Assert.AreEqual (rc, ((DateTimeOffset?) new XElement ("r", rc)).Value, "#3f");
  1297. AssertThrows<FormatException> (() => { DateTimeOffset? r = (DateTimeOffset?) new XElement (d); }, "#4a");
  1298. AssertThrows<FormatException> (() => { DateTimeOffset r = (DateTimeOffset) new XElement (d); }, "#4b");
  1299. Assert.AreEqual (rd, DateTimeOffset.Parse (d.Value), "#4c"); // Sanity check: Okay for standalone DateTimeOffset but not as XML as in above
  1300. Assert.IsNotNull ((DateTimeOffset?) new XElement (x), "#5a");
  1301. Assert.AreEqual (rx, ((DateTimeOffset?) new XElement (x)).Value, "#5b");
  1302. Assert.AreEqual (rx, (DateTimeOffset) new XElement (x), "#5c");
  1303. Assert.IsNotNull ((DateTimeOffset?) new XElement (z), "#6a");
  1304. Assert.AreEqual (rz, ((DateTimeOffset?) new XElement (z)).Value, "#6b");
  1305. Assert.AreEqual (rz, (DateTimeOffset) new XElement (z), "#6c");
  1306. Assert.IsNotNull ((DateTimeOffset?) new XElement (e), "#7a");
  1307. Assert.AreEqual (re, ((DateTimeOffset?) new XElement (e)).Value, "#7b");
  1308. Assert.AreEqual (re, (DateTimeOffset) new XElement (e), "#7c");
  1309. Assert.AreEqual (re, (DateTimeOffset) new XElement ("r", re), "#7d");
  1310. Assert.IsNotNull ((DateTimeOffset?) new XElement ("r", re), "#7e");
  1311. Assert.AreEqual (re, ((DateTimeOffset?) new XElement ("r", re)).Value, "#7f");
  1312. }
  1313. [Test]
  1314. public void CastTimeSpans ()
  1315. {
  1316. TimeSpan ra = new TimeSpan (23, 21, 45, 36, 89);
  1317. TimeSpan rb = -new TimeSpan (3, 4, 5, 6, 789);
  1318. TimeSpan rc = new TimeSpan (2, 0, 0, 0, 0);
  1319. TimeSpan rd = new TimeSpan (0, 0, 0, 1);
  1320. TimeSpan re = new TimeSpan (1L); // one tick, the smallest interval
  1321. TimeSpan rx = DateTimeOffset.Now.Offset;
  1322. TimeSpan rz = TimeSpan.Zero;
  1323. XElement a = new XElement ("a", "P23DT21H45M36.089S");
  1324. XElement b = new XElement ("b", "-P3DT4H5M6.789S");
  1325. XElement c = new XElement ("c", "P2D");
  1326. XElement d = new XElement ("d", "PT1S");
  1327. XElement e = new XElement ("e", " PT0.0000001S \t \n ");
  1328. XElement x = new XElement ("x", rx);
  1329. XElement z = new XElement ("z", rz);
  1330. Assert.IsNotNull ((TimeSpan?) new XElement (a), "#1a");
  1331. Assert.AreEqual (ra, ((TimeSpan?) new XElement (a)).Value, "#1b");
  1332. Assert.AreEqual (ra, (TimeSpan) new XElement (a), "#1c");
  1333. Assert.AreEqual (ra, (TimeSpan) new XElement ("r", ra), "#1d");
  1334. Assert.IsNotNull ((TimeSpan?) new XElement ("r", ra), "#1e");
  1335. Assert.AreEqual (ra, ((TimeSpan?) new XElement ("r", ra)).Value, "#1f");
  1336. Assert.IsNotNull ((TimeSpan?) new XElement (b), "#2a");
  1337. Assert.AreEqual (rb, ((TimeSpan?) new XElement (b)).Value, "#2b");
  1338. Assert.AreEqual (rb, (TimeSpan) new XElement (b), "#2c");
  1339. Assert.AreEqual (rb, (TimeSpan) new XElement ("r", rb), "#2d");
  1340. Assert.IsNotNull ((TimeSpan?) new XElement ("r", rb), "#2e");
  1341. Assert.AreEqual (rb, ((TimeSpan?) new XElement ("r", rb)).Value, "#2f");
  1342. Assert.IsNotNull ((TimeSpan?) new XElement (c), "#3a");
  1343. Assert.AreEqual (rc, ((TimeSpan?) new XElement (c)).Value, "#3b");
  1344. Assert.AreEqual (rc, (TimeSpan) new XElement (c), "#3c");
  1345. Assert.AreEqual (rc, (TimeSpan) new XElement ("r", rc), "#3d");
  1346. Assert.IsNotNull ((TimeSpan?) new XElement ("r", rc), "#3e");
  1347. Assert.AreEqual (rc, ((TimeSpan?) new XElement ("r", rc)).Value, "#3f");
  1348. Assert.IsNotNull ((TimeSpan?) new XElement (d), "#4a");
  1349. Assert.AreEqual (rd, ((TimeSpan?) new XElement (d)).Value, "#4b");
  1350. Assert.AreEqual (rd, (TimeSpan) new XElement (d), "#4c");
  1351. Assert.AreEqual (rd, (TimeSpan) new XElement ("r", rd), "#4d");
  1352. Assert.IsNotNull ((TimeSpan?) new XElement ("r", rd), "#4e");
  1353. Assert.AreEqual (rd, ((TimeSpan?) new XElement ("r", rd)).Value, "#4f");
  1354. Assert.IsNotNull ((TimeSpan?) new XElement (x), "#5a");
  1355. Assert.AreEqual (rx, ((TimeSpan?) new XElement (x)).Value, "#5b");
  1356. Assert.AreEqual (rx, (TimeSpan) new XElement (x), "#5c");
  1357. Assert.IsNotNull ((TimeSpan?) new XElement (z), "#6a");
  1358. Assert.AreEqual (rz, ((TimeSpan?) new XElement (z)).Value, "#6b");
  1359. Assert.AreEqual (rz, (TimeSpan) new XElement (z), "#6c");
  1360. Assert.IsNotNull ((TimeSpan?) new XElement (e), "#7a");
  1361. Assert.AreEqual (re, ((TimeSpan?) new XElement (e)).Value, "#7b");
  1362. Assert.AreEqual (re, (TimeSpan) new XElement (e), "#7c");
  1363. Assert.AreEqual (re, (TimeSpan) new XElement ("r", re), "#7d");
  1364. Assert.IsNotNull ((TimeSpan?) new XElement ("r", re), "#7e");
  1365. Assert.AreEqual (re, ((TimeSpan?) new XElement ("r", re)).Value, "#7f");
  1366. }
  1367. #pragma warning restore 219
  1368. [Test]
  1369. public void Value ()
  1370. {
  1371. // based on bug #360858
  1372. XElement a = new XElement("root",
  1373. new XElement ("foo"),
  1374. "Linux&Windows",
  1375. new XComment ("comment"),
  1376. new XElement ("bar"));
  1377. Assert.AreEqual ("Linux&Windows", a.Value);
  1378. }
  1379. [Test]
  1380. [ExpectedException (typeof (ArgumentException))]
  1381. public void SetValueXAttribute ()
  1382. {
  1383. new XElement ("foo").SetValue (new XAttribute ("foo", "bar"));
  1384. }
  1385. [Test]
  1386. [ExpectedException (typeof (ArgumentException))]
  1387. public void SetValueXDocumnent ()
  1388. {
  1389. new XElement ("foo").SetValue (new XDocument ());
  1390. }
  1391. [Test]
  1392. // LAMESPEC: there is no reason to not reject XDeclaration while it rejects XDocument.
  1393. [ExpectedException (typeof (ArgumentException))]
  1394. [Category ("NotDotNet")]
  1395. public void SetValueXDeclaration ()
  1396. {
  1397. var el = new XElement ("foo");
  1398. el.SetValue (new XDeclaration ("1.0", null, null));
  1399. Assert.AreEqual ("<?xml version=\"1.0\"?>", el.Value);
  1400. }
  1401. [Test]
  1402. [ExpectedException (typeof (ArgumentNullException))]
  1403. public void SetValueNull ()
  1404. {
  1405. new XElement ("foo", "text").SetValue (null);
  1406. }
  1407. [Test]
  1408. public void AddSameInstance () // bug #392063
  1409. {
  1410. XElement root = new XElement (XName.Get ("Root", ""));
  1411. XElement child = new XElement (XName.Get ("Child", ""));
  1412. root.Add (child);
  1413. root.Add (child);
  1414. root.ToString ();
  1415. }
  1416. [Test]
  1417. [ExpectedException (typeof (InvalidOperationException))]
  1418. public void AddSameInstance2 ()
  1419. {
  1420. XElement root = new XElement (XName.Get ("Root"));
  1421. XAttribute attr = new XAttribute (XName.Get ("a"), "v");
  1422. root.Add (attr);
  1423. root.Add (attr); // duplicate attribute
  1424. root.ToString ();
  1425. }
  1426. [Test]
  1427. public void AddAttributeFromDifferentTree ()
  1428. {
  1429. XElement e1 = new XElement (XName.Get ("e1"));
  1430. XElement e2 = new XElement (XName.Get ("e2"));
  1431. XAttribute attr = new XAttribute (XName.Get ("a"), "v");
  1432. e1.Add (attr);
  1433. e2.Add (attr);
  1434. Assert.AreEqual ("<e1 a=\"v\" />", e1.ToString (), "#1");
  1435. Assert.AreEqual ("<e2 a=\"v\" />", e2.ToString (), "#2");
  1436. }
  1437. [Test]
  1438. public void SavePreservePrefixes ()
  1439. {
  1440. var x = XDocument.Parse (@"
  1441. <xxx:a xmlns:xxx='http://www.foobar.com'>
  1442. <xxx:b>blah blah blah</xxx:b>
  1443. </xxx:a>");
  1444. StringWriter sw = new StringWriter ();
  1445. x.Save (sw, SaveOptions.DisableFormatting);
  1446. Assert.AreEqual (@"<?xml version=""1.0"" encoding=""utf-16""?><xxx:a xmlns:xxx=""http://www.foobar.com""><xxx:b>blah blah blah</xxx:b></xxx:a>", sw.ToString ());
  1447. }
  1448. [Test]
  1449. public void LoadFromXmlTextReader ()
  1450. {
  1451. var foo = XElement.Load (new XmlTextReader (new StringReader ("<foo></foo>")));
  1452. Assert.IsNotNull (foo);
  1453. }
  1454. [Test]
  1455. public void ReplaceNodes ()
  1456. {
  1457. var inputXml = "<Foo><C><Three>3</Three><Two></Two><One/></C><B><Aaa/><Yyy/><fff/></B><A Attrib=\"Hello World\"/></Foo>";
  1458. var reader = XmlReader.Create (new StringReader (inputXml), new XmlReaderSettings ());
  1459. XDocument doc = XDocument.Load (reader);
  1460. var result = doc.Root.Elements ().OrderBy (el => el.Name.ToString());
  1461. Assert.AreEqual (3, result.Count (), "#1");
  1462. doc.Root.FirstNode.Remove ();
  1463. Assert.AreEqual (2, result.Count (), "#2");
  1464. XContainer container = doc.Root;
  1465. container.ReplaceNodes (result);
  1466. Assert.AreEqual (2, container.Elements ().Count (), "#3");
  1467. }
  1468. }
  1469. }