XML technologies / XSLT / XSLT Cookbook / Testing and debugging / Testing boundary and error conditions
(Excerpt from “XSLT Kochbuch” by Sal Mangano)
Problem
You are writing utility templates for others and want them to be as robust as possible.
Solution
Testing boundary conditions
In any programming language, errors most often occur at boundaries. Choose test data covering extreme values: maximum and minimum, inside and outside. Templates working correctly for these special values will probably work for others too. No exhaustive list is possible because boundaries depend on the problem. Here are typical cases to check.
If a template processes node sets, or sequences in 2.0, test:
an empty node set
a node set with one element
a node set with two elements
an odd number of elements greater than 1
an even number of elements greater than 2
If a template processes a string, test:
the empty string
a string of length 1
other strings of different lengths
If a template uses substring-before or substring-after for searching, test:
strings without the search string
strings starting with the search string
strings ending with the search string
strings consisting only of the search string
strings with multiple occurrences, adjacent and separated by other text
If a template processes numbers, test:
the number 0
the number 1
negative numbers
fractions (0 < x < 1)
numbers with integer and fractional parts
prime numbers
other problem-specific boundary values
When comparing two numbers X and Y, test cases where:
X < Y, especially X = Y - 1 and X = Y - d, where d is a small fraction
X = Y
X > Y, especially X = Y + 1 and X = Y + d, where d is a small fraction
If you know or can access the processed document’s schema, test inputs where:
optional elements are absent
optional elements are present
unbounded elements have only one instance
unbounded elements have several instances
Testing error conditions
A robust, reusable template or stylesheet should terminate gracefully on invalid input. Normally, use xsl:message with terminate="yes" to detect invalid parameter values.
For numeric templates, test error handling for:
NaN (0 div 0)
infinity (1 div 0)
negative infinity (-1 div 0)
zero where undefined, for example logarithms
negative numbers where undefined, for example factorials
nonnumeric input, such as “blabla”
If templates or stylesheets use parameters, test what happens when:
parameters without defaults are unset
parameter values are out of range
parameter values have the wrong type
Detect unset parameters with this trick:
<xsl:param name="param1">
<xsl:message terminate="yes">$param1 wurde nicht gesetzt.</xsl:message>
</xsl:param>This is not guaranteed: the standard does not specify preserving an unevaluated default when a parameter value is supplied. Most processors nevertheless support this technique. For certainty, set the parameter to an invalid value, such as 1 div 0, and test it inside the template:
<xsl:param name="param1" select="1 div 0" />
<xsl:if test="$param1 = 1 div 0">
<xsl:message terminate="yes">$param1 wurde nicht gesetzt, oder er wurde auf unendlich gesetzt, was ungültig ist.</xsl:message>
</xsl:if>If you know or can access the processed document’s schema, check the stylesheet’s response to documents that meet the following conditions. This assumes the XSLT processor uses a nonvalidating parser, or that you remove the schema reference from the input.
violate the schema entirely, such as a completely different XML document
contain some schema-invalid elements
violate the
minOccursandmaxOccursconstraintsviolate datatype constraints
Discussion
For code used only by yourself, choose the level of robustness freely. If others use it, include reasonable error handling. Your users will appreciate code that handles valid input even when unusual.
For recursive templates, split the implementation into two templates. The main template checks errors and, if none occur, calls an implementation template that computes recursively. The recipe Implementing common mathematical functions uses this technique for logarithms.
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