Commit 44555f06 authored by Kasimier T. Buchcik's avatar Kasimier T. Buchcik
Browse files

Added initial tests for some new DOM-wrapper helping functions.

* test/namespaces/reconcile/tests.xml
  test/namespaces/reconcile/tests-to-c.xsl: Added initial tests
  for some new DOM-wrapper helping functions.
parent cbddfe87
Mon Jun 27 14:06:10 CEST 2005 Kasimier Buchcik <libxml2-cvs@cazic.net>
* test/namespaces/reconcile/tests.xml
test/namespaces/reconcile/tests-to-c.xsl: Added initial tests
for some new DOM-wrapper helping functions.
Mon Jun 27 14:01:06 CEST 2005 Kasimier Buchcik <libxml2-cvs@cazic.net>
* xstc/xstc-to-python.xsl: Cleanup.
......
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:text><![CDATA[
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
]]>
</xsl:text>
<xsl:call-template name="serializer"/>
<xsl:apply-templates select="tests/test"/>
<xsl:text>
int main(int argc, char **argv) {&#xA;</xsl:text>
<xsl:apply-templates select="tests/test" mode="call"/>
<xsl:text>
/* printf("finished.\n"); */
return (0);
}
</xsl:text>
</xsl:template>
<xsl:template match="tests/test" mode="call">
<xsl:text> nsTest_</xsl:text>
<xsl:value-of select="@name"/>
<xsl:text>();&#xA;</xsl:text>
</xsl:template>
<xsl:template name="xml-text">
<xsl:param name="text"/>
<xsl:call-template name="replace-string">
<!-- Substitute #10 for " -->
<xsl:with-param name="from" select="'&#10;'"/>
<xsl:with-param name="to" select="'&quot;&#10;&quot;'"/>
<xsl:with-param name="text">
<xsl:call-template name="replace-string">
<!-- Substitute " for \" -->
<xsl:with-param name="from" select="'&quot;'"/>
<xsl:with-param name="to" select="'\&quot;'"/>
<xsl:with-param name="text">
<xsl:call-template name="replace-string">
<!-- Remove tabs. -->
<xsl:with-param name="from" select="'&#9;'"/>
<xsl:with-param name="to" select="''"/>
<xsl:with-param name="text" select="$text"/>
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</xsl:template>
<xsl:template match="doc" mode="define">
<xsl:text> xmlDocPtr </xsl:text>
<xsl:value-of select="@name"/>
<xsl:text>;&#xA;</xsl:text>
<xsl:text> const char * </xsl:text>
<xsl:value-of select="@name"/>
<xsl:text>_str = "</xsl:text>
<xsl:call-template name="xml-text">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
<xsl:text>";&#xA;</xsl:text>
</xsl:template>
<xsl:template match="expected" mode="define">
<xsl:text> const char * </xsl:text>
<xsl:text>exp_str = "</xsl:text>
<xsl:call-template name="xml-text">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
<xsl:text>";&#xA;</xsl:text>
</xsl:template>
<xsl:template match="doc">
<xsl:text> </xsl:text>
<xsl:value-of select="@name"/>
<xsl:text> = xmlReadDoc(BAD_CAST </xsl:text>
<xsl:value-of select="@name"/>
<xsl:text>_str, NULL, NULL, 0);&#xA;</xsl:text>
<xsl:apply-templates select="following-sibling::*[1]"/>
<xsl:text> xmlFreeDoc(</xsl:text>
<xsl:value-of select="@name"/>
<xsl:text>);&#xA;</xsl:text>
</xsl:template>
<xsl:template match="xpath">
</xsl:template>
<xsl:template match="var" mode="define">
<xsl:text> xmlNodePtr </xsl:text>
<xsl:value-of select="@name"/>
<xsl:text>;&#xA;</xsl:text>
</xsl:template>
<xsl:template match="var">
<xsl:if test="xpath">
<!-- Create XPath context. -->
<xsl:text> /* Selecting node "</xsl:text><xsl:value-of select="@name"/><xsl:text>". */&#xA;</xsl:text>
<xsl:text> xp = xmlXPathNewContext(</xsl:text>
<xsl:value-of select="xpath/@doc"/>
<xsl:text>);&#xA;</xsl:text>
<!-- Register namespaces. -->
<xsl:for-each select="xpath/reg-ns">
<xsl:text> xmlXPathRegisterNs(xp, BAD_CAST "</xsl:text>
<xsl:value-of select="@prefix"/>
<xsl:text>", BAD_CAST "</xsl:text>
<xsl:value-of select="@ns"/>
<xsl:text>");&#xA;</xsl:text>
</xsl:for-each>
<!-- Evaluate. -->
<xsl:text> </xsl:text>
<xsl:value-of select="@name"/>
<xsl:text> = nsSelectNode(xp, "</xsl:text>
<xsl:value-of select="xpath/@select-node"/>
<xsl:text>");&#xA;</xsl:text>
<xsl:text> xmlXPathFreeContext(xp);&#xA;</xsl:text>
</xsl:if>
<xsl:apply-templates select="following-sibling::*[1]"/>
</xsl:template>
<xsl:template match="reconcile-ns">
<xsl:text> /* Reconcile node "</xsl:text><xsl:value-of select="@ref"/><xsl:text>". */&#xA;</xsl:text>
<xsl:text> xmlDOMWrapReconcileNamespaces(NULL, </xsl:text>
<xsl:value-of select="@node"/>
<xsl:text>, 0);&#xA;</xsl:text>
<xsl:apply-templates select="following-sibling::*[1]"/>
</xsl:template>
<xsl:template match="remove">
<xsl:text> xmlDOMWrapRemoveNode(NULL, </xsl:text>
<xsl:value-of select="@node"/>
<xsl:text>->doc, </xsl:text>
<xsl:value-of select="@node"/>
<xsl:text>, 0);&#xA;</xsl:text>
<xsl:apply-templates select="following-sibling::*[1]"/>
</xsl:template>
<xsl:template match="adopt">
<xsl:text> /* Adopt "</xsl:text><xsl:value-of select="@node"/><xsl:text>". */&#xA;</xsl:text>
<xsl:text> xmlDOMWrapAdoptNode(NULL, </xsl:text>
<xsl:value-of select="@node"/>
<xsl:text>->doc, </xsl:text>
<xsl:value-of select="@node"/>
<xsl:text>, </xsl:text>
<xsl:value-of select="@dest-doc"/>
<xsl:text>, </xsl:text>
<xsl:choose>
<xsl:when test="@dest-parent">
<xsl:value-of select="@dest-parent"/>
</xsl:when>
<xsl:otherwise>
<xsl:text>NULL</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:text>, 0);&#xA;</xsl:text>
<xsl:apply-templates select="following-sibling::*[1]"/>
</xsl:template>
<xsl:template match="append-child">
<xsl:text> xmlAddChild(</xsl:text>
<xsl:value-of select="@parent"/>
<xsl:text>, </xsl:text>
<xsl:value-of select="@child"/>
<xsl:text>);&#xA;</xsl:text>
<xsl:apply-templates select="following-sibling::*[1]"/>
</xsl:template>
<xsl:template match="expected">
<xsl:text> /* Serialize "</xsl:text><xsl:value-of select="@doc"/><xsl:text>". */&#xA;</xsl:text>
<xsl:text> result_str = nsSerializeNode(xmlDocGetRootElement(</xsl:text>
<xsl:value-of select="@doc"/>
<xsl:text>));&#xA;</xsl:text>
<xsl:text> /* Compare result. */
if (! xmlStrEqual(BAD_CAST result_str, BAD_CAST exp_str)) {
printf("FAILED\n");
printf("%s\n", (const char *) result_str);
printf("- - -\n");
printf("Expected:\n%s\n", exp_str);
printf("= = =\n");
}
xmlFree(result_str);&#xA;</xsl:text>
</xsl:template>
<!--********
* TEST *
********-->
<xsl:template match="test">
<xsl:text>void nsTest_</xsl:text>
<xsl:value-of select="@name"/>
<xsl:text>(void) {
xmlChar * result_str;
xmlXPathContextPtr xp;
int memory;&#xA;</xsl:text>
<xsl:apply-templates select="*" mode="define"/>
<xsl:text>
memory = xmlMemUsed();
xmlInitParser();&#xA;&#xA;</xsl:text>
<xsl:apply-templates select="child::*[1]"/>
<xsl:text>
xmlCleanupParser();
memory = xmlMemUsed() - memory;
if (memory != 0) {
printf("## '%s' MEMORY leak: %d\n", "</xsl:text>
<xsl:value-of select="@name"/>
<xsl:text>", memory);
}
}
</xsl:text>
</xsl:template>
<xsl:template name="serializer">
<xsl:text>
xmlChar * nsSerializeNode(xmlNodePtr node) {
xmlChar * ret;
xmlOutputBufferPtr buf;
buf = xmlAllocOutputBuffer(NULL);
xmlNodeDumpOutput(buf, node->doc, node, 0, 0, NULL);
xmlOutputBufferFlush(buf);
ret = (xmlChar *) buf->buffer->content;
buf->buffer->content = NULL;
(void) xmlOutputBufferClose(buf);
return (ret);
}
xmlNodePtr nsSelectNode(xmlXPathContextPtr xp, const char * xpath) {
xmlXPathObjectPtr xpres;
xmlNodePtr ret;
xpres = xmlXPathEval(BAD_CAST xpath, xp);
ret = xpres->nodesetval->nodeTab[0];
xmlXPathFreeObject(xpres);
return (ret);
}
</xsl:text>
</xsl:template>
<xsl:template name="replace-string">
<xsl:param name="text"/>
<xsl:param name="from"/>
<xsl:param name="to"/>
<xsl:choose>
<xsl:when test="contains($text, $from)">
<xsl:variable name="before" select="substring-before($text, $from)"/>
<xsl:variable name="after" select="substring-after($text, $from)"/>
<xsl:variable name="prefix" select="concat($before, $to)"/>
<xsl:value-of select="$before"/>
<xsl:value-of select="$to"/>
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="$after"/>
<xsl:with-param name="from" select="$from"/>
<xsl:with-param name="to" select="$to"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0"?>
<tests>
<test name="adopt_recon_default">
<doc name="doc1"><![CDATA[<p1 xmlns="urn:test:AAA"><a/></p1>]]></doc>
<doc name="doc2"><![CDATA[<p1 xmlns="urn:test:BBB"><b/></p1>]]></doc>
<doc name="doc3"><![CDATA[<p2 xmlns="urn:test:ZZZ"/>]]></doc>
<var name="a">
<xpath doc="doc1" select-node="/a:p1/a:a">
<reg-ns prefix="a" ns="urn:test:AAA"/>
</xpath>
</var>
<var name="b" >
<xpath doc="doc2" select-node="/b:p1/b:b">
<reg-ns prefix="b" ns="urn:test:BBB"/>
</xpath>
</var>
<var name="p2">
<xpath doc="doc3" select-node="/z:p2">
<reg-ns prefix="z" ns="urn:test:ZZZ"/>
</xpath>
</var>
<remove node="a"/>
<adopt dest-doc="doc3" node="a"/>
<append-child parent="p2" child="a"/>
<!-- This time without a previous removing. -->
<adopt dest-doc="doc3" node="b"/>
<append-child parent="p2" child="b"/>
<reconcile-ns node="p2"/>
<expected doc="doc3"><![CDATA[<p2 xmlns="urn:test:ZZZ"><a xmlns="urn:test:AAA"/><b xmlns="urn:test:BBB"/></p2>]]></expected>
</test>
<test name="adopt_recon">
<doc name="doc1"><![CDATA[<a:p1 xmlns:a="urn:test:AAA"><a:a/></a:p1>]]></doc>
<doc name="doc2"><![CDATA[<a:p2 xmlns:a="urn:test:AAA"/>]]></doc>
<var name="a">
<xpath doc="doc1" select-node="/a:p1/a:a">
<reg-ns prefix="a" ns="urn:test:AAA"/>
</xpath>
</var>
<var name="p2">
<xpath doc="doc2" select-node="/a:p2">
<reg-ns prefix="a" ns="urn:test:AAA"/>
</xpath>
</var>
<adopt dest-doc="doc2" node="a"/>
<append-child parent="p2" child="a"/>
<reconcile-ns node="a"/>
<expected doc="doc2"><![CDATA[<a:p2 xmlns:a="urn:test:AAA"><a:a/></a:p2>]]></expected>
</test>
<test name="adopt_recon_attr">
<doc name="doc1"><![CDATA[<p1 xmlns:b="urn:test:BBB" b:attr="marvin"/>]]></doc>
<doc name="doc2"><![CDATA[<a:p2 xmlns:a="urn:test:AAA"/>]]></doc>
<var name="attr">
<xpath doc="doc1" select-node="/p1/@b:attr">
<reg-ns prefix="b" ns="urn:test:BBB"/>
</xpath>
</var>
<var name="p2">
<xpath doc="doc2" select-node="/a:p2">
<reg-ns prefix="a" ns="urn:test:AAA"/>
</xpath>
</var>
<adopt dest-doc="doc2" node="attr" />
<append-child parent="p2" child="attr"/>
<reconcile-ns node="p2"/>
<expected doc="doc2"><![CDATA[<a:p2 xmlns:a="urn:test:AAA" xmlns:b="urn:test:BBB" b:attr="marvin"/>]]></expected>
</test>
<test name="adopt_recon_attr_2">
<!-- Here we will specify a destination-parent for adoption. -->
<doc name="doc1"><![CDATA[<p1 xmlns:b="urn:test:BBB" b:attr="marvin"/>]]></doc>
<doc name="doc2"><![CDATA[<a:p2 xmlns:a="urn:test:AAA"/>]]></doc>
<var name="attr">
<xpath doc="doc1" select-node="/p1/@b:attr">
<reg-ns prefix="b" ns="urn:test:BBB"/>
</xpath>
</var>
<var name="p2">
<xpath doc="doc2" select-node="/a:p2">
<reg-ns prefix="a" ns="urn:test:AAA"/>
</xpath>
</var>
<adopt dest-doc="doc2" dest-parent="p2" node="attr" />
<append-child parent="p2" child="attr"/>
<reconcile-ns node="p2"/>
<expected doc="doc2"><![CDATA[<a:p2 xmlns:a="urn:test:AAA" xmlns:b="urn:test:BBB" b:attr="marvin"/>]]></expected>
</test>
<test name="adopt_recon_attr_3">
<!-- Conflicting ns-prefixes for the added attribute. -->
<doc name="doc1"><![CDATA[<p1 xmlns:a="urn:test:AAA" a:attr="marvin"/>]]></doc>
<doc name="doc2"><![CDATA[<a:p2 xmlns:a="urn:test:aaa"/>]]></doc>
<var name="attr">
<xpath doc="doc1" select-node="/p1/@a:attr">
<reg-ns prefix="a" ns="urn:test:AAA"/>
</xpath>
</var>
<var name="p2">
<xpath doc="doc2" select-node="/a:p2">
<reg-ns prefix="a" ns="urn:test:aaa"/>
</xpath>
</var>
<adopt dest-doc="doc2" node="attr" />
<append-child parent="p2" child="attr"/>
<reconcile-ns node="p2"/>
<expected doc="doc2"><![CDATA[<a:p2 xmlns:a="urn:test:aaa" xmlns:a1="urn:test:AAA" a1:attr="marvin"/>]]></expected>
</test>
<test name="adopt_recon_attr_4">
<!-- Qualified attribute added to default namespaced elem. -->
<doc name="doc1"><![CDATA[<p1 xmlns:a="urn:test:AAA" a:attr="marvin"/>]]></doc>
<doc name="doc2"><![CDATA[<p2 xmlns="urn:test:AAA"/>]]></doc>
<var name="attr">
<xpath doc="doc1" select-node="/p1/@a:attr">
<reg-ns prefix="a" ns="urn:test:AAA"/>
</xpath>
</var>
<var name="p2">
<xpath doc="doc2" select-node="/a:p2">
<reg-ns prefix="a" ns="urn:test:AAA"/>
</xpath>
</var>
<adopt dest-doc="doc2" node="attr" />
<append-child parent="p2" child="attr"/>
<reconcile-ns node="p2"/>
<expected doc="doc2"><![CDATA[<p2 xmlns="urn:test:AAA" xmlns:a="urn:test:AAA" a:attr="marvin"/>]]></expected>
</test>
<test name="adopt_recon_attr_5">
<!-- Shadowed and conflicting prefix. dest-parent IS given. -->
<doc name="doc1"><![CDATA[<p1 xmlns:a="urn:test:AAA" a:attr="marvin"/>]]></doc>
<doc name="doc2"><![CDATA[<p2 xmlns:a="urn:test:AAA"><a:p3 xmlns:a="urn:test:ZZZ"/></p2>]]></doc>
<var name="attr">
<xpath doc="doc1" select-node="/p1/@a:attr">
<reg-ns prefix="a" ns="urn:test:AAA"/>
</xpath>
</var>
<var name="p3">
<xpath doc="doc2" select-node="/p2/a:p3">
<reg-ns prefix="a" ns="urn:test:ZZZ"/>
</xpath>
</var>
<adopt dest-doc="doc2" node="attr" dest-parent="p3"/>
<append-child parent="p3" child="attr"/>
<!-- Note that we don't need to reconcile here, since done during
adoption. -->
<expected doc="doc2"><![CDATA[
<p2 xmlns:a="urn:test:AAA">
<a:p3 xmlns:a="urn:test:ZZZ" xmlns:a1="urn:test:AAA" a1:attr="marvin"/>
</p2>]]></expected>
</test>
<test name="adopt_recon_attr_6">
<!-- Shadowed and conflicting prefix. dest-parent NOT given. -->
<doc name="doc1"><![CDATA[<p1 xmlns:a="urn:test:AAA" a:attr="marvin"/>]]></doc>
<doc name="doc2"><![CDATA[<p2 xmlns:a="urn:test:AAA"><a:p3 xmlns:a="urn:test:ZZZ"/></p2>]]></doc>
<var name="attr">
<xpath doc="doc1" select-node="/p1/@a:attr">
<reg-ns prefix="a" ns="urn:test:AAA"/>
</xpath>
</var>
<var name="p3">
<xpath doc="doc2" select-node="/p2/a:p3">
<reg-ns prefix="a" ns="urn:test:ZZZ"/>
</xpath>
</var>
<adopt dest-doc="doc2" node="attr"/>
<append-child parent="p3" child="attr"/>
<reconcile-ns node="p3"/>
<expected doc="doc2"><![CDATA[
<p2 xmlns:a="urn:test:AAA">
<a:p3 xmlns:a="urn:test:ZZZ" xmlns:a1="urn:test:AAA" a1:attr="marvin"/>
</p2>]]></expected>
</test>
<test name="shadow_1">
<doc name="doc"><![CDATA[
<foo xmlns:foo="urn:test:mine">
<p xmlns:bar="urn:test:mine"/>
<a1>
<a2 xmlns:bar="urn:test:OTHER">
<foo:a3/>
</a2>
</a1>
</foo>
]]></doc>
<var name="a1">
<xpath doc="doc" select-node="/foo/a1"/>
</var>
<var name="p" >
<xpath doc="doc" select-node="/foo/p"/>
</var>
<remove node="a1"/>
<append-child parent="p" child="a1"/>
<reconcile-ns node="a1"/>
<expected doc="doc"><![CDATA[
<foo xmlns:foo="urn:test:mine">
<p xmlns:bar="urn:test:mine">
<a1>
<a2 xmlns:bar="urn:test:OTHER">
<foo:a3/>
</a2>
</a1>
</p>
</foo>
]]></expected>
</test>
<test name="norm_1">
<doc name="doc"><![CDATA[
<foo>
<p1 xmlns:a="urn:test:A" xmlns:b="urn:test:B" xmlns:c="urn:test:C">
<a:a>
<b:b a:attr="dont" c:attr="panic"/>
</a:a>
</p1>
<p2/>
</foo>
]]></doc>
<var name="a">
<xpath doc="doc" select-node="/foo/p1/a:a">
<reg-ns prefix="a" ns="urn:test:A"/>
</xpath>
</var>
<var name="p2" >
<xpath doc="doc" select-node="/foo/p2"/>
</var>
<remove node="a"/>
<append-child parent="p2" child="a"/>
<reconcile-ns node="a"/>
<expected doc="doc"><![CDATA[
<foo>
<p1 xmlns:a="urn:test:A" xmlns:b="urn:test:B" xmlns:c="urn:test:C"/>
<p2>
<a:a xmlns:a="urn:test:A">
<b:b xmlns:b="urn:test:B" xmlns:c="urn:test:C" a:attr="dont" c:attr="panic"/>
</a:a>
</p2>
</foo>
]]></expected>
</test>
</tests>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment