f:arity added for delayed functions

master
Mike Gerwitz 2014-11-22 00:14:37 -05:00
parent acdacef1d7
commit eb66c838c6
5 changed files with 87 additions and 15 deletions

View File

@ -193,4 +193,20 @@
</message>
</template>
<!--
Attempt to retrieve arity of delayed function
The input must be a function reference. If the arity cannot be
determined, -1 is returned.
-->
<function name="f:arity" as="xs:decimal">
<param name="fnref" as="element()" />
<sequence select="if ( $fnref/@arity ) then
$fnref/@arity
else
-1" />
</function>
</stylesheet>

View File

@ -112,8 +112,6 @@
dependencies and simply specify the full namespace URI -->
<variable name="name-resolv"
select="resolve-QName( @name, . )" />
<variable name="local-name"
select="substring-after( @name, ':' )" />
<variable name="ns-prefix"
select="substring-before( @name, ':' )" />
<variable name="ns"
@ -121,7 +119,7 @@
$ns-prefix, . )" />
<sequence select="fgen:create-func(
$name-resolv, $local-name, $ns-prefix, $ns )" />
., $name-resolv, $ns-prefix, $ns )" />
<sequence select="fgen:create-tpl(
$name-resolv, ., $ns-prefix, $ns )" />
</template>
@ -147,17 +145,23 @@
mode `f:apply' to invoke the associated application template.
-->
<function name="fgen:create-func">
<param name="fnref" as="element(xsl:function)" />
<param name="name-resolv" as="xs:QName" />
<param name="local-name" as="xs:string" />
<param name="ns-prefix" as="xs:string" />
<param name="ns" as="xs:anyURI" />
<variable name="local-name"
select="substring-after( $fnref/@name, ':' )" />
<out:function name="{$name-resolv}" as="element()">
<namespace name="{$ns-prefix}"
select="$ns" />
<element name="{$local-name}"
namespace="{$ns}" />
namespace="{$ns}">
<attribute name="arity"
select="count( $fnref/xsl:param )" />
</element>
</out:function>
</function>

View File

@ -172,4 +172,41 @@
</scenario>
</scenario>
</scenario>
<scenario label="f:arity">
<scenario label="given a proper function reference">
<variable name="test-arity"
select="5" />
<!-- test our format directly; do not rely on the
apply-gen code -->
<variable name="fnref">
<foo:bar arity="{$test-arity}" />
</variable>
<call function="f:arity">
<param name="fnref"
select="$fnref" />
</call>
<expect label="provides function arity"
test="$x:result = $test-arity" />
</scenario>
<scenario label="given a function reference with no defined arity">
<variable name="fnref">
<foo:bar />
</variable>
<call function="f:arity">
<param name="fnref"
select="$fnref" />
</call>
<expect label="returns -1"
test="$x:result = -1" />
</scenario>
</scenario>
</description>

View File

@ -30,6 +30,8 @@
<!-- SUT -->
<import href="../../src/transform/apply-gen.xsl" />
<import href="../../src/apply.xsl" />
<!-- input stylesheet -->
<import href="apply-gen-test-in.xsl" />

View File

@ -22,6 +22,7 @@
<description xmlns="http://www.jenitennison.com/xslt/xspec"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:f="http://www.lovullo.com/hoxsl/apply"
xmlns:foo="http://www.lovullo.com/_junk"
stylesheet="apply-gen-test.xsl">
@ -49,9 +50,7 @@
<!-- the generated node is simply the same name and namespace as
the function -->
<expect label="generated function produces function node"
context="xsl:stylesheet/xsl:function/*">
<foo:bar />
</expect>
test="exists( xsl:stylesheet/xsl:function/foo:bar )" />
<!-- more detailed tests below -->
@ -157,14 +156,16 @@
Exciting! -->
<scenario label="given a generated document">
<!-- calling the generated nullary -->
<call function="foo:apply-add-two">
<param name="x" select="2" />
<param name="y" select="3" />
</call>
<scenario label="with a processed function">
<!-- calling the generated nullary -->
<call function="foo:apply-add-two">
<param name="x" select="2" />
<param name="y" select="3" />
</call>
<expect label="applying template to nullary yields proper result"
test="$x:result = 5" />
<expect label="applying template to nullary yields proper result"
test="$x:result = 5" />
</scenario>
<!-- ensure that multiple functions are processed -->
@ -177,5 +178,17 @@
<expect label="applying template to nullary yields proper result"
test="$x:result = -1" />
</scenario>
<!-- note that this _does not_ test f:arity itself: it is intended
to test that the arity datum is properly generated -->
<scenario label="f:arity">
<call function="f:arity">
<param name="fnref" select="foo:sub-two()" />
</call>"
<expect label="is able to determine arity"
test="$x:result = 2" />
</scenario>
</scenario>
</description>