public void XPathEvaluationWithNamespaceContext()
        {
            string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                         "<feed xmlns=\"http://www.w3.org/2005/Atom\">" +
                         "   <title>Search Engine Feed</title>" +
                         "   <link href=\"https://en.wikipedia.org/wiki/Web_search_engine\"/>" +
                         "   <entry>" +
                         "       <title>Google</title>" +
                         "       <id>goog</id>" +
                         "   </entry>" +
                         "   <entry>" +
                         "       <title>Bing</title>" +
                         "       <id>msft</id>" +
                         "   </entry>" +
                         "</feed>";

            var prefix2Uri = new Dictionary <string, string>();

            prefix2Uri["atom"] = "http://www.w3.org/2005/Atom";

            Assert.That(xml, EvaluateXPathConstraint.HasXPath("count(//atom:feed/atom:entry)",
                                                              Is.EqualTo("2"))
                        .WithNamespaceContext(prefix2Uri));
            Assert.That(xml, EvaluateXPathConstraint.HasXPath("//atom:feed/atom:entry/atom:title/text()",
                                                              Is.EqualTo("Google"))
                        .WithNamespaceContext(prefix2Uri));
            Assert.That(xml, EvaluateXPathConstraint.HasXPath("//atom:feed/atom:entry[2]/atom:title/text()",
                                                              Is.EqualTo("Bing"))
                        .WithNamespaceContext(prefix2Uri));
        }
 public void CanBeCombinedWithFailingMatcher()
 {
     Assert.That(() =>
                 Assert.That("foo", Is.StringContaining("bar")
                             & EvaluateXPathConstraint
                             .HasXPath("//a/b/@attr", Is.EqualTo("something"))),
                 Throws.TypeOf <AssertionException>());
 }
        public void XPathAttributeValueMatchingInXmlElement()
        {
            string      xml = "<a><b attr=\"abc\"></b></a>";
            XmlDocument doc = new XmlDocument();

            doc.LoadXml(xml);
            var xmlRootElement = doc.DocumentElement;

            Assert.That(xmlRootElement, EvaluateXPathConstraint.HasXPath("//a/b/@attr",
                                                                         Is.EqualTo("abc")));
        }
        public void CanBeCombinedWithPassingMatcher()
        {
            string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                         "<fruits>" +
                         "<fruit name=\"apple\"/>" +
                         "<fruit name=\"orange\"/>" +
                         "<fruit name=\"banana\"/>" +
                         "</fruits>";

            Assert.That(xml, Is.StringContaining("apple")
                        & EvaluateXPathConstraint
                        .HasXPath("count(//fruits/fruit)", Is.EqualTo("3")));
        }
        public void XPathAttributeValueMatchingInXmlString()
        {
            string xml = "<a><b attr=\"abc\"></b></a>";

            Assert.That(xml, EvaluateXPathConstraint.HasXPath("//a/b/@attr",
                                                              Is.EqualTo("abc")));
            Assert.That(xml, EvaluateXPathConstraint.HasXPath("count(//a/b/c)",
                                                              Is.EqualTo("0")));
            Assert.That(() => Assert.That(xml,
                                          EvaluateXPathConstraint
                                          .HasXPath("//a/b/@attr", Is.EqualTo("something"))),
                        Throws.TypeOf <AssertionException>()
                        .With.Property("Message").Contains("But was:  \"abc\""));
        }
Exemple #6
0
        public void CreatesAUsefulMessageWhenFailingCombinedWithNotOnTheOutside()
        {
            string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                         "<fruits>" +
                         "<fruit name=\"apple\"/>" +
                         "<fruit name=\"orange\"/>" +
                         "<fruit name=\"banana\"/>" +
                         "</fruits>";

            Assert.That(() =>
                        Assert.That(xml, !EvaluateXPathConstraint.HasXPath("count(//fruits/fruit)",
                                                                           Is.EqualTo("3"))),
                        Throws.TypeOf <AssertionException>()
                        .With.Property("Message").Contains("not XML with XPath count(//fruits/fruit) evaluated to ")
                        .With.Property("Message").Contains("3"));
        }
        public void XPathCountInXmlString()
        {
            string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                         "<fruits>" +
                         "<fruit name=\"apple\"/>" +
                         "<fruit name=\"orange\"/>" +
                         "<fruit name=\"banana\"/>" +
                         "</fruits>";

            Assert.That(xml, EvaluateXPathConstraint.HasXPath("count(//fruits/fruit)",
                                                              Is.EqualTo("3")));
            Assert.That(xml, EvaluateXPathConstraint.HasXPath("//fruits/fruit/@name",
                                                              Is.EqualTo("apple")));
            Assert.That(xml, EvaluateXPathConstraint.HasXPath("count(//fruits/fruit[@name=\"orange\"])",
                                                              Is.EqualTo("1")));
            Assert.That(xml, EvaluateXPathConstraint.HasXPath("count(//fruits/fruit[@name=\"apricot\"])",
                                                              Is.EqualTo("0")));
        }
Exemple #8
0
 /// <summary>
 /// Creates the result.
 /// </summary>
 public HasXPathConstraintResult(EvaluateXPathConstraint constraint, object actual, ConstraintResult nestedResult)
     : base(constraint, actual, nestedResult.IsSuccess)
 {
     this.constraint   = constraint;
     this.nestedResult = nestedResult;
 }