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