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;
30 using System.Xml.Linq;
32 using NUnit.Framework;
34 namespace MonoTests.System.Xml.Linq
37 public class XAttributeTest
41 [ExpectedException (typeof (ArgumentNullException))]
42 public void ConstructorNameNull ()
44 XAttribute a = new XAttribute (null, "v");
49 [ExpectedException (typeof (ArgumentNullException))]
50 public void ConstructorValueNull ()
52 XAttribute a = new XAttribute (XName.Get ("a"), null);
57 public void IsNamespaceDeclaration ()
59 string xml = "<root a='v' xmlns='urn:foo' xmlns:x='urn:x' x:a='v' xmlns:xml='http://www.w3.org/XML/1998/namespace' />";
60 XElement el = XElement.Parse (xml);
61 List<XAttribute> l = new List<XAttribute> (el.Attributes ());
62 Assert.IsFalse (l [0].IsNamespaceDeclaration, "#1");
63 Assert.IsTrue (l [1].IsNamespaceDeclaration, "#2");
64 Assert.IsTrue (l [2].IsNamespaceDeclaration, "#3");
65 Assert.IsFalse (l [3].IsNamespaceDeclaration, "#4");
66 Assert.IsTrue (l [4].IsNamespaceDeclaration, "#5");
68 Assert.AreEqual ("a", l [0].Name.LocalName, "#2-1");
69 Assert.AreEqual ("xmlns", l [1].Name.LocalName, "#2-2");
70 Assert.AreEqual ("x", l [2].Name.LocalName, "#2-3");
71 Assert.AreEqual ("a", l [3].Name.LocalName, "#2-4");
72 Assert.AreEqual ("xml", l [4].Name.LocalName, "#2-5");
74 Assert.AreEqual ("", l [0].Name.NamespaceName, "#3-1");
75 // not sure how current Orcas behavior makes sense here though ...
76 Assert.AreEqual ("", l [1].Name.NamespaceName, "#3-2");
77 Assert.AreEqual ("http://www.w3.org/2000/xmlns/", l [2].Name.NamespaceName, "#3-3");
78 Assert.AreEqual ("urn:x", l [3].Name.NamespaceName, "#3-4");
79 Assert.AreEqual ("http://www.w3.org/2000/xmlns/", l [4].Name.NamespaceName, "#3-5");
83 public void Document ()
85 XDocument doc = XDocument.Parse ("<root a='v' />");
86 Assert.AreEqual (doc, doc.Root.Document, "#1");
87 foreach (XAttribute a in doc.Root.Attributes ())
88 Assert.AreEqual (doc, a.Document, "#2");
89 Assert.AreEqual (doc, doc.Document, "#3");
93 public void SetValue ()
95 XAttribute a = new XAttribute (XName.Get ("a"), "v");
96 a.SetValue (new XDeclaration ("1.0", null, null));
97 // value object is converted to a string.
98 Assert.AreEqual ("<?xml version=\"1.0\"?>", a.Value, "#1");
102 public void ToString ()
104 XAttribute a = new XAttribute (XName.Get ("a"), "v");
105 Assert.AreEqual ("a=\"v\"", a.ToString ());
107 a = new XAttribute (XName.Get ("a"), " >_< ");
108 Assert.AreEqual ("a=\" >_< \"", a.ToString ());
112 public void DateTimeAttribute ()
114 var date = DateTime.Now;
115 var attribute = new XAttribute ("Date", date);
117 var value = (DateTime) attribute;
119 Assert.AreEqual (date, value);
122 #pragma warning disable 219
124 public void CastNulls ()
126 const XAttribute a = null;
128 Assert.AreEqual (null, (bool?) a, "bool?");
129 Assert.AreEqual (null, (DateTime?) a, "DateTime?");
130 Assert.AreEqual (null, (DateTimeOffset?) a, "DateTimeOffset?");
131 Assert.AreEqual (null, (decimal?) a, "decimal?");
132 Assert.AreEqual (null, (double?) a, "double?");
133 Assert.AreEqual (null, (float?) a, "float?");
134 Assert.AreEqual (null, (Guid?) a, "Guid?");
135 Assert.AreEqual (null, (int?) a, "int?");
136 Assert.AreEqual (null, (long?) a, "long?");
137 Assert.AreEqual (null, (uint?) a, "uint?");
138 Assert.AreEqual (null, (ulong?) a, "ulong?");
139 Assert.AreEqual (null, (TimeSpan?) a, "TimeSpan?");
140 Assert.AreEqual (null, (string) a, "string");
141 AssertThrows<ArgumentNullException> (() => { bool z = (bool) a; }, "bool");
142 AssertThrows<ArgumentNullException> (() => { DateTime z = (DateTime) a; }, "DateTime");
143 AssertThrows<ArgumentNullException> (() => { DateTimeOffset z = (DateTimeOffset) a; }, "DateTimeOffset");
144 AssertThrows<ArgumentNullException> (() => { decimal z = (decimal) a; }, "decimal");
145 AssertThrows<ArgumentNullException> (() => { double z = (double) a; }, "double");
146 AssertThrows<ArgumentNullException> (() => { float z = (float) a; }, "float");
147 AssertThrows<ArgumentNullException> (() => { Guid z = (Guid) a; }, "Guid");
148 AssertThrows<ArgumentNullException> (() => { int z = (int) a; }, "int");
149 AssertThrows<ArgumentNullException> (() => { long z = (long) a; }, "long");
150 AssertThrows<ArgumentNullException> (() => { uint z = (uint) a; }, "uint");
151 AssertThrows<ArgumentNullException> (() => { ulong z = (ulong) a; }, "ulong");
152 AssertThrows<ArgumentNullException> (() => { TimeSpan z = (TimeSpan) a; }, "TimeSpan");
156 /// Provides functionality similar to Assert.Throws that is available on newer versions of NUnit.
158 private static T AssertThrows<T> (Action code, string message, params object[] args) where T : Exception
160 Exception actual = null;
163 } catch (Exception exception) {
166 Assert.That (actual, new NUnit.Framework.Constraints.ExactTypeConstraint (typeof (T)), message, args);
171 public void CastEmptiesOrBlanks ()
173 XAttribute a = new XAttribute ("a", String.Empty);
175 // Verify expected "cloning" and "empty/blank" behaviour as prerequisites
176 Assert.AreEqual (String.Empty, a.Value, "#1-1");
177 Assert.AreEqual (String.Empty, new XAttribute (a).Value, "#1-2");
178 Assert.AreNotSame (a, new XAttribute (a), "#2-1");
179 Assert.AreEqual (a.ToString (), new XAttribute (a).ToString (), "#2-2");
180 Assert.AreEqual ("a=\"\"", a.ToString (), "#2-3");
182 // Execute the primary assertions of this test
183 AssertThrows<FormatException> (() => { bool? z = (bool?) new XAttribute (a); }, "bool?");
184 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XAttribute (a); }, "DateTime?");
185 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XAttribute (a); }, "DateTimeOffset?");
186 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XAttribute (a); }, "decimal?");
187 AssertThrows<FormatException> (() => { double? z = (double?) new XAttribute (a); }, "double?");
188 AssertThrows<FormatException> (() => { float? z = (float?) new XAttribute (a); }, "float?");
189 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XAttribute (a); }, "Guid?");
190 AssertThrows<FormatException> (() => { int? z = (int?) new XAttribute (a); }, "int?");
191 AssertThrows<FormatException> (() => { long? z = (long?) new XAttribute (a); }, "long?");
192 AssertThrows<FormatException> (() => { uint? z = (uint?) new XAttribute (a); }, "uint?");
193 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XAttribute (a); }, "ulong?");
194 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XAttribute (a); }, "TimeSpan?");
195 Assert.AreEqual (String.Empty, (string) new XAttribute (a), "string");
196 AssertThrows<FormatException> (() => { bool z = (bool) new XAttribute (a); }, "bool");
197 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XAttribute (a); }, "DateTime");
198 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XAttribute (a); }, "DateTimeOffset");
199 AssertThrows<FormatException> (() => { decimal z = (decimal) new XAttribute (a); }, "decimal");
200 AssertThrows<FormatException> (() => { double z = (double) new XAttribute (a); }, "double");
201 AssertThrows<FormatException> (() => { float z = (float) new XAttribute (a); }, "float");
202 AssertThrows<FormatException> (() => { Guid z = (Guid) new XAttribute (a); }, "Guid");
203 AssertThrows<FormatException> (() => { int z = (int) new XAttribute (a); }, "int");
204 AssertThrows<FormatException> (() => { long z = (long) new XAttribute (a); }, "long");
205 AssertThrows<FormatException> (() => { uint z = (uint) new XAttribute (a); }, "uint");
206 AssertThrows<FormatException> (() => { ulong z = (ulong) new XAttribute (a); }, "ulong");
207 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XAttribute (a); }, "TimeSpan");
211 public void CastSpaces ()
213 XAttribute a = new XAttribute ("a", " ");
215 // Verify expected "cloning" and "space" behaviour as prerequisites
216 Assert.AreEqual (" ", a.Value, "#1-1");
217 Assert.AreEqual (" ", new XAttribute (a).Value, "#1-2");
218 Assert.AreNotSame (a, new XAttribute (a), "#2-1");
219 Assert.AreEqual (a.ToString (), new XAttribute (a).ToString (), "#2-2");
220 Assert.AreEqual ("a=\" \"", a.ToString (), "#2-3");
221 Assert.AreEqual (a.ToString (), new XAttribute ("a", ' ').ToString (), "#2-4");
223 // Execute the primary assertions of this test
224 AssertThrows<FormatException> (() => { bool? z = (bool?) new XAttribute (a); }, "bool?");
225 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XAttribute (a); }, "DateTime?");
226 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XAttribute (a); }, "DateTimeOffset?");
227 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XAttribute (a); }, "decimal?");
228 AssertThrows<FormatException> (() => { double? z = (double?) new XAttribute (a); }, "double?");
229 AssertThrows<FormatException> (() => { float? z = (float?) new XAttribute (a); }, "float?");
230 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XAttribute (a); }, "Guid?");
231 AssertThrows<FormatException> (() => { int? z = (int?) new XAttribute (a); }, "int?");
232 AssertThrows<FormatException> (() => { long? z = (long?) new XAttribute (a); }, "long?");
233 AssertThrows<FormatException> (() => { uint? z = (uint?) new XAttribute (a); }, "uint?");
234 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XAttribute (a); }, "ulong?");
235 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XAttribute (a); }, "TimeSpan?");
236 Assert.AreEqual (" ", (string) new XAttribute (a), "string");
237 AssertThrows<FormatException> (() => { bool z = (bool) new XAttribute (a); }, "bool");
238 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XAttribute (a); }, "DateTime");
239 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XAttribute (a); }, "DateTimeOffset");
240 AssertThrows<FormatException> (() => { decimal z = (decimal) new XAttribute (a); }, "decimal");
241 AssertThrows<FormatException> (() => { double z = (double) new XAttribute (a); }, "double");
242 AssertThrows<FormatException> (() => { float z = (float) new XAttribute (a); }, "float");
243 AssertThrows<FormatException> (() => { Guid z = (Guid) new XAttribute (a); }, "Guid");
244 AssertThrows<FormatException> (() => { int z = (int) new XAttribute (a); }, "int");
245 AssertThrows<FormatException> (() => { long z = (long) new XAttribute (a); }, "long");
246 AssertThrows<FormatException> (() => { uint z = (uint) new XAttribute (a); }, "uint");
247 AssertThrows<FormatException> (() => { ulong z = (ulong) new XAttribute (a); }, "ulong");
248 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XAttribute (a); }, "TimeSpan");
252 public void CastNumbers ()
254 XAttribute a = new XAttribute ("a", "7");
255 XAttribute b = new XAttribute ("b", " 42 ");
256 XAttribute c = new XAttribute ("c", " \n\r 123 \t ");
257 XAttribute d = new XAttribute ("d", -101);
258 XAttribute o = new XAttribute ("o", "0");
259 XAttribute l = new XAttribute ("l", "1");
260 XAttribute I = new XAttribute ("I", "\t INF ");
261 XAttribute i = new XAttribute ("i", "Infinity");
262 XAttribute M = new XAttribute ("M", "-INF");
263 XAttribute m = new XAttribute ("m", " -Infinity ");
264 XAttribute n = new XAttribute ("n", "NaN");
266 // Verify expected "cloning" and basic conversion behaviour as prerequisites
267 Assert.AreEqual (" \n\r 123 \t ", c.Value, "#1-1");
268 Assert.AreEqual ("-101", new XAttribute (d).Value, "#1-2");
269 Assert.AreNotSame (o, new XAttribute (o), "#2-1");
270 Assert.AreEqual (l.ToString (), new XAttribute (l).ToString (), "#2-2");
271 Assert.AreEqual ("a=\"7\"", a.ToString (), "#2-3a");
272 Assert.AreEqual ("b=\" 42 \"", b.ToString (), "#2-3b");
273 Assert.AreEqual ("c=\" 

 123 	 \"", c.ToString (), "#2-3c");
