2 // MonoTests.System.Xml.XPathAtomicValueTests.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections;
35 using System.Xml.Schema;
36 using System.Xml.XPath;
38 using NUnit.Framework;
40 namespace MonoTests.System.Xml
43 public class XPathAtomicValueTests : Assertion
45 internal const string XdtNamespace = "http://www.w3.org/2003/11/xpath-datatypes";
47 static XmlTypeCode [] AllTypeCode =
48 new ArrayList (Enum.GetValues (typeof (XmlTypeCode))).ToArray (typeof (XmlTypeCode)) as XmlTypeCode [];
50 static XmlQualifiedName [] allTypeNames;
52 static XmlSchemaType [] allTypes;
54 static string [] xstypes = new string [] {
103 static string [] xdttypes = {
110 private static XmlQualifiedName [] AllTypeNames {
112 if (allTypeNames == null) {
113 ArrayList al = new ArrayList ();
114 foreach (string name in xstypes)
115 AddXsType (name, XmlSchema.Namespace, al);
116 foreach (string name in xdttypes)
117 AddXsType (name, XdtNamespace, al);
118 allTypeNames = al.ToArray (typeof (XmlQualifiedName)) as XmlQualifiedName [];
124 private static void AddXsType (string name, string ns, ArrayList al)
126 al.Add (new XmlQualifiedName (name, ns));
129 private static XmlSchemaType [] AllTypes {
131 if (allTypes == null) {
132 ArrayList al = new ArrayList ();
133 foreach (XmlQualifiedName name in AllTypeNames) {
134 XmlSchemaType t = XmlSchemaType.GetBuiltInSimpleType (name);
136 t = XmlSchemaType.GetBuiltInComplexType (name);
139 allTypes = al.ToArray (typeof (XmlSchemaType)) as XmlSchemaType [];
145 public void AssertAtomicValue (XPathAtomicValue av,
148 XmlSchemaType xmlType,
161 AssertEquals ("IsNode", isNode, av.IsNode);
162 AssertEquals ("ValueType", valueType, av.ValueType);
163 AssertEquals ("XmlType", xmlType, av.XmlType);
164 AssertEquals ("TypedValue", typedValue, av.TypedValue);
165 AssertEquals ("typedValue.GetType()", typedValueType, typedValue.GetType ());
168 AssertEquals ("Value", value, av.Value);
172 Fail ("not supported conversion to String.");
173 } catch (InvalidCastException) {
177 // FIXME: Failure cases could not be tested;
178 // any kind of Exceptions are thrown as yet.
179 if (boolValue != null)
180 AssertEquals ("ValueAsBoolean", boolValue, av.ValueAsBoolean);
184 boolValue = av.ValueAsBoolean;
185 Fail ("not supported conversion to Boolean.");
186 } catch (InvalidCastException) {
190 if (dateValue != null)
191 AssertEquals ("ValueAsDateTime", dateValue, av.ValueAsDateTime);
195 dateValue = av.ValueAsDateTime;
196 Fail ("not supported conversion to DateTime.");
197 } catch (InvalidCastException) {
201 if (decimalValue != null)
202 AssertEquals ("ValueAsDecimal", decimalValue, av.ValueAsDecimal);
206 decimalValue = av.ValueAsDecimal;
207 Fail ("not supported conversion to Decimal.");
208 } catch (InvalidCastException) {
212 if (doubleValue != null)
213 AssertEquals ("ValueAsDouble", doubleValue, av.ValueAsDouble);
217 doubleValue = av.ValueAsDouble;
218 Fail ("not supported conversion to Double.");
219 } catch (InvalidCastException) {
223 if (int32Value != null)
224 AssertEquals ("ValueAsInt32", int32Value, av.ValueAsInt32);
228 int32Value = av.ValueAsInt32;
229 Fail ("not supported conversion to Int32.");
230 } catch (InvalidCastException) {
234 if (int64Value != null)
235 AssertEquals ("ValueAsInt64", int64Value, av.ValueAsInt64);
239 int64Value = av.ValueAsInt64;
240 Fail ("not supported conversion to Int64.");
241 } catch (InvalidCastException) {
245 if (singleValue != null)
246 AssertEquals ("ValueAsSingle", singleValue, av.ValueAsSingle);
250 singleValue = av.ValueAsSingle;
251 Fail ("not supported conversion to Single.");
252 } catch (InvalidCastException) {
256 AssertEquals ("ValueAsList.Count", listCount, av.ValueAsList.Count);
260 public void BooleanType ()
262 XmlSchemaType xstype = XmlSchemaType.GetBuiltInSimpleType (XmlTypeCode.Boolean);
267 av = new XPathAtomicValue (true, xstype);
268 AssertAtomicValue (av,
270 typeof (bool), // ValueType
273 typeof (bool), // actual Type of TypedValue
277 (decimal) 1, // decimal
281 (float) 1.0, // single
285 av = new XPathAtomicValue (false, xstype);
286 AssertAtomicValue (av,
288 typeof (bool), // ValueType
291 typeof (bool), // actual Type of TypedValue
295 (decimal) 0, // decimal
299 (float) 0.0, // single
303 av = new XPathAtomicValue (false, xstype);
304 AssertAtomicValue (av,
306 typeof (bool), // ValueType
309 typeof (bool), // actual Type of TypedValue
313 (decimal) 0, // decimal
317 (float) 0.0, // single
321 av = new XPathAtomicValue (5, xstype);
322 AssertAtomicValue (av,
324 typeof (bool), // ValueType
327 typeof (bool), // actual Type of TypedValue
331 (decimal) 5, // decimal
335 (float) 5.0, // single
339 short shortValue = 3;
340 av = new XPathAtomicValue (shortValue, xstype);
341 AssertAtomicValue (av,
343 typeof (bool), // ValueType
346 typeof (bool), // actual Type of TypedValue
350 (decimal) 3, // decimal
354 (float) 3.0, // single
358 av = new XPathAtomicValue ("1", xstype);
359 AssertAtomicValue (av,
361 typeof (bool), // ValueType
364 typeof (bool), // actual Type of TypedValue
368 (decimal) 1, // decimal
372 (float) 1.0, // single
375 // new bool [] {true}
376 av = new XPathAtomicValue (new bool [] {true}, xstype);
377 AssertAtomicValue (av,
379 typeof (bool), // ValueType
382 typeof (bool), // actual Type of TypedValue
386 (decimal) 1, // decimal
390 (float) 1.0, // single
393 // new ArrayList (new int [] {6})
394 av = new XPathAtomicValue (new ArrayList (new int [] {6}), xstype);
395 AssertAtomicValue (av,
397 typeof (bool), // ValueType
400 typeof (bool), // actual Type of TypedValue
404 (decimal) 6, // decimal
408 (float) 6.0, // single
411 // Hashtable, [7] = 7
412 Hashtable ht = new Hashtable ();
414 av = new XPathAtomicValue (ht, xstype);
415 AssertAtomicValue (av,
417 typeof (bool), // ValueType
420 typeof (bool), // actual Type of TypedValue
424 (decimal) 7, // decimal
428 (float) 7.0, // single
431 // - MS.NET will fail here due to its bug -
433 // another XPathAtomicValue that is bool
434 av = new XPathAtomicValue (true, xstype);
435 av = new XPathAtomicValue (av, xstype);
436 AssertAtomicValue (av,
438 typeof (bool), // ValueType
441 typeof (bool), // actual Type of TypedValue
445 (decimal) 1, // decimal
449 (float) 1.0, // single
452 // Array, [0] = XPathAtomicValue
453 av = new XPathAtomicValue (new XPathAtomicValue [] {av}, xstype);
454 AssertAtomicValue (av,
456 typeof (bool), // ValueType
459 typeof (bool), // actual Type of TypedValue
463 (decimal) 1, // decimal
467 (float) 1.0, // single
470 // new bool [] {true, false}
471 av = new XPathAtomicValue (new bool [] {true, false}, xstype);
473 object o = av.ValueAsBoolean;
474 Fail ("ArrayList must contain just one item to be castable to bool");
475 } catch (InvalidCastException) {
479 av = new XPathAtomicValue (new ArrayList (), xstype);
481 object o = av.ValueAsBoolean;
482 Fail ("ArrayList must contain just one item to be castable to bool");
483 } catch (InvalidCastException) {
487 av = new XPathAtomicValue ("True", xstype);
489 object o = av.ValueAsBoolean;
490 Fail ("\"True\" is not a boolean representation (\"true\" is).");
491 } catch (InvalidCastException) {
495 av = new XPathAtomicValue (DateTime.Now, xstype);
497 object o = av.ValueAsBoolean;
498 Fail ("DateTime should not be castable to bool.");
499 } catch (InvalidCastException) {
502 // XmlText node that contains boolean representation value
503 XmlDocument doc = new XmlDocument ();
504 doc.LoadXml ("<root>true</root>");
505 XmlNode node = doc.DocumentElement.FirstChild;
506 av = new XPathAtomicValue (node, xstype);
508 object o = av.ValueAsBoolean;
509 Fail ("XmlText cannot be castable to bool.");
510 } catch (InvalidCastException) {
513 // XPathNavigator whose node points to text node whose
514 // value represents boolean string.
515 av = new XPathAtomicValue (node.CreateNavigator (),
518 object o = av.ValueAsBoolean;
519 Fail ("XmlText cannot be castable to bool.");
520 } catch (InvalidCastException) {