5 // Copyright 2007 Novell (http://www.novell.com)
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:
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
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.
28 using System.Collections.Generic;
32 using System.Xml.Linq;
34 using NUnit.Framework;
36 namespace MonoTests.System.Xml.Linq
39 public class XElementTest
41 [Test] // xml declaration is skipped.
42 public void LoadWithXmldecl ()
44 string xml = "<?xml version='1.0'?><root />";
45 XElement.Load (new StringReader (xml));
51 string xml = "<root><foo/></root>";
53 XElement el = XElement.Load (new StringReader (xml));
54 XElement first = el.FirstNode as XElement;
55 Assert.IsNotNull (first, "#1");
56 Assert.IsTrue (el.LastNode is XElement, "#2");
57 Assert.IsNull (el.NextNode, "#3");
58 Assert.IsNull (el.PreviousNode, "#4");
59 Assert.AreEqual (1, new List<XNode> (el.Nodes ()).Count, "#5");
60 Assert.AreEqual (el, first.Parent, "#6");
61 Assert.AreEqual (first, el.LastNode, "#7");
63 Assert.AreEqual ("root", el.Name.ToString (), "#8");
64 Assert.AreEqual ("foo", first.Name.ToString (), "#9");
65 Assert.IsFalse (el.Attributes ().GetEnumerator ().MoveNext (), "#10");
69 [ExpectedException (typeof (InvalidOperationException))]
70 public void LoadInvalid ()
73 XmlReaderSettings s = new XmlReaderSettings ();
74 s.ConformanceLevel = ConformanceLevel.Fragment;
76 XElement.Load (XmlReader.Create (new StringReader (xml), s));
80 public void PrecedingWhitespaces ()
82 string xml = " <root/>";
83 XmlReaderSettings s = new XmlReaderSettings ();
84 s.ConformanceLevel = ConformanceLevel.Fragment;
86 XElement.Load (XmlReader.Create (new StringReader (xml), s));
90 public void PrecedingWhitespaces2 ()
92 string xml = " <root/>";
93 XmlReaderSettings s = new XmlReaderSettings ();
94 s.ConformanceLevel = ConformanceLevel.Fragment;
96 XmlReader r = XmlReader.Create (new StringReader (xml), s);
97 r.Read (); // at whitespace
104 string xml = "<root>foo</root>";
106 XElement el = XElement.Load (new StringReader (xml));
107 XText first = el.FirstNode as XText;
108 Assert.IsNotNull (first, "#1");
109 Assert.IsTrue (el.LastNode is XText, "#2");
110 Assert.AreEqual (1, new List<XNode> (el.Nodes ()).Count, "#3");
111 Assert.AreEqual (el, first.Parent, "#4");
112 Assert.AreEqual (first, el.LastNode, "#5");
114 Assert.AreEqual ("foo", first.Value, "#6");
118 [ExpectedException (typeof (ArgumentException))]
119 public void AddDocumentTypeToElement ()
121 XElement el = new XElement (XName.Get ("foo"));
122 el.Add (new XDocumentType ("foo", null, null, null));
126 [ExpectedException (typeof (ArgumentException))]
127 [Category ("NotDotNet")]
128 public void AddXDeclarationToElement ()
130 XElement el = new XElement (XName.Get ("foo"));
131 // LAMESPEC: in .NET, XDeclaration is not treated as
132 // invalid, and converted to a string without error.
133 el.Add (new XDeclaration ("1.0", null, null));
137 public void SetAttribute ()
139 XElement el = new XElement (XName.Get ("foo"));
140 el.SetAttributeValue (XName.Get ("a1"), "v1");
141 XAttribute a = el.FirstAttribute;
142 Assert.IsNotNull (a, "#1-1");
143 Assert.AreEqual (el, a.Parent, "#1-2");
144 Assert.IsNotNull (el.LastAttribute, "#1-3");
145 Assert.AreEqual (a, el.LastAttribute, "#1-4");
146 Assert.AreEqual ("a1", a.Name.LocalName, "#1-5");
147 Assert.AreEqual ("v1", a.Value, "#1-6");
148 Assert.IsNull (a.PreviousAttribute, "#1-7");
149 Assert.IsNull (a.NextAttribute, "#1-8");
151 el.SetAttributeValue (XName.Get ("a2"), "v2");
152 Assert.IsFalse (el.FirstAttribute == el.LastAttribute, "#2-1");
153 Assert.AreEqual ("a2", el.LastAttribute.Name.LocalName, "#2-2");
155 el.SetAttributeValue (XName.Get ("a1"), "v3");
156 XAttribute b = el.FirstAttribute;
157 Assert.IsNotNull (b, "#2-3");
158 Assert.IsNotNull (el.LastAttribute, "#2-4");
159 Assert.AreEqual ("a1", b.Name.LocalName, "#2-5");
160 Assert.AreEqual ("v3", b.Value, "#2-6");
161 Assert.AreEqual (a, b, "#2-7");
162 XAttribute c = el.LastAttribute;
163 Assert.AreEqual (a, c.PreviousAttribute, "#2-8");
166 Assert.IsNull (a.Parent, "#3-1");
167 Assert.IsNull (a.PreviousAttribute, "#3-2");
168 Assert.IsNull (a.NextAttribute, "#3-3");
169 Assert.IsNull (c.PreviousAttribute, "#3-4");
170 Assert.IsNull (c.NextAttribute, "#3-5");
172 el.RemoveAttributes ();
173 Assert.IsFalse (el.HasAttributes, "#4-1");
174 Assert.IsNull (b.Parent, "#4-2");
175 Assert.IsNull (c.Parent, "#4-3");
176 Assert.IsNull (el.FirstAttribute, "#4-4");
177 Assert.IsNull (el.LastAttribute, "#4-5");
181 public void AddAfterSelf ()
183 XElement el = XElement.Parse ("<root><foo/><bar/></root>");
184 el.FirstNode.AddAfterSelf ("text");
185 XText t = el.FirstNode.NextNode as XText;
186 Assert.IsNotNull (t, "#1");
187 Assert.AreEqual ("text", t.Value, "#2");
188 XElement bar = t.NextNode as XElement;
189 Assert.IsNotNull (bar, "#3");
190 Assert.AreEqual ("bar", bar.Name.LocalName, "#4");
194 public void AddAfterSelfList ()
196 XElement el = XElement.Parse ("<root><foo/><bar/></root>");
197 el.FirstNode.AddAfterSelf (new XText [] {
201 XText t = el.FirstNode.NextNode as XText;
202 Assert.IsNotNull (t, "#1");
203 Assert.AreEqual ("t1", t.Value, "#2");
204 Assert.AreEqual ("t2", ((XText) t.NextNode).Value, "#3");
205 Assert.AreEqual ("t3", ((XText) t.NextNode.NextNode).Value, "#4");
206 XElement bar = t.NextNode.NextNode.NextNode as XElement;
207 Assert.IsNotNull (bar, "#5");
208 Assert.AreEqual ("bar", bar.Name.LocalName, "#6");
212 [ExpectedException (typeof (ArgumentException))]
213 public void AddAfterSelfAttribute ()
215 var el = new XElement ("root", new XElement ("child"));
216 var el2 = el.FirstNode as XElement;
217 el2.AddAfterSelf (new XAttribute ("foo", "bar"));
221 [ExpectedException (typeof (ArgumentException))]
222 public void AddAfterSelfXDocument ()
224 var el = new XElement ("root", new XElement ("child"));
225 var el2 = el.FirstNode as XElement;
226 el2.AddAfterSelf (new XDocument ());
230 [ExpectedException (typeof (ArgumentException))]
231 [Category ("NotDotNet")]
232 [Category ("NotWorking")]
233 // LAMESPEC: there is no reason to not reject XDeclaration while it rejects XDocument.
234 public void AddAfterSelfXDeclaration ()
236 var el = new XElement ("root", new XElement ("child"));
237 var el2 = el.FirstNode as XElement;
238 el2.AddAfterSelf (new XDeclaration ("1.0", null, null));
242 public void AddAfterSelfCollection ()
244 var el = new XElement ("root", new XElement ("child"));
245 el.FirstNode.AddAfterSelf (new List<XElement> (new XElement [] {new XElement ("foo"), new XElement ("bar")}));
246 Assert.AreEqual ("<root><child /><foo /><bar /></root>", el.ToString (SaveOptions.DisableFormatting), "#1");
247 Assert.AreEqual ("bar", (el.LastNode as XElement).Name.LocalName, "#2");
251 public void AddAfterSelfJoinsStringAfterText ()
253 var el = XElement.Parse ("<foo>text1</foo>");
254 el.LastNode.AddAfterSelf ("text2");
255 el.LastNode.AddAfterSelf (new XText ("text3"));
256 IEnumerator<XNode> e = el.Nodes ().GetEnumerator ();
257 Assert.IsTrue (e.MoveNext (), "#1");
258 Assert.AreEqual ("text1text2", e.Current.ToString (), "#2");
259 Assert.IsTrue (e.MoveNext (), "#3");
260 Assert.AreEqual ("text3", e.Current.ToString (), "#4");
261 Assert.IsFalse (e.MoveNext (), "#5");
265 public void AddBeforeSelf ()
267 XElement el = XElement.Parse ("<root><foo/><bar/></root>");
268 el.FirstNode.AddBeforeSelf ("text");
269 XText t = el.FirstNode as XText;
270 Assert.IsNotNull (t, "#1");
271 Assert.AreEqual ("text", t.Value, "#2");
272 XElement foo = t.NextNode as XElement;
273 Assert.IsNotNull (foo, "#3");
274 Assert.AreEqual ("foo", foo.Name.LocalName, "#4");
278 public void AddBeforeSelfList ()
280 XElement el = XElement.Parse ("<root><foo/><bar/></root>");
281 el.FirstNode.AddBeforeSelf (new XText [] {
285 XText t = el.FirstNode as XText;
286 Assert.IsNotNull (t, "#1");
287 Assert.AreEqual ("t1", t.Value, "#2");
288 Assert.AreEqual ("t2", ((XText) t.NextNode).Value, "#3");
289 Assert.AreEqual ("t3", ((XText) t.NextNode.NextNode).Value, "#4");
290 XElement foo = t.NextNode.NextNode.NextNode as XElement;
291 Assert.IsNotNull (foo, "#5");
292 Assert.AreEqual ("foo", foo.Name.LocalName, "#6");
296 public void AddBeforeSelfList2 ()
298 XElement el = XElement.Parse ("<root><foo/><bar/></root>");
299 el.FirstNode.AddBeforeSelf ("t1", "t2", "t3");
300 XText t = el.FirstNode as XText;
301 Assert.IsNotNull (t, "#1");
302 Assert.AreEqual ("t1t2t3", t.Value, "#2");
303 XElement foo = t.NextNode as XElement;
304 Assert.IsNotNull (foo, "#3");
305 Assert.AreEqual ("foo", foo.Name.LocalName, "#4");
309 public void AddJoinsStringAfterText ()
311 var el = XElement.Parse ("<foo>text1</foo>");
313 el.Add (new XText ("text3"));
314 IEnumerator<XNode> e = el.Nodes ().GetEnumerator ();
315 Assert.IsTrue (e.MoveNext (), "#1");
316 Assert.AreEqual ("text1text2", e.Current.ToString (), "#2");
317 Assert.IsTrue (e.MoveNext (), "#3");
318 Assert.AreEqual ("text3", e.Current.ToString (), "#4");
319 Assert.IsFalse (e.MoveNext (), "#5");
323 [ExpectedException (typeof (InvalidOperationException))]
324 public void AddDuplicateAttribute ()
326 var el = new XElement ("foo",
327 new XAttribute ("bar", "baz"));
328 el.Add (new XAttribute ("bar", "baz"));
332 public void ReplaceWith ()
334 XElement el = XElement.Parse ("<root><foo/><bar/></root>");
335 XNode fc = el.FirstNode;
336 fc.ReplaceWith ("test");
337 XText t = el.FirstNode as XText;
338 Assert.IsNotNull (t, "#1");
339 Assert.AreEqual ("test", t.Value, "#2");
343 public void ReplaceAll ()
345 XElement el = XElement.Parse ("<root><foo/><bar/></root>");
346 el.ReplaceAll ("test");
347 XText t = el.FirstNode as XText;
348 Assert.IsNotNull (t, "#1");
349 Assert.AreEqual ("test", t.Value, "#2");
350 Assert.AreEqual (1, new List<XNode> (el.Nodes ()).Count, "#3");
354 public void ReplaceAllList ()
356 XElement el = XElement.Parse ("<root><foo/><bar/></root>");
360 new XText ("test3"));
361 XText t = el.FirstNode as XText;
362 Assert.IsNotNull (t, "#1");
363 Assert.AreEqual ("test1", t.Value, "#2");
364 t = el.LastNode as XText;
365 Assert.IsNotNull (t, "#3");
366 Assert.AreEqual ("test3", t.Value, "#4");
367 Assert.AreEqual (3, new List<XNode> (el.Nodes ()).Count, "#5");
371 public void ReplaceAttributes ()
373 XElement el = XElement.Parse ("<root x='y'><foo a='b'/></root>");
374 Assert.IsTrue (el.Attributes ().GetEnumerator ().MoveNext (), "#0");
375 el.ReplaceAttributes ("test");
376 Assert.IsTrue (el.FirstNode is XElement, "#1");
377 Assert.IsTrue (el.LastNode is XText, "#2");
378 Assert.IsFalse (el.Attributes ().GetEnumerator ().MoveNext (), "#3");
382 public void GetDefaultNamespace ()
384 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>");
385 XNamespace ns1 = XNamespace.Get ("urn:foo");
386 Assert.AreEqual (ns1, el.GetDefaultNamespace (), "#1");
387 XElement foo = (XElement) el.FirstNode;
388 Assert.AreEqual (ns1, foo.GetDefaultNamespace (), "#2");
389 Assert.AreEqual (ns1, ((XElement) foo.FirstNode).GetDefaultNamespace (), "#3");
390 XElement bar = (XElement) foo.NextNode;
391 Assert.AreEqual (ns1, bar.GetDefaultNamespace (), "#4");
392 Assert.AreEqual (ns1, ((XElement) bar.FirstNode).GetDefaultNamespace (), "#5");
393 XElement baz = (XElement) bar.NextNode;
394 Assert.AreEqual (XNamespace.Get (String.Empty), baz.GetDefaultNamespace (), "#6");
395 Assert.AreEqual (XNamespace.Get (String.Empty), ((XElement) baz.FirstNode).GetDefaultNamespace (), "#7");
399 public void GetPrefixNamespace ()
401 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>");
402 XNamespace ns1 = XNamespace.Get ("urn:foo");
403 XNamespace ns2 = XNamespace.Get ("urn:bar");
404 Assert.AreEqual (ns1, el.GetNamespaceOfPrefix ("x"), "#1-1");
405 Assert.AreEqual ("x", el.GetPrefixOfNamespace (ns1), "#1-2");
406 XElement foo = (XElement) el.FirstNode;
407 Assert.AreEqual (ns1, foo.GetNamespaceOfPrefix ("x"), "#2-1");
408 Assert.AreEqual ("x", foo.GetPrefixOfNamespace (ns1), "#2-2");
409 Assert.AreEqual (ns1, ((XElement) foo.FirstNode).GetNamespaceOfPrefix ("x"), "#3-1");
410 Assert.AreEqual ("x", ((XElement) foo.FirstNode).GetPrefixOfNamespace (ns1), "#3-2");
411 XElement bar = (XElement) foo.NextNode;
412 Assert.AreEqual (ns2, bar.GetNamespaceOfPrefix ("x"), "#4-1");
413 Assert.AreEqual ("x", bar.GetPrefixOfNamespace (ns2), "#4-2");
414 Assert.AreEqual (null, bar.GetPrefixOfNamespace (ns1), "#4-3");
415 Assert.AreEqual (ns2, ((XElement) bar.FirstNode).GetNamespaceOfPrefix ("x"), "#5-1");
416 Assert.AreEqual ("x", ((XElement) bar.FirstNode).GetPrefixOfNamespace (ns2), "#5-2");
417 Assert.AreEqual (null, ((XElement) bar.FirstNode).GetPrefixOfNamespace (ns1), "#5-3");
420 #pragma warning disable 219
422 public void CastNulls ()
424 const XElement a = null;
426 Assert.AreEqual (null, (bool?) a, "bool?");
427 Assert.AreEqual (null, (DateTime?) a, "DateTime?");
428 Assert.AreEqual (null, (DateTimeOffset?) a, "DateTimeOffset?");
429 Assert.AreEqual (null, (decimal?) a, "decimal?");
430 Assert.AreEqual (null, (double?) a, "double?");
431 Assert.AreEqual (null, (float?) a, "float?");
432 Assert.AreEqual (null, (Guid?) a, "Guid?");
433 Assert.AreEqual (null, (int?) a, "int?");
434 Assert.AreEqual (null, (long?) a, "long?");
435 Assert.AreEqual (null, (uint?) a, "uint?");
436 Assert.AreEqual (null, (ulong?) a, "ulong?");
437 Assert.AreEqual (null, (TimeSpan?) a, "TimeSpan?");
438 Assert.AreEqual (null, (string) a, "string");
439 AssertThrows<ArgumentNullException> (() => { bool z = (bool) a; }, "bool");
440 AssertThrows<ArgumentNullException> (() => { DateTime z = (DateTime) a; }, "DateTime");
441 AssertThrows<ArgumentNullException> (() => { DateTimeOffset z = (DateTimeOffset) a; }, "DateTimeOffset");
442 AssertThrows<ArgumentNullException> (() => { decimal z = (decimal) a; }, "decimal");
443 AssertThrows<ArgumentNullException> (() => { double z = (double) a; }, "double");
444 AssertThrows<ArgumentNullException> (() => { float z = (float) a; }, "float");
445 AssertThrows<ArgumentNullException> (() => { Guid z = (Guid) a; }, "Guid");
446 AssertThrows<ArgumentNullException> (() => { int z = (int) a; }, "int");
447 AssertThrows<ArgumentNullException> (() => { long z = (long) a; }, "long");
448 AssertThrows<ArgumentNullException> (() => { uint z = (uint) a; }, "uint");
449 AssertThrows<ArgumentNullException> (() => { ulong z = (ulong) a; }, "ulong");
450 AssertThrows<ArgumentNullException> (() => { TimeSpan z = (TimeSpan) a; }, "TimeSpan");
454 /// Provides functionality similar to Assert.Throws that is available on newer versions of NUnit.
456 private static T AssertThrows<T> (Action code, string message, params object[] args) where T : Exception {
457 Exception actual = null;
460 } catch (Exception exception) {
463 Assert.That (actual, new NUnit.Framework.Constraints.ExactTypeConstraint (typeof (T)), message, args);
468 public void CastEmpties ()
470 XElement a = new XElement ("a");
472 // Verify expected "cloning" and "empty" behaviour as prerequisites
473 Assert.IsTrue (a.IsEmpty, "#1-1");
474 Assert.IsTrue (new XElement (a).IsEmpty, "#1-2");
475 Assert.AreEqual (String.Empty, a.Value, "#2-1");
476 Assert.AreEqual (String.Empty, new XElement (a).Value, "#2-2");
477 Assert.AreNotSame (a, new XElement (a), "#3-1");
478 Assert.AreEqual (a.ToString (), new XElement (a).ToString (), "#3-2");
479 Assert.AreEqual ("<a />", a.ToString (), "#3-3");
480 Assert.AreEqual (a.ToString (), new XElement ("a", null).ToString (), "#3-4");
482 // Execute the primary assertions of this test
483 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (a); }, "bool?");
484 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (a); }, "DateTime?");
485 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (a); }, "DateTimeOffset?");
486 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (a); }, "decimal?");
487 AssertThrows<FormatException> (() => { double? z = (double?) new XElement (a); }, "double?");
488 AssertThrows<FormatException> (() => { float? z = (float?) new XElement (a); }, "float?");
489 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (a); }, "Guid?");
490 AssertThrows<FormatException> (() => { int? z = (int?) new XElement (a); }, "int?");
491 AssertThrows<FormatException> (() => { long? z = (long?) new XElement (a); }, "long?");
492 AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (a); }, "uint?");
493 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (a); }, "ulong?");
494 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (a); }, "TimeSpan?");
495 Assert.AreEqual (String.Empty, (string) new XElement (a), "string");
496 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (a); }, "bool");
497 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (a); }, "DateTime");
498 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (a); }, "DateTimeOffset");
499 AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (a); }, "decimal");
500 AssertThrows<FormatException> (() => { double z = (double) new XElement (a); }, "double");
501 AssertThrows<FormatException> (() => { float z = (float) new XElement (a); }, "float");
502 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (a); }, "Guid");
503 AssertThrows<FormatException> (() => { int z = (int) new XElement (a); }, "int");
504 AssertThrows<FormatException> (() => { long z = (long) new XElement (a); }, "long");
505 AssertThrows<FormatException> (() => { uint z = (uint) new XElement (a); }, "uint");
506 AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (a); }, "ulong");
507 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (a); }, "TimeSpan");
511 public void CastBlanks ()
513 XElement a = new XElement ("a", String.Empty);
514 XElement b = new XElement ("b", new XCData (string.Empty));
516 // Verify expected "cloning" and "blank" behaviour as prerequisites
517 Assert.IsFalse (a.IsEmpty, "#1-1a");
518 Assert.IsFalse (b.IsEmpty, "#1-1b");
519 Assert.IsFalse (new XElement (a).IsEmpty, "#1-2a");
520 Assert.IsFalse (new XElement (b).IsEmpty, "#1-2b");
521 Assert.AreEqual (String.Empty, a.Value, "#2-1a");
522 Assert.AreEqual (String.Empty, b.Value, "#2-1b");
523 Assert.AreEqual (String.Empty, new XElement (a).Value, "#2-2a");
524 Assert.AreEqual (String.Empty, new XElement (b).Value, "#2-2b");
525 Assert.AreNotSame (a, new XElement (a), "#3-1a");
526 Assert.AreNotSame (b, new XElement (b), "#3-1b");
527 Assert.AreEqual (a.ToString (), new XElement (a).ToString (), "#3-2a");
528 Assert.AreEqual (b.ToString (), new XElement (b).ToString (), "#3-2b");
529 Assert.AreEqual ("<a></a>", a.ToString (), "#3-3a");
530 Assert.AreEqual ("<b><![CDATA[]]></b>", b.ToString (), "#3-3b");
531 Assert.AreEqual (a.ToString (), new XElement ("a", "").ToString (), "#3-4a");
532 Assert.AreEqual (b.ToString (), new XElement ("b", new XCData ("")).ToString (), "#3-4b");
534 // Execute the primary assertions of this test
535 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (a); }, "a:bool?");
536 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (b); }, "b:bool?");
537 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (a); }, "a:DateTime?");
538 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (b); }, "b:DateTime?");
539 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (a); }, "a:DateTimeOffset?");
540 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (b); }, "b:DateTimeOffset?");
541 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (a); }, "a:decimal?");
542 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (b); }, "b:decimal?");
543 AssertThrows<FormatException> (() => { double? z = (double?) new XElement (a); }, "a:double?");
544 AssertThrows<FormatException> (() => { double? z = (double?) new XElement (b); }, "b:double?");
545 AssertThrows<FormatException> (() => { float? z = (float?) new XElement (a); }, "a:float?");
546 AssertThrows<FormatException> (() => { float? z = (float?) new XElement (b); }, "b:float?");
547 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (a); }, "a:Guid?");
548 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (b); }, "b:Guid?");
549 AssertThrows<FormatException> (() => { int? z = (int?) new XElement (a); }, "a:int?");
550 AssertThrows<FormatException> (() => { int? z = (int?) new XElement (b); }, "b:int?");
551 AssertThrows<FormatException> (() => { long? z = (long?) new XElement (a); }, "a:long?");
552 AssertThrows<FormatException> (() => { long? z = (long?) new XElement (b); }, "b:long?");
553 AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (a); }, "a:uint?");
554 AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (b); }, "b:uint?");
555 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (a); }, "a:ulong?");
556 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (b); }, "b:ulong?");
557 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (a); }, "a:TimeSpan?");
558 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (b); }, "b:TimeSpan?");
559 Assert.AreEqual (String.Empty, (string) new XElement (a), "a:string");
560 Assert.AreEqual (String.Empty, (string) new XElement (b), "b:string");
561 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (a); }, "a:bool");
562 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (b); }, "b:bool");
563 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (a); }, "a:DateTime");
564 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (b); }, "b:DateTime");
565 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (a); }, "a:DateTimeOffset");
566 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (b); }, "b:DateTimeOffset");
567 AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (a); }, "a:decimal");
568 AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (b); }, "b:decimal");
569 AssertThrows<FormatException> (() => { double z = (double) new XElement (a); }, "a:double");
570 AssertThrows<FormatException> (() => { double z = (double) new XElement (b); }, "b:double");
571 AssertThrows<FormatException> (() => { float z = (float) new XElement (a); }, "a:float");
572 AssertThrows<FormatException> (() => { float z = (float) new XElement (b); }, "b:float");
573 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (a); }, "a:Guid");
574 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (b); }, "b:Guid");
575 AssertThrows<FormatException> (() => { int z = (int) new XElement (a); }, "a:int");
576 AssertThrows<FormatException> (() => { int z = (int) new XElement (b); }, "b:int");
577 AssertThrows<FormatException> (() => { long z = (long) new XElement (a); }, "a:long");
578 AssertThrows<FormatException> (() => { long z = (long) new XElement (b); }, "b:long");
579 AssertThrows<FormatException> (() => { uint z = (uint) new XElement (a); }, "a:uint");
580 AssertThrows<FormatException> (() => { uint z = (uint) new XElement (b); }, "b:uint");
581 AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (a); }, "a:ulong");
582 AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (b); }, "b:ulong");
583 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (a); }, "a:TimeSpan");
584 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (b); }, "b:TimeSpan");
588 public void CastSpaces ()
590 XElement a = new XElement ("a", " ");
591 XElement b = new XElement ("b", new XCData (" "));
593 // Verify expected "cloning" and "space" behaviour as prerequisites
594 Assert.IsFalse (a.IsEmpty, "#1-1a");
595 Assert.IsFalse (b.IsEmpty, "#1-1b");
596 Assert.IsFalse (new XElement (a).IsEmpty, "#1-2a");
597 Assert.IsFalse (new XElement (b).IsEmpty, "#1-2b");
598 Assert.AreEqual (" ", a.Value, "#2-1a");
599 Assert.AreEqual (" ", b.Value, "#2-1b");
600 Assert.AreEqual (" ", new XElement (a).Value, "#2-2a");
601 Assert.AreEqual (" ", new XElement (b).Value, "#2-2b");
602 Assert.AreNotSame (a, new XElement (a), "#3-1a");
603 Assert.AreNotSame (b, new XElement (b), "#3-1b");
604 Assert.AreEqual (a.ToString (), new XElement (a).ToString (), "#3-2a");
605 Assert.AreEqual (b.ToString (), new XElement (b).ToString (), "#3-2b");
606 Assert.AreEqual ("<a> </a>", a.ToString (), "#3-3a");
607 Assert.AreEqual ("<b><![CDATA[ ]]></b>", b.ToString (), "#3-3b");
608 Assert.AreEqual (a.ToString (), new XElement ("a", ' ').ToString (), "#3-4");
610 // Execute the primary assertions of this test
611 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (a); }, "a:bool?");
612 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (b); }, "b:bool?");
613 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (a); }, "a:DateTime?");
614 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (b); }, "b:DateTime?");
615 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (a); }, "a:DateTimeOffset?");
616 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (b); }, "b:DateTimeOffset?");
617 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (a); }, "a:decimal?");
618 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (b); }, "b:decimal?");
619 AssertThrows<FormatException> (() => { double? z = (double?) new XElement (a); }, "a:double?");
620 AssertThrows<FormatException> (() => { double? z = (double?) new XElement (b); }, "b:double?");
621 AssertThrows<FormatException> (() => { float? z = (float?) new XElement (a); }, "a:float?");
622 AssertThrows<FormatException> (() => { float? z = (float?) new XElement (b); }, "b:float?");
623 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (a); }, "a:Guid?");
624 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (b); }, "b:Guid?");
625 AssertThrows<FormatException> (() => { int? z = (int?) new XElement (a); }, "a:int?");
626 AssertThrows<FormatException> (() => { int? z = (int?) new XElement (b); }, "b:int?");
627 AssertThrows<FormatException> (() => { long? z = (long?) new XElement (a); }, "a:long?");
628 AssertThrows<FormatException> (() => { long? z = (long?) new XElement (b); }, "b:long?");
629 AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (a); }, "a:uint?");
630 AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (b); }, "b:uint?");
631 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (a); }, "a:ulong?");
632 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (b); }, "b:ulong?");
633 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (a); }, "a:TimeSpan?");
634 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (b); }, "b:TimeSpan?");
635 Assert.AreEqual (" ", (string) new XElement (a), "a:string");
636 Assert.AreEqual (" ", (string) new XElement (b), "b:string");
637 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (a); }, "a:bool");
638 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (b); }, "b:bool");
639 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (a); }, "a:DateTime");
640 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (b); }, "b:DateTime");
641 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (a); }, "a:DateTimeOffset");
642 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (b); }, "b:DateTimeOffset");
643 AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (a); }, "a:decimal");
644 AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (b); }, "b:decimal");
645 AssertThrows<FormatException> (() => { double z = (double) new XElement (a); }, "a:double");
646 AssertThrows<FormatException> (() => { double z = (double) new XElement (b); }, "b:double");
647 AssertThrows<FormatException> (() => { float z = (float) new XElement (a); }, "a:float");
648 AssertThrows<FormatException> (() => { float z = (float) new XElement (b); }, "b:float");
649 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (a); }, "a:Guid");
650 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (b); }, "b:Guid");
651 AssertThrows<FormatException> (() => { int z = (int) new XElement (a); }, "a:int");
652 AssertThrows<FormatException> (() => { int z = (int) new XElement (b); }, "b:int");
653 AssertThrows<FormatException> (() => { long z = (long) new XElement (a); }, "a:long");
654 AssertThrows<FormatException> (() => { long z = (long) new XElement (b); }, "b:long");
655 AssertThrows<FormatException> (() => { uint z = (uint) new XElement (a); }, "a:uint");
656 AssertThrows<FormatException> (() => { uint z = (uint) new XElement (b); }, "b:uint");
657 AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (a); }, "a:ulong");
658 AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (b); }, "b:ulong");
659 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (a); }, "a:TimeSpan");
660 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (b); }, "b:TimeSpan");
664 public void CastNumbers ()
666 XElement a = new XElement ("a", "7");
667 XElement b = new XElement ("b", new XCData (" 42 "));
668 XElement c = new XElement ("c", " \r\n 13 \t ");
669 XElement d = new XElement ("d", -101);
670 XElement o = new XElement ("o", "0");
671 XElement l = new XElement ("l", "1");
672 XElement I = new XElement ("I", "INF");
673 XElement i = new XElement ("i", " Infinity ");
674 XElement M = new XElement ("M", " -INF ");
675 XElement m = new XElement ("m", "-Infinity");
676 XElement n = new XElement ("n", "\t NaN ");
678 // Verify expected "cloning" and basic conversion behaviour as prerequisites
679 Assert.IsFalse (a.IsEmpty, "#1-1");
680 Assert.IsFalse (new XElement (b).IsEmpty, "#1-2");
681 Assert.AreEqual (" \r\n 13 \t ", c.Value, "#2-1");
682 Assert.AreEqual ("-101", new XElement (d).Value, "#2-2");
683 Assert.AreNotSame (o, new XElement (o), "#3-1");
684 Assert.AreEqual (l.ToString (), new XElement (l).ToString (), "#3-2");
685 Assert.AreEqual ("<a>7</a>", a.ToString (), "#3-3a");
686 Assert.AreEqual ("<b><![CDATA[ 42 ]]></b>", b.ToString (), "#3-3b");
687 Assert.AreEqual ("<c> \r\n 13 \t </c>", c.ToString (), "#3-3c");
688 Assert.AreEqual ("<d>-101</d>", d.ToString (), "#3-3d");
689 Assert.AreEqual ("<o>0</o>", new XElement ("o", 0.0).ToString (), "#3-3o");
690 Assert.AreEqual ("<l>1</l>", new XElement ("l", 1.0f).ToString (), "#3-3l");
691 Assert.AreEqual ("<n>NaN</n>", new XElement ("n", double.NaN).ToString (), "#3-3n");
692 Assert.AreEqual (a.ToString (), new XElement ("a", '7').ToString (), "#3-4a");
693 Assert.AreEqual (d.ToString (), new XElement ("d", "-101").ToString (), "#3-4d");
694 Assert.AreEqual (o.ToString (), new XElement ("o", 0L).ToString (), "#3-4o");
695 Assert.AreEqual (l.ToString (), new XElement ("l", 1m).ToString (), "#3-4l");
697 // Execute the primary assertions of this test
698 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (a); }, "a:bool?");
699 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (b); }, "b:bool?");
700 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (c); }, "c:bool?");
701 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (d); }, "d:bool?");
702 Assert.IsNotNull ((bool?) new XElement (o), "o:bool?:null");
703 Assert.AreEqual (false, ((bool?) new XElement (o)).Value, "o:bool?:value");
704 Assert.IsNotNull ((bool?) new XElement (l), "l:bool?:null");
705 Assert.AreEqual (true, ((bool?) new XElement (l)).Value, "l:bool?:value");
706 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (I); }, "I:bool?");
707 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (i); }, "i:bool?");
708 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (M); }, "M:bool?");
709 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (m); }, "m:bool?");
710 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (n); }, "n:bool?");
711 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (a); }, "a:DateTime?");
712 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (b); }, "b:DateTime?");
713 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (c); }, "c:DateTime?");
714 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (d); }, "d:DateTime?");
715 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (o); }, "o:DateTime?");
716 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (l); }, "l:DateTime?");
717 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (I); }, "I:DateTime?");
718 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (i); }, "i:DateTime?");
719 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (M); }, "M:DateTime?");
720 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (m); }, "m:DateTime?");
721 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (n); }, "n:DateTime?");
722 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (a); }, "a:DateTimeOffset?");
723 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (b); }, "b:DateTimeOffset?");
724 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (c); }, "c:DateTimeOffset?");
725 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (d); }, "d:DateTimeOffset?");
726 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (o); }, "o:DateTimeOffset?");
727 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (l); }, "l:DateTimeOffset?");
728 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (I); }, "I:DateTimeOffset?");
729 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (i); }, "i:DateTimeOffset?");
730 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (M); }, "M:DateTimeOffset?");
731 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (m); }, "m:DateTimeOffset?");
732 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (n); }, "n:DateTimeOffset?");
733 Assert.IsNotNull ((decimal?) new XElement (a), "a:decimal?:null");
734 Assert.AreEqual (7m, ((decimal?) new XElement (a)).Value, "a:decimal?:value");
735 Assert.IsNotNull ((decimal?) new XElement (b), "b:decimal?:null");
736 Assert.AreEqual (42m, ((decimal?) new XElement (b)).Value, "b:decimal?:value");
737 Assert.IsNotNull ((decimal?) new XElement (c), "c:decimal?:null");
738 Assert.AreEqual (13m, ((decimal?) new XElement (c)).Value, "c:decimal?:value");
739 Assert.IsNotNull ((decimal?) new XElement (d), "d:decimal?:null");
740 Assert.AreEqual (-101m, ((decimal?) new XElement (d)).Value, "d:decimal?:value");
741 Assert.IsNotNull ((decimal?) new XElement (o), "o:decimal?:null");
742 Assert.AreEqual (0m, ((decimal?) new XElement (o)).Value, "o:decimal?:value");
743 Assert.IsNotNull ((decimal?) new XElement (l), "l:decimal?:null");
744 Assert.AreEqual (1m, ((decimal?) new XElement (l)).Value, "l:decimal?:value");
745 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (I); }, "I:decimal?");
746 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (i); }, "i:decimal?");
747 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (M); }, "M:decimal?");
748 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (m); }, "m:decimal?");
749 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (n); }, "n:decimal?");
750 Assert.IsNotNull ((double?) new XElement (a), "a:double?:null");
751 Assert.AreEqual (7d, ((double?) new XElement (a)).Value, "a:double?:value");
752 Assert.IsNotNull ((double?) new XElement (b), "b:double?:null");
753 Assert.AreEqual (42d, ((double?) new XElement (b)).Value, "b:double?:value");
754 Assert.IsNotNull ((double?) new XElement (c), "c:double?:null");
755 Assert.AreEqual (13d, ((double?) new XElement (c)).Value, "c:double?:value");
756 Assert.IsNotNull ((double?) new XElement (d), "d:double?:null");
757 Assert.AreEqual (-101d, ((double?) new XElement (d)).Value, "d:double?:value");
758 Assert.IsNotNull ((double?) new XElement (o), "o:double?:null");
759 Assert.AreEqual (0d, ((double?) new XElement (o)).Value, "o:double?:value");
760 Assert.IsNotNull ((double?) new XElement (l), "l:double?:null");
761 Assert.AreEqual (1d, ((double?) new XElement (l)).Value, "l:double?:value");
762 Assert.IsNotNull ((double?) new XElement (I), "I:double?:null");
763 Assert.AreEqual (double.PositiveInfinity, ((double?) new XElement (I)).Value, "I:double?:value");
764 Assert.IsNotNull ((double?) new XElement (i), "i:double?:null");
765 Assert.AreEqual (double.PositiveInfinity, ((double?) new XElement (i)).Value, "i:double?:value");
766 Assert.IsNotNull ((double?) new XElement (M), "M:double?:null");
767 Assert.AreEqual (double.NegativeInfinity, ((double?) new XElement (M)).Value, "M:double?:value");
768 Assert.IsNotNull ((double?) new XElement (m), "m:double?:null");
769 Assert.AreEqual (double.NegativeInfinity, ((double?) new XElement (m)).Value, "m:double?:value");
770 Assert.IsNotNull ((double?) new XElement (n), "n:double?:null");
771 Assert.IsNaN (((double?) new XElement (n)).Value, "n:double?:value");
772 Assert.IsNotNull ((float?) new XElement (a), "a:float?:null");
773 Assert.AreEqual (7f, ((float?) new XElement (a)).Value, "a:float?:value");
774 Assert.IsNotNull ((float?) new XElement (b), "b:float?:null");
775 Assert.AreEqual (42f, ((float?) new XElement (b)).Value, "b:float?:value");
776 Assert.IsNotNull ((float?) new XElement (c), "c:float?:null");
777 Assert.AreEqual (13f, ((float?) new XElement (c)).Value, "c:float?:value");
778 Assert.IsNotNull ((float?) new XElement (d), "d:float?:null");
779 Assert.AreEqual (-101f, ((float?) new XElement (d)).Value, "d:float?:value");
780 Assert.IsNotNull ((float?) new XElement (o), "o:float?:null");
781 Assert.AreEqual (0f, ((float?) new XElement (o)).Value, "o:float?:value");
782 Assert.IsNotNull ((float?) new XElement (l), "l:float?:null");
783 Assert.AreEqual (1f, ((float?) new XElement (l)).Value, "l:float?:value");
784 Assert.IsNotNull ((float?) new XElement (I), "I:float?:null");
785 Assert.AreEqual (float.PositiveInfinity, ((float?) new XElement (I)).Value, "I:float?:value");
786 Assert.IsNotNull ((float?) new XElement (i), "i:float?:null");
787 Assert.AreEqual (float.PositiveInfinity, ((float?) new XElement (i)).Value, "i:float?:value");
788 Assert.IsNotNull ((float?) new XElement (M), "M:float?:null");
789 Assert.AreEqual (float.NegativeInfinity, ((float?) new XElement (M)).Value, "M:float?:value");
790 Assert.IsNotNull ((float?) new XElement (m), "m:float?:null");
791 Assert.AreEqual (float.NegativeInfinity, ((float?) new XElement (m)).Value, "m:float?:value");
792 Assert.IsNotNull ((float?) new XElement (n), "n:float?:null");
793 Assert.IsNaN (((float?) new XElement (n)).Value, "n:float?:value");
794 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (a); }, "a:Guid?");
795 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (b); }, "b:Guid?");
796 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (c); }, "c:Guid?");
797 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (d); }, "d:Guid?");
798 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (o); }, "o:Guid?");
799 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (l); }, "l:Guid?");
800 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (I); }, "I:Guid?");
801 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (i); }, "i:Guid?");
802 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (M); }, "M:Guid?");
803 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (m); }, "m:Guid?");
804 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (n); }, "n:Guid?");
805 Assert.IsNotNull ((int?) new XElement (a), "a:int?:null");
806 Assert.AreEqual (7, ((int?) new XElement (a)).Value, "a:int?:value");
807 Assert.IsNotNull ((int?) new XElement (b), "b:int?:null");
808 Assert.AreEqual (42, ((int?) new XElement (b)).Value, "b:int?:value");
809 Assert.IsNotNull ((int?) new XElement (c), "c:int?:null");
810 Assert.AreEqual (13, ((int?) new XElement (c)).Value, "c:int?:value");
811 Assert.IsNotNull ((int?) new XElement (d), "d:int?:null");
812 Assert.AreEqual (-101, ((int?) new XElement (d)).Value, "d:int?:value");
813 Assert.IsNotNull ((int?) new XElement (o), "o:int?:null");
814 Assert.AreEqual (0, ((int?) new XElement (o)).Value, "o:int?:value");
815 Assert.IsNotNull ((int?) new XElement (l), "l:int?:null");
816 Assert.AreEqual (1, ((int?) new XElement (l)).Value, "l:int?:value");
817 AssertThrows<FormatException> (() => { int? z = (int?) new XElement (I); }, "I:int?");
818 AssertThrows<FormatException> (() => { int? z = (int?) new XElement (i); }, "i:int?");
819 AssertThrows<FormatException> (() => { int? z = (int?) new XElement (M); }, "M:int?");
820 AssertThrows<FormatException> (() => { int? z = (int?) new XElement (m); }, "m:int?");
821 AssertThrows<FormatException> (() => { int? z = (int?) new XElement (n); }, "n:int?");
822 Assert.IsNotNull ((long?) new XElement (a), "a:long?:null");
823 Assert.AreEqual (7L, ((long?) new XElement (a)).Value, "a:long?:value");
824 Assert.IsNotNull ((long?) new XElement (b), "b:long?:null");
825 Assert.AreEqual (42L, ((long?) new XElement (b)).Value, "b:long?:value");
826 Assert.IsNotNull ((long?) new XElement (c), "c:long?:null");
827 Assert.AreEqual (13L, ((long?) new XElement (c)).Value, "c:long?:value");
828 Assert.IsNotNull ((long?) new XElement (d), "d:long?:null");
829 Assert.AreEqual (-101L, ((long?) new XElement (d)).Value, "d:long?:value");
830 Assert.IsNotNull ((long?) new XElement (o), "o:long?:null");
831 Assert.AreEqual (0L, ((long?) new XElement (o)).Value, "o:long?:value");
832 Assert.IsNotNull ((long?) new XElement (l), "l:long?:null");
833 Assert.AreEqual (1L, ((long?) new XElement (l)).Value, "l:long?:value");
834 AssertThrows<FormatException> (() => { long? z = (long?) new XElement (I); }, "I:long?");
835 AssertThrows<FormatException> (() => { long? z = (long?) new XElement (i); }, "i:long?");
836 AssertThrows<FormatException> (() => { long? z = (long?) new XElement (M); }, "M:long?");
837 AssertThrows<FormatException> (() => { long? z = (long?) new XElement (m); }, "m:long?");
838 AssertThrows<FormatException> (() => { long? z = (long?) new XElement (n); }, "n:long?");
839 Assert.IsNotNull ((uint?) new XElement (a), "a:uint?:null");
840 Assert.AreEqual (7u, ((uint?) new XElement (a)).Value, "a:uint?:value");
841 Assert.IsNotNull ((uint?) new XElement (b), "b:uint?:null");
842 Assert.AreEqual (42u, ((uint?) new XElement (b)).Value, "b:uint?:value");
843 Assert.IsNotNull ((uint?) new XElement (c), "c:uint?:null");
844 Assert.AreEqual (13u, ((uint?) new XElement (c)).Value, "c:uint?:value");
845 // LAMESPEC: see XmlConvertTests.ToUInt32().
846 //AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (d); }, "d:uint?");
847 Assert.IsNotNull ((uint?) new XElement (o), "o:uint?:null");
848 Assert.AreEqual (0u, ((uint?) new XElement (o)).Value, "o:uint?:value");
849 Assert.IsNotNull ((uint?) new XElement (l), "l:uint?:null");
850 Assert.AreEqual (1u, ((uint?) new XElement (l)).Value, "l:uint?:value");
851 AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (I); }, "I:uint?");
852 AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (i); }, "i:uint?");
853 AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (M); }, "M:uint?");
854 AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (m); }, "m:uint?");
855 AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (n); }, "n:uint?");
856 Assert.IsNotNull ((ulong?) new XElement (a), "a:ulong?:null");
857 Assert.AreEqual (7UL, ((ulong?) new XElement (a)).Value, "a:ulong?:value");
858 Assert.IsNotNull ((ulong?) new XElement (b), "b:ulong?:null");
859 Assert.AreEqual (42UL, ((ulong?) new XElement (b)).Value, "b:ulong?:value");
860 Assert.IsNotNull ((ulong?) new XElement (c), "c:ulong?:null");
861 Assert.AreEqual (13UL, ((ulong?) new XElement (c)).Value, "c:ulong?:value");
862 // LAMESPEC: see XmlConvertTests.ToUInt64().
863 //AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (d); }, "d:ulong?");
864 Assert.IsNotNull ((ulong?) new XElement (o), "o:ulong?:null");
865 Assert.AreEqual (0UL, ((ulong?) new XElement (o)).Value, "o:ulong?:value");
866 Assert.IsNotNull ((ulong?) new XElement (l), "l:ulong?:null");
867 Assert.AreEqual (1UL, ((ulong?) new XElement (l)).Value, "l:ulong?:value");
868 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (I); }, "I:ulong?");
869 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (i); }, "i:ulong?");
870 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (M); }, "M:ulong?");
871 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (m); }, "m:ulong?");
872 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (n); }, "n:ulong?");
873 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (a); }, "a:TimeSpan?");
874 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (b); }, "b:TimeSpan?");
875 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (c); }, "c:TimeSpan?");
876 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (d); }, "d:TimeSpan?");
877 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (o); }, "o:TimeSpan?");
878 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (l); }, "l:TimeSpan?");
879 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (I); }, "I:TimeSpan?");
880 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (i); }, "i:TimeSpan?");
881 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (M); }, "M:TimeSpan?");
882 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (m); }, "m:TimeSpan?");
883 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (n); }, "n:TimeSpan?");
884 Assert.AreEqual ("7", (string) new XElement (a), "a:string");
885 Assert.AreEqual (" 42 ", (string) new XElement (b), "b:string");
886 Assert.AreEqual (" \r\n 13 \t ", (string) new XElement (c), "c:string");
887 Assert.AreEqual ("-101", (string) new XElement (d), "d:string");
888 Assert.AreEqual ("0", (string) new XElement (o), "o:string");
889 Assert.AreEqual ("1", (string) new XElement (l), "l:string");
890 Assert.AreEqual ("INF", (string) new XElement (I), "I:string");
891 Assert.AreEqual (" Infinity ", (string) new XElement (i), "i:string");
892 Assert.AreEqual (" -INF ", (string) new XElement (M), "M:string");
893 Assert.AreEqual ("-Infinity", (string) new XElement (m), "m:string");
894 Assert.AreEqual ("\t NaN ", (string) new XElement (n), "n:string");
895 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (a); }, "a:bool");
896 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (b); }, "b:bool");
897 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (c); }, "c:bool");
898 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (d); }, "d:bool");
899 Assert.AreEqual (false, (bool) new XElement (o), "o:bool");
900 Assert.AreEqual (true, (bool) new XElement (l), "l:bool");
901 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (I); }, "I:bool");
902 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (i); }, "i:bool");
903 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (M); }, "M:bool");
904 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (m); }, "m:bool");
905 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (n); }, "n:bool");
906 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (a); }, "a:DateTime");
907 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (b); }, "b:DateTime");
908 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (c); }, "c:DateTime");
909 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (d); }, "d:DateTime");
910 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (o); }, "o:DateTime");
911 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (l); }, "l:DateTime");
912 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (I); }, "I:DateTime");
913 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (i); }, "i:DateTime");
914 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (M); }, "M:DateTime");
915 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (m); }, "m:DateTime");
916 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (n); }, "n:DateTime");
917 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (a); }, "a:DateTimeOffset");
918 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (b); }, "b:DateTimeOffset");
919 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (c); }, "c:DateTimeOffset");
920 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (d); }, "d:DateTimeOffset");
921 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (o); }, "o:DateTimeOffset");
922 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (l); }, "l:DateTimeOffset");
923 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (I); }, "I:DateTimeOffset");
924 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (i); }, "i:DateTimeOffset");
925 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (M); }, "M:DateTimeOffset");
926 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (m); }, "m:DateTimeOffset");
927 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (n); }, "n:DateTimeOffset");
928 Assert.AreEqual (7m, (decimal) new XElement (a), "a:decimal");
929 Assert.AreEqual (42m, (decimal) new XElement (b), "b:decimal");
930 Assert.AreEqual (13m, (decimal) new XElement (c), "c:decimal");
931 Assert.AreEqual (-101m, (decimal) new XElement (d), "d:decimal");
932 Assert.AreEqual (0m, (decimal) new XElement (o), "o:decimal");
933 Assert.AreEqual (1m, (decimal) new XElement (l), "l:decimal");
934 AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (I); }, "I:decimal");
935 AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (i); }, "i:decimal");
936 AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (M); }, "M:decimal");
937 AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (m); }, "m:decimal");
938 AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (n); }, "n:decimal");
939 Assert.AreEqual (7d, (double) new XElement (a), "a:double");
940 Assert.AreEqual (42d, (double) new XElement (b), "b:double");
941 Assert.AreEqual (13d, (double) new XElement (c), "c:double");
942 Assert.AreEqual (-101d, (double) new XElement (d), "d:double");
943 Assert.AreEqual (0d, (double) new XElement (o), "o:double");
944 Assert.AreEqual (1d, (double) new XElement (l), "l:double");
945 Assert.AreEqual (double.PositiveInfinity, (double) new XElement (I), "I:double");
946 Assert.AreEqual (double.PositiveInfinity, (double) new XElement (i), "i:double");
947 Assert.AreEqual (double.NegativeInfinity, (double) new XElement (M), "M:double");
948 Assert.AreEqual (double.NegativeInfinity, (double) new XElement (m), "m:double");
949 Assert.IsNaN (((double) new XElement (n)), "n:double");
950 Assert.AreEqual (7f, (float) new XElement (a), "a:float");
951 Assert.AreEqual (42f, (float) new XElement (b), "b:float");
952 Assert.AreEqual (13f, (float) new XElement (c), "c:float");
953 Assert.AreEqual (-101f, (float) new XElement (d), "d:float");
954 Assert.AreEqual (0f, (float) new XElement (o), "o:float");
955 Assert.AreEqual (1f, (float) new XElement (l), "l:float");
956 Assert.AreEqual (float.PositiveInfinity, (float) new XElement (I), "I:float");
957 Assert.AreEqual (float.PositiveInfinity, (float) new XElement (i), "i:float");
958 Assert.AreEqual (float.NegativeInfinity, (float) new XElement (M), "M:float");
959 Assert.AreEqual (float.NegativeInfinity, (float) new XElement (m), "m:float");
960 Assert.IsNaN (((float) new XElement (n)), "n:float");
961 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (a); }, "a:Guid");
962 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (b); }, "b:Guid");
963 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (c); }, "c:Guid");
964 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (d); }, "d:Guid");
965 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (o); }, "o:Guid");
966 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (l); }, "l:Guid");
967 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (I); }, "I:Guid");
968 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (i); }, "i:Guid");
969 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (M); }, "M:Guid");
970 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (m); }, "m:Guid");
971 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (n); }, "n:Guid");
972 Assert.AreEqual (7, (int) new XElement (a), "a:int");
973 Assert.AreEqual (42, (int) new XElement (b), "b:int");
974 Assert.AreEqual (13, (int) new XElement (c), "c:int");
975 Assert.AreEqual (-101, (int) new XElement (d), "d:int");
976 Assert.AreEqual (0, (int) new XElement (o), "o:int");
977 Assert.AreEqual (1, (int) new XElement (l), "l:int");
978 AssertThrows<FormatException> (() => { int z = (int) new XElement (I); }, "I:int");
979 AssertThrows<FormatException> (() => { int z = (int) new XElement (i); }, "i:int");
980 AssertThrows<FormatException> (() => { int z = (int) new XElement (M); }, "M:int");
981 AssertThrows<FormatException> (() => { int z = (int) new XElement (m); }, "m:int");
982 AssertThrows<FormatException> (() => { int z = (int) new XElement (n); }, "n:int");
983 Assert.AreEqual (7L, (long) new XElement (a), "a:long");
984 Assert.AreEqual (42L, (long) new XElement (b), "b:long");
985 Assert.AreEqual (13L, (long) new XElement (c), "c:long");
986 Assert.AreEqual (-101L, (long) new XElement (d), "d:long");
987 Assert.AreEqual (0L, (long) new XElement (o), "o:long");
988 Assert.AreEqual (1L, (long) new XElement (l), "l:long");
989 AssertThrows<FormatException> (() => { long z = (long) new XElement (I); }, "I:long");
990 AssertThrows<FormatException> (() => { long z = (long) new XElement (i); }, "i:long");
991 AssertThrows<FormatException> (() => { long z = (long) new XElement (M); }, "M:long");
992 AssertThrows<FormatException> (() => { long z = (long) new XElement (m); }, "m:long");
993 AssertThrows<FormatException> (() => { long z = (long) new XElement (n); }, "n:long");
994 Assert.AreEqual (7u, (uint) new XElement (a), "a:uint");
995 Assert.AreEqual (42u, (uint) new XElement (b), "b:uint");
996 Assert.AreEqual (13u, (uint) new XElement (c), "c:uint");
997 // LAMESPEC: see XmlConvertTests.ToUInt32().
998 //AssertThrows<FormatException> (() => { uint z = (uint) new XElement (d); }, "d:uint");
999 Assert.AreEqual (0u, (uint) new XElement (o), "o:uint");
1000 Assert.AreEqual (1u, (uint) new XElement (l), "l:uint");
1001 AssertThrows<FormatException> (() => { uint z = (uint) new XElement (I); }, "I:uint");
1002 AssertThrows<FormatException> (() => { uint z = (uint) new XElement (i); }, "i:uint");
1003 AssertThrows<FormatException> (() => { uint z = (uint) new XElement (M); }, "M:uint");
1004 AssertThrows<FormatException> (() => { uint z = (uint) new XElement (m); }, "m:uint");
1005 AssertThrows<FormatException> (() => { uint z = (uint) new XElement (n); }, "n:uint");
1006 Assert.AreEqual (7UL, (ulong) new XElement (a), "a:ulong");
1007 Assert.AreEqual (42UL, (ulong) new XElement (b), "b:ulong");
1008 Assert.AreEqual (13UL, (ulong) new XElement (c), "c:ulong");
1009 // LAMESPEC: see XmlConvertTests.ToUInt64().
1010 //AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (d); }, "d:ulong");
1011 Assert.AreEqual (0UL, (ulong) new XElement (o), "o:ulong");
1012 Assert.AreEqual (1UL, (ulong) new XElement (l), "l:ulong");
1013 AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (I); }, "I:ulong");
1014 AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (i); }, "i:ulong");
1015 AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (M); }, "M:ulong");
1016 AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (m); }, "m:ulong");
1017 AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (n); }, "n:ulong");
1018 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (a); }, "a:TimeSpan");
1019 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (b); }, "b:TimeSpan");
1020 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (c); }, "c:TimeSpan");
1021 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (d); }, "d:TimeSpan");
1022 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (o); }, "o:TimeSpan");
1023 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (l); }, "l:TimeSpan");
1024 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (I); }, "I:TimeSpan");
1025 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (i); }, "i:TimeSpan");
1026 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (M); }, "M:TimeSpan");
1027 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (m); }, "m:TimeSpan");
1028 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (n); }, "n:TimeSpan");
1030 // Perform some round-trip tests with numbers
1032 decimal @decimal = -41051609414188012238960097189m;
1033 double @double = 8.5506609919892972E+307d;
1034 float @float = -1.70151961E+37f;
1035 int @int = -1051251773;
1036 long @long = 4596767133891939716L;
1037 uint @uint = 4106628142u;
1038 ulong @ulong = 10713797297298255927UL;
1039 x = new XElement ("x", @decimal);
1040 Assert.IsNotNull ((decimal?) new XElement (x), "x:decimal?:null");
1041 Assert.AreEqual (@decimal, ((decimal?) new XElement (x)).Value, "x:decimal?:value");
1042 Assert.AreEqual (@decimal, (decimal) new XElement (x), "x:decimal");
1043 x = new XElement ("x", @double);
1044 Assert.IsNotNull ((double?) new XElement (x), "x:double?:null");
1045 Assert.AreEqual (@double, ((double?) new XElement (x)).Value, "x:double?:value");
1046 Assert.AreEqual (@double, (double) new XElement (x), "x:double");
1047 x = new XElement ("x", @float);
1048 Assert.IsNotNull ((float?) new XElement (x), "x:float?:null");
1049 Assert.AreEqual (@float, ((float?) new XElement (x)).Value, "x:float?:value");
1050 Assert.AreEqual (@float, (float) new XElement (x), "x:float");
1051 x = new XElement ("x", @int);
1052 Assert.IsNotNull ((int?) new XElement (x), "x:int?:null");
1053 Assert.AreEqual (@int, ((int?) new XElement (x)).Value, "x:int?:value");
1054 Assert.AreEqual (@int, (int) new XElement (x), "x:int");
1055 x = new XElement ("x", @long);
1056 Assert.IsNotNull ((long?) new XElement (x), "x:long?:null");
1057 Assert.AreEqual (@long, ((long?) new XElement (x)).Value, "x:long?:value");
1058 Assert.AreEqual (@long, (long) new XElement (x), "x:long");
1059 x = new XElement ("x", @uint);
1060 Assert.IsNotNull ((uint?) new XElement (x), "x:uint?:null");
1061 Assert.AreEqual (@uint, ((uint?) new XElement (x)).Value, "x:uint?:value");
1062 Assert.AreEqual (@uint, (uint) new XElement (x), "x:uint");
1063 x = new XElement ("x", @ulong);
1064 Assert.IsNotNull ((ulong?) new XElement (x), "x:ulong?:null");
1065 Assert.AreEqual (@ulong, ((ulong?) new XElement (x)).Value, "x:ulong?:value");
1066 Assert.AreEqual (@ulong, (ulong) new XElement (x), "x:ulong");
1068 // Perform overflow tests with numbers
1069 AssertThrows<OverflowException> (() => { decimal z = (decimal) new XElement("z", "91051609414188012238960097189"); }, "z:decimal");
1070 AssertThrows<OverflowException> (() => { decimal? z = (decimal?) new XElement("z", "91051609414188012238960097189"); }, "z:decimal?");
1071 AssertThrows<OverflowException> (() => { double z = (double) new XElement("z", "8.5506609919892972E+654"); }, "z:double");
1072 AssertThrows<OverflowException> (() => { double? z = (double?) new XElement("z", "8.5506609919892972E+654"); }, "z:double?");
1073 AssertThrows<OverflowException> (() => { float z = (float) new XElement("z", @double); }, "z:float");
1074 AssertThrows<OverflowException> (() => { float? z = (float?) new XElement("z", @double); }, "z:float?");
1075 AssertThrows<OverflowException> (() => { int z = (int) new XElement("z", @long); }, "z:int");
1076 AssertThrows<OverflowException> (() => { int? z = (int?) new XElement("z", @long); }, "z:int?");
1077 AssertThrows<OverflowException> (() => { long z = (long) new XElement("z", @decimal); }, "z:long");
1078 AssertThrows<OverflowException> (() => { long? z = (long?) new XElement("z", @decimal); }, "z:long?");
1079 AssertThrows<OverflowException> (() => { uint z = (uint) new XElement("z", @ulong); }, "z:uint");
1080 AssertThrows<OverflowException> (() => { uint? z = (uint?) new XElement("z", @ulong); }, "z:uint?");
1081 AssertThrows<OverflowException> (() => { ulong z = (ulong) new XElement("z", -@decimal); }, "z:ulong");
1082 AssertThrows<OverflowException> (() => { ulong? z = (ulong?) new XElement("z", -@decimal); }, "z:ulong?");
1086 public void CastBooleans ()
1088 Assert.IsNotNull ((bool?) new XElement ("fq", "false"), "#1a");
1089 Assert.AreEqual (false, ((bool?) new XElement ("fq", "false")).Value, "#1b");
1090 Assert.IsNotNull ((bool?) new XElement ("tq", "true"), "#2a");
1091 Assert.AreEqual (true, ((bool?) new XElement ("tq", "true")).Value, "#2b");
1092 Assert.IsNotNull ((bool?) new XElement ("Fq", "False"), "#3a");
1093 Assert.AreEqual (false, ((bool?) new XElement ("Fq", "False")).Value, "#3b");
1094 Assert.IsNotNull ((bool?) new XElement ("Tq", "True"), "#4a");
1095 Assert.AreEqual (true, ((bool?) new XElement ("Tq", "True")).Value, "#4b");
1096 Assert.IsNotNull ((bool?) new XElement ("Fs", " False \t \r "), "#5a");
1097 Assert.AreEqual (false, ((bool?) new XElement ("Fs", " False \t \r ")).Value, "#5b");
1098 Assert.IsNotNull ((bool?) new XElement ("Ts", " \t True \n "), "#6a");
1099 Assert.AreEqual (true, ((bool?) new XElement ("Ts", " \t True \n ")).Value, "#6b");
1100 Assert.AreEqual (false, (bool) new XElement ("f", "false"), "#7");
1101 Assert.AreEqual (true, (bool) new XElement ("t", "true"), "#8");
1102 Assert.AreEqual (false, (bool) new XElement ("F", "False"), "#9");
1103 Assert.AreEqual (true, (bool) new XElement ("T", "True"), "#10");
1104 Assert.AreEqual (false, (bool)new XElement("fs", " false "), "#11");
1105 Assert.AreEqual (true, (bool)new XElement("ts", " true "), "#12");
1106 Assert.IsNotNull ((bool?) new XElement ("Tc", new XCData (" \t True \n ")), "#13a");
1107 Assert.AreEqual (true, ((bool?) new XElement ("Tc", new XCData (" \t True \n "))).Value, "#13b");
1108 Assert.AreEqual (false, (bool)new XElement ("fc", new XCData (" false ")), "#14");
1109 Assert.IsNotNull ((bool?) new XElement ("x", true), "#15a");
1110 Assert.IsTrue (((bool?) new XElement ("x", true)).Value, "#15b");
1111 Assert.IsTrue ((bool) new XElement ("x", true), "#15c");
1112 Assert.IsNotNull ((bool?) new XElement ("x", false), "#16a");
1113 Assert.IsFalse (((bool?) new XElement ("x", false)).Value, "#16b");
1114 Assert.IsFalse ((bool) new XElement ("x", false), "#16c");
1118 public void CastGuids ()
1120 Guid rb = new Guid (new byte[16] { 0x9A, 0xBF, 0xCE, 0x7E, 0x07, 0x29, 0x9C, 0x43, 0x80, 0x7D, 0x48, 0x20, 0xB9, 0x19, 0xEA, 0x57 });
1121 Guid rd = new Guid (new byte[16] { 0x21, 0x5B, 0x57, 0x26, 0xCD, 0x14, 0x5E, 0x44, 0x8F, 0xFA, 0xE2, 0xBC, 0x24, 0x7B, 0x2E, 0xC9 });
1122 Guid rn = new Guid (new byte[16] { 0xF9, 0x46, 0x41, 0xA8, 0xA5, 0x03, 0xF1, 0x4A, 0xAD, 0x97, 0x7B, 0xC7, 0x79, 0x57, 0x2B, 0x79 });
1123 Guid rp = new Guid (new byte[16] { 0x51, 0x6B, 0x8A, 0x17, 0xEF, 0x11, 0xFB, 0x48, 0x83, 0xBD, 0x57, 0xB4, 0x99, 0xF9, 0xC1, 0xE6 });
1124 Guid rz = Guid.Empty;
1125 Guid rx = Guid.NewGuid();
1127 XElement b = new XElement ("b", " {7ECEBF9A-2907-439c-807D-4820B919EA57}");
1128 XElement d = new XElement ("d", "26575b21-14cd-445e-8ffa-e2bc247b2ec9");
1129 XElement n = new XElement ("n", "a84146f903A54af1ad977bC779572b79\r\n");
1130 XElement p = new XElement ("p", " (178a6b51-11ef-48fb-83bd-57b499f9c1e6) \t ");
1131 XElement z = new XElement ("z", " \t \n 00000000-0000-0000-0000-000000000000 ");
1132 XElement x = new XElement ("x", rx);
1134 Assert.IsNotNull ((Guid?) new XElement (b), "#1a");
1135 Assert.AreEqual (rb, ((Guid?) new XElement (b)).Value, "#1b");
1136 Assert.AreEqual (rb, (Guid) new XElement (b), "#1c");
1137 Assert.IsNotNull ((Guid?) new XElement (d), "#2a");
1138 Assert.AreEqual (rd, ((Guid?) new XElement (d)).Value, "#2b");
1139 Assert.AreEqual (rd, (Guid) new XElement (d), "#2c");
1140 Assert.IsNotNull ((Guid?) new XElement (n), "#3a");
1141 Assert.AreEqual (rn, ((Guid?) new XElement (n)).Value, "#3b");
1142 Assert.AreEqual (rn, (Guid) new XElement (n), "#3c");
1143 Assert.IsNotNull ((Guid?) new XElement (p), "#4a");
1144 Assert.AreEqual (rp, ((Guid?) new XElement (p)).Value, "#4b");
1145 Assert.AreEqual (rp, (Guid) new XElement (p), "#4c");
1146 Assert.IsNotNull ((Guid?) new XElement (z), "#5a");
1147 Assert.AreEqual (rz, ((Guid?) new XElement (z)).Value, "#5b");
1148 Assert.AreEqual (rz, (Guid) new XElement (z), "#5c");
1149 Assert.IsNotNull ((Guid?) new XElement (x), "#6a");
1150 Assert.AreEqual (rx, ((Guid?) new XElement (x)).Value, "#6b");
1151 Assert.AreEqual (rx, (Guid) new XElement (x), "#6c");
1155 public void CastDateTimes ()
1157 DateTime ra = new DateTime (1987, 1, 23, 21, 45, 36, 89, DateTimeKind.Unspecified);
1158 DateTime rb = new DateTime (2001, 2, 3, 4, 5, 6, 789, DateTimeKind.Local);
1159 DateTime rc = new DateTime (2010, 1, 2, 0, 0, 0, 0, DateTimeKind.Utc);
1160 DateTime rd = new DateTime (1956, 11, 2, 0, 34, 0);
1161 DateTime rx = DateTime.Now;
1162 DateTime rz = DateTime.UtcNow;
1164 XElement a = new XElement ("a", "1987-01-23T21:45:36.089");
1165 XElement b = new XElement ("b", "2001-02-03T04:05:06.789" + DateTime.Now.ToString ("zzz"));
1166 XElement c = new XElement ("c", "2010-01-02T00:00:00Z");
1167 XElement d = new XElement ("d", " Nov 2, 1956 12:34 AM \r\n \t");
1168 XElement x = new XElement ("x", rx);
1169 XElement z = new XElement ("z", rz);
1171 Assert.IsNotNull ((DateTime?) new XElement (a), "#1a");
1172 Assert.AreEqual (ra, ((DateTime?) new XElement (a)).Value, "#1b");
1173 Assert.AreEqual (ra, (DateTime) new XElement (a), "#1c");
1174 Assert.IsNotNull ((DateTime?) new XElement (b), "#2a");
1175 Assert.AreEqual (rb, ((DateTime?) new XElement (b)).Value, "#2b");
1176 Assert.AreEqual (rb, (DateTime) new XElement (b), "#2c");
1177 Assert.IsNotNull ((DateTime?) new XElement (c), "#3a");
1178 Assert.AreEqual (rc, ((DateTime?) new XElement (c)).Value, "#3b");
1179 Assert.AreEqual (rc, (DateTime) new XElement (c), "#3c");
1180 Assert.IsNotNull ((DateTime?) new XElement (d), "#4a");
1181 Assert.AreEqual (rd, ((DateTime?) new XElement (d)).Value, "#4b");
1182 Assert.AreEqual (rd, (DateTime) new XElement (d), "#4c");
1183 Assert.IsNotNull ((DateTime?) new XElement (x), "#5a");
1184 Assert.AreEqual (rx, ((DateTime?) new XElement (x)).Value, "#5b");
1185 Assert.AreEqual (rx, (DateTime) new XElement (x), "#5c");
1186 Assert.IsNotNull ((DateTime?) new XElement (z), "#6a");
1187 Assert.AreEqual (rz, ((DateTime?) new XElement (z)).Value, "#6b");
1188 Assert.AreEqual (rz, (DateTime) new XElement (z), "#6c");
1192 public void CastDateTimeOffsets ()
1194 DateTimeOffset ra = new DateTimeOffset (1987, 1, 23, 21, 45, 36, 89, TimeSpan.FromHours(+13.75)); // e.g., Chatham Islands (daylight-savings time)
1195 DateTimeOffset rb = new DateTimeOffset (2001, 2, 3, 4, 5, 6, 789, DateTimeOffset.Now.Offset); // Local time
1196 DateTimeOffset rc = new DateTimeOffset (2010, 1, 2, 0, 0, 0, 0, TimeSpan.Zero); // UTC
1197 DateTimeOffset rd = new DateTimeOffset (1956, 11, 2, 12, 34, 10, TimeSpan.FromHours(-3.5));
1198 DateTimeOffset rx = DateTimeOffset.Now;
1199 DateTimeOffset rz = DateTimeOffset.UtcNow;
1201 XElement a = new XElement ("a", "1987-01-23T21:45:36.089+13:45");
1202 XElement b = new XElement ("b", "2001-02-03T04:05:06.789" + DateTimeOffset.Now.ToString ("zzz"));
1203 XElement c = new XElement ("c", "2010-01-02T00:00:00Z");
1204 XElement d = new XElement ("d", " Nov 2, 1956 12:34:10 PM -3:30 \r\n \t");
1205 XElement x = new XElement ("x", rx);
1206 XElement z = new XElement ("z", rz);
1208 Assert.IsNotNull ((DateTimeOffset?) new XElement (a), "#1a");
1209 Assert.AreEqual (ra, ((DateTimeOffset?) new XElement (a)).Value, "#1b");
1210 Assert.AreEqual (ra, (DateTimeOffset) new XElement (a), "#1c");
1211 Assert.IsNotNull ((DateTimeOffset?) new XElement (b), "#2a");
1212 Assert.AreEqual (rb, ((DateTimeOffset?) new XElement (b)).Value, "#2b");
1213 Assert.AreEqual (rb, (DateTimeOffset) new XElement (b), "#2c");
1214 Assert.IsNotNull ((DateTimeOffset?) new XElement (c), "#3a");
1215 Assert.AreEqual (rc, ((DateTimeOffset?) new XElement (c)).Value, "#3b");
1216 Assert.AreEqual (rc, (DateTimeOffset) new XElement (c), "#3c");
1217 AssertThrows<FormatException> (() => { DateTimeOffset? r = (DateTimeOffset?) new XElement (d); }, "#4a");
1218 AssertThrows<FormatException> (() => { DateTimeOffset r = (DateTimeOffset) new XElement (d); }, "#4b");
1219 Assert.AreEqual (rd, DateTimeOffset.Parse (d.Value), "#4c");
1220 Assert.IsNotNull ((DateTimeOffset?) new XElement (x), "#5a");
1221 Assert.AreEqual (rx, ((DateTimeOffset?) new XElement (x)).Value, "#5b");
1222 Assert.AreEqual (rx, (DateTimeOffset) new XElement (x), "#5c");
1223 Assert.IsNotNull ((DateTimeOffset?) new XElement (z), "#6a");
1224 Assert.AreEqual (rz, ((DateTimeOffset?) new XElement (z)).Value, "#6b");
1225 Assert.AreEqual (rz, (DateTimeOffset) new XElement (z), "#6c");
1229 public void CastTimeSpans ()
1231 TimeSpan ra = new TimeSpan (23, 21, 45, 36, 89);
1232 TimeSpan rb = -new TimeSpan (3, 4, 5, 6, 789);
1233 TimeSpan rc = new TimeSpan (2, 0, 0, 0, 0);
1234 TimeSpan rd = new TimeSpan (0, 0, 0, 1);
1235 TimeSpan rx = DateTimeOffset.Now.Offset;
1236 TimeSpan rz = TimeSpan.Zero;
1238 XElement a = new XElement ("a", "P23DT21H45M36.089S");
1239 XElement b = new XElement ("b", "-P3DT4H5M6.789S");
1240 XElement c = new XElement ("c", "P2D");
1241 XElement d = new XElement ("d", "PT1S");
1242 XElement x = new XElement ("x", rx);
1243 XElement z = new XElement ("z", rz);
1245 Assert.IsNotNull ((TimeSpan?) new XElement (a), "#1a");
1246 Assert.AreEqual (ra, ((TimeSpan?) new XElement (a)).Value, "#1b");
1247 Assert.AreEqual (ra, (TimeSpan) new XElement (a), "#1c");
1248 Assert.IsNotNull ((TimeSpan?) new XElement (b), "#2a");
1249 Assert.AreEqual (rb, ((TimeSpan?) new XElement (b)).Value, "#2b");
1250 Assert.AreEqual (rb, (TimeSpan) new XElement (b), "#2c");
1251 Assert.IsNotNull ((TimeSpan?) new XElement (c), "#3a");
1252 Assert.AreEqual (rc, ((TimeSpan?) new XElement (c)).Value, "#3b");
1253 Assert.AreEqual (rc, (TimeSpan) new XElement (c), "#3c");
1254 Assert.IsNotNull ((TimeSpan?) new XElement (d), "#4a");
1255 Assert.AreEqual (rd, ((TimeSpan?) new XElement (d)).Value, "#4b");
1256 Assert.AreEqual (rd, (TimeSpan) new XElement (d), "#4c");
1257 Assert.IsNotNull ((TimeSpan?) new XElement (x), "#5a");
1258 Assert.AreEqual (rx, ((TimeSpan?) new XElement (x)).Value, "#5b");
1259 Assert.AreEqual (rx, (TimeSpan) new XElement (x), "#5c");
1260 Assert.IsNotNull ((TimeSpan?) new XElement (z), "#6a");
1261 Assert.AreEqual (rz, ((TimeSpan?) new XElement (z)).Value, "#6b");
1262 Assert.AreEqual (rz, (TimeSpan) new XElement (z), "#6c");
1264 #pragma warning restore 219
1267 public void Value ()
1269 // based on bug #360858
1270 XElement a = new XElement("root",
1271 new XElement ("foo"),
1273 new XComment ("comment"),
1274 new XElement ("bar"));
1275 Assert.AreEqual ("Linux&Windows", a.Value);
1279 [ExpectedException (typeof (ArgumentException))]
1280 public void SetValueXAttribute ()
1282 new XElement ("foo").SetValue (new XAttribute ("foo", "bar"));
1286 [ExpectedException (typeof (ArgumentException))]
1287 public void SetValueXDocumnent ()
1289 new XElement ("foo").SetValue (new XDocument ());
1293 // LAMESPEC: there is no reason to not reject XDeclaration while it rejects XDocument.
1294 [ExpectedException (typeof (ArgumentException))]
1295 [Category ("NotDotNet")]
1296 public void SetValueXDeclaration ()
1298 var el = new XElement ("foo");
1299 el.SetValue (new XDeclaration ("1.0", null, null));
1300 Assert.AreEqual ("<?xml version=\"1.0\"?>", el.Value);
1304 [ExpectedException (typeof (ArgumentNullException))]
1305 public void SetValueNull ()
1307 new XElement ("foo", "text").SetValue (null);
1311 public void AddSameInstance () // bug #392063
1313 XElement root = new XElement (XName.Get ("Root", ""));
1314 XElement child = new XElement (XName.Get ("Child", ""));
1322 [ExpectedException (typeof (InvalidOperationException))]
1323 public void AddSameInstance2 ()
1325 XElement root = new XElement (XName.Get ("Root"));
1326 XAttribute attr = new XAttribute (XName.Get ("a"), "v");
1329 root.Add (attr); // duplicate attribute
1334 public void AddAttributeFromDifferentTree ()
1336 XElement e1 = new XElement (XName.Get ("e1"));
1337 XElement e2 = new XElement (XName.Get ("e2"));
1338 XAttribute attr = new XAttribute (XName.Get ("a"), "v");
1342 Assert.AreEqual ("<e1 a=\"v\" />", e1.ToString (), "#1");
1343 Assert.AreEqual ("<e2 a=\"v\" />", e2.ToString (), "#2");
1347 public void SavePreservePrefixes ()
1349 var x = XDocument.Parse (@"
1350 <xxx:a xmlns:xxx='http://www.foobar.com'>
1351 <xxx:b>blah blah blah</xxx:b>
1353 StringWriter sw = new StringWriter ();
1354 x.Save (sw, SaveOptions.DisableFormatting);
1355 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 ());
1359 public void LoadFromXmlTextReader ()
1361 var foo = XElement.Load (new XmlTextReader (new StringReader ("<foo></foo>")));
1362 Assert.IsNotNull (foo);
1366 public void ReplaceNodes ()
1368 var inputXml = "<Foo><C><Three>3</Three><Two></Two><One/></C><B><Aaa/><Yyy/><fff/></B><A Attrib=\"Hello World\"/></Foo>";
1369 var reader = XmlReader.Create (new StringReader (inputXml), new XmlReaderSettings ());
1370 XDocument doc = XDocument.Load (reader);
1371 var result = doc.Root.Elements ().OrderBy (el => el.Name.ToString());
1372 Assert.AreEqual (3, result.Count (), "#1");
1373 doc.Root.FirstNode.Remove ();
1374 Assert.AreEqual (2, result.Count (), "#2");
1376 XContainer container = doc.Root;
1377 container.ReplaceNodes (result);
1379 Assert.AreEqual (2, container.Elements ().Count (), "#3");