|
| 1 | +/* |
| 2 | + This file is licensed to You under the Apache License, Version 2.0 |
| 3 | + (the "License"); you may not use this file except in compliance with |
| 4 | + the License. You may obtain a copy of the License at |
| 5 | +
|
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +
|
| 8 | + Unless required by applicable law or agreed to in writing, software |
| 9 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + See the License for the specific language governing permissions and |
| 12 | + limitations under the License. |
| 13 | +*/ |
| 14 | +using System; |
| 15 | +using System.Collections.Generic; |
| 16 | +using System.Linq; |
| 17 | +using System.Xml; |
| 18 | +using NUnit.Framework; |
| 19 | + |
| 20 | +namespace Org.XmlUnit.Diff { |
| 21 | + |
| 22 | + [TestFixture] |
| 23 | + public class DefaultNodeMatcherTest { |
| 24 | + |
| 25 | + private XmlDocument doc; |
| 26 | + |
| 27 | + [SetUp] |
| 28 | + public void CreateDoc() { |
| 29 | + doc = new XmlDocument(); |
| 30 | + } |
| 31 | + |
| 32 | + [Test] |
| 33 | + public void ElementSelectorsAreQueriedInSequence() { |
| 34 | + XmlElement control1 = doc.CreateElement("a"); |
| 35 | + control1.AppendChild(doc.CreateTextNode("foo")); |
| 36 | + XmlElement control2 = doc.CreateElement("a"); |
| 37 | + control2.AppendChild(doc.CreateTextNode("bar")); |
| 38 | + |
| 39 | + XmlElement test1 = doc.CreateElement("a"); |
| 40 | + test1.AppendChild(doc.CreateTextNode("baz")); |
| 41 | + XmlElement test2 = doc.CreateElement("a"); |
| 42 | + test2.AppendChild(doc.CreateTextNode("foo")); |
| 43 | + |
| 44 | + DefaultNodeMatcher m = |
| 45 | + new DefaultNodeMatcher(ElementSelectors.ByNameAndText, |
| 46 | + ElementSelectors.ByName); |
| 47 | + List<KeyValuePair<XmlNode, XmlNode>> result = |
| 48 | + m.Match(new XmlNode[] { control1, control2}, |
| 49 | + new XmlNode[] { test1, test2 }).ToList(); |
| 50 | + Assert.AreEqual(result.Count, 2); |
| 51 | + |
| 52 | + // ByNameAndText |
| 53 | + Assert.AreSame(result[0].Key, control1); |
| 54 | + Assert.AreSame(result[0].Value, test2); |
| 55 | + |
| 56 | + // ByName |
| 57 | + Assert.AreSame(result[1].Key, control2); |
| 58 | + Assert.AreSame(result[1].Value, test1); |
| 59 | + } |
| 60 | + |
| 61 | + [Test] |
| 62 | + // https://github.com/xmlunit/xmlunit/issues/197 |
| 63 | + public void ElementSelectorsAreQueriedInSequenceWithConditionalSelector() { |
| 64 | + XmlElement control1 = doc.CreateElement("a"); |
| 65 | + control1.AppendChild(doc.CreateTextNode("foo")); |
| 66 | + XmlElement control2 = doc.CreateElement("a"); |
| 67 | + control2.AppendChild(doc.CreateTextNode("bar")); |
| 68 | + |
| 69 | + XmlElement test1 = doc.CreateElement("a"); |
| 70 | + test1.AppendChild(doc.CreateTextNode("baz")); |
| 71 | + XmlElement test2 = doc.CreateElement("a"); |
| 72 | + test2.AppendChild(doc.CreateTextNode("foo")); |
| 73 | + |
| 74 | + DefaultNodeMatcher m = |
| 75 | + new DefaultNodeMatcher(ElementSelectors.SelectorForElementNamed("a", ElementSelectors.ByNameAndText), |
| 76 | + ElementSelectors.ByName); |
| 77 | + List<KeyValuePair<XmlNode, XmlNode>> result = |
| 78 | + m.Match(new XmlNode[] { control1, control2}, |
| 79 | + new XmlNode[] { test1, test2 }).ToList(); |
| 80 | + Assert.AreEqual(result.Count, 2); |
| 81 | + |
| 82 | + // ByNameAndText |
| 83 | + Assert.AreSame(result[0].Key, control1); |
| 84 | + Assert.AreSame(result[0].Value, test2); |
| 85 | + |
| 86 | + // ByName |
| 87 | + Assert.AreSame(result[1].Key, control2); |
| 88 | + Assert.AreSame(result[1].Value, test1); |
| 89 | + } |
| 90 | + |
| 91 | + [Test] |
| 92 | + public void ElementSelectorsAreQueriedInSequenceWithControlNodesSwapped() { |
| 93 | + XmlElement control1 = doc.CreateElement("a"); |
| 94 | + control1.AppendChild(doc.CreateTextNode("bar")); |
| 95 | + XmlElement control2 = doc.CreateElement("a"); |
| 96 | + control2.AppendChild(doc.CreateTextNode("foo")); |
| 97 | + |
| 98 | + XmlElement test1 = doc.CreateElement("a"); |
| 99 | + test1.AppendChild(doc.CreateTextNode("foo")); |
| 100 | + XmlElement test2 = doc.CreateElement("a"); |
| 101 | + test2.AppendChild(doc.CreateTextNode("baz")); |
| 102 | + |
| 103 | + DefaultNodeMatcher m = |
| 104 | + new DefaultNodeMatcher(ElementSelectors.ByNameAndText, |
| 105 | + ElementSelectors.ByName); |
| 106 | + List<KeyValuePair<XmlNode, XmlNode>> result = |
| 107 | + m.Match(new XmlNode[] { control1, control2}, |
| 108 | + new XmlNode[] { test1, test2 }).ToList(); |
| 109 | + Assert.AreEqual(result.Count, 2); |
| 110 | + |
| 111 | + // ByNameAndText |
| 112 | + Assert.AreSame(result[0].Key, control2); |
| 113 | + Assert.AreSame(result[0].Value, test1); |
| 114 | + |
| 115 | + // ByName |
| 116 | + Assert.AreSame(result[1].Key, control1); |
| 117 | + Assert.AreSame(result[1].Value, test2); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments