[gxml/xpath: 4/8] test/: add tests for XPath
- From: Richard Hans Schwarting <rschwart src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gxml/xpath: 4/8] test/: add tests for XPath
- Date: Tue, 15 Oct 2013 15:52:59 +0000 (UTC)
commit 28a689eb595d249c81dd31c021ab57c2939eddcb
Author: Adam Ples <ples adam gmail com>
Date: Mon Oct 14 05:41:21 2013 -0400
test/: add tests for XPath
test/GXmlTest.vala | 12 ++-
test/Makefile.am | 5 +
test/XPathExpressionTest.vala | 353 +++++++++++++++++++++++++++++++++++++++++
test/XPathNSResolverTest.vala | 22 +++
test/XPathResultTest.vala | 153 ++++++++++++++++++
5 files changed, 543 insertions(+), 2 deletions(-)
---
diff --git a/test/GXmlTest.vala b/test/GXmlTest.vala
index 4819a2f..ed9f82d 100644
--- a/test/GXmlTest.vala
+++ b/test/GXmlTest.vala
@@ -37,16 +37,20 @@ class GXmlTest {
ValaLibxml2Test.add_tests ();
SerializationTest.add_tests ();
SerializableTest.add_tests ();
+ XPathExpressionTest.add_tests ();
+ XPathNSResolverTest.add_tests ();
+ XPathResultTest.add_tests ();
Test.run ();
return 0;
}
- internal static Document get_doc () {
+ internal static Document get_doc (string? path = null) {
Document doc = null;
try {
- doc = new Document.from_path (get_test_dir () + "/test.xml");
+ doc = new Document.from_path (path != null ? path :
+ get_test_dir () + "/test.xml");
} catch (GXml.Error e) {
GLib.warning (e.message);
assert_not_reached ();
@@ -54,6 +58,10 @@ class GXmlTest {
return doc;
}
+ internal static Document get_doc_with_ns () {
+ return get_doc (get_test_dir () + "/test_with_ns.xml");
+ }
+
internal static string get_test_dir () {
if (TEST_DIR == null || TEST_DIR == "") {
return ".";
diff --git a/test/Makefile.am b/test/Makefile.am
index 87675cd..bc19782 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -37,6 +37,9 @@ gxml_test_SOURCES = \
ValaLibxml2Test.vala \
SerializableTest.vala \
SerializationTest.vala \
+ XPathExpressionTest.vala \
+ XPathNSResolverTest.vala \
+ XPathResultTest.vala \
$(NULL)
gxml_test.vala.stamp: $(gxml_test_SOURCES)
@@ -80,6 +83,8 @@ BUILT_SOURCES = gxml_test.vala.stamp
CLEANFILES = $(BUILT_SOURCES) gxml_test
+# TODO: should we add *.c to this list?, or is there a var of generated .c we can put in CLEANFILES?
+
DISTCLEANFILES = _serialization_test_*.xml
EXTRA_DIST += \
diff --git a/test/XPathExpressionTest.vala b/test/XPathExpressionTest.vala
new file mode 100644
index 0000000..e550615
--- /dev/null
+++ b/test/XPathExpressionTest.vala
@@ -0,0 +1,353 @@
+/* -*- Mode: vala; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+using GXml;
+
+class XPathExpressionTest : GXmlTest {
+
+ public static void add_tests () {
+
+ Test.add_func ("/gxml/xpath/expression/create_valid_w_resolver", () => {
+ try {
+ XPath.NSResolver r = new XPath.NSResolver(
+ "xs", "http://www.w3.org/2001/XMLSchema",
+ "m", "http://www.w3.org/1998/Math/MathML"
+ );
+ XPath.Expression e = new XPath.Expression("//Author", r);
+
+ } catch (XPath.Error e) {
+ Test.message("%s", e.message);
+ assert_not_reached();
+ }
+ });
+
+ Test.add_func ("/gxml/xpath/expression/create_invalid_w_resolver", () => {
+ try {
+ XPath.NSResolver r = new XPath.NSResolver(
+ "xs", "http://www.w3.org/2001/XMLSchema",
+ "m", "http://www.w3.org/1998/Math/MathML"
+ );
+ XPath.Expression e = new XPath.Expression("//A\\uthor", r);
+
+ assert_not_reached();
+
+ } catch (XPath.Error e) {
+ assert (e is XPath.Error.INVALID_EXPRESSION);
+ }
+ });
+
+ Test.add_func ("/gxml/xpath/expression/create_valid_wo_resolver", () => {
+ try {
+ XPath.Expression e = new XPath.Expression("//Author");
+
+ } catch (XPath.Error e) {
+ Test.message("%s", e.message);
+ assert_not_reached();
+ }
+ });
+
+ Test.add_func ("/gxml/xpath/expression/create_invalid_wo_resolver", () => {
+ try {
+ XPath.Expression e = new XPath.Expression("//Aut&hor");
+ assert_not_reached();
+
+ } catch (XPath.Error e) {
+ assert (e is XPath.Error.INVALID_EXPRESSION);
+ }
+ });
+
+ Test.add_func ("/gxml/xpath/expression/evaluate_wo_ns", () => {
+ try {
+ Document doc = get_doc ();
+ XPath.Expression e = new XPath.Expression("//Author");
+ var result = e.evaluate(doc);
+
+ assert (result.result_type == XPath.ResultType.ORDERED_NODE_SNAPSHOT);
+ var ns = result.nodeset_value;
+
+ assert (ns.size == 2);
+ assert (ns[0].node_type == NodeType.ELEMENT);
+ assert (ns[0].node_name == "Author");
+ assert (ns[1].node_type == NodeType.ELEMENT);
+ assert (ns[1].node_name == "Author");
+
+ } catch (XPath.Error e) {
+ Test.message("%s", e.message);
+ assert_not_reached();
+ }
+ });
+
+ Test.add_func ("/gxml/xpath/expression/evaluate_w_ns_1", () => {
+ try {
+ Document doc = get_doc_with_ns ();
+ XPath.NSResolver r = new XPath.NSResolver(
+ "svg", "http://www.w3.org/2000/svg",
+ "m", "http://www.w3.org/1998/Math/MathML",
+ "r", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+ );
+ XPath.Expression e = new XPath.Expression("//m:*", r);
+ var result = e.evaluate(doc);
+
+ assert (result.result_type == XPath.ResultType.ORDERED_NODE_SNAPSHOT);
+ var ns = result.nodeset_value;
+
+ assert (ns.size == 23);
+
+ } catch (XPath.Error e) {
+ stdout.printf("'%s'\n", e.message);
+ Test.message("%s", e.message);
+ assert_not_reached();
+ }
+ });
+
+ Test.add_func ("/gxml/xpath/expression/evaluate_w_ns_2", () => {
+ try {
+ Document doc = get_doc_with_ns ();
+ XPath.NSResolver r = new XPath.NSResolver(
+ "svg", "http://www.w3.org/2000/svg",
+ "r", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+ );
+ XPath.Expression e = new
XPath.Expression("/svg:svg/svg:metadata/r:RDF/r:Description", r);
+ var result = e.evaluate(doc);
+
+ assert (result.result_type == XPath.ResultType.ORDERED_NODE_SNAPSHOT);
+ var ns = result.nodeset_value;
+
+ assert (ns.size == 2);
+ assert (ns[0].node_type == NodeType.ELEMENT);
+ assert (ns[0].node_name == "Description");
+ assert (ns[0] is Element);
+ assert (((Element) ns[0]).get_attribute("id") == "description1");
+ assert (ns[1].node_type == NodeType.ELEMENT);
+ assert (ns[1].node_name == "Description");
+ assert (ns[1] is Element);
+ assert (((Element) ns[1]).get_attribute("id") == "description2");
+
+ } catch (XPath.Error e) {
+ Test.message("%s", e.message);
+ assert_not_reached();
+ }
+ });
+
+ Test.add_func ("/gxml/xpath/expression/evaluate_w_ns_3", () => {
+ try {
+ Document doc = get_doc_with_ns ();
+ XPath.NSResolver r = new XPath.NSResolver(
+ "math", "http://www.w3.org/1998/Math/MathML",
+ "i", "proto:item-info"
+ );
+ XPath.Expression e = new XPath.Expression("//math:msqrt|//i:country",
r);
+ var result = e.evaluate(doc);
+
+ assert (result.result_type == XPath.ResultType.ORDERED_NODE_SNAPSHOT);
+ var ns = result.nodeset_value;
+
+ assert (ns.size == 2);
+
+ foreach (var node in ns) {
+ assert (node.node_type == NodeType.ELEMENT);
+ if (node.node_name == "msqrt") {
+ assert (node is Element);
+ assert (((Element) node).get_attribute("id") ==
"msqrt1");
+ }
+ else if (node.node_name == "country") {
+ assert (node is Element);
+ assert (((Element) node).get_attribute("id") ==
"country1");
+ }
+ else {
+ assert_not_reached();
+ }
+ }
+
+ } catch (XPath.Error e) {
+ Test.message("%s", e.message);
+ assert_not_reached();
+ }
+ });
+
+ Test.add_func ("/gxml/xpath/expression/evaluate_to_bool", () => {
+ try {
+ Document doc = get_doc_with_ns ();
+ XPath.NSResolver r = new XPath.NSResolver(
+ "m", "http://www.w3.org/1998/Math/MathML",
+ "i", "proto:item-info"
+ );
+ XPath.Expression e = new XPath.Expression("count(//i:*)=3", r);
+ var result = e.evaluate(doc);
+
+ assert (result.result_type == XPath.ResultType.BOOLEAN);
+ assert (result.boolean_value == true);
+
+ e = new XPath.Expression("//m:mi[2]/text()!=\"b\"", r);
+ result = e.evaluate(doc);
+
+ assert (result.result_type == XPath.ResultType.BOOLEAN);
+ assert (result.boolean_value == false);
+
+ } catch (XPath.Error e) {
+ Test.message("%s", e.message);
+ assert_not_reached();
+ }
+ });
+
+ Test.add_func ("/gxml/xpath/expression/evaluate_to_number", () => {
+ try {
+ Document doc = get_doc_with_ns ();
+ XPath.NSResolver r = new XPath.NSResolver(
+ "rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+ );
+ XPath.Expression e = new XPath.Expression("count(//rdf:Description)",
r);
+ var result = e.evaluate(doc);
+
+ assert (result.result_type == XPath.ResultType.NUMBER);
+ assert (result.number_value == 2.0);
+
+ } catch (XPath.Error e) {
+ Test.message("%s", e.message);
+ assert_not_reached();
+ }
+ });
+
+ Test.add_func ("/gxml/xpath/expression/evaluate_to_string", () => {
+ try {
+ Document doc = get_doc_with_ns ();
+ XPath.NSResolver r = new XPath.NSResolver(
+ "m", "http://www.w3.org/1998/Math/MathML"
+ );
+ XPath.Expression e = new
XPath.Expression("normalize-space(//m:mo[1]/text())", r);
+ var result = e.evaluate(doc);
+
+ assert (result.result_type == XPath.ResultType.STRING);
+ assert (result.string_value == "=");
+
+ } catch (XPath.Error e) {
+ Test.message("%s", e.message);
+ assert_not_reached();
+ }
+ });
+
+ Test.add_func ("/gxml/xpath/expression/evaluate_w_ns_to_attr", () => {
+ try {
+ Document doc = get_doc_with_ns ();
+ XPath.NSResolver r = new XPath.NSResolver(
+ "svg", "http://www.w3.org/2000/svg",
+ "m", "http://www.w3.org/1998/Math/MathML",
+ "r", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+ );
+ XPath.Expression e = new XPath.Expression("//svg:g/@id", r);
+ XPath.Result result = null;
+ Node context = null;
+
+ result = e.evaluate(doc);
+ assert (result.result_type == XPath.ResultType.ORDERED_NODE_SNAPSHOT);
+ assert (result.nodeset_value.size == 3);
+ /* For now result.nodeset_value[x] is null because of lack of
+ * implementation in Document.lookup_node for Attr nodes */
+ assert (result.nodeset_value[0] != null);
+ assert (result.nodeset_value[0] is Attr);
+ context = result.nodeset_value[0];
+
+ } catch (XPath.Error e) {
+ Test.message("%s", e.message);
+ assert_not_reached();
+ }
+ });
+
+ Test.add_func ("/gxml/xpath/expression/evaluate_w_ns_in_element_context", () => {
+ try {
+ Document doc = get_doc_with_ns ();
+ XPath.NSResolver r = new XPath.NSResolver(
+ "svg", "http://www.w3.org/2000/svg",
+ "m", "http://www.w3.org/1998/Math/MathML",
+ "r", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+ );
+ XPath.Expression e = new XPath.Expression("./svg:g", r);
+ XPath.Result result = null;
+ Node context = null;
+
+ result = e.evaluate(doc.document_element);
+ assert (result.result_type == XPath.ResultType.ORDERED_NODE_SNAPSHOT);
+ assert (result.nodeset_value.size == 1);
+ assert (result.nodeset_value[0] is Element);
+ assert (((Element) result.nodeset_value[0]).get_attribute("id") ==
"g1");
+
+ context = result.nodeset_value[0];
+ result = e.evaluate(context);
+ assert (result.result_type == XPath.ResultType.ORDERED_NODE_SNAPSHOT);
+ assert (result.nodeset_value.size == 1);
+ assert (result.nodeset_value[0] is Element);
+ assert (((Element) result.nodeset_value[0]).get_attribute("id") ==
"g2");
+
+ context = result.nodeset_value[0];
+ result = e.evaluate(context);
+ assert (result.result_type == XPath.ResultType.ORDERED_NODE_SNAPSHOT);
+ assert (result.nodeset_value.size == 1);
+ assert (result.nodeset_value[0] is Element);
+ assert (((Element) result.nodeset_value[0]).get_attribute("id") ==
"g3");
+
+ } catch (XPath.Error e) {
+ Test.message("%s", e.message);
+ assert_not_reached();
+ }
+ });
+
+ Test.add_func ("/gxml/xpath/expression/evaluate_w_ns_in_attr_context", () => {
+ try {
+ Document doc = get_doc_with_ns ();
+ XPath.Result result = null;
+ Node context = null;
+
+ result = doc.evaluate("//svg:g", new XPath.NSResolver("svg",
"http://www.w3.org/2000/svg"));
+ assert (result.result_type == XPath.ResultType.ORDERED_NODE_SNAPSHOT);
+ assert (result.nodeset_value.size == 3);
+ assert (result.nodeset_value[0] != null);
+ assert (result.nodeset_value[0] is Element);
+
+ context = result.nodeset_value[0].attributes["id"];
+ assert (context != null);
+ assert (context is Attr);
+
+ result = context.evaluate("string-length(.)");
+ assert (result.result_type == XPath.ResultType.NUMBER);
+ assert (result.number_value == 2);
+
+ result = context.evaluate("normalize-space(.)");
+ assert (result.result_type == XPath.ResultType.STRING);
+ assert (result.string_value == "g1");
+
+ } catch (XPath.Error e) {
+ Test.message("%s", e.message);
+ assert_not_reached();
+ }
+ });
+
+ Test.add_func ("/gxml/xpath/expression/evaluate_w_ns_in_text_context", () => {
+ try {
+ Document doc = get_doc_with_ns ();
+ XPath.Result result = null;
+ Node context = null;
+
+ result = doc.evaluate("//i:year/text()", new XPath.NSResolver("i",
"proto:item-info"));
+ assert (result.result_type == XPath.ResultType.ORDERED_NODE_SNAPSHOT);
+ assert (result.nodeset_value.size == 1);
+ assert (result.nodeset_value[0] != null);
+ assert (result.nodeset_value[0] is Text);
+
+ context = result.nodeset_value[0];
+ assert (context != null);
+ assert (context is Text);
+
+ result = context.evaluate("string-length(.)");
+ assert (result.result_type == XPath.ResultType.NUMBER);
+ assert (result.number_value == 4);
+
+ result = context.evaluate("normalize-space(.)");
+ assert (result.result_type == XPath.ResultType.STRING);
+ assert (result.string_value == "1984");
+
+ } catch (XPath.Error e) {
+ Test.message("%s", e.message);
+ assert_not_reached();
+ }
+ });
+ }
+}
+
diff --git a/test/XPathNSResolverTest.vala b/test/XPathNSResolverTest.vala
new file mode 100644
index 0000000..cc90cf5
--- /dev/null
+++ b/test/XPathNSResolverTest.vala
@@ -0,0 +1,22 @@
+/* -*- Mode: vala; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+using GXml;
+
+class XPathNSResolverTest : GXmlTest {
+ public static void add_tests () {
+ Test.add_func ("/gxml/xpath/nsresolver/lookup_namespace_uri", () => {
+ XPath.NSResolver r = new XPath.NSResolver(
+ "xs", "http://www.w3.org/2001/XMLSchema",
+ "m", "http://www.w3.org/1998/Math/MathML"
+ );
+
+ assert (r["xs"] == "http://www.w3.org/2001/XMLSchema");
+ assert (r["m"] == "http://www.w3.org/1998/Math/MathML");
+ assert (r[""] == null);
+ assert (r["other"] == null);
+ assert (r.lookup_namespace_uri("xs") == "http://www.w3.org/2001/XMLSchema");
+ assert (r.lookup_namespace_uri("m") == "http://www.w3.org/1998/Math/MathML");
+ assert (r.lookup_namespace_uri("") == null);
+ assert (r.lookup_namespace_uri("other") == null);
+ });
+ }
+}
diff --git a/test/XPathResultTest.vala b/test/XPathResultTest.vala
new file mode 100644
index 0000000..bf096b4
--- /dev/null
+++ b/test/XPathResultTest.vala
@@ -0,0 +1,153 @@
+/* -*- Mode: vala; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+using GXml;
+
+class TestResult : GXml.XPath.Result {
+ public TestResult (Document doc, Xml.XPath.Object* o) throws XPath.Error {
+ base (doc, o);
+ }
+}
+
+/* sizeof(Xml.XPath.Object) = sizeof(void*) */
+public const int SIZEOF_XPATH_OBJECT = 56;
+
+/*
+ * calloc is used to allocate memory for Xml.XPath.Object as a workaround for
+ * lack of constructor. This ensures that object->stringval will be null before
+ * first assignment (this field would be g_free0d upon assignment, so if it had
+ * some non-null value it would result in segfault).
+ */
+[CCode (cname="calloc")]
+extern void* calloc(size_t num, size_t size);
+
+
+class XPathResultTest : GXmlTest {
+
+ public static void add_tests () {
+
+ Test.add_func ("/gxml/xpath/result/create_type_number", () => {
+ Document doc = get_doc ();
+ Xml.XPath.Object* o = calloc (1, SIZEOF_XPATH_OBJECT);
+
+ o->type = Xml.XPath.ObjectType.NUMBER;
+ o->floatval = 4.0;
+
+ try {
+ TestResult result = new TestResult (doc, o);
+ assert (result.result_type == XPath.ResultType.NUMBER);
+ assert (result.number_value == 4.0);
+ assert (result.to_bool () == true);
+ assert (result.to_number () == 4.0);
+ assert (result.to_string () == "4");
+
+ } catch (XPath.Error e) {
+ Test.message("%s", e.message);
+ assert_not_reached ();
+ }
+ });
+
+ Test.add_func ("/gxml/xpath/result/create_type_boolean_true", () => {
+ Document doc = get_doc ();
+ Xml.XPath.Object* o = calloc (1, SIZEOF_XPATH_OBJECT);
+
+ o->type = Xml.XPath.ObjectType.BOOLEAN;
+ o->boolval = (int) true;
+
+ try {
+ TestResult result = new TestResult (doc, o);
+ assert (result.result_type == XPath.ResultType.BOOLEAN);
+ assert (result.boolean_value == true);
+ assert (result.to_bool () == true);
+ assert (result.to_number () == 1.0);
+ assert (result.to_string () == "true");
+
+ } catch (XPath.Error e) {
+ Test.message("%s", e.message);
+ assert_not_reached ();
+ }
+ });
+
+ Test.add_func ("/gxml/xpath/result/create_type_boolean_false", () => {
+ Document doc = get_doc ();
+ Xml.XPath.Object* o = calloc (1, SIZEOF_XPATH_OBJECT);
+
+ o->type = Xml.XPath.ObjectType.BOOLEAN;
+ o->boolval = (int) false;
+
+ try {
+ TestResult result = new TestResult(doc, o);
+ assert (result.result_type == XPath.ResultType.BOOLEAN);
+ assert (result.boolean_value == false);
+ assert (result.to_bool () == false);
+ assert (result.to_number () == 0.0);
+ assert (result.to_string () == "false");
+
+ } catch (XPath.Error e) {
+ Test.message("%s", e.message);
+ assert_not_reached ();
+ }
+ });
+
+ Test.add_func ("/gxml/xpath/result/create_type_string_nan_true", () => {
+ Document doc = get_doc ();
+ Xml.XPath.Object* o = calloc (1, SIZEOF_XPATH_OBJECT);
+
+ o->type = Xml.XPath.ObjectType.STRING;
+ o->stringval = "SomeString";
+
+ try {
+ TestResult result = new TestResult (doc, o);
+ assert (result.result_type == XPath.ResultType.STRING);
+ assert (result.string_value == "SomeString");
+ assert (result.to_bool () == true);
+ assert (result.to_number ().is_nan());
+ assert (result.to_string () == "SomeString");
+
+ } catch (XPath.Error e) {
+ Test.message("%s", e.message);
+ assert_not_reached ();
+ }
+ });
+
+ Test.add_func ("/gxml/xpath/result/create_type_string_nan_false", () => {
+ Document doc = get_doc ();
+ Xml.XPath.Object* o = calloc (1, SIZEOF_XPATH_OBJECT);
+
+ o->type = Xml.XPath.ObjectType.STRING;
+ o->stringval = "";
+
+ try {
+ TestResult result = new TestResult (doc, o);
+ assert (result.result_type == XPath.ResultType.STRING);
+ assert (result.string_value == "");
+ assert (result.to_bool () == false);
+ assert (result.to_number ().is_nan());
+ assert (result.to_string () == "");
+
+ } catch (XPath.Error e) {
+ Test.message("%s", e.message);
+ assert_not_reached ();
+ }
+ });
+
+ Test.add_func ("/gxml/xpath/result/create_type_string_numeric", () => {
+ Document doc = get_doc ();
+ Xml.XPath.Object* o = calloc (1, SIZEOF_XPATH_OBJECT);
+
+ o->type = Xml.XPath.ObjectType.STRING;
+ o->stringval = "4e+1";
+
+ try {
+ TestResult result = new TestResult(doc, o);
+ assert (result.result_type == XPath.ResultType.STRING);
+ assert (result.string_value == "4e+1");
+ assert (result.to_bool () == true);
+ assert (result.to_number () == 40.0);
+ assert (result.to_string () == "4e+1");
+
+ } catch (XPath.Error e) {
+ Test.message("%s", e.message);
+ assert_not_reached ();
+ }
+ });
+ }
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]