XmlAttributesTests.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // System.Xml.XmlAttributesTests
  3. //
  4. // Author:
  5. // Atsushi Enomoto
  6. //
  7. // (C) 2003 Atsushi Enomoto
  8. //
  9. using System;
  10. using System.IO;
  11. using System.Text;
  12. using System.Reflection;
  13. using System.Xml;
  14. using System.Xml.Schema;
  15. using System.Xml.Serialization;
  16. using NUnit.Framework;
  17. namespace MonoTests.System.XmlSerialization
  18. {
  19. [TestFixture]
  20. public class XmlAttributesTests
  21. {
  22. StringWriter sw;
  23. XmlTextWriter xtw;
  24. XmlSerializer xs;
  25. private void SetUpWriter ()
  26. {
  27. sw = new StringWriter ();
  28. xtw = new XmlTextWriter (sw);
  29. xtw.QuoteChar = '\'';
  30. xtw.Formatting = Formatting.None;
  31. }
  32. private string WriterText
  33. {
  34. get
  35. {
  36. string val = sw.GetStringBuilder ().ToString();
  37. int offset = val.IndexOf ('>') + 1;
  38. val = val.Substring (offset);
  39. return val;
  40. }
  41. }
  42. private void Serialize (object o, XmlAttributeOverrides ao)
  43. {
  44. SetUpWriter ();
  45. xs = new XmlSerializer (o.GetType (), ao);
  46. xs.Serialize (xtw, o);
  47. }
  48. private void Serialize (object o, XmlRootAttribute root)
  49. {
  50. SetUpWriter ();
  51. xs = new XmlSerializer (o.GetType(), root);
  52. xs.Serialize (xtw, o);
  53. }
  54. // Testcases.
  55. [Test]
  56. [Category ("MobileNotWorking")]
  57. public void NewXmlAttributes ()
  58. {
  59. // seems not different from Type specified ctor().
  60. XmlAttributes atts = new XmlAttributes ();
  61. Assert.IsNull (atts.XmlAnyAttribute, "#1");
  62. Assert.IsNotNull (atts.XmlAnyElements, "#2");
  63. Assert.AreEqual (0, atts.XmlAnyElements.Count, "#3");
  64. Assert.IsNull (atts.XmlArray, "#4");
  65. Assert.IsNotNull (atts.XmlArrayItems, "#5");
  66. Assert.AreEqual (0, atts.XmlArrayItems.Count, "#6");
  67. Assert.IsNull (atts.XmlAttribute, "#7");
  68. Assert.IsNull (atts.XmlChoiceIdentifier, "#8");
  69. Assert.IsNull (atts.XmlDefaultValue, "#9");
  70. Assert.IsNotNull (atts.XmlElements, "#11");
  71. Assert.AreEqual (0, atts.XmlElements.Count, "#12");
  72. Assert.IsNull (atts.XmlEnum, "#13");
  73. Assert.IsNotNull (atts.XmlIgnore, "#14");
  74. Assert.AreEqual (TypeCode.Boolean, atts.XmlIgnore.GetTypeCode (), "#15");
  75. Assert.AreEqual (false, atts.Xmlns, "#16");
  76. Assert.IsNull (atts.XmlRoot, "#17");
  77. Assert.IsNull (atts.XmlText, "#18");
  78. Assert.IsNull (atts.XmlType, "#19");
  79. }
  80. [Test]
  81. public void XmlTextAttribute ()
  82. {
  83. // based on default ctor.
  84. XmlTextAttribute attr = new XmlTextAttribute ();
  85. Assert.AreEqual ("", attr.DataType, "#1");
  86. Assert.IsNull (attr.Type, "#2");
  87. // based on a type.
  88. XmlTextAttribute attr2 = new XmlTextAttribute (typeof (XmlNode));
  89. Assert.AreEqual ("", attr.DataType, "#3");
  90. Assert.IsNull (attr.Type, "#4");
  91. }
  92. [Test]
  93. public void XmlInvalidElementAttribute ()
  94. {
  95. XmlAttributeOverrides ao = new XmlAttributeOverrides ();
  96. XmlAttributes atts = new XmlAttributes ();
  97. atts.XmlElements.Add (new XmlElementAttribute ("xInt"));
  98. ao.Add (typeof (int), atts);
  99. try {
  100. Serialize (10, ao);
  101. Assert.Fail ("Should be invalid.");
  102. } catch (InvalidOperationException) {
  103. }
  104. }
  105. [Test]
  106. public void XmlIgnore ()
  107. {
  108. FieldInfo field = GetType ().GetField ("XmlIgnoreField");
  109. XmlAttributes atts = new XmlAttributes (field);
  110. Assert.AreEqual (true, atts.XmlIgnore, "#1");
  111. Assert.AreEqual (0, atts.XmlElements.Count, "#2");
  112. Assert.AreEqual (0, atts.XmlAnyElements.Count, "#3");
  113. }
  114. [XmlIgnore]
  115. [XmlElement (IsNullable = true)]
  116. [XmlAnyElement]
  117. public int XmlIgnoreField;
  118. }
  119. }