XmlDocumentFragmentTests.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // System.Xml.XmlDocumentFragment.cs
  3. //
  4. // Author: Atsushi Enomoto ([email protected])
  5. //
  6. // (C) 2002 Atsushi Enomoto
  7. //
  8. using System;
  9. using System.Xml;
  10. using NUnit.Framework;
  11. namespace MonoTests.System.Xml
  12. {
  13. public class XmlDocumentFragmentTests : TestCase
  14. {
  15. XmlDocument document;
  16. XmlDocumentFragment fragment;
  17. public XmlDocumentFragmentTests (string name)
  18. : base (name)
  19. {
  20. }
  21. protected override void SetUp ()
  22. {
  23. }
  24. public void TestConstructor ()
  25. {
  26. XmlDocument d = new XmlDocument ();
  27. XmlDocumentFragment df = d.CreateDocumentFragment ();
  28. AssertEquals ("#Constructor.NodeName", "#document-fragment", df.Name);
  29. AssertEquals ("#Constructor.NodeType", XmlNodeType.DocumentFragment, df.NodeType);
  30. }
  31. public void TestAppendChildToFragment ()
  32. {
  33. document = new XmlDocument ();
  34. fragment = document.CreateDocumentFragment ();
  35. document.LoadXml ("<html><head></head><body></body></html>");
  36. XmlElement el = document.CreateElement ("p");
  37. el.InnerXml = "Test Paragraph";
  38. // appending element to fragment
  39. fragment.AppendChild (el);
  40. AssertNotNull ("#AppendChildToFragment.Element", fragment.FirstChild);
  41. AssertNotNull ("#AppendChildToFragment.Element.Children", fragment.FirstChild.FirstChild);
  42. AssertEquals ("#AppendChildToFragment.Element.Child.Text", "Test Paragraph", fragment.FirstChild.FirstChild.Value);
  43. }
  44. public void TestAppendFragmentToElement ()
  45. {
  46. document = new XmlDocument ();
  47. fragment = document.CreateDocumentFragment ();
  48. document.LoadXml ("<html><head></head><body></body></html>");
  49. XmlElement body = document.DocumentElement.LastChild as XmlElement;
  50. fragment.AppendChild (document.CreateElement ("p"));
  51. fragment.AppendChild (document.CreateElement ("div"));
  52. // appending fragment to element
  53. body.AppendChild (fragment);
  54. AssertNotNull ("#AppendFragmentToElement.Exist", body.FirstChild);
  55. AssertEquals ("#AppendFragmentToElement.ChildIsElement", XmlNodeType.Element, body.FirstChild.NodeType);
  56. AssertEquals ("#AppendFragmentToElement.FirstChild", "p", body.FirstChild.Name);
  57. AssertEquals ("#AppendFragmentToElement.LastChild", "div", body.LastChild.Name);
  58. }
  59. public void TestGetInnerXml ()
  60. {
  61. // this will be also tests of TestWriteTo()/TestWriteContentTo()
  62. document = new XmlDocument ();
  63. fragment = document.CreateDocumentFragment ();
  64. fragment.AppendChild (document.CreateElement ("foo"));
  65. fragment.AppendChild (document.CreateElement ("bar"));
  66. fragment.AppendChild (document.CreateElement ("baz"));
  67. AssertEquals ("#Simple", "<foo /><bar /><baz />", fragment.InnerXml);
  68. }
  69. public void TestSetInnerXml ()
  70. {
  71. document = new XmlDocument ();
  72. fragment = document.CreateDocumentFragment ();
  73. fragment.InnerXml = "<foo /><bar><child /></bar><baz />";
  74. AssertEquals ("foo", fragment.FirstChild.Name);
  75. AssertEquals ("bar", fragment.FirstChild.NextSibling.Name);
  76. AssertEquals ("child", fragment.FirstChild.NextSibling.FirstChild.Name);
  77. AssertEquals ("baz", fragment.LastChild.Name);
  78. }
  79. }
  80. }