Tecnologías XML / XSLT / Recetario XSLT / Pruebas y depuración / Añadir pruebas unitarias integradas a hojas auxiliares

(Extracto de «XSLT Kochbuch» de Sal Mangano)

Problema

Desea disponer de pruebas para comprobar sus hojas auxiliares en cualquier momento.

Solución

Esta hoja está pensada como utilidad, pero también puede probarse utilizando la propia hoja como documento de entrada:

<!-- 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 &gt; $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>

Discusión

El elemento xsl:stylesheet tiene un atributo opcional llamado id, que normalmente identifica hojas integradas en documentos mayores. Aquí el identificador sirve para pruebas que no deben interferir con el uso normal. Cree una plantilla que solo coincida cuando la hoja se procese a sí misma:

<xsl:template match="/xsl:stylesheet[@id='math:math.max'] | xsl:include[@href='math.max.xslt']">

Esto explica /xsl:stylesheet[@id='math:math.max'], pero ¿qué hace xsl:include[@href='math.max.xslt']? Esta hoja reúne las utilidades matemáticas en un archivo fácil de importar. También necesita una forma sencilla de probar todo el paquete:

<!-- 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>

El código de prueba utiliza un bucle para recorrer todos sus elementos xsl:include y aplicar plantillas. La parte xsl:include[@href='filename'] ya mencionada en el patrón hace que se ejecute la prueba de cada hoja.

Preste especial atención a la plantilla <xsl:template match="xsl:include" priority="-10">. Avisa si un archivo importado no tiene pruebas, una medida importante de calidad porque es fácil olvidarlas.

Si prefiere separar las pruebas, cree un archivo para cada utilidad. No hace falta entonces el atributo de hoja id; basta con hacer coincidir la raíz:

<!-- 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>

Después cree paquetes de pruebas separados:

<!-- 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>

Si separa las pruebas, distribúyalas con la implementación para que los clientes puedan ejecutarlas. También muestran cómo utilizar las plantillas.

  

<< atrás

siguiente >>

 

 

 

volver arriba