274 Assert.AreEqual ("d=\"-101\"", d.ToString (), "#2-3d");
275 Assert.AreEqual ("o=\"0\"", new XAttribute ("o", 0.0).ToString (), "#2-3o");
276 Assert.AreEqual ("l=\"1\"", new XAttribute ("l", 1.0f).ToString (), "#2-3l");
277 Assert.AreEqual ("n=\"NaN\"", new XAttribute ("n", double.NaN).ToString (), "#2-3n");
278 Assert.AreEqual (a.ToString (), new XAttribute ("a", '7').ToString (), "#2-4a");
279 Assert.AreEqual (d.ToString (), new XAttribute ("d", "-101").ToString (), "#2-4d");
280 Assert.AreEqual (o.ToString (), new XAttribute ("o", 0L).ToString (), "#2-4o");
281 Assert.AreEqual (l.ToString (), new XAttribute ("l", 1m).ToString (), "#2-4l");
283 // Execute the primary assertions of this test
284 AssertThrows<FormatException> (() => { bool? z = (bool?) new XAttribute (a); }, "a:bool?");
285 AssertThrows<FormatException> (() => { bool? z = (bool?) new XAttribute (b); }, "b:bool?");
286 AssertThrows<FormatException> (() => { bool? z = (bool?) new XAttribute (c); }, "c:bool?");
287 AssertThrows<FormatException> (() => { bool? z = (bool?) new XAttribute (d); }, "d:bool?");
288 Assert.IsNotNull ((bool?) new XAttribute (o), "o:bool?:null");
289 Assert.AreEqual (false, ((bool?) new XAttribute (o)).Value, "o:bool?:value");
290 Assert.IsNotNull ((bool?) new XAttribute (l), "l:bool?:null");
291 Assert.AreEqual (true, ((bool?) new XAttribute (l)).Value, "l:bool?:value");
292 AssertThrows<FormatException> (() => { bool? z = (bool?) new XAttribute (I); }, "I:bool?");
293 AssertThrows<FormatException> (() => { bool? z = (bool?) new XAttribute (i); }, "i:bool?");
294 AssertThrows<FormatException> (() => { bool? z = (bool?) new XAttribute (M); }, "M:bool?");
295 AssertThrows<FormatException> (() => { bool? z = (bool?) new XAttribute (m); }, "m:bool?");
296 AssertThrows<FormatException> (() => { bool? z = (bool?) new XAttribute (n); }, "n:bool?");
297 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XAttribute (a); }, "a:DateTime?");
298 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XAttribute (b); }, "b:DateTime?");
299 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XAttribute (c); }, "c:DateTime?");
300 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XAttribute (d); }, "d:DateTime?");
301 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XAttribute (o); }, "o:DateTime?");
302 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XAttribute (l); }, "l:DateTime?");
303 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XAttribute (I); }, "I:DateTime?");
304 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XAttribute (i); }, "i:DateTime?");
305 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XAttribute (M); }, "M:DateTime?");
306 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XAttribute (m); }, "m:DateTime?");
307 AssertThrows<FormatException> (() => { DateTime? z = (DateTime?) new XAttribute (n); }, "n:DateTime?");
308 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XAttribute (a); }, "a:DateTimeOffset?");
309 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XAttribute (b); }, "b:DateTimeOffset?");
310 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XAttribute (c); }, "c:DateTimeOffset?");
311 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XAttribute (d); }, "d:DateTimeOffset?");
312 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XAttribute (o); }, "o:DateTimeOffset?");
313 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XAttribute (l); }, "l:DateTimeOffset?");
314 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XAttribute (I); }, "I:DateTimeOffset?");
315 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XAttribute (i); }, "i:DateTimeOffset?");
316 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XAttribute (M); }, "M:DateTimeOffset?");
317 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XAttribute (m); }, "m:DateTimeOffset?");
318 AssertThrows<FormatException> (() => { DateTimeOffset? z = (DateTimeOffset?) new XAttribute (n); }, "n:DateTimeOffset?");
319 Assert.IsNotNull ((decimal?) new XAttribute (a), "a:decimal?:null");
320 Assert.AreEqual (7m, ((decimal?) new XAttribute (a)).Value, "a:decimal?:value");
321 Assert.IsNotNull ((decimal?) new XAttribute (b), "b:decimal?:null");
322 Assert.AreEqual (42m, ((decimal?) new XAttribute (b)).Value, "b:decimal?:value");
323 Assert.IsNotNull ((decimal?) new XAttribute (c), "c:decimal?:null");
324 Assert.AreEqual (123m, ((decimal?) new XAttribute (c)).Value, "c:decimal?:value");
325 Assert.IsNotNull ((decimal?) new XAttribute (d), "d:decimal?:null");
326 Assert.AreEqual (-101m, ((decimal?) new XAttribute (d)).Value, "d:decimal?:value");
327 Assert.IsNotNull ((decimal?) new XAttribute (o), "o:decimal?:null");
328 Assert.AreEqual (0m, ((decimal?) new XAttribute (o)).Value, "o:decimal?:value");
329 Assert.IsNotNull ((decimal?) new XAttribute (l), "l:decimal?:null");
330 Assert.AreEqual (1m, ((decimal?) new XAttribute (l)).Value, "l:decimal?:value");
331 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XAttribute (I); }, "I:decimal?");
332 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XAttribute (i); }, "i:decimal?");
333 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XAttribute (M); }, "M:decimal?");
334 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XAttribute (m); }, "m:decimal?");
335 AssertThrows<FormatException> (() => { decimal? z = (decimal?) new XAttribute (n); }, "n:decimal?");
336 Assert.IsNotNull ((double?) new XAttribute (a), "a:double?:null");
337 Assert.AreEqual (7d, ((double?) new XAttribute (a)).Value, "a:double?:value");
338 Assert.IsNotNull ((double?) new XAttribute (b), "b:double?:null");
339 Assert.AreEqual (42d, ((double?) new XAttribute (b)).Value, "b:double?:value");
340 Assert.IsNotNull ((double?) new XAttribute (c), "c:double?:null");
341 Assert.AreEqual (123d, ((double?) new XAttribute (c)).Value, "c:double?:value");
342 Assert.IsNotNull ((double?) new XAttribute (d), "d:double?:null");
343 Assert.AreEqual (-101d, ((double?) new XAttribute (d)).Value, "d:double?:value");
344 Assert.IsNotNull ((double?) new XAttribute (o), "o:double?:null");
345 Assert.AreEqual (0d, ((double?) new XAttribute (o)).Value, "o:double?:value");
346 Assert.IsNotNull ((double?) new XAttribute (l), "l:double?:null");
347 Assert.AreEqual (1d, ((double?) new XAttribute (l)).Value, "l:double?:value");
348 Assert.IsNotNull ((double?) new XAttribute (I), "I:double?:null");
349 Assert.AreEqual (double.PositiveInfinity, ((double?) new XAttribute (I)).Value, "I:double?:value");
350 Assert.IsNotNull ((double?) new XAttribute (i), "i:double?:null");
351 Assert.AreEqual (double.PositiveInfinity, ((double?) new XAttribute (i)).Value, "i:double?:value");
352 Assert.IsNotNull ((double?) new XAttribute (M), "M:double?:null");
353 Assert.AreEqual (double.NegativeInfinity, ((double?) new XAttribute (M)).Value, "M:double?:value");
354 Assert.IsNotNull ((double?) new XAttribute (m), "m:double?:null");
355 Assert.AreEqual (double.NegativeInfinity, ((double?) new XAttribute (m)).Value, "m:double?:value");
356 Assert.IsNotNull ((double?) new XAttribute (n), "n:double?:null");
357 Assert.IsNaN (((double?) new XAttribute (n)).Value, "n:double?:value");
358 Assert.IsNotNull ((float?) new XAttribute (a), "a:float?:null");
359 Assert.AreEqual (7f, ((float?) new XAttribute (a)).Value, "a:float?:value");
360 Assert.IsNotNull ((float?) new XAttribute (b), "b:float?:null");
361 Assert.AreEqual (42f, ((float?) new XAttribute (b)).Value, "b:float?:value");
362 Assert.IsNotNull ((float?) new XAttribute (c), "c:float?:null");
363 Assert.AreEqual (123f, ((float?) new XAttribute (c)).Value, "c:float?:value");
364 Assert.IsNotNull ((float?) new XAttribute (d), "d:float?:null");
365 Assert.AreEqual (-101f, ((float?) new XAttribute (d)).Value, "d:float?:value");
366 Assert.IsNotNull ((float?) new XAttribute (o), "o:float?:null");
367 Assert.AreEqual (0f, ((float?) new XAttribute (o)).Value, "o:float?:value");
368 Assert.IsNotNull ((float?) new XAttribute (l), "l:float?:null");
369 Assert.AreEqual (1f, ((float?) new XAttribute (l)).Value, "l:float?:value");
370 Assert.IsNotNull ((float?) new XAttribute (I), "I:float?:null");
371 Assert.AreEqual (float.PositiveInfinity, ((float?) new XAttribute (I)).Value, "I:float?:value");
372 Assert.IsNotNull ((float?) new XAttribute (i), "i:float?:null");
373 Assert.AreEqual (float.PositiveInfinity, ((float?) new XAttribute (i)).Value, "i:float?:value");
374 Assert.IsNotNull ((float?) new XAttribute (M), "M:float?:null");
375 Assert.AreEqual (float.NegativeInfinity, ((float?) new XAttribute (M)).Value, "M:float?:value");
376 Assert.IsNotNull ((float?) new XAttribute (m), "m:float?:null");
377 Assert.AreEqual (float.NegativeInfinity, ((float?) new XAttribute (m)).Value, "m:float?:value");
378 Assert.IsNotNull ((float?) new XAttribute (n), "n:float?:null");
379 Assert.IsNaN (((float?) new XAttribute (n)).Value, "n:float?:value");
380 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XAttribute (a); }, "a:Guid?");
381 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XAttribute (b); }, "b:Guid?");
382 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XAttribute (c); }, "c:Guid?");
383 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XAttribute (d); }, "d:Guid?");
384 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XAttribute (o); }, "o:Guid?");
385 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XAttribute (l); }, "l:Guid?");
386 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XAttribute (I); }, "I:Guid?");
387 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XAttribute (i); }, "i:Guid?");
388 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XAttribute (M); }, "M:Guid?");
389 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XAttribute (m); }, "m:Guid?");
390 AssertThrows<FormatException> (() => { Guid? z = (Guid?) new XAttribute (n); }, "n:Guid?");
391 Assert.IsNotNull ((int?) new XAttribute (a), "a:int?:null");
392 Assert.AreEqual (7, ((int?) new XAttribute (a)).Value, "a:int?:value");
393 Assert.IsNotNull ((int?) new XAttribute (b), "b:int?:null");
394 Assert.AreEqual (42, ((int?) new XAttribute (b)).Value, "b:int?:value");
395 Assert.IsNotNull ((int?) new XAttribute (c), "c:int?:null");
396 Assert.AreEqual (123, ((int?) new XAttribute (c)).Value, "c:int?:value");
397 Assert.IsNotNull ((int?) new XAttribute (d), "d:int?:null");
398 Assert.AreEqual (-101, ((int?) new XAttribute (d)).Value, "d:int?:value");
399 Assert.IsNotNull ((int?) new XAttribute (o), "o:int?:null");
400 Assert.AreEqual (0, ((int?) new XAttribute (o)).Value, "o:int?:value");
401 Assert.IsNotNull ((int?) new XAttribute (l), "l:int?:null");
402 Assert.AreEqual (1, ((int?) new XAttribute (l)).Value, "l:int?:value");
403 AssertThrows<FormatException> (() => { int? z = (int?) new XAttribute (I); }, "I:int?");
404 AssertThrows<FormatException> (() => { int? z = (int?) new XAttribute (i); }, "i:int?");
405 AssertThrows<FormatException> (() => { int? z = (int?) new XAttribute (M); }, "M:int?");
406 AssertThrows<FormatException> (() => { int? z = (int?) new XAttribute (m); }, "m:int?");
407 AssertThrows<FormatException> (() => { int? z = (int?) new XAttribute (n); }, "n:int?");
408 Assert.IsNotNull ((long?) new XAttribute (a), "a:long?:null");
409 Assert.AreEqual (7L, ((long?) new XAttribute (a)).Value, "a:long?:value");
410 Assert.IsNotNull ((long?) new XAttribute (b), "b:long?:null");
411 Assert.AreEqual (42L, ((long?) new XAttribute (b)).Value, "b:long?:value");
412 Assert.IsNotNull ((long?) new XAttribute (c), "c:long?:null");
413 Assert.AreEqual (123L, ((long?) new XAttribute (c)).Value, "c:long?:value");
414 Assert.IsNotNull ((long?) new XAttribute (d), "d:long?:null");
415 Assert.AreEqual (-101L, ((long?) new XAttribute (d)).Value, "d:long?:value");
416 Assert.IsNotNull ((long?) new XAttribute (o), "o:long?:null");
417 Assert.AreEqual (0L, ((long?) new XAttribute (o)).Value, "o:long?:value");
418 Assert.IsNotNull ((long?) new XAttribute (l), "l:long?:null");
419 Assert.AreEqual (1L, ((long?) new XAttribute (l)).Value, "l:long?:value");
420 AssertThrows<FormatException> (() => { long? z = (long?) new XAttribute (I); }, "I:long?");
421 AssertThrows<FormatException> (() => { long? z = (long?) new XAttribute (i); }, "i:long?");
422 AssertThrows<FormatException> (() => { long? z = (long?) new XAttribute (M); }, "M:long?");
423 AssertThrows<FormatException> (() => { long? z = (long?) new XAttribute (m); }, "m:long?");
424 AssertThrows<FormatException> (() => { long? z = (long?) new XAttribute (n); }, "n:long?");
425 Assert.IsNotNull ((uint?) new XAttribute (a), "a:uint?:null");
426 Assert.AreEqual (7u, ((uint?) new XAttribute (a)).Value, "a:uint?:value");
427 Assert.IsNotNull ((uint?) new XAttribute (b), "b:uint?:null");
428 Assert.AreEqual (42u, ((uint?) new XAttribute (b)).Value, "b:uint?:value");
429 Assert.IsNotNull ((uint?) new XAttribute (c), "c:uint?:null");
430 Assert.AreEqual (123u, ((uint?) new XAttribute (c)).Value, "c:uint?:value");
431 // LAMESPEC: see XmlConvertTests.ToUInt32().
432 //AssertThrows<FormatException> (() => { uint? z = (uint?) new XAttribute (d); }, "d:uint?");
433 Assert.IsNotNull ((uint?) new XAttribute (o), "o:uint?:null");
434 Assert.AreEqual (0u, ((uint?) new XAttribute (o)).Value, "o:uint?:value");
435 Assert.IsNotNull ((uint?) new XAttribute (l), "l:uint?:null");
436 Assert.AreEqual (1u, ((uint?) new XAttribute (l)).Value, "l:uint?:value");
437 AssertThrows<FormatException> (() => { uint? z = (uint?) new XAttribute (I); }, "I:uint?");
438 AssertThrows<FormatException> (() => { uint? z = (uint?) new XAttribute (i); }, "i:uint?");
439 AssertThrows<FormatException> (() => { uint? z = (uint?) new XAttribute (M); }, "M:uint?");
440 AssertThrows<FormatException> (() => { uint? z = (uint?) new XAttribute (m); }, "m:uint?");
441 AssertThrows<FormatException> (() => { uint? z = (uint?) new XAttribute (n); }, "n:uint?");
442 Assert.IsNotNull ((ulong?) new XAttribute (a), "a:ulong?:null");
443 Assert.AreEqual (7UL, ((ulong?) new XAttribute (a)).Value, "a:ulong?:value");
444 Assert.IsNotNull ((ulong?) new XAttribute (b), "b:ulong?:null");
445 Assert.AreEqual (42UL, ((ulong?) new XAttribute (b)).Value, "b:ulong?:value");
446 Assert.IsNotNull ((ulong?) new XAttribute (c), "c:ulong?:null");
447 Assert.AreEqual (123UL, ((ulong?) new XAttribute (c)).Value, "c:ulong?:value");
448 // LAMESPEC: see XmlConvertTests.ToUInt64().
449 //AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XAttribute (d); }, "d:ulong?");
450 Assert.IsNotNull ((ulong?) new XAttribute (o), "o:ulong?:null");
451 Assert.AreEqual (0UL, ((ulong?) new XAttribute (o)).Value, "o:ulong?:value");
452 Assert.IsNotNull ((ulong?) new XAttribute (l), "l:ulong?:null");
453 Assert.AreEqual (1UL, ((ulong?) new XAttribute (l)).Value, "l:ulong?:value");
454 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XAttribute (I); }, "I:ulong?");
455 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XAttribute (i); }, "i:ulong?");
456 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XAttribute (M); }, "M:ulong?");
457 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XAttribute (m); }, "m:ulong?");
458 AssertThrows<FormatException> (() => { ulong? z = (ulong?) new XAttribute (n); }, "n:ulong?");
459 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XAttribute (a); }, "a:TimeSpan?");
460 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XAttribute (b); }, "b:TimeSpan?");
461 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XAttribute (c); }, "c:TimeSpan?");
462 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XAttribute (d); }, "d:TimeSpan?");
463 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XAttribute (o); }, "o:TimeSpan?");
464 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XAttribute (l); }, "l:TimeSpan?");
465 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XAttribute (I); }, "I:TimeSpan?");
466 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XAttribute (i); }, "i:TimeSpan?");
467 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XAttribute (M); }, "M:TimeSpan?");
468 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XAttribute (m); }, "m:TimeSpan?");
469 AssertThrows<FormatException> (() => { TimeSpan? z = (TimeSpan?) new XAttribute (n); }, "n:TimeSpan?");
470 Assert.AreEqual ("7", (string) new XAttribute (a), "a:string");
471 Assert.AreEqual (" 42 ", (string) new XAttribute (b), "b:string");
472 Assert.AreEqual (" \n\r 123 \t ", (string) new XAttribute (c), "c:string");
473 Assert.AreEqual ("-101", (string) new XAttribute (d), "d:string");
474 Assert.AreEqual ("0", (string) new XAttribute (o), "o:string");
475 Assert.AreEqual ("1", (string) new XAttribute (l), "l:string");
476 Assert.AreEqual ("\t INF ", (string) new XAttribute (I), "I:string");
477 Assert.AreEqual ("Infinity", (string) new XAttribute (i), "i:string");
478 Assert.AreEqual ("-INF", (string) new XAttribute (M), "M:string");
479 Assert.AreEqual (" -Infinity ", (string) new XAttribute (m), "m:string");
480 Assert.AreEqual ("NaN", (string) new XAttribute (n), "n:string");
481 AssertThrows<FormatException> (() => { bool z = (bool) new XAttribute (a); }, "a:bool");
482 AssertThrows<FormatException> (() => { bool z = (bool) new XAttribute (b); }, "b:bool");
483 AssertThrows<FormatException> (() => { bool z = (bool) new XAttribute (c); }, "c:bool");
484 AssertThrows<FormatException> (() => { bool z = (bool) new XAttribute (d); }, "d:bool");
485 Assert.AreEqual (false, (bool) new XAttribute (o), "o:bool");
486 Assert.AreEqual (true, (bool) new XAttribute (l), "l:bool");
487 AssertThrows<FormatException> (() => { bool z = (bool) new XAttribute (I); }, "I:bool");
488 AssertThrows<FormatException> (() => { bool z = (bool) new XAttribute (i); }, "i:bool");
489 AssertThrows<FormatException> (() => { bool z = (bool) new XAttribute (M); }, "M:bool");
490 AssertThrows<FormatException> (() => { bool z = (bool) new XAttribute (m); }, "m:bool");
491 AssertThrows<FormatException> (() => { bool z = (bool) new XAttribute (n); }, "n:bool");
492 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XAttribute (a); }, "a:DateTime");
493 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XAttribute (b); }, "b:DateTime");
494 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XAttribute (c); }, "c:DateTime");
495 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XAttribute (d); }, "d:DateTime");
496 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XAttribute (o); }, "o:DateTime");
497 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XAttribute (l); }, "l:DateTime");
498 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XAttribute (I); }, "I:DateTime");
499 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XAttribute (i); }, "i:DateTime");
500 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XAttribute (M); }, "M:DateTime");
501 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XAttribute (m); }, "m:DateTime");
502 AssertThrows<FormatException> (() => { DateTime z = (DateTime) new XAttribute (n); }, "n:DateTime");
503 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XAttribute (a); }, "a:DateTimeOffset");
504 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XAttribute (b); }, "b:DateTimeOffset");
505 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XAttribute (c); }, "c:DateTimeOffset");
506 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XAttribute (d); }, "d:DateTimeOffset");
507 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XAttribute (o); }, "o:DateTimeOffset");
508 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XAttribute (l); }, "l:DateTimeOffset");
509 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XAttribute (I); }, "I:DateTimeOffset");
510 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XAttribute (i); }, "i:DateTimeOffset");
511 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XAttribute (M); }, "M:DateTimeOffset");
512 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XAttribute (m); }, "m:DateTimeOffset");
513 AssertThrows<FormatException> (() => { DateTimeOffset z = (DateTimeOffset) new XAttribute (n); }, "n:DateTimeOffset");
514 Assert.AreEqual (7m, (decimal) new XAttribute (a), "a:decimal");
515 Assert.AreEqual (42m, (decimal) new XAttribute (b), "b:decimal");
516 Assert.AreEqual (123m, (decimal) new XAttribute (c), "c:decimal");
517 Assert.AreEqual (-101m, (decimal) new XAttribute (d), "d:decimal");
518 Assert.AreEqual (0m, (decimal) new XAttribute (o), "o:decimal");
519 Assert.AreEqual (1m, (decimal) new XAttribute (l), "l:decimal");
520 AssertThrows<FormatException> (() => { decimal z = (decimal) new XAttribute (I); }, "I:decimal");
521 AssertThrows<FormatException> (() => { decimal z = (decimal) new XAttribute (i); }, "i:decimal");
522 AssertThrows<FormatException> (() => { decimal z = (decimal) new XAttribute (M); }, "M:decimal");
523 AssertThrows<FormatException> (() => { decimal z = (decimal) new XAttribute (m); }, "m:decimal");
524 AssertThrows<FormatException> (() => { decimal z = (decimal) new XAttribute (n); }, "n:decimal");
525 Assert.AreEqual (7d, (double) new XAttribute (a), "a:double");
526 Assert.AreEqual (42d, (double) new XAttribute (b), "b:double");
527 Assert.AreEqual (123d, (double) new XAttribute (c), "c:double");
528 Assert.AreEqual (-101d, (double) new XAttribute (d), "d:double");
529 Assert.AreEqual (0d, (double) new XAttribute (o), "o:double");
530 Assert.AreEqual (1d, (double) new XAttribute (l), "l:double");
531 Assert.AreEqual (double.PositiveInfinity, (double) new XAttribute (I), "I:double");
532 Assert.AreEqual (double.PositiveInfinity, (double) new XAttribute (i), "i:double");
533 Assert.AreEqual (double.NegativeInfinity, (double) new XAttribute (M), "M:double");
534 Assert.AreEqual (double.NegativeInfinity, (double) new XAttribute (m), "m:double");
535 Assert.IsNaN (((double) new XAttribute (n)), "n:double");
536 Assert.AreEqual (7f, (float) new XAttribute (a), "a:float");
537 Assert.AreEqual (42f, (float) new XAttribute (b), "b:float");
538 Assert.AreEqual (123f, (float) new XAttribute (c), "c:float");
539 Assert.AreEqual (-101f, (float) new XAttribute (d), "d:float");
540 Assert.AreEqual (0f, (float) new XAttribute (o), "o:float");
541 Assert.AreEqual (1f, (float) new XAttribute (l), "l:float");
542 Assert.AreEqual (float.PositiveInfinity, (float) new XAttribute (I), "I:float");
543 Assert.AreEqual (float.PositiveInfinity, (float) new XAttribute (i), "i:float");
544 Assert.AreEqual (float.NegativeInfinity, (float) new XAttribute (M), "M:float");
545 Assert.AreEqual (float.NegativeInfinity, (float) new XAttribute (m), "m:float");
546 Assert.IsNaN (((float) new XAttribute (n)), "n:float");
547 AssertThrows<FormatException> (() => { Guid z = (Guid) new XAttribute (a); }, "a:Guid");
548 AssertThrows<FormatException> (() => { Guid z = (Guid) new XAttribute (b); }, "b:Guid");
549 AssertThrows<FormatException> (() => { Guid z = (Guid) new XAttribute (c); }, "c:Guid");
550 AssertThrows<FormatException> (() => { Guid z = (Guid) new XAttribute (d); }, "d:Guid");
551 AssertThrows<FormatException> (() => { Guid z = (Guid) new XAttribute (o); }, "o:Guid");
552 AssertThrows<FormatException> (() => { Guid z = (Guid) new XAttribute (l); }, "l:Guid");
553 AssertThrows<FormatException> (() => { Guid z = (Guid) new XAttribute (I); }, "I:Guid");
554 AssertThrows<FormatException> (() => { Guid z = (Guid) new XAttribute (i); }, "i:Guid");
555 AssertThrows<FormatException> (() => { Guid z = (Guid) new XAttribute (M); }, "M:Guid");
556 AssertThrows<FormatException> (() => { Guid z = (Guid) new XAttribute (m); }, "m:Guid");
557 AssertThrows<FormatException> (() => { Guid z = (Guid) new XAttribute (n); }, "n:Guid");
558 Assert.AreEqual (7, (int) new XAttribute (a), "a:int");
559 Assert.AreEqual (42, (int) new XAttribute (b), "b:int");
560 Assert.AreEqual (123, (int) new XAttribute (c), "c:int");
561 Assert.AreEqual (-101, (int) new XAttribute (d), "d:int");
562 Assert.AreEqual (0, (int) new XAttribute (o), "o:int");
563 Assert.AreEqual (1, (int) new XAttribute (l), "l:int");
564 AssertThrows<FormatException> (() => { int z = (int) new XAttribute (I); }, "I:int");
565 AssertThrows<FormatException> (() => { int z = (int) new XAttribute (i); }, "i:int");
566 AssertThrows<FormatException> (() => { int z = (int) new XAttribute (M); }, "M:int");
567 AssertThrows<FormatException> (() => { int z = (int) new XAttribute (m); }, "m:int");
568 AssertThrows<FormatException> (() => { int z = (int) new XAttribute (n); }, "n:int");
569 Assert.AreEqual (7L, (long) new XAttribute (a), "a:long");
570 Assert.AreEqual (42L, (long) new XAttribute (b), "b:long");
571 Assert.AreEqual (123L, (long) new XAttribute (c), "c:long");
572 Assert.AreEqual (-101L, (long) new XAttribute (d), "d:long");
573 Assert.AreEqual (0L, (long) new XAttribute (o), "o:long");
574 Assert.AreEqual (1L, (long) new XAttribute (l), "l:long");
575 AssertThrows<FormatException> (() => { long z = (long) new XAttribute (I); }, "I:long");
576 AssertThrows<FormatException> (() => { long z = (long) new XAttribute (i); }, "i:long");
577 AssertThrows<FormatException> (() => { long z = (long) new XAttribute (M); }, "M:long");
578 AssertThrows<FormatException> (() => { long z = (long) new XAttribute (m); }, "m:long");
579 AssertThrows<FormatException> (() => { long z = (long) new XAttribute (n); }, "n:long");
580 Assert.AreEqual (7u, (uint) new XAttribute (a), "a:uint");
581 Assert.AreEqual (42u, (uint) new XAttribute (b), "b:uint");
582 Assert.AreEqual (123u, (uint) new XAttribute (c), "c:uint");
583 // LAMESPEC: see XmlConvertTests.ToUInt32().
584 //AssertThrows<FormatException> (() => { uint z = (uint) new XAttribute (d); }, "d:uint");
585 Assert.AreEqual (0u, (uint) new XAttribute (o), "o:uint");
586 Assert.AreEqual (1u, (uint) new XAttribute (l), "l:uint");
587 AssertThrows<FormatException> (() => { uint z = (uint) new XAttribute (I); }, "I:uint");
588 AssertThrows<FormatException> (() => { uint z = (uint) new XAttribute (i); }, "i:uint");
589 AssertThrows<FormatException> (() => { uint z = (uint) new XAttribute (M); }, "M:uint");
590 AssertThrows<FormatException> (() => { uint z = (uint) new XAttribute (m); }, "m:uint");
591 AssertThrows<FormatException> (() => { uint z = (uint) new XAttribute (n); }, "n:uint");
592 Assert.AreEqual (7UL, (ulong) new XAttribute (a), "a:ulong");
593 Assert.AreEqual (42UL, (ulong) new XAttribute (b), "b:ulong");
594 Assert.AreEqual (123UL, (ulong) new XAttribute (c), "c:ulong");
595 // LAMESPEC: see XmlConvertTests.ToUInt64().
596 //AssertThrows<FormatException> (() => { ulong z = (ulong) new XAttribute (d); }, "d:ulong");
597 Assert.AreEqual (0UL, (ulong) new XAttribute (o), "o:ulong");
598 Assert.AreEqual (1UL, (ulong) new XAttribute (l), "l:ulong");
599 AssertThrows<FormatException> (() => { ulong z = (ulong) new XAttribute (I); }, "I:ulong");
600 AssertThrows<FormatException> (() => { ulong z = (ulong) new XAttribute (i); }, "i:ulong");
601 AssertThrows<FormatException> (() => { ulong z = (ulong) new XAttribute (M); }, "M:ulong");
602 AssertThrows<FormatException> (() => { ulong z = (ulong) new XAttribute (m); }, "m:ulong");
603 AssertThrows<FormatException> (() => { ulong z = (ulong) new XAttribute (n); }, "n:ulong");
604 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XAttribute (a); }, "a:TimeSpan");
605 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XAttribute (b); }, "b:TimeSpan");
606 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XAttribute (c); }, "c:TimeSpan");
607 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XAttribute (d); }, "d:TimeSpan");
608 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XAttribute (o); }, "o:TimeSpan");
609 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XAttribute (l); }, "l:TimeSpan");
610 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XAttribute (I); }, "I:TimeSpan");
611 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XAttribute (i); }, "i:TimeSpan");
612 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XAttribute (M); }, "M:TimeSpan");
613 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XAttribute (m); }, "m:TimeSpan");
614 AssertThrows<FormatException> (() => { TimeSpan z = (TimeSpan) new XAttribute (n); }, "n:TimeSpan");
616 // Perform some round-trip tests with numbers
618 const decimal @decimal = -41051609414188012238960097189m;
619 const double @double = 8.5506609919892972E+307d;
620 const float @float = -1.70151961E+37f;
621 const int @int = -1051251773;
622 const long @long = 4596767133891939716L;
623 const uint @uint = 4106628142u;
624 const ulong @ulong = 10713797297298255927UL;
625 x = new XAttribute ("x", @decimal);
626 Assert.IsNotNull ((decimal?) new XAttribute (x), "x:decimal?:null");
627 Assert.AreEqual (@decimal, ((decimal?) new XAttribute (x)).Value, "x:decimal?:value");
628 Assert.AreEqual (@decimal, (decimal) new XAttribute (x), "x:decimal");
629 x = new XAttribute ("x", @double);
630 Assert.IsNotNull ((double?) new XAttribute (x), "x:double?:null");
631 Assert.AreEqual (@double, ((double?) new XAttribute (x)).Value, "x:double?:value");
632 Assert.AreEqual (@double, (double) new XAttribute (x), "x:double");
633 x = new XAttribute ("x", @float);
634 Assert.IsNotNull ((float?) new XAttribute (x), "x:float?:null");
635 Assert.AreEqual (@float, ((float?) new XAttribute (x)).Value, "x:float?:value");
636 Assert.AreEqual (@float, (float) new XAttribute (x), "x:float");
637 x = new XAttribute ("x", @int);
638 Assert.IsNotNull ((int?) new XAttribute (x), "x:int?:null");
639 Assert.AreEqual (@int, ((int?) new XAttribute (x)).Value, "x:int?:value");
640 Assert.AreEqual (@int, (int) new XAttribute (x), "x:int");
641 x = new XAttribute ("x", @long);
642 Assert.IsNotNull ((long?) new XAttribute (x), "x:long?:null");
643 Assert.AreEqual (@long, ((long?) new XAttribute (x)).Value, "x:long?:value");
644 Assert.AreEqual (@long, (long) new XAttribute (x), "x:long");
645 x = new XAttribute ("x", @uint);
646 Assert.IsNotNull ((uint?) new XAttribute (x), "x:uint?:null");
647 Assert.AreEqual (@uint, ((uint?) new XAttribute (x)).Value, "x:uint?:value");
648 Assert.AreEqual (@uint, (uint) new XAttribute (x), "x:uint");
649 x = new XAttribute ("x", @ulong);
650 Assert.IsNotNull ((ulong?) new XAttribute (x), "x:ulong?:null");
651 Assert.AreEqual (@ulong, ((ulong?) new XAttribute (x)).Value, "x:ulong?:value");
652 Assert.AreEqual (@ulong, (ulong) new XAttribute (x), "x:ulong");
653 x = new XAttribute ("x", double.NaN);
654 Assert.IsNotNull ((double?) new XAttribute (x), "NaN:double?:null");
655 Assert.AreEqual (double.NaN, ((double?) new XAttribute (x)).Value, "NaN:double?:value");
656 Assert.AreEqual (double.NaN, (double) new XAttribute (x), "NaN:double");
657 x = new XAttribute ("x", float.NaN);
658 Assert.IsNotNull ((float?) new XAttribute (x), "NaN:float?:null");
659 Assert.AreEqual (float.NaN, ((float?) new XAttribute (x)).Value, "NaN:float?:value");
660 Assert.AreEqual (float.NaN, (float) new XAttribute (x), "NaN:float");
661 x = new XAttribute ("x", double.PositiveInfinity);
662 Assert.IsNotNull ((double?) new XAttribute (x), "+Inf:double?:null");
663 Assert.AreEqual (double.PositiveInfinity, ((double?) new XAttribute (x)).Value, "+Inf:double?:value");
664 Assert.AreEqual (double.PositiveInfinity, (double) new XAttribute (x), "+Inf:double");
665 x = new XAttribute ("x", float.PositiveInfinity);
666 Assert.IsNotNull ((float?) new XAttribute (x), "+Inf:float?:null");
667 Assert.AreEqual (float.PositiveInfinity, ((float?) new XAttribute (x)).Value, "+Inf:float?:value");
668 Assert.AreEqual (float.PositiveInfinity, (float) new XAttribute (x), "+Inf:float");
669 x = new XAttribute ("x", double.NegativeInfinity);
670 Assert.IsNotNull ((double?) new XAttribute (x), "-Inf:double?:null");
671 Assert.AreEqual (double.NegativeInfinity, ((double?) new XAttribute (x)).Value, "-Inf:double?:value");
672 Assert.AreEqual (double.NegativeInfinity, (double) new XAttribute (x), "-Inf:double");
673 x = new XAttribute ("x", float.NegativeInfinity);
674 Assert.IsNotNull ((float?) new XAttribute (x), "-Inf:float?:null");
675 Assert.AreEqual (float.NegativeInfinity, ((float?) new XAttribute (x)).Value, "-Inf:float?:value");
676 Assert.AreEqual (float.NegativeInfinity, (float) new XAttribute (x), "-Inf:float");
678 // Perform overflow tests with numbers
679 AssertThrows<OverflowException> (() => { decimal z = (decimal) new XAttribute ("z", "91051609414188012238960097189"); }, "z:decimal");
680 AssertThrows<OverflowException> (() => { decimal? z = (decimal?) new XAttribute ("z", "91051609414188012238960097189"); }, "z:decimal?");
681 AssertThrows<OverflowException> (() => { double z = (double) new XAttribute ("z", "8.5506609919892972E+654"); }, "z:double");
682 AssertThrows<OverflowException> (() => { double? z = (double?) new XAttribute ("z", "8.5506609919892972E+654"); }, "z:double?");
683 AssertThrows<OverflowException> (() => { float z = (float) new XAttribute ("z", @double); }, "z:float");
684 AssertThrows<OverflowException> (() => { float? z = (float?) new XAttribute ("z", @double); }, "z:float?");
685 AssertThrows<OverflowException> (() => { int z = (int) new XAttribute ("z", @long); }, "z:int");
686 AssertThrows<OverflowException> (() => { int? z = (int?) new XAttribute ("z", @long); }, "z:int?");
687 AssertThrows<OverflowException> (() => { long z = (long) new XAttribute ("z", @decimal); }, "z:long");
688 AssertThrows<OverflowException> (() => { long? z = (long?) new XAttribute ("z", @decimal); }, "z:long?");
689 AssertThrows<OverflowException> (() => { uint z = (uint) new XAttribute ("z", @ulong); }, "z:uint");
690 AssertThrows<OverflowException> (() => { uint? z = (uint?) new XAttribute ("z", @ulong); }, "z:uint?");
691 AssertThrows<OverflowException> (() => { ulong z = (ulong) new XAttribute ("z", -@decimal); }, "z:ulong");
692 AssertThrows<OverflowException> (() => { ulong? z = (ulong?) new XAttribute ("z", -@decimal); }, "z:ulong?");
696 public void CastExtremes ()
698 // Test extremes/constants where round-trips should work in specific ways
699 Assert.AreEqual (decimal.MaxValue, (decimal) new XAttribute ("k", decimal.MaxValue), "MaxValue:decimal");
700 Assert.AreEqual (decimal.MinValue, (decimal) new XAttribute ("k", decimal.MinValue), "MinValue:decimal");
701 Assert.AreEqual (decimal.MinusOne, (decimal) new XAttribute ("k", decimal.MinusOne), "MinusOne:decimal");
702 Assert.AreEqual (decimal.One, (decimal) new XAttribute ("k", decimal.One), "One:decimal");
703 Assert.AreEqual (decimal.Zero, (decimal) new XAttribute ("k", decimal.Zero), "Zero:decimal");
704 Assert.AreEqual (double.MaxValue, (double) new XAttribute ("k", double.MaxValue), "MaxValue:double");
705 Assert.AreEqual (double.MinValue, (double) new XAttribute ("k", double.MinValue), "MinValue:double");
706 Assert.AreEqual (double.Epsilon, (double) new XAttribute ("k", double.Epsilon), "Epsilon:double");
707 Assert.AreEqual (double.NaN, (double) new XAttribute ("k", double.NaN), "NaN:double");
708 Assert.AreEqual (double.NegativeInfinity, (double) new XAttribute ("k", double.NegativeInfinity), "-Inf:double");
709 Assert.AreEqual (double.PositiveInfinity, (double) new XAttribute ("k", double.PositiveInfinity), "+Inf:double");
710 Assert.AreEqual (float.MaxValue, (float) new XAttribute ("k", float.MaxValue), "MaxValue:float");
711 Assert.AreEqual (float.MinValue, (float) new XAttribute ("k", float.MinValue), "MinValue:float");
712 Assert.AreEqual (float.Epsilon, (float) new XAttribute ("k", float.Epsilon), "Epsilon:float");
713 Assert.AreEqual (float.NaN, (float) new XAttribute ("k", float.NaN), "NaN:float");
714 Assert.AreEqual (float.NegativeInfinity, (float) new XAttribute ("k", float.NegativeInfinity), "-Inf:float");
715 Assert.AreEqual (float.PositiveInfinity, (float) new XAttribute ("k", float.PositiveInfinity), "+Inf:float");
716 Assert.AreEqual (int.MaxValue, (int) new XAttribute ("k", int.MaxValue), "MaxValue:int");
717 Assert.AreEqual (int.MinValue, (int) new XAttribute ("k", int.MinValue), "MinValue:int");
718 Assert.AreEqual (long.MaxValue, (long) new XAttribute ("k", long.MaxValue), "MaxValue:long");
719 Assert.AreEqual (long.MinValue, (long) new XAttribute ("k", long.MinValue), "MinValue:long");
720 Assert.AreEqual (uint.MaxValue, (uint) new XAttribute ("k", uint.MaxValue), "MaxValue:uint");
721 Assert.AreEqual (uint.MinValue, (uint) new XAttribute ("k", uint.MinValue), "MinValue:uint");
722 Assert.AreEqual (ulong.MaxValue, (ulong) new XAttribute ("k", ulong.MaxValue), "MaxValue:ulong");
723 Assert.AreEqual (ulong.MinValue, (ulong) new XAttribute ("k", ulong.MinValue), "MinValue:ulong");
724 Assert.AreEqual (decimal.MaxValue, (decimal?) new XAttribute ("k", decimal.MaxValue), "MaxValue:decimal?");
725 Assert.AreEqual (decimal.MinValue, (decimal?) new XAttribute ("k", decimal.MinValue), "MinValue:decimal?");
726 Assert.AreEqual (decimal.MinusOne, (decimal?) new XAttribute ("k", decimal.MinusOne), "MinusOne:decimal?");
727 Assert.AreEqual (decimal.One, (decimal?) new XAttribute ("k", decimal.One), "One:decimal?");
728 Assert.AreEqual (decimal.Zero, (decimal?) new XAttribute ("k", decimal.Zero), "Zero:decimal?");
729 Assert.AreEqual (double.MaxValue, (double?) new XAttribute ("k", double.MaxValue), "MaxValue:double?");
730 Assert.AreEqual (double.MinValue, (double?) new XAttribute ("k", double.MinValue), "MinValue:double?");
731 Assert.AreEqual (double.Epsilon, (double?) new XAttribute ("k", double.Epsilon), "Epsilon:double?");
732 Assert.AreEqual (double.NaN, (double?) new XAttribute ("k", double.NaN), "NaN:double?");
733 Assert.AreEqual (double.NegativeInfinity, (double?) new XAttribute ("k", double.NegativeInfinity), "-Inf:double?");
734 Assert.AreEqual (double.PositiveInfinity, (double?) new XAttribute ("k", double.PositiveInfinity), "+Inf:double?");
735 Assert.AreEqual (float.MaxValue, (float?) new XAttribute ("k", float.MaxValue), "MaxValue:float?");
736 Assert.AreEqual (float.MinValue, (float?) new XAttribute ("k", float.MinValue), "MinValue:float?");
737 Assert.AreEqual (float.Epsilon, (float?) new XAttribute ("k", float.Epsilon), "Epsilon:float?");
738 Assert.AreEqual (float.NaN, (float?) new XAttribute ("k", float.NaN), "NaN:float?");
739 Assert.AreEqual (float.NegativeInfinity, (float?) new XAttribute ("k", float.NegativeInfinity), "-Inf:float?");
740 Assert.AreEqual (float.PositiveInfinity, (float?) new XAttribute ("k", float.PositiveInfinity), "+Inf:float?");
741 Assert.AreEqual (int.MaxValue, (int?) new XAttribute ("k", int.MaxValue), "MaxValue:int?");
742 Assert.AreEqual (int.MinValue, (int?) new XAttribute ("k", int.MinValue), "MinValue:int?");
743 Assert.AreEqual (long.MaxValue, (long?) new XAttribute ("k", long.MaxValue), "MaxValue:long?");
744 Assert.AreEqual (long.MinValue, (long?) new XAttribute ("k", long.MinValue), "MinValue:long?");
745 Assert.AreEqual (uint.MaxValue, (uint?) new XAttribute ("k", uint.MaxValue), "MaxValue:uint?");
746 Assert.AreEqual (uint.MinValue, (uint?) new XAttribute ("k", uint.MinValue), "MinValue:uint?");
747 Assert.AreEqual (ulong.MaxValue, (ulong?) new XAttribute ("k", ulong.MaxValue), "MaxValue:ulong?");
748 Assert.AreEqual (ulong.MinValue, (ulong?) new XAttribute ("k", ulong.MinValue), "MinValue:ulong?");
749 Assert.AreEqual (DateTime.MaxValue, (DateTime) new XAttribute ("k", DateTime.MaxValue), "MaxValue:DateTime");
750 Assert.AreEqual (DateTime.MinValue, (DateTime) new XAttribute ("k", DateTime.MinValue), "MinValue:DateTime");
751 Assert.AreEqual (DateTime.MaxValue, (DateTime?) new XAttribute ("k", DateTime.MaxValue), "MaxValue:DateTime?");
752 Assert.AreEqual (DateTime.MinValue, (DateTime?) new XAttribute ("k", DateTime.MinValue), "MinValue:DateTime?");
753 Assert.AreEqual (DateTimeOffset.MaxValue, (DateTimeOffset) new XAttribute ("k", DateTimeOffset.MaxValue), "MaxValue:DateTimeOffset");
754 Assert.AreEqual (DateTimeOffset.MinValue, (DateTimeOffset) new XAttribute ("k", DateTimeOffset.MinValue), "MinValue:DateTimeOffset");
755 Assert.AreEqual (DateTimeOffset.MaxValue, (DateTimeOffset?) new XAttribute ("k", DateTimeOffset.MaxValue), "MaxValue:DateTimeOffset?");
756 Assert.AreEqual (DateTimeOffset.MinValue, (DateTimeOffset?) new XAttribute ("k", DateTimeOffset.MinValue), "MinValue:DateTimeOffset?");
757 Assert.AreEqual (TimeSpan.MaxValue, (TimeSpan) new XAttribute ("k", TimeSpan.MaxValue), "MaxValue:TimeSpan");
758 Assert.AreEqual (TimeSpan.MinValue, (TimeSpan) new XAttribute ("k", TimeSpan.MinValue), "MinValue:TimeSpan");
759 Assert.AreEqual (TimeSpan.MaxValue, (TimeSpan?) new XAttribute ("k", TimeSpan.MaxValue), "MaxValue:TimeSpan?");
760 Assert.AreEqual (TimeSpan.MinValue, (TimeSpan?) new XAttribute ("k", TimeSpan.MinValue), "MinValue:TimeSpan?");
764 public void CastBooleans ()
766 Assert.IsNotNull ((bool?) new XAttribute ("fq", "false"), "#1a");
767 Assert.AreEqual (false, ((bool?) new XAttribute ("fq", "false")).Value, "#1b");
768 Assert.IsNotNull ((bool?) new XAttribute ("tq", "true"), "#2a");
769 Assert.AreEqual (true, ((bool?) new XAttribute ("tq", "true")).Value, "#2b");
770 Assert.IsNotNull ((bool?) new XAttribute ("Fq", "False"), "#3a");
771 Assert.AreEqual (false, ((bool?) new XAttribute ("Fq", "False")).Value, "#3b");
772 Assert.IsNotNull ((bool?) new XAttribute ("Tq", "True"), "#4a");
773 Assert.AreEqual (true, ((bool?) new XAttribute ("Tq", "True")).Value, "#4b");
774 Assert.IsNotNull ((bool?) new XAttribute ("Fs", " False \t \r "), "#5a");
775 Assert.AreEqual (false, ((bool?) new XAttribute ("Fs", " False \t \r ")).Value, "#5b");
776 Assert.IsNotNull ((bool?) new XAttribute ("Ts", " \t True \n "), "#6a");
777 Assert.AreEqual (true, ((bool?) new XAttribute ("Ts", " \t True \n ")).Value, "#6b");
778 Assert.AreEqual (false, (bool) new XAttribute ("f", "false"), "#7");
779 Assert.AreEqual (true, (bool) new XAttribute ("t", "true"), "#8");
780 Assert.AreEqual (false, (bool) new XAttribute ("F", "False"), "#9");
781 Assert.AreEqual (true, (bool) new XAttribute ("T", "True"), "#10");
782 Assert.AreEqual (false, (bool)new XAttribute ("fs", " false "), "#11");
783 Assert.AreEqual (true, (bool)new XAttribute ("ts", " true "), "#12");
784 Assert.IsNotNull ((bool?) new XAttribute ("x", true), "#13a");
785 Assert.IsTrue (((bool?) new XAttribute ("x", true)).Value, "#13b");
786 Assert.IsTrue ((bool) new XAttribute ("x", true), "#13c");
787 Assert.IsNotNull ((bool?) new XAttribute ("x", false), "#14a");
788 Assert.IsFalse (((bool?) new XAttribute ("x", false)).Value, "#14b");
789 Assert.IsFalse ((bool) new XAttribute ("x", false), "#14c");
790 Assert.IsTrue ((bool) new XAttribute ("x", bool.TrueString), "#15a");
791 Assert.IsFalse ((bool) new XAttribute ("x", bool.FalseString), "#15b");
795 public void CastGuids ()
797 Guid rb = new Guid (new byte[16] { 0x9A, 0xBF, 0xCE, 0x7E, 0x07, 0x29, 0x9C, 0x43, 0x80, 0x7D, 0x48, 0x20, 0xB9, 0x19, 0xEA, 0x57 });
798 Guid rd = new Guid (new byte[16] { 0x21, 0x5B, 0x57, 0x26, 0xCD, 0x14, 0x5E, 0x44, 0x8F, 0xFA, 0xE2, 0xBC, 0x24, 0x7B, 0x2E, 0xC9 });
799 Guid rn = new Guid (new byte[16] { 0xF9, 0x46, 0x41, 0xA8, 0xA5, 0x03, 0xF1, 0x4A, 0xAD, 0x97, 0x7B, 0xC7, 0x79, 0x57, 0x2B, 0x79 });
800 Guid rp = new Guid (new byte[16] { 0x51, 0x6B, 0x8A, 0x17, 0xEF, 0x11, 0xFB, 0x48, 0x83, 0xBD, 0x57, 0xB4, 0x99, 0xF9, 0xC1, 0xE6 });
801 Guid rz = Guid.Empty;
802 Guid rx = Guid.NewGuid ();
804 XAttribute b = new XAttribute ("b", "{7ECEBF9A-2907-439c-807D-4820B919EA57}");
805 XAttribute d = new XAttribute ("d", " 26575b21-14cd-445e-8ffa-e2bc247b2ec9");
806 XAttribute n = new XAttribute ("n", " a84146f903A54af1ad977bC779572b79 \t ");
807 XAttribute p = new XAttribute ("p", " \t \n (178a6b51-11ef-48fb-83bd-57b499f9c1e6) ");
808 XAttribute z = new XAttribute ("z", "00000000-0000-0000-0000-000000000000\r\n");
809 XAttribute x = new XAttribute ("x", rx);
811 Assert.IsNotNull ((Guid?) new XAttribute (b), "#1a");
812 Assert.AreEqual (rb, ((Guid?) new XAttribute (b)).Value, "#1b");
813 Assert.AreEqual (rb, (Guid) new XAttribute (b), "#1c");
814 Assert.AreEqual (rb, (Guid) new XAttribute ("r", rb), "#1d");
815 Assert.IsNotNull ((Guid?) new XAttribute ("r", rb), "#1e");
816 Assert.AreEqual (rb, ((Guid?) new XAttribute ("r", rb)).Value, "#1f");
818 Assert.IsNotNull ((Guid?) new XAttribute (d), "#2a");
819 Assert.AreEqual (rd, ((Guid?) new XAttribute (d)).Value, "#2b");
820 Assert.AreEqual (rd, (Guid) new XAttribute (d), "#2c");
821 Assert.AreEqual (rd, (Guid) new XAttribute ("r", rd), "#2d");
822 Assert.IsNotNull ((Guid?) new XAttribute ("r", rd), "#2e");
823 Assert.AreEqual (rd, ((Guid?) new XAttribute ("r", rd)).Value, "#2f");
825 Assert.IsNotNull ((Guid?) new XAttribute (n), "#3a");
826 Assert.AreEqual (rn, ((Guid?) new XAttribute (n)).Value, "#3b");
827 Assert.AreEqual (rn, (Guid) new XAttribute (n), "#3c");
828 Assert.AreEqual (rn, (Guid) new XAttribute ("r", rn), "#3d");
829 Assert.IsNotNull ((Guid?) new XAttribute ("r", rn), "#3e");
830 Assert.AreEqual (rn, ((Guid?) new XAttribute ("r", rn)).Value, "#3f");
832 Assert.IsNotNull ((Guid?) new XAttribute (p), "#4a");
833 Assert.AreEqual (rp, ((Guid?) new XAttribute (p)).Value, "#4b");
834 Assert.AreEqual (rp, (Guid) new XAttribute (p), "#4c");
835 Assert.AreEqual (rp, (Guid) new XAttribute ("r", rp), "#4d");
836 Assert.IsNotNull ((Guid?) new XAttribute ("r", rp), "#4e");
837 Assert.AreEqual (rp, ((Guid?) new XAttribute ("r", rp)).Value, "#4f");
839 Assert.IsNotNull ((Guid?) new XAttribute (z), "#5a");
840 Assert.AreEqual (rz, ((Guid?) new XAttribute (z)).Value, "#5b");
841 Assert.AreEqual (rz, (Guid) new XAttribute (z), "#5c");
843 Assert.IsNotNull ((Guid?) new XAttribute (x), "#6a");
844 Assert.AreEqual (rx, ((Guid?) new XAttribute (x)).Value, "#6b");
845 Assert.AreEqual (rx, (Guid) new XAttribute (x), "#6c");
849 public void CastDateTimes ()
851 const long weirdTicks = 8070L; // Examples of problematic fractions of seconds on Mono 2.6.1: xxx.8796082, xxx.7332000, xxx.0050300, xxx.5678437, xxx.0008070, xxx.0769000 and xxx.2978530 (about 1 in 8 of all possible fractions seem to fail)
852 DateTime ra = new DateTime (1987, 1, 23, 21, 45, 36, 89, DateTimeKind.Unspecified);
853 DateTime rb = new DateTime (2001, 2, 3, 4, 5, 6, 789, DateTimeKind.Local);
854 DateTime rc = new DateTime (2010, 1, 2, 0, 0, 0, 0, DateTimeKind.Utc) + TimeSpan.FromTicks (weirdTicks);
855 DateTime rd = new DateTime (1956, 11, 2, 0, 34, 0);
856 DateTime re = new DateTime (DateTime.Today.Ticks + 619917654321L);
857 DateTime rx = DateTime.Now;
858 DateTime rz = DateTime.UtcNow;
860 XAttribute a = new XAttribute ("a", "1987-01-23T21:45:36.089");
861 XAttribute b = new XAttribute ("b", " \n 2001-02-03T04:05:06.789" + rb.ToString ("zzz") + " \r ");
862 XAttribute c = new XAttribute ("c", "2010-01-02T00:00:00." + weirdTicks.ToString ("d7").TrimEnd ('0') + "Z");
863 XAttribute d = new XAttribute ("d", " Nov 2, 1956 12:34 AM \r\n \t");
864 XAttribute e = new XAttribute ("e", " \t 17:13:11.7654321 ");
865 XAttribute x = new XAttribute ("x", rx);
866 XAttribute z = new XAttribute ("z", rz);
868 Assert.IsNotNull ((DateTime?) new XAttribute (a), "#1a");
869 Assert.AreEqual (ra, ((DateTime?) new XAttribute (a)).Value, "#1b");
870 Assert.AreEqual (ra, (DateTime) new XAttribute (a), "#1c");
871 Assert.AreEqual (ra, (DateTime) new XAttribute ("r", ra), "#1d");
872 Assert.IsNotNull ((DateTime?) new XAttribute ("r", ra), "#1e");
873 Assert.AreEqual (ra, ((DateTime?) new XAttribute ("r", ra)).Value, "#1f");
875 Assert.IsNotNull ((DateTime?) new XAttribute (b), "#2a");
876 Assert.AreEqual (rb, ((DateTime?) new XAttribute (b)).Value, "#2b");
877 Assert.AreEqual (rb, (DateTime) new XAttribute (b), "#2c");
878 Assert.AreEqual (rb, (DateTime) new XAttribute ("r", rb), "#2d");
879 Assert.IsNotNull ((DateTime?) new XAttribute ("r", rb), "#2e");
880 Assert.AreEqual (rb, ((DateTime?) new XAttribute ("r", rb)).Value, "#2f");
882 Assert.AreEqual (10000000L, TimeSpan.TicksPerSecond, "#3a"); // Should be same on all platforms (sanity check for next logical step)
883 Assert.AreEqual (weirdTicks, rc.Ticks % TimeSpan.TicksPerSecond, "#3b"); // Prerequisite: No ticks lost in raw data
884 Assert.IsNotNull ((DateTime?) new XAttribute (c), "#3c");
885 Assert.AreEqual (weirdTicks, ((DateTime?) new XAttribute (c)).Value.Ticks % TimeSpan.TicksPerSecond, "#3d"); // Did casting lose any ticks belonging to fractional seconds?
886 Assert.AreEqual (rc, ((DateTime?) new XAttribute (c)).Value, "#3e");
887 Assert.AreEqual (weirdTicks, ((DateTime) new XAttribute (c)).Ticks % TimeSpan.TicksPerSecond, "#3f");
888 Assert.AreEqual (rc, (DateTime) new XAttribute (c), "#3g");
889 Assert.AreEqual (rc, (DateTime) new XAttribute ("r", rc), "#3h");
890 Assert.IsNotNull ((DateTime?) new XAttribute ("r", rc), "#3i");
891 Assert.AreEqual (rc, ((DateTime?) new XAttribute ("r", rc)).Value, "#3j");
893 Assert.IsNotNull ((DateTime?) new XAttribute (d), "#4a");
894 Assert.AreEqual (rd, ((DateTime?) new XAttribute (d)).Value, "#4b");
895 Assert.AreEqual (rd, (DateTime) new XAttribute (d), "#4c");
896 Assert.AreEqual (rd, (DateTime) new XAttribute ("r", rd), "#4d");
897 Assert.IsNotNull ((DateTime?) new XAttribute ("r", rd), "#4e");
898 Assert.AreEqual (rd, ((DateTime?) new XAttribute ("r", rd)).Value, "#4f");
900 Assert.IsNotNull ((DateTime?) new XAttribute (e), "#5a");
901 Assert.AreEqual (re, ((DateTime?) new XAttribute (e)).Value, "#5b");
902 Assert.AreEqual (re, (DateTime) new XAttribute (e), "#5c");
903 Assert.AreEqual (re, (DateTime) new XAttribute ("r", re), "#5d");
904 Assert.IsNotNull ((DateTime?) new XAttribute ("r", re), "#5e");
905 Assert.AreEqual (re, ((DateTime?) new XAttribute ("r", re)).Value, "#5f");
907 Assert.IsNotNull ((DateTime?) new XAttribute (x), "#6a");
908 Assert.AreEqual (rx, ((DateTime?) new XAttribute (x)).Value, "#6b");
909 Assert.AreEqual (rx, (DateTime) new XAttribute (x), "#6c");
911 Assert.IsNotNull ((DateTime?) new XAttribute (z), "#7a");
912 Assert.AreEqual (rz, ((DateTime?) new XAttribute (z)).Value, "#7b");
913 Assert.AreEqual (rz, (DateTime) new XAttribute (z), "#7c");
917 public void CastDateTimeOffsets ()
919 DateTimeOffset ra = new DateTimeOffset (1987, 1, 23, 21, 45, 36, 89, TimeSpan.FromHours (+13.75)); // e.g., Chatham Islands (daylight-savings time)
920 DateTimeOffset rb = new DateTimeOffset (2001, 2, 3, 4, 5, 6, 789, DateTimeOffset.Now.Offset); // Local time
921 DateTimeOffset rc = new DateTimeOffset (2010, 1, 2, 0, 0, 0, 0, TimeSpan.Zero); // UTC, and midnight
922 DateTimeOffset rd = new DateTimeOffset (1956, 11, 2, 12, 34, 10, TimeSpan.FromHours (-3.5));
923 DateTimeOffset re = new DateTimeOffset (630646468231147764, TimeSpan.FromHours (2)); // UTC+2, and with full resolution
924 DateTimeOffset rf = new DateTimeOffset (643392740967552000, TimeSpan.Zero); // UTC, and with a potentially problematic fractional second that might lose a tick on Mono 2.6.1
925 DateTimeOffset rx = DateTimeOffset.Now;
926 DateTimeOffset rz = DateTimeOffset.UtcNow;
928 XAttribute a = new XAttribute ("a", "1987-01-23T21:45:36.089+13:45");
929 XAttribute b = new XAttribute ("b", "2001-02-03T04:05:06.789" + DateTimeOffset.Now.ToString ("zzz"));
930 XAttribute c = new XAttribute ("c", " 2010-01-02T00:00:00Z \t");
931 XAttribute d = new XAttribute ("d", " Nov 2, 1956 12:34:10 PM -3:30 \r\n \t");
932 XAttribute e = new XAttribute ("e", "1999-06-10T21:27:03.1147764+02:00");
933 XAttribute f = new XAttribute ("f", "2039-10-31T12:34:56.7552+00:00");
934 XAttribute x = new XAttribute ("x", rx);
935 XAttribute z = new XAttribute ("z", rz);
937 Assert.IsNotNull ((DateTimeOffset?) new XAttribute (a), "#1a");
938 Assert.AreEqual (ra, ((DateTimeOffset?) new XAttribute (a)).Value, "#1b");
939 Assert.AreEqual (ra, (DateTimeOffset) new XAttribute (a), "#1c");
940 Assert.AreEqual (ra, (DateTimeOffset) new XAttribute ("r", ra), "#1d");
941 Assert.IsNotNull ((DateTimeOffset?) new XAttribute ("r", ra), "#1e");
942 Assert.AreEqual (ra, ((DateTimeOffset?) new XAttribute ("r", ra)).Value, "#1f");
944 Assert.IsNotNull ((DateTimeOffset?) new XAttribute (b), "#2a");
945 Assert.AreEqual (rb, ((DateTimeOffset?) new XAttribute (b)).Value, "#2b");
946 Assert.AreEqual (rb, (DateTimeOffset) new XAttribute (b), "#2c");
947 Assert.AreEqual (rb, (DateTimeOffset) new XAttribute ("r", rb), "#2d");
948 Assert.IsNotNull ((DateTimeOffset?) new XAttribute ("r", rb), "#2e");
949 Assert.AreEqual (rb, ((DateTimeOffset?) new XAttribute ("r", rb)).Value, "#2f");
951 Assert.IsNotNull ((DateTimeOffset?) new XAttribute (c), "#3a");
952 Assert.AreEqual (rc, ((DateTimeOffset?) new XAttribute (c)).Value, "#3b");
953 Assert.AreEqual (rc, (DateTimeOffset) new XAttribute (c), "#3c");
954 Assert.AreEqual (rc, (DateTimeOffset) new XAttribute ("r", rc), "#3d");
955 Assert.IsNotNull ((DateTimeOffset?) new XAttribute ("r", rc), "#3e");
956 Assert.AreEqual (rc, ((DateTimeOffset?) new XAttribute ("r", rc)).Value, "#3f");
958 AssertThrows<FormatException> (() => { DateTimeOffset? r = (DateTimeOffset?) new XAttribute (d); }, "#4a");
959 AssertThrows<FormatException> (() => { DateTimeOffset r = (DateTimeOffset) new XAttribute (d); }, "#4b");
960 Assert.AreEqual (rd, DateTimeOffset.Parse (d.Value), "#4c"); // Sanity check: Okay for standalone DateTimeOffset but not as XML as in above
962 Assert.IsNotNull ((DateTimeOffset?) new XAttribute (e), "#5a");
963 Assert.AreEqual (re, ((DateTimeOffset?) new XAttribute (e)).Value, "#5b");
964 Assert.AreEqual (re, (DateTimeOffset) new XAttribute (e), "#5c");
965 Assert.AreEqual (re, (DateTimeOffset) new XAttribute ("r", re), "#5d");
966 Assert.IsNotNull ((DateTimeOffset?) new XAttribute ("r", re), "#5e");
967 Assert.AreEqual (re, ((DateTimeOffset?) new XAttribute ("r", re)).Value, "#5f");
969 Assert.IsNotNull ((DateTimeOffset?) new XAttribute (f), "#6a");
970 Assert.AreEqual (rf, ((DateTimeOffset?) new XAttribute (f)).Value, "#6b");
971 Assert.AreEqual (rf, (DateTimeOffset) new XAttribute (f), "#6c");
972 Assert.AreEqual (rf, (DateTimeOffset) new XAttribute ("r", rf), "#6d");
973 Assert.IsNotNull ((DateTimeOffset?) new XAttribute ("r", rf), "#6e");
974 Assert.AreEqual (rf, ((DateTimeOffset?) new XAttribute ("r", rf)).Value, "#6f");
976 Assert.IsNotNull ((DateTimeOffset?) new XAttribute (x), "#7a");
977 Assert.AreEqual (rx, ((DateTimeOffset?) new XAttribute (x)).Value, "#7b");
978 Assert.AreEqual (rx, (DateTimeOffset) new XAttribute (x), "#7c");
980 Assert.IsNotNull ((DateTimeOffset?) new XAttribute (z), "#8a");
981 Assert.AreEqual (rz, ((DateTimeOffset?) new XAttribute (z)).Value, "#8b");
982 Assert.AreEqual (rz, (DateTimeOffset) new XAttribute (z), "#8c");
986 public void CastTimeSpans ()
988 TimeSpan ra = new TimeSpan (23, 21, 45, 36, 89);
989 TimeSpan rb = -new TimeSpan (3, 4, 5, 6, 789);
990 TimeSpan rc = new TimeSpan (2, 0, 0, 0, 0);
991 TimeSpan rd = new TimeSpan (26798453L); // in ticks, using full resolution and longer than a second
992 TimeSpan re = new TimeSpan (2710L); // in ticks, a sub-millisecond interval
993 TimeSpan rx = new TimeSpan (0, 3, 8, 29, 734);
994 TimeSpan rz = TimeSpan.Zero;
996 XAttribute a = new XAttribute ("a", "P23DT21H45M36.089S");
997 XAttribute b = new XAttribute ("b", "-P3DT4H5M6.789S");
998 XAttribute c = new XAttribute ("c", " \r\n \tP2D ");
999 XAttribute d = new XAttribute ("d", "PT2.6798453S");
1000 XAttribute e = new XAttribute ("e", "PT0.000271S");
1001 XAttribute x = new XAttribute ("x", rx);
1002 XAttribute z = new XAttribute ("z", rz);
1004 Assert.IsNotNull ((TimeSpan?) new XAttribute (a), "#1a");
1005 Assert.AreEqual (ra, ((TimeSpan?) new XAttribute (a)).Value, "#1b");
1006 Assert.AreEqual (ra, (TimeSpan) new XAttribute (a), "#1c");
1007 Assert.AreEqual (ra, (TimeSpan) new XAttribute ("r", ra), "#1d");
1008 Assert.IsNotNull ((TimeSpan?) new XAttribute ("r", ra), "#1e");
1009 Assert.AreEqual (ra, ((TimeSpan?) new XAttribute ("r", ra)).Value, "#1f");
1011 Assert.IsNotNull ((TimeSpan?) new XAttribute (b), "#2a");
1012 Assert.AreEqual (rb, ((TimeSpan?) new XAttribute (b)).Value, "#2b");
1013 Assert.AreEqual (rb, (TimeSpan) new XAttribute (b), "#2c");
1014 Assert.AreEqual (rb, (TimeSpan) new XAttribute ("r", rb), "#2d");
1015 Assert.IsNotNull ((TimeSpan?) new XAttribute ("r", rb), "#2e");
1016 Assert.AreEqual (rb, ((TimeSpan?) new XAttribute ("r", rb)).Value, "#2f");
1018 Assert.IsNotNull ((TimeSpan?) new XAttribute (c), "#3a");
1019 Assert.AreEqual (rc, ((TimeSpan?) new XAttribute (c)).Value, "#3b");
1020 Assert.AreEqual (rc, (TimeSpan) new XAttribute (c), "#3c");
1021 Assert.AreEqual (rc, (TimeSpan) new XAttribute ("r", rc), "#3d");
1022 Assert.IsNotNull ((TimeSpan?) new XAttribute ("r", rc), "#3e");
1023 Assert.AreEqual (rc, ((TimeSpan?) new XAttribute ("r", rc)).Value, "#3f");
1025 Assert.IsNotNull ((TimeSpan?) new XAttribute (d), "#4a");
1026 Assert.AreEqual (rd, ((TimeSpan?) new XAttribute (d)).Value, "#4b");
1027 Assert.AreEqual (rd, (TimeSpan) new XAttribute (d), "#4c");
1028 Assert.AreEqual (rd, (TimeSpan) new XAttribute ("r", rd), "#4d");
1029 Assert.IsNotNull ((TimeSpan?) new XAttribute ("r", rd), "#4e");
1030 Assert.AreEqual (rd, ((TimeSpan?) new XAttribute ("r", rd)).Value, "#4f");
1032 Assert.IsNotNull ((TimeSpan?) new XAttribute (e), "#5a");
1033 Assert.AreEqual (re, ((TimeSpan?) new XAttribute (e)).Value, "#5b");
1034 Assert.AreEqual (re, (TimeSpan) new XAttribute (e), "#5c");
1035 Assert.AreEqual (re, (TimeSpan) new XAttribute ("r", re), "#5d");
1036 Assert.IsNotNull ((TimeSpan?) new XAttribute ("r", re), "#5e");
1037 Assert.AreEqual (re, ((TimeSpan?) new XAttribute ("r", re)).Value, "#5f");
1039 Assert.IsNotNull ((TimeSpan?) new XAttribute (x), "#6a");
1040 Assert.AreEqual (rx, ((TimeSpan?) new XAttribute (x)).Value, "#6b");
1041 Assert.AreEqual (rx, (TimeSpan) new XAttribute (x), "#6c");
1043 Assert.IsNotNull ((TimeSpan?) new XAttribute (z), "#7a");
1044 Assert.AreEqual (rz, ((TimeSpan?) new XAttribute (z)).Value, "#7b");
1045 Assert.AreEqual (rz, (TimeSpan) new XAttribute (z), "#7c");
1047 #pragma warning restore 219