Skip to content

Commit

Permalink
Sample code for "Testing an XPath Function Library with XSpec 3.0"
Browse files Browse the repository at this point in the history
  • Loading branch information
galtm committed Apr 24, 2024
1 parent 5ec8df6 commit 0cca0c1
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ Each post corresponds to a [subfolder under `src/`](https://github.com/galtm/xsp
* [Saying "Not Yet" in XSpec](https://github.com/galtm/xspectacles/tree/main/src/pending)
* [Saying "Whatever" in XSpec: How, Why, When](https://github.com/galtm/xspectacles/tree/main/src/three-dots)
* [Testing a Thin-Wrapper XSLT Template Without Excessive Repetition](https://github.com/galtm/xspectacles/tree/main/src/similar-code-wrapper)
* **(NEW!)** [Testing an XPath Function Library with XSpec 3.0](https://github.com/galtm/xspectacles/tree/main/src/xpath-function-library)
* [Testing Boolean-Valued Functions: An XSpec Blooper with Easy Fixes](https://github.com/galtm/xspectacles/tree/main/src/boolean-fcn)
* [Testing Explicit Whitespace in XSpec](https://github.com/galtm/xspectacles/tree/main/src/space-explicit)
* **(NEW!)** [Testing Schematron Messages with XSpec 3.0](https://github.com/galtm/xspectacles/tree/main/src/schematron-messages)
* [Testing Schematron Messages with XSpec 3.0](https://github.com/galtm/xspectacles/tree/main/src/schematron-messages)
* [Testing Preformatted or Codelike Text in XSpec](https://github.com/galtm/xspectacles/tree/main/src/space-preformatted)
* [Testing Schematron Patterns in Isolation](https://github.com/galtm/xspectacles/tree/main/src/isolated-patterns)
* [The Equality Check that's Neither True Nor False](https://github.com/galtm/xspectacles/tree/main/src/non-boolean-eq)
Expand Down
9 changes: 9 additions & 0 deletions src/xpath-function-library/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Sample code for "Testing an XPath Function Library with XSpec 3.0"

Example files:

* `SimpleLibraryAsMap.xpath`
* `test-for-xpath-function-library.xspec`

#### Link to Topic
[Testing an XPath Function Library with XSpec 3.0](https://medium.com/@xspectacles/testing-an-xpath-function-library-with-xspec-3-0-1f324b0837cb)
33 changes: 33 additions & 0 deletions src/xpath-function-library/SimpleLibraryAsMap.xpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
xquery version "3.1";
module namespace SL = "urn:x-xspectacles:functions:simple-library";

declare variable $SL:vMath as map(*) :=
let
$incr := function($n as xs:integer)
{$n +1},
$mult := function($m as xs:integer, $n as xs:integer)
{$m * $n},
$decr := function($n as xs:integer)
{$n -1},
$idiv := function($m as xs:integer, $n as xs:integer)
{$m idiv $n}

(: Provide the function library as a map :)
return
map {
'incr' : $incr,
'mult' : $mult,
'decr' : $decr,
'idiv' : $idiv
}
;

(:
This code is adapted from the following paper, with the module
namespace declaration and variable declaration added as the
paper suggests.

Novatchev, Dimitre. “Programming in XPath 3.0.” Presented at Balisage: The Markup Conference 2013,
Montréal, Canada, August 6 - 9, 2013. In Proceedings of Balisage: The Markup Conference 2013.
Balisage Series on Markup Technologies, vol. 10 (2013). https://doi.org/10.4242/BalisageVol10.Novatchev01.
:)
47 changes: 47 additions & 0 deletions src/xpath-function-library/test-for-xpath-function-library.xspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<x:description
query="urn:x-xspectacles:functions:simple-library"
query-at="SimpleLibraryAsMap.xpath"
xmlns:x="http://www.jenitennison.com/xslt/xspec"
xmlns:SL="urn:x-xspectacles:functions:simple-library">

<!--
Sample Code for "Testing an XPath Function Library with XSpec 3.0"
https://medium.com/@xspectacles/testing-an-xpath-function-library-with-xspec-3-0-1f324b0837cb
-->

<x:scenario label="incr function">
<x:call function="SL:vMath" call-as="variable">
<x:param select="'incr'"/>
</x:call>
<x:expect label="$x:result is a function with one integer parameter"
test="$x:result instance of function(xs:integer) as item()*"/>
<x:expect label="Incrementing 4 produces 5"
test="$x:result(4)"
select="5"/>
<x:scenario label="Alternative syntax, less useful in case of failure">
<x:expect label="Incrementing 4 produces 5"
test="$x:result(4) eq 5"/>
</x:scenario>
</x:scenario>
<x:scenario label="mult function">
<x:call function="SL:vMath" call-as="variable">
<x:param select="'mult'"/>
</x:call>
<x:expect label="$x:result is a function with two integer parameters"
test="$x:result instance of function(xs:integer, xs:integer) as item()*"/>
<x:expect label="4 times 2 equals 8"
test="$x:result(4,2)"
select="8"/>
</x:scenario>

<x:scenario label="Incorrect syntax" pending="Error">
<x:call function="SL:vMath('incr')" call-as="variable">
<x:param select="4"/>
</x:call>
<x:expect label="Incrementing 4 produces 5"
select="5"/>
</x:scenario>
</x:description>

<!-- Copyright © 2024 by Amanda Galtman. -->

0 comments on commit 0cca0c1

Please sign in to comment.