Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample code for "Comparing Attributes Selectively in XSpec 3.0" #41

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Each post corresponds to a [subfolder under `src/`](https://github.com/galtm/xsp
* [But the Diffs Look the Same](https://github.com/galtm/xspectacles/tree/main/src/identical-diffs)
* [Code Reuse at the File Level in XSpec](https://github.com/galtm/xspectacles/tree/main/src/code-reuse-file-level)
* [Code Reuse in XSpec: How to Use x:like](https://github.com/galtm/xspectacles/tree/main/src/code-reuse)
* **(NEW!)** [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)
* [Inheritance as a Form of Reuse in XSpec](https://github.com/galtm/xspectacles/tree/main/src/code-reuse-call)
Expand All @@ -20,7 +21,7 @@ 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 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)
* [Testing Schematron Messages with XSpec 3.0](https://github.com/galtm/xspectacles/tree/main/src/schematron-messages)
Expand Down
15 changes: 15 additions & 0 deletions src/three-dots-attrs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Sample code for "Comparing Attributes Selectively in XSpec 3.0"

Example files for "Example Comparing Attributes in Stages":

* `three-dots-attrs.xqm`
* `three-dots-attrs-xq.xspec`
* `three-dots-attrs-prefix-variant-xq.xspec`

Example files for "Example Skipping Comparison of Schematron SVRL Attributes":

* `three-dots-attrs.sch`
* `three-dots-attrs-sch.xspec`

#### Link to Topic
[Comparing Attributes Selectively in XSpec 3.0](https://medium.com/@xspectacles/comparing-attributes-selectively-in-xspec-3-0-f3a457fe8e04)
119 changes: 119 additions & 0 deletions src/three-dots-attrs/three-dots-attrs-prefix-variant-xq.xspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?xml version="1.0" encoding="UTF-8"?>
<description
query="x-urn:my-xq"
query-at="three-dots-attrs.xqm"
xmlns="http://www.jenitennison.com/xslt/xspec"
xmlns:h="http://www.w3.org/1999/xhtml"
xmlns:my-xq="x-urn:my-xq">

<!--
Sample code for "Comparing Attributes Selectively in XSpec 3.0"
https://medium.com/@xspectacles/comparing-attributes-selectively-in-xspec-3-0-f3a457fe8e04
-->

<!-- This code is not explicitly in the topic but
shows how the x:attrs="..." technique would work
in a situation with different usage of namespace
prefixes. In this file, the XSpec namespace is
the default element namespace. -->

<!-- Verifying All Attributes at Once -->
<scenario label="Check all attributes at once">
<call function="my-xq:spaceship-img"/>
<expect>
<label>img element with class, src, onclick,
and data attributes representing ship ID,
weapons, shields, and coordinates</label>
<h:img
class="spaceship cruiserX3"
src="shipX3.png"
onclick="spaceships[this.dataset.shipId].blasted()"
data-ship-id="324"
data-weapons="laserI laserII"
data-shields="72%"
data-x="414354"
data-y="85160"
data-z="31940"/>
</expect>
</scenario>

<!-- XSpec 2.x, Verifying Attributes in Stages -->
<scenario label="Check attributes in groups, XSpec 2.x">
<call function="my-xq:spaceship-img"/>
<expect label="Correct CSS class">
<h:img
class="spaceship cruiserX3"
src="..."
onclick="..."
data-ship-id="..."
data-weapons="..."
data-shields="..."
data-x="..."
data-y="..."
data-z="..."/>
</expect>
<!-- And other <expect> elements where <h:img> always
names all the attributes -->
</scenario>

<!-- XSpec 3.0, Verifying Attributes in Stages -->
<scenario label="Check attributes in groups"
xmlns:xsp="http://www.jenitennison.com/xslt/xspec">
<call function="my-xq:spaceship-img"/>
<expect label="Correct CSS class">
<h:img class="spaceship cruiserX3"
xsp:attrs="..."/>
</expect>
<expect label="Correct PNG file">
<h:img src="shipX3.png"
xsp:attrs="..."/>
</expect>
<expect label="Correct ship ID">
<h:img data-ship-id="324"
xsp:attrs="..."/>
</expect>
<expect label="Correct weapons and shields">
<h:img data-weapons="laserI laserII"
data-shields="72%"
xsp:attrs="..."/>
</expect>
<expect label="Correct coordinates">
<h:img data-x="414354"
data-y="85160"
data-z="31940"
xsp:attrs="..."/>
</expect>
<expect label="Correct click callback">
<h:img onclick="spaceships[this.dataset.shipId].blasted()"
xsp:attrs="..."/>
</expect>
<!-- $xsp:result in a test for XQuery would require the declaration
xmlns:xsp="http://www.jenitennison.com/xslt/xspec"
on the XSpec <description> element. An alternative shown here is:
$Q{http://www.jenitennison.com/xslt/xspec}result -->
<expect label="No unexpected attributes"
test="sort($Q{http://www.jenitennison.com/xslt/xspec}result/@*/name())"
select="sort(
('class', 'src', 'data-ship-id', 'data-weapons', 'data-shields',
'data-x', 'data-y', 'data-z', 'onclick')
)"
/>
</scenario>

<!-- Three Dots Can Match Nothing -->
<scenario label="Three dots in absence of attributes">
<call function="my-xq:heading">
<param>Heading Content</param>
</call>
<expect label="h1 with class attribute">
<h:h1 class="main">Heading Content</h:h1>
</expect>
<expect label="h1 with class attribute and possibly others"
xmlns:xsp="http://www.jenitennison.com/xslt/xspec">
<h:h1 class="main" xsp:attrs="...">Heading Content</h:h1>
</expect>
</scenario>

</description>

<!-- Copyright © 2024 by Amanda Galtman. -->
30 changes: 30 additions & 0 deletions src/three-dots-attrs/three-dots-attrs-sch.xspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<x:description
schematron="three-dots-attrs.sch"
xmlns:svrl="http://purl.oclc.org/dsdl/svrl"
xmlns:x="http://www.jenitennison.com/xslt/xspec">

<!--
Sample code for "Comparing Attributes Selectively in XSpec 3.0"
https://medium.com/@xspectacles/comparing-attributes-selectively-in-xspec-3-0-f3a457fe8e04
-->

<x:param name="phase">phase1</x:param>

<!-- Example Skipping Comparison of Schematron SVRL Attributes -->
<x:scenario label="Check for active pattern">
<x:context>
<any-element/>
</x:context>
<x:expect label="pattern1 is the only active one (XSpec 3.0)"
test="/svrl:schematron-output/svrl:active-pattern">
<svrl:active-pattern id="pattern1" x:attrs="..."/>
</x:expect>
<x:expect label="pattern1 is the only active one"
test="exactly-one(/svrl:schematron-output/svrl:active-pattern)/@id/string()"
select="'pattern1'"/>
</x:scenario>

</x:description>

<!-- Copyright © 2024 by Amanda Galtman. -->
102 changes: 102 additions & 0 deletions src/three-dots-attrs/three-dots-attrs-xq.xspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?xml version="1.0" encoding="UTF-8"?>
<x:description
query="x-urn:my-xq"
query-at="three-dots-attrs.xqm"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:my-xq="x-urn:my-xq"
xmlns:x="http://www.jenitennison.com/xslt/xspec"
xmlns:xs="http://www.w3.org/2001/XMLSchema">

<!--
Sample code for "Comparing Attributes Selectively in XSpec 3.0"
https://medium.com/@xspectacles/comparing-attributes-selectively-in-xspec-3-0-f3a457fe8e04
-->

<!-- Verifying All Attributes at Once -->
<x:scenario label="Check all attributes at once">
<x:call function="my-xq:spaceship-img"/>
<x:expect>
<x:label>img element with class, src, onclick,
and data attributes representing ship ID,
weapons, shields, and coordinates</x:label>
<img xmlns="http://www.w3.org/1999/xhtml"
class="spaceship cruiserX3"
src="shipX3.png"
onclick="spaceships[this.dataset.shipId].blasted()"
data-ship-id="324"
data-weapons="laserI laserII"
data-shields="72%"
data-x="414354"
data-y="85160"
data-z="31940"/>
</x:expect>
</x:scenario>

<!-- XSpec 2.x, Verifying Attributes in Stages -->
<x:scenario label="Check attributes in groups, XSpec 2.x"
xmlns="http://www.w3.org/1999/xhtml">
<x:call function="my-xq:spaceship-img"/>
<x:expect label="Correct CSS class">
<img
class="spaceship cruiserX3"
src="..."
onclick="..."
data-ship-id="..."
data-weapons="..."
data-shields="..."
data-x="..."
data-y="..."
data-z="..."/>
</x:expect>
<!-- And other <x:expect> elements where <img> always
names all the attributes -->
</x:scenario>

<!-- XSpec 3.0, Verifying Attributes in Stages -->
<x:scenario label="Check attributes in groups, XSpec 3.0"
xmlns="http://www.w3.org/1999/xhtml">
<x:call function="my-xq:spaceship-img"/>
<x:expect label="Correct CSS class">
<img class="spaceship cruiserX3" x:attrs="..."/>
</x:expect>
<x:expect label="Correct PNG file">
<img src="shipX3.png" x:attrs="..."/>
</x:expect>
<x:expect label="Correct ship ID">
<img data-ship-id="324" x:attrs="..."/>
</x:expect>
<x:expect label="Correct weapons and shields">
<img data-weapons="laserI laserII" data-shields="72%" x:attrs="..."/>
</x:expect>
<x:expect label="Correct coordinates">
<img data-x="414354" data-y="85160" data-z="31940" x:attrs="..."/>
</x:expect>
<x:expect label="Correct click callback">
<img onclick="spaceships[this.dataset.shipId].blasted()"
x:attrs="..."/>
</x:expect>
<x:expect label="No unexpected attributes"
test="sort($x:result/@*/name())"
select="sort(
('class', 'src', 'data-ship-id', 'data-weapons', 'data-shields',
'data-x', 'data-y', 'data-z', 'onclick')
)"
/>
</x:scenario>

<!-- Three Dots Can Match Nothing -->
<x:scenario label="Three dots in absence of attributes">
<x:call function="my-xq:heading">
<x:param>Heading Content</x:param>
</x:call>
<x:expect label="h1 with class attribute">
<h1 class="main">Heading Content</h1>
</x:expect>
<x:expect label="h1 with class attribute and possibly others">
<h1 class="main" x:attrs="...">Heading Content</h1>
</x:expect>
</x:scenario>

</x:description>

<!-- Copyright © 2024 by Amanda Galtman. -->
30 changes: 30 additions & 0 deletions src/three-dots-attrs/three-dots-attrs.sch
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt2"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!--
Sample code for "Comparing Attributes Selectively in XSpec 3.0"
https://medium.com/@xspectacles/comparing-attributes-selectively-in-xspec-3-0-f3a457fe8e04
-->

<sch:phase id="phase1">
<sch:active pattern="pattern1"/>
</sch:phase>

<sch:pattern id="pattern1">
<sch:rule context="//glossdef" id="definition">
<sch:assert test="normalize-space(.) != ''"
role="error"
id="text">glossdef must have text</sch:assert>
</sch:rule>
</sch:pattern>

<sch:pattern id="pattern2">
<sch:rule context="//something">
<!-- sch:assert and sch:report would go here -->
</sch:rule>
</sch:pattern>

</sch:schema>

<!-- Copyright © 2024 by Amanda Galtman. -->
29 changes: 29 additions & 0 deletions src/three-dots-attrs/three-dots-attrs.xqm
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
xquery version "3.1";
module namespace my-xq = "x-urn:my-xq";
declare default element namespace "http://www.w3.org/1999/xhtml";

(:
Sample code for "Comparing Attributes Selectively in XSpec 3.0"
https://medium.com/@xspectacles/comparing-attributes-selectively-in-xspec-3-0-f3a457fe8e04
:)

(:
The HTML <img> element with data attributes is taken from
https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/data-*
:)
declare function my-xq:spaceship-img() as element(img) {
<img
class="spaceship cruiserX3"
src="shipX3.png"
data-ship-id="324"
data-weapons="laserI laserII"
data-shields="72%"
data-x="414354"
data-y="85160"
data-z="31940"
onclick="spaceships[this.dataset.shipId].blasted()"/>
};

declare function my-xq:heading($content as node()) as element(h1) {
<h1 class="main">{$content}</h1>
};