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;
29 using System.Globalization;
32 using System.Threading;
34 using System.Xml.Linq;
36 using NUnit.Framework;
38 namespace MonoTests.System.Xml.Linq
41 public class XElementTest
43 [Test] // xml declaration is skipped.
44 public void LoadWithXmldecl ()
46 string xml = "<?xml version='1.0'?><root />";
47 XElement.Load (new StringReader (xml));
53 string xml = "<root><foo/></root>";
55 XElement el = XElement.Load (new StringReader (xml));
56 XElement first = el.FirstNode as XElement;
57 Assert.IsNotNull (first, "#1");
58 Assert.IsTrue (el.LastNode is XElement, "#2");
59 Assert.IsNull (el.NextNode, "#3");
60 Assert.IsNull (el.PreviousNode, "#4");
61 Assert.AreEqual (1, new List<XNode> (el.Nodes ()).Count, "#5");
62 Assert.AreEqual (el, first.Parent, "#6");
63 Assert.AreEqual (first, el.LastNode, "#7");
65 Assert.AreEqual ("root", el.Name.ToString (), "#8");
66 Assert.AreEqual ("foo", first.Name.ToString (), "#9");
67 Assert.IsFalse (el.Attributes ().GetEnumerator ().MoveNext (), "#10");
71 [ExpectedException (typeof (InvalidOperationException))]
72 public void LoadInvalid ()
75 XmlReaderSettings s = new XmlReaderSettings ();
76 s.ConformanceLevel = ConformanceLevel.Fragment;
78 XElement.Load (XmlReader.Create (new StringReader (xml), s));
82 public void PrecedingWhitespaces ()
84 string xml = " <root/>";
85 XmlReaderSettings s = new XmlReaderSettings ();
86 s.ConformanceLevel = ConformanceLevel.Fragment;
88 XElement.Load (XmlReader.Create (new StringReader (xml), s));
92 public void PrecedingWhitespaces2 ()
94 string xml = " <root/>";
95 XmlReaderSettings s = new XmlReaderSettings ();
96 s.ConformanceLevel = ConformanceLevel.Fragment;
98 XmlReader r = XmlReader.Create (new StringReader (xml), s);
99 r.Read (); // at whitespace
106 string xml = "<root>foo</root>";
108 XElement el = XElement.Load (new StringReader (xml));
109 XText first = el.FirstNode as XText;
110 Assert.IsNotNull (first, "#1");
111 Assert.IsTrue (el.LastNode is XText, "#2");
112 Assert.AreEqual (1, new List<XNode> (el.Nodes ()).Count, "#3");
113 Assert.AreEqual (el, first.Parent, "#4");
114 Assert.AreEqual (first, el.LastNode, "#5");
116 Assert.AreEqual ("foo", first.Value, "#6");
120 [ExpectedException (typeof (ArgumentException))]
121 public void AddDocumentTypeToElement ()
123 XElement el = new XElement (XName.Get ("foo"));
124 el.Add (new XDocumentType ("foo", null, null, null));
128 [ExpectedException (typeof (ArgumentException))]
129 [Category ("NotDotNet")]
130 public void AddXDeclarationToElement ()
132 XElement el = new XElement (XName.Get ("foo"));
133 // LAMESPEC: in .NET, XDeclaration is not treated as
134 // invalid, and converted to a string without error.
135 el.Add (new XDeclaration ("1.0", null, null));
139 public void SetAttribute ()
141 XElement el = new XElement (XName.Get ("foo"));
142 el.SetAttributeValue (XName.Get ("a1"), "v1");
143 XAttribute a = el.FirstAttribute;
144 Assert.IsNotNull (a, "#1-1");
145 Assert.AreEqual (el, a.Parent, "#1-2");
146 Assert.IsNotNull (el.LastAttribute, "#1-3");
147 Assert.AreEqual (a, el.LastAttribute, "#1-4");
148 Assert.AreEqual ("a1", a.Name.LocalName, "#1-5");
149 Assert.AreEqual ("v1", a.Value, "#1-6");
150 Assert.IsNull (a.PreviousAttribute, "#1-7");
151 Assert.IsNull (a.NextAttribute, "#1-8");
153 el.SetAttributeValue (XName.Get ("a2"), "v2");
154 Assert.IsFalse (el.FirstAttribute == el.LastAttribute, "#2-1");
155 Assert.AreEqual ("a2", el.LastAttribute.Name.LocalName, "#2-2");
157 el.SetAttributeValue (XName.Get ("a1"), "v3");
158 XAttribute b = el.FirstAttribute;
159 Assert.IsNotNull (b, "#2-3");
160 Assert.IsNotNull (el.LastAttribute, "#2-4");
161 Assert.AreEqual ("a1", b.Name.LocalName, "#2-5");
162 Assert.AreEqual ("v3", b.Value, "#2-6");
163 Assert.AreEqual (a, b, "#2-7");
164 XAttribute c = el.LastAttribute;
165 Assert.AreEqual (a, c.PreviousAttribute, "#2-8");
168 Assert.IsNull (a.Parent, "#3-1");
169 Assert.IsNull (a.PreviousAttribute, "#3-2");
170 Assert.IsNull (a.NextAttribute, "#3-3");
171 Assert.IsNull (c.PreviousAttribute, "#3-4");
172 Assert.IsNull (c.NextAttribute, "#3-5");
174 el.RemoveAttributes ();
175 Assert.IsFalse (el.HasAttributes, "#4-1");
176 Assert.IsNull (b.Parent, "#4-2");
177 Assert.IsNull (c.Parent, "#4-3");
178 Assert.IsNull (el.FirstAttribute, "#4-4");
179 Assert.IsNull (el.LastAttribute, "#4-5");
183 public void AddAfterSelf ()
185 XElement el = XElement.Parse ("<root><foo/><bar/></root>");
186 el.FirstNode.AddAfterSelf ("text");
187 XText t = el.FirstNode.NextNode as XText;
188 Assert.IsNotNull (t, "#1");
189 Assert.AreEqual ("text", t.Value, "#2");
190 XElement bar = t.NextNode as XElement;
191 Assert.IsNotNull (bar, "#3");
192 Assert.AreEqual ("bar", bar.Name.LocalName, "#4");
196 public void AddAfterSelfList ()
198 XElement el = XElement.Parse ("<root><foo/><bar/></root>");
199 el.FirstNode.AddAfterSelf (new XText [] {
203 XText t = el.FirstNode.NextNode as XText;
204 Assert.IsNotNull (t, "#1");
205 Assert.AreEqual ("t1", t.Value, "#2");
206 Assert.AreEqual ("t2", ((XText) t.NextNode).Value, "#3");
207 Assert.AreEqual ("t3", ((XText) t.NextNode.NextNode).Value, "#4");
208 XElement bar = t.NextNode.NextNode.NextNode as XElement;
209 Assert.IsNotNull (bar, "#5");
210 Assert.AreEqual ("bar", bar.Name.LocalName, "#6");
214 [ExpectedException (typeof (ArgumentException))]
215 public void AddAfterSelfAttribute ()
217 var el = new XElement ("root", new XElement ("child"));
218 var el2 = el.FirstNode as XElement;
219 el2.AddAfterSelf (new XAttribute ("foo", "bar"));
223 [ExpectedException (typeof (ArgumentException))]
224 public void AddAfterSelfXDocument ()
226 var el = new XElement ("root", new XElement ("child"));
227 var el2 = el.FirstNode as XElement;
228 el2.AddAfterSelf (new XDocument ());
232 [ExpectedException (typeof (ArgumentException))]
233 [Category ("NotDotNet")]
234 [Category ("NotWorking")]
235 // LAMESPEC: there is no reason to not reject XDeclaration while it rejects XDocument.
236 public void AddAfterSelfXDeclaration ()
238 var el = new XElement ("root", new XElement ("child"));
239 var el2 = el.FirstNode as XElement;
240 el2.AddAfterSelf (new XDeclaration ("1.0", null, null));
244 public void AddAfterSelfCollection ()
246 var el = new XElement ("root", new XElement ("child"));
247 el.FirstNode.AddAfterSelf (new List<XElement> (new XElement [] {new XElement ("foo"), new XElement ("bar")}));
248 Assert.AreEqual ("<root><child /><foo /><bar /></root>", el.ToString (SaveOptions.DisableFormatting), "#1");
249 Assert.AreEqual ("bar", (el.LastNode as XElement).Name.LocalName, "#2");
253 public void AddAfterSelfJoinsStringAfterText ()
255 var el = XElement.Parse ("<foo>text1</foo>");
256 el.LastNode.AddAfterSelf ("text2");
257 el.LastNode.AddAfterSelf (new XText ("text3"));
258 IEnumerator<XNode> e = el.Nodes ().GetEnumerator ();
259 Assert.IsTrue (e.MoveNext (), "#1");
260 Assert.AreEqual ("text1text2", e.Current.ToString (), "#2");
261 Assert.IsTrue (e.MoveNext (), "#3");
262 Assert.AreEqual ("text3", e.Current.ToString (), "#4");
263 Assert.IsFalse (e.MoveNext (), "#5");
267 public void AddBeforeSelf ()
269 XElement el = XElement.Parse ("<root><foo/><bar/></root>");
270 el.FirstNode.AddBeforeSelf ("text");
271 XText t = el.FirstNode as XText;
272 Assert.IsNotNull (t, "#1");
273 Assert.AreEqual ("text", t.Value, "#2");
274 XElement foo = t.NextNode as XElement;
275 Assert.IsNotNull (foo, "#3");
276 Assert.AreEqual ("foo", foo.Name.LocalName, "#4");
280 public void AddBeforeSelfList ()
282 XElement el = XElement.Parse ("<root><foo/><bar/></root>");
283 el.FirstNode.AddBeforeSelf (new XText [] {
287 XText t = el.FirstNode as XText;
288 Assert.IsNotNull (t, "#1");
289 Assert.AreEqual ("t1", t.Value, "#2");
290 Assert.AreEqual ("t2", ((XText) t.NextNode).Value, "#3");
291 Assert.AreEqual ("t3", ((XText) t.NextNode.NextNode).Value, "#4");
292 XElement foo = t.NextNode.NextNode.NextNode as XElement;
293 Assert.IsNotNull (foo, "#5");
294 Assert.AreEqual ("foo", foo.Name.LocalName, "#6");
298 public void AddBeforeSelfList2 ()
300 XElement el = XElement.Parse ("<root><foo/><bar/></root>");
301 el.FirstNode.AddBeforeSelf ("t1", "t2", "t3");
302 XText t = el.FirstNode as XText;
303 Assert.IsNotNull (t, "#1");
304 Assert.AreEqual ("t1t2t3", t.Value, "#2");
305 XElement foo = t.NextNode as XElement;
306 Assert.IsNotNull (foo, "#3");
307 Assert.AreEqual ("foo", foo.Name.LocalName, "#4");
311 public void AddJoinsStringAfterText ()
313 var el = XElement.Parse ("<foo>text1</foo>");
315 el.Add (new XText ("text3"));
316 IEnumerator<XNode> e = el.Nodes ().GetEnumerator ();
317 Assert.IsTrue (e.MoveNext (), "#1");
318 Assert.AreEqual ("text1text2", e.Current.ToString (), "#2");
319 Assert.IsTrue (e.MoveNext (), "#3");
320 Assert.AreEqual ("text3", e.Current.ToString (), "#4");
321 Assert.IsFalse (e.MoveNext (), "#5");
325 [ExpectedException (typeof (InvalidOperationException))]
326 public void AddDuplicateAttribute ()
328 var el = new XElement ("foo",
329 new XAttribute ("bar", "baz"));
330 el.Add (new XAttribute ("bar", "baz"));
334 public void ReplaceWith ()
336 XElement el = XElement.Parse ("<root><foo/><bar/></root>");
337 XNode fc = el.FirstNode;
338 fc.ReplaceWith ("test");
339 XText t = el.FirstNode as XText;
340 Assert.IsNotNull (t, "#1");
341 Assert.AreEqual ("test", t.Value, "#2");
345 public void ReplaceAll ()
347 XElement el = XElement.Parse ("<root><foo/><bar/></root>");
348 el.ReplaceAll ("test");
349 XText t = el.FirstNode as XText;
350 Assert.IsNotNull (t, "#1");
351 Assert.AreEqual ("test", t.Value, "#2");
352 Assert.AreEqual (1, new List<XNode> (el.Nodes ()).Count, "#3");
356 public void ReplaceAllList ()
358 XElement el = XElement.Parse ("<root><foo/><bar/></root>");
362 new XText ("test3"));
363 XText t = el.FirstNode as XText;
364 Assert.IsNotNull (t, "#1");
365 Assert.AreEqual ("test1", t.Value, "#2");
366 t = el.LastNode as XText;
367 Assert.IsNotNull (t, "#3");
368 Assert.AreEqual ("test3", t.Value, "#4");
369 Assert.AreEqual (3, new List<XNode> (el.Nodes ()).Count, "#5");
373 public void ReplaceAttributes ()
375 XElement el = XElement.Parse ("<root x='y'><foo a='b'/></root>");
376 Assert.IsTrue (el.Attributes ().GetEnumerator ().MoveNext (), "#0");
377 el.ReplaceAttributes ("test");
378 Assert.IsTrue (el.FirstNode is XElement, "#1");
379 Assert.IsTrue (el.LastNode is XText, "#2");
380 Assert.IsFalse (el.Attributes ().GetEnumerator ().MoveNext (), "#3");
384 public void GetDefaultNamespace ()
386 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>");
387 XNamespace ns1 = XNamespace.Get ("urn:foo");
388 Assert.AreEqual (ns1, el.GetDefaultNamespace (), "#1");
389 XElement foo = (XElement) el.FirstNode;
390 Assert.AreEqual (ns1, foo.GetDefaultNamespace (), "#2");
391 Assert.AreEqual (ns1, ((XElement) foo.FirstNode).GetDefaultNamespace (), "#3");
392 XElement bar = (XElement) foo.NextNode;
393 Assert.AreEqual (ns1, bar.GetDefaultNamespace (), "#4");
394 Assert.AreEqual (ns1, ((XElement) bar.FirstNode).GetDefaultNamespace (), "#5");
395 XElement baz = (XElement) bar.NextNode;
396 Assert.AreEqual (XNamespace.Get (String.Empty), baz.GetDefaultNamespace (), "#6");
397 Assert.AreEqual (XNamespace.Get (String.Empty), ((XElement) baz.FirstNode).GetDefaultNamespace (), "#7");
401 public void GetPrefixNamespace ()
403 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>");
404 XNamespace ns1 = XNamespace.Get ("urn:foo");
405 XNamespace ns2 = XNamespace.Get ("urn:bar");
406 Assert.AreEqual (ns1, el.GetNamespaceOfPrefix ("x"), "#1-1");
407 Assert.AreEqual ("x", el.GetPrefixOfNamespace (ns1), "#1-2");
408 XElement foo = (XElement) el.FirstNode;
409 Assert.AreEqual (ns1, foo.GetNamespaceOfPrefix ("x"), "#2-1");
410 Assert.AreEqual ("x", foo.GetPrefixOfNamespace (ns1), "#2-2");
411 Assert.AreEqual (ns1, ((XElement) foo.FirstNode).GetNamespaceOfPrefix ("x"), "#3-1");
412 Assert.AreEqual ("x", ((XElement) foo.FirstNode).GetPrefixOfNamespace (ns1), "#3-2");
413 XElement bar = (XElement) foo.NextNode;
414 Assert.AreEqual (ns2, bar.GetNamespaceOfPrefix ("x"), "#4-1");
415 Assert.AreEqual ("x", bar.GetPrefixOfNamespace (ns2), "#4-2");
416 Assert.AreEqual (null, bar.GetPrefixOfNamespace (ns1), "#4-3");
417 Assert.AreEqual (ns2, ((XElement) bar.FirstNode).GetNamespaceOfPrefix ("x"), "#5-1");
418 Assert.AreEqual ("x", ((XElement) bar.FirstNode).GetPrefixOfNamespace (ns2), "#5-2");
419 Assert.AreEqual (null, ((XElement) bar.FirstNode).GetPrefixOfNamespace (ns1), "#5-3");
422 #pragma warning disable 219
424 public void CastNulls ()
426 const XElement a = null;
428 Assert.AreEqual (null, (bool?) a, "bool?");
429 Assert.AreEqual (null, (DateTime?) a, "DateTime?");
430 Assert.AreEqual (null, (DateTimeOffset?) a, "DateTimeOffset?");
431 Assert.AreEqual (null, (decimal?) a, "decimal?");
432 Assert.AreEqual (null, (double?) a, "double?");
433 Assert.AreEqual (null, (float?) a, "float?");
434 Assert.AreEqual (null, (Guid?) a, "Guid?");
435 Assert.AreEqual (null, (int?) a, "int?");
436 Assert.AreEqual (null, (long?) a, "long?");
437 Assert.AreEqual (null, (uint?) a, "uint?");
438 Assert.AreEqual (null, (ulong?) a, "ulong?");
439 Assert.AreEqual (null, (TimeSpan?) a, "TimeSpan?");
440 Assert.AreEqual (null, (string) a, "string");
441 AssertThrows<ArgumentNullException> (() => { bool z = (bool) a; }, "bool");
442 AssertThrows<ArgumentNullException> (() => { DateTime z = (DateTime) a; }, "DateTime");
443 AssertThrows<ArgumentNullException> (() => { DateTimeOffset z = (DateTimeOffset) a; }, "DateTimeOffset");
444 AssertThrows<ArgumentNullException> (() => { decimal z = (decimal) a; }, "decimal");
445 AssertThrows<ArgumentNullException> (() => { double z = (double) a; }, "double");
446 AssertThrows<ArgumentNullException> (() => { float z = (float) a; }, "float");
447 AssertThrows<ArgumentNullException> (() => { Guid z = (Guid) a; }, "Guid");
448 AssertThrows<ArgumentNullException> (() => { int z = (int) a; }, "int");
449 AssertThrows<ArgumentNullException> (() => { long z = (long) a; }, "long");
450 AssertThrows<ArgumentNullException> (() => { uint z = (uint) a; }, "uint");
451 AssertThrows<ArgumentNullException> (() => { ulong z = (ulong) a; }, "ulong");
452 AssertThrows<ArgumentNullException> (() => { TimeSpan z = (TimeSpan) a; }, "TimeSpan");
456 /// Provides functionality similar to Assert.Throws that is available on newer versions of NUnit.
458 private static T AssertThrows<T> (Action code, string message, params object[] args) where T : Exception
460 Exception actual = null;
463 } catch (Exception exception) {
466 Assert.That (actual, new NUnit.Framework.Constraints.ExactTypeConstraint (typeof (T)), message, args);
471 public void CastEmpties ()
473 XElement a = new XElement ("a");
475 // Verify expected "cloning" and "empty" behaviour as prerequisites
476 Assert.IsTrue (a.IsEmpty, "#1-1");
477 Assert.IsTrue (new XElement (a).IsEmpty, "#1-2");
478 Assert.AreEqual (String.Empty, a.Value, "#2-1");
479 Assert.AreEqual (String.Empty, new XElement (a).Value, "#2-2");
480 Assert.AreNotSame (a, new XElement (a), "#3-1");
481 Assert.AreEqual (a.ToString (), new XElement (a).ToString (), "#3-2");
482 Assert.AreEqual ("<a />", a.ToString (), "#3-3");
483 Assert.AreEqual (a.ToString (), new XElement ("a", null).ToString (), "#3-4");
485 // Execute the primary assertions of this test
486 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (a); }, "bool?");
487 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (a); }, "DateTime?");
488 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (a); }, "DateTimeOffset?");
489 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (a); }, "decimal?");
490 AssertThrows<FormatException> (() => { double? z = (double?) new XElement (a); }, "double?");
491 AssertThrows<FormatException> (() => { float? z = (float?) new XElement (a); }, "float?");
492 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (a); }, "Guid?");
493 AssertThrows<FormatException> (() => { int? z = (int?) new XElement (a); }, "int?");
494 AssertThrows<FormatException> (() => { long? z = (long?) new XElement (a); }, "long?");
495 AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (a); }, "uint?");
496 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (a); }, "ulong?");
497 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (a); }, "TimeSpan?");
498 Assert.AreEqual (String.Empty, (string) new XElement (a), "string");
499 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (a); }, "bool");
500 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (a); }, "DateTime");
501 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (a); }, "DateTimeOffset");
502 AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (a); }, "decimal");
503 AssertThrows<FormatException> (() => { double z = (double) new XElement (a); }, "double");
504 AssertThrows<FormatException> (() => { float z = (float) new XElement (a); }, "float");
505 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (a); }, "Guid");
506 AssertThrows<FormatException> (() => { int z = (int) new XElement (a); }, "int");
507 AssertThrows<FormatException> (() => { long z = (long) new XElement (a); }, "long");
508 AssertThrows<FormatException> (() => { uint z = (uint) new XElement (a); }, "uint");
509 AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (a); }, "ulong");
510 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (a); }, "TimeSpan");
514 public void CastBlanks ()
516 XElement a = new XElement ("a", String.Empty);
517 XElement b = new XElement ("b", new XCData (string.Empty));
519 // Verify expected "cloning" and "blank" behaviour as prerequisites
520 Assert.IsFalse (a.IsEmpty, "#1-1a");
521 Assert.IsFalse (b.IsEmpty, "#1-1b");
522 Assert.IsFalse (new XElement (a).IsEmpty, "#1-2a");
523 Assert.IsFalse (new XElement (b).IsEmpty, "#1-2b");
524 Assert.AreEqual (String.Empty, a.Value, "#2-1a");
525 Assert.AreEqual (String.Empty, b.Value, "#2-1b");
526 Assert.AreEqual (String.Empty, new XElement (a).Value, "#2-2a");
527 Assert.AreEqual (String.Empty, new XElement (b).Value, "#2-2b");
528 Assert.AreNotSame (a, new XElement (a), "#3-1a");
529 Assert.AreNotSame (b, new XElement (b), "#3-1b");
530 Assert.AreEqual (a.ToString (), new XElement (a).ToString (), "#3-2a");
531 Assert.AreEqual (b.ToString (), new XElement (b).ToString (), "#3-2b");
532 Assert.AreEqual ("<a></a>", a.ToString (), "#3-3a");
533 Assert.AreEqual ("<b><![CDATA[]]></b>", b.ToString (), "#3-3b");
534 Assert.AreEqual (a.ToString (), new XElement ("a", "").ToString (), "#3-4a");
535 Assert.AreEqual (b.ToString (), new XElement ("b", new XCData ("")).ToString (), "#3-4b");
537 // Execute the primary assertions of this test
538 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (a); }, "a:bool?");
539 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (b); }, "b:bool?");
540 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (a); }, "a:DateTime?");
541 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (b); }, "b:DateTime?");
542 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (a); }, "a:DateTimeOffset?");
543 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (b); }, "b:DateTimeOffset?");
544 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (a); }, "a:decimal?");
545 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (b); }, "b:decimal?");
546 AssertThrows<FormatException> (() => { double? z = (double?) new XElement (a); }, "a:double?");
547 AssertThrows<FormatException> (() => { double? z = (double?) new XElement (b); }, "b:double?");
548 AssertThrows<FormatException> (() => { float? z = (float?) new XElement (a); }, "a:float?");
549 AssertThrows<FormatException> (() => { float? z = (float?) new XElement (b); }, "b:float?");
550 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (a); }, "a:Guid?");
551 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (b); }, "b:Guid?");
552 AssertThrows<FormatException> (() => { int? z = (int?) new XElement (a); }, "a:int?");
553 AssertThrows<FormatException> (() => { int? z = (int?) new XElement (b); }, "b:int?");
554 AssertThrows<FormatException> (() => { long? z = (long?) new XElement (a); }, "a:long?");
555 AssertThrows<FormatException> (() => { long? z = (long?) new XElement (b); }, "b:long?");
556 AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (a); }, "a:uint?");
557 AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (b); }, "b:uint?");
558 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (a); }, "a:ulong?");
559 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (b); }, "b:ulong?");
560 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (a); }, "a:TimeSpan?");
561 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (b); }, "b:TimeSpan?");
562 Assert.AreEqual (String.Empty, (string) new XElement (a), "a:string");
563 Assert.AreEqual (String.Empty, (string) new XElement (b), "b:string");
564 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (a); }, "a:bool");
565 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (b); }, "b:bool");
566 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (a); }, "a:DateTime");
567 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (b); }, "b:DateTime");
568 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (a); }, "a:DateTimeOffset");
569 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (b); }, "b:DateTimeOffset");
570 AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (a); }, "a:decimal");
571 AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (b); }, "b:decimal");
572 AssertThrows<FormatException> (() => { double z = (double) new XElement (a); }, "a:double");
573 AssertThrows<FormatException> (() => { double z = (double) new XElement (b); }, "b:double");
574 AssertThrows<FormatException> (() => { float z = (float) new XElement (a); }, "a:float");
575 AssertThrows<FormatException> (() => { float z = (float) new XElement (b); }, "b:float");
576 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (a); }, "a:Guid");
577 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (b); }, "b:Guid");
578 AssertThrows<FormatException> (() => { int z = (int) new XElement (a); }, "a:int");
579 AssertThrows<FormatException> (() => { int z = (int) new XElement (b); }, "b:int");
580 AssertThrows<FormatException> (() => { long z = (long) new XElement (a); }, "a:long");
581 AssertThrows<FormatException> (() => { long z = (long) new XElement (b); }, "b:long");
582 AssertThrows<FormatException> (() => { uint z = (uint) new XElement (a); }, "a:uint");
583 AssertThrows<FormatException> (() => { uint z = (uint) new XElement (b); }, "b:uint");
584 AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (a); }, "a:ulong");
585 AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (b); }, "b:ulong");
586 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (a); }, "a:TimeSpan");
587 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (b); }, "b:TimeSpan");
591 public void CastSpaces ()
593 XElement a = new XElement ("a", " ");
594 XElement b = new XElement ("b", new XCData (" "));
596 // Verify expected "cloning" and "space" behaviour as prerequisites
597 Assert.IsFalse (a.IsEmpty, "#1-1a");
598 Assert.IsFalse (b.IsEmpty, "#1-1b");
599 Assert.IsFalse (new XElement (a).IsEmpty, "#1-2a");
600 Assert.IsFalse (new XElement (b).IsEmpty, "#1-2b");
601 Assert.AreEqual (" ", a.Value, "#2-1a");
602 Assert.AreEqual (" ", b.Value, "#2-1b");
603 Assert.AreEqual (" ", new XElement (a).Value, "#2-2a");
604 Assert.AreEqual (" ", new XElement (b).Value, "#2-2b");
605 Assert.AreNotSame (a, new XElement (a), "#3-1a");
606 Assert.AreNotSame (b, new XElement (b), "#3-1b");
607 Assert.AreEqual (a.ToString (), new XElement (a).ToString (), "#3-2a");
608 Assert.AreEqual (b.ToString (), new XElement (b).ToString (), "#3-2b");
609 Assert.AreEqual ("<a> </a>", a.ToString (), "#3-3a");
610 Assert.AreEqual ("<b><![CDATA[ ]]></b>", b.ToString (), "#3-3b");
611 Assert.AreEqual (a.ToString (), new XElement ("a", ' ').ToString (), "#3-4");
613 // Execute the primary assertions of this test
614 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (a); }, "a:bool?");
615 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (b); }, "b:bool?");
616 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (a); }, "a:DateTime?");
617 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (b); }, "b:DateTime?");
618 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (a); }, "a:DateTimeOffset?");
619 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (b); }, "b:DateTimeOffset?");
620 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (a); }, "a:decimal?");
621 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (b); }, "b:decimal?");
622 AssertThrows<FormatException> (() => { double? z = (double?) new XElement (a); }, "a:double?");
623 AssertThrows<FormatException> (() => { double? z = (double?) new XElement (b); }, "b:double?");
624 AssertThrows<FormatException> (() => { float? z = (float?) new XElement (a); }, "a:float?");
625 AssertThrows<FormatException> (() => { float? z = (float?) new XElement (b); }, "b:float?");
626 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (a); }, "a:Guid?");
627 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (b); }, "b:Guid?");
628 AssertThrows<FormatException> (() => { int? z = (int?) new XElement (a); }, "a:int?");
629 AssertThrows<FormatException> (() => { int? z = (int?) new XElement (b); }, "b:int?");
630 AssertThrows<FormatException> (() => { long? z = (long?) new XElement (a); }, "a:long?");
631 AssertThrows<FormatException> (() => { long? z = (long?) new XElement (b); }, "b:long?");
632 AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (a); }, "a:uint?");
633 AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (b); }, "b:uint?");
634 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (a); }, "a:ulong?");
635 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (b); }, "b:ulong?");
636 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (a); }, "a:TimeSpan?");
637 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (b); }, "b:TimeSpan?");
638 Assert.AreEqual (" ", (string) new XElement (a), "a:string");
639 Assert.AreEqual (" ", (string) new XElement (b), "b:string");
640 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (a); }, "a:bool");
641 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (b); }, "b:bool");
642 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (a); }, "a:DateTime");
643 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (b); }, "b:DateTime");
644 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (a); }, "a:DateTimeOffset");
645 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (b); }, "b:DateTimeOffset");
646 AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (a); }, "a:decimal");
647 AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (b); }, "b:decimal");
648 AssertThrows<FormatException> (() => { double z = (double) new XElement (a); }, "a:double");
649 AssertThrows<FormatException> (() => { double z = (double) new XElement (b); }, "b:double");
650 AssertThrows<FormatException> (() => { float z = (float) new XElement (a); }, "a:float");
651 AssertThrows<FormatException> (() => { float z = (float) new XElement (b); }, "b:float");
652 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (a); }, "a:Guid");
653 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (b); }, "b:Guid");
654 AssertThrows<FormatException> (() => { int z = (int) new XElement (a); }, "a:int");
655 AssertThrows<FormatException> (() => { int z = (int) new XElement (b); }, "b:int");
656 AssertThrows<FormatException> (() => { long z = (long) new XElement (a); }, "a:long");
657 AssertThrows<FormatException> (() => { long z = (long) new XElement (b); }, "b:long");
658 AssertThrows<FormatException> (() => { uint z = (uint) new XElement (a); }, "a:uint");
659 AssertThrows<FormatException> (() => { uint z = (uint) new XElement (b); }, "b:uint");
660 AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (a); }, "a:ulong");
661 AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (b); }, "b:ulong");
662 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (a); }, "a:TimeSpan");
663 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (b); }, "b:TimeSpan");
667 public void CastNumbers ()
669 XElement a = new XElement ("a", "7");
670 XElement b = new XElement ("b", new XCData (" 42 "));
671 XElement c = new XElement ("c", " \r\n 13 \t ");
672 XElement d = new XElement ("d", -101);
673 XElement o = new XElement ("o", "0");
674 XElement l = new XElement ("l", "1");
675 XElement I = new XElement ("I", "INF");
676 XElement i = new XElement ("i", " Infinity ");
677 XElement M = new XElement ("M", " -INF ");
678 XElement m = new XElement ("m", "-Infinity");
679 XElement n = new XElement ("n", "\t NaN ");
681 // Verify expected "cloning" and basic conversion behaviour as prerequisites
682 Assert.IsFalse (a.IsEmpty, "#1-1");
683 Assert.IsFalse (new XElement (b).IsEmpty, "#1-2");
684 Assert.AreEqual (" \r\n 13 \t ", c.Value, "#2-1");
685 Assert.AreEqual ("-101", new XElement (d).Value, "#2-2");
686 Assert.AreNotSame (o, new XElement (o), "#3-1");
687 Assert.AreEqual (l.ToString (), new XElement (l).ToString (), "#3-2");
688 Assert.AreEqual ("<a>7</a>", a.ToString (), "#3-3a");
689 Assert.AreEqual ("<b><![CDATA[ 42 ]]></b>", b.ToString (), "#3-3b");
690 Assert.AreEqual ("<c> \r\n 13 \t </c>", c.ToString (), "#3-3c");
691 Assert.AreEqual ("<d>-101</d>", d.ToString (), "#3-3d");
692 Assert.AreEqual ("<o>0</o>", new XElement ("o", 0.0).ToString (), "#3-3o");
693 Assert.AreEqual ("<l>1</l>", new XElement ("l", 1.0f).ToString (), "#3-3l");
694 Assert.AreEqual ("<n>NaN</n>", new XElement ("n", double.NaN).ToString (), "#3-3n");
695 Assert.AreEqual (a.ToString (), new XElement ("a", '7').ToString (), "#3-4a");
696 Assert.AreEqual (d.ToString (), new XElement ("d", "-101").ToString (), "#3-4d");
697 Assert.AreEqual (o.ToString (), new XElement ("o", 0L).ToString (), "#3-4o");
698 Assert.AreEqual (l.ToString (), new XElement ("l", 1m).ToString (), "#3-4l");
700 // Execute the primary assertions of this test
701 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (a); }, "a:bool?");
702 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (b); }, "b:bool?");
703 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (c); }, "c:bool?");
704 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (d); }, "d:bool?");
705 Assert.IsNotNull ((bool?) new XElement (o), "o:bool?:null");
706 Assert.AreEqual (false, ((bool?) new XElement (o)).Value, "o:bool?:value");
707 Assert.IsNotNull ((bool?) new XElement (l), "l:bool?:null");
708 Assert.AreEqual (true, ((bool?) new XElement (l)).Value, "l:bool?:value");
709 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (I); }, "I:bool?");
710 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (i); }, "i:bool?");
711 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (M); }, "M:bool?");
712 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (m); }, "m:bool?");
713 AssertThrows<FormatException> (() => { bool? z = (bool?) new XElement (n); }, "n:bool?");
714 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (a); }, "a:DateTime?");
715 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (b); }, "b:DateTime?");
716 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (c); }, "c:DateTime?");
717 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (d); }, "d:DateTime?");
718 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (o); }, "o:DateTime?");
719 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (l); }, "l:DateTime?");
720 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (I); }, "I:DateTime?");
721 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (i); }, "i:DateTime?");
722 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (M); }, "M:DateTime?");
723 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (m); }, "m:DateTime?");
724 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XElement (n); }, "n:DateTime?");
725 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (a); }, "a:DateTimeOffset?");
726 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (b); }, "b:DateTimeOffset?");
727 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (c); }, "c:DateTimeOffset?");
728 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (d); }, "d:DateTimeOffset?");
729 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (o); }, "o:DateTimeOffset?");
730 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (l); }, "l:DateTimeOffset?");
731 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (I); }, "I:DateTimeOffset?");
732 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (i); }, "i:DateTimeOffset?");
733 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (M); }, "M:DateTimeOffset?");
734 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (m); }, "m:DateTimeOffset?");
735 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XElement (n); }, "n:DateTimeOffset?");
736 Assert.IsNotNull ((decimal?) new XElement (a), "a:decimal?:null");
737 Assert.AreEqual (7m, ((decimal?) new XElement (a)).Value, "a:decimal?:value");
738 Assert.IsNotNull ((decimal?) new XElement (b), "b:decimal?:null");
739 Assert.AreEqual (42m, ((decimal?) new XElement (b)).Value, "b:decimal?:value");
740 Assert.IsNotNull ((decimal?) new XElement (c), "c:decimal?:null");
741 Assert.AreEqual (13m, ((decimal?) new XElement (c)).Value, "c:decimal?:value");
742 Assert.IsNotNull ((decimal?) new XElement (d), "d:decimal?:null");
743 Assert.AreEqual (-101m, ((decimal?) new XElement (d)).Value, "d:decimal?:value");
744 Assert.IsNotNull ((decimal?) new XElement (o), "o:decimal?:null");
745 Assert.AreEqual (0m, ((decimal?) new XElement (o)).Value, "o:decimal?:value");
746 Assert.IsNotNull ((decimal?) new XElement (l), "l:decimal?:null");
747 Assert.AreEqual (1m, ((decimal?) new XElement (l)).Value, "l:decimal?:value");
748 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (I); }, "I:decimal?");
749 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (i); }, "i:decimal?");
750 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (M); }, "M:decimal?");
751 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (m); }, "m:decimal?");
752 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XElement (n); }, "n:decimal?");
753 Assert.IsNotNull ((double?) new XElement (a), "a:double?:null");
754 Assert.AreEqual (7d, ((double?) new XElement (a)).Value, "a:double?:value");
755 Assert.IsNotNull ((double?) new XElement (b), "b:double?:null");
756 Assert.AreEqual (42d, ((double?) new XElement (b)).Value, "b:double?:value");
757 Assert.IsNotNull ((double?) new XElement (c), "c:double?:null");
758 Assert.AreEqual (13d, ((double?) new XElement (c)).Value, "c:double?:value");
759 Assert.IsNotNull ((double?) new XElement (d), "d:double?:null");
760 Assert.AreEqual (-101d, ((double?) new XElement (d)).Value, "d:double?:value");
761 Assert.IsNotNull ((double?) new XElement (o), "o:double?:null");
762 Assert.AreEqual (0d, ((double?) new XElement (o)).Value, "o:double?:value");
763 Assert.IsNotNull ((double?) new XElement (l), "l:double?:null");
764 Assert.AreEqual (1d, ((double?) new XElement (l)).Value, "l:double?:value");
765 Assert.IsNotNull ((double?) new XElement (I), "I:double?:null");
766 Assert.AreEqual (double.PositiveInfinity, ((double?) new XElement (I)).Value, "I:double?:value");
767 Assert.IsNotNull ((double?) new XElement (i), "i:double?:null");
768 Assert.AreEqual (double.PositiveInfinity, ((double?) new XElement (i)).Value, "i:double?:value");
769 Assert.IsNotNull ((double?) new XElement (M), "M:double?:null");
770 Assert.AreEqual (double.NegativeInfinity, ((double?) new XElement (M)).Value, "M:double?:value");
771 Assert.IsNotNull ((double?) new XElement (m), "m:double?:null");
772 Assert.AreEqual (double.NegativeInfinity, ((double?) new XElement (m)).Value, "m:double?:value");
773 Assert.IsNotNull ((double?) new XElement (n), "n:double?:null");
774 Assert.IsNaN (((double?) new XElement (n)).Value, "n:double?:value");
775 Assert.IsNotNull ((float?) new XElement (a), "a:float?:null");
776 Assert.AreEqual (7f, ((float?) new XElement (a)).Value, "a:float?:value");
777 Assert.IsNotNull ((float?) new XElement (b), "b:float?:null");
778 Assert.AreEqual (42f, ((float?) new XElement (b)).Value, "b:float?:value");
779 Assert.IsNotNull ((float?) new XElement (c), "c:float?:null");
780 Assert.AreEqual (13f, ((float?) new XElement (c)).Value, "c:float?:value");
781 Assert.IsNotNull ((float?) new XElement (d), "d:float?:null");
782 Assert.AreEqual (-101f, ((float?) new XElement (d)).Value, "d:float?:value");
783 Assert.IsNotNull ((float?) new XElement (o), "o:float?:null");
784 Assert.AreEqual (0f, ((float?) new XElement (o)).Value, "o:float?:value");
785 Assert.IsNotNull ((float?) new XElement (l), "l:float?:null");
786 Assert.AreEqual (1f, ((float?) new XElement (l)).Value, "l:float?:value");
787 Assert.IsNotNull ((float?) new XElement (I), "I:float?:null");
788 Assert.AreEqual (float.PositiveInfinity, ((float?) new XElement (I)).Value, "I:float?:value");
789 Assert.IsNotNull ((float?) new XElement (i), "i:float?:null");
790 Assert.AreEqual (float.PositiveInfinity, ((float?) new XElement (i)).Value, "i:float?:value");
791 Assert.IsNotNull ((float?) new XElement (M), "M:float?:null");
792 Assert.AreEqual (float.NegativeInfinity, ((float?) new XElement (M)).Value, "M:float?:value");
793 Assert.IsNotNull ((float?) new XElement (m), "m:float?:null");
794 Assert.AreEqual (float.NegativeInfinity, ((float?) new XElement (m)).Value, "m:float?:value");
795 Assert.IsNotNull ((float?) new XElement (n), "n:float?:null");
796 Assert.IsNaN (((float?) new XElement (n)).Value, "n:float?:value");
797 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (a); }, "a:Guid?");
798 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (b); }, "b:Guid?");
799 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (c); }, "c:Guid?");
800 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (d); }, "d:Guid?");
801 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (o); }, "o:Guid?");
802 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (l); }, "l:Guid?");
803 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (I); }, "I:Guid?");
804 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (i); }, "i:Guid?");
805 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (M); }, "M:Guid?");
806 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (m); }, "m:Guid?");
807 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XElement (n); }, "n:Guid?");
808 Assert.IsNotNull ((int?) new XElement (a), "a:int?:null");
809 Assert.AreEqual (7, ((int?) new XElement (a)).Value, "a:int?:value");
810 Assert.IsNotNull ((int?) new XElement (b), "b:int?:null");
811 Assert.AreEqual (42, ((int?) new XElement (b)).Value, "b:int?:value");
812 Assert.IsNotNull ((int?) new XElement (c), "c:int?:null");
813 Assert.AreEqual (13, ((int?) new XElement (c)).Value, "c:int?:value");
814 Assert.IsNotNull ((int?) new XElement (d), "d:int?:null");
815 Assert.AreEqual (-101, ((int?) new XElement (d)).Value, "d:int?:value");
816 Assert.IsNotNull ((int?) new XElement (o), "o:int?:null");
817 Assert.AreEqual (0, ((int?) new XElement (o)).Value, "o:int?:value");
818 Assert.IsNotNull ((int?) new XElement (l), "l:int?:null");
819 Assert.AreEqual (1, ((int?) new XElement (l)).Value, "l:int?:value");
820 AssertThrows<FormatException> (() => { int? z = (int?) new XElement (I); }, "I:int?");
821 AssertThrows<FormatException> (() => { int? z = (int?) new XElement (i); }, "i:int?");
822 AssertThrows<FormatException> (() => { int? z = (int?) new XElement (M); }, "M:int?");
823 AssertThrows<FormatException> (() => { int? z = (int?) new XElement (m); }, "m:int?");
824 AssertThrows<FormatException> (() => { int? z = (int?) new XElement (n); }, "n:int?");
825 Assert.IsNotNull ((long?) new XElement (a), "a:long?:null");
826 Assert.AreEqual (7L, ((long?) new XElement (a)).Value, "a:long?:value");
827 Assert.IsNotNull ((long?) new XElement (b), "b:long?:null");
828 Assert.AreEqual (42L, ((long?) new XElement (b)).Value, "b:long?:value");
829 Assert.IsNotNull ((long?) new XElement (c), "c:long?:null");
830 Assert.AreEqual (13L, ((long?) new XElement (c)).Value, "c:long?:value");
831 Assert.IsNotNull ((long?) new XElement (d), "d:long?:null");
832 Assert.AreEqual (-101L, ((long?) new XElement (d)).Value, "d:long?:value");
833 Assert.IsNotNull ((long?) new XElement (o), "o:long?:null");
834 Assert.AreEqual (0L, ((long?) new XElement (o)).Value, "o:long?:value");
835 Assert.IsNotNull ((long?) new XElement (l), "l:long?:null");
836 Assert.AreEqual (1L, ((long?) new XElement (l)).Value, "l:long?:value");
837 AssertThrows<FormatException> (() => { long? z = (long?) new XElement (I); }, "I:long?");
838 AssertThrows<FormatException> (() => { long? z = (long?) new XElement (i); }, "i:long?");
839 AssertThrows<FormatException> (() => { long? z = (long?) new XElement (M); }, "M:long?");
840 AssertThrows<FormatException> (() => { long? z = (long?) new XElement (m); }, "m:long?");
841 AssertThrows<FormatException> (() => { long? z = (long?) new XElement (n); }, "n:long?");
842 Assert.IsNotNull ((uint?) new XElement (a), "a:uint?:null");
843 Assert.AreEqual (7u, ((uint?) new XElement (a)).Value, "a:uint?:value");
844 Assert.IsNotNull ((uint?) new XElement (b), "b:uint?:null");
845 Assert.AreEqual (42u, ((uint?) new XElement (b)).Value, "b:uint?:value");
846 Assert.IsNotNull ((uint?) new XElement (c), "c:uint?:null");
847 Assert.AreEqual (13u, ((uint?) new XElement (c)).Value, "c:uint?:value");
848 // LAMESPEC: see XmlConvertTests.ToUInt32().
849 //AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (d); }, "d:uint?");
850 Assert.IsNotNull ((uint?) new XElement (o), "o:uint?:null");
851 Assert.AreEqual (0u, ((uint?) new XElement (o)).Value, "o:uint?:value");
852 Assert.IsNotNull ((uint?) new XElement (l), "l:uint?:null");
853 Assert.AreEqual (1u, ((uint?) new XElement (l)).Value, "l:uint?:value");
854 AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (I); }, "I:uint?");
855 AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (i); }, "i:uint?");
856 AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (M); }, "M:uint?");
857 AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (m); }, "m:uint?");
858 AssertThrows<FormatException> (() => { uint? z = (uint?) new XElement (n); }, "n:uint?");
859 Assert.IsNotNull ((ulong?) new XElement (a), "a:ulong?:null");
860 Assert.AreEqual (7UL, ((ulong?) new XElement (a)).Value, "a:ulong?:value");
861 Assert.IsNotNull ((ulong?) new XElement (b), "b:ulong?:null");
862 Assert.AreEqual (42UL, ((ulong?) new XElement (b)).Value, "b:ulong?:value");
863 Assert.IsNotNull ((ulong?) new XElement (c), "c:ulong?:null");
864 Assert.AreEqual (13UL, ((ulong?) new XElement (c)).Value, "c:ulong?:value");
865 // LAMESPEC: see XmlConvertTests.ToUInt64().
866 //AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (d); }, "d:ulong?");
867 Assert.IsNotNull ((ulong?) new XElement (o), "o:ulong?:null");
868 Assert.AreEqual (0UL, ((ulong?) new XElement (o)).Value, "o:ulong?:value");
869 Assert.IsNotNull ((ulong?) new XElement (l), "l:ulong?:null");
870 Assert.AreEqual (1UL, ((ulong?) new XElement (l)).Value, "l:ulong?:value");
871 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (I); }, "I:ulong?");
872 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (i); }, "i:ulong?");
873 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (M); }, "M:ulong?");
874 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (m); }, "m:ulong?");
875 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XElement (n); }, "n:ulong?");
876 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (a); }, "a:TimeSpan?");
877 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (b); }, "b:TimeSpan?");
878 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (c); }, "c:TimeSpan?");
879 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (d); }, "d:TimeSpan?");
880 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (o); }, "o:TimeSpan?");
881 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (l); }, "l:TimeSpan?");
882 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (I); }, "I:TimeSpan?");
883 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (i); }, "i:TimeSpan?");
884 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (M); }, "M:TimeSpan?");
885 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (m); }, "m:TimeSpan?");
886 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XElement (n); }, "n:TimeSpan?");
887 Assert.AreEqual ("7", (string) new XElement (a), "a:string");
888 Assert.AreEqual (" 42 ", (string) new XElement (b), "b:string");
889 Assert.AreEqual (" \r\n 13 \t ", (string) new XElement (c), "c:string");
890 Assert.AreEqual ("-101", (string) new XElement (d), "d:string");
891 Assert.AreEqual ("0", (string) new XElement (o), "o:string");
892 Assert.AreEqual ("1", (string) new XElement (l), "l:string");
893 Assert.AreEqual ("INF", (string) new XElement (I), "I:string");
894 Assert.AreEqual (" Infinity ", (string) new XElement (i), "i:string");
895 Assert.AreEqual (" -INF ", (string) new XElement (M), "M:string");
896 Assert.AreEqual ("-Infinity", (string) new XElement (m), "m:string");
897 Assert.AreEqual ("\t NaN ", (string) new XElement (n), "n:string");
898 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (a); }, "a:bool");
899 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (b); }, "b:bool");
900 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (c); }, "c:bool");
901 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (d); }, "d:bool");
902 Assert.AreEqual (false, (bool) new XElement (o), "o:bool");
903 Assert.AreEqual (true, (bool) new XElement (l), "l:bool");
904 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (I); }, "I:bool");
905 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (i); }, "i:bool");
906 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (M); }, "M:bool");
907 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (m); }, "m:bool");
908 AssertThrows<FormatException> (() => { bool z = (bool) new XElement (n); }, "n:bool");
909 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (a); }, "a:DateTime");
910 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (b); }, "b:DateTime");
911 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (c); }, "c:DateTime");
912 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (d); }, "d:DateTime");
913 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (o); }, "o:DateTime");
914 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (l); }, "l:DateTime");
915 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (I); }, "I:DateTime");
916 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (i); }, "i:DateTime");
917 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (M); }, "M:DateTime");
918 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (m); }, "m:DateTime");
919 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XElement (n); }, "n:DateTime");
920 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (a); }, "a:DateTimeOffset");
921 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (b); }, "b:DateTimeOffset");
922 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (c); }, "c:DateTimeOffset");
923 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (d); }, "d:DateTimeOffset");
924 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (o); }, "o:DateTimeOffset");
925 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (l); }, "l:DateTimeOffset");
926 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (I); }, "I:DateTimeOffset");
927 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (i); }, "i:DateTimeOffset");
928 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (M); }, "M:DateTimeOffset");
929 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (m); }, "m:DateTimeOffset");
930 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XElement (n); }, "n:DateTimeOffset");
931 Assert.AreEqual (7m, (decimal) new XElement (a), "a:decimal");
932 Assert.AreEqual (42m, (decimal) new XElement (b), "b:decimal");
933 Assert.AreEqual (13m, (decimal) new XElement (c), "c:decimal");
934 Assert.AreEqual (-101m, (decimal) new XElement (d), "d:decimal");
935 Assert.AreEqual (0m, (decimal) new XElement (o), "o:decimal");
936 Assert.AreEqual (1m, (decimal) new XElement (l), "l:decimal");
937 AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (I); }, "I:decimal");
938 AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (i); }, "i:decimal");
939 AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (M); }, "M:decimal");
940 AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (m); }, "m:decimal");
941 AssertThrows<FormatException> (() => { decimal z = (decimal) new XElement (n); }, "n:decimal");
942 Assert.AreEqual (7d, (double) new XElement (a), "a:double");
943 Assert.AreEqual (42d, (double) new XElement (b), "b:double");
944 Assert.AreEqual (13d, (double) new XElement (c), "c:double");
945 Assert.AreEqual (-101d, (double) new XElement (d), "d:double");
946 Assert.AreEqual (0d, (double) new XElement (o), "o:double");
947 Assert.AreEqual (1d, (double) new XElement (l), "l:double");
948 Assert.AreEqual (double.PositiveInfinity, (double) new XElement (I), "I:double");
949 Assert.AreEqual (double.PositiveInfinity, (double) new XElement (i), "i:double");
950 Assert.AreEqual (double.NegativeInfinity, (double) new XElement (M), "M:double");
951 Assert.AreEqual (double.NegativeInfinity, (double) new XElement (m), "m:double");
952 Assert.IsNaN (((double) new XElement (n)), "n:double");
953 Assert.AreEqual (7f, (float) new XElement (a), "a:float");
954 Assert.AreEqual (42f, (float) new XElement (b), "b:float");
955 Assert.AreEqual (13f, (float) new XElement (c), "c:float");
956 Assert.AreEqual (-101f, (float) new XElement (d), "d:float");
957 Assert.AreEqual (0f, (float) new XElement (o), "o:float");
958 Assert.AreEqual (1f, (float) new XElement (l), "l:float");
959 Assert.AreEqual (float.PositiveInfinity, (float) new XElement (I), "I:float");
960 Assert.AreEqual (float.PositiveInfinity, (float) new XElement (i), "i:float");
961 Assert.AreEqual (float.NegativeInfinity, (float) new XElement (M), "M:float");
962 Assert.AreEqual (float.NegativeInfinity, (float) new XElement (m), "m:float");
963 Assert.IsNaN (((float) new XElement (n)), "n:float");
964 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (a); }, "a:Guid");
965 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (b); }, "b:Guid");
966 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (c); }, "c:Guid");
967 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (d); }, "d:Guid");
968 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (o); }, "o:Guid");
969 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (l); }, "l:Guid");
970 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (I); }, "I:Guid");
971 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (i); }, "i:Guid");
972 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (M); }, "M:Guid");
973 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (m); }, "m:Guid");
974 AssertThrows<FormatException> (() => { Guid z = (Guid) new XElement (n); }, "n:Guid");
975 Assert.AreEqual (7, (int) new XElement (a), "a:int");
976 Assert.AreEqual (42, (int) new XElement (b), "b:int");
977 Assert.AreEqual (13, (int) new XElement (c), "c:int");
978 Assert.AreEqual (-101, (int) new XElement (d), "d:int");
979 Assert.AreEqual (0, (int) new XElement (o), "o:int");
980 Assert.AreEqual (1, (int) new XElement (l), "l:int");
981 AssertThrows<FormatException> (() => { int z = (int) new XElement (I); }, "I:int");
982 AssertThrows<FormatException> (() => { int z = (int) new XElement (i); }, "i:int");
983 AssertThrows<FormatException> (() => { int z = (int) new XElement (M); }, "M:int");
984 AssertThrows<FormatException> (() => { int z = (int) new XElement (m); }, "m:int");
985 AssertThrows<FormatException> (() => { int z = (int) new XElement (n); }, "n:int");
986 Assert.AreEqual (7L, (long) new XElement (a), "a:long");
987 Assert.AreEqual (42L, (long) new XElement (b), "b:long");
988 Assert.AreEqual (13L, (long) new XElement (c), "c:long");
989 Assert.AreEqual (-101L, (long) new XElement (d), "d:long");
990 Assert.AreEqual (0L, (long) new XElement (o), "o:long");
991 Assert.AreEqual (1L, (long) new XElement (l), "l:long");
992 AssertThrows<FormatException> (() => { long z = (long) new XElement (I); }, "I:long");
993 AssertThrows<FormatException> (() => { long z = (long) new XElement (i); }, "i:long");
994 AssertThrows<FormatException> (() => { long z = (long) new XElement (M); }, "M:long");
995 AssertThrows<FormatException> (() => { long z = (long) new XElement (m); }, "m:long");
996 AssertThrows<FormatException> (() => { long z = (long) new XElement (n); }, "n:long");
997 Assert.AreEqual (7u, (uint) new XElement (a), "a:uint");
998 Assert.AreEqual (42u, (uint) new XElement (b), "b:uint");
999 Assert.AreEqual (13u, (uint) new XElement (c), "c:uint");
1000 // LAMESPEC: see XmlConvertTests.ToUInt32().
1001 //AssertThrows<FormatException> (() => { uint z = (uint) new XElement (d); }, "d:uint");
1002 Assert.AreEqual (0u, (uint) new XElement (o), "o:uint");
1003 Assert.AreEqual (1u, (uint) new XElement (l), "l:uint");
1004 AssertThrows<FormatException> (() => { uint z = (uint) new XElement (I); }, "I:uint");
1005 AssertThrows<FormatException> (() => { uint z = (uint) new XElement (i); }, "i:uint");
1006 AssertThrows<FormatException> (() => { uint z = (uint) new XElement (M); }, "M:uint");
1007 AssertThrows<FormatException> (() => { uint z = (uint) new XElement (m); }, "m:uint");
1008 AssertThrows<FormatException> (() => { uint z = (uint) new XElement (n); }, "n:uint");
1009 Assert.AreEqual (7UL, (ulong) new XElement (a), "a:ulong");
1010 Assert.AreEqual (42UL, (ulong) new XElement (b), "b:ulong");
1011 Assert.AreEqual (13UL, (ulong) new XElement (c), "c:ulong");
1012 // LAMESPEC: see XmlConvertTests.ToUInt64().
1013 //AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (d); }, "d:ulong");
1014 Assert.AreEqual (0UL, (ulong) new XElement (o), "o:ulong");
1015 Assert.AreEqual (1UL, (ulong) new XElement (l), "l:ulong");
1016 AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (I); }, "I:ulong");
1017 AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (i); }, "i:ulong");
1018 AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (M); }, "M:ulong");
1019 AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (m); }, "m:ulong");
1020 AssertThrows<FormatException> (() => { ulong z = (ulong) new XElement (n); }, "n:ulong");
1021 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (a); }, "a:TimeSpan");
1022 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (b); }, "b:TimeSpan");
1023 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (c); }, "c:TimeSpan");
1024 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (d); }, "d:TimeSpan");
1025 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (o); }, "o:TimeSpan");
1026 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (l); }, "l:TimeSpan");
1027 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (I); }, "I:TimeSpan");
1028 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (i); }, "i:TimeSpan");
1029 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (M); }, "M:TimeSpan");
1030 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (m); }, "m:TimeSpan");
1031 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XElement (n); }, "n:TimeSpan");
1033 // Perform some round-trip tests with numbers
1035 const decimal @decimal = -41051609414188012238960097189m;
1036 const double @double = 8.5506609919892972E+307d;
1037 const float @float = -1.70151961E+37f;
1038 const int @int = -1051251773;
1039 const long @long = 4596767133891939716L;
1040 const uint @uint = 4106628142u;
1041 const ulong @ulong = 10713797297298255927UL;
1042 x = new XElement ("x", @decimal);
1043 Assert.IsNotNull ((decimal?) new XElement (x), "x:decimal?:null");
1044 Assert.AreEqual (@decimal, ((decimal?) new XElement (x)).Value, "x:decimal?:value");
1045 Assert.AreEqual (@decimal, (decimal) new XElement (x), "x:decimal");
1046 x = new XElement ("x", @double);
1047 Assert.IsNotNull ((double?) new XElement (x), "x:double?:null");
1048 Assert.AreEqual (@double, ((double?) new XElement (x)).Value, "x:double?:value");
1049 Assert.AreEqual (@double, (double) new XElement (x), "x:double");
1050 x = new XElement ("x", @float);
1051 Assert.IsNotNull ((float?) new XElement (x), "x:float?:null");
1052 Assert.AreEqual (@float, ((float?) new XElement (x)).Value, "x:float?:value");
1053 Assert.AreEqual (@float, (float) new XElement (x), "x:float");
1054 x = new XElement ("x", @int);
1055 Assert.IsNotNull ((int?) new XElement (x), "x:int?:null");
1056 Assert.AreEqual (@int, ((int?) new XElement (x)).Value, "x:int?:value");
1057 Assert.AreEqual (@int, (int) new XElement (x), "x:int");
1058 x = new XElement ("x", @long);
1059 Assert.IsNotNull ((long?) new XElement (x), "x:long?:null");
1060 Assert.AreEqual (@long, ((long?) new XElement (x)).Value, "x:long?:value");
1061 Assert.AreEqual (@long, (long) new XElement (x), "x:long");
1062 x = new XElement ("x", @uint);
1063 Assert.IsNotNull ((uint?) new XElement (x), "x:uint?:null");
1064 Assert.AreEqual (@uint, ((uint?) new XElement (x)).Value, "x:uint?:value");
1065 Assert.AreEqual (@uint, (uint) new XElement (x), "x:uint");
1066 x = new XElement ("x", @ulong);
1067 Assert.IsNotNull ((ulong?) new XElement (x), "x:ulong?:null");
1068 Assert.AreEqual (@ulong, ((ulong?) new XElement (x)).Value, "x:ulong?:value");
1069 Assert.AreEqual (@ulong, (ulong) new XElement (x), "x:ulong");
1070 x = new XElement ("x", double.NaN);
1071 Assert.IsNotNull ((double?) new XElement (x), "NaN:double?:null");
1072 Assert.AreEqual (double.NaN, ((double?) new XElement (x)).Value, "NaN:double?:value");
1073 Assert.AreEqual (double.NaN, (double) new XElement (x), "NaN:double");
1074 x = new XElement ("x", float.NaN);
1075 Assert.IsNotNull ((float?) new XElement (x), "NaN:float?:null");
1076 Assert.AreEqual (float.NaN, ((float?) new XElement (x)).Value, "NaN:float?:value");
1077 Assert.AreEqual (float.NaN, (float) new XElement (x), "NaN:float");
1078 x = new XElement ("x", double.PositiveInfinity);
1079 Assert.IsNotNull ((double?) new XElement (x), "+Inf:double?:null");
1080 Assert.AreEqual (double.PositiveInfinity, ((double?) new XElement (x)).Value, "+Inf:double?:value");
1081 Assert.AreEqual (double.PositiveInfinity, (double) new XElement (x), "+Inf:double");
1082 x = new XElement ("x", float.PositiveInfinity);
1083 Assert.IsNotNull ((float?) new XElement (x), "+Inf:float?:null");
1084 Assert.AreEqual (float.PositiveInfinity, ((float?) new XElement (x)).Value, "+Inf:float?:value");
1085 Assert.AreEqual (float.PositiveInfinity, (float) new XElement (x), "+Inf:float");
1086 x = new XElement ("x", double.NegativeInfinity);
1087 Assert.IsNotNull ((double?) new XElement (x), "-Inf:double?:null");
1088 Assert.AreEqual (double.NegativeInfinity, ((double?) new XElement (x)).Value, "-Inf:double?:value");
1089 Assert.AreEqual (double.NegativeInfinity, (double) new XElement (x), "-Inf:double");
1090 x = new XElement ("x", float.NegativeInfinity);
1091 Assert.IsNotNull ((float?) new XElement (x), "-Inf:float?:null");
1092 Assert.AreEqual (float.NegativeInfinity, ((float?) new XElement (x)).Value, "-Inf:float?:value");
1093 Assert.AreEqual (float.NegativeInfinity, (float) new XElement (x), "-Inf:float");
1095 // Perform overflow tests with numbers
1096 AssertThrows<OverflowException> (() => { decimal z = (decimal) new XElement ("z", "91051609414188012238960097189"); }, "z:decimal");
1097 AssertThrows<OverflowException> (() => { decimal? z = (decimal?) new XElement ("z", "91051609414188012238960097189"); }, "z:decimal?");
1098 AssertThrows<OverflowException> (() => { double z = (double) new XElement ("z", "8.5506609919892972E+654"); }, "z:double");
1099 AssertThrows<OverflowException> (() => { double? z = (double?) new XElement ("z", "8.5506609919892972E+654"); }, "z:double?");
1100 AssertThrows<OverflowException> (() => { float z = (float) new XElement ("z", @double); }, "z:float");
1101 AssertThrows<OverflowException> (() => { float? z = (float?) new XElement ("z", @double); }, "z:float?");
1102 AssertThrows<OverflowException> (() => { int z = (int) new XElement ("z", @long); }, "z:int");
1103 AssertThrows<OverflowException> (() => { int? z = (int?) new XElement ("z", @long); }, "z:int?");
1104 AssertThrows<OverflowException> (() => { long z = (long) new XElement ("z", @decimal); }, "z:long");
1105 AssertThrows<OverflowException> (() => { long? z = (long?) new XElement ("z", @decimal); }, "z:long?");
1106 AssertThrows<OverflowException> (() => { uint z = (uint) new XElement ("z", @ulong); }, "z:uint");
1107 AssertThrows<OverflowException> (() => { uint? z = (uint?) new XElement ("z", @ulong); }, "z:uint?");
1108 AssertThrows<OverflowException> (() => { ulong z = (ulong) new XElement ("z", -@decimal); }, "z:ulong");
1109 AssertThrows<OverflowException> (() => { ulong? z = (ulong?) new XElement ("z", -@decimal); }, "z:ulong?");
1113 public void CastExtremes ()
1115 // Test extremes/constants where round-trips should work in specific ways
1116 Assert.AreEqual (decimal.MaxValue, (decimal) new XElement ("k", decimal.MaxValue), "MaxValue:decimal");
1117 Assert.AreEqual (decimal.MinValue, (decimal) new XElement ("k", decimal.MinValue), "MinValue:decimal");
1118 Assert.AreEqual (decimal.MinusOne, (decimal) new XElement ("k", decimal.MinusOne), "MinusOne:decimal");
1119 Assert.AreEqual (decimal.One, (decimal) new XElement ("k", decimal.One), "One:decimal");
1120 Assert.AreEqual (decimal.Zero, (decimal) new XElement ("k", decimal.Zero), "Zero:decimal");
1121 Assert.AreEqual (double.MaxValue, (double) new XElement ("k", double.MaxValue), "MaxValue:double");
1122 Assert.AreEqual (double.MinValue, (double) new XElement ("k", double.MinValue), "MinValue:double");
1123 Assert.AreEqual (double.Epsilon, (double) new XElement ("k", double.Epsilon), "Epsilon:double");
1124 Assert.AreEqual (double.NaN, (double) new XElement ("k", double.NaN), "NaN:double");
1125 Assert.AreEqual (double.NegativeInfinity, (double) new XElement ("k", double.NegativeInfinity), "-Inf:double");
1126 Assert.AreEqual (double.PositiveInfinity, (double) new XElement ("k", double.PositiveInfinity), "+Inf:double");
1127 Assert.AreEqual (float.MaxValue, (float) new XElement ("k", float.MaxValue), "MaxValue:float");
1128 Assert.AreEqual (float.MinValue, (float) new XElement ("k", float.MinValue), "MinValue:float");
1129 Assert.AreEqual (float.Epsilon, (float) new XElement ("k", float.Epsilon), "Epsilon:float");
1130 Assert.AreEqual (float.NaN, (float) new XElement ("k", float.NaN), "NaN:float");
1131 Assert.AreEqual (float.NegativeInfinity, (float) new XElement ("k", float.NegativeInfinity), "-Inf:float");
1132 Assert.AreEqual (float.PositiveInfinity, (float) new XElement ("k", float.PositiveInfinity), "+Inf:float");
1133 Assert.AreEqual (int.MaxValue, (int) new XElement ("k", int.MaxValue), "MaxValue:int");
1134 Assert.AreEqual (int.MinValue, (int) new XElement ("k", int.MinValue), "MinValue:int");
1135 Assert.AreEqual (long.MaxValue, (long) new XElement ("k", long.MaxValue), "MaxValue:long");
1136 Assert.AreEqual (long.MinValue, (long) new XElement ("k", long.MinValue), "MinValue:long");
1137 Assert.AreEqual (uint.MaxValue, (uint) new XElement ("k", uint.MaxValue), "MaxValue:uint");
1138 Assert.AreEqual (uint.MinValue, (uint) new XElement ("k", uint.MinValue), "MinValue:uint");
1139 Assert.AreEqual (ulong.MaxValue, (ulong) new XElement ("k", ulong.MaxValue), "MaxValue:ulong");
1140 Assert.AreEqual (ulong.MinValue, (ulong) new XElement ("k", ulong.MinValue), "MinValue:ulong");
1141 Assert.AreEqual (decimal.MaxValue, (decimal?) new XElement ("k", decimal.MaxValue), "MaxValue:decimal?");
1142 Assert.AreEqual (decimal.MinValue, (decimal?) new XElement ("k", decimal.MinValue), "MinValue:decimal?");
1143 Assert.AreEqual (decimal.MinusOne, (decimal?) new XElement ("k", decimal.MinusOne), "MinusOne:decimal?");
1144 Assert.AreEqual (decimal.One, (decimal?) new XElement ("k", decimal.One), "One:decimal?");
1145 Assert.AreEqual (decimal.Zero, (decimal?) new XElement ("k", decimal.Zero), "Zero:decimal?");
1146 Assert.AreEqual (double.MaxValue, (double?) new XElement ("k", double.MaxValue), "MaxValue:double?");
1147 Assert.AreEqual (double.MinValue, (double?) new XElement ("k", double.MinValue), "MinValue:double?");
1148 Assert.AreEqual (double.Epsilon, (double?) new XElement ("k", double.Epsilon), "Epsilon:double?");
1149 Assert.AreEqual (double.NaN, (double?) new XElement ("k", double.NaN), "NaN:double?");
1150 Assert.AreEqual (double.NegativeInfinity, (double?) new XElement ("k", double.NegativeInfinity), "-Inf:double?");
1151 Assert.AreEqual (double.PositiveInfinity, (double?) new XElement ("k", double.PositiveInfinity), "+Inf:double?");
1152 Assert.AreEqual (float.MaxValue, (float?) new XElement ("k", float.MaxValue), "MaxValue:float?");
1153 Assert.AreEqual (float.MinValue, (float?) new XElement ("k", float.MinValue), "MinValue:float?");
1154 Assert.AreEqual (float.Epsilon, (float?) new XElement ("k", float.Epsilon), "Epsilon:float?");
1155 Assert.AreEqual (float.NaN, (float?) new XElement ("k", float.NaN), "NaN:float?");
1156 Assert.AreEqual (float.NegativeInfinity, (float?) new XElement ("k", float.NegativeInfinity), "-Inf:float?");
1157 Assert.AreEqual (float.PositiveInfinity, (float?) new XElement ("k", float.PositiveInfinity), "+Inf:float?");
1158 Assert.AreEqual (int.MaxValue, (int?) new XElement ("k", int.MaxValue), "MaxValue:int?");
1159 Assert.AreEqual (int.MinValue, (int?) new XElement ("k", int.MinValue), "MinValue:int?");
1160 Assert.AreEqual (long.MaxValue, (long?) new XElement ("k", long.MaxValue), "MaxValue:long?");
1161 Assert.AreEqual (long.MinValue, (long?) new XElement ("k", long.MinValue), "MinValue:long?");
1162 Assert.AreEqual (uint.MaxValue, (uint?) new XElement ("k", uint.MaxValue), "MaxValue:uint?");
1163 Assert.AreEqual (uint.MinValue, (uint?) new XElement ("k", uint.MinValue), "MinValue:uint?");
1164 Assert.AreEqual (ulong.MaxValue, (ulong?) new XElement ("k", ulong.MaxValue), "MaxValue:ulong?");
1165 Assert.AreEqual (ulong.MinValue, (ulong?) new XElement ("k", ulong.MinValue), "MinValue:ulong?");
1166 Assert.AreEqual (DateTime.MaxValue, (DateTime) new XElement ("k", DateTime.MaxValue), "MaxValue:DateTime");
1167 Assert.AreEqual (DateTime.MinValue, (DateTime) new XElement ("k", DateTime.MinValue), "MinValue:DateTime");
1168 Assert.AreEqual (DateTime.MaxValue, (DateTime?) new XElement ("k", DateTime.MaxValue), "MaxValue:DateTime?");
1169 Assert.AreEqual (DateTime.MinValue, (DateTime?) new XElement ("k", DateTime.MinValue), "MinValue:DateTime?");
1170 Assert.AreEqual (DateTimeOffset.MaxValue, (DateTimeOffset) new XElement ("k", DateTimeOffset.MaxValue), "MaxValue:DateTimeOffset");
1171 Assert.AreEqual (DateTimeOffset.MinValue, (DateTimeOffset) new XElement ("k", DateTimeOffset.MinValue), "MinValue:DateTimeOffset");
1172 Assert.AreEqual (DateTimeOffset.MaxValue, (DateTimeOffset?) new XElement ("k", DateTimeOffset.MaxValue), "MaxValue:DateTimeOffset?");
1173 Assert.AreEqual (DateTimeOffset.MinValue, (DateTimeOffset?) new XElement ("k", DateTimeOffset.MinValue), "MinValue:DateTimeOffset?");
1174 Assert.AreEqual (TimeSpan.MaxValue, (TimeSpan) new XElement ("k", TimeSpan.MaxValue), "MaxValue:TimeSpan");
1175 Assert.AreEqual (TimeSpan.MinValue, (TimeSpan) new XElement ("k", TimeSpan.MinValue), "MinValue:TimeSpan");
1176 Assert.AreEqual (TimeSpan.MaxValue, (TimeSpan?) new XElement ("k", TimeSpan.MaxValue), "MaxValue:TimeSpan?");
1177 Assert.AreEqual (TimeSpan.MinValue, (TimeSpan?) new XElement ("k", TimeSpan.MinValue), "MinValue:TimeSpan?");
1181 public void CastBooleans ()
1183 Assert.IsNotNull ((bool?) new XElement ("fq", "false"), "#1a");
1184 Assert.AreEqual (false, ((bool?) new XElement ("fq", "false")).Value, "#1b");
1185 Assert.IsNotNull ((bool?) new XElement ("tq", "true"), "#2a");
1186 Assert.AreEqual (true, ((bool?) new XElement ("tq", "true")).Value, "#2b");
1187 Assert.IsNotNull ((bool?) new XElement ("Fq", "False"), "#3a");
1188 Assert.AreEqual (false, ((bool?) new XElement ("Fq", "False")).Value, "#3b");
1189 Assert.IsNotNull ((bool?) new XElement ("Tq", "True"), "#4a");
1190 Assert.AreEqual (true, ((bool?) new XElement ("Tq", "True")).Value, "#4b");
1191 Assert.IsNotNull ((bool?) new XElement ("Fs", " False \t \r "), "#5a");
1192 Assert.AreEqual (false, ((bool?) new XElement ("Fs", " False \t \r ")).Value, "#5b");
1193 Assert.IsNotNull ((bool?) new XElement ("Ts", " \t True \n "), "#6a");
1194 Assert.AreEqual (true, ((bool?) new XElement ("Ts", " \t True \n ")).Value, "#6b");
1195 Assert.AreEqual (false, (bool) new XElement ("f", "false"), "#7");
1196 Assert.AreEqual (true, (bool) new XElement ("t", "true"), "#8");
1197 Assert.AreEqual (false, (bool) new XElement ("F", "False"), "#9");
1198 Assert.AreEqual (true, (bool) new XElement ("T", "True"), "#10");
1199 Assert.AreEqual (false, (bool)new XElement ("fs", " false "), "#11");
1200 Assert.AreEqual (true, (bool)new XElement ("ts", " true "), "#12");
1201 Assert.IsNotNull ((bool?) new XElement ("Tc", new XCData (" \t True \n ")), "#13a");
1202 Assert.AreEqual (true, ((bool?) new XElement ("Tc", new XCData (" \t True \n "))).Value, "#13b");
1203 Assert.AreEqual (false, (bool)new XElement ("fc", new XCData (" false ")), "#14");
1204 Assert.IsNotNull ((bool?) new XElement ("x", true), "#15a");
1205 Assert.IsTrue (((bool?) new XElement ("x", true)).Value, "#15b");
1206 Assert.IsTrue ((bool) new XElement ("x", true), "#15c");
1207 Assert.IsNotNull ((bool?) new XElement ("x", false), "#16a");
1208 Assert.IsFalse (((bool?) new XElement ("x", false)).Value, "#16b");
1209 Assert.IsFalse ((bool) new XElement ("x", false), "#16c");
1210 Assert.IsTrue ((bool) new XElement ("x", bool.TrueString), "#17a");
1211 Assert.IsFalse ((bool) new XElement ("x", bool.FalseString), "#17b");
1212 Assert.IsTrue ((bool) new XElement ("x", new XCData (bool.TrueString)), "#18a");
1213 Assert.IsFalse ((bool) new XElement ("x", new XCData (bool.FalseString)), "#18b");
1217 public void CastGuids ()
1219 Guid rb = new Guid (new byte[16] { 0x9A, 0xBF, 0xCE, 0x7E, 0x07, 0x29, 0x9C, 0x43, 0x80, 0x7D, 0x48, 0x20, 0xB9, 0x19, 0xEA, 0x57 });
1220 Guid rd = new Guid (new byte[16] { 0x21, 0x5B, 0x57, 0x26, 0xCD, 0x14, 0x5E, 0x44, 0x8F, 0xFA, 0xE2, 0xBC, 0x24, 0x7B, 0x2E, 0xC9 });
1221 Guid rn = new Guid (new byte[16] { 0xF9, 0x46, 0x41, 0xA8, 0xA5, 0x03, 0xF1, 0x4A, 0xAD, 0x97, 0x7B, 0xC7, 0x79, 0x57, 0x2B, 0x79 });
1222 Guid rp = new Guid (new byte[16] { 0x51, 0x6B, 0x8A, 0x17, 0xEF, 0x11, 0xFB, 0x48, 0x83, 0xBD, 0x57, 0xB4, 0x99, 0xF9, 0xC1, 0xE6 });
1223 Guid rz = Guid.Empty;
1224 Guid rx = Guid.NewGuid ();
1226 XElement b = new XElement ("b", " {7ECEBF9A-2907-439c-807D-4820B919EA57}");
1227 XElement d = new XElement ("d", "26575b21-14cd-445e-8ffa-e2bc247b2ec9");
1228 XElement n = new XElement ("n", "a84146f903A54af1ad977bC779572b79\r\n");
1229 XElement p = new XElement ("p", " (178a6b51-11ef-48fb-83bd-57b499f9c1e6) \t ");
1230 XElement z = new XElement ("z", " \t \n 00000000-0000-0000-0000-000000000000 ");
1231 XElement x = new XElement ("x", rx);
1233 Assert.IsNotNull ((Guid?) new XElement (b), "#1a");
1234 Assert.AreEqual (rb, ((Guid?) new XElement (b)).Value, "#1b");
1235 Assert.AreEqual (rb, (Guid) new XElement (b), "#1c");
1236 Assert.AreEqual (rb, (Guid) new XElement ("r", rb), "#1d");
1237 Assert.IsNotNull ((Guid?) new XElement ("r", rb), "#1e");
1238 Assert.AreEqual (rb, ((Guid?) new XElement ("r", rb)).Value, "#1f");
1240 Assert.IsNotNull ((Guid?) new XElement (d), "#2a");
1241 Assert.AreEqual (rd, ((Guid?) new XElement (d)).Value, "#2b");
1242 Assert.AreEqual (rd, (Guid) new XElement (d), "#2c");
1243 Assert.AreEqual (rd, (Guid) new XElement ("r", rd), "#2d");
1244 Assert.IsNotNull ((Guid?) new XElement ("r", rd), "#2e");
1245 Assert.AreEqual (rd, ((Guid?) new XElement ("r", rd)).Value, "#2f");
1247 Assert.IsNotNull ((Guid?) new XElement (n), "#3a");
1248 Assert.AreEqual (rn, ((Guid?) new XElement (n)).Value, "#3b");
1249 Assert.AreEqual (rn, (Guid) new XElement (n), "#3c");
1250 Assert.AreEqual (rn, (Guid) new XElement ("r", rn), "#3d");
1251 Assert.IsNotNull ((Guid?) new XElement ("r", rn), "#3e");
1252 Assert.AreEqual (rn, ((Guid?) new XElement ("r", rn)).Value, "#3f");
1254 Assert.IsNotNull ((Guid?) new XElement (p), "#4a");
1255 Assert.AreEqual (rp, ((Guid?) new XElement (p)).Value, "#4b");
1256 Assert.AreEqual (rp, (Guid) new XElement (p), "#4c");
1257 Assert.AreEqual (rp, (Guid) new XElement ("r", rp), "#4d");
1258 Assert.IsNotNull ((Guid?) new XElement ("r", rp), "#4e");
1259 Assert.AreEqual (rp, ((Guid?) new XElement ("r", rp)).Value, "#4f");
1261 Assert.IsNotNull ((Guid?) new XElement (z), "#5a");
1262 Assert.AreEqual (rz, ((Guid?) new XElement (z)).Value, "#5b");
1263 Assert.AreEqual (rz, (Guid) new XElement (z), "#5c");
1265 Assert.IsNotNull ((Guid?) new XElement (x), "#6a");
1266 Assert.AreEqual (rx, ((Guid?) new XElement (x)).Value, "#6b");
1267 Assert.AreEqual (rx, (Guid) new XElement (x), "#6c");
1271 public void CastDateTimes ()
1273 DateTime ra = new DateTime (1987, 1, 23, 21, 45, 36, 89, DateTimeKind.Unspecified);
1274 DateTime rb = new DateTime (2001, 2, 3, 4, 5, 6, 789, DateTimeKind.Local);
1275 DateTime rc = new DateTime (2010, 1, 2, 0, 0, 0, 0, DateTimeKind.Utc);
1276 DateTime rd = new DateTime (1956, 11, 2, 0, 34, 0);
1277 DateTime re = new DateTime (635085111683456297L, DateTimeKind.Utc);
1278 DateTime rf = re.ToLocalTime ();
1279 DateTime rx = DateTime.Now;
1280 DateTime rz = DateTime.UtcNow;
1282 XElement a = new XElement ("a", "1987-01-23T21:45:36.089");
1283 XElement b = new XElement ("b", "2001-02-03T04:05:06.789" + rb.ToString ("zzz"));
1284 XElement c = new XElement ("c", "2010-01-02T00:00:00Z");
1285 XElement d = new XElement ("d", " Nov 2, 1956 12:34 AM \r\n \t");
1286 XElement e = new XElement ("e", " 2013-07-04T05:06:08.3456297Z "); // UTC, all the way
1287 XElement f = new XElement ("f", " 2013-07-04T05:06:08.3456297+00:00 "); // UTC initially, but should be converted automatically to local time
1288 XElement x = new XElement ("x", rx);
1289 XElement z = new XElement ("z", rz);
1291 Assert.IsNotNull ((DateTime?) new XElement (a), "#1a");
1292 Assert.AreEqual (ra, ((DateTime?) new XElement (a)).Value, "#1b");
1293 Assert.AreEqual (ra, (DateTime) new XElement (a), "#1c");
1294 Assert.AreEqual (ra, (DateTime) new XElement ("r", ra), "#1d");
1295 Assert.IsNotNull ((DateTime?) new XElement ("r", ra), "#1e");
1296 Assert.AreEqual (ra, ((DateTime?) new XElement ("r", ra)).Value, "#1f");
1298 Assert.IsNotNull ((DateTime?) new XElement (b), "#2a");
1299 Assert.AreEqual (rb, ((DateTime?) new XElement (b)).Value, "#2b");
1300 Assert.AreEqual (rb, (DateTime) new XElement (b), "#2c");
1301 Assert.AreEqual (rb, (DateTime) new XElement ("r", rb), "#2d");
1302 Assert.IsNotNull ((DateTime?) new XElement ("r", rb), "#2e");
1303 Assert.AreEqual (rb, ((DateTime?) new XElement ("r", rb)).Value, "#2f");
1305 Assert.IsNotNull ((DateTime?) new XElement (c), "#3a");
1306 Assert.AreEqual (rc, ((DateTime?) new XElement (c)).Value, "#3b");
1307 Assert.AreEqual (rc, (DateTime) new XElement (c), "#3c");
1308 Assert.AreEqual (rc, (DateTime) new XElement ("r", rc), "#3d");
1309 Assert.IsNotNull ((DateTime?) new XElement ("r", rc), "#3e");
1310 Assert.AreEqual (rc, ((DateTime?) new XElement ("r", rc)).Value, "#3f");
1312 Assert.IsNotNull ((DateTime?) new XElement (d), "#4a");
1313 Assert.AreEqual (rd, ((DateTime?) new XElement (d)).Value, "#4b");
1314 Assert.AreEqual (rd, (DateTime) new XElement (d), "#4c");
1315 Assert.AreEqual (rd, (DateTime) new XElement ("r", rd), "#4d");
1316 Assert.IsNotNull ((DateTime?) new XElement ("r", rd), "#4e");
1317 Assert.AreEqual (rd, ((DateTime?) new XElement ("r", rd)).Value, "#4f");
1319 Assert.IsNotNull ((DateTime?) new XElement (x), "#5a");
1320 Assert.AreEqual (rx, ((DateTime?) new XElement (x)).Value, "#5b");
1321 Assert.AreEqual (rx, (DateTime) new XElement (x), "#5c");
1323 Assert.IsNotNull ((DateTime?) new XElement (z), "#6a");
1324 Assert.AreEqual (rz, ((DateTime?) new XElement (z)).Value, "#6b");
1325 Assert.AreEqual (rz, (DateTime) new XElement (z), "#6c");
1327 Assert.IsNotNull ((DateTime?) new XElement (e), "#7a");
1328 Assert.AreEqual (re, ((DateTime?) new XElement (e)).Value, "#7b");
1329 Assert.AreEqual (re, (DateTime) new XElement (e), "#7c");
1330 Assert.AreEqual (re, (DateTime) new XElement ("r", re), "#7d");
1331 Assert.IsNotNull ((DateTime?) new XElement ("r", re), "#7e");
1332 Assert.AreEqual (re, ((DateTime?) new XElement ("r", re)).Value, "#7f");
1334 Assert.IsNotNull ((DateTime?) new XElement (f), "#8a");
1335 Assert.AreEqual (rf, ((DateTime?) new XElement (f)).Value, "#8b");
1336 Assert.AreEqual (rf, (DateTime) new XElement (f), "#8c");
1337 Assert.AreEqual (rf, (DateTime) new XElement ("r", rf), "#8d");
1338 Assert.IsNotNull ((DateTime?) new XElement ("r", rf), "#8e");
1339 Assert.AreEqual (rf, ((DateTime?) new XElement ("r", rf)).Value, "#8f");
1343 public void CastDateTimeOffsets ()
1345 DateTimeOffset ra = new DateTimeOffset (1987, 1, 23, 21, 45, 36, 89, TimeSpan.FromHours (+13.75)); // e.g., Chatham Islands (daylight-savings time)
1346 DateTimeOffset rb = new DateTimeOffset (2001, 2, 3, 4, 5, 6, 789, DateTimeOffset.Now.Offset); // Local time
1347 DateTimeOffset rc = new DateTimeOffset (2010, 1, 2, 0, 0, 0, 0, TimeSpan.Zero); // UTC
1348 DateTimeOffset rd = new DateTimeOffset (1956, 11, 2, 12, 34, 10, TimeSpan.FromHours (-3.5));
1349 DateTimeOffset re = new DateTimeOffset (630646468235678363, TimeSpan.FromHours (-1)); // UTC-1, also with full resolution and a fractional second that might lose a tick on Mono 2.6.1
1350 DateTimeOffset rx = DateTimeOffset.Now;
1351 DateTimeOffset rz = DateTimeOffset.UtcNow;
1353 XElement a = new XElement ("a", "1987-01-23T21:45:36.089+13:45");
1354 XElement b = new XElement ("b", "2001-02-03T04:05:06.789" + DateTimeOffset.Now.ToString ("zzz"));
1355 XElement c = new XElement ("c", "2010-01-02T00:00:00Z");
1356 XElement d = new XElement ("d", " Nov 2, 1956 12:34:10 PM -3:30 \r\n \t");
1357 XElement e = new XElement ("e", " \t \n 1999-06-10T21:27:03.5678363-01:00 ");
1358 XElement x = new XElement ("x", rx);
1359 XElement z = new XElement ("z", rz);
1361 Assert.IsNotNull ((DateTimeOffset?) new XElement (a), "#1a");
1362 Assert.AreEqual (ra, ((DateTimeOffset?) new XElement (a)).Value, "#1b");
1363 Assert.AreEqual (ra, (DateTimeOffset) new XElement (a), "#1c");
1364 Assert.AreEqual (ra, (DateTimeOffset) new XElement ("r", ra), "#1d");
1365 Assert.IsNotNull ((DateTimeOffset?) new XElement ("r", ra), "#1e");
1366 Assert.AreEqual (ra, ((DateTimeOffset?) new XElement ("r", ra)).Value, "#1f");
1368 Assert.IsNotNull ((DateTimeOffset?) new XElement (b), "#2a");
1369 Assert.AreEqual (rb, ((DateTimeOffset?) new XElement (b)).Value, "#2b");
1370 Assert.AreEqual (rb, (DateTimeOffset) new XElement (b), "#2c");
1371 Assert.AreEqual (rb, (DateTimeOffset) new XElement ("r", rb), "#2d");
1372 Assert.IsNotNull ((DateTimeOffset?) new XElement ("r", rb), "#2e");
1373 Assert.AreEqual (rb, ((DateTimeOffset?) new XElement ("r", rb)).Value, "#2f");
1375 Assert.IsNotNull ((DateTimeOffset?) new XElement (c), "#3a");
1376 Assert.AreEqual (rc, ((DateTimeOffset?) new XElement (c)).Value, "#3b");
1377 Assert.AreEqual (rc, (DateTimeOffset) new XElement (c), "#3c");
1378 Assert.AreEqual (rc, (DateTimeOffset) new XElement ("r", rc), "#3d");
1379 Assert.IsNotNull ((DateTimeOffset?) new XElement ("r", rc), "#3e");
1380 Assert.AreEqual (rc, ((DateTimeOffset?) new XElement ("r", rc)).Value, "#3f");
1382 AssertThrows<FormatException> (() => { DateTimeOffset? r = (DateTimeOffset?) new XElement (d); }, "#4a");
1383 AssertThrows<FormatException> (() => { DateTimeOffset r = (DateTimeOffset) new XElement (d); }, "#4b");
1384 Assert.AreEqual (rd, DateTimeOffset.Parse (d.Value), "#4c"); // Sanity check: Okay for standalone DateTimeOffset but not as XML as in above
1386 Assert.IsNotNull ((DateTimeOffset?) new XElement (x), "#5a");
1387 Assert.AreEqual (rx, ((DateTimeOffset?) new XElement (x)).Value, "#5b");
1388 Assert.AreEqual (rx, (DateTimeOffset) new XElement (x), "#5c");
1390 Assert.IsNotNull ((DateTimeOffset?) new XElement (z), "#6a");
1391 Assert.AreEqual (rz, ((DateTimeOffset?) new XElement (z)).Value, "#6b");
1392 Assert.AreEqual (rz, (DateTimeOffset) new XElement (z), "#6c");
1394 Assert.IsNotNull ((DateTimeOffset?) new XElement (e), "#7a");
1395 Assert.AreEqual (re, ((DateTimeOffset?) new XElement (e)).Value, "#7b");
1396 Assert.AreEqual (re, (DateTimeOffset) new XElement (e), "#7c");
1397 Assert.AreEqual (re, (DateTimeOffset) new XElement ("r", re), "#7d");
1398 Assert.IsNotNull ((DateTimeOffset?) new XElement ("r", re), "#7e");
1399 Assert.AreEqual (re, ((DateTimeOffset?) new XElement ("r", re)).Value, "#7f");
1403 public void CastTimeSpans ()
1405 TimeSpan ra = new TimeSpan (23, 21, 45, 36, 89);
1406 TimeSpan rb = -new TimeSpan (3, 4, 5, 6, 789);
1407 TimeSpan rc = new TimeSpan (2, 0, 0, 0, 0);
1408 TimeSpan rd = new TimeSpan (0, 0, 0, 1);
1409 TimeSpan re = new TimeSpan (1L); // one tick, the smallest interval
1410 TimeSpan rx = DateTimeOffset.Now.Offset;
1411 TimeSpan rz = TimeSpan.Zero;
1413 XElement a = new XElement ("a", "P23DT21H45M36.089S");
1414 XElement b = new XElement ("b", "-P3DT4H5M6.789S");
1415 XElement c = new XElement ("c", "P2D");
1416 XElement d = new XElement ("d", "PT1S");
1417 XElement e = new XElement ("e", " PT0.0000001S \t \n ");
1418 XElement x = new XElement ("x", rx);
1419 XElement z = new XElement ("z", rz);
1421 Assert.IsNotNull ((TimeSpan?) new XElement (a), "#1a");
1422 Assert.AreEqual (ra, ((TimeSpan?) new XElement (a)).Value, "#1b");
1423 Assert.AreEqual (ra, (TimeSpan) new XElement (a), "#1c");
1424 Assert.AreEqual (ra, (TimeSpan) new XElement ("r", ra), "#1d");
1425 Assert.IsNotNull ((TimeSpan?) new XElement ("r", ra), "#1e");
1426 Assert.AreEqual (ra, ((TimeSpan?) new XElement ("r", ra)).Value, "#1f");
1428 Assert.IsNotNull ((TimeSpan?) new XElement (b), "#2a");
1429 Assert.AreEqual (rb, ((TimeSpan?) new XElement (b)).Value, "#2b");
1430 Assert.AreEqual (rb, (TimeSpan) new XElement (b), "#2c");
1431 Assert.AreEqual (rb, (TimeSpan) new XElement ("r", rb), "#2d");
1432 Assert.IsNotNull ((TimeSpan?) new XElement ("r", rb), "#2e");
1433 Assert.AreEqual (rb, ((TimeSpan?) new XElement ("r", rb)).Value, "#2f");
1435 Assert.IsNotNull ((TimeSpan?) new XElement (c), "#3a");
1436 Assert.AreEqual (rc, ((TimeSpan?) new XElement (c)).Value, "#3b");
1437 Assert.AreEqual (rc, (TimeSpan) new XElement (c), "#3c");
1438 Assert.AreEqual (rc, (TimeSpan) new XElement ("r", rc), "#3d");
1439 Assert.IsNotNull ((TimeSpan?) new XElement ("r", rc), "#3e");
1440 Assert.AreEqual (rc, ((TimeSpan?) new XElement ("r", rc)).Value, "#3f");
1442 Assert.IsNotNull ((TimeSpan?) new XElement (d), "#4a");
1443 Assert.AreEqual (rd, ((TimeSpan?) new XElement (d)).Value, "#4b");
1444 Assert.AreEqual (rd, (TimeSpan) new XElement (d), "#4c");
1445 Assert.AreEqual (rd, (TimeSpan) new XElement ("r", rd), "#4d");
1446 Assert.IsNotNull ((TimeSpan?) new XElement ("r", rd), "#4e");
1447 Assert.AreEqual (rd, ((TimeSpan?) new XElement ("r", rd)).Value, "#4f");
1449 Assert.IsNotNull ((TimeSpan?) new XElement (x), "#5a");
1450 Assert.AreEqual (rx, ((TimeSpan?) new XElement (x)).Value, "#5b");
1451 Assert.AreEqual (rx, (TimeSpan) new XElement (x), "#5c");
1453 Assert.IsNotNull ((TimeSpan?) new XElement (z), "#6a");
1454 Assert.AreEqual (rz, ((TimeSpan?) new XElement (z)).Value, "#6b");
1455 Assert.AreEqual (rz, (TimeSpan) new XElement (z), "#6c");
1457 Assert.IsNotNull ((TimeSpan?) new XElement (e), "#7a");
1458 Assert.AreEqual (re, ((TimeSpan?) new XElement (e)).Value, "#7b");
1459 Assert.AreEqual (re, (TimeSpan) new XElement (e), "#7c");
1460 Assert.AreEqual (re, (TimeSpan) new XElement ("r", re), "#7d");
1461 Assert.IsNotNull ((TimeSpan?) new XElement ("r", re), "#7e");
1462 Assert.AreEqual (re, ((TimeSpan?) new XElement ("r", re)).Value, "#7f");
1464 #pragma warning restore 219
1467 public void Value ()
1469 // based on bug #360858
1470 XElement a = new XElement("root",
1471 new XElement ("foo"),
1473 new XComment ("comment"),
1474 new XElement ("bar"));
1475 Assert.AreEqual ("Linux&Windows", a.Value);
1479 [ExpectedException (typeof (ArgumentException))]
1480 public void SetValueXAttribute ()
1482 new XElement ("foo").SetValue (new XAttribute ("foo", "bar"));
1486 [ExpectedException (typeof (ArgumentException))]
1487 public void SetValueXDocumnent ()
1489 new XElement ("foo").SetValue (new XDocument ());
1493 // LAMESPEC: there is no reason to not reject XDeclaration while it rejects XDocument.
1494 [ExpectedException (typeof (ArgumentException))]
1495 [Category ("NotDotNet")]
1496 public void SetValueXDeclaration ()
1498 var el = new XElement ("foo");
1499 el.SetValue (new XDeclaration ("1.0", null, null));
1500 Assert.AreEqual ("<?xml version=\"1.0\"?>", el.Value);
1504 [ExpectedException (typeof (ArgumentNullException))]
1505 public void SetValueNull ()
1507 new XElement ("foo", "text").SetValue (null);
1511 public void AddSameInstance () // bug #392063
1513 XElement root = new XElement (XName.Get ("Root", ""));
1514 XElement child = new XElement (XName.Get ("Child", ""));
1522 [ExpectedException (typeof (InvalidOperationException))]
1523 public void AddSameInstance2 ()
1525 XElement root = new XElement (XName.Get ("Root"));
1526 XAttribute attr = new XAttribute (XName.Get ("a"), "v");
1529 root.Add (attr); // duplicate attribute
1534 public void AddAttributeFromDifferentTree ()
1536 XElement e1 = new XElement (XName.Get ("e1"));
1537 XElement e2 = new XElement (XName.Get ("e2"));
1538 XAttribute attr = new XAttribute (XName.Get ("a"), "v");
1542 Assert.AreEqual ("<e1 a=\"v\" />", e1.ToString (), "#1");
1543 Assert.AreEqual ("<e2 a=\"v\" />", e2.ToString (), "#2");
1547 public void SavePreservePrefixes ()
1549 var x = XDocument.Parse (@"
1550 <xxx:a xmlns:xxx='http://www.foobar.com'>
1551 <xxx:b>blah blah blah</xxx:b>
1553 StringWriter sw = new StringWriter ();
1554 x.Save (sw, SaveOptions.DisableFormatting);
1555 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 ());
1559 public void LoadFromXmlTextReader ()
1561 var foo = XElement.Load (new XmlTextReader (new StringReader ("<foo></foo>")));
1562 Assert.IsNotNull (foo);
1566 public void ReplaceNodes ()
1568 var inputXml = "<Foo><C><Three>3</Three><Two></Two><One/></C><B><Aaa/><Yyy/><fff/></B><A Attrib=\"Hello World\"/></Foo>";
1569 var reader = XmlReader.Create (new StringReader (inputXml), new XmlReaderSettings ());
1570 XDocument doc = XDocument.Load (reader);
1571 var result = doc.Root.Elements ().OrderBy (el => el.Name.ToString());
1572 Assert.AreEqual (3, result.Count (), "#1");
1573 doc.Root.FirstNode.Remove ();
1574 Assert.AreEqual (2, result.Count (), "#2");
1576 XContainer container = doc.Root;
1577 container.ReplaceNodes (result);
1579 Assert.AreEqual (2, container.Elements ().Count (), "#3");
1583 public void ReplaceCreatesSnapshotBeforeRemoval ()
1586 XElement data1 = new XElement ("A");
1587 XElement data3 = new XElement ("C");
1588 XElement data4 = new XElement ("D");
1589 XElement root = new XElement ("rt",
1590 new XElement ("z", new XElement ("Name", data1), new XElement ("Desc", data4)), data3);
1591 var elements = root.Elements ().Elements ();
1592 root.ReplaceNodes (elements);
1593 root.Add (elements);
1604 Assert.AreEqual (xml, root.ToString ().Replace ("\r\n", "\n"), "#1");
1607 private class EventHandler
1609 public bool ChangingInvoked { get; private set; }
1611 public bool HasChanged { get; private set; }
1613 public void OnChanging (object node, XObjectChangeEventArgs args)
1615 ChangingInvoked = true;
1618 public void OnChanged (object node, XObjectChangeEventArgs args)
1625 public void Add_ToRootNode_ChangeTriggers ()
1627 var inputXml = "<root><a><b /></a></root>";
1628 var reader = XmlReader.Create (new StringReader (inputXml), new XmlReaderSettings ());
1629 XDocument doc = XDocument.Load (reader);
1630 var eventHandler = new EventHandler ();
1631 doc.Root.Changing += eventHandler.OnChanging;
1632 doc.Root.Changed += eventHandler.OnChanged;
1634 var newElement = XElement.Parse ("<c/>");
1636 doc.Root.Add (newElement);
1638 Assert.IsTrue (eventHandler.ChangingInvoked, "OnChanging not triggered");
1639 Assert.IsTrue (eventHandler.HasChanged, "OnChanged not triggered");
1643 public void Add_ToChildNode_ChangeTriggersOnRoot ()
1645 var inputXml = "<root><a><b /></a></root>";
1646 var reader = XmlReader.Create (new StringReader (inputXml), new XmlReaderSettings ());
1647 XDocument doc = XDocument.Load (reader);
1648 var eventHandler = new EventHandler ();
1650 var newElement = XElement.Parse ("<c/>");
1651 doc.Root.Add (newElement);
1653 doc.Root.Changing += eventHandler.OnChanging;
1654 doc.Root.Changed += eventHandler.OnChanged;
1655 var nextNewElement = XElement.Parse ("<d/>");
1656 newElement.Add (nextNewElement);
1658 Assert.IsTrue (eventHandler.ChangingInvoked, "OnChanging not triggered");
1659 Assert.IsTrue (eventHandler.HasChanged, "OnChanged not triggered");
1663 public void SetElementValue () // #699242
1665 var element = XElement.Parse ("<foo><bar>bar</bar><baz>baz</baz></foo>");
1666 element.SetElementValue ("bar", "babar");
1667 element.SetElementValue ("baz", "babaz");
1668 element.SetElementValue ("gaz", "gazonk");
1670 Assert.AreEqual ("<foo><bar>babar</bar><baz>babaz</baz><gaz>gazonk</gaz></foo>", element.ToString (SaveOptions.DisableFormatting));
1674 public void Bug3137 ()
1676 CultureInfo current = Thread.CurrentThread.CurrentCulture;
1678 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");
1679 var element1 = new XElement ("Property1", new XAttribute ("type", "number"), 1.2343445);
1680 Assert.AreEqual ("<Property1 type=\"number\">1.2343445</Property1>", element1.ToString (), "en-US");
1682 Thread.CurrentThread.CurrentCulture = new CultureInfo ("de-DE");
1683 // this was already working because the element was created with en-US
1684 Assert.AreEqual ("<Property1 type=\"number\">1.2343445</Property1>", element1.ToString (), "de-DE/1");
1685 // however creating a new, identical, element under de-DE return*ed* a different string
1686 var element2 = new XElement ("Property1", new XAttribute ("type", "number"), 1.2343445);
1687 Assert.AreEqual ("<Property1 type=\"number\">1.2343445</Property1>", element2.ToString (), "de-DE/2");
1690 Thread.CurrentThread.CurrentCulture = current;
1695 public void DecimalFormatting () // bug #3634
1698 var bak = Thread.CurrentThread.CurrentCulture;
1699 Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfoByIetfLanguageTag("nl-NL");
1701 var element = new XElement ("Demo", data);
1702 Assert.AreEqual ("<Demo>5.5</Demo>", element.ToString (), "#1");
1704 Thread.CurrentThread.CurrentCulture = bak;