2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / class / System.XML / Test / System.Xml / XmlDocumentFragmentTests.cs
1 //
2 // System.Xml.XmlDocumentFragment.cs
3 //
4 // Author: Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
5 // Author: Martin Willemoes Hansen (mwh@sysrq.dk)
6 //
7 // (C) 2002 Atsushi Enomoto
8 // (C) 2003 Martin Willemoes Hansen
9 //
10
11 using System;
12 using System.Xml;
13
14 using NUnit.Framework;
15
16 namespace MonoTests.System.Xml
17 {
18         [TestFixture]
19         public class XmlDocumentFragmentTests : Assertion
20         {
21                 XmlDocument document;
22                 XmlDocumentFragment fragment;
23
24                 [Test]
25                 public void Constructor ()
26                 {
27                         XmlDocument d = new XmlDocument ();
28                         XmlDocumentFragment df = d.CreateDocumentFragment ();
29                         AssertEquals ("#Constructor.NodeName", "#document-fragment", df.Name);
30                         AssertEquals ("#Constructor.NodeType", XmlNodeType.DocumentFragment, df.NodeType);
31                 }
32
33                 [Test]
34                 public void AppendChildToFragment ()
35                 {
36                         document = new XmlDocument ();
37                         fragment = document.CreateDocumentFragment ();
38                         document.LoadXml ("<html><head></head><body></body></html>");
39                         XmlElement el = document.CreateElement ("p");
40                         el.InnerXml = "Test Paragraph";
41
42                         // appending element to fragment
43                         fragment.AppendChild (el);
44                         AssertNotNull ("#AppendChildToFragment.Element", fragment.FirstChild);
45                         AssertNotNull ("#AppendChildToFragment.Element.Children", fragment.FirstChild.FirstChild);
46                         AssertEquals ("#AppendChildToFragment.Element.Child.Text", "Test Paragraph", fragment.FirstChild.FirstChild.Value);
47                 }
48
49                 [Test]
50                 public void AppendFragmentToElement ()
51                 {
52                         document = new XmlDocument ();
53                         fragment = document.CreateDocumentFragment ();
54                         document.LoadXml ("<html><head></head><body></body></html>");
55                         XmlElement body = document.DocumentElement.LastChild as XmlElement;
56                         fragment.AppendChild (document.CreateElement ("p"));
57                         fragment.AppendChild (document.CreateElement ("div"));
58
59                         // appending fragment to element
60                         body.AppendChild (fragment);
61                         AssertNotNull ("#AppendFragmentToElement.Exist", body.FirstChild);
62                         AssertEquals ("#AppendFragmentToElement.ChildIsElement", XmlNodeType.Element, body.FirstChild.NodeType);
63                         AssertEquals ("#AppendFragmentToElement.FirstChild", "p", body.FirstChild.Name);
64                         AssertEquals ("#AppendFragmentToElement.LastChild", "div", body.LastChild.Name);
65                 }
66
67                 [Test]
68                 public void GetInnerXml ()
69                 {
70                         // this will be also tests of TestWriteTo()/TestWriteContentTo()
71
72                         document = new XmlDocument ();
73                         fragment = document.CreateDocumentFragment ();
74                         fragment.AppendChild (document.CreateElement ("foo"));
75                         fragment.AppendChild (document.CreateElement ("bar"));
76                         fragment.AppendChild (document.CreateElement ("baz"));
77                         AssertEquals ("#Simple", "<foo /><bar /><baz />", fragment.InnerXml);
78                 }
79
80                 [Test]
81                 public void SetInnerXml ()
82                 {
83                         document = new XmlDocument ();
84                         fragment = document.CreateDocumentFragment ();
85                         fragment.InnerXml = "<foo /><bar><child /></bar><baz />";
86                         AssertEquals ("foo", fragment.FirstChild.Name);
87                         AssertEquals ("bar", fragment.FirstChild.NextSibling.Name);
88                         AssertEquals ("child", fragment.FirstChild.NextSibling.FirstChild.Name);
89                         AssertEquals ("baz", fragment.LastChild.Name);
90                 }
91
92                 [Test]
93                 public void InnerText ()
94                 {
95                         document = new XmlDocument ();
96                         fragment = document.CreateDocumentFragment ();
97                         string text = "<foo /><bar><child /></bar><baz />";
98                         fragment.InnerText = text;
99                         AssertEquals (text, fragment.InnerText);
100                 }
101         }
102 }