Skip to content

Commit

Permalink
Sample files for "XQuery Makeover to Improve Testability with XSpec" (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
galtm authored Jul 9, 2024
1 parent 5d5dba7 commit d787837
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Each post corresponds to a [subfolder under `src/`](https://github.com/galtm/xsp
* [Comparing Attributes Selectively in XSpec 3.0](https://github.com/galtm/xspectacles/tree/main/src/three-dots-attrs)
* [Content Outside the Selection in XSpec](https://github.com/galtm/xspectacles/tree/main/src/out-of-scope)
* [Fixing Surprise Spaces in XSpec Actual Result](https://github.com/galtm/xspectacles/tree/main/src/space-actual)
* **(NEW!)** [How to Test Error Handling in XSpec](https://github.com/galtm/xspectacles/tree/main/src/catch-error)
* [How to Test Error Handling in XSpec](https://github.com/galtm/xspectacles/tree/main/src/catch-error)
* [Inheritance as a Form of Reuse in XSpec](https://github.com/galtm/xspectacles/tree/main/src/code-reuse-call)
* [Multiple Cases in One XSpec Scenario](https://github.com/galtm/xspectacles/tree/main/src/context-sequence)
* [My XML Content in XSpec Causes an Error](https://github.com/galtm/xspectacles/tree/main/src/xml-content-error)
Expand All @@ -32,3 +32,4 @@ Each post corresponds to a [subfolder under `src/`](https://github.com/galtm/xsp
* [Text Nodes and Locations in XSpec Tests for Schematron](https://github.com/galtm/xspectacles/tree/main/src/schxslt-in-xspec)
* [The Equality Check that's Neither True Nor False](https://github.com/galtm/xspectacles/tree/main/src/non-boolean-eq)
* [Ways to Access XSLT Result in XSpec Testing: Dot Versus Result Variable](https://github.com/galtm/xspectacles/tree/main/src/dot-versus-result)
* **(NEW!)** [XQuery Makeover to Improve Testability with XSpec](https://github.com/galtm/xspectacles/tree/main/src/xquery-modules)
18 changes: 18 additions & 0 deletions src/xquery-modules/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Sample Code for "XQuery Makeover to Improve Testability with XSpec"

Example files before refactoring:

- `main.xqm`
- `data.xml`

Example files after refactoring:

- `main-refactored.xqm`
- `library.xqm`
- `library.xspec`
- `data.xml`


#### Link to Topic

[XQuery Makeover to Improve Testability with XSpec](https://medium.com/@xspectacles/xquery-makeover-to-improve-testability-with-xspec-dfd36432c3c7)
7 changes: 7 additions & 0 deletions src/xquery-modules/data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<root>
<num>6789</num>
<num>12345</num>
<num>1000</num>
<num>200</num>
</root>
19 changes: 19 additions & 0 deletions src/xquery-modules/library.xqm
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(:
Sample code for "XQuery Makeover to Improve Testability with XSpec" section
https://medium.com/@xspectacles/xquery-makeover-to-improve-testability-with-xspec-dfd36432c3c7
:)

xquery version "3.1";
module namespace f = "urn:x-xspec-book:functions:xquery-modules";
declare decimal-format spaced grouping-separator = " ";

declare function f:filter-round(
$context as document-node(),
$min as xs:integer
) as xs:string* {
for $n in $context//num/number()[. ge $min]
return
format-number($n, '### ###', 'spaced')
};

(: Copyright © 2024 by Amanda Galtman. :)
27 changes: 27 additions & 0 deletions src/xquery-modules/library.xspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<x:description xmlns:f="urn:x-xspec-book:functions:xquery-modules"
xmlns:x="http://www.jenitennison.com/xslt/xspec"
query="urn:x-xspec-book:functions:xquery-modules"
query-at="library.xqm">
<!--
Sample code for "XQuery Makeover to Improve Testability with XSpec" section
https://medium.com/@xspectacles/xquery-makeover-to-improve-testability-with-xspec-dfd36432c3c7
-->

<x:scenario label="Test 'f:filter-round' function">
<x:call function="f:filter-round">
<x:param select="/">
<root>
<num>1000</num>
<num>5000</num>
<num>500</num>
<num>2000.75</num>
</root>
</x:param>
<x:param>1000</x:param>
</x:call>
<x:expect label="Numbers at least 1000, in expected format"
select="('1 000', '5 000', '2 001')"/>
</x:scenario>
</x:description>
<!-- Copyright © 2024 by Amanda Galtman. -->
25 changes: 25 additions & 0 deletions src/xquery-modules/main-refactored.xqm
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(:
Sample code for "XQuery Makeover to Improve Testability with XSpec" section
https://medium.com/@xspectacles/xquery-makeover-to-improve-testability-with-xspec-dfd36432c3c7
:)

xquery version "3.1";
import module namespace f = "urn:x-xspec-book:functions:xquery-modules"
at "library.xqm";
declare namespace output =
"http://www.w3.org/2010/xslt-xquery-serialization";
declare context item as document-node() external := document {
<root>
<num>1000</num>
<num>5000</num>
<num>500</num>
<num>2000.75</num>
</root>
};
declare variable $min as xs:integer external := 1000;
declare option output:method "text";

(: Query body :)
f:filter-round(., $min)

(: Copyright © 2024 by Amanda Galtman. :)
26 changes: 26 additions & 0 deletions src/xquery-modules/main.xqm
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
(:
Sample code for "XQuery Makeover to Improve Testability with XSpec" section
https://medium.com/@xspectacles/xquery-makeover-to-improve-testability-with-xspec-dfd36432c3c7
:)

xquery version "3.1";
declare namespace output =
"http://www.w3.org/2010/xslt-xquery-serialization";
declare decimal-format spaced grouping-separator = " ";
declare context item as document-node() external := document {
<root>
<num>1000</num>
<num>5000</num>
<num>500</num>
<num>2000.75</num>
</root>
};
declare variable $min as xs:integer external := 1000;
declare option output:method "text";

(: Query body :)
for $n in //num/number()[. ge $min]
return
format-number($n, '### ###', 'spaced')

(: Copyright © 2024 by Amanda Galtman. :)

0 comments on commit d787837

Please sign in to comment.