From: <bo...@us...> - 2010-05-07 05:48:09
|
Revision: 382 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=382&view=rev Author: bodewig Date: 2010-05-07 05:48:03 +0000 (Fri, 07 May 2010) Log Message: ----------- port ElementSelectors to .NET Added Paths: ----------- trunk/xmlunit/src/main/net-core/diff/ElementSelectors.cs trunk/xmlunit/src/main/net-core/util/Nodes.cs trunk/xmlunit/src/tests/net-core/diff/ trunk/xmlunit/src/tests/net-core/diff/ElementSelectorsTest.cs trunk/xmlunit/src/tests/net-core/util/ trunk/xmlunit/src/tests/net-core/util/NodesTest.cs Added: trunk/xmlunit/src/main/net-core/diff/ElementSelectors.cs =================================================================== --- trunk/xmlunit/src/main/net-core/diff/ElementSelectors.cs (rev 0) +++ trunk/xmlunit/src/main/net-core/diff/ElementSelectors.cs 2010-05-07 05:48:03 UTC (rev 382) @@ -0,0 +1,262 @@ +/* + This file is licensed to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +using System; +using System.Collections.Generic; +using System.Xml; +using net.sf.xmlunit.util; + +namespace net.sf.xmlunit.diff { + + /// <summary> + /// Common ElementSelector implementations. + /// </summary> + public sealed class ElementSelectors { + private ElementSelectors() { } + + /// <summary> + /// Always returns true, i.e. each element can be compared to each + /// other element. + /// </summary> + /// <remarks> + /// Generally this means elements will be compared in document + /// order. + /// </remarks> + public static bool Default(XmlElement controlElement, + XmlElement testElement) { + return true; + } + + /// <summary> + /// Elements with the same local name (and namespace URI - if any) + /// can be compared. + /// </summary> + public static bool ByName(XmlElement controlElement, + XmlElement testElement) { + return controlElement != null && testElement != null + && BothNullOrEqual(Nodes.GetQName(controlElement), + Nodes.GetQName(testElement)); + } + + /// <summary> + /// Elements with the same local name (and namespace URI - if any) + /// and nested text (if any) can be compared. + /// </summary> + public static bool ByNameAndText(XmlElement controlElement, + XmlElement testElement) { + return ByName(controlElement, testElement) + && BothNullOrEqual(Nodes.GetMergedNestedText(controlElement), + Nodes.GetMergedNestedText(testElement)); + } + + /// <summary> + /// Elements with the same local name (and namespace URI - if any) + /// and attribute values for the given attribute names can be + /// compared. + /// </summary> + /// <remarks>Attributes are only searched for in the null + /// namespace.</remarks> + public static ElementSelector + ByNameAndAttributes(params string[] attribs) { + if (attribs == null) { + throw new ArgumentNullException("attribs"); + } + XmlQualifiedName[] qs = new XmlQualifiedName[attribs.Length]; + for (int i = 0; i < attribs.Length; i++) { + qs[i] = new XmlQualifiedName(attribs[i]); + } + return ByNameAndAttributes(qs); + } + + /// <summary> + /// Elements with the same local name (and namespace URI - if any) + /// and attribute values for the given attribute names can be + /// compared. + /// </summary> + public static ElementSelector + ByNameAndAttributes(params XmlQualifiedName[] attribs) { + if (attribs == null) { + throw new ArgumentNullException("attribs"); + } + XmlQualifiedName[] qs = new XmlQualifiedName[attribs.Length]; + Array.Copy(attribs, 0, qs, 0, qs.Length); + return delegate(XmlElement controlElement, XmlElement testElement) { + if (!ByName(controlElement, testElement)) { + return false; + } + return MapsEqualForKeys(Nodes.GetAttributes(controlElement), + Nodes.GetAttributes(testElement), + qs); + }; + } + + /// <summary> + /// Elements with the same local name (and namespace URI - if any) + /// and attribute values for the given attribute names can be + /// compared. + /// </summary> + /// <remarks> + /// Namespace URIs of attributes are those of the attributes on + /// the control element or the null namespace if the don't + /// exist. + /// </remarks> + public static ElementSelector + ByNameAndAttributesControlNS(params string[] attribs) { + if (attribs == null) { + throw new ArgumentNullException("attribs"); + } + List<string> ats = new List<string>(attribs); + return delegate(XmlElement controlElement, XmlElement testElement) { + if (!ByName(controlElement, testElement)) { + return false; + } + IDictionary<XmlQualifiedName, string> cAttrs = + Nodes.GetAttributes(controlElement); + IDictionary<string, XmlQualifiedName> qNameByLocalName = + new Dictionary<string, XmlQualifiedName>(); + foreach (XmlQualifiedName q in cAttrs.Keys) { + string local = q.Name; + if (ats.Contains(local)) { + qNameByLocalName[local] = q; + } + } + foreach (string a in ats) { + if (!qNameByLocalName.ContainsKey(a)) { + qNameByLocalName[a] = new XmlQualifiedName(a); + } + } + return MapsEqualForKeys(cAttrs, + Nodes.GetAttributes(testElement), + qNameByLocalName.Values); + }; + } + + /// <summary> + /// Elements with the same local name (and namespace URI - if any) + /// and attribute values for all attributes can be compared. + /// </summary> + public static bool ByNameAndAllAttributes(XmlElement controlElement, + XmlElement testElement) { + if (!ByName(controlElement, testElement)) { + return false; + } + IDictionary<XmlQualifiedName, string> cAttrs = + Nodes.GetAttributes(controlElement); + IDictionary<XmlQualifiedName, string> tAttrs = + Nodes.GetAttributes(testElement); + if (cAttrs.Count != tAttrs.Count) { + return false; + } + return MapsEqualForKeys(cAttrs, tAttrs, cAttrs.Keys); + } + + /// <summary> + /// Elements with the same local name (and namespace URI - if any) + /// and child elements and nested text at each level (if any) can + /// be compared. + /// </summary> + public static bool ByNameAndTextRec(XmlElement controlElement, + XmlElement testElement) { + if (!ByNameAndText(controlElement, testElement)) { + return false; + } + + XmlNodeList controlChildren = controlElement.ChildNodes; + XmlNodeList testChildren = testElement.ChildNodes; + int controlLen = controlChildren.Count; + int testLen = testChildren.Count; + int controlIndex, testIndex; + for (controlIndex = testIndex = 0; + controlIndex < controlLen && testIndex < testLen; + ) { + // find next non-text child nodes + XmlNode c = controlChildren[controlIndex]; + while (IsText(c) && ++controlIndex < controlLen) { + c = controlChildren[controlIndex]; + } + if (IsText(c)) { + break; + } + XmlNode t = testChildren[testIndex]; + while (IsText(t) && ++testIndex < testLen) { + t = testChildren[testIndex]; + } + if (IsText(t)) { + break; + } + + // different types of children make elements + // non-comparable + if (c.NodeType != t.NodeType) { + return false; + } + // recurse for child elements + if (c is XmlElement + && !ByNameAndTextRec(c as XmlElement, t as XmlElement)) { + return false; + } + + controlIndex++; + testIndex++; + } + + // child lists exhausted? + if (controlIndex < controlLen) { + XmlNode n = controlChildren[controlIndex]; + while (IsText(n) && ++controlIndex < controlLen) { + n = controlChildren[controlIndex]; + } + // some non-Text children remained + if (controlIndex < controlLen) { + return false; + } + } + if (testIndex < testLen) { + XmlNode n = testChildren[testIndex]; + while (IsText(n) && ++testIndex < testLen) { + n = testChildren[testIndex]; + } + // some non-Text children remained + if (testIndex < testLen) { + return false; + } + } + return true; + } + + private static bool BothNullOrEqual(object o1, object o2) { + return o1 == null ? o2 == null : o1.Equals(o2); + } + + private static bool + MapsEqualForKeys(IDictionary<XmlQualifiedName, string> control, + IDictionary<XmlQualifiedName, string> test, + IEnumerable<XmlQualifiedName> keys) { + foreach (XmlQualifiedName q in keys) { + string c, t; + if (control.TryGetValue(q, out c) != test.TryGetValue(q, out t)) { + return false; + } + if (!BothNullOrEqual(c, t)) { + return false; + } + } + return true; + } + + private static bool IsText(XmlNode n) { + return n is XmlText || n is XmlCDataSection; + } + } +} Property changes on: trunk/xmlunit/src/main/net-core/diff/ElementSelectors.cs ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/xmlunit/src/main/net-core/util/Nodes.cs =================================================================== --- trunk/xmlunit/src/main/net-core/util/Nodes.cs (rev 0) +++ trunk/xmlunit/src/main/net-core/util/Nodes.cs 2010-05-07 05:48:03 UTC (rev 382) @@ -0,0 +1,68 @@ +/* + This file is licensed to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +using System.Collections.Generic; +using System.Text; +using System.Xml; + +namespace net.sf.xmlunit.util { + /// <summary> + /// Utility algorithms that work on DOM nodes. + /// </summary> + public sealed class Nodes { + private Nodes() { } + + /// <summary> + /// Extracts a Node's name and namespace URI (if any). + /// </summary> + public static XmlQualifiedName GetQName(XmlNode n) { + return new XmlQualifiedName(n.LocalName, n.NamespaceURI); + } + + /// <summary> + /// Tries to merge all direct Text and CDATA children of the given + /// Node and concatenates their value. + /// </summary> + /// <return>an empty string if the Node has no Text or CDATA + /// children.</return> + public static string GetMergedNestedText(XmlNode n) { + StringBuilder sb = new StringBuilder(); + foreach (XmlNode child in n.ChildNodes) { + if (child is XmlText || child is XmlCDataSection) { + string s = child.Value; + if (s != null) { + sb.Append(s); + } + } + } + return sb.ToString(); + } + + /// <summary> + /// Obtains an element's attributes as dictionary. + /// </summary> + public static IDictionary<XmlQualifiedName, string> + GetAttributes(XmlNode n) { + IDictionary<XmlQualifiedName, string> map = + new Dictionary<XmlQualifiedName, string>(); + XmlAttributeCollection coll = n.Attributes; + if (coll != null) { + foreach (XmlAttribute a in coll) { + map[GetQName(a)] = a.Value; + } + } + return map; + } + } +} Property changes on: trunk/xmlunit/src/main/net-core/util/Nodes.cs ___________________________________________________________________ Added: svn:eol-style + native Copied: trunk/xmlunit/src/tests/net-core/diff/ElementSelectorsTest.cs (from rev 379, trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/diff/ElementSelectorsTest.java) =================================================================== --- trunk/xmlunit/src/tests/net-core/diff/ElementSelectorsTest.cs (rev 0) +++ trunk/xmlunit/src/tests/net-core/diff/ElementSelectorsTest.cs 2010-05-07 05:48:03 UTC (rev 382) @@ -0,0 +1,268 @@ +/* + This file is licensed to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +using System.Xml; +using NUnit.Framework; + +namespace net.sf.xmlunit.diff { + + [TestFixture] + public class ElementSelectorsTest { + private const string FOO = "foo"; + private const string BAR = "bar"; + private const string SOME_URI = "urn:some:uri"; + + private XmlDocument doc; + + [SetUp] public void CreateDoc() { + doc = new XmlDocument(); + } + + private void PureElementNameComparisons(ElementSelector s) { + XmlElement control = doc.CreateElement(FOO); + XmlElement equal = doc.CreateElement(FOO); + XmlElement different = doc.CreateElement(BAR); + XmlElement controlNS = doc.CreateElement(BAR, FOO, SOME_URI); + + Assert.IsFalse(s(null, null)); + Assert.IsFalse(s(null, control)); + Assert.IsFalse(s(control, null)); + Assert.IsTrue(s(control, equal)); + Assert.IsFalse(s(control, different)); + Assert.IsFalse(s(control, controlNS)); + Assert.IsTrue(s(doc.CreateElement(FOO, SOME_URI), controlNS)); + } + + [Test] public void ByName() { + PureElementNameComparisons(ElementSelectors.ByName); + } + + [Test] public void ByNameAndText_NamePart() { + PureElementNameComparisons(ElementSelectors.ByNameAndText); + } + + private void ByNameAndText_SingleLevel(ElementSelector s) { + XmlElement control = doc.CreateElement(FOO); + control.AppendChild(doc.CreateTextNode(BAR)); + XmlElement equal = doc.CreateElement(FOO); + equal.AppendChild(doc.CreateTextNode(BAR)); + XmlElement equalC = doc.CreateElement(FOO); + equalC.AppendChild(doc.CreateCDataSection(BAR)); + XmlElement noText = doc.CreateElement(FOO); + XmlElement differentText = doc.CreateElement(FOO); + differentText.AppendChild(doc.CreateTextNode(BAR)); + differentText.AppendChild(doc.CreateTextNode(BAR)); + + Assert.IsTrue(s(control, equal)); + Assert.IsTrue(s(control, equalC)); + Assert.IsFalse(s(control, noText)); + Assert.IsFalse(s(control, differentText)); + } + + [Test] public void ByNameAndText() { + ByNameAndText_SingleLevel(ElementSelectors.ByNameAndText); + } + + [Test] public void ByNameAndTextRec_NamePart() { + PureElementNameComparisons(ElementSelectors.ByNameAndTextRec); + } + + [Test] public void ByNameAndTextRec_Single() { + ByNameAndText_SingleLevel(ElementSelectors.ByNameAndTextRec); + } + + [Test] public void ByNameAndTextRec() { + XmlElement control = doc.CreateElement(FOO); + XmlElement child = doc.CreateElement(BAR); + control.AppendChild(child); + child.AppendChild(doc.CreateTextNode(BAR)); + XmlElement equal = doc.CreateElement(FOO); + XmlElement child2 = doc.CreateElement(BAR); + equal.AppendChild(child2); + child2.AppendChild(doc.CreateTextNode(BAR)); + XmlElement equalC = doc.CreateElement(FOO); + XmlElement child3 = doc.CreateElement(BAR); + equalC.AppendChild(child3); + child3.AppendChild(doc.CreateCDataSection(BAR)); + XmlElement noText = doc.CreateElement(FOO); + XmlElement differentLevel = doc.CreateElement(FOO); + differentLevel.AppendChild(doc.CreateTextNode(BAR)); + XmlElement differentElement = doc.CreateElement(FOO); + XmlElement child4 = doc.CreateElement(FOO); + differentElement.AppendChild(child4); + child4.AppendChild(doc.CreateTextNode(BAR)); + XmlElement differentText = doc.CreateElement(FOO); + XmlElement child5 = doc.CreateElement(BAR); + differentText.AppendChild(child5); + child5.AppendChild(doc.CreateTextNode(FOO)); + + ElementSelector s = ElementSelectors.ByNameAndTextRec; + Assert.IsTrue(s(control, equal)); + Assert.IsTrue(s(control, equalC)); + Assert.IsFalse(s(control, noText)); + Assert.IsFalse(s(control, differentLevel)); + Assert.IsFalse(s(control, differentElement)); + Assert.IsFalse(s(control, differentText)); + } + + [Test] public void ByNameAndAllAttributes_NamePart() { + PureElementNameComparisons(ElementSelectors.ByNameAndAllAttributes); + } + + [Test] public void ByNameAndAllAttributes() { + XmlElement control = doc.CreateElement(FOO); + control.SetAttribute(BAR, BAR); + XmlElement equal = doc.CreateElement(FOO); + equal.SetAttribute(BAR, BAR); + XmlElement noAttributes = doc.CreateElement(FOO); + XmlElement differentValue = doc.CreateElement(FOO); + differentValue.SetAttribute(BAR, FOO); + XmlElement differentName = doc.CreateElement(FOO); + differentName.SetAttribute(FOO, FOO); + XmlElement differentNS = doc.CreateElement(FOO); + differentNS.SetAttribute(BAR, SOME_URI, BAR); + + Assert.IsTrue(ElementSelectors.ByNameAndAllAttributes(control, + equal)); + Assert.IsFalse(ElementSelectors.ByNameAndAllAttributes(control, + noAttributes)); + Assert.IsFalse(ElementSelectors.ByNameAndAllAttributes(noAttributes, + control)); + Assert.IsFalse(ElementSelectors.ByNameAndAllAttributes(control, + differentValue)); + Assert.IsFalse(ElementSelectors.ByNameAndAllAttributes(control, + differentName)); + Assert.IsFalse(ElementSelectors.ByNameAndAllAttributes(control, + differentNS)); + } + + [Test] public void ByNameAndAttributes_NamePart() { + PureElementNameComparisons(ElementSelectors + .ByNameAndAttributes(new string[] {})); + PureElementNameComparisons(ElementSelectors + .ByNameAndAttributes(new XmlQualifiedName[] {})); + PureElementNameComparisons(ElementSelectors.ByNameAndAttributes(BAR)); + PureElementNameComparisons(ElementSelectors + .ByNameAndAttributes(new XmlQualifiedName(BAR, + SOME_URI))); + } + + [Test] public void ByNameAndAttributes_String() { + XmlElement control = doc.CreateElement(FOO); + control.SetAttribute(BAR, BAR); + XmlElement equal = doc.CreateElement(FOO); + equal.SetAttribute(BAR, BAR); + XmlElement noAttributes = doc.CreateElement(FOO); + XmlElement differentValue = doc.CreateElement(FOO); + differentValue.SetAttribute(BAR, FOO); + XmlElement differentName = doc.CreateElement(FOO); + differentName.SetAttribute(FOO, FOO); + XmlElement differentNS = doc.CreateElement(FOO); + differentNS.SetAttribute(BAR, SOME_URI, BAR); + + Assert.IsTrue(ElementSelectors.ByNameAndAttributes(BAR)(control, + equal)); + Assert.IsFalse(ElementSelectors.ByNameAndAttributes(BAR)(control, + noAttributes)); + Assert.IsTrue(ElementSelectors.ByNameAndAttributes(FOO)(control, + noAttributes)); + Assert.IsTrue(ElementSelectors.ByNameAndAttributes(new string[] {}) + (control, noAttributes)); + Assert.IsFalse(ElementSelectors.ByNameAndAttributes(BAR)(noAttributes, + control)); + Assert.IsFalse(ElementSelectors.ByNameAndAttributes(BAR)(control, + differentValue)); + Assert.IsFalse(ElementSelectors.ByNameAndAttributes(BAR)(control, + differentName)); + Assert.IsFalse(ElementSelectors.ByNameAndAttributes(BAR)(control, + differentNS)); + } + + [Test] public void byNameAndAttributes_QName() { + XmlElement control = doc.CreateElement(FOO); + control.SetAttribute(BAR, BAR); + XmlElement equal = doc.CreateElement(FOO); + equal.SetAttribute(BAR, BAR); + XmlElement noAttributes = doc.CreateElement(FOO); + XmlElement differentValue = doc.CreateElement(FOO); + differentValue.SetAttribute(BAR, FOO); + XmlElement differentName = doc.CreateElement(FOO); + differentName.SetAttribute(FOO, FOO); + XmlElement differentNS = doc.CreateElement(FOO); + differentNS.SetAttribute(BAR, SOME_URI, BAR); + + Assert.IsTrue(ElementSelectors.ByNameAndAttributes(new XmlQualifiedName(BAR)) + (control, equal)); + Assert.IsFalse(ElementSelectors.ByNameAndAttributes(new XmlQualifiedName(BAR)) + (control, noAttributes)); + Assert.IsTrue(ElementSelectors.ByNameAndAttributes(new XmlQualifiedName(FOO)) + (control, noAttributes)); + Assert.IsTrue(ElementSelectors.ByNameAndAttributes(new XmlQualifiedName[] {}) + (control, noAttributes)); + Assert.IsFalse(ElementSelectors.ByNameAndAttributes(new XmlQualifiedName(BAR)) + (noAttributes, control)); + Assert.IsFalse(ElementSelectors.ByNameAndAttributes(new XmlQualifiedName(BAR)) + (control, differentValue)); + Assert.IsFalse(ElementSelectors.ByNameAndAttributes(new XmlQualifiedName(BAR)) + (control, differentName)); + Assert.IsFalse(ElementSelectors.ByNameAndAttributes(new XmlQualifiedName(BAR)) + (control, differentNS)); + } + + [Test] public void ByNameAndAttributesControlNS_NamePart() { + PureElementNameComparisons(ElementSelectors + .ByNameAndAttributesControlNS()); + PureElementNameComparisons(ElementSelectors + .ByNameAndAttributesControlNS(BAR)); + } + + [Test] public void ByNameAndAttributesControlNS() { + XmlElement control = doc.CreateElement(FOO); + control.SetAttribute(BAR, SOME_URI, BAR); + XmlElement equal = doc.CreateElement(FOO); + equal.SetAttribute(BAR, SOME_URI, BAR); + XmlElement noAttributes = doc.CreateElement(FOO); + XmlElement differentValue = doc.CreateElement(FOO); + differentValue.SetAttribute(BAR, SOME_URI, FOO); + XmlElement differentName = doc.CreateElement(FOO); + differentName.SetAttribute(FOO, SOME_URI, FOO); + XmlElement differentNS = doc.CreateElement(FOO); + differentNS.SetAttribute(BAR, SOME_URI + "2", BAR); + XmlElement noNS = doc.CreateElement(FOO); + noNS.SetAttribute(BAR, BAR); + + Assert.IsTrue(ElementSelectors.ByNameAndAttributesControlNS(BAR) + (control, equal)); + Assert.IsFalse(ElementSelectors.ByNameAndAttributesControlNS(BAR) + (control, noAttributes)); + Assert.IsTrue(ElementSelectors.ByNameAndAttributesControlNS(FOO) + (control, noAttributes)); + Assert.IsTrue(ElementSelectors.ByNameAndAttributesControlNS() + (control, noAttributes)); + Assert.IsTrue(ElementSelectors.ByNameAndAttributesControlNS(BAR) + (noAttributes, control)); + Assert.IsFalse(ElementSelectors.ByNameAndAttributesControlNS(BAR) + (noAttributes, noNS)); + Assert.IsFalse(ElementSelectors.ByNameAndAttributesControlNS(BAR) + (control, differentValue)); + Assert.IsFalse(ElementSelectors.ByNameAndAttributesControlNS(BAR) + (control, differentName)); + Assert.IsFalse(ElementSelectors.ByNameAndAttributesControlNS(BAR) + (control, differentNS)); + Assert.IsFalse(ElementSelectors.ByNameAndAttributesControlNS(BAR) + (control, noNS)); + } + + } +} \ No newline at end of file Added: trunk/xmlunit/src/tests/net-core/util/NodesTest.cs =================================================================== --- trunk/xmlunit/src/tests/net-core/util/NodesTest.cs (rev 0) +++ trunk/xmlunit/src/tests/net-core/util/NodesTest.cs 2010-05-07 05:48:03 UTC (rev 382) @@ -0,0 +1,125 @@ +/* + This file is licensed to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +using System.Collections.Generic; +using System.Xml; +using NUnit.Framework; + +namespace net.sf.xmlunit.util { + [TestFixture] + public class NodesTest { + + private const string FOO = "foo"; + private const string BAR = "bar"; + private const string SOME_URI = "urn:some:uri"; + + private XmlDocument doc; + + [SetUp] + public void CreateDoc() { + doc = new XmlDocument(); + } + + [Test] public void QNameOfElementWithNoNs() { + XmlElement e = doc.CreateElement(FOO); + XmlQualifiedName q = Nodes.GetQName(e); + Assert.AreEqual(FOO, q.Name); + Assert.AreEqual(string.Empty, q.Namespace); + Assert.AreEqual(new XmlQualifiedName(FOO), q); + } + + [Test] public void QNameOfXmlElementWithNsNoPrefix() { + XmlElement e = doc.CreateElement(FOO, SOME_URI); + XmlQualifiedName q = Nodes.GetQName(e); + Assert.AreEqual(FOO, q.Name); + Assert.AreEqual(SOME_URI, q.Namespace); + Assert.AreEqual(new XmlQualifiedName(FOO, SOME_URI), q); + } + + [Test] public void QNameOfXmlElementWithNsAndPrefix() { + XmlElement e = doc.CreateElement(BAR, FOO, SOME_URI); + XmlQualifiedName q = Nodes.GetQName(e); + Assert.AreEqual(FOO, q.Name); + Assert.AreEqual(SOME_URI, q.Namespace); + Assert.AreEqual(new XmlQualifiedName(FOO, SOME_URI), q); + } + + [Test] public void MergeNoTexts() { + XmlElement e = doc.CreateElement(FOO); + Assert.AreEqual(string.Empty, Nodes.GetMergedNestedText(e)); + } + + [Test] public void MergeSingleTextNode() { + XmlElement e = doc.CreateElement(FOO); + XmlText t = doc.CreateTextNode(BAR); + e.AppendChild(t); + Assert.AreEqual(BAR, Nodes.GetMergedNestedText(e)); + } + + [Test] public void MergeSingleCDATASection() { + XmlElement e = doc.CreateElement(FOO); + XmlCDataSection t = doc.CreateCDataSection(BAR); + e.AppendChild(t); + Assert.AreEqual(BAR, Nodes.GetMergedNestedText(e)); + } + + [Test] public void MergeIgnoresTextOfChildren() { + XmlElement e = doc.CreateElement(FOO); + XmlElement c = doc.CreateElement("child"); + XmlText t = doc.CreateTextNode(BAR); + e.AppendChild(c); + c.AppendChild(t); + Assert.AreEqual(string.Empty, Nodes.GetMergedNestedText(e)); + } + + [Test] public void MergeIgnoresComments() { + XmlElement e = doc.CreateElement(FOO); + XmlComment c = doc.CreateComment(BAR); + e.AppendChild(c); + Assert.AreEqual(string.Empty, Nodes.GetMergedNestedText(e)); + } + + [Test] public void MergeMultipleChildren() { + XmlElement e = doc.CreateElement(FOO); + XmlCDataSection c = doc.CreateCDataSection(BAR); + e.AppendChild(c); + e.AppendChild(doc.CreateElement("child")); + XmlText t = doc.CreateTextNode(BAR); + e.AppendChild(t); + Assert.AreEqual(BAR + BAR, Nodes.GetMergedNestedText(e)); + } + + [Test] public void AttributeMapNoAttributes() { + XmlElement e = doc.CreateElement(FOO); + IDictionary<XmlQualifiedName, string> m = Nodes.GetAttributes(e); + Assert.AreEqual(0, m.Count); + } + + [Test] public void AttributeIDictionaryNoNS() { + XmlElement e = doc.CreateElement(FOO); + e.SetAttribute(FOO, BAR); + IDictionary<XmlQualifiedName, string> m = Nodes.GetAttributes(e); + Assert.AreEqual(1, m.Count); + Assert.AreEqual(BAR, m[new XmlQualifiedName(FOO)]); + } + + [Test] public void AttributeIDictionarywithNS() { + XmlElement e = doc.CreateElement(FOO); + e.SetAttribute(FOO, SOME_URI, BAR); + IDictionary<XmlQualifiedName, string> m = Nodes.GetAttributes(e); + Assert.AreEqual(1, m.Count); + Assert.AreEqual(BAR, m[new XmlQualifiedName(FOO, SOME_URI)]); + } + } +} Property changes on: trunk/xmlunit/src/tests/net-core/util/NodesTest.cs ___________________________________________________________________ Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |