More comprehensive fnref abstraction

This will allow the implementation to evolve transparently.  And it will be
evolving.
master
Mike Gerwitz 2014-12-01 09:53:45 -05:00
commit faadaa3edd
11 changed files with 900 additions and 428 deletions

View File

@ -162,78 +162,4 @@
</message>
</template>
<!--
Create a reference to dynamic function @var{name} with arity
@var{arity}
This function @emph{does not} verify that the function @var{name}
exists, nor does it verify that the provided @var{arity} is valid
for it. Further, the returned function reference will work
@emph{only with dynamic functions}—that is, an application template
is needed. See @file{transform/apply-gen.xsl} for more information
and examples.
-->
<function name="f:make-ref" as="element( f:ref )">
<param name="name" as="xs:QName" />
<param name="arity" as="xs:integer" />
<variable name="ns"
select="namespace-uri-from-QName( $name )" />
<f:ref arity="{$arity}">
<element name="{$name}"
namespace="{$ns}" />
</f:ref>
</function>
<!--
Determines whether @var{fnref} represents a valid dynamic function
reference
This can be used to determine if @var{fnref} is valid as input to
other functions, some of which may produce an error if called with
an invalid dynamic function reference.
@i{Implementation details:} To be valid, @var{fnref} must:
@enumerate
@item Be an element of type @code{f:ref};
@item Have a numeric @code{@arity}; and
@item Have a child target function node.
@enumerate
-->
<function name="f:is-ref" as="xs:boolean">
<param name="fnref" as="item()*" />
<variable name="ref" select="$fnref[ 1 ]" />
<!-- for @arity check: note that NaN != NaN -->
<sequence select="$ref instance of element( f:ref )
and number( $ref/@arity ) = number( $ref/@arity )
and exists( $ref/*[ 1 ] )" />
</function>
<!--
Retrieve the QName of the target dynamic function
Usually, this would match precisely the QName of the target
function.
@i{Implementation details:} This actually represents the QName of
the @emph{application template}, which could differ from the target
function name. One reason this may be the case is to provide a
function alias.
-->
<function name="f:QName" as="xs:QName?">
<param name="fnref" as="element( f:ref )" />
<variable name="target" as="element()?"
select="$fnref/element()[ 1 ]" />
<sequence select="node-name( $target )" />
</function>
</stylesheet>

View File

@ -29,7 +29,7 @@
xmlns:f="http://www.lovullo.com/hoxsl/apply"
xmlns:_f="http://www.lovullo.com/hoxsl/apply/_priv">
<import href="arity.xsl" />
<import href="ref.xsl" />
<!--
@ -55,53 +55,32 @@
<param name="fnref" as="item()+" />
<param name="args" as="item()*" />
<!-- perform type check here, not above, since we can be passed a
sequence (e.g.a partially applied function) -->
<variable name="ref"
as="element(f:ref)"
select="$fnref[ 1 ]" />
<!-- note that, if FNREF is partially applied, then this arity
represents the arity of the partially applied function, _not_
the target function -->
<variable name="arity" as="xs:integer"
select="f:arity( $fnref )" />
<variable name="argout" as="item()*">
<!-- include any previously applied arguments (if we're partially
applying a partial application) -->
<sequence select="remove( $fnref, 1 )" />
<!-- nested sequences are implicitly flattened, so we're not
returning a sub-sequence here -->
<sequence select="$args" />
</variable>
<variable name="argn" as="xs:decimal"
select="count( $argout )" />
<variable name="arity" as="xs:decimal"
select="f:arity( $ref )" />
<variable name="argn" as="xs:integer"
select="count( $args )" />
<choose>
<when test="$argn eq $arity">
<sequence select="_f:apply-partial( $ref, $argout )" />
</when>
<when test="$argn gt $arity">
<apply-templates mode="f:partial-arity-error-hook"
select="$ref">
<with-param name="args" select="$argout" />
<with-param name="arity" select="$arity" />
<with-param name="argn" select="$argn" />
select="$fnref">
<with-param name="fnref" select="$fnref" />
<with-param name="args" select="$args" />
</apply-templates>
</when>
<otherwise>
<f:ref>
<sequence select="$ref/@*" />
<variable name="new-ref"
select="f:push-args( $fnref, $args )" />
<attribute name="partial"
select="count( $args )" />
<sequence select="$ref/*" />
</f:ref>
<sequence select="$argout" />
<sequence select="if ( $argn eq $arity ) then
_f:apply-partial( $new-ref )
else
$new-ref" />
</otherwise>
</choose>
</function>
@ -125,8 +104,7 @@
<!-- we never want to fail, so we perform our type check here rather
than using param/@as -->
<sequence select="$fn instance of element(f:ref)
and exists( $fn/@partial )
and number( $fn/@partial ) gt 0" />
and exists( $fnref[ 2 ] )" />
</function>
@ -146,24 +124,18 @@
match="f:ref"
priority="1">
<param name="args" as="item()*" />
<param name="arity" as="xs:decimal" />
<variable name="ref" as="element(f:ref)"
select="." />
<variable name="arity" as="xs:integer"
select="f:arity(.)" />
<variable name="argn" as="xs:decimal"
select="count( $args )" />
<variable name="fn"
select="$ref/*[1]" />
<variable name="fname"
select="concat( '{', namespace-uri( $fn ), '}',
$fn/local-name() )" />
<sequence
select="error(
QName( namespace-uri-for-prefix( 'f', $ref ),
QName( namespace-uri-for-prefix( 'f', . ),
'err:PARTIAL_PARAM_OVERFLOW' ),
concat( 'Attempted partial application of ',
$fname, '#', $arity, ' with ',
'function of arity ', $arity, ' with ',
$argn, ' arguments' ) )" />
</template>
@ -180,14 +152,16 @@
are supported}. This should be enough.
-->
<function name="_f:apply-partial">
<param name="fnref" as="element(f:ref)" />
<param name="args" as="item()*" />
<param name="fnref" as="item()+" />
<variable name="fn" as="element()"
<variable name="args"
select="f:args( $fnref )" />
<variable name="desc" as="element( f:ref )"
select="$fnref[ 1 ]" />
<!-- just as `f:apply', we support up to 8 arguments -->
<apply-templates select="$fn" mode="f:apply">
<apply-templates select="$desc" mode="f:apply">
<with-param name="arg1" select="$args[ 1 ]" />
<with-param name="arg2" select="$args[ 2 ]" />
<with-param name="arg3" select="$args[ 3 ]" />

237
src/apply/ref.xsl 100644
View File

@ -0,0 +1,237 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Dynamic function reference
Copyright (C) 2014 LoVullo Associates, Inc.
This file is part of hoxsl.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
A dynamic function reference is a sequence describing a dynamic
function to be applied. It consists of two major parts—the dynamic
function reference descriptor and the arguments to bind to its
parameters:
@example
(desc[, arg1[, ...argn]])
@end example
The descriptor @var{desc} has the following format:
@example
<f:ref arity="N" [...]>
<target />
</f:ref>
@end example
where the @var{target} node shares the same QName as the function to
be applied, and @var{@arity} is its arity. The @var{f:ref} node may
be decorated with additional attributes depending on its context or
constructor.
-->
<stylesheet version="2.0"
xmlns="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:f="http://www.lovullo.com/hoxsl/apply">
<!--
Create a reference to dynamic function @var{name} with arity
@var{arity}
This function @emph{does not} verify that the function @var{name}
exists, nor does it verify that the provided @var{arity} is valid
for it. Further, the returned function reference will work
@emph{only with dynamic functions}—that is, an application template
is needed. See @file{transform/apply-gen.xsl} for more information
and examples.
-->
<function name="f:make-ref" as="element( f:ref )">
<param name="name" as="xs:QName" />
<param name="arity" as="xs:integer" />
<variable name="ns"
select="namespace-uri-from-QName( $name )" />
<f:ref arity="{$arity}">
<element name="{$name}"
namespace="{$ns}" />
</f:ref>
</function>
<!--
Determines whether @var{fnref} represents a valid dynamic function
reference
This can be used to determine if @var{fnref} is valid as input to
other functions, some of which may produce an error if called with
an invalid dynamic function reference.
@i{Implementation details:} To be valid, @var{fnref} must:
@enumerate
@item Be an element of type @code{f:ref};
@item Have a numeric @code{@arity}; and
@item Have a child target function node.
@enumerate
-->
<function name="f:is-ref" as="xs:boolean">
<param name="fnref" as="item()*" />
<variable name="ref" select="$fnref[ 1 ]" />
<!-- for @arity check: note that NaN != NaN -->
<sequence select="$ref instance of element( f:ref )
and number( $ref/@arity ) = number( $ref/@arity )
and exists( $ref/*[ 1 ] )" />
</function>
<!--
Retrieve the QName of the target dynamic function
Usually, this will match precisely the QName of the target
function.
@var{fnreF} must be a valid dynamic function reference.
@i{Implementation details:} This actually represents the QName of
the @emph{application template}, which could differ from the target
function name. One reason this may be the case is to provide a
function alias.
-->
<function name="f:QName" as="xs:QName?">
<param name="fnref" as="item()+" />
<variable name="desc" as="element( f:ref )"
select="$fnref[ 1 ]" />
<variable name="target" as="element()?"
select="$desc/element()[ 1 ]" />
<sequence select="node-name( $target )" />
</function>
<!--
Attempt to retrieve arity of dynamic function @var{fnref}
@var{fnref} must be a dynamic function reference satisfying
@code{f:is-ref}. Partially applied function references will have an
arity equivalent to the free parameters in the target function.
-->
<function name="f:arity" as="xs:integer">
<param name="fnref" as="item()+" />
<variable name="ref" as="element( f:ref )"
select="$fnref[ 1 ]" />
<!-- this implicitly asserts on the type; it should never fail if
the system is being used properly -->
<variable name="arity" as="xs:integer"
select="$ref/@arity" />
<sequence select="$arity" />
</function>
<!--
Retrieve a sequence of the partially applied arguments of
@var{fnref}
The resulting sequence may be empty. @var{fnref} is assumed to be a
valid dynamic function reference; you should verify that assumption
beforehand.
-->
<function name="f:args" as="item()*">
<param name="fnref" as="item()+" />
<sequence select="remove( $fnref, 1 )" />
</function>
<!--
Set partially applied arguments of @var{fnref} to @var{args},
replacing any existing arguments
The arity of @var{fnref} will be adjusted relative to the number of
items in @var{args} such that the resulting dynamic function
reference has the illusion of being its own, new function:
increasing the argument count of @var{fnref} will @emph{decrease}
the arity of the resulting reference and vice versa.
@emph{No validations are performed on arity}—if there exist more
arguments than the number of target function parameters, no error
will be raised.
@var{fnref} must be a valid dynamic function reference.
-->
<function name="f:set-args" as="item()+">
<param name="fnref" as="item()+" />
<param name="args" as="item()*" />
<variable name="desc" as="element( f:ref )"
select="$fnref[ 1 ]" />
<!-- this reference may be partially applied; see below arity
adjustment -->
<variable name="target-arity" as="xs:double"
select="$desc/@arity + count( f:args( $fnref ) )" />
<f:ref>
<sequence select="$desc/@*" />
<!-- treat partial applications as their own functions (with their
own arities) -->
<attribute name="arity"
select="$target-arity - count( $args )" />
<sequence select="$desc/*" />
</f:ref>
<sequence select="$args" />
</function>
<!--
Pushes @var{args} onto the argument list of @var{fnref}
This operates just as @code{f:set-args}, but retains existing arguments.
-->
<function name="f:push-args" as="item()+">
<param name="fnref" as="item()+" />
<param name="args" as="item()*" />
<sequence select="f:set-args($fnref,
(f:args( $fnref ), $args) )" />
</function>
<!--
Unshifts @var{args} onto the argument list of @var{fnref}
This operates just as @code{f:set-args}, but retains existing arguments.
-->
<function name="f:unshift-args" as="item()+">
<param name="fnref" as="item()+" />
<param name="args" as="item()*" />
<sequence select="f:set-args($fnref,
($args, f:args( $fnref )) )" />
</function>
</stylesheet>

View File

@ -36,6 +36,8 @@
xmlns:out="http://www.lovullo.com/hoxsl/apply/gen/_out"
exclude-result-prefixes="#default fgen">
<import href="../apply/ref.xsl" />
<output indent="yes"
encoding="utf-8" />
@ -119,11 +121,10 @@
dependencies and simply specify the full namespace URI -->
<variable name="name-resolv"
select="resolve-QName( @name, . )" />
<variable name="ns-prefix"
select="substring-before( @name, ':' )" />
<variable name="ns"
select="namespace-uri-for-prefix(
$ns-prefix, . )" />
select="namespace-uri-from-QName( $name-resolv )" />
<variable name="ns-prefix"
select="prefix-from-QName( $name-resolv )" />
<sequence select="fgen:create-func(
., $name-resolv, $ns-prefix, $ns )" />
@ -164,15 +165,10 @@
<namespace name="{$ns-prefix}"
select="$ns" />
<f:ref>
<attribute name="arity"
select="count( $fnref/xsl:param )" />
<variable name="arity"
select="count( $fnref/xsl:param )" />
<!-- represents the function being referenced -->
<element name="{$local-name}"
namespace="{$ns}">
</element>
</f:ref>
<sequence select="f:make-ref( $name-resolv, $arity )" />
</out:function>
</function>

View File

@ -418,162 +418,4 @@
</scenario>
</scenario>
</scenario>
<scenario label="f:make-ref constructor">
<scenario label="when called with a function QName and arity">
<variable name="qname"
select="QName( $foo-uri, 'reftest' )" />
<variable name="arity"
select="2" />
<call function="f:make-ref">
<param name="name" select="$qname" />
<param name="arity" select="$arity" />
</call>
<expect label="produces a dynamic function reference"
test="f:is-ref( $x:result )" />
<expect label="produces provided reference arity"
test="f:arity( $x:result ) = $arity" />
<expect label="produces reference to expected QName"
test="f:QName( $x:result ) = $qname" />
</scenario>
<scenario label="produces a valid dynamic function reference">
<call function="f:apply">
<param name="fnref"
select="f:make-ref( QName( $foo-uri, 'fn1' ),
1 )" />
<param name="arg1"
select="$args/foo:arg1" />
</call>
<expect label="that can be applied using f:apply"
test="$x:result[ 1 ] = foo:applied[ @n = 1 ]
and $x:result[ 2 ] is $args/foo:arg1" />
</scenario>
</scenario>
<scenario label="f:is-ref predicate">
<scenario label="given a valid dynamic function reference">
<call function="f:is-ref">
<param name="fnref"
select="f:make-ref( QName( $foo-uri, 'foo' ), 0 )" />
</call>
<expect label="returns true()"
select="true()" />
</scenario>
<scenario label="given an invalid dynamic function reference">
<call function="f:is-ref">
<param name="fnref">
<foo:bar />
</param>
</call>
<expect label="returns false()"
select="false()" />
</scenario>
<scenario label="given a dynamic function reference without arity">
<call function="f:is-ref">
<param name="fnref">
<f:ref>
<foo:bar />
</f:ref>
</param>
</call>
<expect label="returns false()"
select="false()" />
</scenario>
<scenario label="given a dynamic function reference without
integral arity">
<call function="f:is-ref">
<param name="fnref">
<f:ref arity="moo">
<foo:bar />
</f:ref>
</param>
</call>
<expect label="returns false()"
select="false()" />
</scenario>
<scenario label="given a dynamic function reference without target
node">
<call function="f:is-ref">
<param name="fnref">
<f:ref arity="0" />
</param>
</call>
<expect label="returns false()"
select="false()" />
</scenario>
<scenario label="given an empty sequence">
<call function="f:is-ref">
<param name="fnref" select="()" />
</call>
<expect label="returns false()"
select="false()" />
</scenario>
<scenario label="given a non-element">
<call function="f:is-ref">
<param name="fnref" select="5" />
</call>
<expect label="returns false()"
select="false()" />
</scenario>
</scenario>
<scenario label="f:QName accessor">
<scenario label="given a valid dynamic function reference">
<variable name="foo-qname"
select="QName( $foo-uri, 'foo' )" />
<call function="f:QName">
<param name="fnref"
select="f:make-ref( $foo-qname, 0 )" />
</call>
<expect label="returns QName"
test="$x:result instance of xs:QName" />
<expect label="returns QName of target dynamic function"
select="$foo-qname" />
</scenario>
<scenario label="given a dynamic function reference with no target
node">
<call function="f:QName">
<param name="fnref">
<f:ref arity="5" />
</param>
</call>
<expect label="returns empty sequence"
select="()" />
</scenario>
</scenario>
</description>

View File

@ -1,68 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Tests arity calculations
Copyright (C) 2014 LoVullo Associates, Inc.
This file is part of hoxsl.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<description xmlns="http://www.jenitennison.com/xslt/xspec"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://www.jenitennison.com/xslt/xspec"
xmlns:f="http://www.lovullo.com/hoxsl/apply"
xmlns:foo="http://www.lovullo.com/_junk"
stylesheet="../../src/apply/arity.xsl">
<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">
<f:ref arity="{$test-arity}">
<foo:bar />
</f:ref>
</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">
<f:ref><foo:bar /></f:ref>
</variable>
<call function="f:arity">
<param name="fnref"
select="$fnref" />
</call>
<expect label="returns -1"
test="$x:result = -1" />
</scenario>
</scenario>
</description>

View File

@ -41,8 +41,10 @@
<template mode="f:partial-arity-error-hook"
match="f:ref"
priority="5">
<param name="args" as="item()*" />
<param name="arity" as="xs:decimal" />
<param name="args" as="item()*" />
<variable name="arity"
select="f:arity(.)" />
<foo:partial-error arity="{$arity}" />

View File

@ -51,11 +51,8 @@
<param name="args" select="()" />
</call>
<expect label="returns FNREF"
test="$x:result[ 1 ] = $fnref" />
<expect label="indicates that 0 arguments were partially applied"
test="$x:result/@partial = 0" />
<expect label="returns reference to original target"
test="f:QName( $x:result) = f:QName( $fnref )" />
<expect label="returns nothing else"
test="count( $x:result ) = 1" />
@ -68,15 +65,14 @@
<param name="args" select="$args/foo:a, $args/foo:b" />
</call>
<expect label="returns FNREF as first item in sequence"
test="$x:result[ 1 ] = $fnref" />
<expect label="indicates that N arguments were partially applied"
test="$x:result/@partial = 2" />
<expect label="returns target reference as first item in
sequence"
test="f:QName( $fnref ) = f:QName( $x:result )" />
<variable name="rargs" select="f:args( $x:result )" />
<expect label="returns each argument, ordered"
test="$x:result[ 2 ] is $args/foo:a
and $x:result[ 3 ] is $args/foo:b" />
test="$rargs[ 1 ] is $args/foo:a
and $rargs[ 2 ] is $args/foo:b" />
</scenario>
@ -93,10 +89,11 @@
test="$x:result[ 1 ]
and $x:result[ 1 ] = foo:ternary-applied" />
<variable name="rargs" select="f:args( $x:result )" />
<expect label="applies target function with arguments, by reference"
test="$x:result[ 2 ] is $args/foo:a
and $x:result[ 3 ] is $args/foo:b
and $x:result[ 4 ] is $args/foo:c" />
test="$rargs[ 1 ] is $args/foo:a
and $rargs[ 2 ] is $args/foo:b
and $rargs[ 3 ] is $args/foo:c" />
</scenario>
@ -108,16 +105,17 @@
<param name="args" select="$args/foo:b" />
</call>
<expect label="returns FNREF as first item in sequence"
test="$x:result[ 1 ] = $fnref" />
<expect label="retains target reference"
test="f:QName( $fnref ) = f:QName( $x:result )" />
<expect label="returns arguments from both partial applications"
test="count( $x:result ) = 3" />
test="count( f:args( $x:result ) ) = 2" />
<variable name="rargs" select="f:args( $x:result )" />
<expect label="arguments are ordered with previous applications
first in sequence"
test="$x:result[ 2 ] is $args/foo:a
and $x:result[ 3 ] is $args/foo:b" />
test="$rargs[ 1 ] is $args/foo:a
and $rargs[ 2 ] is $args/foo:b" />
</scenario>
@ -192,9 +190,9 @@
test="$x:result[ 1 ] = foo:applied[ @n = 3 ]" />
<expect label="applies target function with arguments, by reference"
test="$x:result[ 2 ] is $args/foo:a
and $x:result[ 3 ] is $args/foo:b
and $x:result[ 4 ] is $args/foo:c" />
test="$x:result[ 2 ] = $args/foo:a
and $x:result[ 3 ] = $args/foo:b
and $x:result[ 4 ] = $args/foo:c" />
</scenario>
@ -406,7 +404,9 @@
<!-- caller may extract the function ref from the sequence and
assert on that -->
assert on that; we are going to explicitly choose to *not*
support that behavior, since an fnref acquires meaning as an
opaque unit -->
<scenario label="given a valid fref, partially applied with N>0
arguments, without argument sequence">
<call function="f:is-partial">
@ -414,8 +414,8 @@
select="f:partial( foo:ternary(), (1, 2) )[ 1 ]" />
</call>
<expect label="returns true()"
test="$x:result = true()" />
<expect label="returns false()"
select="false()" />
</scenario>
</scenario>
@ -439,8 +439,7 @@
<expect label="provides source dynamic function reference"
test="$x:result[ 2 ] is $fnref" />
<expect label="provides arity of target (root of all partial
applications) function"
<expect label="provides arity of ref"
test="$x:result[ 1 ]/@arity = 3" />
<expect label="provides each argument of partial application
@ -462,10 +461,11 @@
$args/foo:d" />
</call>
<expect label="produces error with all arguments"
<expect label="produces error with arguments of final
application"
test="exists( foo:partial-error )
and $x:result[ 3 ] is $args/foo:a
and $x:result[ 6 ] is $args/foo:d" />
and $x:result[ 3 ] is $args/foo:c
and $x:result[ 4 ] is $args/foo:d" />
</scenario>
</scenario>
</description>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Arity calculations on dynamic and partially applied functions
Tests dynamic function reference
Copyright (C) 2014 LoVullo Associates, Inc.
@ -21,28 +21,15 @@
-->
<stylesheet version="2.0"
xmlns="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:f="http://www.lovullo.com/hoxsl/apply">
xmlns="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:f="http://www.lovullo.com/hoxsl/apply"
xmlns:foo="http://www.lovullo.com/_junk">
<!-- this imports the SUT, as well as additional functions we need
for testing -->
<import href="../../src/apply.xsl" />
<!--
Attempt to retrieve arity of dynamic function
The input must be a function reference. Partially applied function
references will have an arity equivalent to the remaining parameters
in the original function.
If the arity cannot be determined, -1 is returned.
-->
<function name="f:arity" as="xs:decimal">
<param name="fnref" as="element(f:ref)" />
<sequence select="if ( $fnref/@arity ) then
$fnref/@arity
else
-1" />
</function>
<!-- numerous templates for arity tests -->
<import href="../apply-test.xsl" />
</stylesheet>

View File

@ -0,0 +1,550 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Tests dynamic function reference
Copyright (C) 2014 LoVullo Associates, Inc.
This file is part of hoxsl.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<description xmlns="http://www.jenitennison.com/xslt/xspec"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://www.jenitennison.com/xslt/xspec"
xmlns:f="http://www.lovullo.com/hoxsl/apply"
xmlns:foo="http://www.lovullo.com/_junk"
stylesheet="ref-test.xsl">
<variable name="foo-uri"
select="namespace-uri-for-prefix(
'foo', root(.)/element() )" />
<variable name="test-arity"
select="5" />
<variable name="test-fn"
select="f:make-ref( QName( $foo-uri, 'foo:bar' ),
$test-arity )" />
<variable name="args">
<foo:args>
<foo:a />
<foo:b />
<foo:c />
<foo:d />
</foo:args>
</variable>
<scenario label="f:make-ref constructor">
<scenario label="when called with a function QName and arity">
<variable name="qname"
select="QName( $foo-uri, 'reftest' )" />
<variable name="arity"
select="2" />
<call function="f:make-ref">
<param name="name" select="$qname" />
<param name="arity" select="$arity" />
</call>
<expect label="produces a dynamic function reference"
test="f:is-ref( $x:result )" />
<expect label="produces provided reference arity"
test="f:arity( $x:result ) = $arity" />
<expect label="produces reference to expected QName"
test="f:QName( $x:result ) = $qname" />
</scenario>
<scenario label="produces a valid dynamic function reference">
<variable name="arg">
<foo:bar />
</variable>
<call function="f:apply">
<param name="fnref"
select="f:make-ref( QName( $foo-uri, 'fn1' ),
1 )" />
<param name="arg1" select="$arg" />
</call>
<expect label="that can be applied using f:apply"
test="$x:result[ 1 ] = foo:applied[ @n = 1 ]
and $x:result[ 2 ] is $arg" />
</scenario>
</scenario>
<scenario label="f:is-ref predicate">
<scenario label="given a valid dynamic function reference">
<call function="f:is-ref">
<param name="fnref"
select="f:make-ref( QName( $foo-uri, 'foo' ), 0 )" />
</call>
<expect label="returns true()"
select="true()" />
</scenario>
<scenario label="given an invalid dynamic function reference">
<call function="f:is-ref">
<param name="fnref">
<foo:bar />
</param>
</call>
<expect label="returns false()"
select="false()" />
</scenario>
<scenario label="given a dynamic function reference without arity">
<call function="f:is-ref">
<param name="fnref">
<f:ref>
<foo:bar />
</f:ref>
</param>
</call>
<expect label="returns false()"
select="false()" />
</scenario>
<scenario label="given a dynamic function reference without
integral arity">
<call function="f:is-ref">
<param name="fnref">
<f:ref arity="moo">
<foo:bar />
</f:ref>
</param>
</call>
<expect label="returns false()"
select="false()" />
</scenario>
<scenario label="given a dynamic function reference without target
node">
<call function="f:is-ref">
<param name="fnref">
<f:ref arity="0" />
</param>
</call>
<expect label="returns false()"
select="false()" />
</scenario>
<scenario label="given an empty sequence">
<call function="f:is-ref">
<param name="fnref" select="()" />
</call>
<expect label="returns false()"
select="false()" />
</scenario>
<scenario label="given a non-element">
<call function="f:is-ref">
<param name="fnref" select="5" />
</call>
<expect label="returns false()"
select="false()" />
</scenario>
</scenario>
<scenario label="f:QName accessor">
<variable name="foo-qname"
select="QName( $foo-uri, 'foo' )" />
<scenario label="given a valid dynamic function reference with no
argments">
<call function="f:QName">
<param name="fnref"
select="f:make-ref( $foo-qname, 0 )" />
</call>
<expect label="returns QName"
test="$x:result instance of xs:QName" />
<expect label="returns QName of target dynamic function"
select="$foo-qname" />
</scenario>
<scenario label="given a valid dynamic function reference with
argments">
<call function="f:QName">
<param name="fnref"
select="f:set-args( f:make-ref( $foo-qname, 0 ),
(1, 2) )" />
</call>
<expect label="returns QName"
test="$x:result instance of xs:QName" />
<expect label="returns QName of target dynamic function"
select="$foo-qname" />
</scenario>
<scenario label="given a dynamic function reference with no target
node">
<call function="f:QName">
<param name="fnref">
<f:ref arity="5" />
</param>
</call>
<expect label="returns empty sequence"
select="()" />
</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 -->
<call function="f:arity">
<param name="fnref" select="$test-fn" />
</call>
<expect label="provides function arity"
select="$test-arity" />
</scenario>
<scenario label="given a partially applied function reference">
<call function="f:arity">
<param name="fnref"
select="f:apply( $test-fn, 1, 2 )" />
</call>
<expect label="provides arity of the target function minus the
number of bound parameters"
select="$test-arity - 2" />
</scenario>
<scenario label="given a multiply-partially-applied function reference">
<call function="f:arity">
<param name="fnref"
select="f:apply(
f:apply( $test-fn, 1, 2 ),
3 )" />
</call>
<expect label="provides arity of the target function minus the
number of bound parameters"
select="$test-arity - 3" />
</scenario>
</scenario>
<scenario label="f:set-args setter">
<scenario label="setting empty args on reference with no args">
<call function="f:set-args">
<param name="fnref" select="$test-fn" />
<param name="args" select="()" />
</call>
<expect label="retains target QName"
test="f:QName( $x:result ) = f:QName( $test-fn )" />
<expect label="retains target arity"
test="f:arity( $x:result ) = f:arity( $test-fn )" />
</scenario>
<scenario label="setting args on reference with no args">
<call function="f:set-args">
<param name="fnref" select="$test-fn" />
<param name="args" select="(1, 2)" />
</call>
<expect label="retains target QName"
test="f:QName( $x:result ) = f:QName( $test-fn )" />
<!-- the function is now partially applied -->
<expect label="reduces arity by number of arguments"
test="f:arity( $x:result ) = f:arity( $test-fn ) - 2" />
</scenario>
<scenario label="clearing args on reference with args">
<call function="f:set-args">
<param name="fnref"
select="f:set-args( $test-fn, (1, 2) )" />
<param name="args"
select="()" />
</call>
<expect label="retains target QName"
test="f:QName( $x:result ) = f:QName( $test-fn )" />
<!-- the function is no longer partially applied -->
<expect label="restores target function arity"
test="f:arity( $x:result ) = f:arity( $test-fn )" />
</scenario>
<scenario label="increasing reference argument count">
<call function="f:set-args">
<param name="fnref"
select="f:set-args( $test-fn, (1, 2) )" />
<param name="args"
select="(1, 2, 3)" />
</call>
<expect label="retains target QName"
test="f:QName( $x:result ) = f:QName( $test-fn )" />
<!-- the function is no longer partially applied -->
<expect label="reduces arity by number of argments"
test="f:arity( $x:result ) = f:arity( $test-fn ) - 3" />
</scenario>
<scenario label="decrasing reference argument count">
<call function="f:set-args">
<param name="fnref"
select="f:set-args( $test-fn, (1, 2) )" />
<param name="args"
select="1" />
</call>
<expect label="retains target QName"
test="f:QName( $x:result ) = f:QName( $test-fn )" />
<!-- the function is no longer partially applied -->
<expect label="increases arity by difference in argument count"
test="f:arity( $x:result ) = f:arity( $test-fn ) - 1" />
</scenario>
</scenario>
<scenario label="f:args accessor">
<scenario label="given a new dynamic function reference">
<call function="f:args">
<param name="fnref" select="$test-fn" />
</call>
<expect label="produces empty argument list"
select="()" />
</scenario>
<scenario label="after an initial f:set-args">
<call function="f:args">
<param name="fnref"
select="f:set-args( $test-fn, ($args/foo:a,
$args/foo:b) )" />
</call>
<expect label="returns sequence of arguments by reference"
test="$x:result[ 1 ] is $args/foo:a
and $x:result[ 2 ] is $args/foo:b" />
<expect label="returns no other items"
test="count( $x:result ) = 2" />
</scenario>
<scenario label="after a f:set-args on an existing argument list">
<call function="f:args">
<param name="fnref"
select="f:set-args(
f:set-args( $test-fn, ($args/foo:a,
$args/foo:b) ),
($args/foo:b,
$args/foo:c) )" />
</call>
<expect label="returns sequence of arguments by reference,
discarding previously set arguments"
test="$x:result[ 1 ] is $args/foo:b
and $x:result[ 2 ] is $args/foo:c" />
<expect label="returns no other items"
test="count( $x:result ) = 2" />
</scenario>
</scenario>
<scenario label="f:push-args mutator">
<scenario label="pushing arguments onto empty reference list">
<call function="f:push-args">
<param name="fnref"
select="$test-fn" />
<param name="args"
select="$args/foo:a,
$args/foo:b" />
</call>
<expect label="retains target QName"
test="f:QName( $x:result ) = f:QName( $test-fn )" />
<expect label="reduces arity by number of arguments, relative to
target function"
test="f:arity( $x:result ) = f:arity( $test-fn ) - 2" />
<expect label="sets arguments by reference"
test="f:args( $x:result )[ 1 ] is $args/foo:a
and f:args( $x:result )[ 2 ] is $args/foo:b" />
</scenario>
<scenario label="pushing arguments onto populated reference
list">
<call function="f:push-args">
<param name="fnref"
select="f:set-args( $test-fn,
($args/foo:a,
$args/foo:b) )" />
<param name="args"
select="$args/foo:c,
$args/foo:d" />
</call>
<expect label="retains target QName"
test="f:QName( $x:result ) = f:QName( $test-fn )" />
<expect label="reduces arity by number of arguments, relative to
previous arity"
test="f:arity( $x:result ) = f:arity( $test-fn ) - 4" />
<expect label="retains previous arguments by reference"
test="f:args( $x:result )[ 1 ] is $args/foo:a
and f:args( $x:result )[ 2 ] is $args/foo:b" />
<expect label="pushes new arguments by reference"
test="f:args( $x:result )[ 3 ] is $args/foo:c
and f:args( $x:result )[ 4 ] is $args/foo:d" />
</scenario>
<scenario label="pushing empty argument list onto populated
reference list">
<call function="f:push-args">
<param name="fnref"
select="f:set-args( $test-fn,
($args/foo:a,
$args/foo:b) )" />
<param name="args" select="()" />
</call>
<expect label="retains target QName"
test="f:QName( $x:result ) = f:QName( $test-fn )" />
<expect label="retains previous reference arity"
test="f:arity( $x:result ) = f:arity( $test-fn ) - 2" />
<expect label="retains previous arguments by reference"
test="f:args( $x:result )[ 1 ] is $args/foo:a
and f:args( $x:result )[ 2 ] is $args/foo:b" />
</scenario>
</scenario>
<scenario label="f:unshift-args mutator">
<scenario label="unshifting arguments onto empty reference list">
<call function="f:unshift-args">
<param name="fnref"
select="$test-fn" />
<param name="args"
select="$args/foo:a,
$args/foo:b" />
</call>
<expect label="retains target QName"
test="f:QName( $x:result ) = f:QName( $test-fn )" />
<expect label="reduces arity by number of arguments, relative to
target function"
test="f:arity( $x:result ) = f:arity( $test-fn ) - 2" />
<expect label="sets arguments by reference"
test="f:args( $x:result )[ 1 ] is $args/foo:a
and f:args( $x:result )[ 2 ] is $args/foo:b" />
</scenario>
<scenario label="unshifting arguments onto populated reference
list">
<call function="f:unshift-args">
<param name="fnref"
select="f:set-args( $test-fn,
($args/foo:a,
$args/foo:b) )" />
<param name="args"
select="$args/foo:c,
$args/foo:d" />
</call>
<expect label="retains target QName"
test="f:QName( $x:result ) = f:QName( $test-fn )" />
<expect label="reduces arity by number of arguments, relative to
previous arity"
test="f:arity( $x:result ) = f:arity( $test-fn ) - 4" />
<expect label="retains previous arguments by reference"
test="f:args( $x:result )[ 3 ] is $args/foo:a
and f:args( $x:result )[ 4 ] is $args/foo:b" />
<expect label="unshifts new arguments by reference"
test="f:args( $x:result )[ 1 ] is $args/foo:c
and f:args( $x:result )[ 2 ] is $args/foo:d" />
</scenario>
<scenario label="unshifting empty argument list onto populated
reference list">
<call function="f:unshift-args">
<param name="fnref"
select="f:set-args( $test-fn,
($args/foo:a,
$args/foo:b) )" />
<param name="args" select="()" />
</call>
<expect label="retains target QName"
test="f:QName( $x:result ) = f:QName( $test-fn )" />
<expect label="retains previous reference arity"
test="f:arity( $x:result ) = f:arity( $test-fn ) - 2" />
<expect label="retains previous arguments by reference"
test="f:args( $x:result )[ 1 ] is $args/foo:a
and f:args( $x:result )[ 2 ] is $args/foo:b" />
</scenario>
</scenario>
</description>

View File

@ -184,13 +184,39 @@
<!-- 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>
<scenario label="constructed dynamic function reference">
<variable name="two-result"
select="foo:sub-two()" />
<expect label="is able to determine arity"
test="$x:result = 2" />
<scenario label="is recognized as">
<call function="f:is-ref">
<param name="fnref" select="$two-result" />
</call>
<expect label="a dynamic function reference"
select="true()" />
</scenario>
<scenario label="target QName">
<call function="f:QName">
<param name="fnref" select="$two-result" />
</call>
<expect label="is the QName of the target function"
select="QName( 'http://www.lovullo.com/_junk',
'sub-two' )" />
</scenario>
<scenario label="arity">
<call function="f:arity">
<param name="fnref" select="$two-result" />
</call>
<expect label="is the arity of target function"
select="2" />
</scenario>
</scenario>
</scenario>
</description>