Unit Testing in XSLT 2.0 Norman Walsh Sun Microsystems, Inc. Table - - PowerPoint PPT Presentation

unit testing in xslt 2 0
SMART_READER_LITE
LIVE PREVIEW

Unit Testing in XSLT 2.0 Norman Walsh Sun Microsystems, Inc. Table - - PowerPoint PPT Presentation

Unit Testing in XSLT 2.0 Norman Walsh Sun Microsystems, Inc. Table of Contents Using unit testing to improve stylesheet quality and docu- mentation. Background Motivation Survey of the framework Demo http://www.sun.com/ 2


slide-1
SLIDE 1

Unit Testing in XSLT 2.0

Norman Walsh Sun Microsystems, Inc.

slide-2
SLIDE 2

Using unit testing to improve stylesheet quality and docu- mentation.

  • Background
  • Motivation
  • Survey of the framework
  • Demo

2 / 34 http://www.sun.com/

Table of Contents

slide-3
SLIDE 3
  • The DocBook XSL (1.0) stylesheets for HTML and FO con-

sist of roughly:

>2,500 match templates >800 named templates >250 modes >115 modules >62,000 lines >485 parameters

  • Development is a collaborative effort
  • The document test suite contains 200 or so test documents

3 / 34 http://www.sun.com/

Background

slide-4
SLIDE 4
  • 200 test documents isn't sufficient
  • Elements occur in many contexts
  • Markup and stylesheet parameters interact
  • Comparing documents is slow and tedious
  • Comparing documents often yields false negatives
  • Would 2,000 tests be sufficient? 20,000?
  • Would they be practical?

4 / 34 http://www.sun.com/

Integration testing

slide-5
SLIDE 5
  • Motivated by development of XSLT 2.0 stylesheets for

DocBook

  • Based on experience from development of XSLT 1.0

stylesheets for DocBook

  • Attempt to build a testing framework
  • Not a survey of available frameworks

5 / 34 http://www.sun.com/

Can we do better?

slide-6
SLIDE 6
  • Many modern software development paradigms stress

early and frequent testing

  • To make sure your code works
  • To ease the burden of maintenance
  • To make collaboration easier

6 / 34 http://www.sun.com/

Why do you test?

slide-7
SLIDE 7
  • Integration testing: run the stylesheet over a suite of test

documents and see what happens. Problems:

>Comparison is difficult >Coverage is incomplete >Coverage is too coarse

  • Unit testing: test individual modules, in the XSLT case,

individual templates and functions.

7 / 34 http://www.sun.com/

How can you test?

slide-8
SLIDE 8

In XSLT 2.0, we want to be able to test:

  • Individual named templates
  • Individual user-defined functions
  • Match templates in specific contexts

8 / 34 http://www.sun.com/

What can you test?

slide-9
SLIDE 9

Consider this function: <xsl:function name="f:basename" as="xs:string"> <xsl:param name="filename" as="xs:string"/> <xsl:value-of select="tokenize($filename,'/')[last()]"/> </xsl:function> And this test: <xsl:if test="f:basename('/path/to/my/file.ext') = 'file.ext'">PASS</xsl:if> Prints “PASS” (or nothing)

9 / 34 http://www.sun.com/

A first attempt

slide-10
SLIDE 10

But what about the tests that failed?

10 / 34 http://www.sun.com/

A first attempt (Continued)

slide-11
SLIDE 11

<xsl:if test="f:basename('/path/to/my/file.ext') != 'file.ext'">FAIL</xsl:if> Prints “FAIL” (or nothing)

11 / 34 http://www.sun.com/

A second attempt

slide-12
SLIDE 12

<xsl:if test="f:basename('/path/to/my/file.ext') != 'file.ext'">f:basename('/path/to/my/file.ext') FAIL</xsl:if> Prints “f:basename('/path/to/my/file.ext') FAIL” (or nothing)

12 / 34 http://www.sun.com/

A third attempt

slide-13
SLIDE 13

<xsl:choose> <xsl:when test="f:basename('/path/to/my/file.ext') = 'file.ext'"> <xsl:text>f:basename('/path/to/my/file.ext') PASS</xsl:text> </xsl:when> <xsl:otherwise> <xsl:text>f:basename('/path/to/my/file.ext') FAIL</xsl:text> </xsl:otherwise> </xsl:choose>

  • Ok. Now imagine several hundred tests like that, or several

thousand.

13 / 34 http://www.sun.com/

A fourth attempt

slide-14
SLIDE 14

The tests are:

  • Hard to write
  • Hard to read
  • Are more complex to write if the arguments or results are

nodes or atomic values other than strings

  • Are even more complex if they need to test in a specific

context (e.g. a footnote inside a title).

14 / 34 http://www.sun.com/

What's wrong?

slide-15
SLIDE 15
  • Make the tests easy to write
  • Make the tests easy to read
  • Support atomic values and nodes as input and output
  • Support tests that require a specific context

15 / 34 http://www.sun.com/

Challenges

slide-16
SLIDE 16
  • Keep tests (and documentation) close to the actual code

>Not quite literate programming

  • Minimize the amount of boilerplate in the tests
  • Work within the constraints imposed by XSLT

16 / 34 http://www.sun.com/

Meeting the challenges

slide-17
SLIDE 17

<xsl:stylesheet> <doc:function name="f:function-name"> ···documentation··· </doc:function> <u:unittests name="f:function-name"> ···tests··· </u:unittests> <xsl:function name="f:function-name"> ···code··· </xsl:function>

