XML technologies / XSLT / XSLT Cookbook / Testing and debugging / Add embedded unit tests to utility stylesheets
(Excerpt from “XSLT Kochbuch” by Sal Mangano)
Problem
You want tests for your utility stylesheets so they can be checked at any time.
Solution
This stylesheet is intended as a utility. It can also test itself by running with itself as the input document:
<!-- math.max.xslt -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:math="http://www.exslt.org/math" exclude-result-prefixes="math" xmlns:test="http://www.ora.com/XSLTCookbook/test" id="math:math.max">
<xsl:template name="math:max">
<xsl:param name="nodes" select="/.."/>
<xsl:param name="max"/>
<xsl:variable name="count" select="count($nodes)"/>
<xsl:variable name="aNode" select="$nodes[ceiling($count div 2)]"/>
<xsl:choose>
<xsl:when test="not($count)">
<xsl:value-of select="number($max)"/>
</xsl:when>
<xsl:when test="number($aNode) != number($aNode)">
<xsl:value-of select="number($aNode)"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="math:max">
<xsl:with-param name="nodes" select="$nodes[not(. <= number($aNode))]"/>
<xsl:with-param name="max">
<xsl:choose>
<xsl:when test="not($max) or $aNode > $max">
<xsl:value-of select="$aNode"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$max"/>
</xsl:otherwise>
</xsl:choose>
</xsl:with-param>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- TEST-CODE: NICHT ENTFERNEN! -->
<xsl:template match="/xsl:stylesheet[@id='math:math.max'] | xsl:include[@href='math.max.xslt'] " priority="-1000">
<xsl:message>TESTEN von math.max</xsl:message>
<xsl:for-each select="document('')/*/test:test">
<xsl:variable name="ans">
<xsl:call-template name="math:max">
<xsl:with-param name="nodes" select="test:data"/>
</xsl:call-template>
</xsl:variable>
<xsl:if test="$ans != @ans">
<xsl:message>math:max TEST von <xsl:value-of select="@num"/> FEHLGESCHLAGEN [<xsl:value-of select="$ans"/>]</xsl:message>
</xsl:if>
</xsl:for-each>
<!-- Test auf unendlich -->
<xsl:variable name="ans1">
<xsl:call-template name="math:max">
<xsl:with-param name="nodes" select="document('')/*/test:test[@num=1]/test:data"/>
<xsl:with-param name="max" select="1 div 0"/>
</xsl:call-template>
</xsl:variable>
<xsl:if test="$ans1 != Infinity">
<xsl:message>math:max TEST auf unendlich FEHLGESCHLAGEN [<xsl:value-of select="$ans1"/>]</xsl:message>
</xsl:if>
<!-- Test auf -unendlich -->
<xsl:variable name="ans2">
<xsl:call-template name="math:max">
<xsl:with-param name="nodes" select="document('')/*/test:test[@num=1]/test:data"/>
<xsl:with-param name="max" select="-1 div 0"/>
</xsl:call-template>
</xsl:variable>
<xsl:if test="$ans2 != document('')/*/test:test[@num=1]/@ans">
<xsl:message>math:max TEST auf -unendlich FEHLGESCHLAGEN [<xsl:value-of select="$ans2"/>]</xsl:message>
</xsl:if>
</xsl:template>
<test:test num="1" ans="9" xmlns="http://www.ora.com/XSLTCookbook/test">
<data>9</data>
<data>8</data>
<data>7</data>
<data>6</data>
<data>5</data>
<data>4</data>
<data>3</data>
<data>2</data>
<data>1</data>
</test:test>
<test:test num="2" ans="1" xmlns="http://www.ora.com/XSLTCookbook/test">
<data>1</data>
</test:test>
<test:test num="3" ans="1" xmlns="http://www.ora.com/XSLTCookbook/test">
<data>-1</data>
<data>1</data>
</test:test>
<test:test num="4" ans="0" xmlns="http://www.ora.com/XSLTCookbook/test">
<data>0</data>
<data>0</data>
</test:test>
<test:test num="5" ans="NaN" xmlns="http://www.ora.com/XSLTCookbook/test">
<data>foo</data>
<data>1</data>
</test:test>
<test:test num="6" ans="NaN" xmlns="http://www.ora.com/XSLTCookbook/test">
<data>1</data>
<data>foo</data>
</test:test>
<test:test num="7" ans="NaN" xmlns="http://www.ora.com/XSLTCookbook/test"></test:test>
</xsl:stylesheet>Discussion
Element xsl:stylesheet has an optional attribute named id, normally identifying stylesheets embedded in larger documents. Here the ID is used for testing. Test code should not interfere with normal use. Create a template that matches only when the stylesheet processes itself:
<xsl:template match="/xsl:stylesheet[@id='math:math.max'] | xsl:include[@href='math.max.xslt']">This explains /xsl:stylesheet[@id='math:math.max'], but what does xsl:include[@href='math.max.xslt'] do? To see its value, here is a stylesheet combining your mathematical utilities in one easily imported file. You also want a simple way to test the entire package:
<!-- math.xslt -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:math="http://exslt.org/math" extension-element-prefixes="math" id="math:math">
<xsl:include href="math.abs.xslt"/>
<xsl:include href="math.constant.xslt"/>
<xsl:include href="math.exp.xslt"/>
<xsl:include href="math.highest.xslt"/>
<xsl:include href="math.log.xslt"/>
<xsl:include href="math.lowest.xslt"/>
<xsl:include href="math.max.xslt"/>
<xsl:include href="math.min.xslt"/>
<xsl:include href="math.power.xslt"/>
<xsl:include href="math.sqrt.xslt"/>
<!-- TEST-CODE -->
<xsl:template match="xsl:stylesheet[@id='math:math'] | xsl:include[@href='math.xslt']">
<xsl:message>TESTEN von math</xsl:message>
<xsl:for-each select="document('')/*/xsl:include">
<xsl:apply-templates select="."/>
</xsl:for-each>
</xsl:template>
<xsl:template match="xsl:include" priority="-10">
<xsl:message>WARNUNG: <xsl:value-of select="@href"/> enthält keinen Testcode.</xsl:message>
</xsl:template>
</xsl:stylesheet>The package’s test code uses a loop to visit every xsl:include element and apply templates. The previously mentioned xsl:include[@href='filename'] in the match makes each stylesheet’s test run.
Pay special attention to template <xsl:template match="xsl:include" priority="-10">. It warns when an imported file has no tests, an important quality-assurance measure because tests are easily forgotten.
If you prefer keeping tests separate, create a test file for each utility. There is then no need for stylesheet attribute id; simply match the root:
<!-- math.max.test.xslt-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:math="http://www.exslt.org/math" exclude-result-prefixes="math" xmlns:test="http://www.ora.com/XSLTCookbook/test">
<xsl:include href="../math/math.max.xslt"/>
<!-- TEST-CODE: NICHT ENTFERNEN! -->
<xsl:template match="/ | xsl:include[@href='math.max.test.xslt']">
<xsl:message>TESTING math.max</xsl:message>
<xsl:for-each select="document('')/*/test:test">
<xsl:variable name="ans">
<xsl:call-template name="math:max">
<xsl:with-param name="nodes" select="test:data"/>
</xsl:call-template>
</xsl:variable>
<xsl:if test="$ans != @ans">
<xsl:message>math:max TEST von <xsl:value-of select="@num"/> FEHLGESCHLAGEN [<xsl:value-of select="$ans"/>]</xsl:message>
</xsl:if>
</xsl:for-each>
<!-- ... Identisch mit math.max.xslt oben ... -->
</xsl:stylesheet>Then create separate test packages:
<!-- math.test.xslt -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:math="http://exslt.org/math" extension-element-prefixes="math">
<xsl:include href="math.max.test.xslt"/>
<xsl:include href="math.min.test.xslt"/>
<!-- ... Identisch mit math.xslt, oben ... -->
</xsl:stylesheet>If tests are separated, ship them with the real implementation so customers can run them themselves. Tests also demonstrate how the templates are used.
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XSLT Kochbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de