[gxml] Adding Unit Tests for GElement XPath implementation



commit 27a35c26f67c2a5f59f2828ee8eae5a0586a0a8e
Author: Yannick Inizan <inizan yannick gmail com>
Date:   Mon Oct 3 17:57:10 2016 -0500

    Adding Unit Tests for GElement XPath implementation
    
    * Just a few modification from Daniel Espinosa <esodan gmail com>
      in order to avoid change dependencies on DomDocument interfaces
    
    * Needs fixes on network file access

 gxml/GXmlDocument.vala      |   14 +++++++++++++-
 gxml/XPath.vala             |    2 +-
 test/GElementXPathTest.vala |   37 +++++++++++++++++++++++++++++++++----
 3 files changed, 47 insertions(+), 6 deletions(-)
---
diff --git a/gxml/GXmlDocument.vala b/gxml/GXmlDocument.vala
index 53301ea..85814a3 100644
--- a/gxml/GXmlDocument.vala
+++ b/gxml/GXmlDocument.vala
@@ -34,7 +34,8 @@ public class GXml.GDocument : GXml.GNode,
                               GXml.DomParentNode,
                               GXml.DomNonElementParentNode,
                               GXml.DomDocument,
-                              GXml.DomXMLDocument
+                              GXml.DomXMLDocument,
+                              GXml.XPathContext
 {
   protected Xml.Doc* doc;
   protected Xml.Buffer _buffer;
@@ -356,6 +357,17 @@ public class GXml.GDocument : GXml.GNode,
     if (l.size > 0) return (DomElement) l[0];
     return null;
   }
+  /**
+   * {@link XPathContext} implementation.
+   */
+  public GXml.XPathObject evaluate (string expression,
+                                    Gee.List<GXml.Namespace>? resolver = null)
+                                    throws GXml.XPathError
+  {
+    if (document_element == null)
+      return null;
+    return (document_element as XPathContext).evaluate (expression, resolver);
+  }
 }
 
 
diff --git a/gxml/XPath.vala b/gxml/XPath.vala
index d1e47b8..9e8ecf3 100644
--- a/gxml/XPath.vala
+++ b/gxml/XPath.vala
@@ -69,7 +69,7 @@ public interface GXml.XPathContext : GLib.Object {
    * Throw {@link GXml.XPath.Error} if one of provided namespaces is invalid.
    */
   public abstract GXml.XPathObject evaluate (string expression,
-                                            Gee.List<GXml.Namespace>? resolver)
+                                            Gee.List<GXml.Namespace>? resolver = null)
                                             throws GXml.XPathError;
 
 }
diff --git a/test/GElementXPathTest.vala b/test/GElementXPathTest.vala
index 0cd3e30..a8bc32e 100644
--- a/test/GElementXPathTest.vala
+++ b/test/GElementXPathTest.vala
@@ -1,7 +1,8 @@
-/* -*- Mode: vala; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
+/* -*- Mode: vala; indent-tabs-mode: nil; c-basic-offset: 2; tab-width: 2 -*- */
 /* Notation.vala
  *
  * Copyright (C) 2016  Daniel Espinosa <esodan gmail com>
+ * Copyright (C) 2016  Yannick Inizan <inizan yannick gmail com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -18,14 +19,42 @@
  *
  * Authors:
  *      Daniel Espinosa <esodan gmail com>
+ *      Yannick Inizan <inizan yannick gmail com>
  */
 
 using GXml;
 
 class GElementXPathTest : GXmlTest  {
-       public static void add_tests () {
-               Test.add_func ("/gxml/gelement/xpath", () => {
-                       
+  public static void add_tests () {
+    Test.add_func ("/gxml/gelement/xpath", () => {
+      var document = new GDocument.from_uri ("http://www.w3schools.com/xsl/books.xml";);
+      var object = document.evaluate ("/bookstore/book/title");
+      assert (object.object_type == XPathObjectType.NODESET);
+      var array = object.nodeset.to_array();
+      assert (array.length == 4);
+      assert (array[0].node_value == "Everyday Italian");
+      assert (array[1].node_value == "Harry Potter");
+      assert (array[2].node_value == "XQuery Kick Start");
+      assert (array[3].node_value == "Learning XML");
+      object = document.evaluate ("/bookstore/book[1]/title");
+      assert (object.object_type == XPathObjectType.NODESET);
+      array = object.nodeset.to_array();
+      assert (array.length == 1);
+      assert (array[0].node_value == "Everyday Italian");
+      object = document.evaluate ("/bookstore/book/price[text()]");
+      assert (object.object_type == XPathObjectType.NODESET);
+      array = object.nodeset.to_array();
+      assert (array.length == 4);
+      assert (array[0].node_value == "30.00");
+      assert (array[1].node_value == "29.99");
+      assert (array[2].node_value == "49.99");
+      assert (array[3].node_value == "39.95");
+      object = document.evaluate ("/bookstore/book[price>35]/price");
+      assert (object.object_type == XPathObjectType.NODESET);
+      array = object.nodeset.to_array();
+      assert (array.length == 2);
+      assert (array[0].node_value == "49.99");
+      assert (array[1].node_value == "39.95");
                });
        }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]