Skip to content

Commit

Permalink
Tweak code for Part 2 of "Ignoring Code Comments During XSpec Testing"
Browse files Browse the repository at this point in the history
  • Loading branch information
galtm committed Oct 2, 2024
1 parent 29bcee9 commit c3004e5
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 38 deletions.
6 changes: 4 additions & 2 deletions src/helper-comments/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ Example files for XQuery:
- `helper-remove-comments-xq.xspec`


#### Link to Topic
[Ignoring Code Comments During XSpec Testing](https://medium.com/@xspectacles/ignoring-code-comments-during-xspec-testing-a460f35a25bb)
#### Links to Topics

- [Ignoring Code Comments During XSpec Testing, Part 1](https://medium.com/@xspectacles/ignoring-code-comments-during-xspec-testing-a460f35a25bb)
- [Ignoring Code Comments During XSpec Testing, Part 2](https://medium.com/@xspectacles/ignoring-code-comments-during-xspec-testing-part-2-8266ee8ceccc)
4 changes: 2 additions & 2 deletions src/helper-comments/helper-remove-comments-xq.xspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
xmlns:x="http://www.jenitennison.com/xslt/xspec">

<!--
Sample Code for "Ignoring Code Comments During XSpec Testing"
https://medium.com/@xspectacles/ignoring-code-comments-during-xspec-testing-a460f35a25bb
Sample Code for "Ignoring Code Comments During XSpec Testing, Part 2"
https://medium.com/@xspectacles/ignoring-code-comments-during-xspec-testing-part-2-8266ee8ceccc
-->

<x:helper query="urn:x-xspectacles:functions:helper:remove-comments"
Expand Down
60 changes: 32 additions & 28 deletions src/helper-comments/helper-remove-comments.xqm
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
xquery version "3.1";
xquery version "1.0";
module namespace frc = "urn:x-xspectacles:functions:helper:remove-comments";

(:
Sample Code for "Ignoring Code Comments During XSpec Testing"
https://medium.com/@xspectacles/ignoring-code-comments-during-xspec-testing-a460f35a25bb
Sample Code for "Ignoring Code Comments During XSpec Testing, Part 2"
https://medium.com/@xspectacles/ignoring-code-comments-during-xspec-testing-part-2-8266ee8ceccc
:)

declare variable $frc:prefix as xs:string := 'TEST NOTE:';

(:
Removes certain comments from $node, assumed to be an element,
Remove certain comments from $node, assumed to be an element,
document node, or empty sequence. The comments to remove are those
whose space-normalized content starts with 'TEST NOTE:'.
:)
declare %public function frc:remove-comments(
declare function frc:remove-comments(
$node as node()?
) {
) as node()? {
typeswitch ($node)
case comment()
return
Expand All @@ -30,21 +31,19 @@ $node as node()?
$node
};

declare %private function frc:element-handler(
$node as element()
) as element() {
element {node-name($node)} {
for $n in in-scope-prefixes($node)
return
namespace {$n} {namespace-uri-for-prefix($n, $node)},
$node/@*,
for $child in $node/node()
return
frc:remove-comments($child)
}
(: Discard comment nodes that start with specified prefix. :)
declare function frc:comment-handler(
$node as comment()
) as comment()? {
if (starts-with(normalize-space($node), $frc:prefix))
then
()
else
$node
};

declare %private function frc:doc-node-handler(
(: Create document node and process children. :)
declare function frc:doc-node-handler(
$node as document-node()
) as document-node() {
document {
Expand All @@ -54,14 +53,19 @@ $node as document-node()
}
};

declare %private function frc:comment-handler(
$node as comment()
) as comment()? {
if (starts-with(normalize-space($node), $frc:prefix))
then
()
else
$node
(: Create element wrapper and process children. :)
declare function frc:element-handler(
$node as element()
) as element() {
element {node-name($node)} {
for $n in in-scope-prefixes($node)
return
namespace {$n} {namespace-uri-for-prefix($n, $node)},
$node/@*,
for $child in $node/node()
return
frc:remove-comments($child)
}
};

(: Copyright © 2024 by Amanda Galtman. :)
(: Copyright © 2024 by Amanda Galtman. :)
7 changes: 4 additions & 3 deletions src/helper-comments/helper-remove-comments.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
-->

<!--
Removes certain comments from $node, assumed to be an element,
Remove certain comments from $node, assumed to be an element,
document node, or empty sequence. The comments to remove are those
whose space-normalized content starts with 'TEST NOTE:'.
-->
<xsl:function name="frc:remove-comments" as="node()" visibility="public">
<xsl:param name="element-or-document-node" as="node()"/>
<xsl:function name="frc:remove-comments" as="node()?"
visibility="public">
<xsl:param name="element-or-document-node" as="node()?"/>
<xsl:apply-templates select="$element-or-document-node"
mode="mrc:remove-comments"/>
</xsl:function>
Expand Down
6 changes: 3 additions & 3 deletions src/helper-comments/test-target.xqm
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
xquery version "3.1";
xquery version "1.0";
module namespace rpt = "urn:x-xspectacles:functions:report";

(:
Sample Code for "Ignoring Code Comments During XSpec Testing"
https://medium.com/@xspectacles/ignoring-code-comments-during-xspec-testing-a460f35a25bb
Sample Code for "Ignoring Code Comments During XSpec Testing, Part 2"
https://medium.com/@xspectacles/ignoring-code-comments-during-xspec-testing-part-2-8266ee8ceccc
:)

declare function rpt:report(
Expand Down

0 comments on commit c3004e5

Please sign in to comment.