Table of Contents |
Recipe 15.6. Testing Boundary and Error ConditionsProblemYou are writing utility templates to be used by others, and you want them to be robust. SolutionBoundary-condition testingIn all programming languages, bugs most often appear at boundary conditions. Thus, you should choose test data in which values lie along data extremes. Boundary values include maximum, minimum, and just inside/outside boundaries. If your templates work correctly for these special values, then they will probably work correctly for all other values. It is impossible to provide an exhaustive list of boundary conditions because they vary from problem to problem. Next is a list of typical cases you should consider. If a template acts on node sets (sequences in 2.0), then be sure to test the following cases:
If a template acts on a string, be sure to test the following cases:
If your template uses substring-before or substring-after for searches, be sure to test the following cases:
If a template acts on numbers, be sure to test:
If a template compares two numbers X and Y, be sure to test cases in which:
When you know or have access to the schema of a document that a stylesheet will process, be sure to test inputs where:
Error-condition testingA robust reusable template or stylesheet should fail gracefully in the face of erroneous input. Here you often use xsl:message with terminate=yes to report illegal parameter values. If a template acts on numbers, be sure to test error handling for:
When templates or stylesheets use parameters, be sure to test what happens when:
You can check for parameters that aren't set by using the following trick: <xsl:param name="param1"> <xsl:message terminate="yes"> $param1 has not been set. </xsl:message> </xsl:param> However, this trick is not guaranteed to work because nothing in the standard setup states the value of a parameter not evaluated if a value is passed for that parameter. However, most XSLT processors are friendly to this technique. If you wanted to be absolutely safe, you could set the value to an illegal value (such as 1 div 0) and test for it in the body of the template: <xsl:param name="param1" select="1 div 0" /> <xsl:if test="$param1 = 1 div 0"> <xsl:message terminate="yes"> $param1 has not been set, or has been set to Infinity, which is invalid. </xsl:message> </xsl:if> When you know or have access to the schema of a document a stylesheet is expected to process, see how the stylesheet responds to documents that:[1]
DiscussionWhen developing XSLT for your own consumption, you are free to choose just how robust you want the code to be. However, when others use your code, it is a good practice to include reasonable error handling. Your clients will also thank you for delivering code that works for legal but unusual input. When you create templates that use recursion, dividing the implementation into two templates is a good idea. The main template does the error checking and, if no errors are detected, calls an implementation template that computes the result recursively. Recipe Recipe 3.5 used this tactic for logarithms. |
Table of Contents |