Skip to content

Commit

Permalink
Test and documentation for numeral-conversion.xsl
Browse files Browse the repository at this point in the history
changes to numeral-conversion.xsl:
- comments for readability/future documentation
- line 21 formatting for readability

new test, numeral-conversion.xspec
  • Loading branch information
mccallum-sgd authored and bertfrees committed Nov 19, 2018
1 parent 24a9e91 commit 531d4e1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
12 changes: 10 additions & 2 deletions common-utils/src/main/resources/xml/xslt/numeral-conversion.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@

<xsl:function name="pf:numeric-is-roman" as="xs:boolean">
<xsl:param name="roman" as="xs:string"/>
<!-- Check each char in roman string if upper-case roman numeral (i.e. part of set: 'MDCLXVI') -->
<xsl:value-of select="not((for $char in string-to-codepoints($roman) return contains('MDCLXVI',upper-case(codepoints-to-string($char)))) = false())"/>
</xsl:function>

<xsl:function name="pf:numeric-roman-to-decimal" as="xs:integer">
<xsl:param name="roman" as="xs:string"/>
<!-- TODO: throw error for strings containing characters other than MDCLXVI (case insensitive), the seven characters still in use. -->
<!-- replace numerals with their values -->
<xsl:variable name="hindu-sequence"
select="for $char in string-to-codepoints($roman) return
number(replace(replace(replace(replace(replace(replace(replace(upper-case(codepoints-to-string($char)),'I','1'),'V','5'),'X','10'),'L','50'),'C','100'),'D','500'),'M','1000'))"/>
number(replace(replace(replace(replace(replace(replace(replace(upper-case(codepoints-to-string($char)),'I','1'),'V','5'),'X','10'),'L','50'),'C','100'),'D','500'),'M','1000'))"/>
<!-- sign values -->
<xsl:variable name="hindu-sequence-signed"
select="for $i in 1 to count($hindu-sequence) return if (subsequence($hindu-sequence,$i+1) &gt; $hindu-sequence[$i]) then -$hindu-sequence[$i] else $hindu-sequence[$i]"/>
select="for $i in 1 to count($hindu-sequence) return
if (subsequence($hindu-sequence,$i+1) &gt; $hindu-sequence[$i]) then
-$hindu-sequence[$i]
else
$hindu-sequence[$i]"/>
<!-- sum values -->
<xsl:value-of select="sum($hindu-sequence-signed)"/>
</xsl:function>

Expand Down
55 changes: 55 additions & 0 deletions common-utils/src/test/xspec/numeral-conversion.xspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<x:description xmlns:x="http://www.jenitennison.com/xslt/xspec" stylesheet="../../main/resources/xml/xslt/numeral-conversion.xsl" xmlns:pf="http://www.daisy.org/ns/pipeline/functions" xmlns:f="http://www.daisy.org/ns/pipeline/internal-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<x:scenario label="testing pf:numeric-is-roman with valid roman numerals">
<x:call function="pf:numeric-is-roman">
<x:param name="roman" select="'MMDCLXXVIII'" as="xs:string"/>
</x:call>
<x:expect label="result should be true" select="true()"/>
</x:scenario>

<x:scenario label="testing pf:numeric-is-roman with invalid roman numerals">
<x:call function="pf:numeric-is-roman">
<x:param name="roman" select="'MMDCuLXXVIII'" as="xs:string"/>
</x:call>
<x:expect label="result should be false" select="false()"/>
</x:scenario>

<x:scenario label="testing pf:numeric-roman-to-decimal with valid roman numerals">
<x:call function="pf:numeric-roman-to-decimal">
<x:param name="roman" select="'MMDCLXXVIII'" as="xs:string"/>
</x:call>
<x:expect label="result should be 2678" select="2678"/>
</x:scenario>

<!-- PENDING - IN PROGRESS, WAITING ON TODO -->
<x:scenario label="testing pf:numeric-roman-to-decimal with invalid roman numerals" pending="true">
<x:call function="pf:numeric-roman-to-decimal">
<x:param name="roman" select="'MMDCuLXXVIII'"/>
</x:call>
<x:expect label="should result in error" select=""/>
</x:scenario>

<x:scenario label="testing pf:numeric-decimal-to-roman with valid decimal">
<x:call function="pf:numeric-decimal-to-roman">
<x:param name="roman" select="2678" as="xs:integer"/>
</x:call>
<x:expect label="result should be MMDCLXXVIII" select="'MMDCLXXVIII'"/>
</x:scenario>

<!-- PENDING - IN PROGRESS, WAITING ON TODO -->
<x:scenario label="testing pf:numeric-decimal-to-roman with non-positive decimal" pending="true">
<x:call function="pf:numeric-roman-to-decimal">
<x:param name="roman" select="2678" as="xs:integer"/>
</x:call>
<x:expect label="should result in error" select=""/>
</x:scenario>

<!-- PENDING - IN PROGRESS -->
<x:scenario label="testing of pf:numeric-alpha-to-decimal" pending="true">
<x:call function="pf:numeric-alpha-to-decimal">
<x:param name="alpha" select="'¥Î'" as="xs:string"/>
</x:call>
<x:expect label="result should be 165206" select="165206"/>
</x:scenario>
</x:description>

0 comments on commit 531d4e1

Please sign in to comment.