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> </message>
</template> </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> </stylesheet>

View File

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

View File

@ -172,4 +172,41 @@
</scenario> </scenario>
</scenario> </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> </description>

View File

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

View File

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