2 // System.Xml.XmlNodeTests
5 // Kral Ferch <kral_ferch@hotmail.com>
15 using NUnit.Framework;
17 namespace MonoTests.System.Xml
19 public class XmlNodeTests : TestCase
21 public XmlNodeTests () : base ("MonoTests.System.Xml.XmlNodeTests testsuite") {}
22 public XmlNodeTests (string name) : base (name) {}
32 protected override void SetUp ()
34 document = new XmlDocument ();
35 document.NodeInserted += new XmlNodeChangedEventHandler (this.EventNodeInserted);
36 document.NodeInserting += new XmlNodeChangedEventHandler (this.EventNodeInserting);
37 document.NodeRemoved += new XmlNodeChangedEventHandler (this.EventNodeRemoved);
38 document.NodeRemoving += new XmlNodeChangedEventHandler (this.EventNodeRemoving);
39 element = document.CreateElement ("foo");
40 element2 = document.CreateElement ("bar");
43 private void EventNodeInserted(Object sender, XmlNodeChangedEventArgs e)
48 private void EventNodeInserting (Object sender, XmlNodeChangedEventArgs e)
53 private void EventNodeRemoved(Object sender, XmlNodeChangedEventArgs e)
58 private void EventNodeRemoving (Object sender, XmlNodeChangedEventArgs e)
63 public void TestAppendChild ()
69 element.AppendChild (element2);
73 // Can only append to elements, documents, and attributes
76 comment = document.CreateComment ("baz");
77 comment.AppendChild (element2);
78 Fail ("Expected an InvalidOperationException to be thrown.");
80 catch (InvalidOperationException) {}
82 // Can't append a node from one document into another document.
83 XmlDocument document2 = new XmlDocument();
84 AssertEquals (1, element.ChildNodes.Count);
87 element2 = document2.CreateElement ("qux");
88 element.AppendChild (element2);
89 Fail ("Expected an ArgumentException to be thrown.");
91 catch (ArgumentException) {}
92 AssertEquals (1, element.ChildNodes.Count);
94 // Can't append to a readonly node.
95 /* TODO put this in when I figure out how to create a read-only node.
98 XmlElement element3 = (XmlElement)element.CloneNode (false);
99 Assert (!element.IsReadOnly);
100 Assert (element3.IsReadOnly);
101 element2 = document.CreateElement ("quux");
102 element3.AppendChild (element2);
103 Fail ("Expected an ArgumentException to be thrown.");
105 catch (ArgumentException) {}
109 public void TestInsertBefore()
111 document = new XmlDocument();
112 document.LoadXml("<root><sub /></root>");
113 XmlElement docelem = document.DocumentElement;
114 docelem.InsertBefore(document.CreateElement("good_child"), docelem.FirstChild);
115 AssertEquals("InsertBefore.Normal", "good_child", docelem.FirstChild.Name);
117 document.InsertBefore(document.CreateElement("BAD_MAN"), docelem);
118 Fail("#InsertBefore.BadPositionButNoError.1");
120 catch(XmlException) {}
123 public void TestInsertAfter()
125 document = new XmlDocument();
126 document.LoadXml("<root><sub1 /><sub2 /></root>");
127 XmlElement docelem = document.DocumentElement;
128 XmlElement newelem = document.CreateElement("good_child");
129 docelem.InsertAfter(newelem, docelem.FirstChild);
130 AssertEquals("InsertAfter.Normal", 3, docelem.ChildNodes.Count);
131 AssertEquals("InsertAfter.First", "sub1", docelem.FirstChild.Name);
132 AssertEquals("InsertAfter.Last", "sub2", docelem.LastChild.Name);
133 AssertEquals("InsertAfter.Prev", "good_child", docelem.FirstChild.NextSibling.Name);
134 AssertEquals("InsertAfter.Next", "good_child", docelem.LastChild.PreviousSibling.Name);
137 document.InsertAfter(document.CreateElement("BAD_MAN"), docelem);
138 Fail("#InsertAfter.BadPositionButNoError.1");
140 catch(XmlException) {}
143 public void TestPrependChild()
145 document = new XmlDocument();
146 document.LoadXml("<root><sub1 /><sub2 /></root>");
147 XmlElement docelem = document.DocumentElement;
148 docelem.PrependChild(document.CreateElement("prepender"));
149 AssertEquals("PrependChild", "prepender", docelem.FirstChild.Name);
152 public void saveTestRemoveAll ()
154 // TODO: put this test back in when AttributeCollection.RemoveAll() is implemented.
155 element.AppendChild(element2);
158 element.RemoveAll ();
163 public void TestRemoveChild ()
165 element.AppendChild(element2);
168 element.RemoveChild (element2);