PageRenderTime 83ms CodeModel.GetById 28ms RepoModel.GetById 0ms 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

Large files files are truncated, but you can click here to view the full file

  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 (

Large files files are truncated, but you can click here to view the full file