17 / 34 http://www.sun.com/

The test harness

slide-18
SLIDE 18

<xsl:stylesheet>

18 / 34 http://www.sun.com/

The test harness (Continued)

slide-19
SLIDE 19

<u:unittests function="f:node-id"> <u:param name="persistent.generated.ids" select="0"/> <u:test> <u:param><db:anchor id='id'/></u:param> <u:result>'id'</u:result> </u:test> </u:unittests>

19 / 34 http://www.sun.com/

Focus of this talk

slide-20
SLIDE 20

The unittests element:

  • Identifies the template or function being tested
  • Groups a collection of related tests
  • Specifies the value of any top-level parameters related to

those tests <u:unittests function="..."> <u:param>...</u:param> <u:test>...</u:test> <u:test>...</u:test> </u:unittests>

20 / 34 http://www.sun.com/

The unittests element

slide-21
SLIDE 21

The test element:

  • Describes an individual test
  • Specifies arguments to the artifact being tested
  • May specify variables
  • Specifies the expected result

<u:test> <u:param>...</u:param> <u:result>...</u:result> </u:test>

21 / 34 http://www.sun.com/

The test element

slide-22
SLIDE 22
  • The u:variable and u:param elements are semantic

clones of the xsl:param element

  • Each must have a name attribute
  • The value of a variable or parameter is specified with either

a select attribute or as content

  • The as attribute can be used to specify the type

22 / 34 http://www.sun.com/

Test parameters and variables

slide-23
SLIDE 23

To select part of a structure and pass it as an argument: <u:test> <u:variable name="mydoc"> <db:book> <db:title>Some Title</db:title> <db:chapter> <db:title>Some Chapter Title</db:title> <db:para>My para.</db:para> </db:chapter> </db:book> </u:variable> <u:param select="$mydoc//db:para[1]"/>

23 / 34 http://www.sun.com/

Why variables?

slide-24
SLIDE 24

<u:result>'R.1.2.2'</u:result> </u:test>

24 / 34 http://www.sun.com/

Why variables? (Continued)

slide-25
SLIDE 25
  • Results are expressed with result
  • Always expressed in element content
  • Literal strings must be quoted

25 / 34 http://www.sun.com/

Test results

slide-26
SLIDE 26
  • Parameters and variables are insufficient for testing named

templates

  • Named templates often expect to be called in a particular

context

  • The context element in a test specifies the context.

26 / 34 http://www.sun.com/

Establishing context

slide-27
SLIDE 27

<u:test> <u:context> <db:varname xml:id="varfoo">someVarName</db:varname> </u:context> <u:result> <span xmlns="http://www.w3.org/1999/xhtml" class="varname" id="varfoo">someVarName</span> </u:result> </u:test>

27 / 34 http://www.sun.com/

Context example

slide-28
SLIDE 28
  • 1. The context is stored in a variable with an automatically

generated, unique name.

  • 2. Instead of calling the named template directly, xsl:ap-

ply-templates is performed on that variable in an automatically generated, unique mode.

  • 3. Finally, a top-level template is generated which matches

node() in the appropriate mode. The call to the named template is placed inside that template. The practical effect of this three-step process is to change the context node to the specified context and call the named template in that context.

28 / 34 http://www.sun.com/

How does context work?

slide-29
SLIDE 29
  • Establishing a context is a necessary step in testing a

match pattern, but it is not a sufficient step.

  • Simply calling apply-templates can not guarantee that the

correct template will be evaluated because the stylesheet processor will select the template based on the match pattern, priority, mode, and input precedence.

  • To overcome this obstacle, the framework creates a copy
  • f the template that should be tested and gives it an auto-

matically generated, unique name. It can then call the template directly using that name.

29 / 34 http://www.sun.com/

Testing match patterns

slide-30
SLIDE 30

This methodology has drawbacks:

  • The framework selects a template based on the match

pattern, mode, and priority. Sometimes that's insufficient, in which case the framework simply reports “indeterminate results”.

  • The framework's exact, textual match is more restrictive

than the real processor which would see no distinction between priority="1" and priority="01".

  • It is possible to force a template to be applied in a context

that it can never match in the real stylesheet.

30 / 34 http://www.sun.com/

Testing match patterns

slide-31
SLIDE 31
  • Running the tests and constructing the report is a mostly-

automated process

  • In order to allow tests to specify different top-level

stylesheet parameters, it is often necessary to transform the collection of unit tests into several stylesheets

  • After the stylesheets have been written, each must be run
  • Each stylesheet produces a fragment of XHTML that de-

scribes the test results

  • These fragments are then assembled to produce the final

report

31 / 34 http://www.sun.com/

Using the test harness

slide-32
SLIDE 32
  • Transform the stylesheet with writetests.xsl
  • Run each testn.xsl stylesheet

>The input is irrelevant

  • Assemble the results with assembletests.xsl
  • view the result in your browser of choice

32 / 34 http://www.sun.com/

Running the tests

slide-33
SLIDE 33
  • In which norm tempts the fates…and Murphy.

33 / 34 http://www.sun.com/

Demo

slide-34
SLIDE 34
  • It's all checked into the DocBook project at SourceForge:

>http://sourceforge.net/projects/docbook

  • It's not in any distribution yet, but it's in CVS:

>/xsl2/tools/

34 / 34 http://www.sun.com/

Getting the bits